bnx2fc_fcoe.c 71 KB

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