fcoe_transport.c 21 KB

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