bnx2fc_fcoe.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465
  1. /* bnx2fc_fcoe.c: Broadcom NetXtreme II Linux FCoE offload driver.
  2. * This file contains the code that interacts with libfc, libfcoe,
  3. * cnic modules to create FCoE instances, send/receive non-offloaded
  4. * FIP/FCoE packets, listen to link events etc.
  5. *
  6. * Copyright (c) 2008 - 2010 Broadcom Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. *
  12. * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
  13. */
  14. #include "bnx2fc.h"
  15. static struct list_head adapter_list;
  16. static u32 adapter_count;
  17. static DEFINE_MUTEX(bnx2fc_dev_lock);
  18. DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
  19. #define DRV_MODULE_NAME "bnx2fc"
  20. #define DRV_MODULE_VERSION BNX2FC_VERSION
  21. #define DRV_MODULE_RELDATE "Jun 10, 2011"
  22. static char version[] __devinitdata =
  23. "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
  24. " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  25. MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
  26. MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
  27. MODULE_LICENSE("GPL");
  28. MODULE_VERSION(DRV_MODULE_VERSION);
  29. #define BNX2FC_MAX_QUEUE_DEPTH 256
  30. #define BNX2FC_MIN_QUEUE_DEPTH 32
  31. #define FCOE_WORD_TO_BYTE 4
  32. static struct scsi_transport_template *bnx2fc_transport_template;
  33. static struct scsi_transport_template *bnx2fc_vport_xport_template;
  34. struct workqueue_struct *bnx2fc_wq;
  35. /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
  36. * Here the io threads are per cpu but the l2 thread is just one
  37. */
  38. struct fcoe_percpu_s bnx2fc_global;
  39. DEFINE_SPINLOCK(bnx2fc_global_lock);
  40. static struct cnic_ulp_ops bnx2fc_cnic_cb;
  41. static struct libfc_function_template bnx2fc_libfc_fcn_templ;
  42. static struct scsi_host_template bnx2fc_shost_template;
  43. static struct fc_function_template bnx2fc_transport_function;
  44. static struct fc_function_template bnx2fc_vport_xport_function;
  45. static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
  46. static int bnx2fc_destroy(struct net_device *net_device);
  47. static int bnx2fc_enable(struct net_device *netdev);
  48. static int bnx2fc_disable(struct net_device *netdev);
  49. static void bnx2fc_recv_frame(struct sk_buff *skb);
  50. static void bnx2fc_start_disc(struct bnx2fc_hba *hba);
  51. static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
  52. static int bnx2fc_net_config(struct fc_lport *lp);
  53. static int bnx2fc_lport_config(struct fc_lport *lport);
  54. static int bnx2fc_em_config(struct fc_lport *lport);
  55. static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
  56. static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
  57. static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
  58. static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
  59. static struct fc_lport *bnx2fc_if_create(struct bnx2fc_hba *hba,
  60. struct device *parent, int npiv);
  61. static void bnx2fc_destroy_work(struct work_struct *work);
  62. static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
  63. static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
  64. static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
  65. static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
  66. static void bnx2fc_port_shutdown(struct fc_lport *lport);
  67. static void bnx2fc_stop(struct bnx2fc_hba *hba);
  68. static int __init bnx2fc_mod_init(void);
  69. static void __exit bnx2fc_mod_exit(void);
  70. unsigned int bnx2fc_debug_level;
  71. module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
  72. static int bnx2fc_cpu_callback(struct notifier_block *nfb,
  73. unsigned long action, void *hcpu);
  74. /* notification function for CPU hotplug events */
  75. static struct notifier_block bnx2fc_cpu_notifier = {
  76. .notifier_call = bnx2fc_cpu_callback,
  77. };
  78. static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
  79. {
  80. struct fcoe_percpu_s *bg;
  81. struct fcoe_rcv_info *fr;
  82. struct sk_buff_head *list;
  83. struct sk_buff *skb, *next;
  84. struct sk_buff *head;
  85. bg = &bnx2fc_global;
  86. spin_lock_bh(&bg->fcoe_rx_list.lock);
  87. list = &bg->fcoe_rx_list;
  88. head = list->next;
  89. for (skb = head; skb != (struct sk_buff *)list;
  90. skb = next) {
  91. next = skb->next;
  92. fr = fcoe_dev_from_skb(skb);
  93. if (fr->fr_dev == lp) {
  94. __skb_unlink(skb, list);
  95. kfree_skb(skb);
  96. }
  97. }
  98. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  99. }
  100. int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
  101. {
  102. int rc;
  103. spin_lock(&bnx2fc_global_lock);
  104. rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
  105. spin_unlock(&bnx2fc_global_lock);
  106. return rc;
  107. }
  108. static void bnx2fc_abort_io(struct fc_lport *lport)
  109. {
  110. /*
  111. * This function is no-op for bnx2fc, but we do
  112. * not want to leave it as NULL either, as libfc
  113. * can call the default function which is
  114. * fc_fcp_abort_io.
  115. */
  116. }
  117. static void bnx2fc_cleanup(struct fc_lport *lport)
  118. {
  119. struct fcoe_port *port = lport_priv(lport);
  120. struct bnx2fc_hba *hba = port->priv;
  121. struct bnx2fc_rport *tgt;
  122. int i;
  123. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  124. mutex_lock(&hba->hba_mutex);
  125. spin_lock_bh(&hba->hba_lock);
  126. for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
  127. tgt = hba->tgt_ofld_list[i];
  128. if (tgt) {
  129. /* Cleanup IOs belonging to requested vport */
  130. if (tgt->port == port) {
  131. spin_unlock_bh(&hba->hba_lock);
  132. BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
  133. bnx2fc_flush_active_ios(tgt);
  134. spin_lock_bh(&hba->hba_lock);
  135. }
  136. }
  137. }
  138. spin_unlock_bh(&hba->hba_lock);
  139. mutex_unlock(&hba->hba_mutex);
  140. }
  141. static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
  142. struct fc_frame *fp)
  143. {
  144. struct fc_rport_priv *rdata = tgt->rdata;
  145. struct fc_frame_header *fh;
  146. int rc = 0;
  147. fh = fc_frame_header_get(fp);
  148. BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
  149. "r_ctl = 0x%x\n", rdata->ids.port_id,
  150. ntohs(fh->fh_ox_id), fh->fh_r_ctl);
  151. if ((fh->fh_type == FC_TYPE_ELS) &&
  152. (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
  153. switch (fc_frame_payload_op(fp)) {
  154. case ELS_ADISC:
  155. rc = bnx2fc_send_adisc(tgt, fp);
  156. break;
  157. case ELS_LOGO:
  158. rc = bnx2fc_send_logo(tgt, fp);
  159. break;
  160. case ELS_RLS:
  161. rc = bnx2fc_send_rls(tgt, fp);
  162. break;
  163. default:
  164. break;
  165. }
  166. } else if ((fh->fh_type == FC_TYPE_BLS) &&
  167. (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
  168. BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
  169. else {
  170. BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
  171. "rctl 0x%x thru non-offload path\n",
  172. fh->fh_type, fh->fh_r_ctl);
  173. return -ENODEV;
  174. }
  175. if (rc)
  176. return -ENOMEM;
  177. else
  178. return 0;
  179. }
  180. /**
  181. * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
  182. *
  183. * @lport: the associated local port
  184. * @fp: the fc_frame to be transmitted
  185. */
  186. static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
  187. {
  188. struct ethhdr *eh;
  189. struct fcoe_crc_eof *cp;
  190. struct sk_buff *skb;
  191. struct fc_frame_header *fh;
  192. struct bnx2fc_hba *hba;
  193. struct fcoe_port *port;
  194. struct fcoe_hdr *hp;
  195. struct bnx2fc_rport *tgt;
  196. struct fcoe_dev_stats *stats;
  197. u8 sof, eof;
  198. u32 crc;
  199. unsigned int hlen, tlen, elen;
  200. int wlen, rc = 0;
  201. port = (struct fcoe_port *)lport_priv(lport);
  202. hba = port->priv;
  203. fh = fc_frame_header_get(fp);
  204. skb = fp_skb(fp);
  205. if (!lport->link_up) {
  206. BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
  207. kfree_skb(skb);
  208. return 0;
  209. }
  210. if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
  211. if (!hba->ctlr.sel_fcf) {
  212. BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
  213. kfree_skb(skb);
  214. return -EINVAL;
  215. }
  216. if (fcoe_ctlr_els_send(&hba->ctlr, lport, skb))
  217. return 0;
  218. }
  219. sof = fr_sof(fp);
  220. eof = fr_eof(fp);
  221. /*
  222. * Snoop the frame header to check if the frame is for
  223. * an offloaded session
  224. */
  225. /*
  226. * tgt_ofld_list access is synchronized using
  227. * both hba mutex and hba lock. Atleast hba mutex or
  228. * hba lock needs to be held for read access.
  229. */
  230. spin_lock_bh(&hba->hba_lock);
  231. tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
  232. if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
  233. /* This frame is for offloaded session */
  234. BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
  235. "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
  236. spin_unlock_bh(&hba->hba_lock);
  237. rc = bnx2fc_xmit_l2_frame(tgt, fp);
  238. if (rc != -ENODEV) {
  239. kfree_skb(skb);
  240. return rc;
  241. }
  242. } else {
  243. spin_unlock_bh(&hba->hba_lock);
  244. }
  245. elen = sizeof(struct ethhdr);
  246. hlen = sizeof(struct fcoe_hdr);
  247. tlen = sizeof(struct fcoe_crc_eof);
  248. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  249. skb->ip_summed = CHECKSUM_NONE;
  250. crc = fcoe_fc_crc(fp);
  251. /* copy port crc and eof to the skb buff */
  252. if (skb_is_nonlinear(skb)) {
  253. skb_frag_t *frag;
  254. if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
  255. kfree_skb(skb);
  256. return -ENOMEM;
  257. }
  258. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  259. cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
  260. + frag->page_offset;
  261. } else {
  262. cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
  263. }
  264. memset(cp, 0, sizeof(*cp));
  265. cp->fcoe_eof = eof;
  266. cp->fcoe_crc32 = cpu_to_le32(~crc);
  267. if (skb_is_nonlinear(skb)) {
  268. kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
  269. cp = NULL;
  270. }
  271. /* adjust skb network/transport offsets to match mac/fcoe/port */
  272. skb_push(skb, elen + hlen);
  273. skb_reset_mac_header(skb);
  274. skb_reset_network_header(skb);
  275. skb->mac_len = elen;
  276. skb->protocol = htons(ETH_P_FCOE);
  277. skb->dev = hba->netdev;
  278. /* fill up mac and fcoe headers */
  279. eh = eth_hdr(skb);
  280. eh->h_proto = htons(ETH_P_FCOE);
  281. if (hba->ctlr.map_dest)
  282. fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
  283. else
  284. /* insert GW address */
  285. memcpy(eh->h_dest, hba->ctlr.dest_addr, ETH_ALEN);
  286. if (unlikely(hba->ctlr.flogi_oxid != FC_XID_UNKNOWN))
  287. memcpy(eh->h_source, hba->ctlr.ctl_src_addr, ETH_ALEN);
  288. else
  289. memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
  290. hp = (struct fcoe_hdr *)(eh + 1);
  291. memset(hp, 0, sizeof(*hp));
  292. if (FC_FCOE_VER)
  293. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  294. hp->fcoe_sof = sof;
  295. /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
  296. if (lport->seq_offload && fr_max_payload(fp)) {
  297. skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
  298. skb_shinfo(skb)->gso_size = fr_max_payload(fp);
  299. } else {
  300. skb_shinfo(skb)->gso_type = 0;
  301. skb_shinfo(skb)->gso_size = 0;
  302. }
  303. /*update tx stats */
  304. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  305. stats->TxFrames++;
  306. stats->TxWords += wlen;
  307. put_cpu();
  308. /* send down to lld */
  309. fr_dev(fp) = lport;
  310. if (port->fcoe_pending_queue.qlen)
  311. fcoe_check_wait_queue(lport, skb);
  312. else if (fcoe_start_io(skb))
  313. fcoe_check_wait_queue(lport, skb);
  314. return 0;
  315. }
  316. /**
  317. * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
  318. *
  319. * @skb: the receive socket buffer
  320. * @dev: associated net device
  321. * @ptype: context
  322. * @olddev: last device
  323. *
  324. * This function receives the packet and builds FC frame and passes it up
  325. */
  326. static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
  327. struct packet_type *ptype, struct net_device *olddev)
  328. {
  329. struct fc_lport *lport;
  330. struct bnx2fc_hba *hba;
  331. struct fc_frame_header *fh;
  332. struct fcoe_rcv_info *fr;
  333. struct fcoe_percpu_s *bg;
  334. unsigned short oxid;
  335. hba = container_of(ptype, struct bnx2fc_hba, fcoe_packet_type);
  336. lport = hba->ctlr.lp;
  337. if (unlikely(lport == NULL)) {
  338. printk(KERN_ALERT PFX "bnx2fc_rcv: lport is NULL\n");
  339. goto err;
  340. }
  341. if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
  342. printk(KERN_ALERT PFX "bnx2fc_rcv: Wrong FC type frame\n");
  343. goto err;
  344. }
  345. /*
  346. * Check for minimum frame length, and make sure required FCoE
  347. * and FC headers are pulled into the linear data area.
  348. */
  349. if (unlikely((skb->len < FCOE_MIN_FRAME) ||
  350. !pskb_may_pull(skb, FCOE_HEADER_LEN)))
  351. goto err;
  352. skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
  353. fh = (struct fc_frame_header *) skb_transport_header(skb);
  354. oxid = ntohs(fh->fh_ox_id);
  355. fr = fcoe_dev_from_skb(skb);
  356. fr->fr_dev = lport;
  357. fr->ptype = ptype;
  358. bg = &bnx2fc_global;
  359. spin_lock_bh(&bg->fcoe_rx_list.lock);
  360. __skb_queue_tail(&bg->fcoe_rx_list, skb);
  361. if (bg->fcoe_rx_list.qlen == 1)
  362. wake_up_process(bg->thread);
  363. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  364. return 0;
  365. err:
  366. kfree_skb(skb);
  367. return -1;
  368. }
  369. static int bnx2fc_l2_rcv_thread(void *arg)
  370. {
  371. struct fcoe_percpu_s *bg = arg;
  372. struct sk_buff *skb;
  373. set_user_nice(current, -20);
  374. set_current_state(TASK_INTERRUPTIBLE);
  375. while (!kthread_should_stop()) {
  376. schedule();
  377. spin_lock_bh(&bg->fcoe_rx_list.lock);
  378. while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
  379. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  380. bnx2fc_recv_frame(skb);
  381. spin_lock_bh(&bg->fcoe_rx_list.lock);
  382. }
  383. __set_current_state(TASK_INTERRUPTIBLE);
  384. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  385. }
  386. __set_current_state(TASK_RUNNING);
  387. return 0;
  388. }
  389. static void bnx2fc_recv_frame(struct sk_buff *skb)
  390. {
  391. u32 fr_len;
  392. struct fc_lport *lport;
  393. struct fcoe_rcv_info *fr;
  394. struct fcoe_dev_stats *stats;
  395. struct fc_frame_header *fh;
  396. struct fcoe_crc_eof crc_eof;
  397. struct fc_frame *fp;
  398. struct fc_lport *vn_port;
  399. struct fcoe_port *port;
  400. u8 *mac = NULL;
  401. u8 *dest_mac = NULL;
  402. struct fcoe_hdr *hp;
  403. fr = fcoe_dev_from_skb(skb);
  404. lport = fr->fr_dev;
  405. if (unlikely(lport == NULL)) {
  406. printk(KERN_ALERT PFX "Invalid lport struct\n");
  407. kfree_skb(skb);
  408. return;
  409. }
  410. if (skb_is_nonlinear(skb))
  411. skb_linearize(skb);
  412. mac = eth_hdr(skb)->h_source;
  413. dest_mac = eth_hdr(skb)->h_dest;
  414. /* Pull the header */
  415. hp = (struct fcoe_hdr *) skb_network_header(skb);
  416. fh = (struct fc_frame_header *) skb_transport_header(skb);
  417. skb_pull(skb, sizeof(struct fcoe_hdr));
  418. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  419. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  420. stats->RxFrames++;
  421. stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
  422. fp = (struct fc_frame *)skb;
  423. fc_frame_init(fp);
  424. fr_dev(fp) = lport;
  425. fr_sof(fp) = hp->fcoe_sof;
  426. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
  427. put_cpu();
  428. kfree_skb(skb);
  429. return;
  430. }
  431. fr_eof(fp) = crc_eof.fcoe_eof;
  432. fr_crc(fp) = crc_eof.fcoe_crc32;
  433. if (pskb_trim(skb, fr_len)) {
  434. put_cpu();
  435. kfree_skb(skb);
  436. return;
  437. }
  438. fh = fc_frame_header_get(fp);
  439. vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
  440. if (vn_port) {
  441. port = lport_priv(vn_port);
  442. if (compare_ether_addr(port->data_src_addr, dest_mac)
  443. != 0) {
  444. BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
  445. put_cpu();
  446. kfree_skb(skb);
  447. return;
  448. }
  449. }
  450. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
  451. fh->fh_type == FC_TYPE_FCP) {
  452. /* Drop FCP data. We dont this in L2 path */
  453. put_cpu();
  454. kfree_skb(skb);
  455. return;
  456. }
  457. if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
  458. fh->fh_type == FC_TYPE_ELS) {
  459. switch (fc_frame_payload_op(fp)) {
  460. case ELS_LOGO:
  461. if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
  462. /* drop non-FIP LOGO */
  463. put_cpu();
  464. kfree_skb(skb);
  465. return;
  466. }
  467. break;
  468. }
  469. }
  470. if (le32_to_cpu(fr_crc(fp)) !=
  471. ~crc32(~0, skb->data, fr_len)) {
  472. if (stats->InvalidCRCCount < 5)
  473. printk(KERN_WARNING PFX "dropping frame with "
  474. "CRC error\n");
  475. stats->InvalidCRCCount++;
  476. put_cpu();
  477. kfree_skb(skb);
  478. return;
  479. }
  480. put_cpu();
  481. fc_exch_recv(lport, fp);
  482. }
  483. /**
  484. * bnx2fc_percpu_io_thread - thread per cpu for ios
  485. *
  486. * @arg: ptr to bnx2fc_percpu_info structure
  487. */
  488. int bnx2fc_percpu_io_thread(void *arg)
  489. {
  490. struct bnx2fc_percpu_s *p = arg;
  491. struct bnx2fc_work *work, *tmp;
  492. LIST_HEAD(work_list);
  493. set_user_nice(current, -20);
  494. set_current_state(TASK_INTERRUPTIBLE);
  495. while (!kthread_should_stop()) {
  496. schedule();
  497. spin_lock_bh(&p->fp_work_lock);
  498. while (!list_empty(&p->work_list)) {
  499. list_splice_init(&p->work_list, &work_list);
  500. spin_unlock_bh(&p->fp_work_lock);
  501. list_for_each_entry_safe(work, tmp, &work_list, list) {
  502. list_del_init(&work->list);
  503. bnx2fc_process_cq_compl(work->tgt, work->wqe);
  504. kfree(work);
  505. }
  506. spin_lock_bh(&p->fp_work_lock);
  507. }
  508. __set_current_state(TASK_INTERRUPTIBLE);
  509. spin_unlock_bh(&p->fp_work_lock);
  510. }
  511. __set_current_state(TASK_RUNNING);
  512. return 0;
  513. }
  514. static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
  515. {
  516. struct fc_host_statistics *bnx2fc_stats;
  517. struct fc_lport *lport = shost_priv(shost);
  518. struct fcoe_port *port = lport_priv(lport);
  519. struct bnx2fc_hba *hba = port->priv;
  520. struct fcoe_statistics_params *fw_stats;
  521. int rc = 0;
  522. fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
  523. if (!fw_stats)
  524. return NULL;
  525. bnx2fc_stats = fc_get_host_stats(shost);
  526. init_completion(&hba->stat_req_done);
  527. if (bnx2fc_send_stat_req(hba))
  528. return bnx2fc_stats;
  529. rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
  530. if (!rc) {
  531. BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
  532. return bnx2fc_stats;
  533. }
  534. bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
  535. bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
  536. bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
  537. bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
  538. bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
  539. bnx2fc_stats->dumped_frames = 0;
  540. bnx2fc_stats->lip_count = 0;
  541. bnx2fc_stats->nos_count = 0;
  542. bnx2fc_stats->loss_of_sync_count = 0;
  543. bnx2fc_stats->loss_of_signal_count = 0;
  544. bnx2fc_stats->prim_seq_protocol_err_count = 0;
  545. return bnx2fc_stats;
  546. }
  547. static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
  548. {
  549. struct fcoe_port *port = lport_priv(lport);
  550. struct bnx2fc_hba *hba = port->priv;
  551. struct Scsi_Host *shost = lport->host;
  552. int rc = 0;
  553. shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
  554. shost->max_lun = BNX2FC_MAX_LUN;
  555. shost->max_id = BNX2FC_MAX_FCP_TGT;
  556. shost->max_channel = 0;
  557. if (lport->vport)
  558. shost->transportt = bnx2fc_vport_xport_template;
  559. else
  560. shost->transportt = bnx2fc_transport_template;
  561. /* Add the new host to SCSI-ml */
  562. rc = scsi_add_host(lport->host, dev);
  563. if (rc) {
  564. printk(KERN_ERR PFX "Error on scsi_add_host\n");
  565. return rc;
  566. }
  567. if (!lport->vport)
  568. fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
  569. sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
  570. BNX2FC_NAME, BNX2FC_VERSION,
  571. hba->netdev->name);
  572. return 0;
  573. }
  574. static void bnx2fc_link_speed_update(struct fc_lport *lport)
  575. {
  576. struct fcoe_port *port = lport_priv(lport);
  577. struct bnx2fc_hba *hba = port->priv;
  578. struct net_device *netdev = hba->netdev;
  579. struct ethtool_cmd ecmd;
  580. if (!dev_ethtool_get_settings(netdev, &ecmd)) {
  581. lport->link_supported_speeds &=
  582. ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
  583. if (ecmd.supported & (SUPPORTED_1000baseT_Half |
  584. SUPPORTED_1000baseT_Full))
  585. lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
  586. if (ecmd.supported & SUPPORTED_10000baseT_Full)
  587. lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
  588. switch (ethtool_cmd_speed(&ecmd)) {
  589. case SPEED_1000:
  590. lport->link_speed = FC_PORTSPEED_1GBIT;
  591. break;
  592. case SPEED_10000:
  593. lport->link_speed = FC_PORTSPEED_10GBIT;
  594. break;
  595. }
  596. }
  597. }
  598. static int bnx2fc_link_ok(struct fc_lport *lport)
  599. {
  600. struct fcoe_port *port = lport_priv(lport);
  601. struct bnx2fc_hba *hba = port->priv;
  602. struct net_device *dev = hba->phys_dev;
  603. int rc = 0;
  604. if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
  605. clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  606. else {
  607. set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  608. rc = -1;
  609. }
  610. return rc;
  611. }
  612. /**
  613. * bnx2fc_get_link_state - get network link state
  614. *
  615. * @hba: adapter instance pointer
  616. *
  617. * updates adapter structure flag based on netdev state
  618. */
  619. void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
  620. {
  621. if (test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state))
  622. set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  623. else
  624. clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  625. }
  626. static int bnx2fc_net_config(struct fc_lport *lport)
  627. {
  628. struct bnx2fc_hba *hba;
  629. struct fcoe_port *port;
  630. u64 wwnn, wwpn;
  631. port = lport_priv(lport);
  632. hba = port->priv;
  633. /* require support for get_pauseparam ethtool op. */
  634. if (!hba->phys_dev->ethtool_ops ||
  635. !hba->phys_dev->ethtool_ops->get_pauseparam)
  636. return -EOPNOTSUPP;
  637. if (fc_set_mfs(lport, BNX2FC_MFS))
  638. return -EINVAL;
  639. skb_queue_head_init(&port->fcoe_pending_queue);
  640. port->fcoe_pending_queue_active = 0;
  641. setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
  642. bnx2fc_link_speed_update(lport);
  643. if (!lport->vport) {
  644. wwnn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 1, 0);
  645. BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
  646. fc_set_wwnn(lport, wwnn);
  647. wwpn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 2, 0);
  648. BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
  649. fc_set_wwpn(lport, wwpn);
  650. }
  651. return 0;
  652. }
  653. static void bnx2fc_destroy_timer(unsigned long data)
  654. {
  655. struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
  656. BNX2FC_HBA_DBG(hba->ctlr.lp, "ERROR:bnx2fc_destroy_timer - "
  657. "Destroy compl not received!!\n");
  658. hba->flags |= BNX2FC_FLAG_DESTROY_CMPL;
  659. wake_up_interruptible(&hba->destroy_wait);
  660. }
  661. /**
  662. * bnx2fc_indicate_netevent - Generic netdev event handler
  663. *
  664. * @context: adapter structure pointer
  665. * @event: event type
  666. *
  667. * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
  668. * NETDEV_CHANGE_MTU events
  669. */
  670. static void bnx2fc_indicate_netevent(void *context, unsigned long event)
  671. {
  672. struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
  673. struct fc_lport *lport = hba->ctlr.lp;
  674. struct fc_lport *vport;
  675. u32 link_possible = 1;
  676. if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  677. BNX2FC_MISC_DBG("driver not ready. event=%s %ld\n",
  678. hba->netdev->name, event);
  679. return;
  680. }
  681. /*
  682. * ASSUMPTION:
  683. * indicate_netevent cannot be called from cnic unless bnx2fc
  684. * does register_device
  685. */
  686. BUG_ON(!lport);
  687. BNX2FC_HBA_DBG(lport, "enter netevent handler - event=%s %ld\n",
  688. hba->netdev->name, event);
  689. switch (event) {
  690. case NETDEV_UP:
  691. BNX2FC_HBA_DBG(lport, "Port up, adapter_state = %ld\n",
  692. hba->adapter_state);
  693. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
  694. printk(KERN_ERR "indicate_netevent: "\
  695. "adapter is not UP!!\n");
  696. break;
  697. case NETDEV_DOWN:
  698. BNX2FC_HBA_DBG(lport, "Port down\n");
  699. clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  700. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  701. link_possible = 0;
  702. break;
  703. case NETDEV_GOING_DOWN:
  704. BNX2FC_HBA_DBG(lport, "Port going down\n");
  705. set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  706. link_possible = 0;
  707. break;
  708. case NETDEV_CHANGE:
  709. BNX2FC_HBA_DBG(lport, "NETDEV_CHANGE\n");
  710. break;
  711. default:
  712. printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
  713. return;
  714. }
  715. bnx2fc_link_speed_update(lport);
  716. if (link_possible && !bnx2fc_link_ok(lport)) {
  717. printk(KERN_ERR "indicate_netevent: call ctlr_link_up\n");
  718. fcoe_ctlr_link_up(&hba->ctlr);
  719. } else {
  720. printk(KERN_ERR "indicate_netevent: call ctlr_link_down\n");
  721. if (fcoe_ctlr_link_down(&hba->ctlr)) {
  722. clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
  723. mutex_lock(&lport->lp_mutex);
  724. list_for_each_entry(vport, &lport->vports, list)
  725. fc_host_port_type(vport->host) =
  726. FC_PORTTYPE_UNKNOWN;
  727. mutex_unlock(&lport->lp_mutex);
  728. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  729. per_cpu_ptr(lport->dev_stats,
  730. get_cpu())->LinkFailureCount++;
  731. put_cpu();
  732. fcoe_clean_pending_queue(lport);
  733. init_waitqueue_head(&hba->shutdown_wait);
  734. BNX2FC_HBA_DBG(lport, "indicate_netevent "
  735. "num_ofld_sess = %d\n",
  736. hba->num_ofld_sess);
  737. hba->wait_for_link_down = 1;
  738. BNX2FC_HBA_DBG(lport, "waiting for uploads to "
  739. "compl proc = %s\n",
  740. current->comm);
  741. wait_event_interruptible(hba->shutdown_wait,
  742. (hba->num_ofld_sess == 0));
  743. BNX2FC_HBA_DBG(lport, "wakeup - num_ofld_sess = %d\n",
  744. hba->num_ofld_sess);
  745. hba->wait_for_link_down = 0;
  746. if (signal_pending(current))
  747. flush_signals(current);
  748. }
  749. }
  750. }
  751. static int bnx2fc_libfc_config(struct fc_lport *lport)
  752. {
  753. /* Set the function pointers set by bnx2fc driver */
  754. memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
  755. sizeof(struct libfc_function_template));
  756. fc_elsct_init(lport);
  757. fc_exch_init(lport);
  758. fc_rport_init(lport);
  759. fc_disc_init(lport);
  760. return 0;
  761. }
  762. static int bnx2fc_em_config(struct fc_lport *lport)
  763. {
  764. struct fcoe_port *port = lport_priv(lport);
  765. struct bnx2fc_hba *hba = port->priv;
  766. if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
  767. FCOE_MAX_XID, NULL)) {
  768. printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
  769. return -ENOMEM;
  770. }
  771. hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
  772. BNX2FC_MAX_XID);
  773. if (!hba->cmd_mgr) {
  774. printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
  775. fc_exch_mgr_free(lport);
  776. return -ENOMEM;
  777. }
  778. return 0;
  779. }
  780. static int bnx2fc_lport_config(struct fc_lport *lport)
  781. {
  782. lport->link_up = 0;
  783. lport->qfull = 0;
  784. lport->max_retry_count = 3;
  785. lport->max_rport_retry_count = 3;
  786. lport->e_d_tov = 2 * 1000;
  787. lport->r_a_tov = 10 * 1000;
  788. /* REVISIT: enable when supporting tape devices
  789. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  790. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  791. */
  792. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS);
  793. lport->does_npiv = 1;
  794. memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
  795. lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
  796. /* alloc stats structure */
  797. if (fc_lport_init_stats(lport))
  798. return -ENOMEM;
  799. /* Finish fc_lport configuration */
  800. fc_lport_config(lport);
  801. return 0;
  802. }
  803. /**
  804. * bnx2fc_fip_recv - handle a received FIP frame.
  805. *
  806. * @skb: the received skb
  807. * @dev: associated &net_device
  808. * @ptype: the &packet_type structure which was used to register this handler.
  809. * @orig_dev: original receive &net_device, in case @ dev is a bond.
  810. *
  811. * Returns: 0 for success
  812. */
  813. static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
  814. struct packet_type *ptype,
  815. struct net_device *orig_dev)
  816. {
  817. struct bnx2fc_hba *hba;
  818. hba = container_of(ptype, struct bnx2fc_hba, fip_packet_type);
  819. fcoe_ctlr_recv(&hba->ctlr, skb);
  820. return 0;
  821. }
  822. /**
  823. * bnx2fc_update_src_mac - Update Ethernet MAC filters.
  824. *
  825. * @fip: FCoE controller.
  826. * @old: Unicast MAC address to delete if the MAC is non-zero.
  827. * @new: Unicast MAC address to add.
  828. *
  829. * Remove any previously-set unicast MAC filter.
  830. * Add secondary FCoE MAC address filter for our OUI.
  831. */
  832. static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
  833. {
  834. struct fcoe_port *port = lport_priv(lport);
  835. memcpy(port->data_src_addr, addr, ETH_ALEN);
  836. }
  837. /**
  838. * bnx2fc_get_src_mac - return the ethernet source address for an lport
  839. *
  840. * @lport: libfc port
  841. */
  842. static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
  843. {
  844. struct fcoe_port *port;
  845. port = (struct fcoe_port *)lport_priv(lport);
  846. return port->data_src_addr;
  847. }
  848. /**
  849. * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
  850. *
  851. * @fip: FCoE controller.
  852. * @skb: FIP Packet.
  853. */
  854. static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  855. {
  856. skb->dev = bnx2fc_from_ctlr(fip)->netdev;
  857. dev_queue_xmit(skb);
  858. }
  859. static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
  860. {
  861. struct Scsi_Host *shost = vport_to_shost(vport);
  862. struct fc_lport *n_port = shost_priv(shost);
  863. struct fcoe_port *port = lport_priv(n_port);
  864. struct bnx2fc_hba *hba = port->priv;
  865. struct net_device *netdev = hba->netdev;
  866. struct fc_lport *vn_port;
  867. if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  868. printk(KERN_ERR PFX "vn ports cannot be created on"
  869. "this hba\n");
  870. return -EIO;
  871. }
  872. mutex_lock(&bnx2fc_dev_lock);
  873. vn_port = bnx2fc_if_create(hba, &vport->dev, 1);
  874. mutex_unlock(&bnx2fc_dev_lock);
  875. if (IS_ERR(vn_port)) {
  876. printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
  877. netdev->name);
  878. return -EIO;
  879. }
  880. if (disabled) {
  881. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  882. } else {
  883. vn_port->boot_time = jiffies;
  884. fc_lport_init(vn_port);
  885. fc_fabric_login(vn_port);
  886. fc_vport_setlink(vn_port);
  887. }
  888. return 0;
  889. }
  890. static int bnx2fc_vport_destroy(struct fc_vport *vport)
  891. {
  892. struct Scsi_Host *shost = vport_to_shost(vport);
  893. struct fc_lport *n_port = shost_priv(shost);
  894. struct fc_lport *vn_port = vport->dd_data;
  895. struct fcoe_port *port = lport_priv(vn_port);
  896. mutex_lock(&n_port->lp_mutex);
  897. list_del(&vn_port->list);
  898. mutex_unlock(&n_port->lp_mutex);
  899. queue_work(bnx2fc_wq, &port->destroy_work);
  900. return 0;
  901. }
  902. static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
  903. {
  904. struct fc_lport *lport = vport->dd_data;
  905. if (disable) {
  906. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  907. fc_fabric_logoff(lport);
  908. } else {
  909. lport->boot_time = jiffies;
  910. fc_fabric_login(lport);
  911. fc_vport_setlink(lport);
  912. }
  913. return 0;
  914. }
  915. static int bnx2fc_netdev_setup(struct bnx2fc_hba *hba)
  916. {
  917. struct net_device *netdev = hba->netdev;
  918. struct net_device *physdev = hba->phys_dev;
  919. struct netdev_hw_addr *ha;
  920. int sel_san_mac = 0;
  921. /* setup Source MAC Address */
  922. rcu_read_lock();
  923. for_each_dev_addr(physdev, ha) {
  924. BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
  925. ha->type);
  926. printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
  927. ha->addr[1], ha->addr[2], ha->addr[3],
  928. ha->addr[4], ha->addr[5]);
  929. if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
  930. (is_valid_ether_addr(ha->addr))) {
  931. memcpy(hba->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
  932. sel_san_mac = 1;
  933. BNX2FC_MISC_DBG("Found SAN MAC\n");
  934. }
  935. }
  936. rcu_read_unlock();
  937. if (!sel_san_mac)
  938. return -ENODEV;
  939. hba->fip_packet_type.func = bnx2fc_fip_recv;
  940. hba->fip_packet_type.type = htons(ETH_P_FIP);
  941. hba->fip_packet_type.dev = netdev;
  942. dev_add_pack(&hba->fip_packet_type);
  943. hba->fcoe_packet_type.func = bnx2fc_rcv;
  944. hba->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  945. hba->fcoe_packet_type.dev = netdev;
  946. dev_add_pack(&hba->fcoe_packet_type);
  947. return 0;
  948. }
  949. static int bnx2fc_attach_transport(void)
  950. {
  951. bnx2fc_transport_template =
  952. fc_attach_transport(&bnx2fc_transport_function);
  953. if (bnx2fc_transport_template == NULL) {
  954. printk(KERN_ERR PFX "Failed to attach FC transport\n");
  955. return -ENODEV;
  956. }
  957. bnx2fc_vport_xport_template =
  958. fc_attach_transport(&bnx2fc_vport_xport_function);
  959. if (bnx2fc_vport_xport_template == NULL) {
  960. printk(KERN_ERR PFX
  961. "Failed to attach FC transport for vport\n");
  962. fc_release_transport(bnx2fc_transport_template);
  963. bnx2fc_transport_template = NULL;
  964. return -ENODEV;
  965. }
  966. return 0;
  967. }
  968. static void bnx2fc_release_transport(void)
  969. {
  970. fc_release_transport(bnx2fc_transport_template);
  971. fc_release_transport(bnx2fc_vport_xport_template);
  972. bnx2fc_transport_template = NULL;
  973. bnx2fc_vport_xport_template = NULL;
  974. }
  975. static void bnx2fc_interface_release(struct kref *kref)
  976. {
  977. struct bnx2fc_hba *hba;
  978. struct net_device *netdev;
  979. struct net_device *phys_dev;
  980. hba = container_of(kref, struct bnx2fc_hba, kref);
  981. BNX2FC_MISC_DBG("Interface is being released\n");
  982. netdev = hba->netdev;
  983. phys_dev = hba->phys_dev;
  984. /* tear-down FIP controller */
  985. if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done))
  986. fcoe_ctlr_destroy(&hba->ctlr);
  987. /* Free the command manager */
  988. if (hba->cmd_mgr) {
  989. bnx2fc_cmd_mgr_free(hba->cmd_mgr);
  990. hba->cmd_mgr = NULL;
  991. }
  992. dev_put(netdev);
  993. module_put(THIS_MODULE);
  994. }
  995. static inline void bnx2fc_interface_get(struct bnx2fc_hba *hba)
  996. {
  997. kref_get(&hba->kref);
  998. }
  999. static inline void bnx2fc_interface_put(struct bnx2fc_hba *hba)
  1000. {
  1001. kref_put(&hba->kref, bnx2fc_interface_release);
  1002. }
  1003. static void bnx2fc_interface_destroy(struct bnx2fc_hba *hba)
  1004. {
  1005. bnx2fc_unbind_pcidev(hba);
  1006. kfree(hba);
  1007. }
  1008. /**
  1009. * bnx2fc_interface_create - create a new fcoe instance
  1010. *
  1011. * @cnic: pointer to cnic device
  1012. *
  1013. * Creates a new FCoE instance on the given device which include allocating
  1014. * hba structure, scsi_host and lport structures.
  1015. */
  1016. static struct bnx2fc_hba *bnx2fc_interface_create(struct cnic_dev *cnic)
  1017. {
  1018. struct bnx2fc_hba *hba;
  1019. int rc;
  1020. hba = kzalloc(sizeof(*hba), GFP_KERNEL);
  1021. if (!hba) {
  1022. printk(KERN_ERR PFX "Unable to allocate hba structure\n");
  1023. return NULL;
  1024. }
  1025. spin_lock_init(&hba->hba_lock);
  1026. mutex_init(&hba->hba_mutex);
  1027. hba->cnic = cnic;
  1028. rc = bnx2fc_bind_pcidev(hba);
  1029. if (rc)
  1030. goto bind_err;
  1031. hba->phys_dev = cnic->netdev;
  1032. /* will get overwritten after we do vlan discovery */
  1033. hba->netdev = hba->phys_dev;
  1034. init_waitqueue_head(&hba->shutdown_wait);
  1035. init_waitqueue_head(&hba->destroy_wait);
  1036. return hba;
  1037. bind_err:
  1038. printk(KERN_ERR PFX "create_interface: bind error\n");
  1039. kfree(hba);
  1040. return NULL;
  1041. }
  1042. static int bnx2fc_interface_setup(struct bnx2fc_hba *hba,
  1043. enum fip_state fip_mode)
  1044. {
  1045. int rc = 0;
  1046. struct net_device *netdev = hba->netdev;
  1047. struct fcoe_ctlr *fip = &hba->ctlr;
  1048. dev_hold(netdev);
  1049. kref_init(&hba->kref);
  1050. hba->flags = 0;
  1051. /* Initialize FIP */
  1052. memset(fip, 0, sizeof(*fip));
  1053. fcoe_ctlr_init(fip, fip_mode);
  1054. hba->ctlr.send = bnx2fc_fip_send;
  1055. hba->ctlr.update_mac = bnx2fc_update_src_mac;
  1056. hba->ctlr.get_src_addr = bnx2fc_get_src_mac;
  1057. set_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done);
  1058. rc = bnx2fc_netdev_setup(hba);
  1059. if (rc)
  1060. goto setup_err;
  1061. hba->next_conn_id = 0;
  1062. memset(hba->tgt_ofld_list, 0, sizeof(hba->tgt_ofld_list));
  1063. hba->num_ofld_sess = 0;
  1064. return 0;
  1065. setup_err:
  1066. fcoe_ctlr_destroy(&hba->ctlr);
  1067. dev_put(netdev);
  1068. bnx2fc_interface_put(hba);
  1069. return rc;
  1070. }
  1071. /**
  1072. * bnx2fc_if_create - Create FCoE instance on a given interface
  1073. *
  1074. * @hba: FCoE interface to create a local port on
  1075. * @parent: Device pointer to be the parent in sysfs for the SCSI host
  1076. * @npiv: Indicates if the port is vport or not
  1077. *
  1078. * Creates a fc_lport instance and a Scsi_Host instance and configure them.
  1079. *
  1080. * Returns: Allocated fc_lport or an error pointer
  1081. */
  1082. static struct fc_lport *bnx2fc_if_create(struct bnx2fc_hba *hba,
  1083. struct device *parent, int npiv)
  1084. {
  1085. struct fc_lport *lport, *n_port;
  1086. struct fcoe_port *port;
  1087. struct Scsi_Host *shost;
  1088. struct fc_vport *vport = dev_to_vport(parent);
  1089. int rc = 0;
  1090. /* Allocate Scsi_Host structure */
  1091. if (!npiv)
  1092. lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
  1093. else
  1094. lport = libfc_vport_create(vport, sizeof(*port));
  1095. if (!lport) {
  1096. printk(KERN_ERR PFX "could not allocate scsi host structure\n");
  1097. return NULL;
  1098. }
  1099. shost = lport->host;
  1100. port = lport_priv(lport);
  1101. port->lport = lport;
  1102. port->priv = hba;
  1103. INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
  1104. /* Configure fcoe_port */
  1105. rc = bnx2fc_lport_config(lport);
  1106. if (rc)
  1107. goto lp_config_err;
  1108. if (npiv) {
  1109. printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
  1110. vport->node_name, vport->port_name);
  1111. fc_set_wwnn(lport, vport->node_name);
  1112. fc_set_wwpn(lport, vport->port_name);
  1113. }
  1114. /* Configure netdev and networking properties of the lport */
  1115. rc = bnx2fc_net_config(lport);
  1116. if (rc) {
  1117. printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
  1118. goto lp_config_err;
  1119. }
  1120. rc = bnx2fc_shost_config(lport, parent);
  1121. if (rc) {
  1122. printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
  1123. hba->netdev->name);
  1124. goto lp_config_err;
  1125. }
  1126. /* Initialize the libfc library */
  1127. rc = bnx2fc_libfc_config(lport);
  1128. if (rc) {
  1129. printk(KERN_ERR PFX "Couldnt configure libfc\n");
  1130. goto shost_err;
  1131. }
  1132. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  1133. /* Allocate exchange manager */
  1134. if (!npiv)
  1135. rc = bnx2fc_em_config(lport);
  1136. else {
  1137. shost = vport_to_shost(vport);
  1138. n_port = shost_priv(shost);
  1139. rc = fc_exch_mgr_list_clone(n_port, lport);
  1140. }
  1141. if (rc) {
  1142. printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
  1143. goto shost_err;
  1144. }
  1145. bnx2fc_interface_get(hba);
  1146. return lport;
  1147. shost_err:
  1148. scsi_remove_host(shost);
  1149. lp_config_err:
  1150. scsi_host_put(lport->host);
  1151. return NULL;
  1152. }
  1153. static void bnx2fc_netdev_cleanup(struct bnx2fc_hba *hba)
  1154. {
  1155. /* Dont listen for Ethernet packets anymore */
  1156. __dev_remove_pack(&hba->fcoe_packet_type);
  1157. __dev_remove_pack(&hba->fip_packet_type);
  1158. synchronize_net();
  1159. }
  1160. static void bnx2fc_if_destroy(struct fc_lport *lport)
  1161. {
  1162. struct fcoe_port *port = lport_priv(lport);
  1163. struct bnx2fc_hba *hba = port->priv;
  1164. BNX2FC_HBA_DBG(hba->ctlr.lp, "ENTERED bnx2fc_if_destroy\n");
  1165. /* Stop the transmit retry timer */
  1166. del_timer_sync(&port->timer);
  1167. /* Free existing transmit skbs */
  1168. fcoe_clean_pending_queue(lport);
  1169. /* Free queued packets for the receive thread */
  1170. bnx2fc_clean_rx_queue(lport);
  1171. /* Detach from scsi-ml */
  1172. fc_remove_host(lport->host);
  1173. scsi_remove_host(lport->host);
  1174. /*
  1175. * Note that only the physical lport will have the exchange manager.
  1176. * for vports, this function is NOP
  1177. */
  1178. fc_exch_mgr_free(lport);
  1179. /* Free memory used by statistical counters */
  1180. fc_lport_free_stats(lport);
  1181. /* Release Scsi_Host */
  1182. scsi_host_put(lport->host);
  1183. bnx2fc_interface_put(hba);
  1184. }
  1185. /**
  1186. * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
  1187. *
  1188. * @buffer: The name of the Ethernet interface to be destroyed
  1189. * @kp: The associated kernel parameter
  1190. *
  1191. * Called from sysfs.
  1192. *
  1193. * Returns: 0 for success
  1194. */
  1195. static int bnx2fc_destroy(struct net_device *netdev)
  1196. {
  1197. struct bnx2fc_hba *hba = NULL;
  1198. struct net_device *phys_dev;
  1199. int rc = 0;
  1200. rtnl_lock();
  1201. mutex_lock(&bnx2fc_dev_lock);
  1202. /* obtain physical netdev */
  1203. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1204. phys_dev = vlan_dev_real_dev(netdev);
  1205. else {
  1206. printk(KERN_ERR PFX "Not a vlan device\n");
  1207. rc = -ENODEV;
  1208. goto netdev_err;
  1209. }
  1210. hba = bnx2fc_hba_lookup(phys_dev);
  1211. if (!hba || !hba->ctlr.lp) {
  1212. rc = -ENODEV;
  1213. printk(KERN_ERR PFX "bnx2fc_destroy: hba or lport not found\n");
  1214. goto netdev_err;
  1215. }
  1216. if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1217. printk(KERN_ERR PFX "bnx2fc_destroy: Create not called\n");
  1218. goto netdev_err;
  1219. }
  1220. bnx2fc_netdev_cleanup(hba);
  1221. bnx2fc_stop(hba);
  1222. bnx2fc_if_destroy(hba->ctlr.lp);
  1223. destroy_workqueue(hba->timer_work_queue);
  1224. if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
  1225. bnx2fc_fw_destroy(hba);
  1226. clear_bit(BNX2FC_CREATE_DONE, &hba->init_done);
  1227. netdev_err:
  1228. mutex_unlock(&bnx2fc_dev_lock);
  1229. rtnl_unlock();
  1230. return rc;
  1231. }
  1232. static void bnx2fc_destroy_work(struct work_struct *work)
  1233. {
  1234. struct fcoe_port *port;
  1235. struct fc_lport *lport;
  1236. port = container_of(work, struct fcoe_port, destroy_work);
  1237. lport = port->lport;
  1238. BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
  1239. bnx2fc_port_shutdown(lport);
  1240. rtnl_lock();
  1241. mutex_lock(&bnx2fc_dev_lock);
  1242. bnx2fc_if_destroy(lport);
  1243. mutex_unlock(&bnx2fc_dev_lock);
  1244. rtnl_unlock();
  1245. }
  1246. static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
  1247. {
  1248. bnx2fc_free_fw_resc(hba);
  1249. bnx2fc_free_task_ctx(hba);
  1250. }
  1251. /**
  1252. * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
  1253. * pci structure
  1254. *
  1255. * @hba: Adapter instance
  1256. */
  1257. static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
  1258. {
  1259. if (bnx2fc_setup_task_ctx(hba))
  1260. goto mem_err;
  1261. if (bnx2fc_setup_fw_resc(hba))
  1262. goto mem_err;
  1263. return 0;
  1264. mem_err:
  1265. bnx2fc_unbind_adapter_devices(hba);
  1266. return -ENOMEM;
  1267. }
  1268. static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
  1269. {
  1270. struct cnic_dev *cnic;
  1271. if (!hba->cnic) {
  1272. printk(KERN_ERR PFX "cnic is NULL\n");
  1273. return -ENODEV;
  1274. }
  1275. cnic = hba->cnic;
  1276. hba->pcidev = cnic->pcidev;
  1277. if (hba->pcidev)
  1278. pci_dev_get(hba->pcidev);
  1279. return 0;
  1280. }
  1281. static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
  1282. {
  1283. if (hba->pcidev)
  1284. pci_dev_put(hba->pcidev);
  1285. hba->pcidev = NULL;
  1286. }
  1287. /**
  1288. * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
  1289. *
  1290. * @handle: transport handle pointing to adapter struture
  1291. *
  1292. * This function maps adapter structure to pcidev structure and initiates
  1293. * firmware handshake to enable/initialize on-chip FCoE components.
  1294. * This bnx2fc - cnic interface api callback is used after following
  1295. * conditions are met -
  1296. * a) underlying network interface is up (marked by event NETDEV_UP
  1297. * from netdev
  1298. * b) bnx2fc adatper structure is registered.
  1299. */
  1300. static void bnx2fc_ulp_start(void *handle)
  1301. {
  1302. struct bnx2fc_hba *hba = handle;
  1303. struct fc_lport *lport = hba->ctlr.lp;
  1304. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1305. mutex_lock(&bnx2fc_dev_lock);
  1306. if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
  1307. goto start_disc;
  1308. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done))
  1309. bnx2fc_fw_init(hba);
  1310. start_disc:
  1311. mutex_unlock(&bnx2fc_dev_lock);
  1312. BNX2FC_MISC_DBG("bnx2fc started.\n");
  1313. /* Kick off Fabric discovery*/
  1314. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1315. printk(KERN_ERR PFX "ulp_init: start discovery\n");
  1316. lport->tt.frame_send = bnx2fc_xmit;
  1317. bnx2fc_start_disc(hba);
  1318. }
  1319. }
  1320. static void bnx2fc_port_shutdown(struct fc_lport *lport)
  1321. {
  1322. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1323. fc_fabric_logoff(lport);
  1324. fc_lport_destroy(lport);
  1325. }
  1326. static void bnx2fc_stop(struct bnx2fc_hba *hba)
  1327. {
  1328. struct fc_lport *lport;
  1329. struct fc_lport *vport;
  1330. BNX2FC_MISC_DBG("ENTERED %s - init_done = %ld\n", __func__,
  1331. hba->init_done);
  1332. if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done) &&
  1333. test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1334. lport = hba->ctlr.lp;
  1335. bnx2fc_port_shutdown(lport);
  1336. BNX2FC_HBA_DBG(lport, "bnx2fc_stop: waiting for %d "
  1337. "offloaded sessions\n",
  1338. hba->num_ofld_sess);
  1339. wait_event_interruptible(hba->shutdown_wait,
  1340. (hba->num_ofld_sess == 0));
  1341. mutex_lock(&lport->lp_mutex);
  1342. list_for_each_entry(vport, &lport->vports, list)
  1343. fc_host_port_type(vport->host) = FC_PORTTYPE_UNKNOWN;
  1344. mutex_unlock(&lport->lp_mutex);
  1345. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  1346. fcoe_ctlr_link_down(&hba->ctlr);
  1347. fcoe_clean_pending_queue(lport);
  1348. mutex_lock(&hba->hba_mutex);
  1349. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  1350. clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  1351. clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
  1352. mutex_unlock(&hba->hba_mutex);
  1353. }
  1354. }
  1355. static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
  1356. {
  1357. #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
  1358. int rc = -1;
  1359. int i = HZ;
  1360. rc = bnx2fc_bind_adapter_devices(hba);
  1361. if (rc) {
  1362. printk(KERN_ALERT PFX
  1363. "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
  1364. goto err_out;
  1365. }
  1366. rc = bnx2fc_send_fw_fcoe_init_msg(hba);
  1367. if (rc) {
  1368. printk(KERN_ALERT PFX
  1369. "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
  1370. goto err_unbind;
  1371. }
  1372. /*
  1373. * Wait until the adapter init message is complete, and adapter
  1374. * state is UP.
  1375. */
  1376. while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
  1377. msleep(BNX2FC_INIT_POLL_TIME);
  1378. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
  1379. printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
  1380. "Ignoring...\n",
  1381. hba->cnic->netdev->name);
  1382. rc = -1;
  1383. goto err_unbind;
  1384. }
  1385. /* Mark HBA to indicate that the FW INIT is done */
  1386. set_bit(BNX2FC_FW_INIT_DONE, &hba->init_done);
  1387. return 0;
  1388. err_unbind:
  1389. bnx2fc_unbind_adapter_devices(hba);
  1390. err_out:
  1391. return rc;
  1392. }
  1393. static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
  1394. {
  1395. if (test_and_clear_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  1396. if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
  1397. init_timer(&hba->destroy_timer);
  1398. hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
  1399. jiffies;
  1400. hba->destroy_timer.function = bnx2fc_destroy_timer;
  1401. hba->destroy_timer.data = (unsigned long)hba;
  1402. add_timer(&hba->destroy_timer);
  1403. wait_event_interruptible(hba->destroy_wait,
  1404. (hba->flags &
  1405. BNX2FC_FLAG_DESTROY_CMPL));
  1406. /* This should never happen */
  1407. if (signal_pending(current))
  1408. flush_signals(current);
  1409. del_timer_sync(&hba->destroy_timer);
  1410. }
  1411. bnx2fc_unbind_adapter_devices(hba);
  1412. }
  1413. }
  1414. /**
  1415. * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
  1416. *
  1417. * @handle: transport handle pointing to adapter structure
  1418. *
  1419. * Driver checks if adapter is already in shutdown mode, if not start
  1420. * the shutdown process.
  1421. */
  1422. static void bnx2fc_ulp_stop(void *handle)
  1423. {
  1424. struct bnx2fc_hba *hba = (struct bnx2fc_hba *)handle;
  1425. printk(KERN_ERR "ULP_STOP\n");
  1426. mutex_lock(&bnx2fc_dev_lock);
  1427. bnx2fc_stop(hba);
  1428. bnx2fc_fw_destroy(hba);
  1429. mutex_unlock(&bnx2fc_dev_lock);
  1430. }
  1431. static void bnx2fc_start_disc(struct bnx2fc_hba *hba)
  1432. {
  1433. struct fc_lport *lport;
  1434. int wait_cnt = 0;
  1435. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1436. /* Kick off FIP/FLOGI */
  1437. if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  1438. printk(KERN_ERR PFX "Init not done yet\n");
  1439. return;
  1440. }
  1441. lport = hba->ctlr.lp;
  1442. BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
  1443. if (!bnx2fc_link_ok(lport)) {
  1444. BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
  1445. fcoe_ctlr_link_up(&hba->ctlr);
  1446. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1447. set_bit(ADAPTER_STATE_READY, &hba->adapter_state);
  1448. }
  1449. /* wait for the FCF to be selected before issuing FLOGI */
  1450. while (!hba->ctlr.sel_fcf) {
  1451. msleep(250);
  1452. /* give up after 3 secs */
  1453. if (++wait_cnt > 12)
  1454. break;
  1455. }
  1456. fc_lport_init(lport);
  1457. fc_fabric_login(lport);
  1458. }
  1459. /**
  1460. * bnx2fc_ulp_init - Initialize an adapter instance
  1461. *
  1462. * @dev : cnic device handle
  1463. * Called from cnic_register_driver() context to initialize all
  1464. * enumerated cnic devices. This routine allocates adapter structure
  1465. * and other device specific resources.
  1466. */
  1467. static void bnx2fc_ulp_init(struct cnic_dev *dev)
  1468. {
  1469. struct bnx2fc_hba *hba;
  1470. int rc = 0;
  1471. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1472. /* bnx2fc works only when bnx2x is loaded */
  1473. if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  1474. printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
  1475. " flags: %lx\n",
  1476. dev->netdev->name, dev->flags);
  1477. return;
  1478. }
  1479. /* Configure FCoE interface */
  1480. hba = bnx2fc_interface_create(dev);
  1481. if (!hba) {
  1482. printk(KERN_ERR PFX "hba initialization failed\n");
  1483. return;
  1484. }
  1485. /* Add HBA to the adapter list */
  1486. mutex_lock(&bnx2fc_dev_lock);
  1487. list_add_tail(&hba->link, &adapter_list);
  1488. adapter_count++;
  1489. mutex_unlock(&bnx2fc_dev_lock);
  1490. clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
  1491. rc = dev->register_device(dev, CNIC_ULP_FCOE,
  1492. (void *) hba);
  1493. if (rc)
  1494. printk(KERN_ALERT PFX "register_device failed, rc = %d\n", rc);
  1495. else
  1496. set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
  1497. }
  1498. static int bnx2fc_disable(struct net_device *netdev)
  1499. {
  1500. struct bnx2fc_hba *hba;
  1501. struct net_device *phys_dev;
  1502. struct ethtool_drvinfo drvinfo;
  1503. int rc = 0;
  1504. rtnl_lock();
  1505. mutex_lock(&bnx2fc_dev_lock);
  1506. /* obtain physical netdev */
  1507. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1508. phys_dev = vlan_dev_real_dev(netdev);
  1509. else {
  1510. printk(KERN_ERR PFX "Not a vlan device\n");
  1511. rc = -ENODEV;
  1512. goto nodev;
  1513. }
  1514. /* verify if the physical device is a netxtreme2 device */
  1515. if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
  1516. memset(&drvinfo, 0, sizeof(drvinfo));
  1517. phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
  1518. if (strcmp(drvinfo.driver, "bnx2x")) {
  1519. printk(KERN_ERR PFX "Not a netxtreme2 device\n");
  1520. rc = -ENODEV;
  1521. goto nodev;
  1522. }
  1523. } else {
  1524. printk(KERN_ERR PFX "unable to obtain drv_info\n");
  1525. rc = -ENODEV;
  1526. goto nodev;
  1527. }
  1528. printk(KERN_ERR PFX "phys_dev is netxtreme2 device\n");
  1529. /* obtain hba and initialize rest of the structure */
  1530. hba = bnx2fc_hba_lookup(phys_dev);
  1531. if (!hba || !hba->ctlr.lp) {
  1532. rc = -ENODEV;
  1533. printk(KERN_ERR PFX "bnx2fc_disable: hba or lport not found\n");
  1534. } else {
  1535. fcoe_ctlr_link_down(&hba->ctlr);
  1536. fcoe_clean_pending_queue(hba->ctlr.lp);
  1537. }
  1538. nodev:
  1539. mutex_unlock(&bnx2fc_dev_lock);
  1540. rtnl_unlock();
  1541. return rc;
  1542. }
  1543. static int bnx2fc_enable(struct net_device *netdev)
  1544. {
  1545. struct bnx2fc_hba *hba;
  1546. struct net_device *phys_dev;
  1547. struct ethtool_drvinfo drvinfo;
  1548. int rc = 0;
  1549. rtnl_lock();
  1550. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1551. mutex_lock(&bnx2fc_dev_lock);
  1552. /* obtain physical netdev */
  1553. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1554. phys_dev = vlan_dev_real_dev(netdev);
  1555. else {
  1556. printk(KERN_ERR PFX "Not a vlan device\n");
  1557. rc = -ENODEV;
  1558. goto nodev;
  1559. }
  1560. /* verify if the physical device is a netxtreme2 device */
  1561. if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
  1562. memset(&drvinfo, 0, sizeof(drvinfo));
  1563. phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
  1564. if (strcmp(drvinfo.driver, "bnx2x")) {
  1565. printk(KERN_ERR PFX "Not a netxtreme2 device\n");
  1566. rc = -ENODEV;
  1567. goto nodev;
  1568. }
  1569. } else {
  1570. printk(KERN_ERR PFX "unable to obtain drv_info\n");
  1571. rc = -ENODEV;
  1572. goto nodev;
  1573. }
  1574. /* obtain hba and initialize rest of the structure */
  1575. hba = bnx2fc_hba_lookup(phys_dev);
  1576. if (!hba || !hba->ctlr.lp) {
  1577. rc = -ENODEV;
  1578. printk(KERN_ERR PFX "bnx2fc_enable: hba or lport not found\n");
  1579. } else if (!bnx2fc_link_ok(hba->ctlr.lp))
  1580. fcoe_ctlr_link_up(&hba->ctlr);
  1581. nodev:
  1582. mutex_unlock(&bnx2fc_dev_lock);
  1583. rtnl_unlock();
  1584. return rc;
  1585. }
  1586. /**
  1587. * bnx2fc_create - Create bnx2fc FCoE interface
  1588. *
  1589. * @buffer: The name of Ethernet interface to create on
  1590. * @kp: The associated kernel param
  1591. *
  1592. * Called from sysfs.
  1593. *
  1594. * Returns: 0 for success
  1595. */
  1596. static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
  1597. {
  1598. struct bnx2fc_hba *hba;
  1599. struct net_device *phys_dev;
  1600. struct fc_lport *lport;
  1601. struct ethtool_drvinfo drvinfo;
  1602. int rc = 0;
  1603. int vlan_id;
  1604. BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
  1605. if (fip_mode != FIP_MODE_FABRIC) {
  1606. printk(KERN_ERR "fip mode not FABRIC\n");
  1607. return -EIO;
  1608. }
  1609. rtnl_lock();
  1610. mutex_lock(&bnx2fc_dev_lock);
  1611. if (!try_module_get(THIS_MODULE)) {
  1612. rc = -EINVAL;
  1613. goto mod_err;
  1614. }
  1615. /* obtain physical netdev */
  1616. if (netdev->priv_flags & IFF_802_1Q_VLAN) {
  1617. phys_dev = vlan_dev_real_dev(netdev);
  1618. vlan_id = vlan_dev_vlan_id(netdev);
  1619. } else {
  1620. printk(KERN_ERR PFX "Not a vlan device\n");
  1621. rc = -EINVAL;
  1622. goto netdev_err;
  1623. }
  1624. /* verify if the physical device is a netxtreme2 device */
  1625. if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
  1626. memset(&drvinfo, 0, sizeof(drvinfo));
  1627. phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
  1628. if (strcmp(drvinfo.driver, "bnx2x")) {
  1629. printk(KERN_ERR PFX "Not a netxtreme2 device\n");
  1630. rc = -EINVAL;
  1631. goto netdev_err;
  1632. }
  1633. } else {
  1634. printk(KERN_ERR PFX "unable to obtain drv_info\n");
  1635. rc = -EINVAL;
  1636. goto netdev_err;
  1637. }
  1638. /* obtain hba and initialize rest of the structure */
  1639. hba = bnx2fc_hba_lookup(phys_dev);
  1640. if (!hba) {
  1641. rc = -ENODEV;
  1642. printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
  1643. goto netdev_err;
  1644. }
  1645. if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  1646. rc = bnx2fc_fw_init(hba);
  1647. if (rc)
  1648. goto netdev_err;
  1649. }
  1650. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1651. rc = -EEXIST;
  1652. goto netdev_err;
  1653. }
  1654. /* update netdev with vlan netdev */
  1655. hba->netdev = netdev;
  1656. hba->vlan_id = vlan_id;
  1657. hba->vlan_enabled = 1;
  1658. rc = bnx2fc_interface_setup(hba, fip_mode);
  1659. if (rc) {
  1660. printk(KERN_ERR PFX "bnx2fc_interface_setup failed\n");
  1661. goto ifput_err;
  1662. }
  1663. hba->timer_work_queue =
  1664. create_singlethread_workqueue("bnx2fc_timer_wq");
  1665. if (!hba->timer_work_queue) {
  1666. printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
  1667. rc = -EINVAL;
  1668. goto ifput_err;
  1669. }
  1670. lport = bnx2fc_if_create(hba, &hba->pcidev->dev, 0);
  1671. if (!lport) {
  1672. printk(KERN_ERR PFX "Failed to create interface (%s)\n",
  1673. netdev->name);
  1674. bnx2fc_netdev_cleanup(hba);
  1675. rc = -EINVAL;
  1676. goto if_create_err;
  1677. }
  1678. lport->boot_time = jiffies;
  1679. /* Make this master N_port */
  1680. hba->ctlr.lp = lport;
  1681. set_bit(BNX2FC_CREATE_DONE, &hba->init_done);
  1682. printk(KERN_ERR PFX "create: START DISC\n");
  1683. bnx2fc_start_disc(hba);
  1684. /*
  1685. * Release from kref_init in bnx2fc_interface_setup, on success
  1686. * lport should be holding a reference taken in bnx2fc_if_create
  1687. */
  1688. bnx2fc_interface_put(hba);
  1689. /* put netdev that was held while calling dev_get_by_name */
  1690. mutex_unlock(&bnx2fc_dev_lock);
  1691. rtnl_unlock();
  1692. return 0;
  1693. if_create_err:
  1694. destroy_workqueue(hba->timer_work_queue);
  1695. ifput_err:
  1696. bnx2fc_interface_put(hba);
  1697. netdev_err:
  1698. module_put(THIS_MODULE);
  1699. mod_err:
  1700. mutex_unlock(&bnx2fc_dev_lock);
  1701. rtnl_unlock();
  1702. return rc;
  1703. }
  1704. /**
  1705. * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc adapter instance
  1706. *
  1707. * @cnic: Pointer to cnic device instance
  1708. *
  1709. **/
  1710. static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
  1711. {
  1712. struct list_head *list;
  1713. struct list_head *temp;
  1714. struct bnx2fc_hba *hba;
  1715. /* Called with bnx2fc_dev_lock held */
  1716. list_for_each_safe(list, temp, &adapter_list) {
  1717. hba = (struct bnx2fc_hba *)list;
  1718. if (hba->cnic == cnic)
  1719. return hba;
  1720. }
  1721. return NULL;
  1722. }
  1723. static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev)
  1724. {
  1725. struct list_head *list;
  1726. struct list_head *temp;
  1727. struct bnx2fc_hba *hba;
  1728. /* Called with bnx2fc_dev_lock held */
  1729. list_for_each_safe(list, temp, &adapter_list) {
  1730. hba = (struct bnx2fc_hba *)list;
  1731. if (hba->phys_dev == phys_dev)
  1732. return hba;
  1733. }
  1734. printk(KERN_ERR PFX "hba_lookup: hba NULL\n");
  1735. return NULL;
  1736. }
  1737. /**
  1738. * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
  1739. *
  1740. * @dev cnic device handle
  1741. */
  1742. static void bnx2fc_ulp_exit(struct cnic_dev *dev)
  1743. {
  1744. struct bnx2fc_hba *hba;
  1745. BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
  1746. if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  1747. printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
  1748. dev->netdev->name, dev->flags);
  1749. return;
  1750. }
  1751. mutex_lock(&bnx2fc_dev_lock);
  1752. hba = bnx2fc_find_hba_for_cnic(dev);
  1753. if (!hba) {
  1754. printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
  1755. dev);
  1756. mutex_unlock(&bnx2fc_dev_lock);
  1757. return;
  1758. }
  1759. list_del_init(&hba->link);
  1760. adapter_count--;
  1761. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1762. /* destroy not called yet, move to quiesced list */
  1763. bnx2fc_netdev_cleanup(hba);
  1764. bnx2fc_if_destroy(hba->ctlr.lp);
  1765. }
  1766. mutex_unlock(&bnx2fc_dev_lock);
  1767. bnx2fc_ulp_stop(hba);
  1768. /* unregister cnic device */
  1769. if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
  1770. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
  1771. bnx2fc_interface_destroy(hba);
  1772. }
  1773. /**
  1774. * bnx2fc_fcoe_reset - Resets the fcoe
  1775. *
  1776. * @shost: shost the reset is from
  1777. *
  1778. * Returns: always 0
  1779. */
  1780. static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
  1781. {
  1782. struct fc_lport *lport = shost_priv(shost);
  1783. fc_lport_reset(lport);
  1784. return 0;
  1785. }
  1786. static bool bnx2fc_match(struct net_device *netdev)
  1787. {
  1788. mutex_lock(&bnx2fc_dev_lock);
  1789. if (netdev->priv_flags & IFF_802_1Q_VLAN) {
  1790. struct net_device *phys_dev = vlan_dev_real_dev(netdev);
  1791. if (bnx2fc_hba_lookup(phys_dev)) {
  1792. mutex_unlock(&bnx2fc_dev_lock);
  1793. return true;
  1794. }
  1795. }
  1796. mutex_unlock(&bnx2fc_dev_lock);
  1797. return false;
  1798. }
  1799. static struct fcoe_transport bnx2fc_transport = {
  1800. .name = {"bnx2fc"},
  1801. .attached = false,
  1802. .list = LIST_HEAD_INIT(bnx2fc_transport.list),
  1803. .match = bnx2fc_match,
  1804. .create = bnx2fc_create,
  1805. .destroy = bnx2fc_destroy,
  1806. .enable = bnx2fc_enable,
  1807. .disable = bnx2fc_disable,
  1808. };
  1809. /**
  1810. * bnx2fc_percpu_thread_create - Create a receive thread for an
  1811. * online CPU
  1812. *
  1813. * @cpu: cpu index for the online cpu
  1814. */
  1815. static void bnx2fc_percpu_thread_create(unsigned int cpu)
  1816. {
  1817. struct bnx2fc_percpu_s *p;
  1818. struct task_struct *thread;
  1819. p = &per_cpu(bnx2fc_percpu, cpu);
  1820. thread = kthread_create(bnx2fc_percpu_io_thread,
  1821. (void *)p,
  1822. "bnx2fc_thread/%d", cpu);
  1823. /* bind thread to the cpu */
  1824. if (likely(!IS_ERR(p->iothread))) {
  1825. kthread_bind(thread, cpu);
  1826. p->iothread = thread;
  1827. wake_up_process(thread);
  1828. }
  1829. }
  1830. static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
  1831. {
  1832. struct bnx2fc_percpu_s *p;
  1833. struct task_struct *thread;
  1834. struct bnx2fc_work *work, *tmp;
  1835. LIST_HEAD(work_list);
  1836. BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
  1837. /* Prevent any new work from being queued for this CPU */
  1838. p = &per_cpu(bnx2fc_percpu, cpu);
  1839. spin_lock_bh(&p->fp_work_lock);
  1840. thread = p->iothread;
  1841. p->iothread = NULL;
  1842. /* Free all work in the list */
  1843. list_for_each_entry_safe(work, tmp, &work_list, list) {
  1844. list_del_init(&work->list);
  1845. bnx2fc_process_cq_compl(work->tgt, work->wqe);
  1846. kfree(work);
  1847. }
  1848. spin_unlock_bh(&p->fp_work_lock);
  1849. if (thread)
  1850. kthread_stop(thread);
  1851. }
  1852. /**
  1853. * bnx2fc_cpu_callback - Handler for CPU hotplug events
  1854. *
  1855. * @nfb: The callback data block
  1856. * @action: The event triggering the callback
  1857. * @hcpu: The index of the CPU that the event is for
  1858. *
  1859. * This creates or destroys per-CPU data for fcoe
  1860. *
  1861. * Returns NOTIFY_OK always.
  1862. */
  1863. static int bnx2fc_cpu_callback(struct notifier_block *nfb,
  1864. unsigned long action, void *hcpu)
  1865. {
  1866. unsigned cpu = (unsigned long)hcpu;
  1867. switch (action) {
  1868. case CPU_ONLINE:
  1869. case CPU_ONLINE_FROZEN:
  1870. printk(PFX "CPU %x online: Create Rx thread\n", cpu);
  1871. bnx2fc_percpu_thread_create(cpu);
  1872. break;
  1873. case CPU_DEAD:
  1874. case CPU_DEAD_FROZEN:
  1875. printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
  1876. bnx2fc_percpu_thread_destroy(cpu);
  1877. break;
  1878. default:
  1879. break;
  1880. }
  1881. return NOTIFY_OK;
  1882. }
  1883. /**
  1884. * bnx2fc_mod_init - module init entry point
  1885. *
  1886. * Initialize driver wide global data structures, and register
  1887. * with cnic module
  1888. **/
  1889. static int __init bnx2fc_mod_init(void)
  1890. {
  1891. struct fcoe_percpu_s *bg;
  1892. struct task_struct *l2_thread;
  1893. int rc = 0;
  1894. unsigned int cpu = 0;
  1895. struct bnx2fc_percpu_s *p;
  1896. printk(KERN_INFO PFX "%s", version);
  1897. /* register as a fcoe transport */
  1898. rc = fcoe_transport_attach(&bnx2fc_transport);
  1899. if (rc) {
  1900. printk(KERN_ERR "failed to register an fcoe transport, check "
  1901. "if libfcoe is loaded\n");
  1902. goto out;
  1903. }
  1904. INIT_LIST_HEAD(&adapter_list);
  1905. mutex_init(&bnx2fc_dev_lock);
  1906. adapter_count = 0;
  1907. /* Attach FC transport template */
  1908. rc = bnx2fc_attach_transport();
  1909. if (rc)
  1910. goto detach_ft;
  1911. bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
  1912. if (!bnx2fc_wq) {
  1913. rc = -ENOMEM;
  1914. goto release_bt;
  1915. }
  1916. bg = &bnx2fc_global;
  1917. skb_queue_head_init(&bg->fcoe_rx_list);
  1918. l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
  1919. (void *)bg,
  1920. "bnx2fc_l2_thread");
  1921. if (IS_ERR(l2_thread)) {
  1922. rc = PTR_ERR(l2_thread);
  1923. goto free_wq;
  1924. }
  1925. wake_up_process(l2_thread);
  1926. spin_lock_bh(&bg->fcoe_rx_list.lock);
  1927. bg->thread = l2_thread;
  1928. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  1929. for_each_possible_cpu(cpu) {
  1930. p = &per_cpu(bnx2fc_percpu, cpu);
  1931. INIT_LIST_HEAD(&p->work_list);
  1932. spin_lock_init(&p->fp_work_lock);
  1933. }
  1934. for_each_online_cpu(cpu) {
  1935. bnx2fc_percpu_thread_create(cpu);
  1936. }
  1937. /* Initialize per CPU interrupt thread */
  1938. register_hotcpu_notifier(&bnx2fc_cpu_notifier);
  1939. cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
  1940. return 0;
  1941. free_wq:
  1942. destroy_workqueue(bnx2fc_wq);
  1943. release_bt:
  1944. bnx2fc_release_transport();
  1945. detach_ft:
  1946. fcoe_transport_detach(&bnx2fc_transport);
  1947. out:
  1948. return rc;
  1949. }
  1950. static void __exit bnx2fc_mod_exit(void)
  1951. {
  1952. LIST_HEAD(to_be_deleted);
  1953. struct bnx2fc_hba *hba, *next;
  1954. struct fcoe_percpu_s *bg;
  1955. struct task_struct *l2_thread;
  1956. struct sk_buff *skb;
  1957. unsigned int cpu = 0;
  1958. /*
  1959. * NOTE: Since cnic calls register_driver routine rtnl_lock,
  1960. * it will have higher precedence than bnx2fc_dev_lock.
  1961. * unregister_device() cannot be called with bnx2fc_dev_lock
  1962. * held.
  1963. */
  1964. mutex_lock(&bnx2fc_dev_lock);
  1965. list_splice(&adapter_list, &to_be_deleted);
  1966. INIT_LIST_HEAD(&adapter_list);
  1967. adapter_count = 0;
  1968. mutex_unlock(&bnx2fc_dev_lock);
  1969. /* Unregister with cnic */
  1970. list_for_each_entry_safe(hba, next, &to_be_deleted, link) {
  1971. list_del_init(&hba->link);
  1972. printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p, kref = %d\n",
  1973. hba, atomic_read(&hba->kref.refcount));
  1974. bnx2fc_ulp_stop(hba);
  1975. /* unregister cnic device */
  1976. if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
  1977. &hba->reg_with_cnic))
  1978. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
  1979. bnx2fc_interface_destroy(hba);
  1980. }
  1981. cnic_unregister_driver(CNIC_ULP_FCOE);
  1982. /* Destroy global thread */
  1983. bg = &bnx2fc_global;
  1984. spin_lock_bh(&bg->fcoe_rx_list.lock);
  1985. l2_thread = bg->thread;
  1986. bg->thread = NULL;
  1987. while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
  1988. kfree_skb(skb);
  1989. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  1990. if (l2_thread)
  1991. kthread_stop(l2_thread);
  1992. unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
  1993. /* Destroy per cpu threads */
  1994. for_each_online_cpu(cpu) {
  1995. bnx2fc_percpu_thread_destroy(cpu);
  1996. }
  1997. destroy_workqueue(bnx2fc_wq);
  1998. /*
  1999. * detach from scsi transport
  2000. * must happen after all destroys are done
  2001. */
  2002. bnx2fc_release_transport();
  2003. /* detach from fcoe transport */
  2004. fcoe_transport_detach(&bnx2fc_transport);
  2005. }
  2006. module_init(bnx2fc_mod_init);
  2007. module_exit(bnx2fc_mod_exit);
  2008. static struct fc_function_template bnx2fc_transport_function = {
  2009. .show_host_node_name = 1,
  2010. .show_host_port_name = 1,
  2011. .show_host_supported_classes = 1,
  2012. .show_host_supported_fc4s = 1,
  2013. .show_host_active_fc4s = 1,
  2014. .show_host_maxframe_size = 1,
  2015. .show_host_port_id = 1,
  2016. .show_host_supported_speeds = 1,
  2017. .get_host_speed = fc_get_host_speed,
  2018. .show_host_speed = 1,
  2019. .show_host_port_type = 1,
  2020. .get_host_port_state = fc_get_host_port_state,
  2021. .show_host_port_state = 1,
  2022. .show_host_symbolic_name = 1,
  2023. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  2024. sizeof(struct bnx2fc_rport)),
  2025. .show_rport_maxframe_size = 1,
  2026. .show_rport_supported_classes = 1,
  2027. .show_host_fabric_name = 1,
  2028. .show_starget_node_name = 1,
  2029. .show_starget_port_name = 1,
  2030. .show_starget_port_id = 1,
  2031. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  2032. .show_rport_dev_loss_tmo = 1,
  2033. .get_fc_host_stats = bnx2fc_get_host_stats,
  2034. .issue_fc_host_lip = bnx2fc_fcoe_reset,
  2035. .terminate_rport_io = fc_rport_terminate_io,
  2036. .vport_create = bnx2fc_vport_create,
  2037. .vport_delete = bnx2fc_vport_destroy,
  2038. .vport_disable = bnx2fc_vport_disable,
  2039. };
  2040. static struct fc_function_template bnx2fc_vport_xport_function = {
  2041. .show_host_node_name = 1,
  2042. .show_host_port_name = 1,
  2043. .show_host_supported_classes = 1,
  2044. .show_host_supported_fc4s = 1,
  2045. .show_host_active_fc4s = 1,
  2046. .show_host_maxframe_size = 1,
  2047. .show_host_port_id = 1,
  2048. .show_host_supported_speeds = 1,
  2049. .get_host_speed = fc_get_host_speed,
  2050. .show_host_speed = 1,
  2051. .show_host_port_type = 1,
  2052. .get_host_port_state = fc_get_host_port_state,
  2053. .show_host_port_state = 1,
  2054. .show_host_symbolic_name = 1,
  2055. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  2056. sizeof(struct bnx2fc_rport)),
  2057. .show_rport_maxframe_size = 1,
  2058. .show_rport_supported_classes = 1,
  2059. .show_host_fabric_name = 1,
  2060. .show_starget_node_name = 1,
  2061. .show_starget_port_name = 1,
  2062. .show_starget_port_id = 1,
  2063. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  2064. .show_rport_dev_loss_tmo = 1,
  2065. .get_fc_host_stats = fc_get_host_stats,
  2066. .issue_fc_host_lip = bnx2fc_fcoe_reset,
  2067. .terminate_rport_io = fc_rport_terminate_io,
  2068. };
  2069. /**
  2070. * scsi_host_template structure used while registering with SCSI-ml
  2071. */
  2072. static struct scsi_host_template bnx2fc_shost_template = {
  2073. .module = THIS_MODULE,
  2074. .name = "Broadcom Offload FCoE Initiator",
  2075. .queuecommand = bnx2fc_queuecommand,
  2076. .eh_abort_handler = bnx2fc_eh_abort, /* abts */
  2077. .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
  2078. .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
  2079. .eh_host_reset_handler = fc_eh_host_reset,
  2080. .slave_alloc = fc_slave_alloc,
  2081. .change_queue_depth = fc_change_queue_depth,
  2082. .change_queue_type = fc_change_queue_type,
  2083. .this_id = -1,
  2084. .cmd_per_lun = 3,
  2085. .can_queue = BNX2FC_CAN_QUEUE,
  2086. .use_clustering = ENABLE_CLUSTERING,
  2087. .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
  2088. .max_sectors = 512,
  2089. };
  2090. static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
  2091. .frame_send = bnx2fc_xmit,
  2092. .elsct_send = bnx2fc_elsct_send,
  2093. .fcp_abort_io = bnx2fc_abort_io,
  2094. .fcp_cleanup = bnx2fc_cleanup,
  2095. .rport_event_callback = bnx2fc_rport_event_handler,
  2096. };
  2097. /**
  2098. * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
  2099. * structure carrying callback function pointers
  2100. */
  2101. static struct cnic_ulp_ops bnx2fc_cnic_cb = {
  2102. .owner = THIS_MODULE,
  2103. .cnic_init = bnx2fc_ulp_init,
  2104. .cnic_exit = bnx2fc_ulp_exit,
  2105. .cnic_start = bnx2fc_ulp_start,
  2106. .cnic_stop = bnx2fc_ulp_stop,
  2107. .indicate_kcqes = bnx2fc_indicate_kcqe,
  2108. .indicate_netevent = bnx2fc_indicate_netevent,
  2109. };