fcoe.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. /*
  2. * Copyright(c) 2007 - 2009 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/module.h>
  20. #include <linux/version.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_ether.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/crc32.h>
  28. #include <linux/cpu.h>
  29. #include <linux/fs.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/ctype.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsicam.h>
  34. #include <scsi/scsi_transport.h>
  35. #include <scsi/scsi_transport_fc.h>
  36. #include <net/rtnetlink.h>
  37. #include <scsi/fc/fc_encaps.h>
  38. #include <scsi/fc/fc_fip.h>
  39. #include <scsi/libfc.h>
  40. #include <scsi/fc_frame.h>
  41. #include <scsi/libfcoe.h>
  42. #include "fcoe.h"
  43. MODULE_AUTHOR("Open-FCoE.org");
  44. MODULE_DESCRIPTION("FCoE");
  45. MODULE_LICENSE("GPL v2");
  46. /* Performance tuning parameters for fcoe */
  47. static unsigned int fcoe_ddp_min;
  48. module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
  49. MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
  50. "Direct Data Placement (DDP).");
  51. DEFINE_MUTEX(fcoe_config_mutex);
  52. /* fcoe host list */
  53. LIST_HEAD(fcoe_hostlist);
  54. DEFINE_RWLOCK(fcoe_hostlist_lock);
  55. DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
  56. /* Function Prototypes */
  57. static int fcoe_reset(struct Scsi_Host *shost);
  58. static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
  59. static int fcoe_rcv(struct sk_buff *, struct net_device *,
  60. struct packet_type *, struct net_device *);
  61. static int fcoe_percpu_receive_thread(void *arg);
  62. static void fcoe_clean_pending_queue(struct fc_lport *lp);
  63. static void fcoe_percpu_clean(struct fc_lport *lp);
  64. static int fcoe_link_ok(struct fc_lport *lp);
  65. static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
  66. static int fcoe_hostlist_add(const struct fc_lport *);
  67. static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
  68. static int fcoe_device_notification(struct notifier_block *, ulong, void *);
  69. static void fcoe_dev_setup(void);
  70. static void fcoe_dev_cleanup(void);
  71. static struct fcoe_interface *
  72. fcoe_hostlist_lookup_port(const struct net_device *dev);
  73. /* notification function from net device */
  74. static struct notifier_block fcoe_notifier = {
  75. .notifier_call = fcoe_device_notification,
  76. };
  77. static struct scsi_transport_template *scsi_transport_fcoe_sw;
  78. struct fc_function_template fcoe_transport_function = {
  79. .show_host_node_name = 1,
  80. .show_host_port_name = 1,
  81. .show_host_supported_classes = 1,
  82. .show_host_supported_fc4s = 1,
  83. .show_host_active_fc4s = 1,
  84. .show_host_maxframe_size = 1,
  85. .show_host_port_id = 1,
  86. .show_host_supported_speeds = 1,
  87. .get_host_speed = fc_get_host_speed,
  88. .show_host_speed = 1,
  89. .show_host_port_type = 1,
  90. .get_host_port_state = fc_get_host_port_state,
  91. .show_host_port_state = 1,
  92. .show_host_symbolic_name = 1,
  93. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  94. .show_rport_maxframe_size = 1,
  95. .show_rport_supported_classes = 1,
  96. .show_host_fabric_name = 1,
  97. .show_starget_node_name = 1,
  98. .show_starget_port_name = 1,
  99. .show_starget_port_id = 1,
  100. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  101. .show_rport_dev_loss_tmo = 1,
  102. .get_fc_host_stats = fc_get_host_stats,
  103. .issue_fc_host_lip = fcoe_reset,
  104. .terminate_rport_io = fc_rport_terminate_io,
  105. };
  106. static struct scsi_host_template fcoe_shost_template = {
  107. .module = THIS_MODULE,
  108. .name = "FCoE Driver",
  109. .proc_name = FCOE_NAME,
  110. .queuecommand = fc_queuecommand,
  111. .eh_abort_handler = fc_eh_abort,
  112. .eh_device_reset_handler = fc_eh_device_reset,
  113. .eh_host_reset_handler = fc_eh_host_reset,
  114. .slave_alloc = fc_slave_alloc,
  115. .change_queue_depth = fc_change_queue_depth,
  116. .change_queue_type = fc_change_queue_type,
  117. .this_id = -1,
  118. .cmd_per_lun = 32,
  119. .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
  120. .use_clustering = ENABLE_CLUSTERING,
  121. .sg_tablesize = SG_ALL,
  122. .max_sectors = 0xffff,
  123. };
  124. static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
  125. struct packet_type *ptype,
  126. struct net_device *orig_dev);
  127. /**
  128. * fcoe_interface_setup()
  129. * @fcoe: new fcoe_interface
  130. * @netdev : ptr to the associated netdevice struct
  131. *
  132. * Returns : 0 for success
  133. * Locking: must be called with the RTNL mutex held
  134. */
  135. static int fcoe_interface_setup(struct fcoe_interface *fcoe,
  136. struct net_device *netdev)
  137. {
  138. struct fcoe_ctlr *fip = &fcoe->ctlr;
  139. struct netdev_hw_addr *ha;
  140. u8 flogi_maddr[ETH_ALEN];
  141. fcoe->netdev = netdev;
  142. /* Do not support for bonding device */
  143. if ((netdev->priv_flags & IFF_MASTER_ALB) ||
  144. (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
  145. (netdev->priv_flags & IFF_MASTER_8023AD)) {
  146. return -EOPNOTSUPP;
  147. }
  148. /* look for SAN MAC address, if multiple SAN MACs exist, only
  149. * use the first one for SPMA */
  150. rcu_read_lock();
  151. for_each_dev_addr(netdev, ha) {
  152. if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
  153. (is_valid_ether_addr(fip->ctl_src_addr))) {
  154. memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
  155. fip->spma = 1;
  156. break;
  157. }
  158. }
  159. rcu_read_unlock();
  160. /* setup Source Mac Address */
  161. if (!fip->spma)
  162. memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
  163. /*
  164. * Add FCoE MAC address as second unicast MAC address
  165. * or enter promiscuous mode if not capable of listening
  166. * for multiple unicast MACs.
  167. */
  168. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  169. dev_unicast_add(netdev, flogi_maddr);
  170. if (fip->spma)
  171. dev_unicast_add(netdev, fip->ctl_src_addr);
  172. dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
  173. /*
  174. * setup the receive function from ethernet driver
  175. * on the ethertype for the given device
  176. */
  177. fcoe->fcoe_packet_type.func = fcoe_rcv;
  178. fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  179. fcoe->fcoe_packet_type.dev = netdev;
  180. dev_add_pack(&fcoe->fcoe_packet_type);
  181. fcoe->fip_packet_type.func = fcoe_fip_recv;
  182. fcoe->fip_packet_type.type = htons(ETH_P_FIP);
  183. fcoe->fip_packet_type.dev = netdev;
  184. dev_add_pack(&fcoe->fip_packet_type);
  185. return 0;
  186. }
  187. static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
  188. static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
  189. static void fcoe_destroy_work(struct work_struct *work);
  190. /**
  191. * fcoe_interface_create()
  192. * @netdev: network interface
  193. *
  194. * Returns: pointer to a struct fcoe_interface or NULL on error
  195. */
  196. static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
  197. {
  198. struct fcoe_interface *fcoe;
  199. fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
  200. if (!fcoe) {
  201. FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
  202. return NULL;
  203. }
  204. dev_hold(netdev);
  205. kref_init(&fcoe->kref);
  206. /*
  207. * Initialize FIP.
  208. */
  209. fcoe_ctlr_init(&fcoe->ctlr);
  210. fcoe->ctlr.send = fcoe_fip_send;
  211. fcoe->ctlr.update_mac = fcoe_update_src_mac;
  212. fcoe_interface_setup(fcoe, netdev);
  213. return fcoe;
  214. }
  215. /**
  216. * fcoe_interface_cleanup() - clean up netdev configurations
  217. * @fcoe:
  218. *
  219. * Caller must be holding the RTNL mutex
  220. */
  221. void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
  222. {
  223. struct net_device *netdev = fcoe->netdev;
  224. struct fcoe_ctlr *fip = &fcoe->ctlr;
  225. u8 flogi_maddr[ETH_ALEN];
  226. /*
  227. * Don't listen for Ethernet packets anymore.
  228. * synchronize_net() ensures that the packet handlers are not running
  229. * on another CPU. dev_remove_pack() would do that, this calls the
  230. * unsyncronized version __dev_remove_pack() to avoid multiple delays.
  231. */
  232. __dev_remove_pack(&fcoe->fcoe_packet_type);
  233. __dev_remove_pack(&fcoe->fip_packet_type);
  234. synchronize_net();
  235. /* Delete secondary MAC addresses */
  236. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  237. dev_unicast_delete(netdev, flogi_maddr);
  238. if (!is_zero_ether_addr(fip->data_src_addr))
  239. dev_unicast_delete(netdev, fip->data_src_addr);
  240. if (fip->spma)
  241. dev_unicast_delete(netdev, fip->ctl_src_addr);
  242. dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
  243. }
  244. /**
  245. * fcoe_interface_release() - fcoe_port kref release function
  246. * @kref: embedded reference count in an fcoe_interface struct
  247. */
  248. static void fcoe_interface_release(struct kref *kref)
  249. {
  250. struct fcoe_interface *fcoe;
  251. struct net_device *netdev;
  252. fcoe = container_of(kref, struct fcoe_interface, kref);
  253. netdev = fcoe->netdev;
  254. /* tear-down the FCoE controller */
  255. fcoe_ctlr_destroy(&fcoe->ctlr);
  256. kfree(fcoe);
  257. dev_put(netdev);
  258. }
  259. /**
  260. * fcoe_interface_get()
  261. * @fcoe:
  262. */
  263. static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
  264. {
  265. kref_get(&fcoe->kref);
  266. }
  267. /**
  268. * fcoe_interface_put()
  269. * @fcoe:
  270. */
  271. static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
  272. {
  273. kref_put(&fcoe->kref, fcoe_interface_release);
  274. }
  275. /**
  276. * fcoe_fip_recv - handle a received FIP frame.
  277. * @skb: the receive skb
  278. * @dev: associated &net_device
  279. * @ptype: the &packet_type structure which was used to register this handler.
  280. * @orig_dev: original receive &net_device, in case @dev is a bond.
  281. *
  282. * Returns: 0 for success
  283. */
  284. static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
  285. struct packet_type *ptype,
  286. struct net_device *orig_dev)
  287. {
  288. struct fcoe_interface *fcoe;
  289. fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
  290. fcoe_ctlr_recv(&fcoe->ctlr, skb);
  291. return 0;
  292. }
  293. /**
  294. * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
  295. * @fip: FCoE controller.
  296. * @skb: FIP Packet.
  297. */
  298. static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  299. {
  300. skb->dev = fcoe_from_ctlr(fip)->netdev;
  301. dev_queue_xmit(skb);
  302. }
  303. /**
  304. * fcoe_update_src_mac() - Update Ethernet MAC filters.
  305. * @fip: FCoE controller.
  306. * @old: Unicast MAC address to delete if the MAC is non-zero.
  307. * @new: Unicast MAC address to add.
  308. *
  309. * Remove any previously-set unicast MAC filter.
  310. * Add secondary FCoE MAC address filter for our OUI.
  311. */
  312. static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
  313. {
  314. struct fcoe_interface *fcoe;
  315. fcoe = fcoe_from_ctlr(fip);
  316. rtnl_lock();
  317. if (!is_zero_ether_addr(old))
  318. dev_unicast_delete(fcoe->netdev, old);
  319. dev_unicast_add(fcoe->netdev, new);
  320. rtnl_unlock();
  321. }
  322. /**
  323. * fcoe_lport_config() - sets up the fc_lport
  324. * @lp: ptr to the fc_lport
  325. *
  326. * Returns: 0 for success
  327. */
  328. static int fcoe_lport_config(struct fc_lport *lp)
  329. {
  330. lp->link_up = 0;
  331. lp->qfull = 0;
  332. lp->max_retry_count = 3;
  333. lp->max_rport_retry_count = 3;
  334. lp->e_d_tov = 2 * 1000; /* FC-FS default */
  335. lp->r_a_tov = 2 * 2 * 1000;
  336. lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  337. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  338. fc_lport_init_stats(lp);
  339. /* lport fc_lport related configuration */
  340. fc_lport_config(lp);
  341. /* offload related configuration */
  342. lp->crc_offload = 0;
  343. lp->seq_offload = 0;
  344. lp->lro_enabled = 0;
  345. lp->lro_xid = 0;
  346. lp->lso_max = 0;
  347. return 0;
  348. }
  349. /**
  350. * fcoe_queue_timer() - fcoe queue timer
  351. * @lp: the fc_lport pointer
  352. *
  353. * Calls fcoe_check_wait_queue on timeout
  354. *
  355. */
  356. static void fcoe_queue_timer(ulong lp)
  357. {
  358. fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
  359. }
  360. /**
  361. * fcoe_netdev_config() - Set up netdev for SW FCoE
  362. * @lp : ptr to the fc_lport
  363. * @netdev : ptr to the associated netdevice struct
  364. *
  365. * Must be called after fcoe_lport_config() as it will use lport mutex
  366. *
  367. * Returns : 0 for success
  368. */
  369. static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
  370. {
  371. u32 mfs;
  372. u64 wwnn, wwpn;
  373. struct fcoe_interface *fcoe;
  374. struct fcoe_port *port;
  375. /* Setup lport private data to point to fcoe softc */
  376. port = lport_priv(lp);
  377. fcoe = port->fcoe;
  378. /*
  379. * Determine max frame size based on underlying device and optional
  380. * user-configured limit. If the MFS is too low, fcoe_link_ok()
  381. * will return 0, so do this first.
  382. */
  383. mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
  384. sizeof(struct fcoe_crc_eof));
  385. if (fc_set_mfs(lp, mfs))
  386. return -EINVAL;
  387. /* offload features support */
  388. if (netdev->features & NETIF_F_SG)
  389. lp->sg_supp = 1;
  390. if (netdev->features & NETIF_F_FCOE_CRC) {
  391. lp->crc_offload = 1;
  392. FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
  393. }
  394. if (netdev->features & NETIF_F_FSO) {
  395. lp->seq_offload = 1;
  396. lp->lso_max = netdev->gso_max_size;
  397. FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
  398. lp->lso_max);
  399. }
  400. if (netdev->fcoe_ddp_xid) {
  401. lp->lro_enabled = 1;
  402. lp->lro_xid = netdev->fcoe_ddp_xid;
  403. FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
  404. lp->lro_xid);
  405. }
  406. skb_queue_head_init(&port->fcoe_pending_queue);
  407. port->fcoe_pending_queue_active = 0;
  408. setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
  409. wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
  410. fc_set_wwnn(lp, wwnn);
  411. /* XXX - 3rd arg needs to be vlan id */
  412. wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
  413. fc_set_wwpn(lp, wwpn);
  414. return 0;
  415. }
  416. /**
  417. * fcoe_shost_config() - Sets up fc_lport->host
  418. * @lp : ptr to the fc_lport
  419. * @shost : ptr to the associated scsi host
  420. * @dev : device associated to scsi host
  421. *
  422. * Must be called after fcoe_lport_config() and fcoe_netdev_config()
  423. *
  424. * Returns : 0 for success
  425. */
  426. static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
  427. struct device *dev)
  428. {
  429. int rc = 0;
  430. /* lport scsi host config */
  431. lp->host = shost;
  432. lp->host->max_lun = FCOE_MAX_LUN;
  433. lp->host->max_id = FCOE_MAX_FCP_TARGET;
  434. lp->host->max_channel = 0;
  435. lp->host->transportt = scsi_transport_fcoe_sw;
  436. /* add the new host to the SCSI-ml */
  437. rc = scsi_add_host(lp->host, dev);
  438. if (rc) {
  439. FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
  440. "error on scsi_add_host\n");
  441. return rc;
  442. }
  443. sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
  444. FCOE_NAME, FCOE_VERSION,
  445. fcoe_netdev(lp)->name);
  446. return 0;
  447. }
  448. /*
  449. * fcoe_oem_match() - match for read types IO
  450. * @fp: the fc_frame for new IO.
  451. *
  452. * Returns : true for read types IO, otherwise returns false.
  453. */
  454. bool fcoe_oem_match(struct fc_frame *fp)
  455. {
  456. return fc_fcp_is_read(fr_fsp(fp)) &&
  457. (fr_fsp(fp)->data_len > fcoe_ddp_min);
  458. }
  459. /**
  460. * fcoe_em_config() - allocates em for this lport
  461. * @lp: the fcoe that em is to allocated for
  462. *
  463. * Called with write fcoe_hostlist_lock held.
  464. *
  465. * Returns : 0 on success
  466. */
  467. static inline int fcoe_em_config(struct fc_lport *lp)
  468. {
  469. struct fcoe_port *port = lport_priv(lp);
  470. struct fcoe_interface *fcoe = port->fcoe;
  471. struct fcoe_interface *oldfcoe = NULL;
  472. struct net_device *old_real_dev, *cur_real_dev;
  473. u16 min_xid = FCOE_MIN_XID;
  474. u16 max_xid = FCOE_MAX_XID;
  475. /*
  476. * Check if need to allocate an em instance for
  477. * offload exchange ids to be shared across all VN_PORTs/lport.
  478. */
  479. if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
  480. lp->lro_xid = 0;
  481. goto skip_oem;
  482. }
  483. /*
  484. * Reuse existing offload em instance in case
  485. * it is already allocated on real eth device
  486. */
  487. if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  488. cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
  489. else
  490. cur_real_dev = fcoe->netdev;
  491. list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
  492. if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  493. old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
  494. else
  495. old_real_dev = oldfcoe->netdev;
  496. if (cur_real_dev == old_real_dev) {
  497. fcoe->oem = oldfcoe->oem;
  498. break;
  499. }
  500. }
  501. if (fcoe->oem) {
  502. if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
  503. printk(KERN_ERR "fcoe_em_config: failed to add "
  504. "offload em:%p on interface:%s\n",
  505. fcoe->oem, fcoe->netdev->name);
  506. return -ENOMEM;
  507. }
  508. } else {
  509. fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
  510. FCOE_MIN_XID, lp->lro_xid,
  511. fcoe_oem_match);
  512. if (!fcoe->oem) {
  513. printk(KERN_ERR "fcoe_em_config: failed to allocate "
  514. "em for offload exches on interface:%s\n",
  515. fcoe->netdev->name);
  516. return -ENOMEM;
  517. }
  518. }
  519. /*
  520. * Exclude offload EM xid range from next EM xid range.
  521. */
  522. min_xid += lp->lro_xid + 1;
  523. skip_oem:
  524. if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
  525. printk(KERN_ERR "fcoe_em_config: failed to "
  526. "allocate em on interface %s\n", fcoe->netdev->name);
  527. return -ENOMEM;
  528. }
  529. return 0;
  530. }
  531. /**
  532. * fcoe_if_destroy() - FCoE software HBA tear-down function
  533. * @lport: fc_lport to destroy
  534. */
  535. static void fcoe_if_destroy(struct fc_lport *lport)
  536. {
  537. struct fcoe_port *port = lport_priv(lport);
  538. struct fcoe_interface *fcoe = port->fcoe;
  539. struct net_device *netdev = fcoe->netdev;
  540. FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
  541. /* Logout of the fabric */
  542. fc_fabric_logoff(lport);
  543. /* Cleanup the fc_lport */
  544. fc_lport_destroy(lport);
  545. fc_fcp_destroy(lport);
  546. /* Stop the transmit retry timer */
  547. del_timer_sync(&port->timer);
  548. /* Free existing transmit skbs */
  549. fcoe_clean_pending_queue(lport);
  550. /* receives may not be stopped until after this */
  551. fcoe_interface_put(fcoe);
  552. /* Free queued packets for the per-CPU receive threads */
  553. fcoe_percpu_clean(lport);
  554. /* Detach from the scsi-ml */
  555. fc_remove_host(lport->host);
  556. scsi_remove_host(lport->host);
  557. /* There are no more rports or I/O, free the EM */
  558. fc_exch_mgr_free(lport);
  559. /* Free memory used by statistical counters */
  560. fc_lport_free_stats(lport);
  561. /* Release the Scsi_Host */
  562. scsi_host_put(lport->host);
  563. }
  564. /*
  565. * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
  566. * @lp: the corresponding fc_lport
  567. * @xid: the exchange id for this ddp transfer
  568. * @sgl: the scatterlist describing this transfer
  569. * @sgc: number of sg items
  570. *
  571. * Returns : 0 no ddp
  572. */
  573. static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
  574. struct scatterlist *sgl, unsigned int sgc)
  575. {
  576. struct net_device *n = fcoe_netdev(lp);
  577. if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
  578. return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
  579. return 0;
  580. }
  581. /*
  582. * fcoe_ddp_done - calls LLD's ddp_done through net_device
  583. * @lp: the corresponding fc_lport
  584. * @xid: the exchange id for this ddp transfer
  585. *
  586. * Returns : the length of data that have been completed by ddp
  587. */
  588. static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
  589. {
  590. struct net_device *n = fcoe_netdev(lp);
  591. if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
  592. return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
  593. return 0;
  594. }
  595. static struct libfc_function_template fcoe_libfc_fcn_templ = {
  596. .frame_send = fcoe_xmit,
  597. .ddp_setup = fcoe_ddp_setup,
  598. .ddp_done = fcoe_ddp_done,
  599. };
  600. /**
  601. * fcoe_if_create() - this function creates the fcoe port
  602. * @fcoe: fcoe_interface structure to create an fc_lport instance on
  603. * @parent: device pointer to be the parent in sysfs for the SCSI host
  604. *
  605. * Creates fc_lport struct and scsi_host for lport, configures lport.
  606. *
  607. * Returns : The allocated fc_lport or an error pointer
  608. */
  609. static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
  610. struct device *parent)
  611. {
  612. int rc;
  613. struct fc_lport *lport = NULL;
  614. struct fcoe_port *port;
  615. struct Scsi_Host *shost;
  616. struct net_device *netdev = fcoe->netdev;
  617. FCOE_NETDEV_DBG(netdev, "Create Interface\n");
  618. shost = libfc_host_alloc(&fcoe_shost_template,
  619. sizeof(struct fcoe_port));
  620. if (!shost) {
  621. FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
  622. rc = -ENOMEM;
  623. goto out;
  624. }
  625. lport = shost_priv(shost);
  626. port = lport_priv(lport);
  627. port->lport = lport;
  628. port->fcoe = fcoe;
  629. INIT_WORK(&port->destroy_work, fcoe_destroy_work);
  630. /* configure fc_lport, e.g., em */
  631. rc = fcoe_lport_config(lport);
  632. if (rc) {
  633. FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
  634. "interface\n");
  635. goto out_host_put;
  636. }
  637. /* configure lport network properties */
  638. rc = fcoe_netdev_config(lport, netdev);
  639. if (rc) {
  640. FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
  641. "interface\n");
  642. goto out_lp_destroy;
  643. }
  644. /* configure lport scsi host properties */
  645. rc = fcoe_shost_config(lport, shost, parent);
  646. if (rc) {
  647. FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
  648. "interface\n");
  649. goto out_lp_destroy;
  650. }
  651. /* Initialize the library */
  652. rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
  653. if (rc) {
  654. FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
  655. "interface\n");
  656. goto out_lp_destroy;
  657. }
  658. /*
  659. * fcoe_em_alloc() and fcoe_hostlist_add() both
  660. * need to be atomic with respect to other changes to the hostlist
  661. * since fcoe_em_alloc() looks for an existing EM
  662. * instance on host list updated by fcoe_hostlist_add().
  663. *
  664. * This is currently handled through the fcoe_config_mutex begin held.
  665. */
  666. /* lport exch manager allocation */
  667. rc = fcoe_em_config(lport);
  668. if (rc) {
  669. FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
  670. "interface\n");
  671. goto out_lp_destroy;
  672. }
  673. fcoe_interface_get(fcoe);
  674. return lport;
  675. out_lp_destroy:
  676. fc_exch_mgr_free(lport);
  677. out_host_put:
  678. scsi_host_put(lport->host);
  679. out:
  680. return ERR_PTR(rc);
  681. }
  682. /**
  683. * fcoe_if_init() - attach to scsi transport
  684. *
  685. * Returns : 0 on success
  686. */
  687. static int __init fcoe_if_init(void)
  688. {
  689. /* attach to scsi transport */
  690. scsi_transport_fcoe_sw =
  691. fc_attach_transport(&fcoe_transport_function);
  692. if (!scsi_transport_fcoe_sw) {
  693. printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
  694. return -ENODEV;
  695. }
  696. return 0;
  697. }
  698. /**
  699. * fcoe_if_exit() - detach from scsi transport
  700. *
  701. * Returns : 0 on success
  702. */
  703. int __exit fcoe_if_exit(void)
  704. {
  705. fc_release_transport(scsi_transport_fcoe_sw);
  706. scsi_transport_fcoe_sw = NULL;
  707. return 0;
  708. }
  709. /**
  710. * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
  711. * @cpu: cpu index for the online cpu
  712. */
  713. static void fcoe_percpu_thread_create(unsigned int cpu)
  714. {
  715. struct fcoe_percpu_s *p;
  716. struct task_struct *thread;
  717. p = &per_cpu(fcoe_percpu, cpu);
  718. thread = kthread_create(fcoe_percpu_receive_thread,
  719. (void *)p, "fcoethread/%d", cpu);
  720. if (likely(!IS_ERR(p->thread))) {
  721. kthread_bind(thread, cpu);
  722. wake_up_process(thread);
  723. spin_lock_bh(&p->fcoe_rx_list.lock);
  724. p->thread = thread;
  725. spin_unlock_bh(&p->fcoe_rx_list.lock);
  726. }
  727. }
  728. /**
  729. * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
  730. * @cpu: cpu index the rx thread is to be removed
  731. *
  732. * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
  733. * current CPU's Rx thread. If the thread being destroyed is bound to
  734. * the CPU processing this context the skbs will be freed.
  735. */
  736. static void fcoe_percpu_thread_destroy(unsigned int cpu)
  737. {
  738. struct fcoe_percpu_s *p;
  739. struct task_struct *thread;
  740. struct page *crc_eof;
  741. struct sk_buff *skb;
  742. #ifdef CONFIG_SMP
  743. struct fcoe_percpu_s *p0;
  744. unsigned targ_cpu = smp_processor_id();
  745. #endif /* CONFIG_SMP */
  746. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  747. /* Prevent any new skbs from being queued for this CPU. */
  748. p = &per_cpu(fcoe_percpu, cpu);
  749. spin_lock_bh(&p->fcoe_rx_list.lock);
  750. thread = p->thread;
  751. p->thread = NULL;
  752. crc_eof = p->crc_eof_page;
  753. p->crc_eof_page = NULL;
  754. p->crc_eof_offset = 0;
  755. spin_unlock_bh(&p->fcoe_rx_list.lock);
  756. #ifdef CONFIG_SMP
  757. /*
  758. * Don't bother moving the skb's if this context is running
  759. * on the same CPU that is having its thread destroyed. This
  760. * can easily happen when the module is removed.
  761. */
  762. if (cpu != targ_cpu) {
  763. p0 = &per_cpu(fcoe_percpu, targ_cpu);
  764. spin_lock_bh(&p0->fcoe_rx_list.lock);
  765. if (p0->thread) {
  766. FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
  767. cpu, targ_cpu);
  768. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  769. __skb_queue_tail(&p0->fcoe_rx_list, skb);
  770. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  771. } else {
  772. /*
  773. * The targeted CPU is not initialized and cannot accept
  774. * new skbs. Unlock the targeted CPU and drop the skbs
  775. * on the CPU that is going offline.
  776. */
  777. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  778. kfree_skb(skb);
  779. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  780. }
  781. } else {
  782. /*
  783. * This scenario occurs when the module is being removed
  784. * and all threads are being destroyed. skbs will continue
  785. * to be shifted from the CPU thread that is being removed
  786. * to the CPU thread associated with the CPU that is processing
  787. * the module removal. Once there is only one CPU Rx thread it
  788. * will reach this case and we will drop all skbs and later
  789. * stop the thread.
  790. */
  791. spin_lock_bh(&p->fcoe_rx_list.lock);
  792. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  793. kfree_skb(skb);
  794. spin_unlock_bh(&p->fcoe_rx_list.lock);
  795. }
  796. #else
  797. /*
  798. * This a non-SMP scenario where the singular Rx thread is
  799. * being removed. Free all skbs and stop the thread.
  800. */
  801. spin_lock_bh(&p->fcoe_rx_list.lock);
  802. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  803. kfree_skb(skb);
  804. spin_unlock_bh(&p->fcoe_rx_list.lock);
  805. #endif
  806. if (thread)
  807. kthread_stop(thread);
  808. if (crc_eof)
  809. put_page(crc_eof);
  810. }
  811. /**
  812. * fcoe_cpu_callback() - fcoe cpu hotplug event callback
  813. * @nfb: callback data block
  814. * @action: event triggering the callback
  815. * @hcpu: index for the cpu of this event
  816. *
  817. * This creates or destroys per cpu data for fcoe
  818. *
  819. * Returns NOTIFY_OK always.
  820. */
  821. static int fcoe_cpu_callback(struct notifier_block *nfb,
  822. unsigned long action, void *hcpu)
  823. {
  824. unsigned cpu = (unsigned long)hcpu;
  825. switch (action) {
  826. case CPU_ONLINE:
  827. case CPU_ONLINE_FROZEN:
  828. FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
  829. fcoe_percpu_thread_create(cpu);
  830. break;
  831. case CPU_DEAD:
  832. case CPU_DEAD_FROZEN:
  833. FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
  834. fcoe_percpu_thread_destroy(cpu);
  835. break;
  836. default:
  837. break;
  838. }
  839. return NOTIFY_OK;
  840. }
  841. static struct notifier_block fcoe_cpu_notifier = {
  842. .notifier_call = fcoe_cpu_callback,
  843. };
  844. /**
  845. * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
  846. * @skb: the receive skb
  847. * @dev: associated net device
  848. * @ptype: context
  849. * @olddev: last device
  850. *
  851. * this function will receive the packet and build fc frame and pass it up
  852. *
  853. * Returns: 0 for success
  854. */
  855. int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
  856. struct packet_type *ptype, struct net_device *olddev)
  857. {
  858. struct fc_lport *lp;
  859. struct fcoe_rcv_info *fr;
  860. struct fcoe_interface *fcoe;
  861. struct fc_frame_header *fh;
  862. struct fcoe_percpu_s *fps;
  863. unsigned int cpu;
  864. fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
  865. lp = fcoe->ctlr.lp;
  866. if (unlikely(lp == NULL)) {
  867. FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
  868. goto err2;
  869. }
  870. if (!lp->link_up)
  871. goto err2;
  872. FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
  873. "data:%p tail:%p end:%p sum:%d dev:%s",
  874. skb->len, skb->data_len, skb->head, skb->data,
  875. skb_tail_pointer(skb), skb_end_pointer(skb),
  876. skb->csum, skb->dev ? skb->dev->name : "<NULL>");
  877. /* check for FCOE packet type */
  878. if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
  879. FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
  880. goto err;
  881. }
  882. /*
  883. * Check for minimum frame length, and make sure required FCoE
  884. * and FC headers are pulled into the linear data area.
  885. */
  886. if (unlikely((skb->len < FCOE_MIN_FRAME) ||
  887. !pskb_may_pull(skb, FCOE_HEADER_LEN)))
  888. goto err;
  889. skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
  890. fh = (struct fc_frame_header *) skb_transport_header(skb);
  891. fr = fcoe_dev_from_skb(skb);
  892. fr->fr_dev = lp;
  893. fr->ptype = ptype;
  894. /*
  895. * In case the incoming frame's exchange is originated from
  896. * the initiator, then received frame's exchange id is ANDed
  897. * with fc_cpu_mask bits to get the same cpu on which exchange
  898. * was originated, otherwise just use the current cpu.
  899. */
  900. if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
  901. cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
  902. else
  903. cpu = smp_processor_id();
  904. fps = &per_cpu(fcoe_percpu, cpu);
  905. spin_lock_bh(&fps->fcoe_rx_list.lock);
  906. if (unlikely(!fps->thread)) {
  907. /*
  908. * The targeted CPU is not ready, let's target
  909. * the first CPU now. For non-SMP systems this
  910. * will check the same CPU twice.
  911. */
  912. FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
  913. "ready for incoming skb- using first online "
  914. "CPU.\n");
  915. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  916. cpu = first_cpu(cpu_online_map);
  917. fps = &per_cpu(fcoe_percpu, cpu);
  918. spin_lock_bh(&fps->fcoe_rx_list.lock);
  919. if (!fps->thread) {
  920. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  921. goto err;
  922. }
  923. }
  924. /*
  925. * We now have a valid CPU that we're targeting for
  926. * this skb. We also have this receive thread locked,
  927. * so we're free to queue skbs into it's queue.
  928. */
  929. __skb_queue_tail(&fps->fcoe_rx_list, skb);
  930. if (fps->fcoe_rx_list.qlen == 1)
  931. wake_up_process(fps->thread);
  932. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  933. return 0;
  934. err:
  935. fc_lport_get_stats(lp)->ErrorFrames++;
  936. err2:
  937. kfree_skb(skb);
  938. return -1;
  939. }
  940. /**
  941. * fcoe_start_io() - pass to netdev to start xmit for fcoe
  942. * @skb: the skb to be xmitted
  943. *
  944. * Returns: 0 for success
  945. */
  946. static inline int fcoe_start_io(struct sk_buff *skb)
  947. {
  948. int rc;
  949. skb_get(skb);
  950. rc = dev_queue_xmit(skb);
  951. if (rc != 0)
  952. return rc;
  953. kfree_skb(skb);
  954. return 0;
  955. }
  956. /**
  957. * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
  958. * @skb: the skb to be xmitted
  959. * @tlen: total len
  960. *
  961. * Returns: 0 for success
  962. */
  963. static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
  964. {
  965. struct fcoe_percpu_s *fps;
  966. struct page *page;
  967. fps = &get_cpu_var(fcoe_percpu);
  968. page = fps->crc_eof_page;
  969. if (!page) {
  970. page = alloc_page(GFP_ATOMIC);
  971. if (!page) {
  972. put_cpu_var(fcoe_percpu);
  973. return -ENOMEM;
  974. }
  975. fps->crc_eof_page = page;
  976. fps->crc_eof_offset = 0;
  977. }
  978. get_page(page);
  979. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
  980. fps->crc_eof_offset, tlen);
  981. skb->len += tlen;
  982. skb->data_len += tlen;
  983. skb->truesize += tlen;
  984. fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
  985. if (fps->crc_eof_offset >= PAGE_SIZE) {
  986. fps->crc_eof_page = NULL;
  987. fps->crc_eof_offset = 0;
  988. put_page(page);
  989. }
  990. put_cpu_var(fcoe_percpu);
  991. return 0;
  992. }
  993. /**
  994. * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
  995. * @fp: the fc_frame containing data to be checksummed
  996. *
  997. * This uses crc32() to calculate the crc for port frame
  998. * Return : 32 bit crc
  999. */
  1000. u32 fcoe_fc_crc(struct fc_frame *fp)
  1001. {
  1002. struct sk_buff *skb = fp_skb(fp);
  1003. struct skb_frag_struct *frag;
  1004. unsigned char *data;
  1005. unsigned long off, len, clen;
  1006. u32 crc;
  1007. unsigned i;
  1008. crc = crc32(~0, skb->data, skb_headlen(skb));
  1009. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1010. frag = &skb_shinfo(skb)->frags[i];
  1011. off = frag->page_offset;
  1012. len = frag->size;
  1013. while (len > 0) {
  1014. clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
  1015. data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
  1016. KM_SKB_DATA_SOFTIRQ);
  1017. crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
  1018. kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
  1019. off += clen;
  1020. len -= clen;
  1021. }
  1022. }
  1023. return crc;
  1024. }
  1025. /**
  1026. * fcoe_xmit() - FCoE frame transmit function
  1027. * @lp: the associated local fcoe
  1028. * @fp: the fc_frame to be transmitted
  1029. *
  1030. * Return : 0 for success
  1031. */
  1032. int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
  1033. {
  1034. int wlen;
  1035. u32 crc;
  1036. struct ethhdr *eh;
  1037. struct fcoe_crc_eof *cp;
  1038. struct sk_buff *skb;
  1039. struct fcoe_dev_stats *stats;
  1040. struct fc_frame_header *fh;
  1041. unsigned int hlen; /* header length implies the version */
  1042. unsigned int tlen; /* trailer length */
  1043. unsigned int elen; /* eth header, may include vlan */
  1044. struct fcoe_port *port = lport_priv(lp);
  1045. struct fcoe_interface *fcoe = port->fcoe;
  1046. u8 sof, eof;
  1047. struct fcoe_hdr *hp;
  1048. WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
  1049. fh = fc_frame_header_get(fp);
  1050. skb = fp_skb(fp);
  1051. wlen = skb->len / FCOE_WORD_TO_BYTE;
  1052. if (!lp->link_up) {
  1053. kfree_skb(skb);
  1054. return 0;
  1055. }
  1056. if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
  1057. fcoe_ctlr_els_send(&fcoe->ctlr, skb))
  1058. return 0;
  1059. sof = fr_sof(fp);
  1060. eof = fr_eof(fp);
  1061. elen = sizeof(struct ethhdr);
  1062. hlen = sizeof(struct fcoe_hdr);
  1063. tlen = sizeof(struct fcoe_crc_eof);
  1064. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  1065. /* crc offload */
  1066. if (likely(lp->crc_offload)) {
  1067. skb->ip_summed = CHECKSUM_PARTIAL;
  1068. skb->csum_start = skb_headroom(skb);
  1069. skb->csum_offset = skb->len;
  1070. crc = 0;
  1071. } else {
  1072. skb->ip_summed = CHECKSUM_NONE;
  1073. crc = fcoe_fc_crc(fp);
  1074. }
  1075. /* copy port crc and eof to the skb buff */
  1076. if (skb_is_nonlinear(skb)) {
  1077. skb_frag_t *frag;
  1078. if (fcoe_get_paged_crc_eof(skb, tlen)) {
  1079. kfree_skb(skb);
  1080. return -ENOMEM;
  1081. }
  1082. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  1083. cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
  1084. + frag->page_offset;
  1085. } else {
  1086. cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
  1087. }
  1088. memset(cp, 0, sizeof(*cp));
  1089. cp->fcoe_eof = eof;
  1090. cp->fcoe_crc32 = cpu_to_le32(~crc);
  1091. if (skb_is_nonlinear(skb)) {
  1092. kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
  1093. cp = NULL;
  1094. }
  1095. /* adjust skb network/transport offsets to match mac/fcoe/port */
  1096. skb_push(skb, elen + hlen);
  1097. skb_reset_mac_header(skb);
  1098. skb_reset_network_header(skb);
  1099. skb->mac_len = elen;
  1100. skb->protocol = htons(ETH_P_FCOE);
  1101. skb->dev = fcoe->netdev;
  1102. /* fill up mac and fcoe headers */
  1103. eh = eth_hdr(skb);
  1104. eh->h_proto = htons(ETH_P_FCOE);
  1105. if (fcoe->ctlr.map_dest)
  1106. fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
  1107. else
  1108. /* insert GW address */
  1109. memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
  1110. if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
  1111. memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
  1112. else
  1113. memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
  1114. hp = (struct fcoe_hdr *)(eh + 1);
  1115. memset(hp, 0, sizeof(*hp));
  1116. if (FC_FCOE_VER)
  1117. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  1118. hp->fcoe_sof = sof;
  1119. /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
  1120. if (lp->seq_offload && fr_max_payload(fp)) {
  1121. skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
  1122. skb_shinfo(skb)->gso_size = fr_max_payload(fp);
  1123. } else {
  1124. skb_shinfo(skb)->gso_type = 0;
  1125. skb_shinfo(skb)->gso_size = 0;
  1126. }
  1127. /* update tx stats: regardless if LLD fails */
  1128. stats = fc_lport_get_stats(lp);
  1129. stats->TxFrames++;
  1130. stats->TxWords += wlen;
  1131. /* send down to lld */
  1132. fr_dev(fp) = lp;
  1133. if (port->fcoe_pending_queue.qlen)
  1134. fcoe_check_wait_queue(lp, skb);
  1135. else if (fcoe_start_io(skb))
  1136. fcoe_check_wait_queue(lp, skb);
  1137. return 0;
  1138. }
  1139. /**
  1140. * fcoe_percpu_receive_thread() - recv thread per cpu
  1141. * @arg: ptr to the fcoe per cpu struct
  1142. *
  1143. * Return: 0 for success
  1144. */
  1145. int fcoe_percpu_receive_thread(void *arg)
  1146. {
  1147. struct fcoe_percpu_s *p = arg;
  1148. u32 fr_len;
  1149. struct fc_lport *lp;
  1150. struct fcoe_rcv_info *fr;
  1151. struct fcoe_dev_stats *stats;
  1152. struct fc_frame_header *fh;
  1153. struct sk_buff *skb;
  1154. struct fcoe_crc_eof crc_eof;
  1155. struct fc_frame *fp;
  1156. u8 *mac = NULL;
  1157. struct fcoe_port *port;
  1158. struct fcoe_hdr *hp;
  1159. set_user_nice(current, -20);
  1160. while (!kthread_should_stop()) {
  1161. spin_lock_bh(&p->fcoe_rx_list.lock);
  1162. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
  1163. set_current_state(TASK_INTERRUPTIBLE);
  1164. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1165. schedule();
  1166. set_current_state(TASK_RUNNING);
  1167. if (kthread_should_stop())
  1168. return 0;
  1169. spin_lock_bh(&p->fcoe_rx_list.lock);
  1170. }
  1171. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1172. fr = fcoe_dev_from_skb(skb);
  1173. lp = fr->fr_dev;
  1174. if (unlikely(lp == NULL)) {
  1175. FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
  1176. kfree_skb(skb);
  1177. continue;
  1178. }
  1179. FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
  1180. "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
  1181. skb->len, skb->data_len,
  1182. skb->head, skb->data, skb_tail_pointer(skb),
  1183. skb_end_pointer(skb), skb->csum,
  1184. skb->dev ? skb->dev->name : "<NULL>");
  1185. /*
  1186. * Save source MAC address before discarding header.
  1187. */
  1188. port = lport_priv(lp);
  1189. if (skb_is_nonlinear(skb))
  1190. skb_linearize(skb); /* not ideal */
  1191. mac = eth_hdr(skb)->h_source;
  1192. /*
  1193. * Frame length checks and setting up the header pointers
  1194. * was done in fcoe_rcv already.
  1195. */
  1196. hp = (struct fcoe_hdr *) skb_network_header(skb);
  1197. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1198. stats = fc_lport_get_stats(lp);
  1199. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  1200. if (stats->ErrorFrames < 5)
  1201. printk(KERN_WARNING "fcoe: FCoE version "
  1202. "mismatch: The frame has "
  1203. "version %x, but the "
  1204. "initiator supports version "
  1205. "%x\n", FC_FCOE_DECAPS_VER(hp),
  1206. FC_FCOE_VER);
  1207. stats->ErrorFrames++;
  1208. kfree_skb(skb);
  1209. continue;
  1210. }
  1211. skb_pull(skb, sizeof(struct fcoe_hdr));
  1212. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  1213. stats->RxFrames++;
  1214. stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
  1215. fp = (struct fc_frame *)skb;
  1216. fc_frame_init(fp);
  1217. fr_dev(fp) = lp;
  1218. fr_sof(fp) = hp->fcoe_sof;
  1219. /* Copy out the CRC and EOF trailer for access */
  1220. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
  1221. kfree_skb(skb);
  1222. continue;
  1223. }
  1224. fr_eof(fp) = crc_eof.fcoe_eof;
  1225. fr_crc(fp) = crc_eof.fcoe_crc32;
  1226. if (pskb_trim(skb, fr_len)) {
  1227. kfree_skb(skb);
  1228. continue;
  1229. }
  1230. /*
  1231. * We only check CRC if no offload is available and if it is
  1232. * it's solicited data, in which case, the FCP layer would
  1233. * check it during the copy.
  1234. */
  1235. if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
  1236. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1237. else
  1238. fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
  1239. fh = fc_frame_header_get(fp);
  1240. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
  1241. fh->fh_type == FC_TYPE_FCP) {
  1242. fc_exch_recv(lp, fp);
  1243. continue;
  1244. }
  1245. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
  1246. if (le32_to_cpu(fr_crc(fp)) !=
  1247. ~crc32(~0, skb->data, fr_len)) {
  1248. if (stats->InvalidCRCCount < 5)
  1249. printk(KERN_WARNING "fcoe: dropping "
  1250. "frame with CRC error\n");
  1251. stats->InvalidCRCCount++;
  1252. stats->ErrorFrames++;
  1253. fc_frame_free(fp);
  1254. continue;
  1255. }
  1256. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1257. }
  1258. if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
  1259. fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
  1260. fc_frame_free(fp);
  1261. continue;
  1262. }
  1263. fc_exch_recv(lp, fp);
  1264. }
  1265. return 0;
  1266. }
  1267. /**
  1268. * fcoe_check_wait_queue() - attempt to clear the transmit backlog
  1269. * @lp: the fc_lport
  1270. *
  1271. * This empties the wait_queue, dequeue the head of the wait_queue queue
  1272. * and calls fcoe_start_io() for each packet, if all skb have been
  1273. * transmitted, return qlen or -1 if a error occurs, then restore
  1274. * wait_queue and try again later.
  1275. *
  1276. * The wait_queue is used when the skb transmit fails. skb will go
  1277. * in the wait_queue which will be emptied by the timer function or
  1278. * by the next skb transmit.
  1279. */
  1280. static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
  1281. {
  1282. struct fcoe_port *port = lport_priv(lp);
  1283. int rc;
  1284. spin_lock_bh(&port->fcoe_pending_queue.lock);
  1285. if (skb)
  1286. __skb_queue_tail(&port->fcoe_pending_queue, skb);
  1287. if (port->fcoe_pending_queue_active)
  1288. goto out;
  1289. port->fcoe_pending_queue_active = 1;
  1290. while (port->fcoe_pending_queue.qlen) {
  1291. /* keep qlen > 0 until fcoe_start_io succeeds */
  1292. port->fcoe_pending_queue.qlen++;
  1293. skb = __skb_dequeue(&port->fcoe_pending_queue);
  1294. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  1295. rc = fcoe_start_io(skb);
  1296. spin_lock_bh(&port->fcoe_pending_queue.lock);
  1297. if (rc) {
  1298. __skb_queue_head(&port->fcoe_pending_queue, skb);
  1299. /* undo temporary increment above */
  1300. port->fcoe_pending_queue.qlen--;
  1301. break;
  1302. }
  1303. /* undo temporary increment above */
  1304. port->fcoe_pending_queue.qlen--;
  1305. }
  1306. if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
  1307. lp->qfull = 0;
  1308. if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
  1309. mod_timer(&port->timer, jiffies + 2);
  1310. port->fcoe_pending_queue_active = 0;
  1311. out:
  1312. if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
  1313. lp->qfull = 1;
  1314. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  1315. return;
  1316. }
  1317. /**
  1318. * fcoe_dev_setup() - setup link change notification interface
  1319. */
  1320. static void fcoe_dev_setup(void)
  1321. {
  1322. register_netdevice_notifier(&fcoe_notifier);
  1323. }
  1324. /**
  1325. * fcoe_dev_cleanup() - cleanup link change notification interface
  1326. */
  1327. static void fcoe_dev_cleanup(void)
  1328. {
  1329. unregister_netdevice_notifier(&fcoe_notifier);
  1330. }
  1331. /**
  1332. * fcoe_device_notification() - netdev event notification callback
  1333. * @notifier: context of the notification
  1334. * @event: type of event
  1335. * @ptr: fixed array for output parsed ifname
  1336. *
  1337. * This function is called by the ethernet driver in case of link change event
  1338. *
  1339. * Returns: 0 for success
  1340. */
  1341. static int fcoe_device_notification(struct notifier_block *notifier,
  1342. ulong event, void *ptr)
  1343. {
  1344. struct fc_lport *lp = NULL;
  1345. struct net_device *netdev = ptr;
  1346. struct fcoe_interface *fcoe;
  1347. struct fcoe_port *port;
  1348. struct fcoe_dev_stats *stats;
  1349. u32 link_possible = 1;
  1350. u32 mfs;
  1351. int rc = NOTIFY_OK;
  1352. write_lock(&fcoe_hostlist_lock);
  1353. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1354. if (fcoe->netdev == netdev) {
  1355. lp = fcoe->ctlr.lp;
  1356. break;
  1357. }
  1358. }
  1359. if (lp == NULL) {
  1360. rc = NOTIFY_DONE;
  1361. goto out;
  1362. }
  1363. switch (event) {
  1364. case NETDEV_DOWN:
  1365. case NETDEV_GOING_DOWN:
  1366. link_possible = 0;
  1367. break;
  1368. case NETDEV_UP:
  1369. case NETDEV_CHANGE:
  1370. break;
  1371. case NETDEV_CHANGEMTU:
  1372. mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
  1373. sizeof(struct fcoe_crc_eof));
  1374. if (mfs >= FC_MIN_MAX_FRAME)
  1375. fc_set_mfs(lp, mfs);
  1376. break;
  1377. case NETDEV_REGISTER:
  1378. break;
  1379. case NETDEV_UNREGISTER:
  1380. list_del(&fcoe->list);
  1381. port = lport_priv(fcoe->ctlr.lp);
  1382. fcoe_interface_cleanup(fcoe);
  1383. schedule_work(&port->destroy_work);
  1384. goto out;
  1385. break;
  1386. default:
  1387. FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
  1388. "from netdev netlink\n", event);
  1389. }
  1390. if (link_possible && !fcoe_link_ok(lp))
  1391. fcoe_ctlr_link_up(&fcoe->ctlr);
  1392. else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
  1393. stats = fc_lport_get_stats(lp);
  1394. stats->LinkFailureCount++;
  1395. fcoe_clean_pending_queue(lp);
  1396. }
  1397. out:
  1398. write_unlock(&fcoe_hostlist_lock);
  1399. return rc;
  1400. }
  1401. /**
  1402. * fcoe_if_to_netdev() - parse a name buffer to get netdev
  1403. * @buffer: incoming buffer to be copied
  1404. *
  1405. * Returns: NULL or ptr to net_device
  1406. */
  1407. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  1408. {
  1409. char *cp;
  1410. char ifname[IFNAMSIZ + 2];
  1411. if (buffer) {
  1412. strlcpy(ifname, buffer, IFNAMSIZ);
  1413. cp = ifname + strlen(ifname);
  1414. while (--cp >= ifname && *cp == '\n')
  1415. *cp = '\0';
  1416. return dev_get_by_name(&init_net, ifname);
  1417. }
  1418. return NULL;
  1419. }
  1420. /**
  1421. * fcoe_destroy() - handles the destroy from sysfs
  1422. * @buffer: expected to be an eth if name
  1423. * @kp: associated kernel param
  1424. *
  1425. * Returns: 0 for success
  1426. */
  1427. static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
  1428. {
  1429. struct fcoe_interface *fcoe;
  1430. struct net_device *netdev;
  1431. int rc;
  1432. mutex_lock(&fcoe_config_mutex);
  1433. #ifdef CONFIG_FCOE_MODULE
  1434. /*
  1435. * Make sure the module has been initialized, and is not about to be
  1436. * removed. Module paramter sysfs files are writable before the
  1437. * module_init function is called and after module_exit.
  1438. */
  1439. if (THIS_MODULE->state != MODULE_STATE_LIVE) {
  1440. rc = -ENODEV;
  1441. goto out_nodev;
  1442. }
  1443. #endif
  1444. netdev = fcoe_if_to_netdev(buffer);
  1445. if (!netdev) {
  1446. rc = -ENODEV;
  1447. goto out_nodev;
  1448. }
  1449. write_lock(&fcoe_hostlist_lock);
  1450. fcoe = fcoe_hostlist_lookup_port(netdev);
  1451. if (!fcoe) {
  1452. write_unlock(&fcoe_hostlist_lock);
  1453. rc = -ENODEV;
  1454. goto out_putdev;
  1455. }
  1456. list_del(&fcoe->list);
  1457. write_unlock(&fcoe_hostlist_lock);
  1458. rtnl_lock();
  1459. fcoe_interface_cleanup(fcoe);
  1460. rtnl_unlock();
  1461. fcoe_if_destroy(fcoe->ctlr.lp);
  1462. out_putdev:
  1463. dev_put(netdev);
  1464. out_nodev:
  1465. mutex_unlock(&fcoe_config_mutex);
  1466. return rc;
  1467. }
  1468. static void fcoe_destroy_work(struct work_struct *work)
  1469. {
  1470. struct fcoe_port *port;
  1471. port = container_of(work, struct fcoe_port, destroy_work);
  1472. mutex_lock(&fcoe_config_mutex);
  1473. fcoe_if_destroy(port->lport);
  1474. mutex_unlock(&fcoe_config_mutex);
  1475. }
  1476. /**
  1477. * fcoe_create() - Handles the create call from sysfs
  1478. * @buffer: expected to be an eth if name
  1479. * @kp: associated kernel param
  1480. *
  1481. * Returns: 0 for success
  1482. */
  1483. static int fcoe_create(const char *buffer, struct kernel_param *kp)
  1484. {
  1485. int rc;
  1486. struct fcoe_interface *fcoe;
  1487. struct fc_lport *lport;
  1488. struct net_device *netdev;
  1489. mutex_lock(&fcoe_config_mutex);
  1490. #ifdef CONFIG_FCOE_MODULE
  1491. /*
  1492. * Make sure the module has been initialized, and is not about to be
  1493. * removed. Module paramter sysfs files are writable before the
  1494. * module_init function is called and after module_exit.
  1495. */
  1496. if (THIS_MODULE->state != MODULE_STATE_LIVE) {
  1497. rc = -ENODEV;
  1498. goto out_nodev;
  1499. }
  1500. #endif
  1501. rtnl_lock();
  1502. netdev = fcoe_if_to_netdev(buffer);
  1503. if (!netdev) {
  1504. rc = -ENODEV;
  1505. goto out_nodev;
  1506. }
  1507. /* look for existing lport */
  1508. if (fcoe_hostlist_lookup(netdev)) {
  1509. rc = -EEXIST;
  1510. goto out_putdev;
  1511. }
  1512. fcoe = fcoe_interface_create(netdev);
  1513. if (!fcoe) {
  1514. rc = -ENOMEM;
  1515. goto out_putdev;
  1516. }
  1517. lport = fcoe_if_create(fcoe, &netdev->dev);
  1518. if (IS_ERR(lport)) {
  1519. printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
  1520. netdev->name);
  1521. rc = -EIO;
  1522. fcoe_interface_cleanup(fcoe);
  1523. goto out_free;
  1524. }
  1525. /* Make this the "master" N_Port */
  1526. fcoe->ctlr.lp = lport;
  1527. /* add to lports list */
  1528. fcoe_hostlist_add(lport);
  1529. /* start FIP Discovery and FLOGI */
  1530. lport->boot_time = jiffies;
  1531. fc_fabric_login(lport);
  1532. if (!fcoe_link_ok(lport))
  1533. fcoe_ctlr_link_up(&fcoe->ctlr);
  1534. rc = 0;
  1535. out_free:
  1536. /*
  1537. * Release from init in fcoe_interface_create(), on success lport
  1538. * should be holding a reference taken in fcoe_if_create().
  1539. */
  1540. fcoe_interface_put(fcoe);
  1541. out_putdev:
  1542. dev_put(netdev);
  1543. out_nodev:
  1544. rtnl_unlock();
  1545. mutex_unlock(&fcoe_config_mutex);
  1546. return rc;
  1547. }
  1548. module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
  1549. __MODULE_PARM_TYPE(create, "string");
  1550. MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
  1551. module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
  1552. __MODULE_PARM_TYPE(destroy, "string");
  1553. MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
  1554. /**
  1555. * fcoe_link_ok() - Check if link is ok for the fc_lport
  1556. * @lp: ptr to the fc_lport
  1557. *
  1558. * Any permanently-disqualifying conditions have been previously checked.
  1559. * This also updates the speed setting, which may change with link for 100/1000.
  1560. *
  1561. * This function should probably be checking for PAUSE support at some point
  1562. * in the future. Currently Per-priority-pause is not determinable using
  1563. * ethtool, so we shouldn't be restrictive until that problem is resolved.
  1564. *
  1565. * Returns: 0 if link is OK for use by FCoE.
  1566. *
  1567. */
  1568. int fcoe_link_ok(struct fc_lport *lp)
  1569. {
  1570. struct fcoe_port *port = lport_priv(lp);
  1571. struct net_device *dev = port->fcoe->netdev;
  1572. struct ethtool_cmd ecmd = { ETHTOOL_GSET };
  1573. if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
  1574. (!dev_ethtool_get_settings(dev, &ecmd))) {
  1575. lp->link_supported_speeds &=
  1576. ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
  1577. if (ecmd.supported & (SUPPORTED_1000baseT_Half |
  1578. SUPPORTED_1000baseT_Full))
  1579. lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
  1580. if (ecmd.supported & SUPPORTED_10000baseT_Full)
  1581. lp->link_supported_speeds |=
  1582. FC_PORTSPEED_10GBIT;
  1583. if (ecmd.speed == SPEED_1000)
  1584. lp->link_speed = FC_PORTSPEED_1GBIT;
  1585. if (ecmd.speed == SPEED_10000)
  1586. lp->link_speed = FC_PORTSPEED_10GBIT;
  1587. return 0;
  1588. }
  1589. return -1;
  1590. }
  1591. /**
  1592. * fcoe_percpu_clean() - Clear the pending skbs for an lport
  1593. * @lp: the fc_lport
  1594. */
  1595. void fcoe_percpu_clean(struct fc_lport *lp)
  1596. {
  1597. struct fcoe_percpu_s *pp;
  1598. struct fcoe_rcv_info *fr;
  1599. struct sk_buff_head *list;
  1600. struct sk_buff *skb, *next;
  1601. struct sk_buff *head;
  1602. unsigned int cpu;
  1603. for_each_possible_cpu(cpu) {
  1604. pp = &per_cpu(fcoe_percpu, cpu);
  1605. spin_lock_bh(&pp->fcoe_rx_list.lock);
  1606. list = &pp->fcoe_rx_list;
  1607. head = list->next;
  1608. for (skb = head; skb != (struct sk_buff *)list;
  1609. skb = next) {
  1610. next = skb->next;
  1611. fr = fcoe_dev_from_skb(skb);
  1612. if (fr->fr_dev == lp) {
  1613. __skb_unlink(skb, list);
  1614. kfree_skb(skb);
  1615. }
  1616. }
  1617. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1618. }
  1619. }
  1620. /**
  1621. * fcoe_clean_pending_queue() - Dequeue a skb and free it
  1622. * @lp: the corresponding fc_lport
  1623. *
  1624. * Returns: none
  1625. */
  1626. void fcoe_clean_pending_queue(struct fc_lport *lp)
  1627. {
  1628. struct fcoe_port *port = lport_priv(lp);
  1629. struct sk_buff *skb;
  1630. spin_lock_bh(&port->fcoe_pending_queue.lock);
  1631. while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
  1632. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  1633. kfree_skb(skb);
  1634. spin_lock_bh(&port->fcoe_pending_queue.lock);
  1635. }
  1636. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  1637. }
  1638. /**
  1639. * fcoe_reset() - Resets the fcoe
  1640. * @shost: shost the reset is from
  1641. *
  1642. * Returns: always 0
  1643. */
  1644. int fcoe_reset(struct Scsi_Host *shost)
  1645. {
  1646. struct fc_lport *lport = shost_priv(shost);
  1647. fc_lport_reset(lport);
  1648. return 0;
  1649. }
  1650. /**
  1651. * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
  1652. * @dev: this is currently ptr to net_device
  1653. *
  1654. * Called with fcoe_hostlist_lock held.
  1655. *
  1656. * Returns: NULL or the located fcoe_port
  1657. */
  1658. static struct fcoe_interface *
  1659. fcoe_hostlist_lookup_port(const struct net_device *dev)
  1660. {
  1661. struct fcoe_interface *fcoe;
  1662. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1663. if (fcoe->netdev == dev)
  1664. return fcoe;
  1665. }
  1666. return NULL;
  1667. }
  1668. /**
  1669. * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
  1670. * @netdev: ptr to net_device
  1671. *
  1672. * Returns: 0 for success
  1673. */
  1674. struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
  1675. {
  1676. struct fcoe_interface *fcoe;
  1677. read_lock(&fcoe_hostlist_lock);
  1678. fcoe = fcoe_hostlist_lookup_port(netdev);
  1679. read_unlock(&fcoe_hostlist_lock);
  1680. return (fcoe) ? fcoe->ctlr.lp : NULL;
  1681. }
  1682. /**
  1683. * fcoe_hostlist_add() - Add a lport to lports list
  1684. * @lp: ptr to the fc_lport to be added
  1685. *
  1686. * Returns: 0 for success
  1687. */
  1688. int fcoe_hostlist_add(const struct fc_lport *lport)
  1689. {
  1690. struct fcoe_interface *fcoe;
  1691. struct fcoe_port *port;
  1692. write_lock_bh(&fcoe_hostlist_lock);
  1693. fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
  1694. if (!fcoe) {
  1695. port = lport_priv(lport);
  1696. fcoe = port->fcoe;
  1697. list_add_tail(&fcoe->list, &fcoe_hostlist);
  1698. }
  1699. write_unlock_bh(&fcoe_hostlist_lock);
  1700. return 0;
  1701. }
  1702. /**
  1703. * fcoe_init() - fcoe module loading initialization
  1704. *
  1705. * Returns 0 on success, negative on failure
  1706. */
  1707. static int __init fcoe_init(void)
  1708. {
  1709. unsigned int cpu;
  1710. int rc = 0;
  1711. struct fcoe_percpu_s *p;
  1712. mutex_lock(&fcoe_config_mutex);
  1713. for_each_possible_cpu(cpu) {
  1714. p = &per_cpu(fcoe_percpu, cpu);
  1715. skb_queue_head_init(&p->fcoe_rx_list);
  1716. }
  1717. for_each_online_cpu(cpu)
  1718. fcoe_percpu_thread_create(cpu);
  1719. /* Initialize per CPU interrupt thread */
  1720. rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
  1721. if (rc)
  1722. goto out_free;
  1723. /* Setup link change notification */
  1724. fcoe_dev_setup();
  1725. rc = fcoe_if_init();
  1726. if (rc)
  1727. goto out_free;
  1728. mutex_unlock(&fcoe_config_mutex);
  1729. return 0;
  1730. out_free:
  1731. for_each_online_cpu(cpu) {
  1732. fcoe_percpu_thread_destroy(cpu);
  1733. }
  1734. mutex_unlock(&fcoe_config_mutex);
  1735. return rc;
  1736. }
  1737. module_init(fcoe_init);
  1738. /**
  1739. * fcoe_exit() - fcoe module unloading cleanup
  1740. *
  1741. * Returns 0 on success, negative on failure
  1742. */
  1743. static void __exit fcoe_exit(void)
  1744. {
  1745. unsigned int cpu;
  1746. struct fcoe_interface *fcoe, *tmp;
  1747. LIST_HEAD(local_list);
  1748. struct fcoe_port *port;
  1749. mutex_lock(&fcoe_config_mutex);
  1750. fcoe_dev_cleanup();
  1751. /* releases the associated fcoe hosts */
  1752. write_lock_bh(&fcoe_hostlist_lock);
  1753. list_splice_init(&fcoe_hostlist, &local_list);
  1754. write_unlock_bh(&fcoe_hostlist_lock);
  1755. list_for_each_entry_safe(fcoe, tmp, &local_list, list) {
  1756. list_del(&fcoe->list);
  1757. port = lport_priv(fcoe->ctlr.lp);
  1758. rtnl_lock();
  1759. fcoe_interface_cleanup(fcoe);
  1760. rtnl_unlock();
  1761. schedule_work(&port->destroy_work);
  1762. }
  1763. unregister_hotcpu_notifier(&fcoe_cpu_notifier);
  1764. for_each_online_cpu(cpu)
  1765. fcoe_percpu_thread_destroy(cpu);
  1766. mutex_unlock(&fcoe_config_mutex);
  1767. /* flush any asyncronous interface destroys,
  1768. * this should happen after the netdev notifier is unregistered */
  1769. flush_scheduled_work();
  1770. /* detach from scsi transport
  1771. * must happen after all destroys are done, therefor after the flush */
  1772. fcoe_if_exit();
  1773. }
  1774. module_exit(fcoe_exit);