bnx2fc_fcoe.c 70 KB

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