bnx2fc_fcoe.c 63 KB

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