bnx2fc_fcoe.c 64 KB

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