bnx2fc_fcoe.c 64 KB

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