bnx2fc_fcoe.c 64 KB

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