fcoe_transport.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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_fc_crc() - Calculates the CRC for a given frame
  74. * @fp: The frame to be checksumed
  75. *
  76. * This uses crc32() routine to calculate the CRC for a frame
  77. *
  78. * Return: The 32 bit CRC value
  79. */
  80. u32 fcoe_fc_crc(struct fc_frame *fp)
  81. {
  82. struct sk_buff *skb = fp_skb(fp);
  83. struct skb_frag_struct *frag;
  84. unsigned char *data;
  85. unsigned long off, len, clen;
  86. u32 crc;
  87. unsigned i;
  88. crc = crc32(~0, skb->data, skb_headlen(skb));
  89. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  90. frag = &skb_shinfo(skb)->frags[i];
  91. off = frag->page_offset;
  92. len = frag->size;
  93. while (len > 0) {
  94. clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
  95. data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
  96. KM_SKB_DATA_SOFTIRQ);
  97. crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
  98. kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
  99. off += clen;
  100. len -= clen;
  101. }
  102. }
  103. return crc;
  104. }
  105. EXPORT_SYMBOL_GPL(fcoe_fc_crc);
  106. /**
  107. * fcoe_start_io() - Start FCoE I/O
  108. * @skb: The packet to be transmitted
  109. *
  110. * This routine is called from the net device to start transmitting
  111. * FCoE packets.
  112. *
  113. * Returns: 0 for success
  114. */
  115. int fcoe_start_io(struct sk_buff *skb)
  116. {
  117. struct sk_buff *nskb;
  118. int rc;
  119. nskb = skb_clone(skb, GFP_ATOMIC);
  120. if (!nskb)
  121. return -ENOMEM;
  122. rc = dev_queue_xmit(nskb);
  123. if (rc != 0)
  124. return rc;
  125. kfree_skb(skb);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL_GPL(fcoe_start_io);
  129. /**
  130. * fcoe_clean_pending_queue() - Dequeue a skb and free it
  131. * @lport: The local port to dequeue a skb on
  132. */
  133. void fcoe_clean_pending_queue(struct fc_lport *lport)
  134. {
  135. struct fcoe_port *port = lport_priv(lport);
  136. struct sk_buff *skb;
  137. spin_lock_bh(&port->fcoe_pending_queue.lock);
  138. while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
  139. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  140. kfree_skb(skb);
  141. spin_lock_bh(&port->fcoe_pending_queue.lock);
  142. }
  143. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  144. }
  145. EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
  146. /**
  147. * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
  148. * @lport: The local port whose backlog is to be cleared
  149. *
  150. * This empties the wait_queue, dequeues the head of the wait_queue queue
  151. * and calls fcoe_start_io() for each packet. If all skb have been
  152. * transmitted it returns the qlen. If an error occurs it restores
  153. * wait_queue (to try again later) and returns -1.
  154. *
  155. * The wait_queue is used when the skb transmit fails. The failed skb
  156. * will go in the wait_queue which will be emptied by the timer function or
  157. * by the next skb transmit.
  158. */
  159. void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
  160. {
  161. struct fcoe_port *port = lport_priv(lport);
  162. int rc;
  163. spin_lock_bh(&port->fcoe_pending_queue.lock);
  164. if (skb)
  165. __skb_queue_tail(&port->fcoe_pending_queue, skb);
  166. if (port->fcoe_pending_queue_active)
  167. goto out;
  168. port->fcoe_pending_queue_active = 1;
  169. while (port->fcoe_pending_queue.qlen) {
  170. /* keep qlen > 0 until fcoe_start_io succeeds */
  171. port->fcoe_pending_queue.qlen++;
  172. skb = __skb_dequeue(&port->fcoe_pending_queue);
  173. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  174. rc = fcoe_start_io(skb);
  175. spin_lock_bh(&port->fcoe_pending_queue.lock);
  176. if (rc) {
  177. __skb_queue_head(&port->fcoe_pending_queue, skb);
  178. /* undo temporary increment above */
  179. port->fcoe_pending_queue.qlen--;
  180. break;
  181. }
  182. /* undo temporary increment above */
  183. port->fcoe_pending_queue.qlen--;
  184. }
  185. if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
  186. lport->qfull = 0;
  187. if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
  188. mod_timer(&port->timer, jiffies + 2);
  189. port->fcoe_pending_queue_active = 0;
  190. out:
  191. if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
  192. lport->qfull = 1;
  193. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  194. }
  195. EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
  196. /**
  197. * fcoe_queue_timer() - The fcoe queue timer
  198. * @lport: The local port
  199. *
  200. * Calls fcoe_check_wait_queue on timeout
  201. */
  202. void fcoe_queue_timer(ulong lport)
  203. {
  204. fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
  205. }
  206. EXPORT_SYMBOL_GPL(fcoe_queue_timer);
  207. /**
  208. * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
  209. * @skb: The packet to be transmitted
  210. * @tlen: The total length of the trailer
  211. * @fps: The fcoe context
  212. *
  213. * This routine allocates a page for frame trailers. The page is re-used if
  214. * there is enough room left on it for the current trailer. If there isn't
  215. * enough buffer left a new page is allocated for the trailer. Reference to
  216. * the page from this function as well as the skbs using the page fragments
  217. * ensure that the page is freed at the appropriate time.
  218. *
  219. * Returns: 0 for success
  220. */
  221. int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
  222. struct fcoe_percpu_s *fps)
  223. {
  224. struct page *page;
  225. page = fps->crc_eof_page;
  226. if (!page) {
  227. page = alloc_page(GFP_ATOMIC);
  228. if (!page)
  229. return -ENOMEM;
  230. fps->crc_eof_page = page;
  231. fps->crc_eof_offset = 0;
  232. }
  233. get_page(page);
  234. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
  235. fps->crc_eof_offset, tlen);
  236. skb->len += tlen;
  237. skb->data_len += tlen;
  238. skb->truesize += tlen;
  239. fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
  240. if (fps->crc_eof_offset >= PAGE_SIZE) {
  241. fps->crc_eof_page = NULL;
  242. fps->crc_eof_offset = 0;
  243. put_page(page);
  244. }
  245. return 0;
  246. }
  247. EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
  248. /**
  249. * fcoe_transport_lookup - find an fcoe transport that matches a netdev
  250. * @netdev: The netdev to look for from all attached transports
  251. *
  252. * Returns : ptr to the fcoe transport that supports this netdev or NULL
  253. * if not found.
  254. *
  255. * The ft_mutex should be held when this is called
  256. */
  257. static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
  258. {
  259. struct fcoe_transport *ft = NULL;
  260. list_for_each_entry(ft, &fcoe_transports, list)
  261. if (ft->match && ft->match(netdev))
  262. return ft;
  263. return NULL;
  264. }
  265. /**
  266. * fcoe_transport_attach - Attaches an FCoE transport
  267. * @ft: The fcoe transport to be attached
  268. *
  269. * Returns : 0 for success
  270. */
  271. int fcoe_transport_attach(struct fcoe_transport *ft)
  272. {
  273. int rc = 0;
  274. mutex_lock(&ft_mutex);
  275. if (ft->attached) {
  276. LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
  277. ft->name);
  278. rc = -EEXIST;
  279. goto out_attach;
  280. }
  281. /* Add default transport to the tail */
  282. if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
  283. list_add(&ft->list, &fcoe_transports);
  284. else
  285. list_add_tail(&ft->list, &fcoe_transports);
  286. ft->attached = true;
  287. LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
  288. out_attach:
  289. mutex_unlock(&ft_mutex);
  290. return rc;
  291. }
  292. EXPORT_SYMBOL(fcoe_transport_attach);
  293. /**
  294. * fcoe_transport_attach - Detaches an FCoE transport
  295. * @ft: The fcoe transport to be attached
  296. *
  297. * Returns : 0 for success
  298. */
  299. int fcoe_transport_detach(struct fcoe_transport *ft)
  300. {
  301. int rc = 0;
  302. mutex_lock(&ft_mutex);
  303. if (!ft->attached) {
  304. LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
  305. ft->name);
  306. rc = -ENODEV;
  307. goto out_attach;
  308. }
  309. list_del(&ft->list);
  310. ft->attached = false;
  311. LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
  312. out_attach:
  313. mutex_unlock(&ft_mutex);
  314. return rc;
  315. }
  316. EXPORT_SYMBOL(fcoe_transport_detach);
  317. static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
  318. {
  319. int i, j;
  320. struct fcoe_transport *ft = NULL;
  321. i = j = sprintf(buffer, "Attached FCoE transports:");
  322. mutex_lock(&ft_mutex);
  323. list_for_each_entry(ft, &fcoe_transports, list) {
  324. i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
  325. if (i >= PAGE_SIZE)
  326. break;
  327. }
  328. mutex_unlock(&ft_mutex);
  329. if (i == j)
  330. i += snprintf(&buffer[i], IFNAMSIZ, "none");
  331. return i;
  332. }
  333. static int __init fcoe_transport_init(void)
  334. {
  335. register_netdevice_notifier(&libfcoe_notifier);
  336. return 0;
  337. }
  338. static int __exit fcoe_transport_exit(void)
  339. {
  340. struct fcoe_transport *ft;
  341. unregister_netdevice_notifier(&libfcoe_notifier);
  342. mutex_lock(&ft_mutex);
  343. list_for_each_entry(ft, &fcoe_transports, list)
  344. printk(KERN_ERR "FCoE transport %s is still attached!\n",
  345. ft->name);
  346. mutex_unlock(&ft_mutex);
  347. return 0;
  348. }
  349. static int fcoe_add_netdev_mapping(struct net_device *netdev,
  350. struct fcoe_transport *ft)
  351. {
  352. struct fcoe_netdev_mapping *nm;
  353. nm = kmalloc(sizeof(*nm), GFP_KERNEL);
  354. if (!nm) {
  355. printk(KERN_ERR "Unable to allocate netdev_mapping");
  356. return -ENOMEM;
  357. }
  358. nm->netdev = netdev;
  359. nm->ft = ft;
  360. mutex_lock(&fn_mutex);
  361. list_add(&nm->list, &fcoe_netdevs);
  362. mutex_unlock(&fn_mutex);
  363. return 0;
  364. }
  365. static void fcoe_del_netdev_mapping(struct net_device *netdev)
  366. {
  367. struct fcoe_netdev_mapping *nm = NULL, *tmp;
  368. mutex_lock(&fn_mutex);
  369. list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
  370. if (nm->netdev == netdev) {
  371. list_del(&nm->list);
  372. kfree(nm);
  373. mutex_unlock(&fn_mutex);
  374. return;
  375. }
  376. }
  377. mutex_unlock(&fn_mutex);
  378. }
  379. /**
  380. * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
  381. * it was created
  382. *
  383. * Returns : ptr to the fcoe transport that supports this netdev or NULL
  384. * if not found.
  385. *
  386. * The ft_mutex should be held when this is called
  387. */
  388. static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
  389. {
  390. struct fcoe_transport *ft = NULL;
  391. struct fcoe_netdev_mapping *nm;
  392. mutex_lock(&fn_mutex);
  393. list_for_each_entry(nm, &fcoe_netdevs, list) {
  394. if (netdev == nm->netdev) {
  395. ft = nm->ft;
  396. mutex_unlock(&fn_mutex);
  397. return ft;
  398. }
  399. }
  400. mutex_unlock(&fn_mutex);
  401. return NULL;
  402. }
  403. /**
  404. * fcoe_if_to_netdev() - Parse a name buffer to get a net device
  405. * @buffer: The name of the net device
  406. *
  407. * Returns: NULL or a ptr to net_device
  408. */
  409. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  410. {
  411. char *cp;
  412. char ifname[IFNAMSIZ + 2];
  413. if (buffer) {
  414. strlcpy(ifname, buffer, IFNAMSIZ);
  415. cp = ifname + strlen(ifname);
  416. while (--cp >= ifname && *cp == '\n')
  417. *cp = '\0';
  418. return dev_get_by_name(&init_net, ifname);
  419. }
  420. return NULL;
  421. }
  422. /**
  423. * libfcoe_device_notification() - Handler for net device events
  424. * @notifier: The context of the notification
  425. * @event: The type of event
  426. * @ptr: The net device that the event was on
  427. *
  428. * This function is called by the Ethernet driver in case of link change event.
  429. *
  430. * Returns: 0 for success
  431. */
  432. static int libfcoe_device_notification(struct notifier_block *notifier,
  433. ulong event, void *ptr)
  434. {
  435. struct net_device *netdev = ptr;
  436. switch (event) {
  437. case NETDEV_UNREGISTER:
  438. printk(KERN_ERR "libfcoe_device_notification: NETDEV_UNREGISTER %s\n",
  439. netdev->name);
  440. fcoe_del_netdev_mapping(netdev);
  441. break;
  442. }
  443. return NOTIFY_OK;
  444. }
  445. /**
  446. * fcoe_transport_create() - Create a fcoe interface
  447. * @buffer: The name of the Ethernet interface to create on
  448. * @kp: The associated kernel param
  449. *
  450. * Called from sysfs. This holds the ft_mutex while calling the
  451. * registered fcoe transport's create function.
  452. *
  453. * Returns: 0 for success
  454. */
  455. static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
  456. {
  457. int rc = -ENODEV;
  458. struct net_device *netdev = NULL;
  459. struct fcoe_transport *ft = NULL;
  460. enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
  461. if (!mutex_trylock(&ft_mutex))
  462. return restart_syscall();
  463. #ifdef CONFIG_LIBFCOE_MODULE
  464. /*
  465. * Make sure the module has been initialized, and is not about to be
  466. * removed. Module parameter sysfs files are writable before the
  467. * module_init function is called and after module_exit.
  468. */
  469. if (THIS_MODULE->state != MODULE_STATE_LIVE)
  470. goto out_nodev;
  471. #endif
  472. netdev = fcoe_if_to_netdev(buffer);
  473. if (!netdev) {
  474. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
  475. goto out_nodev;
  476. }
  477. ft = fcoe_netdev_map_lookup(netdev);
  478. if (ft) {
  479. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  480. "FCoE instance on %s.\n",
  481. ft->name, netdev->name);
  482. rc = -EEXIST;
  483. goto out_putdev;
  484. }
  485. ft = fcoe_transport_lookup(netdev);
  486. if (!ft) {
  487. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  488. netdev->name);
  489. goto out_putdev;
  490. }
  491. rc = fcoe_add_netdev_mapping(netdev, ft);
  492. if (rc) {
  493. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  494. "for FCoE transport %s for %s.\n",
  495. ft->name, netdev->name);
  496. goto out_putdev;
  497. }
  498. /* pass to transport create */
  499. rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
  500. if (rc)
  501. fcoe_del_netdev_mapping(netdev);
  502. LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
  503. ft->name, (rc) ? "failed" : "succeeded",
  504. netdev->name);
  505. out_putdev:
  506. dev_put(netdev);
  507. out_nodev:
  508. mutex_unlock(&ft_mutex);
  509. if (rc == -ERESTARTSYS)
  510. return restart_syscall();
  511. else
  512. return rc;
  513. }
  514. /**
  515. * fcoe_transport_destroy() - Destroy a FCoE interface
  516. * @buffer: The name of the Ethernet interface to be destroyed
  517. * @kp: The associated kernel parameter
  518. *
  519. * Called from sysfs. This holds the ft_mutex while calling the
  520. * registered fcoe transport's destroy function.
  521. *
  522. * Returns: 0 for success
  523. */
  524. static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
  525. {
  526. int rc = -ENODEV;
  527. struct net_device *netdev = NULL;
  528. struct fcoe_transport *ft = NULL;
  529. if (!mutex_trylock(&ft_mutex))
  530. return restart_syscall();
  531. #ifdef CONFIG_LIBFCOE_MODULE
  532. /*
  533. * Make sure the module has been initialized, and is not about to be
  534. * removed. Module parameter sysfs files are writable before the
  535. * module_init function is called and after module_exit.
  536. */
  537. if (THIS_MODULE->state != MODULE_STATE_LIVE)
  538. goto out_nodev;
  539. #endif
  540. netdev = fcoe_if_to_netdev(buffer);
  541. if (!netdev) {
  542. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
  543. goto out_nodev;
  544. }
  545. ft = fcoe_netdev_map_lookup(netdev);
  546. if (!ft) {
  547. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  548. netdev->name);
  549. goto out_putdev;
  550. }
  551. /* pass to transport destroy */
  552. rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
  553. fcoe_del_netdev_mapping(netdev);
  554. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  555. ft->name, (rc) ? "failed" : "succeeded",
  556. netdev->name);
  557. out_putdev:
  558. dev_put(netdev);
  559. out_nodev:
  560. mutex_unlock(&ft_mutex);
  561. if (rc == -ERESTARTSYS)
  562. return restart_syscall();
  563. else
  564. return rc;
  565. }
  566. /**
  567. * fcoe_transport_disable() - Disables a FCoE interface
  568. * @buffer: The name of the Ethernet interface to be disabled
  569. * @kp: The associated kernel parameter
  570. *
  571. * Called from sysfs.
  572. *
  573. * Returns: 0 for success
  574. */
  575. static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
  576. {
  577. int rc = -ENODEV;
  578. struct net_device *netdev = NULL;
  579. struct fcoe_transport *ft = NULL;
  580. if (!mutex_trylock(&ft_mutex))
  581. return restart_syscall();
  582. #ifdef CONFIG_LIBFCOE_MODULE
  583. /*
  584. * Make sure the module has been initialized, and is not about to be
  585. * removed. Module parameter sysfs files are writable before the
  586. * module_init function is called and after module_exit.
  587. */
  588. if (THIS_MODULE->state != MODULE_STATE_LIVE)
  589. goto out_nodev;
  590. #endif
  591. netdev = fcoe_if_to_netdev(buffer);
  592. if (!netdev)
  593. goto out_nodev;
  594. ft = fcoe_netdev_map_lookup(netdev);
  595. if (!ft)
  596. goto out_putdev;
  597. rc = ft->disable ? ft->disable(netdev) : -ENODEV;
  598. out_putdev:
  599. dev_put(netdev);
  600. out_nodev:
  601. mutex_unlock(&ft_mutex);
  602. if (rc == -ERESTARTSYS)
  603. return restart_syscall();
  604. else
  605. return rc;
  606. }
  607. /**
  608. * fcoe_transport_enable() - Enables a FCoE interface
  609. * @buffer: The name of the Ethernet interface to be enabled
  610. * @kp: The associated kernel parameter
  611. *
  612. * Called from sysfs.
  613. *
  614. * Returns: 0 for success
  615. */
  616. static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
  617. {
  618. int rc = -ENODEV;
  619. struct net_device *netdev = NULL;
  620. struct fcoe_transport *ft = NULL;
  621. if (!mutex_trylock(&ft_mutex))
  622. return restart_syscall();
  623. #ifdef CONFIG_LIBFCOE_MODULE
  624. /*
  625. * Make sure the module has been initialized, and is not about to be
  626. * removed. Module parameter sysfs files are writable before the
  627. * module_init function is called and after module_exit.
  628. */
  629. if (THIS_MODULE->state != MODULE_STATE_LIVE)
  630. goto out_nodev;
  631. #endif
  632. netdev = fcoe_if_to_netdev(buffer);
  633. if (!netdev)
  634. goto out_nodev;
  635. ft = fcoe_netdev_map_lookup(netdev);
  636. if (!ft)
  637. goto out_putdev;
  638. rc = ft->enable ? ft->enable(netdev) : -ENODEV;
  639. out_putdev:
  640. dev_put(netdev);
  641. out_nodev:
  642. mutex_unlock(&ft_mutex);
  643. if (rc == -ERESTARTSYS)
  644. return restart_syscall();
  645. else
  646. return rc;
  647. }
  648. /**
  649. * libfcoe_init() - Initialization routine for libfcoe.ko
  650. */
  651. static int __init libfcoe_init(void)
  652. {
  653. fcoe_transport_init();
  654. return 0;
  655. }
  656. module_init(libfcoe_init);
  657. /**
  658. * libfcoe_exit() - Tear down libfcoe.ko
  659. */
  660. static void __exit libfcoe_exit(void)
  661. {
  662. fcoe_transport_exit();
  663. }
  664. module_exit(libfcoe_exit);