bnx2fc_fcoe.c 63 KB

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