bnx2fc_fcoe.c 64 KB

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