fcoe_transport.c 26 KB

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