fcoe.c 46 KB

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