fcoe_transport.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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_detach - 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. struct fcoe_netdev_mapping *nm = NULL, *tmp;
  303. mutex_lock(&ft_mutex);
  304. if (!ft->attached) {
  305. LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
  306. ft->name);
  307. rc = -ENODEV;
  308. goto out_attach;
  309. }
  310. /* remove netdev mapping for this transport as it is going away */
  311. mutex_lock(&fn_mutex);
  312. list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
  313. if (nm->ft == ft) {
  314. LIBFCOE_TRANSPORT_DBG("transport %s going away, "
  315. "remove its netdev mapping for %s\n",
  316. ft->name, nm->netdev->name);
  317. list_del(&nm->list);
  318. kfree(nm);
  319. }
  320. }
  321. mutex_unlock(&fn_mutex);
  322. list_del(&ft->list);
  323. ft->attached = false;
  324. LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
  325. out_attach:
  326. mutex_unlock(&ft_mutex);
  327. return rc;
  328. }
  329. EXPORT_SYMBOL(fcoe_transport_detach);
  330. static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
  331. {
  332. int i, j;
  333. struct fcoe_transport *ft = NULL;
  334. i = j = sprintf(buffer, "Attached FCoE transports:");
  335. mutex_lock(&ft_mutex);
  336. list_for_each_entry(ft, &fcoe_transports, list) {
  337. if (i >= PAGE_SIZE - IFNAMSIZ)
  338. break;
  339. i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
  340. }
  341. mutex_unlock(&ft_mutex);
  342. if (i == j)
  343. i += snprintf(&buffer[i], IFNAMSIZ, "none");
  344. return i;
  345. }
  346. static int __init fcoe_transport_init(void)
  347. {
  348. register_netdevice_notifier(&libfcoe_notifier);
  349. return 0;
  350. }
  351. static int __exit fcoe_transport_exit(void)
  352. {
  353. struct fcoe_transport *ft;
  354. unregister_netdevice_notifier(&libfcoe_notifier);
  355. mutex_lock(&ft_mutex);
  356. list_for_each_entry(ft, &fcoe_transports, list)
  357. printk(KERN_ERR "FCoE transport %s is still attached!\n",
  358. ft->name);
  359. mutex_unlock(&ft_mutex);
  360. return 0;
  361. }
  362. static int fcoe_add_netdev_mapping(struct net_device *netdev,
  363. struct fcoe_transport *ft)
  364. {
  365. struct fcoe_netdev_mapping *nm;
  366. nm = kmalloc(sizeof(*nm), GFP_KERNEL);
  367. if (!nm) {
  368. printk(KERN_ERR "Unable to allocate netdev_mapping");
  369. return -ENOMEM;
  370. }
  371. nm->netdev = netdev;
  372. nm->ft = ft;
  373. mutex_lock(&fn_mutex);
  374. list_add(&nm->list, &fcoe_netdevs);
  375. mutex_unlock(&fn_mutex);
  376. return 0;
  377. }
  378. static void fcoe_del_netdev_mapping(struct net_device *netdev)
  379. {
  380. struct fcoe_netdev_mapping *nm = NULL, *tmp;
  381. mutex_lock(&fn_mutex);
  382. list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
  383. if (nm->netdev == netdev) {
  384. list_del(&nm->list);
  385. kfree(nm);
  386. mutex_unlock(&fn_mutex);
  387. return;
  388. }
  389. }
  390. mutex_unlock(&fn_mutex);
  391. }
  392. /**
  393. * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
  394. * it was created
  395. *
  396. * Returns : ptr to the fcoe transport that supports this netdev or NULL
  397. * if not found.
  398. *
  399. * The ft_mutex should be held when this is called
  400. */
  401. static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
  402. {
  403. struct fcoe_transport *ft = NULL;
  404. struct fcoe_netdev_mapping *nm;
  405. mutex_lock(&fn_mutex);
  406. list_for_each_entry(nm, &fcoe_netdevs, list) {
  407. if (netdev == nm->netdev) {
  408. ft = nm->ft;
  409. mutex_unlock(&fn_mutex);
  410. return ft;
  411. }
  412. }
  413. mutex_unlock(&fn_mutex);
  414. return NULL;
  415. }
  416. /**
  417. * fcoe_if_to_netdev() - Parse a name buffer to get a net device
  418. * @buffer: The name of the net device
  419. *
  420. * Returns: NULL or a ptr to net_device
  421. */
  422. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  423. {
  424. char *cp;
  425. char ifname[IFNAMSIZ + 2];
  426. if (buffer) {
  427. strlcpy(ifname, buffer, IFNAMSIZ);
  428. cp = ifname + strlen(ifname);
  429. while (--cp >= ifname && *cp == '\n')
  430. *cp = '\0';
  431. return dev_get_by_name(&init_net, ifname);
  432. }
  433. return NULL;
  434. }
  435. /**
  436. * libfcoe_device_notification() - Handler for net device events
  437. * @notifier: The context of the notification
  438. * @event: The type of event
  439. * @ptr: The net device that the event was on
  440. *
  441. * This function is called by the Ethernet driver in case of link change event.
  442. *
  443. * Returns: 0 for success
  444. */
  445. static int libfcoe_device_notification(struct notifier_block *notifier,
  446. ulong event, void *ptr)
  447. {
  448. struct net_device *netdev = ptr;
  449. switch (event) {
  450. case NETDEV_UNREGISTER:
  451. printk(KERN_ERR "libfcoe_device_notification: NETDEV_UNREGISTER %s\n",
  452. netdev->name);
  453. fcoe_del_netdev_mapping(netdev);
  454. break;
  455. }
  456. return NOTIFY_OK;
  457. }
  458. /**
  459. * fcoe_transport_create() - Create a fcoe interface
  460. * @buffer: The name of the Ethernet interface to create on
  461. * @kp: The associated kernel param
  462. *
  463. * Called from sysfs. This holds the ft_mutex while calling the
  464. * registered fcoe transport's create function.
  465. *
  466. * Returns: 0 for success
  467. */
  468. static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
  469. {
  470. int rc = -ENODEV;
  471. struct net_device *netdev = NULL;
  472. struct fcoe_transport *ft = NULL;
  473. enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
  474. mutex_lock(&ft_mutex);
  475. netdev = fcoe_if_to_netdev(buffer);
  476. if (!netdev) {
  477. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
  478. goto out_nodev;
  479. }
  480. ft = fcoe_netdev_map_lookup(netdev);
  481. if (ft) {
  482. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  483. "FCoE instance on %s.\n",
  484. ft->name, netdev->name);
  485. rc = -EEXIST;
  486. goto out_putdev;
  487. }
  488. ft = fcoe_transport_lookup(netdev);
  489. if (!ft) {
  490. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  491. netdev->name);
  492. goto out_putdev;
  493. }
  494. rc = fcoe_add_netdev_mapping(netdev, ft);
  495. if (rc) {
  496. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  497. "for FCoE transport %s for %s.\n",
  498. ft->name, netdev->name);
  499. goto out_putdev;
  500. }
  501. /* pass to transport create */
  502. rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
  503. if (rc)
  504. fcoe_del_netdev_mapping(netdev);
  505. LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
  506. ft->name, (rc) ? "failed" : "succeeded",
  507. netdev->name);
  508. out_putdev:
  509. dev_put(netdev);
  510. out_nodev:
  511. mutex_unlock(&ft_mutex);
  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. mutex_lock(&ft_mutex);
  530. netdev = fcoe_if_to_netdev(buffer);
  531. if (!netdev) {
  532. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
  533. goto out_nodev;
  534. }
  535. ft = fcoe_netdev_map_lookup(netdev);
  536. if (!ft) {
  537. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  538. netdev->name);
  539. goto out_putdev;
  540. }
  541. /* pass to transport destroy */
  542. rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
  543. fcoe_del_netdev_mapping(netdev);
  544. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  545. ft->name, (rc) ? "failed" : "succeeded",
  546. netdev->name);
  547. out_putdev:
  548. dev_put(netdev);
  549. out_nodev:
  550. mutex_unlock(&ft_mutex);
  551. return rc;
  552. }
  553. /**
  554. * fcoe_transport_disable() - Disables a FCoE interface
  555. * @buffer: The name of the Ethernet interface to be disabled
  556. * @kp: The associated kernel parameter
  557. *
  558. * Called from sysfs.
  559. *
  560. * Returns: 0 for success
  561. */
  562. static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
  563. {
  564. int rc = -ENODEV;
  565. struct net_device *netdev = NULL;
  566. struct fcoe_transport *ft = NULL;
  567. mutex_lock(&ft_mutex);
  568. netdev = fcoe_if_to_netdev(buffer);
  569. if (!netdev)
  570. goto out_nodev;
  571. ft = fcoe_netdev_map_lookup(netdev);
  572. if (!ft)
  573. goto out_putdev;
  574. rc = ft->disable ? ft->disable(netdev) : -ENODEV;
  575. out_putdev:
  576. dev_put(netdev);
  577. out_nodev:
  578. mutex_unlock(&ft_mutex);
  579. if (rc == -ERESTARTSYS)
  580. return restart_syscall();
  581. else
  582. return rc;
  583. }
  584. /**
  585. * fcoe_transport_enable() - Enables a FCoE interface
  586. * @buffer: The name of the Ethernet interface to be enabled
  587. * @kp: The associated kernel parameter
  588. *
  589. * Called from sysfs.
  590. *
  591. * Returns: 0 for success
  592. */
  593. static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
  594. {
  595. int rc = -ENODEV;
  596. struct net_device *netdev = NULL;
  597. struct fcoe_transport *ft = NULL;
  598. mutex_lock(&ft_mutex);
  599. netdev = fcoe_if_to_netdev(buffer);
  600. if (!netdev)
  601. goto out_nodev;
  602. ft = fcoe_netdev_map_lookup(netdev);
  603. if (!ft)
  604. goto out_putdev;
  605. rc = ft->enable ? ft->enable(netdev) : -ENODEV;
  606. out_putdev:
  607. dev_put(netdev);
  608. out_nodev:
  609. mutex_unlock(&ft_mutex);
  610. return rc;
  611. }
  612. /**
  613. * libfcoe_init() - Initialization routine for libfcoe.ko
  614. */
  615. static int __init libfcoe_init(void)
  616. {
  617. fcoe_transport_init();
  618. return 0;
  619. }
  620. module_init(libfcoe_init);
  621. /**
  622. * libfcoe_exit() - Tear down libfcoe.ko
  623. */
  624. static void __exit libfcoe_exit(void)
  625. {
  626. fcoe_transport_exit();
  627. }
  628. module_exit(libfcoe_exit);