bnx2fc_fcoe.c 63 KB

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