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. return 0;
  818. }
  819. static int bnx2fc_em_config(struct fc_lport *lport)
  820. {
  821. int max_xid;
  822. if (nr_cpu_ids <= 2)
  823. max_xid = FCOE_XIDS_PER_CPU;
  824. else
  825. max_xid = FCOE_MAX_XID;
  826. if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
  827. max_xid, NULL)) {
  828. printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
  829. return -ENOMEM;
  830. }
  831. return 0;
  832. }
  833. static int bnx2fc_lport_config(struct fc_lport *lport)
  834. {
  835. lport->link_up = 0;
  836. lport->qfull = 0;
  837. lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
  838. lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
  839. lport->e_d_tov = 2 * 1000;
  840. lport->r_a_tov = 10 * 1000;
  841. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  842. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  843. lport->does_npiv = 1;
  844. memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
  845. lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
  846. /* alloc stats structure */
  847. if (fc_lport_init_stats(lport))
  848. return -ENOMEM;
  849. /* Finish fc_lport configuration */
  850. fc_lport_config(lport);
  851. return 0;
  852. }
  853. /**
  854. * bnx2fc_fip_recv - handle a received FIP frame.
  855. *
  856. * @skb: the received skb
  857. * @dev: associated &net_device
  858. * @ptype: the &packet_type structure which was used to register this handler.
  859. * @orig_dev: original receive &net_device, in case @ dev is a bond.
  860. *
  861. * Returns: 0 for success
  862. */
  863. static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
  864. struct packet_type *ptype,
  865. struct net_device *orig_dev)
  866. {
  867. struct bnx2fc_interface *interface;
  868. struct fcoe_ctlr *ctlr;
  869. interface = container_of(ptype, struct bnx2fc_interface,
  870. fip_packet_type);
  871. ctlr = bnx2fc_to_ctlr(interface);
  872. fcoe_ctlr_recv(ctlr, skb);
  873. return 0;
  874. }
  875. /**
  876. * bnx2fc_update_src_mac - Update Ethernet MAC filters.
  877. *
  878. * @fip: FCoE controller.
  879. * @old: Unicast MAC address to delete if the MAC is non-zero.
  880. * @new: Unicast MAC address to add.
  881. *
  882. * Remove any previously-set unicast MAC filter.
  883. * Add secondary FCoE MAC address filter for our OUI.
  884. */
  885. static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
  886. {
  887. struct fcoe_port *port = lport_priv(lport);
  888. memcpy(port->data_src_addr, addr, ETH_ALEN);
  889. }
  890. /**
  891. * bnx2fc_get_src_mac - return the ethernet source address for an lport
  892. *
  893. * @lport: libfc port
  894. */
  895. static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
  896. {
  897. struct fcoe_port *port;
  898. port = (struct fcoe_port *)lport_priv(lport);
  899. return port->data_src_addr;
  900. }
  901. /**
  902. * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
  903. *
  904. * @fip: FCoE controller.
  905. * @skb: FIP Packet.
  906. */
  907. static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  908. {
  909. skb->dev = bnx2fc_from_ctlr(fip)->netdev;
  910. dev_queue_xmit(skb);
  911. }
  912. static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
  913. {
  914. struct Scsi_Host *shost = vport_to_shost(vport);
  915. struct fc_lport *n_port = shost_priv(shost);
  916. struct fcoe_port *port = lport_priv(n_port);
  917. struct bnx2fc_interface *interface = port->priv;
  918. struct net_device *netdev = interface->netdev;
  919. struct fc_lport *vn_port;
  920. int rc;
  921. char buf[32];
  922. rc = fcoe_validate_vport_create(vport);
  923. if (rc) {
  924. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  925. printk(KERN_ERR PFX "Failed to create vport, "
  926. "WWPN (0x%s) already exists\n",
  927. buf);
  928. return rc;
  929. }
  930. if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
  931. printk(KERN_ERR PFX "vn ports cannot be created on"
  932. "this interface\n");
  933. return -EIO;
  934. }
  935. rtnl_lock();
  936. mutex_lock(&bnx2fc_dev_lock);
  937. vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
  938. mutex_unlock(&bnx2fc_dev_lock);
  939. rtnl_unlock();
  940. if (IS_ERR(vn_port)) {
  941. printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
  942. netdev->name);
  943. return -EIO;
  944. }
  945. if (disabled) {
  946. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  947. } else {
  948. vn_port->boot_time = jiffies;
  949. fc_lport_init(vn_port);
  950. fc_fabric_login(vn_port);
  951. fc_vport_setlink(vn_port);
  952. }
  953. return 0;
  954. }
  955. static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
  956. {
  957. struct bnx2fc_lport *blport, *tmp;
  958. spin_lock_bh(&hba->hba_lock);
  959. list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
  960. if (blport->lport == lport) {
  961. list_del(&blport->list);
  962. kfree(blport);
  963. }
  964. }
  965. spin_unlock_bh(&hba->hba_lock);
  966. }
  967. static int bnx2fc_vport_destroy(struct fc_vport *vport)
  968. {
  969. struct Scsi_Host *shost = vport_to_shost(vport);
  970. struct fc_lport *n_port = shost_priv(shost);
  971. struct fc_lport *vn_port = vport->dd_data;
  972. struct fcoe_port *port = lport_priv(vn_port);
  973. struct bnx2fc_interface *interface = port->priv;
  974. struct fc_lport *v_port;
  975. bool found = false;
  976. mutex_lock(&n_port->lp_mutex);
  977. list_for_each_entry(v_port, &n_port->vports, list)
  978. if (v_port->vport == vport) {
  979. found = true;
  980. break;
  981. }
  982. if (!found) {
  983. mutex_unlock(&n_port->lp_mutex);
  984. return -ENOENT;
  985. }
  986. list_del(&vn_port->list);
  987. mutex_unlock(&n_port->lp_mutex);
  988. bnx2fc_free_vport(interface->hba, port->lport);
  989. bnx2fc_port_shutdown(port->lport);
  990. bnx2fc_interface_put(interface);
  991. queue_work(bnx2fc_wq, &port->destroy_work);
  992. return 0;
  993. }
  994. static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
  995. {
  996. struct fc_lport *lport = vport->dd_data;
  997. if (disable) {
  998. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  999. fc_fabric_logoff(lport);
  1000. } else {
  1001. lport->boot_time = jiffies;
  1002. fc_fabric_login(lport);
  1003. fc_vport_setlink(lport);
  1004. }
  1005. return 0;
  1006. }
  1007. static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
  1008. {
  1009. struct net_device *netdev = interface->netdev;
  1010. struct net_device *physdev = interface->hba->phys_dev;
  1011. struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
  1012. struct netdev_hw_addr *ha;
  1013. int sel_san_mac = 0;
  1014. /* setup Source MAC Address */
  1015. rcu_read_lock();
  1016. for_each_dev_addr(physdev, ha) {
  1017. BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
  1018. ha->type);
  1019. printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
  1020. ha->addr[1], ha->addr[2], ha->addr[3],
  1021. ha->addr[4], ha->addr[5]);
  1022. if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
  1023. (is_valid_ether_addr(ha->addr))) {
  1024. memcpy(ctlr->ctl_src_addr, ha->addr,
  1025. ETH_ALEN);
  1026. sel_san_mac = 1;
  1027. BNX2FC_MISC_DBG("Found SAN MAC\n");
  1028. }
  1029. }
  1030. rcu_read_unlock();
  1031. if (!sel_san_mac)
  1032. return -ENODEV;
  1033. interface->fip_packet_type.func = bnx2fc_fip_recv;
  1034. interface->fip_packet_type.type = htons(ETH_P_FIP);
  1035. interface->fip_packet_type.dev = netdev;
  1036. dev_add_pack(&interface->fip_packet_type);
  1037. interface->fcoe_packet_type.func = bnx2fc_rcv;
  1038. interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  1039. interface->fcoe_packet_type.dev = netdev;
  1040. dev_add_pack(&interface->fcoe_packet_type);
  1041. return 0;
  1042. }
  1043. static int bnx2fc_attach_transport(void)
  1044. {
  1045. bnx2fc_transport_template =
  1046. fc_attach_transport(&bnx2fc_transport_function);
  1047. if (bnx2fc_transport_template == NULL) {
  1048. printk(KERN_ERR PFX "Failed to attach FC transport\n");
  1049. return -ENODEV;
  1050. }
  1051. bnx2fc_vport_xport_template =
  1052. fc_attach_transport(&bnx2fc_vport_xport_function);
  1053. if (bnx2fc_vport_xport_template == NULL) {
  1054. printk(KERN_ERR PFX
  1055. "Failed to attach FC transport for vport\n");
  1056. fc_release_transport(bnx2fc_transport_template);
  1057. bnx2fc_transport_template = NULL;
  1058. return -ENODEV;
  1059. }
  1060. return 0;
  1061. }
  1062. static void bnx2fc_release_transport(void)
  1063. {
  1064. fc_release_transport(bnx2fc_transport_template);
  1065. fc_release_transport(bnx2fc_vport_xport_template);
  1066. bnx2fc_transport_template = NULL;
  1067. bnx2fc_vport_xport_template = NULL;
  1068. }
  1069. static void bnx2fc_interface_release(struct kref *kref)
  1070. {
  1071. struct fcoe_ctlr_device *ctlr_dev;
  1072. struct bnx2fc_interface *interface;
  1073. struct fcoe_ctlr *ctlr;
  1074. struct net_device *netdev;
  1075. interface = container_of(kref, struct bnx2fc_interface, kref);
  1076. BNX2FC_MISC_DBG("Interface is being released\n");
  1077. ctlr = bnx2fc_to_ctlr(interface);
  1078. ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
  1079. netdev = interface->netdev;
  1080. /* tear-down FIP controller */
  1081. if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
  1082. fcoe_ctlr_destroy(ctlr);
  1083. fcoe_ctlr_device_delete(ctlr_dev);
  1084. dev_put(netdev);
  1085. module_put(THIS_MODULE);
  1086. }
  1087. static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
  1088. {
  1089. kref_get(&interface->kref);
  1090. }
  1091. static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
  1092. {
  1093. kref_put(&interface->kref, bnx2fc_interface_release);
  1094. }
  1095. static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
  1096. {
  1097. /* Free the command manager */
  1098. if (hba->cmd_mgr) {
  1099. bnx2fc_cmd_mgr_free(hba->cmd_mgr);
  1100. hba->cmd_mgr = NULL;
  1101. }
  1102. kfree(hba->tgt_ofld_list);
  1103. bnx2fc_unbind_pcidev(hba);
  1104. kfree(hba);
  1105. }
  1106. /**
  1107. * bnx2fc_hba_create - create a new bnx2fc hba
  1108. *
  1109. * @cnic: pointer to cnic device
  1110. *
  1111. * Creates a new FCoE hba on the given device.
  1112. *
  1113. */
  1114. static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
  1115. {
  1116. struct bnx2fc_hba *hba;
  1117. struct fcoe_capabilities *fcoe_cap;
  1118. int rc;
  1119. hba = kzalloc(sizeof(*hba), GFP_KERNEL);
  1120. if (!hba) {
  1121. printk(KERN_ERR PFX "Unable to allocate hba structure\n");
  1122. return NULL;
  1123. }
  1124. spin_lock_init(&hba->hba_lock);
  1125. mutex_init(&hba->hba_mutex);
  1126. hba->cnic = cnic;
  1127. rc = bnx2fc_bind_pcidev(hba);
  1128. if (rc) {
  1129. printk(KERN_ERR PFX "create_adapter: bind error\n");
  1130. goto bind_err;
  1131. }
  1132. hba->phys_dev = cnic->netdev;
  1133. hba->next_conn_id = 0;
  1134. hba->tgt_ofld_list =
  1135. kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
  1136. GFP_KERNEL);
  1137. if (!hba->tgt_ofld_list) {
  1138. printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
  1139. goto tgtofld_err;
  1140. }
  1141. hba->num_ofld_sess = 0;
  1142. hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
  1143. BNX2FC_MAX_XID);
  1144. if (!hba->cmd_mgr) {
  1145. printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
  1146. goto cmgr_err;
  1147. }
  1148. fcoe_cap = &hba->fcoe_cap;
  1149. fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
  1150. FCOE_IOS_PER_CONNECTION_SHIFT;
  1151. fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
  1152. FCOE_LOGINS_PER_PORT_SHIFT;
  1153. fcoe_cap->capability2 = BNX2FC_MAX_OUTSTANDING_CMNDS <<
  1154. FCOE_NUMBER_OF_EXCHANGES_SHIFT;
  1155. fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
  1156. FCOE_NPIV_WWN_PER_PORT_SHIFT;
  1157. fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
  1158. FCOE_TARGETS_SUPPORTED_SHIFT;
  1159. fcoe_cap->capability3 |= BNX2FC_MAX_OUTSTANDING_CMNDS <<
  1160. FCOE_OUTSTANDING_COMMANDS_SHIFT;
  1161. fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
  1162. init_waitqueue_head(&hba->shutdown_wait);
  1163. init_waitqueue_head(&hba->destroy_wait);
  1164. INIT_LIST_HEAD(&hba->vports);
  1165. return hba;
  1166. cmgr_err:
  1167. kfree(hba->tgt_ofld_list);
  1168. tgtofld_err:
  1169. bnx2fc_unbind_pcidev(hba);
  1170. bind_err:
  1171. kfree(hba);
  1172. return NULL;
  1173. }
  1174. struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
  1175. struct net_device *netdev,
  1176. enum fip_state fip_mode)
  1177. {
  1178. struct fcoe_ctlr_device *ctlr_dev;
  1179. struct bnx2fc_interface *interface;
  1180. struct fcoe_ctlr *ctlr;
  1181. int size;
  1182. int rc = 0;
  1183. size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
  1184. ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
  1185. size);
  1186. if (!ctlr_dev) {
  1187. printk(KERN_ERR PFX "Unable to allocate interface structure\n");
  1188. return NULL;
  1189. }
  1190. ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  1191. interface = fcoe_ctlr_priv(ctlr);
  1192. dev_hold(netdev);
  1193. kref_init(&interface->kref);
  1194. interface->hba = hba;
  1195. interface->netdev = netdev;
  1196. /* Initialize FIP */
  1197. fcoe_ctlr_init(ctlr, fip_mode);
  1198. ctlr->send = bnx2fc_fip_send;
  1199. ctlr->update_mac = bnx2fc_update_src_mac;
  1200. ctlr->get_src_addr = bnx2fc_get_src_mac;
  1201. set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
  1202. rc = bnx2fc_interface_setup(interface);
  1203. if (!rc)
  1204. return interface;
  1205. fcoe_ctlr_destroy(ctlr);
  1206. dev_put(netdev);
  1207. fcoe_ctlr_device_delete(ctlr_dev);
  1208. return NULL;
  1209. }
  1210. /**
  1211. * bnx2fc_if_create - Create FCoE instance on a given interface
  1212. *
  1213. * @interface: FCoE interface to create a local port on
  1214. * @parent: Device pointer to be the parent in sysfs for the SCSI host
  1215. * @npiv: Indicates if the port is vport or not
  1216. *
  1217. * Creates a fc_lport instance and a Scsi_Host instance and configure them.
  1218. *
  1219. * Returns: Allocated fc_lport or an error pointer
  1220. */
  1221. static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
  1222. struct device *parent, int npiv)
  1223. {
  1224. struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
  1225. struct fc_lport *lport, *n_port;
  1226. struct fcoe_port *port;
  1227. struct Scsi_Host *shost;
  1228. struct fc_vport *vport = dev_to_vport(parent);
  1229. struct bnx2fc_lport *blport;
  1230. struct bnx2fc_hba *hba;
  1231. int rc = 0;
  1232. blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
  1233. if (!blport) {
  1234. BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
  1235. return NULL;
  1236. }
  1237. /* Allocate Scsi_Host structure */
  1238. if (!npiv)
  1239. lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
  1240. else
  1241. lport = libfc_vport_create(vport, sizeof(*port));
  1242. if (!lport) {
  1243. printk(KERN_ERR PFX "could not allocate scsi host structure\n");
  1244. goto free_blport;
  1245. }
  1246. shost = lport->host;
  1247. port = lport_priv(lport);
  1248. port->lport = lport;
  1249. port->priv = interface;
  1250. port->get_netdev = bnx2fc_netdev;
  1251. INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
  1252. /* Configure fcoe_port */
  1253. rc = bnx2fc_lport_config(lport);
  1254. if (rc)
  1255. goto lp_config_err;
  1256. if (npiv) {
  1257. printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
  1258. vport->node_name, vport->port_name);
  1259. fc_set_wwnn(lport, vport->node_name);
  1260. fc_set_wwpn(lport, vport->port_name);
  1261. }
  1262. /* Configure netdev and networking properties of the lport */
  1263. rc = bnx2fc_net_config(lport, interface->netdev);
  1264. if (rc) {
  1265. printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
  1266. goto lp_config_err;
  1267. }
  1268. rc = bnx2fc_shost_config(lport, parent);
  1269. if (rc) {
  1270. printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
  1271. interface->netdev->name);
  1272. goto lp_config_err;
  1273. }
  1274. /* Initialize the libfc library */
  1275. rc = bnx2fc_libfc_config(lport);
  1276. if (rc) {
  1277. printk(KERN_ERR PFX "Couldnt configure libfc\n");
  1278. goto shost_err;
  1279. }
  1280. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  1281. /* Allocate exchange manager */
  1282. if (!npiv)
  1283. rc = bnx2fc_em_config(lport);
  1284. else {
  1285. shost = vport_to_shost(vport);
  1286. n_port = shost_priv(shost);
  1287. rc = fc_exch_mgr_list_clone(n_port, lport);
  1288. }
  1289. if (rc) {
  1290. printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
  1291. goto shost_err;
  1292. }
  1293. bnx2fc_interface_get(interface);
  1294. hba = interface->hba;
  1295. spin_lock_bh(&hba->hba_lock);
  1296. blport->lport = lport;
  1297. list_add_tail(&blport->list, &hba->vports);
  1298. spin_unlock_bh(&hba->hba_lock);
  1299. return lport;
  1300. shost_err:
  1301. scsi_remove_host(shost);
  1302. lp_config_err:
  1303. scsi_host_put(lport->host);
  1304. free_blport:
  1305. kfree(blport);
  1306. return NULL;
  1307. }
  1308. static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
  1309. {
  1310. /* Dont listen for Ethernet packets anymore */
  1311. __dev_remove_pack(&interface->fcoe_packet_type);
  1312. __dev_remove_pack(&interface->fip_packet_type);
  1313. synchronize_net();
  1314. }
  1315. static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
  1316. {
  1317. struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
  1318. struct fc_lport *lport = ctlr->lp;
  1319. struct fcoe_port *port = lport_priv(lport);
  1320. struct bnx2fc_hba *hba = interface->hba;
  1321. /* Stop the transmit retry timer */
  1322. del_timer_sync(&port->timer);
  1323. /* Free existing transmit skbs */
  1324. fcoe_clean_pending_queue(lport);
  1325. bnx2fc_net_cleanup(interface);
  1326. bnx2fc_free_vport(hba, lport);
  1327. }
  1328. static void bnx2fc_if_destroy(struct fc_lport *lport)
  1329. {
  1330. /* Free queued packets for the receive thread */
  1331. bnx2fc_clean_rx_queue(lport);
  1332. /* Detach from scsi-ml */
  1333. fc_remove_host(lport->host);
  1334. scsi_remove_host(lport->host);
  1335. /*
  1336. * Note that only the physical lport will have the exchange manager.
  1337. * for vports, this function is NOP
  1338. */
  1339. fc_exch_mgr_free(lport);
  1340. /* Free memory used by statistical counters */
  1341. fc_lport_free_stats(lport);
  1342. /* Release Scsi_Host */
  1343. scsi_host_put(lport->host);
  1344. }
  1345. static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
  1346. {
  1347. struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
  1348. struct fc_lport *lport = ctlr->lp;
  1349. struct fcoe_port *port = lport_priv(lport);
  1350. bnx2fc_interface_cleanup(interface);
  1351. bnx2fc_stop(interface);
  1352. list_del(&interface->list);
  1353. bnx2fc_interface_put(interface);
  1354. queue_work(bnx2fc_wq, &port->destroy_work);
  1355. }
  1356. /**
  1357. * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
  1358. *
  1359. * @buffer: The name of the Ethernet interface to be destroyed
  1360. * @kp: The associated kernel parameter
  1361. *
  1362. * Called from sysfs.
  1363. *
  1364. * Returns: 0 for success
  1365. */
  1366. static int bnx2fc_destroy(struct net_device *netdev)
  1367. {
  1368. struct bnx2fc_interface *interface = NULL;
  1369. struct workqueue_struct *timer_work_queue;
  1370. struct fcoe_ctlr *ctlr;
  1371. int rc = 0;
  1372. rtnl_lock();
  1373. mutex_lock(&bnx2fc_dev_lock);
  1374. interface = bnx2fc_interface_lookup(netdev);
  1375. ctlr = bnx2fc_to_ctlr(interface);
  1376. if (!interface || !ctlr->lp) {
  1377. rc = -ENODEV;
  1378. printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
  1379. goto netdev_err;
  1380. }
  1381. timer_work_queue = interface->timer_work_queue;
  1382. __bnx2fc_destroy(interface);
  1383. destroy_workqueue(timer_work_queue);
  1384. netdev_err:
  1385. mutex_unlock(&bnx2fc_dev_lock);
  1386. rtnl_unlock();
  1387. return rc;
  1388. }
  1389. static void bnx2fc_destroy_work(struct work_struct *work)
  1390. {
  1391. struct fcoe_port *port;
  1392. struct fc_lport *lport;
  1393. port = container_of(work, struct fcoe_port, destroy_work);
  1394. lport = port->lport;
  1395. BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
  1396. bnx2fc_if_destroy(lport);
  1397. }
  1398. static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
  1399. {
  1400. bnx2fc_free_fw_resc(hba);
  1401. bnx2fc_free_task_ctx(hba);
  1402. }
  1403. /**
  1404. * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
  1405. * pci structure
  1406. *
  1407. * @hba: Adapter instance
  1408. */
  1409. static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
  1410. {
  1411. if (bnx2fc_setup_task_ctx(hba))
  1412. goto mem_err;
  1413. if (bnx2fc_setup_fw_resc(hba))
  1414. goto mem_err;
  1415. return 0;
  1416. mem_err:
  1417. bnx2fc_unbind_adapter_devices(hba);
  1418. return -ENOMEM;
  1419. }
  1420. static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
  1421. {
  1422. struct cnic_dev *cnic;
  1423. if (!hba->cnic) {
  1424. printk(KERN_ERR PFX "cnic is NULL\n");
  1425. return -ENODEV;
  1426. }
  1427. cnic = hba->cnic;
  1428. hba->pcidev = cnic->pcidev;
  1429. if (hba->pcidev)
  1430. pci_dev_get(hba->pcidev);
  1431. return 0;
  1432. }
  1433. static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
  1434. {
  1435. if (hba->pcidev)
  1436. pci_dev_put(hba->pcidev);
  1437. hba->pcidev = NULL;
  1438. }
  1439. /**
  1440. * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
  1441. *
  1442. * @handle: transport handle pointing to adapter struture
  1443. */
  1444. static int bnx2fc_ulp_get_stats(void *handle)
  1445. {
  1446. struct bnx2fc_hba *hba = handle;
  1447. struct cnic_dev *cnic;
  1448. struct fcoe_stats_info *stats_addr;
  1449. if (!hba)
  1450. return -EINVAL;
  1451. cnic = hba->cnic;
  1452. stats_addr = &cnic->stats_addr->fcoe_stat;
  1453. if (!stats_addr)
  1454. return -EINVAL;
  1455. strncpy(stats_addr->version, BNX2FC_VERSION,
  1456. sizeof(stats_addr->version));
  1457. stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
  1458. stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
  1459. return 0;
  1460. }
  1461. /**
  1462. * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
  1463. *
  1464. * @handle: transport handle pointing to adapter structure
  1465. *
  1466. * This function maps adapter structure to pcidev structure and initiates
  1467. * firmware handshake to enable/initialize on-chip FCoE components.
  1468. * This bnx2fc - cnic interface api callback is used after following
  1469. * conditions are met -
  1470. * a) underlying network interface is up (marked by event NETDEV_UP
  1471. * from netdev
  1472. * b) bnx2fc adatper structure is registered.
  1473. */
  1474. static void bnx2fc_ulp_start(void *handle)
  1475. {
  1476. struct bnx2fc_hba *hba = handle;
  1477. struct bnx2fc_interface *interface;
  1478. struct fcoe_ctlr *ctlr;
  1479. struct fc_lport *lport;
  1480. mutex_lock(&bnx2fc_dev_lock);
  1481. if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
  1482. bnx2fc_fw_init(hba);
  1483. BNX2FC_MISC_DBG("bnx2fc started.\n");
  1484. list_for_each_entry(interface, &if_list, list) {
  1485. if (interface->hba == hba) {
  1486. ctlr = bnx2fc_to_ctlr(interface);
  1487. lport = ctlr->lp;
  1488. /* Kick off Fabric discovery*/
  1489. printk(KERN_ERR PFX "ulp_init: start discovery\n");
  1490. lport->tt.frame_send = bnx2fc_xmit;
  1491. bnx2fc_start_disc(interface);
  1492. }
  1493. }
  1494. mutex_unlock(&bnx2fc_dev_lock);
  1495. }
  1496. static void bnx2fc_port_shutdown(struct fc_lport *lport)
  1497. {
  1498. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1499. fc_fabric_logoff(lport);
  1500. fc_lport_destroy(lport);
  1501. }
  1502. static void bnx2fc_stop(struct bnx2fc_interface *interface)
  1503. {
  1504. struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
  1505. struct fc_lport *lport;
  1506. struct fc_lport *vport;
  1507. if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
  1508. return;
  1509. lport = ctlr->lp;
  1510. bnx2fc_port_shutdown(lport);
  1511. mutex_lock(&lport->lp_mutex);
  1512. list_for_each_entry(vport, &lport->vports, list)
  1513. fc_host_port_type(vport->host) =
  1514. FC_PORTTYPE_UNKNOWN;
  1515. mutex_unlock(&lport->lp_mutex);
  1516. fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
  1517. fcoe_ctlr_link_down(ctlr);
  1518. fcoe_clean_pending_queue(lport);
  1519. }
  1520. static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
  1521. {
  1522. #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
  1523. int rc = -1;
  1524. int i = HZ;
  1525. rc = bnx2fc_bind_adapter_devices(hba);
  1526. if (rc) {
  1527. printk(KERN_ALERT PFX
  1528. "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
  1529. goto err_out;
  1530. }
  1531. rc = bnx2fc_send_fw_fcoe_init_msg(hba);
  1532. if (rc) {
  1533. printk(KERN_ALERT PFX
  1534. "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
  1535. goto err_unbind;
  1536. }
  1537. /*
  1538. * Wait until the adapter init message is complete, and adapter
  1539. * state is UP.
  1540. */
  1541. while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
  1542. msleep(BNX2FC_INIT_POLL_TIME);
  1543. if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
  1544. printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
  1545. "Ignoring...\n",
  1546. hba->cnic->netdev->name);
  1547. rc = -1;
  1548. goto err_unbind;
  1549. }
  1550. set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
  1551. return 0;
  1552. err_unbind:
  1553. bnx2fc_unbind_adapter_devices(hba);
  1554. err_out:
  1555. return rc;
  1556. }
  1557. static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
  1558. {
  1559. if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
  1560. if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
  1561. init_timer(&hba->destroy_timer);
  1562. hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
  1563. jiffies;
  1564. hba->destroy_timer.function = bnx2fc_destroy_timer;
  1565. hba->destroy_timer.data = (unsigned long)hba;
  1566. add_timer(&hba->destroy_timer);
  1567. wait_event_interruptible(hba->destroy_wait,
  1568. test_bit(BNX2FC_FLAG_DESTROY_CMPL,
  1569. &hba->flags));
  1570. clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
  1571. /* This should never happen */
  1572. if (signal_pending(current))
  1573. flush_signals(current);
  1574. del_timer_sync(&hba->destroy_timer);
  1575. }
  1576. bnx2fc_unbind_adapter_devices(hba);
  1577. }
  1578. }
  1579. /**
  1580. * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
  1581. *
  1582. * @handle: transport handle pointing to adapter structure
  1583. *
  1584. * Driver checks if adapter is already in shutdown mode, if not start
  1585. * the shutdown process.
  1586. */
  1587. static void bnx2fc_ulp_stop(void *handle)
  1588. {
  1589. struct bnx2fc_hba *hba = handle;
  1590. struct bnx2fc_interface *interface;
  1591. printk(KERN_ERR "ULP_STOP\n");
  1592. mutex_lock(&bnx2fc_dev_lock);
  1593. if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
  1594. goto exit;
  1595. list_for_each_entry(interface, &if_list, list) {
  1596. if (interface->hba == hba)
  1597. bnx2fc_stop(interface);
  1598. }
  1599. BUG_ON(hba->num_ofld_sess != 0);
  1600. mutex_lock(&hba->hba_mutex);
  1601. clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
  1602. clear_bit(ADAPTER_STATE_GOING_DOWN,
  1603. &hba->adapter_state);
  1604. clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
  1605. mutex_unlock(&hba->hba_mutex);
  1606. bnx2fc_fw_destroy(hba);
  1607. exit:
  1608. mutex_unlock(&bnx2fc_dev_lock);
  1609. }
  1610. static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
  1611. {
  1612. struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
  1613. struct fc_lport *lport;
  1614. int wait_cnt = 0;
  1615. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1616. /* Kick off FIP/FLOGI */
  1617. if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
  1618. printk(KERN_ERR PFX "Init not done yet\n");
  1619. return;
  1620. }
  1621. lport = ctlr->lp;
  1622. BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
  1623. if (!bnx2fc_link_ok(lport) && interface->enabled) {
  1624. BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
  1625. fcoe_ctlr_link_up(ctlr);
  1626. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1627. set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
  1628. }
  1629. /* wait for the FCF to be selected before issuing FLOGI */
  1630. while (!ctlr->sel_fcf) {
  1631. msleep(250);
  1632. /* give up after 3 secs */
  1633. if (++wait_cnt > 12)
  1634. break;
  1635. }
  1636. /* Reset max receive frame size to default */
  1637. if (fc_set_mfs(lport, BNX2FC_MFS))
  1638. return;
  1639. fc_lport_init(lport);
  1640. fc_fabric_login(lport);
  1641. }
  1642. /**
  1643. * bnx2fc_ulp_init - Initialize an adapter instance
  1644. *
  1645. * @dev : cnic device handle
  1646. * Called from cnic_register_driver() context to initialize all
  1647. * enumerated cnic devices. This routine allocates adapter structure
  1648. * and other device specific resources.
  1649. */
  1650. static void bnx2fc_ulp_init(struct cnic_dev *dev)
  1651. {
  1652. struct bnx2fc_hba *hba;
  1653. int rc = 0;
  1654. BNX2FC_MISC_DBG("Entered %s\n", __func__);
  1655. /* bnx2fc works only when bnx2x is loaded */
  1656. if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
  1657. (dev->max_fcoe_conn == 0)) {
  1658. printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
  1659. " flags: %lx fcoe_conn: %d\n",
  1660. dev->netdev->name, dev->flags, dev->max_fcoe_conn);
  1661. return;
  1662. }
  1663. hba = bnx2fc_hba_create(dev);
  1664. if (!hba) {
  1665. printk(KERN_ERR PFX "hba initialization failed\n");
  1666. return;
  1667. }
  1668. /* Add HBA to the adapter list */
  1669. mutex_lock(&bnx2fc_dev_lock);
  1670. list_add_tail(&hba->list, &adapter_list);
  1671. adapter_count++;
  1672. mutex_unlock(&bnx2fc_dev_lock);
  1673. dev->fcoe_cap = &hba->fcoe_cap;
  1674. clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
  1675. rc = dev->register_device(dev, CNIC_ULP_FCOE,
  1676. (void *) hba);
  1677. if (rc)
  1678. printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
  1679. else
  1680. set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
  1681. }
  1682. /**
  1683. * Deperecated: Use bnx2fc_enabled()
  1684. */
  1685. static int bnx2fc_disable(struct net_device *netdev)
  1686. {
  1687. struct bnx2fc_interface *interface;
  1688. struct fcoe_ctlr *ctlr;
  1689. int rc = 0;
  1690. rtnl_lock();
  1691. mutex_lock(&bnx2fc_dev_lock);
  1692. interface = bnx2fc_interface_lookup(netdev);
  1693. ctlr = bnx2fc_to_ctlr(interface);
  1694. if (!interface || !ctlr->lp) {
  1695. rc = -ENODEV;
  1696. printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
  1697. } else {
  1698. interface->enabled = false;
  1699. fcoe_ctlr_link_down(ctlr);
  1700. fcoe_clean_pending_queue(ctlr->lp);
  1701. }
  1702. mutex_unlock(&bnx2fc_dev_lock);
  1703. rtnl_unlock();
  1704. return rc;
  1705. }
  1706. /**
  1707. * Deprecated: Use bnx2fc_enabled()
  1708. */
  1709. static int bnx2fc_enable(struct net_device *netdev)
  1710. {
  1711. struct bnx2fc_interface *interface;
  1712. struct fcoe_ctlr *ctlr;
  1713. int rc = 0;
  1714. rtnl_lock();
  1715. mutex_lock(&bnx2fc_dev_lock);
  1716. interface = bnx2fc_interface_lookup(netdev);
  1717. ctlr = bnx2fc_to_ctlr(interface);
  1718. if (!interface || !ctlr->lp) {
  1719. rc = -ENODEV;
  1720. printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
  1721. } else if (!bnx2fc_link_ok(ctlr->lp)) {
  1722. fcoe_ctlr_link_up(ctlr);
  1723. interface->enabled = true;
  1724. }
  1725. mutex_unlock(&bnx2fc_dev_lock);
  1726. rtnl_unlock();
  1727. return rc;
  1728. }
  1729. /**
  1730. * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
  1731. * @cdev: The FCoE Controller that is being enabled or disabled
  1732. *
  1733. * fcoe_sysfs will ensure that the state of 'enabled' has
  1734. * changed, so no checking is necessary here. This routine simply
  1735. * calls fcoe_enable or fcoe_disable, both of which are deprecated.
  1736. * When those routines are removed the functionality can be merged
  1737. * here.
  1738. */
  1739. static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
  1740. {
  1741. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
  1742. struct fc_lport *lport = ctlr->lp;
  1743. struct net_device *netdev = bnx2fc_netdev(lport);
  1744. switch (cdev->enabled) {
  1745. case FCOE_CTLR_ENABLED:
  1746. return bnx2fc_enable(netdev);
  1747. case FCOE_CTLR_DISABLED:
  1748. return bnx2fc_disable(netdev);
  1749. case FCOE_CTLR_UNUSED:
  1750. default:
  1751. return -ENOTSUPP;
  1752. };
  1753. }
  1754. enum bnx2fc_create_link_state {
  1755. BNX2FC_CREATE_LINK_DOWN,
  1756. BNX2FC_CREATE_LINK_UP,
  1757. };
  1758. /**
  1759. * _bnx2fc_create() - Create bnx2fc FCoE interface
  1760. * @netdev : The net_device object the Ethernet interface to create on
  1761. * @fip_mode: The FIP mode for this creation
  1762. * @link_state: The ctlr link state on creation
  1763. *
  1764. * Called from either the libfcoe 'create' module parameter
  1765. * via fcoe_create or from fcoe_syfs's ctlr_create file.
  1766. *
  1767. * libfcoe's 'create' module parameter is deprecated so some
  1768. * consolidation of code can be done when that interface is
  1769. * removed.
  1770. *
  1771. * Returns: 0 for success
  1772. */
  1773. static int _bnx2fc_create(struct net_device *netdev,
  1774. enum fip_state fip_mode,
  1775. enum bnx2fc_create_link_state link_state)
  1776. {
  1777. struct fcoe_ctlr_device *cdev;
  1778. struct fcoe_ctlr *ctlr;
  1779. struct bnx2fc_interface *interface;
  1780. struct bnx2fc_hba *hba;
  1781. struct net_device *phys_dev = netdev;
  1782. struct fc_lport *lport;
  1783. struct ethtool_drvinfo drvinfo;
  1784. int rc = 0;
  1785. int vlan_id = 0;
  1786. BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
  1787. if (fip_mode != FIP_MODE_FABRIC) {
  1788. printk(KERN_ERR "fip mode not FABRIC\n");
  1789. return -EIO;
  1790. }
  1791. rtnl_lock();
  1792. mutex_lock(&bnx2fc_dev_lock);
  1793. if (!try_module_get(THIS_MODULE)) {
  1794. rc = -EINVAL;
  1795. goto mod_err;
  1796. }
  1797. /* obtain physical netdev */
  1798. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  1799. phys_dev = vlan_dev_real_dev(netdev);
  1800. /* verify if the physical device is a netxtreme2 device */
  1801. if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
  1802. memset(&drvinfo, 0, sizeof(drvinfo));
  1803. phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
  1804. if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
  1805. printk(KERN_ERR PFX "Not a netxtreme2 device\n");
  1806. rc = -EINVAL;
  1807. goto netdev_err;
  1808. }
  1809. } else {
  1810. printk(KERN_ERR PFX "unable to obtain drv_info\n");
  1811. rc = -EINVAL;
  1812. goto netdev_err;
  1813. }
  1814. /* obtain interface and initialize rest of the structure */
  1815. hba = bnx2fc_hba_lookup(phys_dev);
  1816. if (!hba) {
  1817. rc = -ENODEV;
  1818. printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
  1819. goto netdev_err;
  1820. }
  1821. if (bnx2fc_interface_lookup(netdev)) {
  1822. rc = -EEXIST;
  1823. goto netdev_err;
  1824. }
  1825. interface = bnx2fc_interface_create(hba, netdev, fip_mode);
  1826. if (!interface) {
  1827. printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
  1828. goto ifput_err;
  1829. }
  1830. if (netdev->priv_flags & IFF_802_1Q_VLAN) {
  1831. vlan_id = vlan_dev_vlan_id(netdev);
  1832. interface->vlan_enabled = 1;
  1833. }
  1834. ctlr = bnx2fc_to_ctlr(interface);
  1835. interface->vlan_id = vlan_id;
  1836. interface->timer_work_queue =
  1837. create_singlethread_workqueue("bnx2fc_timer_wq");
  1838. if (!interface->timer_work_queue) {
  1839. printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
  1840. rc = -EINVAL;
  1841. goto ifput_err;
  1842. }
  1843. lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
  1844. if (!lport) {
  1845. printk(KERN_ERR PFX "Failed to create interface (%s)\n",
  1846. netdev->name);
  1847. rc = -EINVAL;
  1848. goto if_create_err;
  1849. }
  1850. /* Add interface to if_list */
  1851. list_add_tail(&interface->list, &if_list);
  1852. lport->boot_time = jiffies;
  1853. /* Make this master N_port */
  1854. ctlr->lp = lport;
  1855. cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
  1856. if (link_state == BNX2FC_CREATE_LINK_UP)
  1857. cdev->enabled = FCOE_CTLR_ENABLED;
  1858. else
  1859. cdev->enabled = FCOE_CTLR_DISABLED;
  1860. if (link_state == BNX2FC_CREATE_LINK_UP &&
  1861. !bnx2fc_link_ok(lport)) {
  1862. fcoe_ctlr_link_up(ctlr);
  1863. fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
  1864. set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
  1865. }
  1866. BNX2FC_HBA_DBG(lport, "create: START DISC\n");
  1867. bnx2fc_start_disc(interface);
  1868. if (link_state == BNX2FC_CREATE_LINK_UP)
  1869. interface->enabled = true;
  1870. /*
  1871. * Release from kref_init in bnx2fc_interface_setup, on success
  1872. * lport should be holding a reference taken in bnx2fc_if_create
  1873. */
  1874. bnx2fc_interface_put(interface);
  1875. /* put netdev that was held while calling dev_get_by_name */
  1876. mutex_unlock(&bnx2fc_dev_lock);
  1877. rtnl_unlock();
  1878. return 0;
  1879. if_create_err:
  1880. destroy_workqueue(interface->timer_work_queue);
  1881. ifput_err:
  1882. bnx2fc_net_cleanup(interface);
  1883. bnx2fc_interface_put(interface);
  1884. goto mod_err;
  1885. netdev_err:
  1886. module_put(THIS_MODULE);
  1887. mod_err:
  1888. mutex_unlock(&bnx2fc_dev_lock);
  1889. rtnl_unlock();
  1890. return rc;
  1891. }
  1892. /**
  1893. * bnx2fc_create() - Create a bnx2fc interface
  1894. * @netdev : The net_device object the Ethernet interface to create on
  1895. * @fip_mode: The FIP mode for this creation
  1896. *
  1897. * Called from fcoe transport
  1898. *
  1899. * Returns: 0 for success
  1900. */
  1901. static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
  1902. {
  1903. return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
  1904. }
  1905. /**
  1906. * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
  1907. * @netdev: The net_device to be used by the allocated FCoE Controller
  1908. *
  1909. * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
  1910. * in a link_down state. The allows the user an opportunity to configure
  1911. * the FCoE Controller from sysfs before enabling the FCoE Controller.
  1912. *
  1913. * Creating in with this routine starts the FCoE Controller in Fabric
  1914. * mode. The user can change to VN2VN or another mode before enabling.
  1915. */
  1916. static int bnx2fc_ctlr_alloc(struct net_device *netdev)
  1917. {
  1918. return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
  1919. BNX2FC_CREATE_LINK_DOWN);
  1920. }
  1921. /**
  1922. * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
  1923. *
  1924. * @cnic: Pointer to cnic device instance
  1925. *
  1926. **/
  1927. static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
  1928. {
  1929. struct bnx2fc_hba *hba;
  1930. /* Called with bnx2fc_dev_lock held */
  1931. list_for_each_entry(hba, &adapter_list, list) {
  1932. if (hba->cnic == cnic)
  1933. return hba;
  1934. }
  1935. return NULL;
  1936. }
  1937. static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
  1938. *netdev)
  1939. {
  1940. struct bnx2fc_interface *interface;
  1941. /* Called with bnx2fc_dev_lock held */
  1942. list_for_each_entry(interface, &if_list, list) {
  1943. if (interface->netdev == netdev)
  1944. return interface;
  1945. }
  1946. return NULL;
  1947. }
  1948. static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
  1949. *phys_dev)
  1950. {
  1951. struct bnx2fc_hba *hba;
  1952. /* Called with bnx2fc_dev_lock held */
  1953. list_for_each_entry(hba, &adapter_list, list) {
  1954. if (hba->phys_dev == phys_dev)
  1955. return hba;
  1956. }
  1957. printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
  1958. return NULL;
  1959. }
  1960. /**
  1961. * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
  1962. *
  1963. * @dev cnic device handle
  1964. */
  1965. static void bnx2fc_ulp_exit(struct cnic_dev *dev)
  1966. {
  1967. struct bnx2fc_hba *hba;
  1968. struct bnx2fc_interface *interface, *tmp;
  1969. BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
  1970. if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
  1971. printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
  1972. dev->netdev->name, dev->flags);
  1973. return;
  1974. }
  1975. mutex_lock(&bnx2fc_dev_lock);
  1976. hba = bnx2fc_find_hba_for_cnic(dev);
  1977. if (!hba) {
  1978. printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
  1979. dev);
  1980. mutex_unlock(&bnx2fc_dev_lock);
  1981. return;
  1982. }
  1983. list_del_init(&hba->list);
  1984. adapter_count--;
  1985. list_for_each_entry_safe(interface, tmp, &if_list, list)
  1986. /* destroy not called yet, move to quiesced list */
  1987. if (interface->hba == hba)
  1988. __bnx2fc_destroy(interface);
  1989. mutex_unlock(&bnx2fc_dev_lock);
  1990. bnx2fc_ulp_stop(hba);
  1991. /* unregister cnic device */
  1992. if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
  1993. hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
  1994. bnx2fc_hba_destroy(hba);
  1995. }
  1996. /**
  1997. * bnx2fc_fcoe_reset - Resets the fcoe
  1998. *
  1999. * @shost: shost the reset is from
  2000. *
  2001. * Returns: always 0
  2002. */
  2003. static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
  2004. {
  2005. struct fc_lport *lport = shost_priv(shost);
  2006. fc_lport_reset(lport);
  2007. return 0;
  2008. }
  2009. static bool bnx2fc_match(struct net_device *netdev)
  2010. {
  2011. struct net_device *phys_dev = netdev;
  2012. mutex_lock(&bnx2fc_dev_lock);
  2013. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  2014. phys_dev = vlan_dev_real_dev(netdev);
  2015. if (bnx2fc_hba_lookup(phys_dev)) {
  2016. mutex_unlock(&bnx2fc_dev_lock);
  2017. return true;
  2018. }
  2019. mutex_unlock(&bnx2fc_dev_lock);
  2020. return false;
  2021. }
  2022. static struct fcoe_transport bnx2fc_transport = {
  2023. .name = {"bnx2fc"},
  2024. .attached = false,
  2025. .list = LIST_HEAD_INIT(bnx2fc_transport.list),
  2026. .alloc = bnx2fc_ctlr_alloc,
  2027. .match = bnx2fc_match,
  2028. .create = bnx2fc_create,
  2029. .destroy = bnx2fc_destroy,
  2030. .enable = bnx2fc_enable,
  2031. .disable = bnx2fc_disable,
  2032. };
  2033. /**
  2034. * bnx2fc_percpu_thread_create - Create a receive thread for an
  2035. * online CPU
  2036. *
  2037. * @cpu: cpu index for the online cpu
  2038. */
  2039. static void bnx2fc_percpu_thread_create(unsigned int cpu)
  2040. {
  2041. struct bnx2fc_percpu_s *p;
  2042. struct task_struct *thread;
  2043. p = &per_cpu(bnx2fc_percpu, cpu);
  2044. thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
  2045. (void *)p, cpu_to_node(cpu),
  2046. "bnx2fc_thread/%d", cpu);
  2047. /* bind thread to the cpu */
  2048. if (likely(!IS_ERR(thread))) {
  2049. kthread_bind(thread, cpu);
  2050. p->iothread = thread;
  2051. wake_up_process(thread);
  2052. }
  2053. }
  2054. static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
  2055. {
  2056. struct bnx2fc_percpu_s *p;
  2057. struct task_struct *thread;
  2058. struct bnx2fc_work *work, *tmp;
  2059. BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
  2060. /* Prevent any new work from being queued for this CPU */
  2061. p = &per_cpu(bnx2fc_percpu, cpu);
  2062. spin_lock_bh(&p->fp_work_lock);
  2063. thread = p->iothread;
  2064. p->iothread = NULL;
  2065. /* Free all work in the list */
  2066. list_for_each_entry_safe(work, tmp, &p->work_list, list) {
  2067. list_del_init(&work->list);
  2068. bnx2fc_process_cq_compl(work->tgt, work->wqe);
  2069. kfree(work);
  2070. }
  2071. spin_unlock_bh(&p->fp_work_lock);
  2072. if (thread)
  2073. kthread_stop(thread);
  2074. }
  2075. /**
  2076. * bnx2fc_cpu_callback - Handler for CPU hotplug events
  2077. *
  2078. * @nfb: The callback data block
  2079. * @action: The event triggering the callback
  2080. * @hcpu: The index of the CPU that the event is for
  2081. *
  2082. * This creates or destroys per-CPU data for fcoe
  2083. *
  2084. * Returns NOTIFY_OK always.
  2085. */
  2086. static int bnx2fc_cpu_callback(struct notifier_block *nfb,
  2087. unsigned long action, void *hcpu)
  2088. {
  2089. unsigned cpu = (unsigned long)hcpu;
  2090. switch (action) {
  2091. case CPU_ONLINE:
  2092. case CPU_ONLINE_FROZEN:
  2093. printk(PFX "CPU %x online: Create Rx thread\n", cpu);
  2094. bnx2fc_percpu_thread_create(cpu);
  2095. break;
  2096. case CPU_DEAD:
  2097. case CPU_DEAD_FROZEN:
  2098. printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
  2099. bnx2fc_percpu_thread_destroy(cpu);
  2100. break;
  2101. default:
  2102. break;
  2103. }
  2104. return NOTIFY_OK;
  2105. }
  2106. /**
  2107. * bnx2fc_mod_init - module init entry point
  2108. *
  2109. * Initialize driver wide global data structures, and register
  2110. * with cnic module
  2111. **/
  2112. static int __init bnx2fc_mod_init(void)
  2113. {
  2114. struct fcoe_percpu_s *bg;
  2115. struct task_struct *l2_thread;
  2116. int rc = 0;
  2117. unsigned int cpu = 0;
  2118. struct bnx2fc_percpu_s *p;
  2119. printk(KERN_INFO PFX "%s", version);
  2120. /* register as a fcoe transport */
  2121. rc = fcoe_transport_attach(&bnx2fc_transport);
  2122. if (rc) {
  2123. printk(KERN_ERR "failed to register an fcoe transport, check "
  2124. "if libfcoe is loaded\n");
  2125. goto out;
  2126. }
  2127. INIT_LIST_HEAD(&adapter_list);
  2128. INIT_LIST_HEAD(&if_list);
  2129. mutex_init(&bnx2fc_dev_lock);
  2130. adapter_count = 0;
  2131. /* Attach FC transport template */
  2132. rc = bnx2fc_attach_transport();
  2133. if (rc)
  2134. goto detach_ft;
  2135. bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
  2136. if (!bnx2fc_wq) {
  2137. rc = -ENOMEM;
  2138. goto release_bt;
  2139. }
  2140. bg = &bnx2fc_global;
  2141. skb_queue_head_init(&bg->fcoe_rx_list);
  2142. l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
  2143. (void *)bg,
  2144. "bnx2fc_l2_thread");
  2145. if (IS_ERR(l2_thread)) {
  2146. rc = PTR_ERR(l2_thread);
  2147. goto free_wq;
  2148. }
  2149. wake_up_process(l2_thread);
  2150. spin_lock_bh(&bg->fcoe_rx_list.lock);
  2151. bg->thread = l2_thread;
  2152. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  2153. for_each_possible_cpu(cpu) {
  2154. p = &per_cpu(bnx2fc_percpu, cpu);
  2155. INIT_LIST_HEAD(&p->work_list);
  2156. spin_lock_init(&p->fp_work_lock);
  2157. }
  2158. for_each_online_cpu(cpu) {
  2159. bnx2fc_percpu_thread_create(cpu);
  2160. }
  2161. /* Initialize per CPU interrupt thread */
  2162. register_hotcpu_notifier(&bnx2fc_cpu_notifier);
  2163. cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
  2164. return 0;
  2165. free_wq:
  2166. destroy_workqueue(bnx2fc_wq);
  2167. release_bt:
  2168. bnx2fc_release_transport();
  2169. detach_ft:
  2170. fcoe_transport_detach(&bnx2fc_transport);
  2171. out:
  2172. return rc;
  2173. }
  2174. static void __exit bnx2fc_mod_exit(void)
  2175. {
  2176. LIST_HEAD(to_be_deleted);
  2177. struct bnx2fc_hba *hba, *next;
  2178. struct fcoe_percpu_s *bg;
  2179. struct task_struct *l2_thread;
  2180. struct sk_buff *skb;
  2181. unsigned int cpu = 0;
  2182. /*
  2183. * NOTE: Since cnic calls register_driver routine rtnl_lock,
  2184. * it will have higher precedence than bnx2fc_dev_lock.
  2185. * unregister_device() cannot be called with bnx2fc_dev_lock
  2186. * held.
  2187. */
  2188. mutex_lock(&bnx2fc_dev_lock);
  2189. list_splice(&adapter_list, &to_be_deleted);
  2190. INIT_LIST_HEAD(&adapter_list);
  2191. adapter_count = 0;
  2192. mutex_unlock(&bnx2fc_dev_lock);
  2193. /* Unregister with cnic */
  2194. list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
  2195. list_del_init(&hba->list);
  2196. printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
  2197. hba);
  2198. bnx2fc_ulp_stop(hba);
  2199. /* unregister cnic device */
  2200. if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
  2201. &hba->reg_with_cnic))
  2202. hba->cnic->unregister_device(hba->cnic,
  2203. CNIC_ULP_FCOE);
  2204. bnx2fc_hba_destroy(hba);
  2205. }
  2206. cnic_unregister_driver(CNIC_ULP_FCOE);
  2207. /* Destroy global thread */
  2208. bg = &bnx2fc_global;
  2209. spin_lock_bh(&bg->fcoe_rx_list.lock);
  2210. l2_thread = bg->thread;
  2211. bg->thread = NULL;
  2212. while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
  2213. kfree_skb(skb);
  2214. spin_unlock_bh(&bg->fcoe_rx_list.lock);
  2215. if (l2_thread)
  2216. kthread_stop(l2_thread);
  2217. unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
  2218. /* Destroy per cpu threads */
  2219. for_each_online_cpu(cpu) {
  2220. bnx2fc_percpu_thread_destroy(cpu);
  2221. }
  2222. destroy_workqueue(bnx2fc_wq);
  2223. /*
  2224. * detach from scsi transport
  2225. * must happen after all destroys are done
  2226. */
  2227. bnx2fc_release_transport();
  2228. /* detach from fcoe transport */
  2229. fcoe_transport_detach(&bnx2fc_transport);
  2230. }
  2231. module_init(bnx2fc_mod_init);
  2232. module_exit(bnx2fc_mod_exit);
  2233. static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
  2234. .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
  2235. .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
  2236. .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
  2237. .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
  2238. .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
  2239. .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
  2240. .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
  2241. .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
  2242. .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
  2243. };
  2244. static struct fc_function_template bnx2fc_transport_function = {
  2245. .show_host_node_name = 1,
  2246. .show_host_port_name = 1,
  2247. .show_host_supported_classes = 1,
  2248. .show_host_supported_fc4s = 1,
  2249. .show_host_active_fc4s = 1,
  2250. .show_host_maxframe_size = 1,
  2251. .show_host_port_id = 1,
  2252. .show_host_supported_speeds = 1,
  2253. .get_host_speed = fc_get_host_speed,
  2254. .show_host_speed = 1,
  2255. .show_host_port_type = 1,
  2256. .get_host_port_state = fc_get_host_port_state,
  2257. .show_host_port_state = 1,
  2258. .show_host_symbolic_name = 1,
  2259. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  2260. sizeof(struct bnx2fc_rport)),
  2261. .show_rport_maxframe_size = 1,
  2262. .show_rport_supported_classes = 1,
  2263. .show_host_fabric_name = 1,
  2264. .show_starget_node_name = 1,
  2265. .show_starget_port_name = 1,
  2266. .show_starget_port_id = 1,
  2267. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  2268. .show_rport_dev_loss_tmo = 1,
  2269. .get_fc_host_stats = bnx2fc_get_host_stats,
  2270. .issue_fc_host_lip = bnx2fc_fcoe_reset,
  2271. .terminate_rport_io = fc_rport_terminate_io,
  2272. .vport_create = bnx2fc_vport_create,
  2273. .vport_delete = bnx2fc_vport_destroy,
  2274. .vport_disable = bnx2fc_vport_disable,
  2275. .bsg_request = fc_lport_bsg_request,
  2276. };
  2277. static struct fc_function_template bnx2fc_vport_xport_function = {
  2278. .show_host_node_name = 1,
  2279. .show_host_port_name = 1,
  2280. .show_host_supported_classes = 1,
  2281. .show_host_supported_fc4s = 1,
  2282. .show_host_active_fc4s = 1,
  2283. .show_host_maxframe_size = 1,
  2284. .show_host_port_id = 1,
  2285. .show_host_supported_speeds = 1,
  2286. .get_host_speed = fc_get_host_speed,
  2287. .show_host_speed = 1,
  2288. .show_host_port_type = 1,
  2289. .get_host_port_state = fc_get_host_port_state,
  2290. .show_host_port_state = 1,
  2291. .show_host_symbolic_name = 1,
  2292. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  2293. sizeof(struct bnx2fc_rport)),
  2294. .show_rport_maxframe_size = 1,
  2295. .show_rport_supported_classes = 1,
  2296. .show_host_fabric_name = 1,
  2297. .show_starget_node_name = 1,
  2298. .show_starget_port_name = 1,
  2299. .show_starget_port_id = 1,
  2300. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  2301. .show_rport_dev_loss_tmo = 1,
  2302. .get_fc_host_stats = fc_get_host_stats,
  2303. .issue_fc_host_lip = bnx2fc_fcoe_reset,
  2304. .terminate_rport_io = fc_rport_terminate_io,
  2305. .bsg_request = fc_lport_bsg_request,
  2306. };
  2307. /**
  2308. * scsi_host_template structure used while registering with SCSI-ml
  2309. */
  2310. static struct scsi_host_template bnx2fc_shost_template = {
  2311. .module = THIS_MODULE,
  2312. .name = "Broadcom Offload FCoE Initiator",
  2313. .queuecommand = bnx2fc_queuecommand,
  2314. .eh_abort_handler = bnx2fc_eh_abort, /* abts */
  2315. .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
  2316. .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
  2317. .eh_host_reset_handler = fc_eh_host_reset,
  2318. .slave_alloc = fc_slave_alloc,
  2319. .change_queue_depth = fc_change_queue_depth,
  2320. .change_queue_type = fc_change_queue_type,
  2321. .this_id = -1,
  2322. .cmd_per_lun = 3,
  2323. .can_queue = BNX2FC_CAN_QUEUE,
  2324. .use_clustering = ENABLE_CLUSTERING,
  2325. .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
  2326. .max_sectors = 1024,
  2327. };
  2328. static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
  2329. .frame_send = bnx2fc_xmit,
  2330. .elsct_send = bnx2fc_elsct_send,
  2331. .fcp_abort_io = bnx2fc_abort_io,
  2332. .fcp_cleanup = bnx2fc_cleanup,
  2333. .get_lesb = fcoe_get_lesb,
  2334. .rport_event_callback = bnx2fc_rport_event_handler,
  2335. };
  2336. /**
  2337. * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
  2338. * structure carrying callback function pointers
  2339. */
  2340. static struct cnic_ulp_ops bnx2fc_cnic_cb = {
  2341. .owner = THIS_MODULE,
  2342. .cnic_init = bnx2fc_ulp_init,
  2343. .cnic_exit = bnx2fc_ulp_exit,
  2344. .cnic_start = bnx2fc_ulp_start,
  2345. .cnic_stop = bnx2fc_ulp_stop,
  2346. .indicate_kcqes = bnx2fc_indicate_kcqe,
  2347. .indicate_netevent = bnx2fc_indicate_netevent,
  2348. .cnic_get_stats = bnx2fc_ulp_get_stats,
  2349. };