fcoe_transport.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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 *, const struct kernel_param *);
  32. static int fcoe_transport_destroy(const char *, const 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 *, const struct kernel_param *);
  37. static int fcoe_transport_disable(const char *, const 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(struct timer_list *t)
  404. {
  405. struct fcoe_port *port = from_timer(port, t, timer);
  406. fcoe_check_wait_queue(port->lport, NULL);
  407. }
  408. EXPORT_SYMBOL_GPL(fcoe_queue_timer);
  409. /**
  410. * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
  411. * @skb: The packet to be transmitted
  412. * @tlen: The total length of the trailer
  413. * @fps: The fcoe context
  414. *
  415. * This routine allocates a page for frame trailers. The page is re-used if
  416. * there is enough room left on it for the current trailer. If there isn't
  417. * enough buffer left a new page is allocated for the trailer. Reference to
  418. * the page from this function as well as the skbs using the page fragments
  419. * ensure that the page is freed at the appropriate time.
  420. *
  421. * Returns: 0 for success
  422. */
  423. int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
  424. struct fcoe_percpu_s *fps)
  425. {
  426. struct page *page;
  427. page = fps->crc_eof_page;
  428. if (!page) {
  429. page = alloc_page(GFP_ATOMIC);
  430. if (!page)
  431. return -ENOMEM;
  432. fps->crc_eof_page = page;
  433. fps->crc_eof_offset = 0;
  434. }
  435. get_page(page);
  436. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
  437. fps->crc_eof_offset, tlen);
  438. skb->len += tlen;
  439. skb->data_len += tlen;
  440. skb->truesize += tlen;
  441. fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
  442. if (fps->crc_eof_offset >= PAGE_SIZE) {
  443. fps->crc_eof_page = NULL;
  444. fps->crc_eof_offset = 0;
  445. put_page(page);
  446. }
  447. return 0;
  448. }
  449. EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
  450. /**
  451. * fcoe_transport_lookup - find an fcoe transport that matches a netdev
  452. * @netdev: The netdev to look for from all attached transports
  453. *
  454. * Returns : ptr to the fcoe transport that supports this netdev or NULL
  455. * if not found.
  456. *
  457. * The ft_mutex should be held when this is called
  458. */
  459. static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
  460. {
  461. struct fcoe_transport *ft = NULL;
  462. list_for_each_entry(ft, &fcoe_transports, list)
  463. if (ft->match && ft->match(netdev))
  464. return ft;
  465. return NULL;
  466. }
  467. /**
  468. * fcoe_transport_attach - Attaches an FCoE transport
  469. * @ft: The fcoe transport to be attached
  470. *
  471. * Returns : 0 for success
  472. */
  473. int fcoe_transport_attach(struct fcoe_transport *ft)
  474. {
  475. int rc = 0;
  476. mutex_lock(&ft_mutex);
  477. if (ft->attached) {
  478. LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
  479. ft->name);
  480. rc = -EEXIST;
  481. goto out_attach;
  482. }
  483. /* Add default transport to the tail */
  484. if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
  485. list_add(&ft->list, &fcoe_transports);
  486. else
  487. list_add_tail(&ft->list, &fcoe_transports);
  488. ft->attached = true;
  489. LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
  490. out_attach:
  491. mutex_unlock(&ft_mutex);
  492. return rc;
  493. }
  494. EXPORT_SYMBOL(fcoe_transport_attach);
  495. /**
  496. * fcoe_transport_detach - Detaches an FCoE transport
  497. * @ft: The fcoe transport to be attached
  498. *
  499. * Returns : 0 for success
  500. */
  501. int fcoe_transport_detach(struct fcoe_transport *ft)
  502. {
  503. int rc = 0;
  504. struct fcoe_netdev_mapping *nm = NULL, *tmp;
  505. mutex_lock(&ft_mutex);
  506. if (!ft->attached) {
  507. LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
  508. ft->name);
  509. rc = -ENODEV;
  510. goto out_attach;
  511. }
  512. /* remove netdev mapping for this transport as it is going away */
  513. mutex_lock(&fn_mutex);
  514. list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
  515. if (nm->ft == ft) {
  516. LIBFCOE_TRANSPORT_DBG("transport %s going away, "
  517. "remove its netdev mapping for %s\n",
  518. ft->name, nm->netdev->name);
  519. list_del(&nm->list);
  520. kfree(nm);
  521. }
  522. }
  523. mutex_unlock(&fn_mutex);
  524. list_del(&ft->list);
  525. ft->attached = false;
  526. LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
  527. out_attach:
  528. mutex_unlock(&ft_mutex);
  529. return rc;
  530. }
  531. EXPORT_SYMBOL(fcoe_transport_detach);
  532. static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
  533. {
  534. int i, j;
  535. struct fcoe_transport *ft = NULL;
  536. i = j = sprintf(buffer, "Attached FCoE transports:");
  537. mutex_lock(&ft_mutex);
  538. list_for_each_entry(ft, &fcoe_transports, list) {
  539. if (i >= PAGE_SIZE - IFNAMSIZ)
  540. break;
  541. i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
  542. }
  543. mutex_unlock(&ft_mutex);
  544. if (i == j)
  545. i += snprintf(&buffer[i], IFNAMSIZ, "none");
  546. return i;
  547. }
  548. static int __init fcoe_transport_init(void)
  549. {
  550. register_netdevice_notifier(&libfcoe_notifier);
  551. return 0;
  552. }
  553. static int fcoe_transport_exit(void)
  554. {
  555. struct fcoe_transport *ft;
  556. unregister_netdevice_notifier(&libfcoe_notifier);
  557. mutex_lock(&ft_mutex);
  558. list_for_each_entry(ft, &fcoe_transports, list)
  559. printk(KERN_ERR "FCoE transport %s is still attached!\n",
  560. ft->name);
  561. mutex_unlock(&ft_mutex);
  562. return 0;
  563. }
  564. static int fcoe_add_netdev_mapping(struct net_device *netdev,
  565. struct fcoe_transport *ft)
  566. {
  567. struct fcoe_netdev_mapping *nm;
  568. nm = kmalloc(sizeof(*nm), GFP_KERNEL);
  569. if (!nm) {
  570. printk(KERN_ERR "Unable to allocate netdev_mapping");
  571. return -ENOMEM;
  572. }
  573. nm->netdev = netdev;
  574. nm->ft = ft;
  575. mutex_lock(&fn_mutex);
  576. list_add(&nm->list, &fcoe_netdevs);
  577. mutex_unlock(&fn_mutex);
  578. return 0;
  579. }
  580. static void fcoe_del_netdev_mapping(struct net_device *netdev)
  581. {
  582. struct fcoe_netdev_mapping *nm = NULL, *tmp;
  583. mutex_lock(&fn_mutex);
  584. list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
  585. if (nm->netdev == netdev) {
  586. list_del(&nm->list);
  587. kfree(nm);
  588. mutex_unlock(&fn_mutex);
  589. return;
  590. }
  591. }
  592. mutex_unlock(&fn_mutex);
  593. }
  594. /**
  595. * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
  596. * it was created
  597. *
  598. * Returns : ptr to the fcoe transport that supports this netdev or NULL
  599. * if not found.
  600. *
  601. * The ft_mutex should be held when this is called
  602. */
  603. static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
  604. {
  605. struct fcoe_transport *ft = NULL;
  606. struct fcoe_netdev_mapping *nm;
  607. mutex_lock(&fn_mutex);
  608. list_for_each_entry(nm, &fcoe_netdevs, list) {
  609. if (netdev == nm->netdev) {
  610. ft = nm->ft;
  611. mutex_unlock(&fn_mutex);
  612. return ft;
  613. }
  614. }
  615. mutex_unlock(&fn_mutex);
  616. return NULL;
  617. }
  618. /**
  619. * fcoe_if_to_netdev() - Parse a name buffer to get a net device
  620. * @buffer: The name of the net device
  621. *
  622. * Returns: NULL or a ptr to net_device
  623. */
  624. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  625. {
  626. char *cp;
  627. char ifname[IFNAMSIZ + 2];
  628. if (buffer) {
  629. strlcpy(ifname, buffer, IFNAMSIZ);
  630. cp = ifname + strlen(ifname);
  631. while (--cp >= ifname && *cp == '\n')
  632. *cp = '\0';
  633. return dev_get_by_name(&init_net, ifname);
  634. }
  635. return NULL;
  636. }
  637. /**
  638. * libfcoe_device_notification() - Handler for net device events
  639. * @notifier: The context of the notification
  640. * @event: The type of event
  641. * @ptr: The net device that the event was on
  642. *
  643. * This function is called by the Ethernet driver in case of link change event.
  644. *
  645. * Returns: 0 for success
  646. */
  647. static int libfcoe_device_notification(struct notifier_block *notifier,
  648. ulong event, void *ptr)
  649. {
  650. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  651. switch (event) {
  652. case NETDEV_UNREGISTER:
  653. LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
  654. netdev->name);
  655. fcoe_del_netdev_mapping(netdev);
  656. break;
  657. }
  658. return NOTIFY_OK;
  659. }
  660. ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
  661. const char *buf, size_t count)
  662. {
  663. struct net_device *netdev = NULL;
  664. struct fcoe_transport *ft = NULL;
  665. int rc = 0;
  666. int err;
  667. mutex_lock(&ft_mutex);
  668. netdev = fcoe_if_to_netdev(buf);
  669. if (!netdev) {
  670. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
  671. rc = -ENODEV;
  672. goto out_nodev;
  673. }
  674. ft = fcoe_netdev_map_lookup(netdev);
  675. if (ft) {
  676. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  677. "FCoE instance on %s.\n",
  678. ft->name, netdev->name);
  679. rc = -EEXIST;
  680. goto out_putdev;
  681. }
  682. ft = fcoe_transport_lookup(netdev);
  683. if (!ft) {
  684. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  685. netdev->name);
  686. rc = -ENODEV;
  687. goto out_putdev;
  688. }
  689. /* pass to transport create */
  690. err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
  691. if (err) {
  692. fcoe_del_netdev_mapping(netdev);
  693. rc = -ENOMEM;
  694. goto out_putdev;
  695. }
  696. err = fcoe_add_netdev_mapping(netdev, ft);
  697. if (err) {
  698. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  699. "for FCoE transport %s for %s.\n",
  700. ft->name, netdev->name);
  701. rc = -ENODEV;
  702. goto out_putdev;
  703. }
  704. LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
  705. ft->name, netdev->name);
  706. out_putdev:
  707. dev_put(netdev);
  708. out_nodev:
  709. mutex_unlock(&ft_mutex);
  710. if (rc)
  711. return rc;
  712. return count;
  713. }
  714. ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
  715. const char *buf, size_t count)
  716. {
  717. int rc = -ENODEV;
  718. struct net_device *netdev = NULL;
  719. struct fcoe_transport *ft = NULL;
  720. mutex_lock(&ft_mutex);
  721. netdev = fcoe_if_to_netdev(buf);
  722. if (!netdev) {
  723. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
  724. goto out_nodev;
  725. }
  726. ft = fcoe_netdev_map_lookup(netdev);
  727. if (!ft) {
  728. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  729. netdev->name);
  730. goto out_putdev;
  731. }
  732. /* pass to transport destroy */
  733. rc = ft->destroy(netdev);
  734. if (rc)
  735. goto out_putdev;
  736. fcoe_del_netdev_mapping(netdev);
  737. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  738. ft->name, (rc) ? "failed" : "succeeded",
  739. netdev->name);
  740. rc = count; /* required for successful return */
  741. out_putdev:
  742. dev_put(netdev);
  743. out_nodev:
  744. mutex_unlock(&ft_mutex);
  745. return rc;
  746. }
  747. EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
  748. /**
  749. * fcoe_transport_create() - Create a fcoe interface
  750. * @buffer: The name of the Ethernet interface to create on
  751. * @kp: The associated kernel param
  752. *
  753. * Called from sysfs. This holds the ft_mutex while calling the
  754. * registered fcoe transport's create function.
  755. *
  756. * Returns: 0 for success
  757. */
  758. static int fcoe_transport_create(const char *buffer,
  759. const struct kernel_param *kp)
  760. {
  761. int rc = -ENODEV;
  762. struct net_device *netdev = NULL;
  763. struct fcoe_transport *ft = NULL;
  764. enum fip_mode fip_mode = (enum fip_mode)kp->arg;
  765. mutex_lock(&ft_mutex);
  766. netdev = fcoe_if_to_netdev(buffer);
  767. if (!netdev) {
  768. LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
  769. goto out_nodev;
  770. }
  771. ft = fcoe_netdev_map_lookup(netdev);
  772. if (ft) {
  773. LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
  774. "FCoE instance on %s.\n",
  775. ft->name, netdev->name);
  776. rc = -EEXIST;
  777. goto out_putdev;
  778. }
  779. ft = fcoe_transport_lookup(netdev);
  780. if (!ft) {
  781. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  782. netdev->name);
  783. goto out_putdev;
  784. }
  785. rc = fcoe_add_netdev_mapping(netdev, ft);
  786. if (rc) {
  787. LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
  788. "for FCoE transport %s for %s.\n",
  789. ft->name, netdev->name);
  790. goto out_putdev;
  791. }
  792. /* pass to transport create */
  793. rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
  794. if (rc)
  795. fcoe_del_netdev_mapping(netdev);
  796. LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
  797. ft->name, (rc) ? "failed" : "succeeded",
  798. netdev->name);
  799. out_putdev:
  800. dev_put(netdev);
  801. out_nodev:
  802. mutex_unlock(&ft_mutex);
  803. return rc;
  804. }
  805. /**
  806. * fcoe_transport_destroy() - Destroy a FCoE interface
  807. * @buffer: The name of the Ethernet interface to be destroyed
  808. * @kp: The associated kernel parameter
  809. *
  810. * Called from sysfs. This holds the ft_mutex while calling the
  811. * registered fcoe transport's destroy function.
  812. *
  813. * Returns: 0 for success
  814. */
  815. static int fcoe_transport_destroy(const char *buffer,
  816. const struct kernel_param *kp)
  817. {
  818. int rc = -ENODEV;
  819. struct net_device *netdev = NULL;
  820. struct fcoe_transport *ft = NULL;
  821. mutex_lock(&ft_mutex);
  822. netdev = fcoe_if_to_netdev(buffer);
  823. if (!netdev) {
  824. LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
  825. goto out_nodev;
  826. }
  827. ft = fcoe_netdev_map_lookup(netdev);
  828. if (!ft) {
  829. LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
  830. netdev->name);
  831. goto out_putdev;
  832. }
  833. /* pass to transport destroy */
  834. rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
  835. fcoe_del_netdev_mapping(netdev);
  836. LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
  837. ft->name, (rc) ? "failed" : "succeeded",
  838. netdev->name);
  839. out_putdev:
  840. dev_put(netdev);
  841. out_nodev:
  842. mutex_unlock(&ft_mutex);
  843. return rc;
  844. }
  845. /**
  846. * fcoe_transport_disable() - Disables a FCoE interface
  847. * @buffer: The name of the Ethernet interface to be disabled
  848. * @kp: The associated kernel parameter
  849. *
  850. * Called from sysfs.
  851. *
  852. * Returns: 0 for success
  853. */
  854. static int fcoe_transport_disable(const char *buffer,
  855. const struct kernel_param *kp)
  856. {
  857. int rc = -ENODEV;
  858. struct net_device *netdev = NULL;
  859. struct fcoe_transport *ft = NULL;
  860. mutex_lock(&ft_mutex);
  861. netdev = fcoe_if_to_netdev(buffer);
  862. if (!netdev)
  863. goto out_nodev;
  864. ft = fcoe_netdev_map_lookup(netdev);
  865. if (!ft)
  866. goto out_putdev;
  867. rc = ft->disable ? ft->disable(netdev) : -ENODEV;
  868. out_putdev:
  869. dev_put(netdev);
  870. out_nodev:
  871. mutex_unlock(&ft_mutex);
  872. return rc;
  873. }
  874. /**
  875. * fcoe_transport_enable() - Enables a FCoE interface
  876. * @buffer: The name of the Ethernet interface to be enabled
  877. * @kp: The associated kernel parameter
  878. *
  879. * Called from sysfs.
  880. *
  881. * Returns: 0 for success
  882. */
  883. static int fcoe_transport_enable(const char *buffer,
  884. const struct kernel_param *kp)
  885. {
  886. int rc = -ENODEV;
  887. struct net_device *netdev = NULL;
  888. struct fcoe_transport *ft = NULL;
  889. mutex_lock(&ft_mutex);
  890. netdev = fcoe_if_to_netdev(buffer);
  891. if (!netdev)
  892. goto out_nodev;
  893. ft = fcoe_netdev_map_lookup(netdev);
  894. if (!ft)
  895. goto out_putdev;
  896. rc = ft->enable ? ft->enable(netdev) : -ENODEV;
  897. out_putdev:
  898. dev_put(netdev);
  899. out_nodev:
  900. mutex_unlock(&ft_mutex);
  901. return rc;
  902. }
  903. /**
  904. * libfcoe_init() - Initialization routine for libfcoe.ko
  905. */
  906. static int __init libfcoe_init(void)
  907. {
  908. int rc = 0;
  909. rc = fcoe_transport_init();
  910. if (rc)
  911. return rc;
  912. rc = fcoe_sysfs_setup();
  913. if (rc)
  914. fcoe_transport_exit();
  915. return rc;
  916. }
  917. module_init(libfcoe_init);
  918. /**
  919. * libfcoe_exit() - Tear down libfcoe.ko
  920. */
  921. static void __exit libfcoe_exit(void)
  922. {
  923. fcoe_sysfs_teardown();
  924. fcoe_transport_exit();
  925. }
  926. module_exit(libfcoe_exit);