libfcoe.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
  4. * Copyright (c) 2007-2008 Intel Corporation. All rights reserved.
  5. *
  6. * Maintained at www.Open-FCoE.org
  7. */
  8. #ifndef _LIBFCOE_H
  9. #define _LIBFCOE_H
  10. #include <linux/etherdevice.h>
  11. #include <linux/if_ether.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/local_lock.h>
  16. #include <linux/prandom.h>
  17. #include <scsi/fc/fc_fcoe.h>
  18. #include <scsi/libfc.h>
  19. #include <scsi/fcoe_sysfs.h>
  20. #define FCOE_MAX_CMD_LEN 16 /* Supported CDB length */
  21. /*
  22. * Max MTU for FCoE: 14 (FCoE header) + 24 (FC header) + 2112 (max FC payload)
  23. * + 4 (FC CRC) + 4 (FCoE trailer) = 2158 bytes
  24. */
  25. #define FCOE_MTU 2158
  26. /*
  27. * FIP tunable parameters.
  28. */
  29. #define FCOE_CTLR_START_DELAY 2000 /* mS after first adv. to choose FCF */
  30. #define FCOE_CTLR_SOL_TOV 2000 /* min. solicitation interval (mS) */
  31. #define FCOE_CTLR_FCF_LIMIT 20 /* max. number of FCF entries */
  32. #define FCOE_CTLR_VN2VN_LOGIN_LIMIT 3 /* max. VN2VN rport login retries */
  33. /**
  34. * enum fip_state - internal state of FCoE controller.
  35. * @FIP_ST_DISABLED: controller has been disabled or not yet enabled.
  36. * @FIP_ST_LINK_WAIT: the physical link is down or unusable.
  37. * @FIP_ST_AUTO: determining whether to use FIP or non-FIP mode.
  38. * @FIP_ST_NON_FIP: non-FIP mode selected.
  39. * @FIP_ST_ENABLED: FIP mode selected.
  40. * @FIP_ST_VNMP_START: VN2VN multipath mode start, wait
  41. * @FIP_ST_VNMP_PROBE1: VN2VN sent first probe, listening
  42. * @FIP_ST_VNMP_PROBE2: VN2VN sent second probe, listening
  43. * @FIP_ST_VNMP_CLAIM: VN2VN sent claim, waiting for responses
  44. * @FIP_ST_VNMP_UP: VN2VN multipath mode operation
  45. */
  46. enum fip_state {
  47. FIP_ST_DISABLED,
  48. FIP_ST_LINK_WAIT,
  49. FIP_ST_AUTO,
  50. FIP_ST_NON_FIP,
  51. FIP_ST_ENABLED,
  52. FIP_ST_VNMP_START,
  53. FIP_ST_VNMP_PROBE1,
  54. FIP_ST_VNMP_PROBE2,
  55. FIP_ST_VNMP_CLAIM,
  56. FIP_ST_VNMP_UP,
  57. };
  58. /*
  59. * Modes:
  60. * The mode is the state that is to be entered after link up.
  61. * It must not change after fcoe_ctlr_init() sets it.
  62. */
  63. enum fip_mode {
  64. FIP_MODE_AUTO,
  65. FIP_MODE_NON_FIP,
  66. FIP_MODE_FABRIC,
  67. FIP_MODE_VN2VN,
  68. };
  69. /**
  70. * struct fcoe_ctlr - FCoE Controller and FIP state
  71. * @state: internal FIP state for network link and FIP or non-FIP mode.
  72. * @mode: LLD-selected mode.
  73. * @lp: &fc_lport: libfc local port.
  74. * @sel_fcf: currently selected FCF, or NULL.
  75. * @fcfs: list of discovered FCFs.
  76. * @cdev: (Optional) pointer to sysfs fcoe_ctlr_device.
  77. * @fcf_count: number of discovered FCF entries.
  78. * @sol_time: time when a multicast solicitation was last sent.
  79. * @sel_time: time after which to select an FCF.
  80. * @port_ka_time: time of next port keep-alive.
  81. * @ctlr_ka_time: time of next controller keep-alive.
  82. * @timer: timer struct used for all delayed events.
  83. * @timer_work: &work_struct for doing keep-alives and resets.
  84. * @recv_work: &work_struct for receiving FIP frames.
  85. * @fip_recv_list: list of received FIP frames.
  86. * @flogi_req: clone of FLOGI request sent
  87. * @rnd_state: state for pseudo-random number generator.
  88. * @port_id: proposed or selected local-port ID.
  89. * @user_mfs: configured maximum FC frame size, including FC header.
  90. * @flogi_oxid: exchange ID of most recent fabric login.
  91. * @flogi_req_send: send of FLOGI requested
  92. * @flogi_count: number of FLOGI attempts in AUTO mode.
  93. * @map_dest: use the FC_MAP mode for destination MAC addresses.
  94. * @fip_resp: start FIP VLAN discovery responder
  95. * @spma: supports SPMA server-provided MACs mode
  96. * @probe_tries: number of FC_IDs probed
  97. * @priority: DCBx FCoE APP priority
  98. * @dest_addr: MAC address of the selected FC forwarder.
  99. * @ctl_src_addr: the native MAC address of our local port.
  100. * @send: LLD-supplied function to handle sending FIP Ethernet frames
  101. * @update_mac: LLD-supplied function to handle changes to MAC addresses.
  102. * @get_src_addr: LLD-supplied function to supply a source MAC address.
  103. * @ctlr_mutex: lock protecting this structure.
  104. * @ctlr_lock: spinlock covering flogi_req
  105. *
  106. * This structure is used by all FCoE drivers. It contains information
  107. * needed by all FCoE low-level drivers (LLDs) as well as internal state
  108. * for FIP, and fields shared with the LLDS.
  109. */
  110. struct fcoe_ctlr {
  111. enum fip_state state;
  112. enum fip_mode mode;
  113. struct fc_lport *lp;
  114. struct fcoe_fcf *sel_fcf;
  115. struct list_head fcfs;
  116. struct fcoe_ctlr_device *cdev;
  117. u16 fcf_count;
  118. unsigned long sol_time;
  119. unsigned long sel_time;
  120. unsigned long port_ka_time;
  121. unsigned long ctlr_ka_time;
  122. struct timer_list timer;
  123. struct work_struct timer_work;
  124. struct work_struct recv_work;
  125. struct sk_buff_head fip_recv_list;
  126. struct sk_buff *flogi_req;
  127. struct rnd_state rnd_state;
  128. u32 port_id;
  129. u16 user_mfs;
  130. u16 flogi_oxid;
  131. u8 flogi_req_send;
  132. u8 flogi_count;
  133. bool map_dest;
  134. bool fip_resp;
  135. u8 spma;
  136. u8 probe_tries;
  137. u8 priority;
  138. u8 dest_addr[ETH_ALEN];
  139. u8 ctl_src_addr[ETH_ALEN];
  140. void (*send)(struct fcoe_ctlr *, struct sk_buff *);
  141. void (*update_mac)(struct fc_lport *, u8 *addr);
  142. u8 * (*get_src_addr)(struct fc_lport *);
  143. struct mutex ctlr_mutex;
  144. spinlock_t ctlr_lock;
  145. };
  146. /**
  147. * fcoe_ctlr_priv() - Return the private data from a fcoe_ctlr
  148. * @ctlr: The fcoe_ctlr whose private data will be returned
  149. *
  150. * Returns: pointer to the private data
  151. */
  152. static inline void *fcoe_ctlr_priv(const struct fcoe_ctlr *ctlr)
  153. {
  154. return (void *)(ctlr + 1);
  155. }
  156. /*
  157. * This assumes that the fcoe_ctlr (x) is allocated with the fcoe_ctlr_device.
  158. */
  159. #define fcoe_ctlr_to_ctlr_dev(x) \
  160. (x)->cdev
  161. /**
  162. * struct fcoe_fcf - Fibre-Channel Forwarder
  163. * @list: list linkage
  164. * @event_work: Work for FC Transport actions queue
  165. * @fip: The controller that the FCF was discovered on
  166. * @fcf_dev: The associated fcoe_fcf_device instance
  167. * @time: system time (jiffies) when an advertisement was last received
  168. * @switch_name: WWN of switch from advertisement
  169. * @fabric_name: WWN of fabric from advertisement
  170. * @fc_map: FC_MAP value from advertisement
  171. * @fcf_mac: Ethernet address of the FCF for FIP traffic
  172. * @fcoe_mac: Ethernet address of the FCF for FCoE traffic
  173. * @vfid: virtual fabric ID
  174. * @pri: selection priority, smaller values are better
  175. * @flogi_sent: current FLOGI sent to this FCF
  176. * @flags: flags received from advertisement
  177. * @fka_period: keep-alive period, in jiffies
  178. * @fd_flags: no need for FKA from ENode
  179. *
  180. * A Fibre-Channel Forwarder (FCF) is the entity on the Ethernet that
  181. * passes FCoE frames on to an FC fabric. This structure represents
  182. * one FCF from which advertisements have been received.
  183. *
  184. * When looking up an FCF, @switch_name, @fabric_name, @fc_map, @vfid, and
  185. * @fcf_mac together form the lookup key.
  186. */
  187. struct fcoe_fcf {
  188. struct list_head list;
  189. struct work_struct event_work;
  190. struct fcoe_ctlr *fip;
  191. struct fcoe_fcf_device *fcf_dev;
  192. unsigned long time;
  193. u64 switch_name;
  194. u64 fabric_name;
  195. u32 fc_map;
  196. u16 vfid;
  197. u8 fcf_mac[ETH_ALEN];
  198. u8 fcoe_mac[ETH_ALEN];
  199. u8 pri;
  200. u8 flogi_sent;
  201. u16 flags;
  202. u32 fka_period;
  203. u8 fd_flags:1;
  204. };
  205. #define fcoe_fcf_to_fcf_dev(x) \
  206. ((x)->fcf_dev)
  207. /**
  208. * struct fcoe_rport - VN2VN remote port
  209. * @rdata: libfc remote port private data
  210. * @time: time of create or last beacon packet received from node
  211. * @fcoe_len: max FCoE frame size, not including VLAN or Ethernet headers
  212. * @flags: flags from probe or claim
  213. * @login_count: number of unsuccessful rport logins to this port
  214. * @enode_mac: E_Node control MAC address
  215. * @vn_mac: VN_Node assigned MAC address for data
  216. */
  217. struct fcoe_rport {
  218. struct fc_rport_priv rdata;
  219. unsigned long time;
  220. u16 fcoe_len;
  221. u16 flags;
  222. u8 login_count;
  223. u8 enode_mac[ETH_ALEN];
  224. u8 vn_mac[ETH_ALEN];
  225. };
  226. /* FIP API functions */
  227. void fcoe_ctlr_init(struct fcoe_ctlr *, enum fip_mode);
  228. void fcoe_ctlr_destroy(struct fcoe_ctlr *);
  229. void fcoe_ctlr_link_up(struct fcoe_ctlr *);
  230. int fcoe_ctlr_link_down(struct fcoe_ctlr *);
  231. int fcoe_ctlr_els_send(struct fcoe_ctlr *, struct fc_lport *, struct sk_buff *);
  232. void fcoe_ctlr_recv(struct fcoe_ctlr *, struct sk_buff *);
  233. int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *, struct fc_lport *,
  234. struct fc_frame *);
  235. /* libfcoe funcs */
  236. u64 fcoe_wwn_from_mac(unsigned char mac[ETH_ALEN], unsigned int scheme,
  237. unsigned int port);
  238. int fcoe_libfc_config(struct fc_lport *, struct fcoe_ctlr *,
  239. const struct libfc_function_template *, int init_fcp);
  240. u32 fcoe_fc_crc(struct fc_frame *fp);
  241. int fcoe_start_io(struct sk_buff *skb);
  242. int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type);
  243. void __fcoe_get_lesb(struct fc_lport *lport, struct fc_els_lesb *fc_lesb,
  244. struct net_device *netdev);
  245. void fcoe_wwn_to_str(u64 wwn, char *buf, int len);
  246. int fcoe_validate_vport_create(struct fc_vport *vport);
  247. int fcoe_link_speed_update(struct fc_lport *);
  248. void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
  249. void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev);
  250. /**
  251. * is_fip_mode() - test if FIP mode selected.
  252. * @fip: FCoE controller.
  253. *
  254. * Returns: %true if FIP mode is selected
  255. */
  256. static inline bool is_fip_mode(struct fcoe_ctlr *fip)
  257. {
  258. return fip->state == FIP_ST_ENABLED;
  259. }
  260. /* helper for FCoE SW HBA drivers, can include subven and subdev if needed. The
  261. * modpost would use pci_device_id table to auto-generate formatted module alias
  262. * into the corresponding .mod.c file, but there may or may not be a pci device
  263. * id table for FCoE drivers so we use the following helper for build the fcoe
  264. * driver module alias.
  265. */
  266. #define MODULE_ALIAS_FCOE_PCI(ven, dev) \
  267. MODULE_ALIAS("fcoe-pci:" \
  268. "v" __stringify(ven) \
  269. "d" __stringify(dev) "sv*sd*bc*sc*i*")
  270. /* the name of the default FCoE transport driver fcoe.ko */
  271. #define FCOE_TRANSPORT_DEFAULT "fcoe"
  272. /* struct fcoe_transport - The FCoE transport interface
  273. * @name: a vendor specific name for their FCoE transport driver
  274. * @attached: whether this transport is already attached
  275. * @list: list linkage to all attached transports
  276. * @match: handler to allow the transport driver to match up a given netdev
  277. * @alloc: handler to allocate per-instance FCoE structures
  278. * (no discovery or login)
  279. * @create: handler to sysfs entry of create for FCoE instances
  280. * @destroy: handler to delete per-instance FCoE structures
  281. * (frees all memory)
  282. * @enable: handler to sysfs entry of enable for FCoE instances
  283. * @disable: handler to sysfs entry of disable for FCoE instances
  284. */
  285. struct fcoe_transport {
  286. char name[IFNAMSIZ];
  287. bool attached;
  288. struct list_head list;
  289. bool (*match) (struct net_device *device);
  290. int (*alloc) (struct net_device *device);
  291. int (*create) (struct net_device *device, enum fip_mode fip_mode);
  292. int (*destroy) (struct net_device *device);
  293. int (*enable) (struct net_device *device);
  294. int (*disable) (struct net_device *device);
  295. };
  296. /**
  297. * struct fcoe_percpu_s - The context for FCoE receive thread(s)
  298. * @kthread: The thread context (used by bnx2fc)
  299. * @work: The work item (used by fcoe)
  300. * @fcoe_rx_list: The queue of pending packets to process
  301. * @crc_eof_page: The memory page for calculating frame trailer CRCs
  302. * @crc_eof_offset: The offset into the CRC page pointing to available
  303. * memory for a new trailer
  304. * @lock: local lock for members of this struct
  305. */
  306. struct fcoe_percpu_s {
  307. struct task_struct *kthread;
  308. struct work_struct work;
  309. struct sk_buff_head fcoe_rx_list;
  310. struct page *crc_eof_page;
  311. int crc_eof_offset;
  312. local_lock_t lock;
  313. };
  314. /**
  315. * struct fcoe_port - The FCoE private structure
  316. * @priv: The associated fcoe interface. The structure is
  317. * defined by the low level driver
  318. * @lport: The associated local port
  319. * @fcoe_pending_queue: The pending Rx queue of skbs
  320. * @fcoe_pending_queue_active: Indicates if the pending queue is active
  321. * @max_queue_depth: Max queue depth of pending queue
  322. * @min_queue_depth: Min queue depth of pending queue
  323. * @timer: The queue timer
  324. * @destroy_work: Handle for work context
  325. * (to prevent RTNL deadlocks)
  326. * @data_src_addr: Source address for data
  327. * @get_netdev: function that returns a &net_device from @lport
  328. *
  329. * An instance of this structure is to be allocated along with the
  330. * Scsi_Host and libfc fc_lport structures.
  331. */
  332. struct fcoe_port {
  333. void *priv;
  334. struct fc_lport *lport;
  335. struct sk_buff_head fcoe_pending_queue;
  336. u8 fcoe_pending_queue_active;
  337. u32 max_queue_depth;
  338. u32 min_queue_depth;
  339. struct timer_list timer;
  340. struct work_struct destroy_work;
  341. u8 data_src_addr[ETH_ALEN];
  342. struct net_device * (*get_netdev)(const struct fc_lport *lport);
  343. };
  344. /**
  345. * fcoe_get_netdev() - Return the net device associated with a local port
  346. * @lport: The local port to get the net device from
  347. *
  348. * Returns: the &net_device associated with this @lport
  349. */
  350. static inline struct net_device *fcoe_get_netdev(const struct fc_lport *lport)
  351. {
  352. struct fcoe_port *port = ((struct fcoe_port *)lport_priv(lport));
  353. return (port->get_netdev) ? port->get_netdev(lport) : NULL;
  354. }
  355. void fcoe_clean_pending_queue(struct fc_lport *);
  356. void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb);
  357. void fcoe_queue_timer(struct timer_list *t);
  358. int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
  359. struct fcoe_percpu_s *fps);
  360. /* FCoE Sysfs helpers */
  361. void fcoe_fcf_get_selected(struct fcoe_fcf_device *);
  362. void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *);
  363. /**
  364. * struct fcoe_netdev_mapping - A mapping from &net_device to &fcoe_transport
  365. * @list: list linkage of the mappings
  366. * @netdev: the &net_device
  367. * @ft: the fcoe_transport associated with @netdev
  368. */
  369. struct fcoe_netdev_mapping {
  370. struct list_head list;
  371. struct net_device *netdev;
  372. struct fcoe_transport *ft;
  373. };
  374. /* fcoe transports registration and deregistration */
  375. int fcoe_transport_attach(struct fcoe_transport *ft);
  376. int fcoe_transport_detach(struct fcoe_transport *ft);
  377. /* sysfs store handler for ctrl_control interface */
  378. ssize_t fcoe_ctlr_create_store(const char *buf, size_t count);
  379. ssize_t fcoe_ctlr_destroy_store(const char *buf, size_t count);
  380. #endif /* _LIBFCOE_H */