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