bnx2fc_fcoe.c 71 KB

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