bnx2fc_fcoe.c 64 KB

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