bnx2fc_fcoe.c 63 KB

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