fcoe_transport.c 27 KB

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