bnx2fc_fcoe.c 64 KB

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