bnx2fc_fcoe.c 70 KB

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