bnx2fc_fcoe.c 68 KB

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