bnx2fc_fcoe.c 63 KB

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