fcoe_transport.c 27 KB

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