bnx2fc_fcoe.c 71 KB

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