fcoe_transport.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Maintained at www.Open-FCoE.org
  6. */
  7. #include <linux/types.h>
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/list.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/ethtool.h>
  13. #include <linux/errno.h>
  14. #include <linux/crc32.h>
  15. #include <scsi/libfcoe.h>
  16. #include "libfcoe.h"
  17. MODULE_AUTHOR("Open-FCoE.org");
  18. MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
  19. MODULE_LICENSE("GPL v2");
  20. static int fcoe_transport_create(const char *, const struct kernel_param *);
  21. static int fcoe_transport_destroy(const char *, const struct kernel_param *);
  22. static int fcoe_transport_show(char *buffer, const struct kernel_param *kp);
  23. static struct fcoe_transport *fcoe_transport_lookup(struct net_device *device);
  24. static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *device);
  25. static int fcoe_transport_enable(const char *, const struct kernel_param *);
  26. static int fcoe_transport_disable(const char *, const struct kernel_param *);
  27. static int libfcoe_device_notification(struct notifier_block *notifier,
  28. ulong event, void *ptr);
  29. static LIST_HEAD(fcoe_transports);
  30. static DEFINE_MUTEX(ft_mutex);
  31. static LIST_HEAD(fcoe_netdevs);
  32. static DEFINE_MUTEX(fn_mutex);
  33. unsigned int libfcoe_debug_logging;
  34. module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
  35. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  36. module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR);
  37. __MODULE_PARM_TYPE(show, "string");
  38. MODULE_PARM_DESC(show, " Show attached FCoE transports");
  39. module_param_call(create, fcoe_transport_create, NULL,
  40. (void *)FIP_MODE_FABRIC, S_IWUSR);
  41. __MODULE_PARM_TYPE(create, "string");
  42. MODULE_PARM_DESC(create, " Creates fcoe instance on an ethernet interface");
  43. module_param_call(create_vn2vn, fcoe_transport_create, NULL,
  44. (void *)FIP_MODE_VN2VN, S_IWUSR);
  45. __MODULE_PARM_TYPE(create_vn2vn, "string");
  46. MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
  47. "on an Ethernet interface");
  48. module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR);
  49. __MODULE_PARM_TYPE(destroy, "string");
  50. MODULE_PARM_DESC(destroy, " Destroys fcoe instance on an ethernet interface");
  51. module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
  52. __MODULE_PARM_TYPE(enable, "string");
  53. MODULE_PARM_DESC(enable, " Enables fcoe on an ethernet interface.");
  54. module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
  55. __MODULE_PARM_TYPE(disable, "string");
  56. MODULE_PARM_DESC(disable, " Disables fcoe on an ethernet interface.");
  57. /* notification function for packets from net device */
  58. static struct notifier_block libfcoe_notifier = {
  59. .notifier_call = libfcoe_device_notification,
  60. };
  61. static const struct {
  62. u32 fc_port_speed;
  63. #define SPEED_2000 2000
  64. #define SPEED_4000 4000
  65. #define SPEED_8000 8000
  66. #define SPEED_16000 16000
  67. #define SPEED_32000 32000
  68. u32 eth_port_speed;
  69. } fcoe_port_speed_mapping[] = {
  70. { FC_PORTSPEED_1GBIT, SPEED_1000 },
  71. { FC_PORTSPEED_2GBIT, SPEED_2000 },
  72. { FC_PORTSPEED_4GBIT, SPEED_4000 },
  73. { FC_PORTSPEED_8GBIT, SPEED_8000 },
  74. { FC_PORTSPEED_10GBIT, SPEED_10000 },
  75. { FC_PORTSPEED_16GBIT, SPEED_16000 },
  76. { FC_PORTSPEED_20GBIT, SPEED_20000 },
  77. { FC_PORTSPEED_25GBIT, SPEED_25000 },
  78. { FC_PORTSPEED_32GBIT, SPEED_32000 },
  79. { FC_PORTSPEED_40GBIT, SPEED_40000 },
  80. { FC_PORTSPEED_50GBIT, SPEED_50000 },
  81. { FC_PORTSPEED_100GBIT, SPEED_100000 },
  82. };
  83. static inline u32 eth2fc_speed(u32 eth_port_speed)
  84. {
  85. int i;
  86. for (i = 0; i < ARRAY_SIZE(fcoe_port_speed_mapping); i++) {
  87. if (fcoe_port_speed_mapping[i].eth_port_speed == eth_port_speed)
  88. return fcoe_port_speed_mapping[i].fc_port_speed;
  89. }
  90. return FC_PORTSPEED_UNKNOWN;
  91. }
  92. /**
  93. * fcoe_link_speed_update() - Update the supported and actual link speeds
  94. * @lport: The local port to update speeds for
  95. *
  96. * Returns: 0 if the ethtool query was successful
  97. * -1 if the ethtool query failed
  98. */
  99. int fcoe_link_speed_update(struct fc_lport *lport)
  100. {
  101. struct net_device *netdev = fcoe_get_netdev(lport);
  102. struct ethtool_link_ksettings ecmd;
  103. if (!__ethtool_get_link_ksettings(netdev, &ecmd)) {
  104. lport->link_supported_speeds &= ~(FC_PORTSPEED_1GBIT |
  105. FC_PORTSPEED_10GBIT |
  106. FC_PORTSPEED_20GBIT |
  107. FC_PORTSPEED_40GBIT);
  108. if (ecmd.link_modes.supported[0] & (
  109. SUPPORTED_1000baseT_Half |
  110. SUPPORTED_1000baseT_Full |
  111. SUPPORTED_1000baseKX_Full))
  112. lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
  113. if (ecmd.link_modes.supported[0] & (
  114. SUPPORTED_10000baseT_Full |
  115. SUPPORTED_10000baseKX4_Full |
  116. SUPPORTED_10000baseKR_Full |
  117. SUPPORTED_10000baseR_FEC))
  118. lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
  119. if (ecmd.link_modes.supported[0] & (
  120. SUPPORTED_20000baseMLD2_Full |
  121. SUPPORTED_20000baseKR2_Full))
  122. lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
  123. if (ecmd.link_modes.supported[0] & (
  124. SUPPORTED_40000baseKR4_Full |
  125. SUPPORTED_40000baseCR4_Full |
  126. SUPPORTED_40000baseSR4_Full |
  127. SUPPORTED_40000baseLR4_Full))
  128. lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
  129. lport->link_speed = eth2fc_speed(ecmd.base.speed);
  130. return 0;
  131. }
  132. return -1;
  133. }
  134. EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
  135. /**
  136. * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
  137. * @lport: The local port to update speeds for
  138. * @fc_lesb: Pointer to the LESB to be filled up
  139. * @netdev: Pointer to the netdev that is associated with the lport
  140. *
  141. * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
  142. * Clause 7.11 in v1.04.
  143. */
  144. void __fcoe_get_lesb(struct fc_lport *lport,
  145. struct fc_els_lesb *fc_lesb,
  146. struct net_device *netdev)
  147. {
  148. unsigned int cpu;
  149. u32 lfc, vlfc, mdac;
  150. struct fc_stats *stats;
  151. struct fcoe_fc_els_lesb *lesb;
  152. struct rtnl_link_stats64 temp;
  153. lfc = 0;
  154. vlfc = 0;
  155. mdac = 0;
  156. lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
  157. memset(lesb, 0, sizeof(*lesb));
  158. for_each_possible_cpu(cpu) {
  159. stats = per_cpu_ptr(lport->stats, cpu);
  160. lfc += READ_ONCE(stats->LinkFailureCount);
  161. vlfc += READ_ONCE(stats->VLinkFailureCount);
  162. mdac += READ_ONCE(stats->MissDiscAdvCount);
  163. }
  164. lesb->lesb_link_fail = htonl(lfc);
  165. lesb->lesb_vlink_fail = htonl(vlfc);
  166. lesb->lesb_miss_fka = htonl(mdac);
  167. lesb->lesb_fcs_error =
  168. htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
  169. }
  170. EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
  171. /**
  172. * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
  173. * @lport: the local port
  174. * @fc_lesb: the link error status block
  175. */
  176. void fcoe_get_lesb(struct fc_lport *lport,
  177. struct fc_els_lesb *fc_lesb)
  178. {
  179. struct net_device *netdev = fcoe_get_netdev(lport);
  180. __fcoe_get_lesb(lport, fc_lesb, netdev);
  181. }
  182. EXPORT_SYMBOL_GPL(fcoe_get_lesb);
  183. /**
  184. * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
  185. * fcoe controller device
  186. * @ctlr_dev: The given fcoe controller device
  187. *
  188. */
  189. void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
  190. {
  191. struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
  192. struct net_device *netdev = fcoe_get_netdev(fip->lp);
  193. struct fc_els_lesb *fc_lesb;
  194. fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
  195. __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
  196. }
  197. EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
  198. void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
  199. {
  200. u8 wwpn[8];
  201. u64_to_wwn(wwn, wwpn);
  202. snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
  203. wwpn[0], wwpn[1], wwpn[2], wwpn[3],
  204. wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
  205. }
  206. EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
  207. /**
  208. * fcoe_validate_vport_create() - Validate a vport before creating it
  209. * @vport: NPIV port to be created
  210. *
  211. * This routine is meant to add validation for a vport before creating it
  212. * via fcoe_vport_create().
  213. * Current validations are:
  214. * - WWPN supplied is unique for given lport
  215. */
  216. int fcoe_validate_vport_create(struct fc_vport *vport)
  217. {
  218. struct Scsi_Host *shost = vport_to_shost(vport);
  219. struct fc_lport *n_port = shost_priv(shost);
  220. struct fc_lport *vn_port;
  221. int rc = 0;
  222. char buf[32];
  223. mutex_lock(&n_port->lp_mutex);
  224. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  225. /* Check if the wwpn is not same as that of the lport */
  226. if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
  227. LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
  228. "base port WWPN\n", buf);
  229. rc = -EINVAL;
  230. goto out;
  231. }
  232. /* Check if there is any existing vport with same wwpn */
  233. list_for_each_entry(vn_port, &n_port->vports, list) {
  234. if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
  235. LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
  236. "already exists\n", buf);
  237. rc = -EINVAL;
  238. break;
  239. }
  240. }
  241. out:
  242. mutex_unlock(&n_port->lp_mutex);
  243. return rc;
  244. }
  245. EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
  246. /**
  247. * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
  248. * @netdev: the associated net device
  249. * @wwn: the output WWN
  250. * @type: the type of WWN (WWPN or WWNN)
  251. *
  252. * Returns: 0 for success
  253. */
  254. int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
  255. {
  256. const struct net_device_ops *ops = netdev->netdev_ops;
  257. if (ops->ndo_fcoe_get_wwn)
  258. return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
  259. return -EINVAL;
  260. }
  261. EXPORT_SYMBOL_GPL(fcoe_get_wwn);
  262. /**
  263. * fcoe_fc_crc() - Calculates the CRC for a given frame
  264. * @fp: The frame to be checksumed
  265. *
  266. * This uses crc32() routine to calculate the CRC for a frame
  267. *
  268. * Return: The 32 bit CRC value
  269. */
  270. u32 fcoe_fc_crc(struct fc_frame *fp)
  271. {
  272. struct sk_buff *skb = fp_skb(fp);
  273. skb_frag_t *frag;
  274. unsigned char *data;
  275. unsigned long off, len, clen;
  276. u32 crc;
  277. unsigned i;
  278. crc = crc32(~0, skb->data, skb_headlen(skb));
  279. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  280. frag = &skb_shinfo(skb)->frags[i];
  281. off = skb_frag_off(frag);
  282. len = skb_frag_size(frag);
  283. while (len > 0) {
  284. clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
  285. data = kmap_atomic(
  286. skb_frag_page(frag) + (off >> PAGE_SHIFT));
  287. crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
  288. kunmap_atomic(data);
  289. off += clen;
  290. len -= clen;
  291. }
  292. }
  293. return crc;
  294. }
  295. EXPORT_SYMBOL_GPL(fcoe_fc_crc);
  296. /**
  297. * fcoe_start_io() - Start FCoE I/O
  298. * @skb: The packet to be transmitted
  299. *
  300. * This routine is called from the net device to start transmitting
  301. * FCoE packets.
  302. *
  303. * Returns: 0 for success
  304. */
  305. int fcoe_start_io(struct sk_buff *skb)
  306. {
  307. struct sk_buff *nskb;
  308. int rc;
  309. nskb = skb_clone(skb, GFP_ATOMIC);
  310. if (!nskb)
  311. return -ENOMEM;
  312. rc = dev_queue_xmit(nskb);
  313. if (rc != 0)
  314. return rc;
  315. kfree_skb(skb);
  316. return 0;
  317. }
  318. EXPORT_SYMBOL_GPL(fcoe_start_io);
  319. /**
  320. * fcoe_clean_pending_queue() - Dequeue a skb and free it
  321. * @lport: The local port to dequeue a skb on
  322. */
  323. void fcoe_clean_pending_queue(struct fc_lport *lport)
  324. {
  325. struct fcoe_port *port = lport_priv(lport);
  326. struct sk_buff *skb;
  327. spin_lock_bh(&port->fcoe_pending_queue.lock);
  328. while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
  329. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  330. kfree_skb(skb);
  331. spin_lock_bh(&port->fcoe_pending_queue.lock);
  332. }
  333. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  334. }
  335. EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
  336. /**
  337. * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
  338. * @lport: The local port whose backlog is to be cleared
  339. * @skb: The received FIP packet
  340. *
  341. * This empties the wait_queue, dequeues the head of the wait_queue queue
  342. * and calls fcoe_start_io() for each packet. If all skb have been
  343. * transmitted it returns the qlen. If an error occurs it restores
  344. * wait_queue (to try again later) and returns -1.
  345. *
  346. * The wait_queue is used when the skb transmit fails. The failed skb
  347. * will go in the wait_queue which will be emptied by the timer function or
  348. * by the next skb transmit.
  349. */
  350. void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
  351. {
  352. struct fcoe_port *port = lport_priv(lport);
  353. int rc;
  354. spin_lock_bh(&port->fcoe_pending_queue.lock);
  355. if (skb)
  356. __skb_queue_tail(&port->fcoe_pending_queue, skb);
  357. if (port->fcoe_pending_queue_active)
  358. goto out;
  359. port->fcoe_pending_queue_active = 1;
  360. while (port->fcoe_pending_queue.qlen) {
  361. /* keep qlen > 0 until fcoe_start_io succeeds */
  362. port->fcoe_pending_queue.qlen++;
  363. skb = __skb_dequeue(&port->fcoe_pending_queue);
  364. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  365. rc = fcoe_start_io(skb);
  366. spin_lock_bh(&port->fcoe_pending_queue.lock);
  367. if (rc) {
  368. __skb_queue_head(&port->fcoe_pending_queue, skb);
  369. /* undo temporary increment above */
  370. port->fcoe_pending_queue.qlen--;
  371. break;
  372. }
  373. /* undo temporary increment above */
  374. port->fcoe_pending_queue.qlen--;
  375. }
  376. if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
  377. lport->qfull = 0;
  378. if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
  379. mod_timer(&port->timer, jiffies + 2);
  380. port->fcoe_pending_queue_active = 0;
  381. out:
  382. if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
  383. lport->qfull = 1;
  384. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  385. }
  386. EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
  387. /**
  388. * fcoe_queue_timer() - The fcoe queue timer
  389. * @t: Timer context use to obtain the FCoE port
  390. *
  391. * Calls fcoe_check_wait_queue on timeout
  392. */
  393. void fcoe_queue_timer(struct timer_list *t)
  394. {
  395. struct fcoe_port *port = from_timer(port, t, timer);
  396. fcoe_check_wait_queue(port->lport, NULL);
  397. }
  398. EXPORT_SYMBOL_GPL(fcoe_queue_timer);
  399. /**
  400. * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
  401. * @skb: The packet to be transmitted
  402. * @tlen: The total length of the trailer
  403. * @fps: The fcoe context
  404. *
  405. * This routine allocates a page for frame trailers. The page is re-used if
  406. * there is enough room left on it for the current trailer. If there isn't
  407. * enough buffer left a new page is allocated for the trailer. Reference to
  408. * the page from this function as well as the skbs using the page fragments
  409. * ensure that the page is freed at the appropriate time.
  410. *
  411. * Returns: 0 for success
  412. */
  413. int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
  414. struct fcoe_percpu_s *fps)
  415. {
  416. struct page *page;
  417. page = fps->crc_eof_page;
  418. if (!page) {
  419. page = alloc_page(GFP_ATOMIC);
  420. if (!page)
  421. return -ENOMEM;
  422. fps->crc_eof_page = page;
  423. fps->crc_eof_offset = 0;
  424. }
  425. get_page(page);
  426. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
  427. fps->crc_eof_offset, tlen);
  428. skb->len += tlen;
  429. skb->data_len += tlen;
  430. skb->truesize += tlen;
  431. fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
  432. if (fps->crc_eof_offset >= PAGE_SIZE) {
  433. fps->crc_eof_page = NULL;
  434. fps->crc_eof_offset = 0;
  435. put_page(page);
  436. }
  437. return 0;
  438. }
  439. EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
  440. /**
  441. * fcoe_transport_lookup - find an fcoe transport that matches a netdev
  442. * @netdev: The netdev to look for from all attached transports
  443. *
  444. * Returns : ptr to the fcoe transport that supports this netdev or NULL
  445. * if not found.
  446. *
  447. * The ft_mutex should be held when this is called
  448. */
  449. static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
  450. {
  451. struct fcoe_transport *ft = NULL;
  452. list_for_each_entry(ft, &fcoe_transports, list)
  453. if (ft->match && ft->match(netdev))
  454. return ft;
  455. return NULL;
  456. }
  457. /**
  458. * fcoe_transport_attach - Attaches an FCoE transport
  459. * @ft: The fcoe transport to be attached
  460. *
  461. * Returns : 0 for success
  462. */
  463. int fcoe_transport_attach(struct fcoe_transport *ft)
  464. {
  465. int rc = 0;
  466. mutex_lock(&ft_mutex);
  467. if (ft->attached) {
  468. LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
  469. ft->name);
  470. rc = -EEXIST;
  471. goto out_attach;
  472. }
  473. /* Add default transport to the tail */
  474. if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
  475. list_add(&ft->list, &fcoe_transports);
  476. else
  477. list_add_tail(&ft->list, &fcoe_transports);
  478. ft->attached = true;
  479. LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
  480. out_attach:
  481. mutex_unlock(&ft_mutex);
  482. return rc;
  483. }
  484. EXPORT_SYMBOL(fcoe_transport_attach);
  485. /**
  486. * fcoe_transport_detach - Detaches an FCoE transport
  487. * @ft: The fcoe transport to be attached
  488. *
  489. * Returns : 0 for success
  490. */
  491. int fcoe_transport_detach(struct fcoe_transport *ft)
  492. {
  493. int rc = 0;
  494. struct fcoe_netdev_mapping *nm = NULL, *tmp;
  495. mutex_lock(&ft_mutex);
  496. if (!ft->attached) {
  497. LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
  498. ft->name);
  499. rc = -ENODEV;
  500. goto out_attach;
  501. }
  502. /* remove netdev mapping for this transport as it is going away */
  503. mutex_lock(&fn_mutex);
  504. list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
  505. if (nm->ft == ft) {
  506. LIBFCOE_TRANSPORT_DBG("transport %s going away, "
  507. "remove its netdev mapping for %s\n",
  508. ft->name, nm->netdev->name);
  509. list_del(&nm->list);
  510. kfree(nm);
  511. }
  512. }
  513. mutex_unlock(&fn_mutex);
  514. list_del(&ft->list);
  515. ft->attached = false;
  516. LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
  517. out_attach:
  518. mutex_unlock(&ft_mutex);
  519. return rc;
  520. }
  521. EXPORT_SYMBOL(fcoe_transport_detach);
  522. static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
  523. {
  524. int i, j;
  525. struct fcoe_transport *ft = NULL;
  526. i = j = sprintf(buffer, "Attached FCoE transports:");
  527. mutex_lock(&ft_mutex);
  528. list_for_each_entry(ft, &fcoe_transports, list) {
  529. if (i >= PAGE_SIZE - IFNAMSIZ)
  530. break;
  531. i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
  532. }
  533. mutex_unlock(&ft_mutex);
  534. if (i == j)
  535. i += snprintf(&buffer[i], IFNAMSIZ, "none");
  536. return i;
  537. }
  538. static int __init fcoe_transport_init(void)
  539. {
  540. register_netdevice_notifier(&libfcoe_notifier);
  541. return 0;
  542. }
  543. static int fcoe_transport_exit(void)
  544. {
  545. struct fcoe_transport *ft;
  546. unregister_netdevice_notifier(&libfcoe_notifier);
  547. mutex_lock(&ft_mutex);
  548. list_for_each_entry(ft, &fcoe_transports, list)
  549. printk(KERN_ERR "FCoE transport %s is still attached!\n",
  550. ft->name);
  551. mutex_unlock(&ft_mutex);
  552. return 0;
  553. }
  554. static int fcoe_add_netdev_mapping(struct net_device *netdev,
  555. struct fcoe_transport *ft)
  556. {
  557. struct fcoe_netdev_mapping *nm;
  558. nm = kmalloc(sizeof(*nm), GFP_KERNEL);
  559. if (!nm) {
  560. printk(KERN_ERR "Unable to allocate netdev_mapping");
  561. return -ENOMEM;
  562. }
  563. nm->netdev = netdev;
  564. nm->ft = ft;
  565. mutex_lock(&fn_mutex);
  566. list_add(&nm->list, &fcoe_netdevs);
  567. mutex_unlock(&fn_mutex);
  568. return 0;
  569. }
  570. static void fcoe_del_netdev_mapping(struct net_device *netdev)
  571. {
  572. struct fcoe_netdev_mapping *nm = NULL, *tmp;
  573. mutex_lock(&fn_mutex);
  574. list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
  575. if (nm->netdev == netdev) {
  576. list_del(&nm->list);
  577. kfree(nm);
  578. mutex_unlock(&fn_mutex);
  579. return;
  580. }
  581. }
  582. mutex_unlock(&fn_mutex);
  583. }
  584. /**
  585. * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
  586. * it was created
  587. * @netdev: The net device that the FCoE interface is on
  588. *
  589. * Returns : ptr to the fcoe transport that supports this netdev or NULL
  590. * if not found.
  591. *
  592. * The ft_mutex should be held when this is called
  593. */
  594. static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
  595. {
  596. struct fcoe_transport *ft = NULL;
  597. struct fcoe_netdev_mapping *nm;
  598. mutex_lock(&fn_mutex);
  599. list_for_each_entry(nm, &fcoe_netdevs, list) {
  600. if (netdev == nm->netdev) {
  601. ft = nm->ft;
  602. mutex_unlock(&fn_mutex);
  603. return ft;
  604. }
  605. }
  606. mutex_unlock(&fn_mutex);
  607. return NULL;
  608. }
  609. /**
  610. * fcoe_if_to_netdev() - Parse a name buffer to get a net device
  611. * @buffer: The name of the net device
  612. *
  613. * Returns: NULL or a ptr to net_device
  614. */
  615. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  616. {
  617. char *cp;
  618. char ifname[IFNAMSIZ + 2];
  619. if (buffer) {
  620. strlcpy(ifname, buffer, IFNAMSIZ);
  621. cp = ifname + strlen(ifname);
  622. while (--cp >= ifname && *cp == '\n')
  623. *cp = '\0';
  624. return dev_get_by_name(&init_net, ifname);
  625. }
  626. return NULL;
  627. }
  628. /**
  629. * libfcoe_device_notification() - Handler for net device events
  630. * @notifier: The context of the notification
  631. * @event: The type of event
  632. * @ptr: The net device that the event was on
  633. *
  634. * This function is called by the Ethernet driver in case of link change event.
  635. *
  636. * Returns: 0 for success
  637. */
  638. static int libfcoe_device_notification(struct notifier_block *notifier,
  639. ulong event, void *ptr)
  640. {
  641. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  642. switch (event) {
  643. case NETDEV_UNREGISTER:
  644. LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
  645. netdev->name);
  646. fcoe_del_netdev_mapping(netdev);
  647. break;
  648. }
  649. return NOTIFY_OK;
  650. }
  651. ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
  652. const char *buf, size_t count)
  653. {
  654. struct net_device *netdev = NULL;
  655. struct fcoe_transport *ft = NULL;
  656. int rc = 0;
  657. int err;
  658. mutex_lock(&ft_mutex);
  659. netdev = fcoe_if_to_netdev(buf);
  660. if (!netdev) {
  661. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
  662. rc = -ENODEV;
  663. goto out_nodev;
  664. }
  665. ft = fcoe_netdev_map_lookup(netdev);
  666. if (ft) {
  667. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  668. "FCoE instance on %s.\n",
  669. ft->name, netdev->name);
  670. rc = -EEXIST;
  671. goto out_putdev;
  672. }
  673. ft = fcoe_transport_lookup(netdev);
  674. if (!ft) {
  675. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  676. netdev->name);
  677. rc = -ENODEV;
  678. goto out_putdev;
  679. }
  680. /* pass to transport create */
  681. err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
  682. if (err) {
  683. fcoe_del_netdev_mapping(netdev);
  684. rc = -ENOMEM;
  685. goto out_putdev;
  686. }
  687. err = fcoe_add_netdev_mapping(netdev, ft);
  688. if (err) {
  689. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  690. "for FCoE transport %s for %s.\n",
  691. ft->name, netdev->name);
  692. rc = -ENODEV;
  693. goto out_putdev;
  694. }
  695. LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
  696. ft->name, netdev->name);
  697. out_putdev:
  698. dev_put(netdev);
  699. out_nodev:
  700. mutex_unlock(&ft_mutex);
  701. if (rc)
  702. return rc;
  703. return count;
  704. }
  705. ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
  706. const char *buf, size_t count)
  707. {
  708. int rc = -ENODEV;
  709. struct net_device *netdev = NULL;
  710. struct fcoe_transport *ft = NULL;
  711. mutex_lock(&ft_mutex);
  712. netdev = fcoe_if_to_netdev(buf);
  713. if (!netdev) {
  714. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
  715. goto out_nodev;
  716. }
  717. ft = fcoe_netdev_map_lookup(netdev);
  718. if (!ft) {
  719. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  720. netdev->name);
  721. goto out_putdev;
  722. }
  723. /* pass to transport destroy */
  724. rc = ft->destroy(netdev);
  725. if (rc)
  726. goto out_putdev;
  727. fcoe_del_netdev_mapping(netdev);
  728. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  729. ft->name, (rc) ? "failed" : "succeeded",
  730. netdev->name);
  731. rc = count; /* required for successful return */
  732. out_putdev:
  733. dev_put(netdev);
  734. out_nodev:
  735. mutex_unlock(&ft_mutex);
  736. return rc;
  737. }
  738. /**
  739. * fcoe_transport_create() - Create a fcoe interface
  740. * @buffer: The name of the Ethernet interface to create on
  741. * @kp: The associated kernel param
  742. *
  743. * Called from sysfs. This holds the ft_mutex while calling the
  744. * registered fcoe transport's create function.
  745. *
  746. * Returns: 0 for success
  747. */
  748. static int fcoe_transport_create(const char *buffer,
  749. const struct kernel_param *kp)
  750. {
  751. int rc = -ENODEV;
  752. struct net_device *netdev = NULL;
  753. struct fcoe_transport *ft = NULL;
  754. enum fip_mode fip_mode = (enum fip_mode)(uintptr_t)kp->arg;
  755. mutex_lock(&ft_mutex);
  756. netdev = fcoe_if_to_netdev(buffer);
  757. if (!netdev) {
  758. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
  759. goto out_nodev;
  760. }
  761. ft = fcoe_netdev_map_lookup(netdev);
  762. if (ft) {
  763. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  764. "FCoE instance on %s.\n",
  765. ft->name, netdev->name);
  766. rc = -EEXIST;
  767. goto out_putdev;
  768. }
  769. ft = fcoe_transport_lookup(netdev);
  770. if (!ft) {
  771. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  772. netdev->name);
  773. goto out_putdev;
  774. }
  775. rc = fcoe_add_netdev_mapping(netdev, ft);
  776. if (rc) {
  777. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  778. "for FCoE transport %s for %s.\n",
  779. ft->name, netdev->name);
  780. goto out_putdev;
  781. }
  782. /* pass to transport create */
  783. rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
  784. if (rc)
  785. fcoe_del_netdev_mapping(netdev);
  786. LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
  787. ft->name, (rc) ? "failed" : "succeeded",
  788. netdev->name);
  789. out_putdev:
  790. dev_put(netdev);
  791. out_nodev:
  792. mutex_unlock(&ft_mutex);
  793. return rc;
  794. }
  795. /**
  796. * fcoe_transport_destroy() - Destroy a FCoE interface
  797. * @buffer: The name of the Ethernet interface to be destroyed
  798. * @kp: The associated kernel parameter
  799. *
  800. * Called from sysfs. This holds the ft_mutex while calling the
  801. * registered fcoe transport's destroy function.
  802. *
  803. * Returns: 0 for success
  804. */
  805. static int fcoe_transport_destroy(const char *buffer,
  806. const struct kernel_param *kp)
  807. {
  808. int rc = -ENODEV;
  809. struct net_device *netdev = NULL;
  810. struct fcoe_transport *ft = NULL;
  811. mutex_lock(&ft_mutex);
  812. netdev = fcoe_if_to_netdev(buffer);
  813. if (!netdev) {
  814. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
  815. goto out_nodev;
  816. }
  817. ft = fcoe_netdev_map_lookup(netdev);
  818. if (!ft) {
  819. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  820. netdev->name);
  821. goto out_putdev;
  822. }
  823. /* pass to transport destroy */
  824. rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
  825. fcoe_del_netdev_mapping(netdev);
  826. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  827. ft->name, (rc) ? "failed" : "succeeded",
  828. netdev->name);
  829. out_putdev:
  830. dev_put(netdev);
  831. out_nodev:
  832. mutex_unlock(&ft_mutex);
  833. return rc;
  834. }
  835. /**
  836. * fcoe_transport_disable() - Disables a FCoE interface
  837. * @buffer: The name of the Ethernet interface to be disabled
  838. * @kp: The associated kernel parameter
  839. *
  840. * Called from sysfs.
  841. *
  842. * Returns: 0 for success
  843. */
  844. static int fcoe_transport_disable(const char *buffer,
  845. const struct kernel_param *kp)
  846. {
  847. int rc = -ENODEV;
  848. struct net_device *netdev = NULL;
  849. struct fcoe_transport *ft = NULL;
  850. mutex_lock(&ft_mutex);
  851. netdev = fcoe_if_to_netdev(buffer);
  852. if (!netdev)
  853. goto out_nodev;
  854. ft = fcoe_netdev_map_lookup(netdev);
  855. if (!ft)
  856. goto out_putdev;
  857. rc = ft->disable ? ft->disable(netdev) : -ENODEV;
  858. out_putdev:
  859. dev_put(netdev);
  860. out_nodev:
  861. mutex_unlock(&ft_mutex);
  862. return rc;
  863. }
  864. /**
  865. * fcoe_transport_enable() - Enables a FCoE interface
  866. * @buffer: The name of the Ethernet interface to be enabled
  867. * @kp: The associated kernel parameter
  868. *
  869. * Called from sysfs.
  870. *
  871. * Returns: 0 for success
  872. */
  873. static int fcoe_transport_enable(const char *buffer,
  874. const struct kernel_param *kp)
  875. {
  876. int rc = -ENODEV;
  877. struct net_device *netdev = NULL;
  878. struct fcoe_transport *ft = NULL;
  879. mutex_lock(&ft_mutex);
  880. netdev = fcoe_if_to_netdev(buffer);
  881. if (!netdev)
  882. goto out_nodev;
  883. ft = fcoe_netdev_map_lookup(netdev);
  884. if (!ft)
  885. goto out_putdev;
  886. rc = ft->enable ? ft->enable(netdev) : -ENODEV;
  887. out_putdev:
  888. dev_put(netdev);
  889. out_nodev:
  890. mutex_unlock(&ft_mutex);
  891. return rc;
  892. }
  893. /**
  894. * libfcoe_init() - Initialization routine for libfcoe.ko
  895. */
  896. static int __init libfcoe_init(void)
  897. {
  898. int rc = 0;
  899. rc = fcoe_transport_init();
  900. if (rc)
  901. return rc;
  902. rc = fcoe_sysfs_setup();
  903. if (rc)
  904. fcoe_transport_exit();
  905. return rc;
  906. }
  907. module_init(libfcoe_init);
  908. /**
  909. * libfcoe_exit() - Tear down libfcoe.ko
  910. */
  911. static void __exit libfcoe_exit(void)
  912. {
  913. fcoe_sysfs_teardown();
  914. fcoe_transport_exit();
  915. }
  916. module_exit(libfcoe_exit);