bnx2fc_fcoe.c 63 KB

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