bnx2fc_fcoe.c 71 KB

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