bnx2fc_fcoe.c 63 KB

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