fcoe_transport.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 = timer_container_of(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_obj(*nm);
  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. strscpy(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(const char *buf, size_t count)
  652. {
  653. struct net_device *netdev = NULL;
  654. struct fcoe_transport *ft = NULL;
  655. int rc = 0;
  656. int err;
  657. mutex_lock(&ft_mutex);
  658. netdev = fcoe_if_to_netdev(buf);
  659. if (!netdev) {
  660. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
  661. rc = -ENODEV;
  662. goto out_nodev;
  663. }
  664. ft = fcoe_netdev_map_lookup(netdev);
  665. if (ft) {
  666. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  667. "FCoE instance on %s.\n",
  668. ft->name, netdev->name);
  669. rc = -EEXIST;
  670. goto out_putdev;
  671. }
  672. ft = fcoe_transport_lookup(netdev);
  673. if (!ft) {
  674. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  675. netdev->name);
  676. rc = -ENODEV;
  677. goto out_putdev;
  678. }
  679. /* pass to transport create */
  680. err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
  681. if (err) {
  682. fcoe_del_netdev_mapping(netdev);
  683. rc = -ENOMEM;
  684. goto out_putdev;
  685. }
  686. err = fcoe_add_netdev_mapping(netdev, ft);
  687. if (err) {
  688. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  689. "for FCoE transport %s for %s.\n",
  690. ft->name, netdev->name);
  691. rc = -ENODEV;
  692. goto out_putdev;
  693. }
  694. LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
  695. ft->name, netdev->name);
  696. out_putdev:
  697. dev_put(netdev);
  698. out_nodev:
  699. mutex_unlock(&ft_mutex);
  700. if (rc)
  701. return rc;
  702. return count;
  703. }
  704. ssize_t fcoe_ctlr_destroy_store(const char *buf, size_t count)
  705. {
  706. int rc = -ENODEV;
  707. struct net_device *netdev = NULL;
  708. struct fcoe_transport *ft = NULL;
  709. mutex_lock(&ft_mutex);
  710. netdev = fcoe_if_to_netdev(buf);
  711. if (!netdev) {
  712. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
  713. goto out_nodev;
  714. }
  715. ft = fcoe_netdev_map_lookup(netdev);
  716. if (!ft) {
  717. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  718. netdev->name);
  719. goto out_putdev;
  720. }
  721. /* pass to transport destroy */
  722. rc = ft->destroy(netdev);
  723. if (rc)
  724. goto out_putdev;
  725. fcoe_del_netdev_mapping(netdev);
  726. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  727. ft->name, (rc) ? "failed" : "succeeded",
  728. netdev->name);
  729. rc = count; /* required for successful return */
  730. out_putdev:
  731. dev_put(netdev);
  732. out_nodev:
  733. mutex_unlock(&ft_mutex);
  734. return rc;
  735. }
  736. /**
  737. * fcoe_transport_create() - Create a fcoe interface
  738. * @buffer: The name of the Ethernet interface to create on
  739. * @kp: The associated kernel param
  740. *
  741. * Called from sysfs. This holds the ft_mutex while calling the
  742. * registered fcoe transport's create function.
  743. *
  744. * Returns: 0 for success
  745. */
  746. static int fcoe_transport_create(const char *buffer,
  747. const struct kernel_param *kp)
  748. {
  749. int rc = -ENODEV;
  750. struct net_device *netdev = NULL;
  751. struct fcoe_transport *ft = NULL;
  752. enum fip_mode fip_mode = (enum fip_mode)(uintptr_t)kp->arg;
  753. mutex_lock(&ft_mutex);
  754. netdev = fcoe_if_to_netdev(buffer);
  755. if (!netdev) {
  756. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
  757. goto out_nodev;
  758. }
  759. ft = fcoe_netdev_map_lookup(netdev);
  760. if (ft) {
  761. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  762. "FCoE instance on %s.\n",
  763. ft->name, netdev->name);
  764. rc = -EEXIST;
  765. goto out_putdev;
  766. }
  767. ft = fcoe_transport_lookup(netdev);
  768. if (!ft) {
  769. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  770. netdev->name);
  771. goto out_putdev;
  772. }
  773. rc = fcoe_add_netdev_mapping(netdev, ft);
  774. if (rc) {
  775. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  776. "for FCoE transport %s for %s.\n",
  777. ft->name, netdev->name);
  778. goto out_putdev;
  779. }
  780. /* pass to transport create */
  781. rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
  782. if (rc)
  783. fcoe_del_netdev_mapping(netdev);
  784. LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
  785. ft->name, (rc) ? "failed" : "succeeded",
  786. netdev->name);
  787. out_putdev:
  788. dev_put(netdev);
  789. out_nodev:
  790. mutex_unlock(&ft_mutex);
  791. return rc;
  792. }
  793. /**
  794. * fcoe_transport_destroy() - Destroy a FCoE interface
  795. * @buffer: The name of the Ethernet interface to be destroyed
  796. * @kp: The associated kernel parameter
  797. *
  798. * Called from sysfs. This holds the ft_mutex while calling the
  799. * registered fcoe transport's destroy function.
  800. *
  801. * Returns: 0 for success
  802. */
  803. static int fcoe_transport_destroy(const char *buffer,
  804. const struct kernel_param *kp)
  805. {
  806. int rc = -ENODEV;
  807. struct net_device *netdev = NULL;
  808. struct fcoe_transport *ft = NULL;
  809. mutex_lock(&ft_mutex);
  810. netdev = fcoe_if_to_netdev(buffer);
  811. if (!netdev) {
  812. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
  813. goto out_nodev;
  814. }
  815. ft = fcoe_netdev_map_lookup(netdev);
  816. if (!ft) {
  817. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  818. netdev->name);
  819. goto out_putdev;
  820. }
  821. /* pass to transport destroy */
  822. rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
  823. fcoe_del_netdev_mapping(netdev);
  824. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  825. ft->name, (rc) ? "failed" : "succeeded",
  826. netdev->name);
  827. out_putdev:
  828. dev_put(netdev);
  829. out_nodev:
  830. mutex_unlock(&ft_mutex);
  831. return rc;
  832. }
  833. /**
  834. * fcoe_transport_disable() - Disables a FCoE interface
  835. * @buffer: The name of the Ethernet interface to be disabled
  836. * @kp: The associated kernel parameter
  837. *
  838. * Called from sysfs.
  839. *
  840. * Returns: 0 for success
  841. */
  842. static int fcoe_transport_disable(const char *buffer,
  843. const struct kernel_param *kp)
  844. {
  845. int rc = -ENODEV;
  846. struct net_device *netdev = NULL;
  847. struct fcoe_transport *ft = NULL;
  848. mutex_lock(&ft_mutex);
  849. netdev = fcoe_if_to_netdev(buffer);
  850. if (!netdev)
  851. goto out_nodev;
  852. ft = fcoe_netdev_map_lookup(netdev);
  853. if (!ft)
  854. goto out_putdev;
  855. rc = ft->disable ? ft->disable(netdev) : -ENODEV;
  856. out_putdev:
  857. dev_put(netdev);
  858. out_nodev:
  859. mutex_unlock(&ft_mutex);
  860. return rc;
  861. }
  862. /**
  863. * fcoe_transport_enable() - Enables a FCoE interface
  864. * @buffer: The name of the Ethernet interface to be enabled
  865. * @kp: The associated kernel parameter
  866. *
  867. * Called from sysfs.
  868. *
  869. * Returns: 0 for success
  870. */
  871. static int fcoe_transport_enable(const char *buffer,
  872. const struct kernel_param *kp)
  873. {
  874. int rc = -ENODEV;
  875. struct net_device *netdev = NULL;
  876. struct fcoe_transport *ft = NULL;
  877. mutex_lock(&ft_mutex);
  878. netdev = fcoe_if_to_netdev(buffer);
  879. if (!netdev)
  880. goto out_nodev;
  881. ft = fcoe_netdev_map_lookup(netdev);
  882. if (!ft)
  883. goto out_putdev;
  884. rc = ft->enable ? ft->enable(netdev) : -ENODEV;
  885. out_putdev:
  886. dev_put(netdev);
  887. out_nodev:
  888. mutex_unlock(&ft_mutex);
  889. return rc;
  890. }
  891. /**
  892. * libfcoe_init() - Initialization routine for libfcoe.ko
  893. */
  894. static int __init libfcoe_init(void)
  895. {
  896. int rc = 0;
  897. rc = fcoe_transport_init();
  898. if (rc)
  899. return rc;
  900. rc = fcoe_sysfs_setup();
  901. if (rc)
  902. fcoe_transport_exit();
  903. return rc;
  904. }
  905. module_init(libfcoe_init);
  906. /**
  907. * libfcoe_exit() - Tear down libfcoe.ko
  908. */
  909. static void __exit libfcoe_exit(void)
  910. {
  911. fcoe_sysfs_teardown();
  912. fcoe_transport_exit();
  913. }
  914. module_exit(libfcoe_exit);