bnx2fc_fcoe.c 62 KB

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