fcoe_transport.c 26 KB

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