fcoe_transport.c 27 KB

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