bnx2fc_fcoe.c 68 KB

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