bnx2fc_fcoe.c 67 KB

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