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