bnx2fc_fcoe.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  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 "Mar 17, 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_stat1.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_2500:
  593. lport->link_speed = FC_PORTSPEED_2GBIT;
  594. break;
  595. case SPEED_10000:
  596. lport->link_speed = FC_PORTSPEED_10GBIT;
  597. break;
  598. }
  599. }
  600. }
  601. static int bnx2fc_link_ok(struct fc_lport *lport)
  602. {
  603. struct fcoe_port *port = lport_priv(lport);
  604. struct bnx2fc_hba *hba = port->priv;
  605. struct net_device *dev = hba->phys_dev;
  606. int rc = 0;
  607. if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
  608. clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  609. else {
  610. set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  611. rc = -1;
  612. }
  613. return rc;
  614. }
  615. /**
  616. * bnx2fc_get_link_state - get network link state
  617. *
  618. * @hba: adapter instance pointer
  619. *
  620. * updates adapter structure flag based on netdev state
  621. */
  622. void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
  623. {
  624. if (test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state))
  625. set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  626. else
  627. clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
  628. }
  629. static int bnx2fc_net_config(struct fc_lport *lport)
  630. {
  631. struct bnx2fc_hba *hba;
  632. struct fcoe_port *port;
  633. u64 wwnn, wwpn;
  634. port = lport_priv(lport);
  635. hba = port->priv;
  636. /* require support for get_pauseparam ethtool op. */
  637. if (!hba->phys_dev->ethtool_ops ||
  638. !hba->phys_dev->ethtool_ops->get_pauseparam)
  639. return -EOPNOTSUPP;
  640. if (fc_set_mfs(lport, BNX2FC_MFS))
  641. return -EINVAL;
  642. skb_queue_head_init(&port->fcoe_pending_queue);
  643. port->fcoe_pending_queue_active = 0;
  644. setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
  645. bnx2fc_link_speed_update(lport);
  646. if (!lport->vport) {
  647. wwnn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 1, 0);
  648. BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
  649. fc_set_wwnn(lport, wwnn);
  650. wwpn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 2, 0);
  651. BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
  652. fc_set_wwpn(lport, wwpn);
  653. }
  654. return 0;
  655. }
  656. static void bnx2fc_destroy_timer(unsigned long data)
  657. {
  658. struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
  659. BNX2FC_HBA_DBG(hba->ctlr.lp, "ERROR:bnx2fc_destroy_timer - "
  660. "Destroy compl not received!!\n");
  661. hba->flags |= BNX2FC_FLAG_DESTROY_CMPL;
  662. wake_up_interruptible(&hba->destroy_wait);
  663. }
  664. /**
  665. * bnx2fc_indicate_netevent - Generic netdev event handler
  666. *
  667. * @context: adapter structure pointer
  668. * @event: event type
  669. *
  670. * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
  671. * NETDEV_CHANGE_MTU events
  672. */
  673. static void bnx2fc_indicate_netevent(void *context, unsigned long event)
  674. {
  675. struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
  676. struct fc_lport *lport = hba->ctlr.lp;
  677. struct fc_lport *vport;
  678. u32 link_possible = 1;
  679. if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  680. BNX2FC_MISC_DBG("driver not ready. event=%s %ld\n",
  681. hba->netdev->name, event);
  682. return;
  683. }
  684. /*
  685. * ASSUMPTION:
  686. * indicate_netevent cannot be called from cnic unless bnx2fc
  687. * does register_device
  688. */
  689. BUG_ON(!lport);
  690. BNX2FC_HBA_DBG(lport, "enter netevent handler - event=%s %ld\n",
  691. hba->netdev->name, event);
  692. switch (event) {
  693. case NETDEV_UP:
  694. BNX2FC_HBA_DBG(lport, "Port up, adapter_state = %ld\n",
  695. hba->adapter_state);
  696. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
  697. printk(KERN_ERR "indicate_netevent: "\
  698. "adapter is not UP!!\n");
  699. break;
  700. case NETDEV_DOWN:
  701. BNX2FC_HBA_DBG(lport, "Port down\n");
  702. clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  703. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  704. link_possible = 0;
  705. break;
  706. case NETDEV_GOING_DOWN:
  707. BNX2FC_HBA_DBG(lport, "Port going down\n");
  708. set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  709. link_possible = 0;
  710. break;
  711. case NETDEV_CHANGE:
  712. BNX2FC_HBA_DBG(lport, "NETDEV_CHANGE\n");
  713. break;
  714. default:
  715. printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
  716. return;
  717. }
  718. bnx2fc_link_speed_update(lport);
  719. if (link_possible && !bnx2fc_link_ok(lport)) {
  720. printk(KERN_ERR "indicate_netevent: call ctlr_link_up\n");
  721. fcoe_ctlr_link_up(&hba->ctlr);
  722. } else {
  723. printk(KERN_ERR "indicate_netevent: call ctlr_link_down\n");
  724. if (fcoe_ctlr_link_down(&hba->ctlr)) {
  725. clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
  726. mutex_lock(&lport->lp_mutex);
  727. list_for_each_entry(vport, &lport->vports, list)
  728. fc_host_port_type(vport->host) =
  729. FC_PORTTYPE_UNKNOWN;
  730. mutex_unlock(&lport->lp_mutex);
  731. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  732. per_cpu_ptr(lport->dev_stats,
  733. get_cpu())->LinkFailureCount++;
  734. put_cpu();
  735. fcoe_clean_pending_queue(lport);
  736. init_waitqueue_head(&hba->shutdown_wait);
  737. BNX2FC_HBA_DBG(lport, "indicate_netevent "
  738. "num_ofld_sess = %d\n",
  739. hba->num_ofld_sess);
  740. hba->wait_for_link_down = 1;
  741. BNX2FC_HBA_DBG(lport, "waiting for uploads to "
  742. "compl proc = %s\n",
  743. current->comm);
  744. wait_event_interruptible(hba->shutdown_wait,
  745. (hba->num_ofld_sess == 0));
  746. BNX2FC_HBA_DBG(lport, "wakeup - num_ofld_sess = %d\n",
  747. hba->num_ofld_sess);
  748. hba->wait_for_link_down = 0;
  749. if (signal_pending(current))
  750. flush_signals(current);
  751. }
  752. }
  753. }
  754. static int bnx2fc_libfc_config(struct fc_lport *lport)
  755. {
  756. /* Set the function pointers set by bnx2fc driver */
  757. memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
  758. sizeof(struct libfc_function_template));
  759. fc_elsct_init(lport);
  760. fc_exch_init(lport);
  761. fc_rport_init(lport);
  762. fc_disc_init(lport);
  763. return 0;
  764. }
  765. static int bnx2fc_em_config(struct fc_lport *lport)
  766. {
  767. struct fcoe_port *port = lport_priv(lport);
  768. struct bnx2fc_hba *hba = port->priv;
  769. if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
  770. FCOE_MAX_XID, NULL)) {
  771. printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
  772. return -ENOMEM;
  773. }
  774. hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
  775. BNX2FC_MAX_XID);
  776. if (!hba->cmd_mgr) {
  777. printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
  778. fc_exch_mgr_free(lport);
  779. return -ENOMEM;
  780. }
  781. return 0;
  782. }
  783. static int bnx2fc_lport_config(struct fc_lport *lport)
  784. {
  785. lport->link_up = 0;
  786. lport->qfull = 0;
  787. lport->max_retry_count = 3;
  788. lport->max_rport_retry_count = 3;
  789. lport->e_d_tov = 2 * 1000;
  790. lport->r_a_tov = 10 * 1000;
  791. /* REVISIT: enable when supporting tape devices
  792. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  793. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  794. */
  795. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS);
  796. lport->does_npiv = 1;
  797. memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
  798. lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
  799. /* alloc stats structure */
  800. if (fc_lport_init_stats(lport))
  801. return -ENOMEM;
  802. /* Finish fc_lport configuration */
  803. fc_lport_config(lport);
  804. return 0;
  805. }
  806. /**
  807. * bnx2fc_fip_recv - handle a received FIP frame.
  808. *
  809. * @skb: the received skb
  810. * @dev: associated &net_device
  811. * @ptype: the &packet_type structure which was used to register this handler.
  812. * @orig_dev: original receive &net_device, in case @ dev is a bond.
  813. *
  814. * Returns: 0 for success
  815. */
  816. static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
  817. struct packet_type *ptype,
  818. struct net_device *orig_dev)
  819. {
  820. struct bnx2fc_hba *hba;
  821. hba = container_of(ptype, struct bnx2fc_hba, fip_packet_type);
  822. fcoe_ctlr_recv(&hba->ctlr, skb);
  823. return 0;
  824. }
  825. /**
  826. * bnx2fc_update_src_mac - Update Ethernet MAC filters.
  827. *
  828. * @fip: FCoE controller.
  829. * @old: Unicast MAC address to delete if the MAC is non-zero.
  830. * @new: Unicast MAC address to add.
  831. *
  832. * Remove any previously-set unicast MAC filter.
  833. * Add secondary FCoE MAC address filter for our OUI.
  834. */
  835. static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
  836. {
  837. struct fcoe_port *port = lport_priv(lport);
  838. memcpy(port->data_src_addr, addr, ETH_ALEN);
  839. }
  840. /**
  841. * bnx2fc_get_src_mac - return the ethernet source address for an lport
  842. *
  843. * @lport: libfc port
  844. */
  845. static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
  846. {
  847. struct fcoe_port *port;
  848. port = (struct fcoe_port *)lport_priv(lport);
  849. return port->data_src_addr;
  850. }
  851. /**
  852. * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
  853. *
  854. * @fip: FCoE controller.
  855. * @skb: FIP Packet.
  856. */
  857. static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  858. {
  859. skb->dev = bnx2fc_from_ctlr(fip)->netdev;
  860. dev_queue_xmit(skb);
  861. }
  862. static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
  863. {
  864. struct Scsi_Host *shost = vport_to_shost(vport);
  865. struct fc_lport *n_port = shost_priv(shost);
  866. struct fcoe_port *port = lport_priv(n_port);
  867. struct bnx2fc_hba *hba = port->priv;
  868. struct net_device *netdev = hba->netdev;
  869. struct fc_lport *vn_port;
  870. if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  871. printk(KERN_ERR PFX "vn ports cannot be created on"
  872. "this hba\n");
  873. return -EIO;
  874. }
  875. mutex_lock(&bnx2fc_dev_lock);
  876. vn_port = bnx2fc_if_create(hba, &vport->dev, 1);
  877. mutex_unlock(&bnx2fc_dev_lock);
  878. if (IS_ERR(vn_port)) {
  879. printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
  880. netdev->name);
  881. return -EIO;
  882. }
  883. if (disabled) {
  884. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  885. } else {
  886. vn_port->boot_time = jiffies;
  887. fc_lport_init(vn_port);
  888. fc_fabric_login(vn_port);
  889. fc_vport_setlink(vn_port);
  890. }
  891. return 0;
  892. }
  893. static int bnx2fc_vport_destroy(struct fc_vport *vport)
  894. {
  895. struct Scsi_Host *shost = vport_to_shost(vport);
  896. struct fc_lport *n_port = shost_priv(shost);
  897. struct fc_lport *vn_port = vport->dd_data;
  898. struct fcoe_port *port = lport_priv(vn_port);
  899. mutex_lock(&n_port->lp_mutex);
  900. list_del(&vn_port->list);
  901. mutex_unlock(&n_port->lp_mutex);
  902. queue_work(bnx2fc_wq, &port->destroy_work);
  903. return 0;
  904. }
  905. static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
  906. {
  907. struct fc_lport *lport = vport->dd_data;
  908. if (disable) {
  909. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  910. fc_fabric_logoff(lport);
  911. } else {
  912. lport->boot_time = jiffies;
  913. fc_fabric_login(lport);
  914. fc_vport_setlink(lport);
  915. }
  916. return 0;
  917. }
  918. static int bnx2fc_netdev_setup(struct bnx2fc_hba *hba)
  919. {
  920. struct net_device *netdev = hba->netdev;
  921. struct net_device *physdev = hba->phys_dev;
  922. struct netdev_hw_addr *ha;
  923. int sel_san_mac = 0;
  924. /* setup Source MAC Address */
  925. rcu_read_lock();
  926. for_each_dev_addr(physdev, ha) {
  927. BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
  928. ha->type);
  929. printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
  930. ha->addr[1], ha->addr[2], ha->addr[3],
  931. ha->addr[4], ha->addr[5]);
  932. if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
  933. (is_valid_ether_addr(ha->addr))) {
  934. memcpy(hba->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
  935. sel_san_mac = 1;
  936. BNX2FC_MISC_DBG("Found SAN MAC\n");
  937. }
  938. }
  939. rcu_read_unlock();
  940. if (!sel_san_mac)
  941. return -ENODEV;
  942. hba->fip_packet_type.func = bnx2fc_fip_recv;
  943. hba->fip_packet_type.type = htons(ETH_P_FIP);
  944. hba->fip_packet_type.dev = netdev;
  945. dev_add_pack(&hba->fip_packet_type);
  946. hba->fcoe_packet_type.func = bnx2fc_rcv;
  947. hba->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  948. hba->fcoe_packet_type.dev = netdev;
  949. dev_add_pack(&hba->fcoe_packet_type);
  950. return 0;
  951. }
  952. static int bnx2fc_attach_transport(void)
  953. {
  954. bnx2fc_transport_template =
  955. fc_attach_transport(&bnx2fc_transport_function);
  956. if (bnx2fc_transport_template == NULL) {
  957. printk(KERN_ERR PFX "Failed to attach FC transport\n");
  958. return -ENODEV;
  959. }
  960. bnx2fc_vport_xport_template =
  961. fc_attach_transport(&bnx2fc_vport_xport_function);
  962. if (bnx2fc_vport_xport_template == NULL) {
  963. printk(KERN_ERR PFX
  964. "Failed to attach FC transport for vport\n");
  965. fc_release_transport(bnx2fc_transport_template);
  966. bnx2fc_transport_template = NULL;
  967. return -ENODEV;
  968. }
  969. return 0;
  970. }
  971. static void bnx2fc_release_transport(void)
  972. {
  973. fc_release_transport(bnx2fc_transport_template);
  974. fc_release_transport(bnx2fc_vport_xport_template);
  975. bnx2fc_transport_template = NULL;
  976. bnx2fc_vport_xport_template = NULL;
  977. }
  978. static void bnx2fc_interface_release(struct kref *kref)
  979. {
  980. struct bnx2fc_hba *hba;
  981. struct net_device *netdev;
  982. struct net_device *phys_dev;
  983. hba = container_of(kref, struct bnx2fc_hba, kref);
  984. BNX2FC_MISC_DBG("Interface is being released\n");
  985. netdev = hba->netdev;
  986. phys_dev = hba->phys_dev;
  987. /* tear-down FIP controller */
  988. if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done))
  989. fcoe_ctlr_destroy(&hba->ctlr);
  990. /* Free the command manager */
  991. if (hba->cmd_mgr) {
  992. bnx2fc_cmd_mgr_free(hba->cmd_mgr);
  993. hba->cmd_mgr = NULL;
  994. }
  995. dev_put(netdev);
  996. module_put(THIS_MODULE);
  997. }
  998. static inline void bnx2fc_interface_get(struct bnx2fc_hba *hba)
  999. {
  1000. kref_get(&hba->kref);
  1001. }
  1002. static inline void bnx2fc_interface_put(struct bnx2fc_hba *hba)
  1003. {
  1004. kref_put(&hba->kref, bnx2fc_interface_release);
  1005. }
  1006. static void bnx2fc_interface_destroy(struct bnx2fc_hba *hba)
  1007. {
  1008. bnx2fc_unbind_pcidev(hba);
  1009. kfree(hba);
  1010. }
  1011. /**
  1012. * bnx2fc_interface_create - create a new fcoe instance
  1013. *
  1014. * @cnic: pointer to cnic device
  1015. *
  1016. * Creates a new FCoE instance on the given device which include allocating
  1017. * hba structure, scsi_host and lport structures.
  1018. */
  1019. static struct bnx2fc_hba *bnx2fc_interface_create(struct cnic_dev *cnic)
  1020. {
  1021. struct bnx2fc_hba *hba;
  1022. int rc;
  1023. hba = kzalloc(sizeof(*hba), GFP_KERNEL);
  1024. if (!hba) {
  1025. printk(KERN_ERR PFX "Unable to allocate hba structure\n");
  1026. return NULL;
  1027. }
  1028. spin_lock_init(&hba->hba_lock);
  1029. mutex_init(&hba->hba_mutex);
  1030. hba->cnic = cnic;
  1031. rc = bnx2fc_bind_pcidev(hba);
  1032. if (rc)
  1033. goto bind_err;
  1034. hba->phys_dev = cnic->netdev;
  1035. /* will get overwritten after we do vlan discovery */
  1036. hba->netdev = hba->phys_dev;
  1037. init_waitqueue_head(&hba->shutdown_wait);
  1038. init_waitqueue_head(&hba->destroy_wait);
  1039. return hba;
  1040. bind_err:
  1041. printk(KERN_ERR PFX "create_interface: bind error\n");
  1042. kfree(hba);
  1043. return NULL;
  1044. }
  1045. static int bnx2fc_interface_setup(struct bnx2fc_hba *hba,
  1046. enum fip_state fip_mode)
  1047. {
  1048. int rc = 0;
  1049. struct net_device *netdev = hba->netdev;
  1050. struct fcoe_ctlr *fip = &hba->ctlr;
  1051. dev_hold(netdev);
  1052. kref_init(&hba->kref);
  1053. hba->flags = 0;
  1054. /* Initialize FIP */
  1055. memset(fip, 0, sizeof(*fip));
  1056. fcoe_ctlr_init(fip, fip_mode);
  1057. hba->ctlr.send = bnx2fc_fip_send;
  1058. hba->ctlr.update_mac = bnx2fc_update_src_mac;
  1059. hba->ctlr.get_src_addr = bnx2fc_get_src_mac;
  1060. set_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done);
  1061. rc = bnx2fc_netdev_setup(hba);
  1062. if (rc)
  1063. goto setup_err;
  1064. hba->next_conn_id = 0;
  1065. memset(hba->tgt_ofld_list, 0, sizeof(hba->tgt_ofld_list));
  1066. hba->num_ofld_sess = 0;
  1067. return 0;
  1068. setup_err:
  1069. fcoe_ctlr_destroy(&hba->ctlr);
  1070. dev_put(netdev);
  1071. bnx2fc_interface_put(hba);
  1072. return rc;
  1073. }
  1074. /**
  1075. * bnx2fc_if_create - Create FCoE instance on a given interface
  1076. *
  1077. * @hba: FCoE interface to create a local port on
  1078. * @parent: Device pointer to be the parent in sysfs for the SCSI host
  1079. * @npiv: Indicates if the port is vport or not
  1080. *
  1081. * Creates a fc_lport instance and a Scsi_Host instance and configure them.
  1082. *
  1083. * Returns: Allocated fc_lport or an error pointer
  1084. */
  1085. static struct fc_lport *bnx2fc_if_create(struct bnx2fc_hba *hba,
  1086. struct device *parent, int npiv)
  1087. {
  1088. struct fc_lport *lport, *n_port;
  1089. struct fcoe_port *port;
  1090. struct Scsi_Host *shost;
  1091. struct fc_vport *vport = dev_to_vport(parent);
  1092. int rc = 0;
  1093. /* Allocate Scsi_Host structure */
  1094. if (!npiv)
  1095. lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
  1096. else
  1097. lport = libfc_vport_create(vport, sizeof(*port));
  1098. if (!lport) {
  1099. printk(KERN_ERR PFX "could not allocate scsi host structure\n");
  1100. return NULL;
  1101. }
  1102. shost = lport->host;
  1103. port = lport_priv(lport);
  1104. port->lport = lport;
  1105. port->priv = hba;
  1106. INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
  1107. /* Configure fcoe_port */
  1108. rc = bnx2fc_lport_config(lport);
  1109. if (rc)
  1110. goto lp_config_err;
  1111. if (npiv) {
  1112. printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
  1113. vport->node_name, vport->port_name);
  1114. fc_set_wwnn(lport, vport->node_name);
  1115. fc_set_wwpn(lport, vport->port_name);
  1116. }
  1117. /* Configure netdev and networking properties of the lport */
  1118. rc = bnx2fc_net_config(lport);
  1119. if (rc) {
  1120. printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
  1121. goto lp_config_err;
  1122. }
  1123. rc = bnx2fc_shost_config(lport, parent);
  1124. if (rc) {
  1125. printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
  1126. hba->netdev->name);
  1127. goto lp_config_err;
  1128. }
  1129. /* Initialize the libfc library */
  1130. rc = bnx2fc_libfc_config(lport);
  1131. if (rc) {
  1132. printk(KERN_ERR PFX "Couldnt configure libfc\n");
  1133. goto shost_err;
  1134. }
  1135. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  1136. /* Allocate exchange manager */
  1137. if (!npiv)
  1138. rc = bnx2fc_em_config(lport);
  1139. else {
  1140. shost = vport_to_shost(vport);
  1141. n_port = shost_priv(shost);
  1142. rc = fc_exch_mgr_list_clone(n_port, lport);
  1143. }
  1144. if (rc) {
  1145. printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
  1146. goto shost_err;
  1147. }
  1148. bnx2fc_interface_get(hba);
  1149. return lport;
  1150. shost_err:
  1151. scsi_remove_host(shost);
  1152. lp_config_err:
  1153. scsi_host_put(lport->host);
  1154. return NULL;
  1155. }
  1156. static void bnx2fc_netdev_cleanup(struct bnx2fc_hba *hba)
  1157. {
  1158. /* Dont listen for Ethernet packets anymore */
  1159. __dev_remove_pack(&hba->fcoe_packet_type);
  1160. __dev_remove_pack(&hba->fip_packet_type);
  1161. synchronize_net();
  1162. }
  1163. static void bnx2fc_if_destroy(struct fc_lport *lport)
  1164. {
  1165. struct fcoe_port *port = lport_priv(lport);
  1166. struct bnx2fc_hba *hba = port->priv;
  1167. BNX2FC_HBA_DBG(hba->ctlr.lp, "ENTERED bnx2fc_if_destroy\n");
  1168. /* Stop the transmit retry timer */
  1169. del_timer_sync(&port->timer);
  1170. /* Free existing transmit skbs */
  1171. fcoe_clean_pending_queue(lport);
  1172. /* Free queued packets for the receive thread */
  1173. bnx2fc_clean_rx_queue(lport);
  1174. /* Detach from scsi-ml */
  1175. fc_remove_host(lport->host);
  1176. scsi_remove_host(lport->host);
  1177. /*
  1178. * Note that only the physical lport will have the exchange manager.
  1179. * for vports, this function is NOP
  1180. */
  1181. fc_exch_mgr_free(lport);
  1182. /* Free memory used by statistical counters */
  1183. fc_lport_free_stats(lport);
  1184. /* Release Scsi_Host */
  1185. scsi_host_put(lport->host);
  1186. bnx2fc_interface_put(hba);
  1187. }
  1188. /**
  1189. * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
  1190. *
  1191. * @buffer: The name of the Ethernet interface to be destroyed
  1192. * @kp: The associated kernel parameter
  1193. *
  1194. * Called from sysfs.
  1195. *
  1196. * Returns: 0 for success
  1197. */
  1198. static int bnx2fc_destroy(struct net_device *netdev)
  1199. {
  1200. struct bnx2fc_hba *hba = NULL;
  1201. struct net_device *phys_dev;
  1202. int rc = 0;
  1203. rtnl_lock();
  1204. mutex_lock(&bnx2fc_dev_lock);
  1205. /* obtain physical netdev */
  1206. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1207. phys_dev = vlan_dev_real_dev(netdev);
  1208. else {
  1209. printk(KERN_ERR PFX "Not a vlan device\n");
  1210. rc = -ENODEV;
  1211. goto netdev_err;
  1212. }
  1213. hba = bnx2fc_hba_lookup(phys_dev);
  1214. if (!hba || !hba->ctlr.lp) {
  1215. rc = -ENODEV;
  1216. printk(KERN_ERR PFX "bnx2fc_destroy: hba or lport not found\n");
  1217. goto netdev_err;
  1218. }
  1219. if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1220. printk(KERN_ERR PFX "bnx2fc_destroy: Create not called\n");
  1221. goto netdev_err;
  1222. }
  1223. bnx2fc_netdev_cleanup(hba);
  1224. bnx2fc_stop(hba);
  1225. bnx2fc_if_destroy(hba->ctlr.lp);
  1226. destroy_workqueue(hba->timer_work_queue);
  1227. if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
  1228. bnx2fc_fw_destroy(hba);
  1229. clear_bit(BNX2FC_CREATE_DONE, &hba->init_done);
  1230. netdev_err:
  1231. mutex_unlock(&bnx2fc_dev_lock);
  1232. rtnl_unlock();
  1233. return rc;
  1234. }
  1235. static void bnx2fc_destroy_work(struct work_struct *work)
  1236. {
  1237. struct fcoe_port *port;
  1238. struct fc_lport *lport;
  1239. port = container_of(work, struct fcoe_port, destroy_work);
  1240. lport = port->lport;
  1241. BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
  1242. bnx2fc_port_shutdown(lport);
  1243. rtnl_lock();
  1244. mutex_lock(&bnx2fc_dev_lock);
  1245. bnx2fc_if_destroy(lport);
  1246. mutex_unlock(&bnx2fc_dev_lock);
  1247. rtnl_unlock();
  1248. }
  1249. static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
  1250. {
  1251. bnx2fc_free_fw_resc(hba);
  1252. bnx2fc_free_task_ctx(hba);
  1253. }
  1254. /**
  1255. * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
  1256. * pci structure
  1257. *
  1258. * @hba: Adapter instance
  1259. */
  1260. static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
  1261. {
  1262. if (bnx2fc_setup_task_ctx(hba))
  1263. goto mem_err;
  1264. if (bnx2fc_setup_fw_resc(hba))
  1265. goto mem_err;
  1266. return 0;
  1267. mem_err:
  1268. bnx2fc_unbind_adapter_devices(hba);
  1269. return -ENOMEM;
  1270. }
  1271. static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
  1272. {
  1273. struct cnic_dev *cnic;
  1274. if (!hba->cnic) {
  1275. printk(KERN_ERR PFX "cnic is NULL\n");
  1276. return -ENODEV;
  1277. }
  1278. cnic = hba->cnic;
  1279. hba->pcidev = cnic->pcidev;
  1280. if (hba->pcidev)
  1281. pci_dev_get(hba->pcidev);
  1282. return 0;
  1283. }
  1284. static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
  1285. {
  1286. if (hba->pcidev)
  1287. pci_dev_put(hba->pcidev);
  1288. hba->pcidev = NULL;
  1289. }
  1290. /**
  1291. * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
  1292. *
  1293. * @handle: transport handle pointing to adapter struture
  1294. *
  1295. * This function maps adapter structure to pcidev structure and initiates
  1296. * firmware handshake to enable/initialize on-chip FCoE components.
  1297. * This bnx2fc - cnic interface api callback is used after following
  1298. * conditions are met -
  1299. * a) underlying network interface is up (marked by event NETDEV_UP
  1300. * from netdev
  1301. * b) bnx2fc adatper structure is registered.
  1302. */
  1303. static void bnx2fc_ulp_start(void *handle)
  1304. {
  1305. struct bnx2fc_hba *hba = handle;
  1306. struct fc_lport *lport = hba->ctlr.lp;
  1307. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1308. mutex_lock(&bnx2fc_dev_lock);
  1309. if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
  1310. goto start_disc;
  1311. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done))
  1312. bnx2fc_fw_init(hba);
  1313. start_disc:
  1314. mutex_unlock(&bnx2fc_dev_lock);
  1315. BNX2FC_MISC_DBG("bnx2fc started.\n");
  1316. /* Kick off Fabric discovery*/
  1317. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1318. printk(KERN_ERR PFX "ulp_init: start discovery\n");
  1319. lport->tt.frame_send = bnx2fc_xmit;
  1320. bnx2fc_start_disc(hba);
  1321. }
  1322. }
  1323. static void bnx2fc_port_shutdown(struct fc_lport *lport)
  1324. {
  1325. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1326. fc_fabric_logoff(lport);
  1327. fc_lport_destroy(lport);
  1328. }
  1329. static void bnx2fc_stop(struct bnx2fc_hba *hba)
  1330. {
  1331. struct fc_lport *lport;
  1332. struct fc_lport *vport;
  1333. BNX2FC_MISC_DBG("ENTERED %s - init_done = %ld\n", __func__,
  1334. hba->init_done);
  1335. if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done) &&
  1336. test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1337. lport = hba->ctlr.lp;
  1338. bnx2fc_port_shutdown(lport);
  1339. BNX2FC_HBA_DBG(lport, "bnx2fc_stop: waiting for %d "
  1340. "offloaded sessions\n",
  1341. hba->num_ofld_sess);
  1342. wait_event_interruptible(hba->shutdown_wait,
  1343. (hba->num_ofld_sess == 0));
  1344. mutex_lock(&lport->lp_mutex);
  1345. list_for_each_entry(vport, &lport->vports, list)
  1346. fc_host_port_type(vport->host) = FC_PORTTYPE_UNKNOWN;
  1347. mutex_unlock(&lport->lp_mutex);
  1348. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  1349. fcoe_ctlr_link_down(&hba->ctlr);
  1350. fcoe_clean_pending_queue(lport);
  1351. mutex_lock(&hba->hba_mutex);
  1352. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  1353. clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
  1354. clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
  1355. mutex_unlock(&hba->hba_mutex);
  1356. }
  1357. }
  1358. static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
  1359. {
  1360. #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
  1361. int rc = -1;
  1362. int i = HZ;
  1363. rc = bnx2fc_bind_adapter_devices(hba);
  1364. if (rc) {
  1365. printk(KERN_ALERT PFX
  1366. "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
  1367. goto err_out;
  1368. }
  1369. rc = bnx2fc_send_fw_fcoe_init_msg(hba);
  1370. if (rc) {
  1371. printk(KERN_ALERT PFX
  1372. "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
  1373. goto err_unbind;
  1374. }
  1375. /*
  1376. * Wait until the adapter init message is complete, and adapter
  1377. * state is UP.
  1378. */
  1379. while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
  1380. msleep(BNX2FC_INIT_POLL_TIME);
  1381. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
  1382. printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
  1383. "Ignoring...\n",
  1384. hba->cnic->netdev->name);
  1385. rc = -1;
  1386. goto err_unbind;
  1387. }
  1388. /* Mark HBA to indicate that the FW INIT is done */
  1389. set_bit(BNX2FC_FW_INIT_DONE, &hba->init_done);
  1390. return 0;
  1391. err_unbind:
  1392. bnx2fc_unbind_adapter_devices(hba);
  1393. err_out:
  1394. return rc;
  1395. }
  1396. static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
  1397. {
  1398. if (test_and_clear_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  1399. if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
  1400. init_timer(&hba->destroy_timer);
  1401. hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
  1402. jiffies;
  1403. hba->destroy_timer.function = bnx2fc_destroy_timer;
  1404. hba->destroy_timer.data = (unsigned long)hba;
  1405. add_timer(&hba->destroy_timer);
  1406. wait_event_interruptible(hba->destroy_wait,
  1407. (hba->flags &
  1408. BNX2FC_FLAG_DESTROY_CMPL));
  1409. /* This should never happen */
  1410. if (signal_pending(current))
  1411. flush_signals(current);
  1412. del_timer_sync(&hba->destroy_timer);
  1413. }
  1414. bnx2fc_unbind_adapter_devices(hba);
  1415. }
  1416. }
  1417. /**
  1418. * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
  1419. *
  1420. * @handle: transport handle pointing to adapter structure
  1421. *
  1422. * Driver checks if adapter is already in shutdown mode, if not start
  1423. * the shutdown process.
  1424. */
  1425. static void bnx2fc_ulp_stop(void *handle)
  1426. {
  1427. struct bnx2fc_hba *hba = (struct bnx2fc_hba *)handle;
  1428. printk(KERN_ERR "ULP_STOP\n");
  1429. mutex_lock(&bnx2fc_dev_lock);
  1430. bnx2fc_stop(hba);
  1431. bnx2fc_fw_destroy(hba);
  1432. mutex_unlock(&bnx2fc_dev_lock);
  1433. }
  1434. static void bnx2fc_start_disc(struct bnx2fc_hba *hba)
  1435. {
  1436. struct fc_lport *lport;
  1437. int wait_cnt = 0;
  1438. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1439. /* Kick off FIP/FLOGI */
  1440. if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  1441. printk(KERN_ERR PFX "Init not done yet\n");
  1442. return;
  1443. }
  1444. lport = hba->ctlr.lp;
  1445. BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
  1446. if (!bnx2fc_link_ok(lport)) {
  1447. BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
  1448. fcoe_ctlr_link_up(&hba->ctlr);
  1449. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1450. set_bit(ADAPTER_STATE_READY, &hba->adapter_state);
  1451. }
  1452. /* wait for the FCF to be selected before issuing FLOGI */
  1453. while (!hba->ctlr.sel_fcf) {
  1454. msleep(250);
  1455. /* give up after 3 secs */
  1456. if (++wait_cnt > 12)
  1457. break;
  1458. }
  1459. fc_lport_init(lport);
  1460. fc_fabric_login(lport);
  1461. }
  1462. /**
  1463. * bnx2fc_ulp_init - Initialize an adapter instance
  1464. *
  1465. * @dev : cnic device handle
  1466. * Called from cnic_register_driver() context to initialize all
  1467. * enumerated cnic devices. This routine allocates adapter structure
  1468. * and other device specific resources.
  1469. */
  1470. static void bnx2fc_ulp_init(struct cnic_dev *dev)
  1471. {
  1472. struct bnx2fc_hba *hba;
  1473. int rc = 0;
  1474. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1475. /* bnx2fc works only when bnx2x is loaded */
  1476. if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  1477. printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
  1478. " flags: %lx\n",
  1479. dev->netdev->name, dev->flags);
  1480. return;
  1481. }
  1482. /* Configure FCoE interface */
  1483. hba = bnx2fc_interface_create(dev);
  1484. if (!hba) {
  1485. printk(KERN_ERR PFX "hba initialization failed\n");
  1486. return;
  1487. }
  1488. /* Add HBA to the adapter list */
  1489. mutex_lock(&bnx2fc_dev_lock);
  1490. list_add_tail(&hba->link, &adapter_list);
  1491. adapter_count++;
  1492. mutex_unlock(&bnx2fc_dev_lock);
  1493. clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
  1494. rc = dev->register_device(dev, CNIC_ULP_FCOE,
  1495. (void *) hba);
  1496. if (rc)
  1497. printk(KERN_ALERT PFX "register_device failed, rc = %d\n", rc);
  1498. else
  1499. set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
  1500. }
  1501. static int bnx2fc_disable(struct net_device *netdev)
  1502. {
  1503. struct bnx2fc_hba *hba;
  1504. struct net_device *phys_dev;
  1505. struct ethtool_drvinfo drvinfo;
  1506. int rc = 0;
  1507. rtnl_lock();
  1508. mutex_lock(&bnx2fc_dev_lock);
  1509. /* obtain physical netdev */
  1510. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1511. phys_dev = vlan_dev_real_dev(netdev);
  1512. else {
  1513. printk(KERN_ERR PFX "Not a vlan device\n");
  1514. rc = -ENODEV;
  1515. goto nodev;
  1516. }
  1517. /* verify if the physical device is a netxtreme2 device */
  1518. if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
  1519. memset(&drvinfo, 0, sizeof(drvinfo));
  1520. phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
  1521. if (strcmp(drvinfo.driver, "bnx2x")) {
  1522. printk(KERN_ERR PFX "Not a netxtreme2 device\n");
  1523. rc = -ENODEV;
  1524. goto nodev;
  1525. }
  1526. } else {
  1527. printk(KERN_ERR PFX "unable to obtain drv_info\n");
  1528. rc = -ENODEV;
  1529. goto nodev;
  1530. }
  1531. printk(KERN_ERR PFX "phys_dev is netxtreme2 device\n");
  1532. /* obtain hba and initialize rest of the structure */
  1533. hba = bnx2fc_hba_lookup(phys_dev);
  1534. if (!hba || !hba->ctlr.lp) {
  1535. rc = -ENODEV;
  1536. printk(KERN_ERR PFX "bnx2fc_disable: hba or lport not found\n");
  1537. } else {
  1538. fcoe_ctlr_link_down(&hba->ctlr);
  1539. fcoe_clean_pending_queue(hba->ctlr.lp);
  1540. }
  1541. nodev:
  1542. mutex_unlock(&bnx2fc_dev_lock);
  1543. rtnl_unlock();
  1544. return rc;
  1545. }
  1546. static int bnx2fc_enable(struct net_device *netdev)
  1547. {
  1548. struct bnx2fc_hba *hba;
  1549. struct net_device *phys_dev;
  1550. struct ethtool_drvinfo drvinfo;
  1551. int rc = 0;
  1552. rtnl_lock();
  1553. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1554. mutex_lock(&bnx2fc_dev_lock);
  1555. /* obtain physical netdev */
  1556. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1557. phys_dev = vlan_dev_real_dev(netdev);
  1558. else {
  1559. printk(KERN_ERR PFX "Not a vlan device\n");
  1560. rc = -ENODEV;
  1561. goto nodev;
  1562. }
  1563. /* verify if the physical device is a netxtreme2 device */
  1564. if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
  1565. memset(&drvinfo, 0, sizeof(drvinfo));
  1566. phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
  1567. if (strcmp(drvinfo.driver, "bnx2x")) {
  1568. printk(KERN_ERR PFX "Not a netxtreme2 device\n");
  1569. rc = -ENODEV;
  1570. goto nodev;
  1571. }
  1572. } else {
  1573. printk(KERN_ERR PFX "unable to obtain drv_info\n");
  1574. rc = -ENODEV;
  1575. goto nodev;
  1576. }
  1577. /* obtain hba and initialize rest of the structure */
  1578. hba = bnx2fc_hba_lookup(phys_dev);
  1579. if (!hba || !hba->ctlr.lp) {
  1580. rc = -ENODEV;
  1581. printk(KERN_ERR PFX "bnx2fc_enable: hba or lport not found\n");
  1582. } else if (!bnx2fc_link_ok(hba->ctlr.lp))
  1583. fcoe_ctlr_link_up(&hba->ctlr);
  1584. nodev:
  1585. mutex_unlock(&bnx2fc_dev_lock);
  1586. rtnl_unlock();
  1587. return rc;
  1588. }
  1589. /**
  1590. * bnx2fc_create - Create bnx2fc FCoE interface
  1591. *
  1592. * @buffer: The name of Ethernet interface to create on
  1593. * @kp: The associated kernel param
  1594. *
  1595. * Called from sysfs.
  1596. *
  1597. * Returns: 0 for success
  1598. */
  1599. static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
  1600. {
  1601. struct bnx2fc_hba *hba;
  1602. struct net_device *phys_dev;
  1603. struct fc_lport *lport;
  1604. struct ethtool_drvinfo drvinfo;
  1605. int rc = 0;
  1606. int vlan_id;
  1607. BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
  1608. if (fip_mode != FIP_MODE_FABRIC) {
  1609. printk(KERN_ERR "fip mode not FABRIC\n");
  1610. return -EIO;
  1611. }
  1612. rtnl_lock();
  1613. mutex_lock(&bnx2fc_dev_lock);
  1614. if (!try_module_get(THIS_MODULE)) {
  1615. rc = -EINVAL;
  1616. goto mod_err;
  1617. }
  1618. /* obtain physical netdev */
  1619. if (netdev->priv_flags & IFF_802_1Q_VLAN) {
  1620. phys_dev = vlan_dev_real_dev(netdev);
  1621. vlan_id = vlan_dev_vlan_id(netdev);
  1622. } else {
  1623. printk(KERN_ERR PFX "Not a vlan device\n");
  1624. rc = -EINVAL;
  1625. goto netdev_err;
  1626. }
  1627. /* verify if the physical device is a netxtreme2 device */
  1628. if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
  1629. memset(&drvinfo, 0, sizeof(drvinfo));
  1630. phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
  1631. if (strcmp(drvinfo.driver, "bnx2x")) {
  1632. printk(KERN_ERR PFX "Not a netxtreme2 device\n");
  1633. rc = -EINVAL;
  1634. goto netdev_err;
  1635. }
  1636. } else {
  1637. printk(KERN_ERR PFX "unable to obtain drv_info\n");
  1638. rc = -EINVAL;
  1639. goto netdev_err;
  1640. }
  1641. /* obtain hba and initialize rest of the structure */
  1642. hba = bnx2fc_hba_lookup(phys_dev);
  1643. if (!hba) {
  1644. rc = -ENODEV;
  1645. printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
  1646. goto netdev_err;
  1647. }
  1648. if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
  1649. rc = bnx2fc_fw_init(hba);
  1650. if (rc)
  1651. goto netdev_err;
  1652. }
  1653. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1654. rc = -EEXIST;
  1655. goto netdev_err;
  1656. }
  1657. /* update netdev with vlan netdev */
  1658. hba->netdev = netdev;
  1659. hba->vlan_id = vlan_id;
  1660. hba->vlan_enabled = 1;
  1661. rc = bnx2fc_interface_setup(hba, fip_mode);
  1662. if (rc) {
  1663. printk(KERN_ERR PFX "bnx2fc_interface_setup failed\n");
  1664. goto ifput_err;
  1665. }
  1666. hba->timer_work_queue =
  1667. create_singlethread_workqueue("bnx2fc_timer_wq");
  1668. if (!hba->timer_work_queue) {
  1669. printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
  1670. rc = -EINVAL;
  1671. goto ifput_err;
  1672. }
  1673. lport = bnx2fc_if_create(hba, &hba->pcidev->dev, 0);
  1674. if (!lport) {
  1675. printk(KERN_ERR PFX "Failed to create interface (%s)\n",
  1676. netdev->name);
  1677. bnx2fc_netdev_cleanup(hba);
  1678. rc = -EINVAL;
  1679. goto if_create_err;
  1680. }
  1681. lport->boot_time = jiffies;
  1682. /* Make this master N_port */
  1683. hba->ctlr.lp = lport;
  1684. set_bit(BNX2FC_CREATE_DONE, &hba->init_done);
  1685. printk(KERN_ERR PFX "create: START DISC\n");
  1686. bnx2fc_start_disc(hba);
  1687. /*
  1688. * Release from kref_init in bnx2fc_interface_setup, on success
  1689. * lport should be holding a reference taken in bnx2fc_if_create
  1690. */
  1691. bnx2fc_interface_put(hba);
  1692. /* put netdev that was held while calling dev_get_by_name */
  1693. mutex_unlock(&bnx2fc_dev_lock);
  1694. rtnl_unlock();
  1695. return 0;
  1696. if_create_err:
  1697. destroy_workqueue(hba->timer_work_queue);
  1698. ifput_err:
  1699. bnx2fc_interface_put(hba);
  1700. netdev_err:
  1701. module_put(THIS_MODULE);
  1702. mod_err:
  1703. mutex_unlock(&bnx2fc_dev_lock);
  1704. rtnl_unlock();
  1705. return rc;
  1706. }
  1707. /**
  1708. * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc adapter instance
  1709. *
  1710. * @cnic: Pointer to cnic device instance
  1711. *
  1712. **/
  1713. static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
  1714. {
  1715. struct list_head *list;
  1716. struct list_head *temp;
  1717. struct bnx2fc_hba *hba;
  1718. /* Called with bnx2fc_dev_lock held */
  1719. list_for_each_safe(list, temp, &adapter_list) {
  1720. hba = (struct bnx2fc_hba *)list;
  1721. if (hba->cnic == cnic)
  1722. return hba;
  1723. }
  1724. return NULL;
  1725. }
  1726. static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev)
  1727. {
  1728. struct list_head *list;
  1729. struct list_head *temp;
  1730. struct bnx2fc_hba *hba;
  1731. /* Called with bnx2fc_dev_lock held */
  1732. list_for_each_safe(list, temp, &adapter_list) {
  1733. hba = (struct bnx2fc_hba *)list;
  1734. if (hba->phys_dev == phys_dev)
  1735. return hba;
  1736. }
  1737. printk(KERN_ERR PFX "hba_lookup: hba NULL\n");
  1738. return NULL;
  1739. }
  1740. /**
  1741. * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
  1742. *
  1743. * @dev cnic device handle
  1744. */
  1745. static void bnx2fc_ulp_exit(struct cnic_dev *dev)
  1746. {
  1747. struct bnx2fc_hba *hba;
  1748. BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
  1749. if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  1750. printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
  1751. dev->netdev->name, dev->flags);
  1752. return;
  1753. }
  1754. mutex_lock(&bnx2fc_dev_lock);
  1755. hba = bnx2fc_find_hba_for_cnic(dev);
  1756. if (!hba) {
  1757. printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
  1758. dev);
  1759. mutex_unlock(&bnx2fc_dev_lock);
  1760. return;
  1761. }
  1762. list_del_init(&hba->link);
  1763. adapter_count--;
  1764. if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
  1765. /* destroy not called yet, move to quiesced list */
  1766. bnx2fc_netdev_cleanup(hba);
  1767. bnx2fc_if_destroy(hba->ctlr.lp);
  1768. }
  1769. mutex_unlock(&bnx2fc_dev_lock);
  1770. bnx2fc_ulp_stop(hba);
  1771. /* unregister cnic device */
  1772. if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
  1773. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
  1774. bnx2fc_interface_destroy(hba);
  1775. }
  1776. /**
  1777. * bnx2fc_fcoe_reset - Resets the fcoe
  1778. *
  1779. * @shost: shost the reset is from
  1780. *
  1781. * Returns: always 0
  1782. */
  1783. static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
  1784. {
  1785. struct fc_lport *lport = shost_priv(shost);
  1786. fc_lport_reset(lport);
  1787. return 0;
  1788. }
  1789. static bool bnx2fc_match(struct net_device *netdev)
  1790. {
  1791. mutex_lock(&bnx2fc_dev_lock);
  1792. if (netdev->priv_flags & IFF_802_1Q_VLAN) {
  1793. struct net_device *phys_dev = vlan_dev_real_dev(netdev);
  1794. if (bnx2fc_hba_lookup(phys_dev)) {
  1795. mutex_unlock(&bnx2fc_dev_lock);
  1796. return true;
  1797. }
  1798. }
  1799. mutex_unlock(&bnx2fc_dev_lock);
  1800. return false;
  1801. }
  1802. static struct fcoe_transport bnx2fc_transport = {
  1803. .name = {"bnx2fc"},
  1804. .attached = false,
  1805. .list = LIST_HEAD_INIT(bnx2fc_transport.list),
  1806. .match = bnx2fc_match,
  1807. .create = bnx2fc_create,
  1808. .destroy = bnx2fc_destroy,
  1809. .enable = bnx2fc_enable,
  1810. .disable = bnx2fc_disable,
  1811. };
  1812. /**
  1813. * bnx2fc_percpu_thread_create - Create a receive thread for an
  1814. * online CPU
  1815. *
  1816. * @cpu: cpu index for the online cpu
  1817. */
  1818. static void bnx2fc_percpu_thread_create(unsigned int cpu)
  1819. {
  1820. struct bnx2fc_percpu_s *p;
  1821. struct task_struct *thread;
  1822. p = &per_cpu(bnx2fc_percpu, cpu);
  1823. thread = kthread_create(bnx2fc_percpu_io_thread,
  1824. (void *)p,
  1825. "bnx2fc_thread/%d", cpu);
  1826. /* bind thread to the cpu */
  1827. if (likely(!IS_ERR(p->iothread))) {
  1828. kthread_bind(thread, cpu);
  1829. p->iothread = thread;
  1830. wake_up_process(thread);
  1831. }
  1832. }
  1833. static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
  1834. {
  1835. struct bnx2fc_percpu_s *p;
  1836. struct task_struct *thread;
  1837. struct bnx2fc_work *work, *tmp;
  1838. LIST_HEAD(work_list);
  1839. BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
  1840. /* Prevent any new work from being queued for this CPU */
  1841. p = &per_cpu(bnx2fc_percpu, cpu);
  1842. spin_lock_bh(&p->fp_work_lock);
  1843. thread = p->iothread;
  1844. p->iothread = NULL;
  1845. /* Free all work in the list */
  1846. list_for_each_entry_safe(work, tmp, &work_list, list) {
  1847. list_del_init(&work->list);
  1848. bnx2fc_process_cq_compl(work->tgt, work->wqe);
  1849. kfree(work);
  1850. }
  1851. spin_unlock_bh(&p->fp_work_lock);
  1852. if (thread)
  1853. kthread_stop(thread);
  1854. }
  1855. /**
  1856. * bnx2fc_cpu_callback - Handler for CPU hotplug events
  1857. *
  1858. * @nfb: The callback data block
  1859. * @action: The event triggering the callback
  1860. * @hcpu: The index of the CPU that the event is for
  1861. *
  1862. * This creates or destroys per-CPU data for fcoe
  1863. *
  1864. * Returns NOTIFY_OK always.
  1865. */
  1866. static int bnx2fc_cpu_callback(struct notifier_block *nfb,
  1867. unsigned long action, void *hcpu)
  1868. {
  1869. unsigned cpu = (unsigned long)hcpu;
  1870. switch (action) {
  1871. case CPU_ONLINE:
  1872. case CPU_ONLINE_FROZEN:
  1873. printk(PFX "CPU %x online: Create Rx thread\n", cpu);
  1874. bnx2fc_percpu_thread_create(cpu);
  1875. break;
  1876. case CPU_DEAD:
  1877. case CPU_DEAD_FROZEN:
  1878. printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
  1879. bnx2fc_percpu_thread_destroy(cpu);
  1880. break;
  1881. default:
  1882. break;
  1883. }
  1884. return NOTIFY_OK;
  1885. }
  1886. /**
  1887. * bnx2fc_mod_init - module init entry point
  1888. *
  1889. * Initialize driver wide global data structures, and register
  1890. * with cnic module
  1891. **/
  1892. static int __init bnx2fc_mod_init(void)
  1893. {
  1894. struct fcoe_percpu_s *bg;
  1895. struct task_struct *l2_thread;
  1896. int rc = 0;
  1897. unsigned int cpu = 0;
  1898. struct bnx2fc_percpu_s *p;
  1899. printk(KERN_INFO PFX "%s", version);
  1900. /* register as a fcoe transport */
  1901. rc = fcoe_transport_attach(&bnx2fc_transport);
  1902. if (rc) {
  1903. printk(KERN_ERR "failed to register an fcoe transport, check "
  1904. "if libfcoe is loaded\n");
  1905. goto out;
  1906. }
  1907. INIT_LIST_HEAD(&adapter_list);
  1908. mutex_init(&bnx2fc_dev_lock);
  1909. adapter_count = 0;
  1910. /* Attach FC transport template */
  1911. rc = bnx2fc_attach_transport();
  1912. if (rc)
  1913. goto detach_ft;
  1914. bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
  1915. if (!bnx2fc_wq) {
  1916. rc = -ENOMEM;
  1917. goto release_bt;
  1918. }
  1919. bg = &bnx2fc_global;
  1920. skb_queue_head_init(&bg->fcoe_rx_list);
  1921. l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
  1922. (void *)bg,
  1923. "bnx2fc_l2_thread");
  1924. if (IS_ERR(l2_thread)) {
  1925. rc = PTR_ERR(l2_thread);
  1926. goto free_wq;
  1927. }
  1928. wake_up_process(l2_thread);
  1929. spin_lock_bh(&bg->fcoe_rx_list.lock);
  1930. bg->thread = l2_thread;
  1931. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  1932. for_each_possible_cpu(cpu) {
  1933. p = &per_cpu(bnx2fc_percpu, cpu);
  1934. INIT_LIST_HEAD(&p->work_list);
  1935. spin_lock_init(&p->fp_work_lock);
  1936. }
  1937. for_each_online_cpu(cpu) {
  1938. bnx2fc_percpu_thread_create(cpu);
  1939. }
  1940. /* Initialize per CPU interrupt thread */
  1941. register_hotcpu_notifier(&bnx2fc_cpu_notifier);
  1942. cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
  1943. return 0;
  1944. free_wq:
  1945. destroy_workqueue(bnx2fc_wq);
  1946. release_bt:
  1947. bnx2fc_release_transport();
  1948. detach_ft:
  1949. fcoe_transport_detach(&bnx2fc_transport);
  1950. out:
  1951. return rc;
  1952. }
  1953. static void __exit bnx2fc_mod_exit(void)
  1954. {
  1955. LIST_HEAD(to_be_deleted);
  1956. struct bnx2fc_hba *hba, *next;
  1957. struct fcoe_percpu_s *bg;
  1958. struct task_struct *l2_thread;
  1959. struct sk_buff *skb;
  1960. unsigned int cpu = 0;
  1961. /*
  1962. * NOTE: Since cnic calls register_driver routine rtnl_lock,
  1963. * it will have higher precedence than bnx2fc_dev_lock.
  1964. * unregister_device() cannot be called with bnx2fc_dev_lock
  1965. * held.
  1966. */
  1967. mutex_lock(&bnx2fc_dev_lock);
  1968. list_splice(&adapter_list, &to_be_deleted);
  1969. INIT_LIST_HEAD(&adapter_list);
  1970. adapter_count = 0;
  1971. mutex_unlock(&bnx2fc_dev_lock);
  1972. /* Unregister with cnic */
  1973. list_for_each_entry_safe(hba, next, &to_be_deleted, link) {
  1974. list_del_init(&hba->link);
  1975. printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p, kref = %d\n",
  1976. hba, atomic_read(&hba->kref.refcount));
  1977. bnx2fc_ulp_stop(hba);
  1978. /* unregister cnic device */
  1979. if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
  1980. &hba->reg_with_cnic))
  1981. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
  1982. bnx2fc_interface_destroy(hba);
  1983. }
  1984. cnic_unregister_driver(CNIC_ULP_FCOE);
  1985. /* Destroy global thread */
  1986. bg = &bnx2fc_global;
  1987. spin_lock_bh(&bg->fcoe_rx_list.lock);
  1988. l2_thread = bg->thread;
  1989. bg->thread = NULL;
  1990. while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
  1991. kfree_skb(skb);
  1992. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  1993. if (l2_thread)
  1994. kthread_stop(l2_thread);
  1995. unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
  1996. /* Destroy per cpu threads */
  1997. for_each_online_cpu(cpu) {
  1998. bnx2fc_percpu_thread_destroy(cpu);
  1999. }
  2000. destroy_workqueue(bnx2fc_wq);
  2001. /*
  2002. * detach from scsi transport
  2003. * must happen after all destroys are done
  2004. */
  2005. bnx2fc_release_transport();
  2006. /* detach from fcoe transport */
  2007. fcoe_transport_detach(&bnx2fc_transport);
  2008. }
  2009. module_init(bnx2fc_mod_init);
  2010. module_exit(bnx2fc_mod_exit);
  2011. static struct fc_function_template bnx2fc_transport_function = {
  2012. .show_host_node_name = 1,
  2013. .show_host_port_name = 1,
  2014. .show_host_supported_classes = 1,
  2015. .show_host_supported_fc4s = 1,
  2016. .show_host_active_fc4s = 1,
  2017. .show_host_maxframe_size = 1,
  2018. .show_host_port_id = 1,
  2019. .show_host_supported_speeds = 1,
  2020. .get_host_speed = fc_get_host_speed,
  2021. .show_host_speed = 1,
  2022. .show_host_port_type = 1,
  2023. .get_host_port_state = fc_get_host_port_state,
  2024. .show_host_port_state = 1,
  2025. .show_host_symbolic_name = 1,
  2026. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  2027. sizeof(struct bnx2fc_rport)),
  2028. .show_rport_maxframe_size = 1,
  2029. .show_rport_supported_classes = 1,
  2030. .show_host_fabric_name = 1,
  2031. .show_starget_node_name = 1,
  2032. .show_starget_port_name = 1,
  2033. .show_starget_port_id = 1,
  2034. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  2035. .show_rport_dev_loss_tmo = 1,
  2036. .get_fc_host_stats = bnx2fc_get_host_stats,
  2037. .issue_fc_host_lip = bnx2fc_fcoe_reset,
  2038. .terminate_rport_io = fc_rport_terminate_io,
  2039. .vport_create = bnx2fc_vport_create,
  2040. .vport_delete = bnx2fc_vport_destroy,
  2041. .vport_disable = bnx2fc_vport_disable,
  2042. };
  2043. static struct fc_function_template bnx2fc_vport_xport_function = {
  2044. .show_host_node_name = 1,
  2045. .show_host_port_name = 1,
  2046. .show_host_supported_classes = 1,
  2047. .show_host_supported_fc4s = 1,
  2048. .show_host_active_fc4s = 1,
  2049. .show_host_maxframe_size = 1,
  2050. .show_host_port_id = 1,
  2051. .show_host_supported_speeds = 1,
  2052. .get_host_speed = fc_get_host_speed,
  2053. .show_host_speed = 1,
  2054. .show_host_port_type = 1,
  2055. .get_host_port_state = fc_get_host_port_state,
  2056. .show_host_port_state = 1,
  2057. .show_host_symbolic_name = 1,
  2058. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  2059. sizeof(struct bnx2fc_rport)),
  2060. .show_rport_maxframe_size = 1,
  2061. .show_rport_supported_classes = 1,
  2062. .show_host_fabric_name = 1,
  2063. .show_starget_node_name = 1,
  2064. .show_starget_port_name = 1,
  2065. .show_starget_port_id = 1,
  2066. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  2067. .show_rport_dev_loss_tmo = 1,
  2068. .get_fc_host_stats = fc_get_host_stats,
  2069. .issue_fc_host_lip = bnx2fc_fcoe_reset,
  2070. .terminate_rport_io = fc_rport_terminate_io,
  2071. };
  2072. /**
  2073. * scsi_host_template structure used while registering with SCSI-ml
  2074. */
  2075. static struct scsi_host_template bnx2fc_shost_template = {
  2076. .module = THIS_MODULE,
  2077. .name = "Broadcom Offload FCoE Initiator",
  2078. .queuecommand = bnx2fc_queuecommand,
  2079. .eh_abort_handler = bnx2fc_eh_abort, /* abts */
  2080. .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
  2081. .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
  2082. .eh_host_reset_handler = fc_eh_host_reset,
  2083. .slave_alloc = fc_slave_alloc,
  2084. .change_queue_depth = fc_change_queue_depth,
  2085. .change_queue_type = fc_change_queue_type,
  2086. .this_id = -1,
  2087. .cmd_per_lun = 3,
  2088. .can_queue = BNX2FC_CAN_QUEUE,
  2089. .use_clustering = ENABLE_CLUSTERING,
  2090. .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
  2091. .max_sectors = 512,
  2092. };
  2093. static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
  2094. .frame_send = bnx2fc_xmit,
  2095. .elsct_send = bnx2fc_elsct_send,
  2096. .fcp_abort_io = bnx2fc_abort_io,
  2097. .fcp_cleanup = bnx2fc_cleanup,
  2098. .rport_event_callback = bnx2fc_rport_event_handler,
  2099. };
  2100. /**
  2101. * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
  2102. * structure carrying callback function pointers
  2103. */
  2104. static struct cnic_ulp_ops bnx2fc_cnic_cb = {
  2105. .owner = THIS_MODULE,
  2106. .cnic_init = bnx2fc_ulp_init,
  2107. .cnic_exit = bnx2fc_ulp_exit,
  2108. .cnic_start = bnx2fc_ulp_start,
  2109. .cnic_stop = bnx2fc_ulp_stop,
  2110. .indicate_kcqes = bnx2fc_indicate_kcqe,
  2111. .indicate_netevent = bnx2fc_indicate_netevent,
  2112. };