bnx2fc_fcoe.c 63 KB

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