bnad.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887
  1. /*
  2. * Linux network driver for QLogic BR-series Converged Network Adapter.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License (GPL) Version 2 as
  6. * published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. /*
  14. * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
  15. * Copyright (c) 2014-2015 QLogic Corporation
  16. * All rights reserved
  17. * www.qlogic.com
  18. */
  19. #include <linux/bitops.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/in.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/ip.h>
  28. #include <linux/prefetch.h>
  29. #include <linux/module.h>
  30. #include "bnad.h"
  31. #include "bna.h"
  32. #include "cna.h"
  33. static DEFINE_MUTEX(bnad_fwimg_mutex);
  34. /*
  35. * Module params
  36. */
  37. static uint bnad_msix_disable;
  38. module_param(bnad_msix_disable, uint, 0444);
  39. MODULE_PARM_DESC(bnad_msix_disable, "Disable MSIX mode");
  40. static uint bnad_ioc_auto_recover = 1;
  41. module_param(bnad_ioc_auto_recover, uint, 0444);
  42. MODULE_PARM_DESC(bnad_ioc_auto_recover, "Enable / Disable auto recovery");
  43. static uint bna_debugfs_enable = 1;
  44. module_param(bna_debugfs_enable, uint, 0644);
  45. MODULE_PARM_DESC(bna_debugfs_enable, "Enables debugfs feature, default=1,"
  46. " Range[false:0|true:1]");
  47. /*
  48. * Global variables
  49. */
  50. static u32 bnad_rxqs_per_cq = 2;
  51. static atomic_t bna_id;
  52. static const u8 bnad_bcast_addr[] __aligned(2) =
  53. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  54. /*
  55. * Local MACROS
  56. */
  57. #define BNAD_GET_MBOX_IRQ(_bnad) \
  58. (((_bnad)->cfg_flags & BNAD_CF_MSIX) ? \
  59. ((_bnad)->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector) : \
  60. ((_bnad)->pcidev->irq))
  61. #define BNAD_FILL_UNMAPQ_MEM_REQ(_res_info, _num, _size) \
  62. do { \
  63. (_res_info)->res_type = BNA_RES_T_MEM; \
  64. (_res_info)->res_u.mem_info.mem_type = BNA_MEM_T_KVA; \
  65. (_res_info)->res_u.mem_info.num = (_num); \
  66. (_res_info)->res_u.mem_info.len = (_size); \
  67. } while (0)
  68. /*
  69. * Reinitialize completions in CQ, once Rx is taken down
  70. */
  71. static void
  72. bnad_cq_cleanup(struct bnad *bnad, struct bna_ccb *ccb)
  73. {
  74. struct bna_cq_entry *cmpl;
  75. int i;
  76. for (i = 0; i < ccb->q_depth; i++) {
  77. cmpl = &((struct bna_cq_entry *)ccb->sw_q)[i];
  78. cmpl->valid = 0;
  79. }
  80. }
  81. /* Tx Datapath functions */
  82. /* Caller should ensure that the entry at unmap_q[index] is valid */
  83. static u32
  84. bnad_tx_buff_unmap(struct bnad *bnad,
  85. struct bnad_tx_unmap *unmap_q,
  86. u32 q_depth, u32 index)
  87. {
  88. struct bnad_tx_unmap *unmap;
  89. struct sk_buff *skb;
  90. int vector, nvecs;
  91. unmap = &unmap_q[index];
  92. nvecs = unmap->nvecs;
  93. skb = unmap->skb;
  94. unmap->skb = NULL;
  95. unmap->nvecs = 0;
  96. dma_unmap_single(&bnad->pcidev->dev,
  97. dma_unmap_addr(&unmap->vectors[0], dma_addr),
  98. skb_headlen(skb), DMA_TO_DEVICE);
  99. dma_unmap_addr_set(&unmap->vectors[0], dma_addr, 0);
  100. nvecs--;
  101. vector = 0;
  102. while (nvecs) {
  103. vector++;
  104. if (vector == BFI_TX_MAX_VECTORS_PER_WI) {
  105. vector = 0;
  106. BNA_QE_INDX_INC(index, q_depth);
  107. unmap = &unmap_q[index];
  108. }
  109. dma_unmap_page(&bnad->pcidev->dev,
  110. dma_unmap_addr(&unmap->vectors[vector], dma_addr),
  111. dma_unmap_len(&unmap->vectors[vector], dma_len),
  112. DMA_TO_DEVICE);
  113. dma_unmap_addr_set(&unmap->vectors[vector], dma_addr, 0);
  114. nvecs--;
  115. }
  116. BNA_QE_INDX_INC(index, q_depth);
  117. return index;
  118. }
  119. /*
  120. * Frees all pending Tx Bufs
  121. * At this point no activity is expected on the Q,
  122. * so DMA unmap & freeing is fine.
  123. */
  124. static void
  125. bnad_txq_cleanup(struct bnad *bnad, struct bna_tcb *tcb)
  126. {
  127. struct bnad_tx_unmap *unmap_q = tcb->unmap_q;
  128. struct sk_buff *skb;
  129. int i;
  130. for (i = 0; i < tcb->q_depth; i++) {
  131. skb = unmap_q[i].skb;
  132. if (!skb)
  133. continue;
  134. bnad_tx_buff_unmap(bnad, unmap_q, tcb->q_depth, i);
  135. dev_kfree_skb_any(skb);
  136. }
  137. }
  138. /*
  139. * bnad_txcmpl_process : Frees the Tx bufs on Tx completion
  140. * Can be called in a) Interrupt context
  141. * b) Sending context
  142. */
  143. static u32
  144. bnad_txcmpl_process(struct bnad *bnad, struct bna_tcb *tcb)
  145. {
  146. u32 sent_packets = 0, sent_bytes = 0;
  147. u32 wis, unmap_wis, hw_cons, cons, q_depth;
  148. struct bnad_tx_unmap *unmap_q = tcb->unmap_q;
  149. struct bnad_tx_unmap *unmap;
  150. struct sk_buff *skb;
  151. /* Just return if TX is stopped */
  152. if (!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
  153. return 0;
  154. hw_cons = *(tcb->hw_consumer_index);
  155. rmb();
  156. cons = tcb->consumer_index;
  157. q_depth = tcb->q_depth;
  158. wis = BNA_Q_INDEX_CHANGE(cons, hw_cons, q_depth);
  159. BUG_ON(!(wis <= BNA_QE_IN_USE_CNT(tcb, tcb->q_depth)));
  160. while (wis) {
  161. unmap = &unmap_q[cons];
  162. skb = unmap->skb;
  163. sent_packets++;
  164. sent_bytes += skb->len;
  165. unmap_wis = BNA_TXQ_WI_NEEDED(unmap->nvecs);
  166. wis -= unmap_wis;
  167. cons = bnad_tx_buff_unmap(bnad, unmap_q, q_depth, cons);
  168. dev_kfree_skb_any(skb);
  169. }
  170. /* Update consumer pointers. */
  171. tcb->consumer_index = hw_cons;
  172. tcb->txq->tx_packets += sent_packets;
  173. tcb->txq->tx_bytes += sent_bytes;
  174. return sent_packets;
  175. }
  176. static u32
  177. bnad_tx_complete(struct bnad *bnad, struct bna_tcb *tcb)
  178. {
  179. struct net_device *netdev = bnad->netdev;
  180. u32 sent = 0;
  181. if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags))
  182. return 0;
  183. sent = bnad_txcmpl_process(bnad, tcb);
  184. if (sent) {
  185. if (netif_queue_stopped(netdev) &&
  186. netif_carrier_ok(netdev) &&
  187. BNA_QE_FREE_CNT(tcb, tcb->q_depth) >=
  188. BNAD_NETIF_WAKE_THRESHOLD) {
  189. if (test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)) {
  190. netif_wake_queue(netdev);
  191. BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
  192. }
  193. }
  194. }
  195. if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  196. bna_ib_ack(tcb->i_dbell, sent);
  197. smp_mb__before_atomic();
  198. clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
  199. return sent;
  200. }
  201. /* MSIX Tx Completion Handler */
  202. static irqreturn_t
  203. bnad_msix_tx(int irq, void *data)
  204. {
  205. struct bna_tcb *tcb = (struct bna_tcb *)data;
  206. struct bnad *bnad = tcb->bnad;
  207. bnad_tx_complete(bnad, tcb);
  208. return IRQ_HANDLED;
  209. }
  210. static inline void
  211. bnad_rxq_alloc_uninit(struct bnad *bnad, struct bna_rcb *rcb)
  212. {
  213. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  214. unmap_q->reuse_pi = -1;
  215. unmap_q->alloc_order = -1;
  216. unmap_q->map_size = 0;
  217. unmap_q->type = BNAD_RXBUF_NONE;
  218. }
  219. /* Default is page-based allocation. Multi-buffer support - TBD */
  220. static int
  221. bnad_rxq_alloc_init(struct bnad *bnad, struct bna_rcb *rcb)
  222. {
  223. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  224. int order;
  225. bnad_rxq_alloc_uninit(bnad, rcb);
  226. order = get_order(rcb->rxq->buffer_size);
  227. unmap_q->type = BNAD_RXBUF_PAGE;
  228. if (bna_is_small_rxq(rcb->id)) {
  229. unmap_q->alloc_order = 0;
  230. unmap_q->map_size = rcb->rxq->buffer_size;
  231. } else {
  232. if (rcb->rxq->multi_buffer) {
  233. unmap_q->alloc_order = 0;
  234. unmap_q->map_size = rcb->rxq->buffer_size;
  235. unmap_q->type = BNAD_RXBUF_MULTI_BUFF;
  236. } else {
  237. unmap_q->alloc_order = order;
  238. unmap_q->map_size =
  239. (rcb->rxq->buffer_size > 2048) ?
  240. PAGE_SIZE << order : 2048;
  241. }
  242. }
  243. BUG_ON((PAGE_SIZE << order) % unmap_q->map_size);
  244. return 0;
  245. }
  246. static inline void
  247. bnad_rxq_cleanup_page(struct bnad *bnad, struct bnad_rx_unmap *unmap)
  248. {
  249. if (!unmap->page)
  250. return;
  251. dma_unmap_page(&bnad->pcidev->dev,
  252. dma_unmap_addr(&unmap->vector, dma_addr),
  253. unmap->vector.len, DMA_FROM_DEVICE);
  254. put_page(unmap->page);
  255. unmap->page = NULL;
  256. dma_unmap_addr_set(&unmap->vector, dma_addr, 0);
  257. unmap->vector.len = 0;
  258. }
  259. static inline void
  260. bnad_rxq_cleanup_skb(struct bnad *bnad, struct bnad_rx_unmap *unmap)
  261. {
  262. if (!unmap->skb)
  263. return;
  264. dma_unmap_single(&bnad->pcidev->dev,
  265. dma_unmap_addr(&unmap->vector, dma_addr),
  266. unmap->vector.len, DMA_FROM_DEVICE);
  267. dev_kfree_skb_any(unmap->skb);
  268. unmap->skb = NULL;
  269. dma_unmap_addr_set(&unmap->vector, dma_addr, 0);
  270. unmap->vector.len = 0;
  271. }
  272. static void
  273. bnad_rxq_cleanup(struct bnad *bnad, struct bna_rcb *rcb)
  274. {
  275. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  276. int i;
  277. for (i = 0; i < rcb->q_depth; i++) {
  278. struct bnad_rx_unmap *unmap = &unmap_q->unmap[i];
  279. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  280. bnad_rxq_cleanup_skb(bnad, unmap);
  281. else
  282. bnad_rxq_cleanup_page(bnad, unmap);
  283. }
  284. bnad_rxq_alloc_uninit(bnad, rcb);
  285. }
  286. static u32
  287. bnad_rxq_refill_page(struct bnad *bnad, struct bna_rcb *rcb, u32 nalloc)
  288. {
  289. u32 alloced, prod, q_depth;
  290. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  291. struct bnad_rx_unmap *unmap, *prev;
  292. struct bna_rxq_entry *rxent;
  293. struct page *page;
  294. u32 page_offset, alloc_size;
  295. dma_addr_t dma_addr;
  296. prod = rcb->producer_index;
  297. q_depth = rcb->q_depth;
  298. alloc_size = PAGE_SIZE << unmap_q->alloc_order;
  299. alloced = 0;
  300. while (nalloc--) {
  301. unmap = &unmap_q->unmap[prod];
  302. if (unmap_q->reuse_pi < 0) {
  303. page = alloc_pages(GFP_ATOMIC | __GFP_COMP,
  304. unmap_q->alloc_order);
  305. page_offset = 0;
  306. } else {
  307. prev = &unmap_q->unmap[unmap_q->reuse_pi];
  308. page = prev->page;
  309. page_offset = prev->page_offset + unmap_q->map_size;
  310. get_page(page);
  311. }
  312. if (unlikely(!page)) {
  313. BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
  314. rcb->rxq->rxbuf_alloc_failed++;
  315. goto finishing;
  316. }
  317. dma_addr = dma_map_page(&bnad->pcidev->dev, page, page_offset,
  318. unmap_q->map_size, DMA_FROM_DEVICE);
  319. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  320. put_page(page);
  321. BNAD_UPDATE_CTR(bnad, rxbuf_map_failed);
  322. rcb->rxq->rxbuf_map_failed++;
  323. goto finishing;
  324. }
  325. unmap->page = page;
  326. unmap->page_offset = page_offset;
  327. dma_unmap_addr_set(&unmap->vector, dma_addr, dma_addr);
  328. unmap->vector.len = unmap_q->map_size;
  329. page_offset += unmap_q->map_size;
  330. if (page_offset < alloc_size)
  331. unmap_q->reuse_pi = prod;
  332. else
  333. unmap_q->reuse_pi = -1;
  334. rxent = &((struct bna_rxq_entry *)rcb->sw_q)[prod];
  335. BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
  336. BNA_QE_INDX_INC(prod, q_depth);
  337. alloced++;
  338. }
  339. finishing:
  340. if (likely(alloced)) {
  341. rcb->producer_index = prod;
  342. smp_mb();
  343. if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
  344. bna_rxq_prod_indx_doorbell(rcb);
  345. }
  346. return alloced;
  347. }
  348. static u32
  349. bnad_rxq_refill_skb(struct bnad *bnad, struct bna_rcb *rcb, u32 nalloc)
  350. {
  351. u32 alloced, prod, q_depth, buff_sz;
  352. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  353. struct bnad_rx_unmap *unmap;
  354. struct bna_rxq_entry *rxent;
  355. struct sk_buff *skb;
  356. dma_addr_t dma_addr;
  357. buff_sz = rcb->rxq->buffer_size;
  358. prod = rcb->producer_index;
  359. q_depth = rcb->q_depth;
  360. alloced = 0;
  361. while (nalloc--) {
  362. unmap = &unmap_q->unmap[prod];
  363. skb = netdev_alloc_skb_ip_align(bnad->netdev, buff_sz);
  364. if (unlikely(!skb)) {
  365. BNAD_UPDATE_CTR(bnad, rxbuf_alloc_failed);
  366. rcb->rxq->rxbuf_alloc_failed++;
  367. goto finishing;
  368. }
  369. dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
  370. buff_sz, DMA_FROM_DEVICE);
  371. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  372. dev_kfree_skb_any(skb);
  373. BNAD_UPDATE_CTR(bnad, rxbuf_map_failed);
  374. rcb->rxq->rxbuf_map_failed++;
  375. goto finishing;
  376. }
  377. unmap->skb = skb;
  378. dma_unmap_addr_set(&unmap->vector, dma_addr, dma_addr);
  379. unmap->vector.len = buff_sz;
  380. rxent = &((struct bna_rxq_entry *)rcb->sw_q)[prod];
  381. BNA_SET_DMA_ADDR(dma_addr, &rxent->host_addr);
  382. BNA_QE_INDX_INC(prod, q_depth);
  383. alloced++;
  384. }
  385. finishing:
  386. if (likely(alloced)) {
  387. rcb->producer_index = prod;
  388. smp_mb();
  389. if (likely(test_bit(BNAD_RXQ_POST_OK, &rcb->flags)))
  390. bna_rxq_prod_indx_doorbell(rcb);
  391. }
  392. return alloced;
  393. }
  394. static inline void
  395. bnad_rxq_post(struct bnad *bnad, struct bna_rcb *rcb)
  396. {
  397. struct bnad_rx_unmap_q *unmap_q = rcb->unmap_q;
  398. u32 to_alloc;
  399. to_alloc = BNA_QE_FREE_CNT(rcb, rcb->q_depth);
  400. if (!(to_alloc >> BNAD_RXQ_REFILL_THRESHOLD_SHIFT))
  401. return;
  402. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  403. bnad_rxq_refill_skb(bnad, rcb, to_alloc);
  404. else
  405. bnad_rxq_refill_page(bnad, rcb, to_alloc);
  406. }
  407. #define flags_cksum_prot_mask (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
  408. BNA_CQ_EF_IPV6 | \
  409. BNA_CQ_EF_TCP | BNA_CQ_EF_UDP | \
  410. BNA_CQ_EF_L4_CKSUM_OK)
  411. #define flags_tcp4 (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
  412. BNA_CQ_EF_TCP | BNA_CQ_EF_L4_CKSUM_OK)
  413. #define flags_tcp6 (BNA_CQ_EF_IPV6 | \
  414. BNA_CQ_EF_TCP | BNA_CQ_EF_L4_CKSUM_OK)
  415. #define flags_udp4 (BNA_CQ_EF_IPV4 | BNA_CQ_EF_L3_CKSUM_OK | \
  416. BNA_CQ_EF_UDP | BNA_CQ_EF_L4_CKSUM_OK)
  417. #define flags_udp6 (BNA_CQ_EF_IPV6 | \
  418. BNA_CQ_EF_UDP | BNA_CQ_EF_L4_CKSUM_OK)
  419. static void
  420. bnad_cq_drop_packet(struct bnad *bnad, struct bna_rcb *rcb,
  421. u32 sop_ci, u32 nvecs)
  422. {
  423. struct bnad_rx_unmap_q *unmap_q;
  424. struct bnad_rx_unmap *unmap;
  425. u32 ci, vec;
  426. unmap_q = rcb->unmap_q;
  427. for (vec = 0, ci = sop_ci; vec < nvecs; vec++) {
  428. unmap = &unmap_q->unmap[ci];
  429. BNA_QE_INDX_INC(ci, rcb->q_depth);
  430. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  431. bnad_rxq_cleanup_skb(bnad, unmap);
  432. else
  433. bnad_rxq_cleanup_page(bnad, unmap);
  434. }
  435. }
  436. static void
  437. bnad_cq_setup_skb_frags(struct bna_ccb *ccb, struct sk_buff *skb, u32 nvecs)
  438. {
  439. struct bna_rcb *rcb;
  440. struct bnad *bnad;
  441. struct bnad_rx_unmap_q *unmap_q;
  442. struct bna_cq_entry *cq, *cmpl;
  443. u32 ci, pi, totlen = 0;
  444. cq = ccb->sw_q;
  445. pi = ccb->producer_index;
  446. cmpl = &cq[pi];
  447. rcb = bna_is_small_rxq(cmpl->rxq_id) ? ccb->rcb[1] : ccb->rcb[0];
  448. unmap_q = rcb->unmap_q;
  449. bnad = rcb->bnad;
  450. ci = rcb->consumer_index;
  451. /* prefetch header */
  452. prefetch(page_address(unmap_q->unmap[ci].page) +
  453. unmap_q->unmap[ci].page_offset);
  454. while (nvecs--) {
  455. struct bnad_rx_unmap *unmap;
  456. u32 len;
  457. unmap = &unmap_q->unmap[ci];
  458. BNA_QE_INDX_INC(ci, rcb->q_depth);
  459. dma_unmap_page(&bnad->pcidev->dev,
  460. dma_unmap_addr(&unmap->vector, dma_addr),
  461. unmap->vector.len, DMA_FROM_DEVICE);
  462. len = ntohs(cmpl->length);
  463. skb->truesize += unmap->vector.len;
  464. totlen += len;
  465. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  466. unmap->page, unmap->page_offset, len);
  467. unmap->page = NULL;
  468. unmap->vector.len = 0;
  469. BNA_QE_INDX_INC(pi, ccb->q_depth);
  470. cmpl = &cq[pi];
  471. }
  472. skb->len += totlen;
  473. skb->data_len += totlen;
  474. }
  475. static inline void
  476. bnad_cq_setup_skb(struct bnad *bnad, struct sk_buff *skb,
  477. struct bnad_rx_unmap *unmap, u32 len)
  478. {
  479. prefetch(skb->data);
  480. dma_unmap_single(&bnad->pcidev->dev,
  481. dma_unmap_addr(&unmap->vector, dma_addr),
  482. unmap->vector.len, DMA_FROM_DEVICE);
  483. skb_put(skb, len);
  484. skb->protocol = eth_type_trans(skb, bnad->netdev);
  485. unmap->skb = NULL;
  486. unmap->vector.len = 0;
  487. }
  488. static u32
  489. bnad_cq_process(struct bnad *bnad, struct bna_ccb *ccb, int budget)
  490. {
  491. struct bna_cq_entry *cq, *cmpl, *next_cmpl;
  492. struct bna_rcb *rcb = NULL;
  493. struct bnad_rx_unmap_q *unmap_q;
  494. struct bnad_rx_unmap *unmap = NULL;
  495. struct sk_buff *skb = NULL;
  496. struct bna_pkt_rate *pkt_rt = &ccb->pkt_rate;
  497. struct bnad_rx_ctrl *rx_ctrl = ccb->ctrl;
  498. u32 packets = 0, len = 0, totlen = 0;
  499. u32 pi, vec, sop_ci = 0, nvecs = 0;
  500. u32 flags, masked_flags;
  501. prefetch(bnad->netdev);
  502. cq = ccb->sw_q;
  503. while (packets < budget) {
  504. cmpl = &cq[ccb->producer_index];
  505. if (!cmpl->valid)
  506. break;
  507. /* The 'valid' field is set by the adapter, only after writing
  508. * the other fields of completion entry. Hence, do not load
  509. * other fields of completion entry *before* the 'valid' is
  510. * loaded. Adding the rmb() here prevents the compiler and/or
  511. * CPU from reordering the reads which would potentially result
  512. * in reading stale values in completion entry.
  513. */
  514. rmb();
  515. BNA_UPDATE_PKT_CNT(pkt_rt, ntohs(cmpl->length));
  516. if (bna_is_small_rxq(cmpl->rxq_id))
  517. rcb = ccb->rcb[1];
  518. else
  519. rcb = ccb->rcb[0];
  520. unmap_q = rcb->unmap_q;
  521. /* start of packet ci */
  522. sop_ci = rcb->consumer_index;
  523. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type)) {
  524. unmap = &unmap_q->unmap[sop_ci];
  525. skb = unmap->skb;
  526. } else {
  527. skb = napi_get_frags(&rx_ctrl->napi);
  528. if (unlikely(!skb))
  529. break;
  530. }
  531. prefetch(skb);
  532. flags = ntohl(cmpl->flags);
  533. len = ntohs(cmpl->length);
  534. totlen = len;
  535. nvecs = 1;
  536. /* Check all the completions for this frame.
  537. * busy-wait doesn't help much, break here.
  538. */
  539. if (BNAD_RXBUF_IS_MULTI_BUFF(unmap_q->type) &&
  540. (flags & BNA_CQ_EF_EOP) == 0) {
  541. pi = ccb->producer_index;
  542. do {
  543. BNA_QE_INDX_INC(pi, ccb->q_depth);
  544. next_cmpl = &cq[pi];
  545. if (!next_cmpl->valid)
  546. break;
  547. /* The 'valid' field is set by the adapter, only
  548. * after writing the other fields of completion
  549. * entry. Hence, do not load other fields of
  550. * completion entry *before* the 'valid' is
  551. * loaded. Adding the rmb() here prevents the
  552. * compiler and/or CPU from reordering the reads
  553. * which would potentially result in reading
  554. * stale values in completion entry.
  555. */
  556. rmb();
  557. len = ntohs(next_cmpl->length);
  558. flags = ntohl(next_cmpl->flags);
  559. nvecs++;
  560. totlen += len;
  561. } while ((flags & BNA_CQ_EF_EOP) == 0);
  562. if (!next_cmpl->valid)
  563. break;
  564. }
  565. packets++;
  566. /* TODO: BNA_CQ_EF_LOCAL ? */
  567. if (unlikely(flags & (BNA_CQ_EF_MAC_ERROR |
  568. BNA_CQ_EF_FCS_ERROR |
  569. BNA_CQ_EF_TOO_LONG))) {
  570. bnad_cq_drop_packet(bnad, rcb, sop_ci, nvecs);
  571. rcb->rxq->rx_packets_with_error++;
  572. goto next;
  573. }
  574. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  575. bnad_cq_setup_skb(bnad, skb, unmap, len);
  576. else
  577. bnad_cq_setup_skb_frags(ccb, skb, nvecs);
  578. rcb->rxq->rx_packets++;
  579. rcb->rxq->rx_bytes += totlen;
  580. ccb->bytes_per_intr += totlen;
  581. masked_flags = flags & flags_cksum_prot_mask;
  582. if (likely
  583. ((bnad->netdev->features & NETIF_F_RXCSUM) &&
  584. ((masked_flags == flags_tcp4) ||
  585. (masked_flags == flags_udp4) ||
  586. (masked_flags == flags_tcp6) ||
  587. (masked_flags == flags_udp6))))
  588. skb->ip_summed = CHECKSUM_UNNECESSARY;
  589. else
  590. skb_checksum_none_assert(skb);
  591. if ((flags & BNA_CQ_EF_VLAN) &&
  592. (bnad->netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  593. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), ntohs(cmpl->vlan_tag));
  594. if (BNAD_RXBUF_IS_SK_BUFF(unmap_q->type))
  595. netif_receive_skb(skb);
  596. else
  597. napi_gro_frags(&rx_ctrl->napi);
  598. next:
  599. BNA_QE_INDX_ADD(rcb->consumer_index, nvecs, rcb->q_depth);
  600. for (vec = 0; vec < nvecs; vec++) {
  601. cmpl = &cq[ccb->producer_index];
  602. cmpl->valid = 0;
  603. BNA_QE_INDX_INC(ccb->producer_index, ccb->q_depth);
  604. }
  605. }
  606. napi_gro_flush(&rx_ctrl->napi, false);
  607. if (likely(test_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags)))
  608. bna_ib_ack_disable_irq(ccb->i_dbell, packets);
  609. bnad_rxq_post(bnad, ccb->rcb[0]);
  610. if (ccb->rcb[1])
  611. bnad_rxq_post(bnad, ccb->rcb[1]);
  612. return packets;
  613. }
  614. static void
  615. bnad_netif_rx_schedule_poll(struct bnad *bnad, struct bna_ccb *ccb)
  616. {
  617. struct bnad_rx_ctrl *rx_ctrl = (struct bnad_rx_ctrl *)(ccb->ctrl);
  618. struct napi_struct *napi = &rx_ctrl->napi;
  619. if (likely(napi_schedule_prep(napi))) {
  620. __napi_schedule(napi);
  621. rx_ctrl->rx_schedule++;
  622. }
  623. }
  624. /* MSIX Rx Path Handler */
  625. static irqreturn_t
  626. bnad_msix_rx(int irq, void *data)
  627. {
  628. struct bna_ccb *ccb = (struct bna_ccb *)data;
  629. if (ccb) {
  630. ((struct bnad_rx_ctrl *)ccb->ctrl)->rx_intr_ctr++;
  631. bnad_netif_rx_schedule_poll(ccb->bnad, ccb);
  632. }
  633. return IRQ_HANDLED;
  634. }
  635. /* Interrupt handlers */
  636. /* Mbox Interrupt Handlers */
  637. static irqreturn_t
  638. bnad_msix_mbox_handler(int irq, void *data)
  639. {
  640. u32 intr_status;
  641. unsigned long flags;
  642. struct bnad *bnad = (struct bnad *)data;
  643. spin_lock_irqsave(&bnad->bna_lock, flags);
  644. if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
  645. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  646. return IRQ_HANDLED;
  647. }
  648. bna_intr_status_get(&bnad->bna, intr_status);
  649. if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
  650. bna_mbox_handler(&bnad->bna, intr_status);
  651. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  652. return IRQ_HANDLED;
  653. }
  654. static irqreturn_t
  655. bnad_isr(int irq, void *data)
  656. {
  657. int i, j;
  658. u32 intr_status;
  659. unsigned long flags;
  660. struct bnad *bnad = (struct bnad *)data;
  661. struct bnad_rx_info *rx_info;
  662. struct bnad_rx_ctrl *rx_ctrl;
  663. struct bna_tcb *tcb = NULL;
  664. spin_lock_irqsave(&bnad->bna_lock, flags);
  665. if (unlikely(test_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags))) {
  666. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  667. return IRQ_NONE;
  668. }
  669. bna_intr_status_get(&bnad->bna, intr_status);
  670. if (unlikely(!intr_status)) {
  671. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  672. return IRQ_NONE;
  673. }
  674. if (BNA_IS_MBOX_ERR_INTR(&bnad->bna, intr_status))
  675. bna_mbox_handler(&bnad->bna, intr_status);
  676. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  677. if (!BNA_IS_INTX_DATA_INTR(intr_status))
  678. return IRQ_HANDLED;
  679. /* Process data interrupts */
  680. /* Tx processing */
  681. for (i = 0; i < bnad->num_tx; i++) {
  682. for (j = 0; j < bnad->num_txq_per_tx; j++) {
  683. tcb = bnad->tx_info[i].tcb[j];
  684. if (tcb && test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))
  685. bnad_tx_complete(bnad, bnad->tx_info[i].tcb[j]);
  686. }
  687. }
  688. /* Rx processing */
  689. for (i = 0; i < bnad->num_rx; i++) {
  690. rx_info = &bnad->rx_info[i];
  691. if (!rx_info->rx)
  692. continue;
  693. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  694. rx_ctrl = &rx_info->rx_ctrl[j];
  695. if (rx_ctrl->ccb)
  696. bnad_netif_rx_schedule_poll(bnad,
  697. rx_ctrl->ccb);
  698. }
  699. }
  700. return IRQ_HANDLED;
  701. }
  702. /*
  703. * Called in interrupt / callback context
  704. * with bna_lock held, so cfg_flags access is OK
  705. */
  706. static void
  707. bnad_enable_mbox_irq(struct bnad *bnad)
  708. {
  709. clear_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
  710. BNAD_UPDATE_CTR(bnad, mbox_intr_enabled);
  711. }
  712. /*
  713. * Called with bnad->bna_lock held b'cos of
  714. * bnad->cfg_flags access.
  715. */
  716. static void
  717. bnad_disable_mbox_irq(struct bnad *bnad)
  718. {
  719. set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
  720. BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
  721. }
  722. static void
  723. bnad_set_netdev_perm_addr(struct bnad *bnad)
  724. {
  725. struct net_device *netdev = bnad->netdev;
  726. ether_addr_copy(netdev->perm_addr, bnad->perm_addr);
  727. if (is_zero_ether_addr(netdev->dev_addr))
  728. ether_addr_copy(netdev->dev_addr, bnad->perm_addr);
  729. }
  730. /* Control Path Handlers */
  731. /* Callbacks */
  732. void
  733. bnad_cb_mbox_intr_enable(struct bnad *bnad)
  734. {
  735. bnad_enable_mbox_irq(bnad);
  736. }
  737. void
  738. bnad_cb_mbox_intr_disable(struct bnad *bnad)
  739. {
  740. bnad_disable_mbox_irq(bnad);
  741. }
  742. void
  743. bnad_cb_ioceth_ready(struct bnad *bnad)
  744. {
  745. bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
  746. complete(&bnad->bnad_completions.ioc_comp);
  747. }
  748. void
  749. bnad_cb_ioceth_failed(struct bnad *bnad)
  750. {
  751. bnad->bnad_completions.ioc_comp_status = BNA_CB_FAIL;
  752. complete(&bnad->bnad_completions.ioc_comp);
  753. }
  754. void
  755. bnad_cb_ioceth_disabled(struct bnad *bnad)
  756. {
  757. bnad->bnad_completions.ioc_comp_status = BNA_CB_SUCCESS;
  758. complete(&bnad->bnad_completions.ioc_comp);
  759. }
  760. static void
  761. bnad_cb_enet_disabled(void *arg)
  762. {
  763. struct bnad *bnad = (struct bnad *)arg;
  764. netif_carrier_off(bnad->netdev);
  765. complete(&bnad->bnad_completions.enet_comp);
  766. }
  767. void
  768. bnad_cb_ethport_link_status(struct bnad *bnad,
  769. enum bna_link_status link_status)
  770. {
  771. bool link_up = false;
  772. link_up = (link_status == BNA_LINK_UP) || (link_status == BNA_CEE_UP);
  773. if (link_status == BNA_CEE_UP) {
  774. if (!test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
  775. BNAD_UPDATE_CTR(bnad, cee_toggle);
  776. set_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
  777. } else {
  778. if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags))
  779. BNAD_UPDATE_CTR(bnad, cee_toggle);
  780. clear_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags);
  781. }
  782. if (link_up) {
  783. if (!netif_carrier_ok(bnad->netdev)) {
  784. uint tx_id, tcb_id;
  785. netdev_info(bnad->netdev, "link up\n");
  786. netif_carrier_on(bnad->netdev);
  787. BNAD_UPDATE_CTR(bnad, link_toggle);
  788. for (tx_id = 0; tx_id < bnad->num_tx; tx_id++) {
  789. for (tcb_id = 0; tcb_id < bnad->num_txq_per_tx;
  790. tcb_id++) {
  791. struct bna_tcb *tcb =
  792. bnad->tx_info[tx_id].tcb[tcb_id];
  793. u32 txq_id;
  794. if (!tcb)
  795. continue;
  796. txq_id = tcb->id;
  797. if (test_bit(BNAD_TXQ_TX_STARTED,
  798. &tcb->flags)) {
  799. /*
  800. * Force an immediate
  801. * Transmit Schedule */
  802. netif_wake_subqueue(
  803. bnad->netdev,
  804. txq_id);
  805. BNAD_UPDATE_CTR(bnad,
  806. netif_queue_wakeup);
  807. } else {
  808. netif_stop_subqueue(
  809. bnad->netdev,
  810. txq_id);
  811. BNAD_UPDATE_CTR(bnad,
  812. netif_queue_stop);
  813. }
  814. }
  815. }
  816. }
  817. } else {
  818. if (netif_carrier_ok(bnad->netdev)) {
  819. netdev_info(bnad->netdev, "link down\n");
  820. netif_carrier_off(bnad->netdev);
  821. BNAD_UPDATE_CTR(bnad, link_toggle);
  822. }
  823. }
  824. }
  825. static void
  826. bnad_cb_tx_disabled(void *arg, struct bna_tx *tx)
  827. {
  828. struct bnad *bnad = (struct bnad *)arg;
  829. complete(&bnad->bnad_completions.tx_comp);
  830. }
  831. static void
  832. bnad_cb_tcb_setup(struct bnad *bnad, struct bna_tcb *tcb)
  833. {
  834. struct bnad_tx_info *tx_info =
  835. (struct bnad_tx_info *)tcb->txq->tx->priv;
  836. tcb->priv = tcb;
  837. tx_info->tcb[tcb->id] = tcb;
  838. }
  839. static void
  840. bnad_cb_tcb_destroy(struct bnad *bnad, struct bna_tcb *tcb)
  841. {
  842. struct bnad_tx_info *tx_info =
  843. (struct bnad_tx_info *)tcb->txq->tx->priv;
  844. tx_info->tcb[tcb->id] = NULL;
  845. tcb->priv = NULL;
  846. }
  847. static void
  848. bnad_cb_ccb_setup(struct bnad *bnad, struct bna_ccb *ccb)
  849. {
  850. struct bnad_rx_info *rx_info =
  851. (struct bnad_rx_info *)ccb->cq->rx->priv;
  852. rx_info->rx_ctrl[ccb->id].ccb = ccb;
  853. ccb->ctrl = &rx_info->rx_ctrl[ccb->id];
  854. }
  855. static void
  856. bnad_cb_ccb_destroy(struct bnad *bnad, struct bna_ccb *ccb)
  857. {
  858. struct bnad_rx_info *rx_info =
  859. (struct bnad_rx_info *)ccb->cq->rx->priv;
  860. rx_info->rx_ctrl[ccb->id].ccb = NULL;
  861. }
  862. static void
  863. bnad_cb_tx_stall(struct bnad *bnad, struct bna_tx *tx)
  864. {
  865. struct bnad_tx_info *tx_info =
  866. (struct bnad_tx_info *)tx->priv;
  867. struct bna_tcb *tcb;
  868. u32 txq_id;
  869. int i;
  870. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  871. tcb = tx_info->tcb[i];
  872. if (!tcb)
  873. continue;
  874. txq_id = tcb->id;
  875. clear_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
  876. netif_stop_subqueue(bnad->netdev, txq_id);
  877. }
  878. }
  879. static void
  880. bnad_cb_tx_resume(struct bnad *bnad, struct bna_tx *tx)
  881. {
  882. struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
  883. struct bna_tcb *tcb;
  884. u32 txq_id;
  885. int i;
  886. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  887. tcb = tx_info->tcb[i];
  888. if (!tcb)
  889. continue;
  890. txq_id = tcb->id;
  891. BUG_ON(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags));
  892. set_bit(BNAD_TXQ_TX_STARTED, &tcb->flags);
  893. BUG_ON(*(tcb->hw_consumer_index) != 0);
  894. if (netif_carrier_ok(bnad->netdev)) {
  895. netif_wake_subqueue(bnad->netdev, txq_id);
  896. BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
  897. }
  898. }
  899. /*
  900. * Workaround for first ioceth enable failure & we
  901. * get a 0 MAC address. We try to get the MAC address
  902. * again here.
  903. */
  904. if (is_zero_ether_addr(bnad->perm_addr)) {
  905. bna_enet_perm_mac_get(&bnad->bna.enet, bnad->perm_addr);
  906. bnad_set_netdev_perm_addr(bnad);
  907. }
  908. }
  909. /*
  910. * Free all TxQs buffers and then notify TX_E_CLEANUP_DONE to Tx fsm.
  911. */
  912. static void
  913. bnad_tx_cleanup(struct delayed_work *work)
  914. {
  915. struct bnad_tx_info *tx_info =
  916. container_of(work, struct bnad_tx_info, tx_cleanup_work);
  917. struct bnad *bnad = NULL;
  918. struct bna_tcb *tcb;
  919. unsigned long flags;
  920. u32 i, pending = 0;
  921. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  922. tcb = tx_info->tcb[i];
  923. if (!tcb)
  924. continue;
  925. bnad = tcb->bnad;
  926. if (test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
  927. pending++;
  928. continue;
  929. }
  930. bnad_txq_cleanup(bnad, tcb);
  931. smp_mb__before_atomic();
  932. clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
  933. }
  934. if (pending) {
  935. queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work,
  936. msecs_to_jiffies(1));
  937. return;
  938. }
  939. spin_lock_irqsave(&bnad->bna_lock, flags);
  940. bna_tx_cleanup_complete(tx_info->tx);
  941. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  942. }
  943. static void
  944. bnad_cb_tx_cleanup(struct bnad *bnad, struct bna_tx *tx)
  945. {
  946. struct bnad_tx_info *tx_info = (struct bnad_tx_info *)tx->priv;
  947. struct bna_tcb *tcb;
  948. int i;
  949. for (i = 0; i < BNAD_MAX_TXQ_PER_TX; i++) {
  950. tcb = tx_info->tcb[i];
  951. if (!tcb)
  952. continue;
  953. }
  954. queue_delayed_work(bnad->work_q, &tx_info->tx_cleanup_work, 0);
  955. }
  956. static void
  957. bnad_cb_rx_stall(struct bnad *bnad, struct bna_rx *rx)
  958. {
  959. struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
  960. struct bna_ccb *ccb;
  961. struct bnad_rx_ctrl *rx_ctrl;
  962. int i;
  963. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  964. rx_ctrl = &rx_info->rx_ctrl[i];
  965. ccb = rx_ctrl->ccb;
  966. if (!ccb)
  967. continue;
  968. clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[0]->flags);
  969. if (ccb->rcb[1])
  970. clear_bit(BNAD_RXQ_POST_OK, &ccb->rcb[1]->flags);
  971. }
  972. }
  973. /*
  974. * Free all RxQs buffers and then notify RX_E_CLEANUP_DONE to Rx fsm.
  975. */
  976. static void
  977. bnad_rx_cleanup(void *work)
  978. {
  979. struct bnad_rx_info *rx_info =
  980. container_of(work, struct bnad_rx_info, rx_cleanup_work);
  981. struct bnad_rx_ctrl *rx_ctrl;
  982. struct bnad *bnad = NULL;
  983. unsigned long flags;
  984. u32 i;
  985. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  986. rx_ctrl = &rx_info->rx_ctrl[i];
  987. if (!rx_ctrl->ccb)
  988. continue;
  989. bnad = rx_ctrl->ccb->bnad;
  990. /*
  991. * Wait till the poll handler has exited
  992. * and nothing can be scheduled anymore
  993. */
  994. napi_disable(&rx_ctrl->napi);
  995. bnad_cq_cleanup(bnad, rx_ctrl->ccb);
  996. bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[0]);
  997. if (rx_ctrl->ccb->rcb[1])
  998. bnad_rxq_cleanup(bnad, rx_ctrl->ccb->rcb[1]);
  999. }
  1000. spin_lock_irqsave(&bnad->bna_lock, flags);
  1001. bna_rx_cleanup_complete(rx_info->rx);
  1002. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1003. }
  1004. static void
  1005. bnad_cb_rx_cleanup(struct bnad *bnad, struct bna_rx *rx)
  1006. {
  1007. struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
  1008. struct bna_ccb *ccb;
  1009. struct bnad_rx_ctrl *rx_ctrl;
  1010. int i;
  1011. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  1012. rx_ctrl = &rx_info->rx_ctrl[i];
  1013. ccb = rx_ctrl->ccb;
  1014. if (!ccb)
  1015. continue;
  1016. clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[0]->flags);
  1017. if (ccb->rcb[1])
  1018. clear_bit(BNAD_RXQ_STARTED, &ccb->rcb[1]->flags);
  1019. }
  1020. queue_work(bnad->work_q, &rx_info->rx_cleanup_work);
  1021. }
  1022. static void
  1023. bnad_cb_rx_post(struct bnad *bnad, struct bna_rx *rx)
  1024. {
  1025. struct bnad_rx_info *rx_info = (struct bnad_rx_info *)rx->priv;
  1026. struct bna_ccb *ccb;
  1027. struct bna_rcb *rcb;
  1028. struct bnad_rx_ctrl *rx_ctrl;
  1029. int i, j;
  1030. for (i = 0; i < BNAD_MAX_RXP_PER_RX; i++) {
  1031. rx_ctrl = &rx_info->rx_ctrl[i];
  1032. ccb = rx_ctrl->ccb;
  1033. if (!ccb)
  1034. continue;
  1035. napi_enable(&rx_ctrl->napi);
  1036. for (j = 0; j < BNAD_MAX_RXQ_PER_RXP; j++) {
  1037. rcb = ccb->rcb[j];
  1038. if (!rcb)
  1039. continue;
  1040. bnad_rxq_alloc_init(bnad, rcb);
  1041. set_bit(BNAD_RXQ_STARTED, &rcb->flags);
  1042. set_bit(BNAD_RXQ_POST_OK, &rcb->flags);
  1043. bnad_rxq_post(bnad, rcb);
  1044. }
  1045. }
  1046. }
  1047. static void
  1048. bnad_cb_rx_disabled(void *arg, struct bna_rx *rx)
  1049. {
  1050. struct bnad *bnad = (struct bnad *)arg;
  1051. complete(&bnad->bnad_completions.rx_comp);
  1052. }
  1053. static void
  1054. bnad_cb_rx_mcast_add(struct bnad *bnad, struct bna_rx *rx)
  1055. {
  1056. bnad->bnad_completions.mcast_comp_status = BNA_CB_SUCCESS;
  1057. complete(&bnad->bnad_completions.mcast_comp);
  1058. }
  1059. void
  1060. bnad_cb_stats_get(struct bnad *bnad, enum bna_cb_status status,
  1061. struct bna_stats *stats)
  1062. {
  1063. if (status == BNA_CB_SUCCESS)
  1064. BNAD_UPDATE_CTR(bnad, hw_stats_updates);
  1065. if (!netif_running(bnad->netdev) ||
  1066. !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
  1067. return;
  1068. mod_timer(&bnad->stats_timer,
  1069. jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
  1070. }
  1071. static void
  1072. bnad_cb_enet_mtu_set(struct bnad *bnad)
  1073. {
  1074. bnad->bnad_completions.mtu_comp_status = BNA_CB_SUCCESS;
  1075. complete(&bnad->bnad_completions.mtu_comp);
  1076. }
  1077. void
  1078. bnad_cb_completion(void *arg, enum bfa_status status)
  1079. {
  1080. struct bnad_iocmd_comp *iocmd_comp =
  1081. (struct bnad_iocmd_comp *)arg;
  1082. iocmd_comp->comp_status = (u32) status;
  1083. complete(&iocmd_comp->comp);
  1084. }
  1085. /* Resource allocation, free functions */
  1086. static void
  1087. bnad_mem_free(struct bnad *bnad,
  1088. struct bna_mem_info *mem_info)
  1089. {
  1090. int i;
  1091. dma_addr_t dma_pa;
  1092. if (mem_info->mdl == NULL)
  1093. return;
  1094. for (i = 0; i < mem_info->num; i++) {
  1095. if (mem_info->mdl[i].kva != NULL) {
  1096. if (mem_info->mem_type == BNA_MEM_T_DMA) {
  1097. BNA_GET_DMA_ADDR(&(mem_info->mdl[i].dma),
  1098. dma_pa);
  1099. dma_free_coherent(&bnad->pcidev->dev,
  1100. mem_info->mdl[i].len,
  1101. mem_info->mdl[i].kva, dma_pa);
  1102. } else
  1103. kfree(mem_info->mdl[i].kva);
  1104. }
  1105. }
  1106. kfree(mem_info->mdl);
  1107. mem_info->mdl = NULL;
  1108. }
  1109. static int
  1110. bnad_mem_alloc(struct bnad *bnad,
  1111. struct bna_mem_info *mem_info)
  1112. {
  1113. int i;
  1114. dma_addr_t dma_pa;
  1115. if ((mem_info->num == 0) || (mem_info->len == 0)) {
  1116. mem_info->mdl = NULL;
  1117. return 0;
  1118. }
  1119. mem_info->mdl = kcalloc(mem_info->num, sizeof(struct bna_mem_descr),
  1120. GFP_KERNEL);
  1121. if (mem_info->mdl == NULL)
  1122. return -ENOMEM;
  1123. if (mem_info->mem_type == BNA_MEM_T_DMA) {
  1124. for (i = 0; i < mem_info->num; i++) {
  1125. mem_info->mdl[i].len = mem_info->len;
  1126. mem_info->mdl[i].kva =
  1127. dma_alloc_coherent(&bnad->pcidev->dev,
  1128. mem_info->len, &dma_pa,
  1129. GFP_KERNEL);
  1130. if (mem_info->mdl[i].kva == NULL)
  1131. goto err_return;
  1132. BNA_SET_DMA_ADDR(dma_pa,
  1133. &(mem_info->mdl[i].dma));
  1134. }
  1135. } else {
  1136. for (i = 0; i < mem_info->num; i++) {
  1137. mem_info->mdl[i].len = mem_info->len;
  1138. mem_info->mdl[i].kva = kzalloc(mem_info->len,
  1139. GFP_KERNEL);
  1140. if (mem_info->mdl[i].kva == NULL)
  1141. goto err_return;
  1142. }
  1143. }
  1144. return 0;
  1145. err_return:
  1146. bnad_mem_free(bnad, mem_info);
  1147. return -ENOMEM;
  1148. }
  1149. /* Free IRQ for Mailbox */
  1150. static void
  1151. bnad_mbox_irq_free(struct bnad *bnad)
  1152. {
  1153. int irq;
  1154. unsigned long flags;
  1155. spin_lock_irqsave(&bnad->bna_lock, flags);
  1156. bnad_disable_mbox_irq(bnad);
  1157. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1158. irq = BNAD_GET_MBOX_IRQ(bnad);
  1159. free_irq(irq, bnad);
  1160. }
  1161. /*
  1162. * Allocates IRQ for Mailbox, but keep it disabled
  1163. * This will be enabled once we get the mbox enable callback
  1164. * from bna
  1165. */
  1166. static int
  1167. bnad_mbox_irq_alloc(struct bnad *bnad)
  1168. {
  1169. int err = 0;
  1170. unsigned long irq_flags, flags;
  1171. u32 irq;
  1172. irq_handler_t irq_handler;
  1173. spin_lock_irqsave(&bnad->bna_lock, flags);
  1174. if (bnad->cfg_flags & BNAD_CF_MSIX) {
  1175. irq_handler = (irq_handler_t)bnad_msix_mbox_handler;
  1176. irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
  1177. irq_flags = 0;
  1178. } else {
  1179. irq_handler = (irq_handler_t)bnad_isr;
  1180. irq = bnad->pcidev->irq;
  1181. irq_flags = IRQF_SHARED;
  1182. }
  1183. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1184. sprintf(bnad->mbox_irq_name, "%s", BNAD_NAME);
  1185. /*
  1186. * Set the Mbox IRQ disable flag, so that the IRQ handler
  1187. * called from request_irq() for SHARED IRQs do not execute
  1188. */
  1189. set_bit(BNAD_RF_MBOX_IRQ_DISABLED, &bnad->run_flags);
  1190. BNAD_UPDATE_CTR(bnad, mbox_intr_disabled);
  1191. err = request_irq(irq, irq_handler, irq_flags,
  1192. bnad->mbox_irq_name, bnad);
  1193. return err;
  1194. }
  1195. static void
  1196. bnad_txrx_irq_free(struct bnad *bnad, struct bna_intr_info *intr_info)
  1197. {
  1198. kfree(intr_info->idl);
  1199. intr_info->idl = NULL;
  1200. }
  1201. /* Allocates Interrupt Descriptor List for MSIX/INT-X vectors */
  1202. static int
  1203. bnad_txrx_irq_alloc(struct bnad *bnad, enum bnad_intr_source src,
  1204. u32 txrx_id, struct bna_intr_info *intr_info)
  1205. {
  1206. int i, vector_start = 0;
  1207. u32 cfg_flags;
  1208. unsigned long flags;
  1209. spin_lock_irqsave(&bnad->bna_lock, flags);
  1210. cfg_flags = bnad->cfg_flags;
  1211. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1212. if (cfg_flags & BNAD_CF_MSIX) {
  1213. intr_info->intr_type = BNA_INTR_T_MSIX;
  1214. intr_info->idl = kcalloc(intr_info->num,
  1215. sizeof(struct bna_intr_descr),
  1216. GFP_KERNEL);
  1217. if (!intr_info->idl)
  1218. return -ENOMEM;
  1219. switch (src) {
  1220. case BNAD_INTR_TX:
  1221. vector_start = BNAD_MAILBOX_MSIX_VECTORS + txrx_id;
  1222. break;
  1223. case BNAD_INTR_RX:
  1224. vector_start = BNAD_MAILBOX_MSIX_VECTORS +
  1225. (bnad->num_tx * bnad->num_txq_per_tx) +
  1226. txrx_id;
  1227. break;
  1228. default:
  1229. BUG();
  1230. }
  1231. for (i = 0; i < intr_info->num; i++)
  1232. intr_info->idl[i].vector = vector_start + i;
  1233. } else {
  1234. intr_info->intr_type = BNA_INTR_T_INTX;
  1235. intr_info->num = 1;
  1236. intr_info->idl = kcalloc(intr_info->num,
  1237. sizeof(struct bna_intr_descr),
  1238. GFP_KERNEL);
  1239. if (!intr_info->idl)
  1240. return -ENOMEM;
  1241. switch (src) {
  1242. case BNAD_INTR_TX:
  1243. intr_info->idl[0].vector = BNAD_INTX_TX_IB_BITMASK;
  1244. break;
  1245. case BNAD_INTR_RX:
  1246. intr_info->idl[0].vector = BNAD_INTX_RX_IB_BITMASK;
  1247. break;
  1248. }
  1249. }
  1250. return 0;
  1251. }
  1252. /* NOTE: Should be called for MSIX only
  1253. * Unregisters Tx MSIX vector(s) from the kernel
  1254. */
  1255. static void
  1256. bnad_tx_msix_unregister(struct bnad *bnad, struct bnad_tx_info *tx_info,
  1257. int num_txqs)
  1258. {
  1259. int i;
  1260. int vector_num;
  1261. for (i = 0; i < num_txqs; i++) {
  1262. if (tx_info->tcb[i] == NULL)
  1263. continue;
  1264. vector_num = tx_info->tcb[i]->intr_vector;
  1265. free_irq(bnad->msix_table[vector_num].vector, tx_info->tcb[i]);
  1266. }
  1267. }
  1268. /* NOTE: Should be called for MSIX only
  1269. * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
  1270. */
  1271. static int
  1272. bnad_tx_msix_register(struct bnad *bnad, struct bnad_tx_info *tx_info,
  1273. u32 tx_id, int num_txqs)
  1274. {
  1275. int i;
  1276. int err;
  1277. int vector_num;
  1278. for (i = 0; i < num_txqs; i++) {
  1279. vector_num = tx_info->tcb[i]->intr_vector;
  1280. sprintf(tx_info->tcb[i]->name, "%s TXQ %d", bnad->netdev->name,
  1281. tx_id + tx_info->tcb[i]->id);
  1282. err = request_irq(bnad->msix_table[vector_num].vector,
  1283. (irq_handler_t)bnad_msix_tx, 0,
  1284. tx_info->tcb[i]->name,
  1285. tx_info->tcb[i]);
  1286. if (err)
  1287. goto err_return;
  1288. }
  1289. return 0;
  1290. err_return:
  1291. if (i > 0)
  1292. bnad_tx_msix_unregister(bnad, tx_info, (i - 1));
  1293. return -1;
  1294. }
  1295. /* NOTE: Should be called for MSIX only
  1296. * Unregisters Rx MSIX vector(s) from the kernel
  1297. */
  1298. static void
  1299. bnad_rx_msix_unregister(struct bnad *bnad, struct bnad_rx_info *rx_info,
  1300. int num_rxps)
  1301. {
  1302. int i;
  1303. int vector_num;
  1304. for (i = 0; i < num_rxps; i++) {
  1305. if (rx_info->rx_ctrl[i].ccb == NULL)
  1306. continue;
  1307. vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
  1308. free_irq(bnad->msix_table[vector_num].vector,
  1309. rx_info->rx_ctrl[i].ccb);
  1310. }
  1311. }
  1312. /* NOTE: Should be called for MSIX only
  1313. * Registers Tx MSIX vector(s) and ISR(s), cookie with the kernel
  1314. */
  1315. static int
  1316. bnad_rx_msix_register(struct bnad *bnad, struct bnad_rx_info *rx_info,
  1317. u32 rx_id, int num_rxps)
  1318. {
  1319. int i;
  1320. int err;
  1321. int vector_num;
  1322. for (i = 0; i < num_rxps; i++) {
  1323. vector_num = rx_info->rx_ctrl[i].ccb->intr_vector;
  1324. sprintf(rx_info->rx_ctrl[i].ccb->name, "%s CQ %d",
  1325. bnad->netdev->name,
  1326. rx_id + rx_info->rx_ctrl[i].ccb->id);
  1327. err = request_irq(bnad->msix_table[vector_num].vector,
  1328. (irq_handler_t)bnad_msix_rx, 0,
  1329. rx_info->rx_ctrl[i].ccb->name,
  1330. rx_info->rx_ctrl[i].ccb);
  1331. if (err)
  1332. goto err_return;
  1333. }
  1334. return 0;
  1335. err_return:
  1336. if (i > 0)
  1337. bnad_rx_msix_unregister(bnad, rx_info, (i - 1));
  1338. return -1;
  1339. }
  1340. /* Free Tx object Resources */
  1341. static void
  1342. bnad_tx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
  1343. {
  1344. int i;
  1345. for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
  1346. if (res_info[i].res_type == BNA_RES_T_MEM)
  1347. bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
  1348. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1349. bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
  1350. }
  1351. }
  1352. /* Allocates memory and interrupt resources for Tx object */
  1353. static int
  1354. bnad_tx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
  1355. u32 tx_id)
  1356. {
  1357. int i, err = 0;
  1358. for (i = 0; i < BNA_TX_RES_T_MAX; i++) {
  1359. if (res_info[i].res_type == BNA_RES_T_MEM)
  1360. err = bnad_mem_alloc(bnad,
  1361. &res_info[i].res_u.mem_info);
  1362. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1363. err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_TX, tx_id,
  1364. &res_info[i].res_u.intr_info);
  1365. if (err)
  1366. goto err_return;
  1367. }
  1368. return 0;
  1369. err_return:
  1370. bnad_tx_res_free(bnad, res_info);
  1371. return err;
  1372. }
  1373. /* Free Rx object Resources */
  1374. static void
  1375. bnad_rx_res_free(struct bnad *bnad, struct bna_res_info *res_info)
  1376. {
  1377. int i;
  1378. for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
  1379. if (res_info[i].res_type == BNA_RES_T_MEM)
  1380. bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
  1381. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1382. bnad_txrx_irq_free(bnad, &res_info[i].res_u.intr_info);
  1383. }
  1384. }
  1385. /* Allocates memory and interrupt resources for Rx object */
  1386. static int
  1387. bnad_rx_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
  1388. uint rx_id)
  1389. {
  1390. int i, err = 0;
  1391. /* All memory needs to be allocated before setup_ccbs */
  1392. for (i = 0; i < BNA_RX_RES_T_MAX; i++) {
  1393. if (res_info[i].res_type == BNA_RES_T_MEM)
  1394. err = bnad_mem_alloc(bnad,
  1395. &res_info[i].res_u.mem_info);
  1396. else if (res_info[i].res_type == BNA_RES_T_INTR)
  1397. err = bnad_txrx_irq_alloc(bnad, BNAD_INTR_RX, rx_id,
  1398. &res_info[i].res_u.intr_info);
  1399. if (err)
  1400. goto err_return;
  1401. }
  1402. return 0;
  1403. err_return:
  1404. bnad_rx_res_free(bnad, res_info);
  1405. return err;
  1406. }
  1407. /* Timer callbacks */
  1408. /* a) IOC timer */
  1409. static void
  1410. bnad_ioc_timeout(struct timer_list *t)
  1411. {
  1412. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.ioc_timer);
  1413. unsigned long flags;
  1414. spin_lock_irqsave(&bnad->bna_lock, flags);
  1415. bfa_nw_ioc_timeout(&bnad->bna.ioceth.ioc);
  1416. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1417. }
  1418. static void
  1419. bnad_ioc_hb_check(struct timer_list *t)
  1420. {
  1421. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.hb_timer);
  1422. unsigned long flags;
  1423. spin_lock_irqsave(&bnad->bna_lock, flags);
  1424. bfa_nw_ioc_hb_check(&bnad->bna.ioceth.ioc);
  1425. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1426. }
  1427. static void
  1428. bnad_iocpf_timeout(struct timer_list *t)
  1429. {
  1430. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.iocpf_timer);
  1431. unsigned long flags;
  1432. spin_lock_irqsave(&bnad->bna_lock, flags);
  1433. bfa_nw_iocpf_timeout(&bnad->bna.ioceth.ioc);
  1434. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1435. }
  1436. static void
  1437. bnad_iocpf_sem_timeout(struct timer_list *t)
  1438. {
  1439. struct bnad *bnad = from_timer(bnad, t, bna.ioceth.ioc.sem_timer);
  1440. unsigned long flags;
  1441. spin_lock_irqsave(&bnad->bna_lock, flags);
  1442. bfa_nw_iocpf_sem_timeout(&bnad->bna.ioceth.ioc);
  1443. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1444. }
  1445. /*
  1446. * All timer routines use bnad->bna_lock to protect against
  1447. * the following race, which may occur in case of no locking:
  1448. * Time CPU m CPU n
  1449. * 0 1 = test_bit
  1450. * 1 clear_bit
  1451. * 2 del_timer_sync
  1452. * 3 mod_timer
  1453. */
  1454. /* b) Dynamic Interrupt Moderation Timer */
  1455. static void
  1456. bnad_dim_timeout(struct timer_list *t)
  1457. {
  1458. struct bnad *bnad = from_timer(bnad, t, dim_timer);
  1459. struct bnad_rx_info *rx_info;
  1460. struct bnad_rx_ctrl *rx_ctrl;
  1461. int i, j;
  1462. unsigned long flags;
  1463. if (!netif_carrier_ok(bnad->netdev))
  1464. return;
  1465. spin_lock_irqsave(&bnad->bna_lock, flags);
  1466. for (i = 0; i < bnad->num_rx; i++) {
  1467. rx_info = &bnad->rx_info[i];
  1468. if (!rx_info->rx)
  1469. continue;
  1470. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  1471. rx_ctrl = &rx_info->rx_ctrl[j];
  1472. if (!rx_ctrl->ccb)
  1473. continue;
  1474. bna_rx_dim_update(rx_ctrl->ccb);
  1475. }
  1476. }
  1477. /* Check for BNAD_CF_DIM_ENABLED, does not eleminate a race */
  1478. if (test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags))
  1479. mod_timer(&bnad->dim_timer,
  1480. jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
  1481. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1482. }
  1483. /* c) Statistics Timer */
  1484. static void
  1485. bnad_stats_timeout(struct timer_list *t)
  1486. {
  1487. struct bnad *bnad = from_timer(bnad, t, stats_timer);
  1488. unsigned long flags;
  1489. if (!netif_running(bnad->netdev) ||
  1490. !test_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
  1491. return;
  1492. spin_lock_irqsave(&bnad->bna_lock, flags);
  1493. bna_hw_stats_get(&bnad->bna);
  1494. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1495. }
  1496. /*
  1497. * Set up timer for DIM
  1498. * Called with bnad->bna_lock held
  1499. */
  1500. void
  1501. bnad_dim_timer_start(struct bnad *bnad)
  1502. {
  1503. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
  1504. !test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
  1505. timer_setup(&bnad->dim_timer, bnad_dim_timeout, 0);
  1506. set_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
  1507. mod_timer(&bnad->dim_timer,
  1508. jiffies + msecs_to_jiffies(BNAD_DIM_TIMER_FREQ));
  1509. }
  1510. }
  1511. /*
  1512. * Set up timer for statistics
  1513. * Called with mutex_lock(&bnad->conf_mutex) held
  1514. */
  1515. static void
  1516. bnad_stats_timer_start(struct bnad *bnad)
  1517. {
  1518. unsigned long flags;
  1519. spin_lock_irqsave(&bnad->bna_lock, flags);
  1520. if (!test_and_set_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags)) {
  1521. timer_setup(&bnad->stats_timer, bnad_stats_timeout, 0);
  1522. mod_timer(&bnad->stats_timer,
  1523. jiffies + msecs_to_jiffies(BNAD_STATS_TIMER_FREQ));
  1524. }
  1525. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1526. }
  1527. /*
  1528. * Stops the stats timer
  1529. * Called with mutex_lock(&bnad->conf_mutex) held
  1530. */
  1531. static void
  1532. bnad_stats_timer_stop(struct bnad *bnad)
  1533. {
  1534. int to_del = 0;
  1535. unsigned long flags;
  1536. spin_lock_irqsave(&bnad->bna_lock, flags);
  1537. if (test_and_clear_bit(BNAD_RF_STATS_TIMER_RUNNING, &bnad->run_flags))
  1538. to_del = 1;
  1539. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1540. if (to_del)
  1541. del_timer_sync(&bnad->stats_timer);
  1542. }
  1543. /* Utilities */
  1544. static void
  1545. bnad_netdev_mc_list_get(struct net_device *netdev, u8 *mc_list)
  1546. {
  1547. int i = 1; /* Index 0 has broadcast address */
  1548. struct netdev_hw_addr *mc_addr;
  1549. netdev_for_each_mc_addr(mc_addr, netdev) {
  1550. ether_addr_copy(&mc_list[i * ETH_ALEN], &mc_addr->addr[0]);
  1551. i++;
  1552. }
  1553. }
  1554. static int
  1555. bnad_napi_poll_rx(struct napi_struct *napi, int budget)
  1556. {
  1557. struct bnad_rx_ctrl *rx_ctrl =
  1558. container_of(napi, struct bnad_rx_ctrl, napi);
  1559. struct bnad *bnad = rx_ctrl->bnad;
  1560. int rcvd = 0;
  1561. rx_ctrl->rx_poll_ctr++;
  1562. if (!netif_carrier_ok(bnad->netdev))
  1563. goto poll_exit;
  1564. rcvd = bnad_cq_process(bnad, rx_ctrl->ccb, budget);
  1565. if (rcvd >= budget)
  1566. return rcvd;
  1567. poll_exit:
  1568. napi_complete_done(napi, rcvd);
  1569. rx_ctrl->rx_complete++;
  1570. if (rx_ctrl->ccb)
  1571. bnad_enable_rx_irq_unsafe(rx_ctrl->ccb);
  1572. return rcvd;
  1573. }
  1574. #define BNAD_NAPI_POLL_QUOTA 64
  1575. static void
  1576. bnad_napi_add(struct bnad *bnad, u32 rx_id)
  1577. {
  1578. struct bnad_rx_ctrl *rx_ctrl;
  1579. int i;
  1580. /* Initialize & enable NAPI */
  1581. for (i = 0; i < bnad->num_rxp_per_rx; i++) {
  1582. rx_ctrl = &bnad->rx_info[rx_id].rx_ctrl[i];
  1583. netif_napi_add(bnad->netdev, &rx_ctrl->napi,
  1584. bnad_napi_poll_rx, BNAD_NAPI_POLL_QUOTA);
  1585. }
  1586. }
  1587. static void
  1588. bnad_napi_delete(struct bnad *bnad, u32 rx_id)
  1589. {
  1590. int i;
  1591. /* First disable and then clean up */
  1592. for (i = 0; i < bnad->num_rxp_per_rx; i++)
  1593. netif_napi_del(&bnad->rx_info[rx_id].rx_ctrl[i].napi);
  1594. }
  1595. /* Should be held with conf_lock held */
  1596. void
  1597. bnad_destroy_tx(struct bnad *bnad, u32 tx_id)
  1598. {
  1599. struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
  1600. struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
  1601. unsigned long flags;
  1602. if (!tx_info->tx)
  1603. return;
  1604. init_completion(&bnad->bnad_completions.tx_comp);
  1605. spin_lock_irqsave(&bnad->bna_lock, flags);
  1606. bna_tx_disable(tx_info->tx, BNA_HARD_CLEANUP, bnad_cb_tx_disabled);
  1607. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1608. wait_for_completion(&bnad->bnad_completions.tx_comp);
  1609. if (tx_info->tcb[0]->intr_type == BNA_INTR_T_MSIX)
  1610. bnad_tx_msix_unregister(bnad, tx_info,
  1611. bnad->num_txq_per_tx);
  1612. spin_lock_irqsave(&bnad->bna_lock, flags);
  1613. bna_tx_destroy(tx_info->tx);
  1614. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1615. tx_info->tx = NULL;
  1616. tx_info->tx_id = 0;
  1617. bnad_tx_res_free(bnad, res_info);
  1618. }
  1619. /* Should be held with conf_lock held */
  1620. int
  1621. bnad_setup_tx(struct bnad *bnad, u32 tx_id)
  1622. {
  1623. int err;
  1624. struct bnad_tx_info *tx_info = &bnad->tx_info[tx_id];
  1625. struct bna_res_info *res_info = &bnad->tx_res_info[tx_id].res_info[0];
  1626. struct bna_intr_info *intr_info =
  1627. &res_info[BNA_TX_RES_INTR_T_TXCMPL].res_u.intr_info;
  1628. struct bna_tx_config *tx_config = &bnad->tx_config[tx_id];
  1629. static const struct bna_tx_event_cbfn tx_cbfn = {
  1630. .tcb_setup_cbfn = bnad_cb_tcb_setup,
  1631. .tcb_destroy_cbfn = bnad_cb_tcb_destroy,
  1632. .tx_stall_cbfn = bnad_cb_tx_stall,
  1633. .tx_resume_cbfn = bnad_cb_tx_resume,
  1634. .tx_cleanup_cbfn = bnad_cb_tx_cleanup,
  1635. };
  1636. struct bna_tx *tx;
  1637. unsigned long flags;
  1638. tx_info->tx_id = tx_id;
  1639. /* Initialize the Tx object configuration */
  1640. tx_config->num_txq = bnad->num_txq_per_tx;
  1641. tx_config->txq_depth = bnad->txq_depth;
  1642. tx_config->tx_type = BNA_TX_T_REGULAR;
  1643. tx_config->coalescing_timeo = bnad->tx_coalescing_timeo;
  1644. /* Get BNA's resource requirement for one tx object */
  1645. spin_lock_irqsave(&bnad->bna_lock, flags);
  1646. bna_tx_res_req(bnad->num_txq_per_tx,
  1647. bnad->txq_depth, res_info);
  1648. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1649. /* Fill Unmap Q memory requirements */
  1650. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_TX_RES_MEM_T_UNMAPQ],
  1651. bnad->num_txq_per_tx, (sizeof(struct bnad_tx_unmap) *
  1652. bnad->txq_depth));
  1653. /* Allocate resources */
  1654. err = bnad_tx_res_alloc(bnad, res_info, tx_id);
  1655. if (err)
  1656. return err;
  1657. /* Ask BNA to create one Tx object, supplying required resources */
  1658. spin_lock_irqsave(&bnad->bna_lock, flags);
  1659. tx = bna_tx_create(&bnad->bna, bnad, tx_config, &tx_cbfn, res_info,
  1660. tx_info);
  1661. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1662. if (!tx) {
  1663. err = -ENOMEM;
  1664. goto err_return;
  1665. }
  1666. tx_info->tx = tx;
  1667. INIT_DELAYED_WORK(&tx_info->tx_cleanup_work,
  1668. (work_func_t)bnad_tx_cleanup);
  1669. /* Register ISR for the Tx object */
  1670. if (intr_info->intr_type == BNA_INTR_T_MSIX) {
  1671. err = bnad_tx_msix_register(bnad, tx_info,
  1672. tx_id, bnad->num_txq_per_tx);
  1673. if (err)
  1674. goto cleanup_tx;
  1675. }
  1676. spin_lock_irqsave(&bnad->bna_lock, flags);
  1677. bna_tx_enable(tx);
  1678. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1679. return 0;
  1680. cleanup_tx:
  1681. spin_lock_irqsave(&bnad->bna_lock, flags);
  1682. bna_tx_destroy(tx_info->tx);
  1683. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1684. tx_info->tx = NULL;
  1685. tx_info->tx_id = 0;
  1686. err_return:
  1687. bnad_tx_res_free(bnad, res_info);
  1688. return err;
  1689. }
  1690. /* Setup the rx config for bna_rx_create */
  1691. /* bnad decides the configuration */
  1692. static void
  1693. bnad_init_rx_config(struct bnad *bnad, struct bna_rx_config *rx_config)
  1694. {
  1695. memset(rx_config, 0, sizeof(*rx_config));
  1696. rx_config->rx_type = BNA_RX_T_REGULAR;
  1697. rx_config->num_paths = bnad->num_rxp_per_rx;
  1698. rx_config->coalescing_timeo = bnad->rx_coalescing_timeo;
  1699. if (bnad->num_rxp_per_rx > 1) {
  1700. rx_config->rss_status = BNA_STATUS_T_ENABLED;
  1701. rx_config->rss_config.hash_type =
  1702. (BFI_ENET_RSS_IPV6 |
  1703. BFI_ENET_RSS_IPV6_TCP |
  1704. BFI_ENET_RSS_IPV4 |
  1705. BFI_ENET_RSS_IPV4_TCP);
  1706. rx_config->rss_config.hash_mask =
  1707. bnad->num_rxp_per_rx - 1;
  1708. netdev_rss_key_fill(rx_config->rss_config.toeplitz_hash_key,
  1709. sizeof(rx_config->rss_config.toeplitz_hash_key));
  1710. } else {
  1711. rx_config->rss_status = BNA_STATUS_T_DISABLED;
  1712. memset(&rx_config->rss_config, 0,
  1713. sizeof(rx_config->rss_config));
  1714. }
  1715. rx_config->frame_size = BNAD_FRAME_SIZE(bnad->netdev->mtu);
  1716. rx_config->q0_multi_buf = BNA_STATUS_T_DISABLED;
  1717. /* BNA_RXP_SINGLE - one data-buffer queue
  1718. * BNA_RXP_SLR - one small-buffer and one large-buffer queues
  1719. * BNA_RXP_HDS - one header-buffer and one data-buffer queues
  1720. */
  1721. /* TODO: configurable param for queue type */
  1722. rx_config->rxp_type = BNA_RXP_SLR;
  1723. if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
  1724. rx_config->frame_size > 4096) {
  1725. /* though size_routing_enable is set in SLR,
  1726. * small packets may get routed to same rxq.
  1727. * set buf_size to 2048 instead of PAGE_SIZE.
  1728. */
  1729. rx_config->q0_buf_size = 2048;
  1730. /* this should be in multiples of 2 */
  1731. rx_config->q0_num_vecs = 4;
  1732. rx_config->q0_depth = bnad->rxq_depth * rx_config->q0_num_vecs;
  1733. rx_config->q0_multi_buf = BNA_STATUS_T_ENABLED;
  1734. } else {
  1735. rx_config->q0_buf_size = rx_config->frame_size;
  1736. rx_config->q0_num_vecs = 1;
  1737. rx_config->q0_depth = bnad->rxq_depth;
  1738. }
  1739. /* initialize for q1 for BNA_RXP_SLR/BNA_RXP_HDS */
  1740. if (rx_config->rxp_type == BNA_RXP_SLR) {
  1741. rx_config->q1_depth = bnad->rxq_depth;
  1742. rx_config->q1_buf_size = BFI_SMALL_RXBUF_SIZE;
  1743. }
  1744. rx_config->vlan_strip_status =
  1745. (bnad->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) ?
  1746. BNA_STATUS_T_ENABLED : BNA_STATUS_T_DISABLED;
  1747. }
  1748. static void
  1749. bnad_rx_ctrl_init(struct bnad *bnad, u32 rx_id)
  1750. {
  1751. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1752. int i;
  1753. for (i = 0; i < bnad->num_rxp_per_rx; i++)
  1754. rx_info->rx_ctrl[i].bnad = bnad;
  1755. }
  1756. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1757. static u32
  1758. bnad_reinit_rx(struct bnad *bnad)
  1759. {
  1760. struct net_device *netdev = bnad->netdev;
  1761. u32 err = 0, current_err = 0;
  1762. u32 rx_id = 0, count = 0;
  1763. unsigned long flags;
  1764. /* destroy and create new rx objects */
  1765. for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
  1766. if (!bnad->rx_info[rx_id].rx)
  1767. continue;
  1768. bnad_destroy_rx(bnad, rx_id);
  1769. }
  1770. spin_lock_irqsave(&bnad->bna_lock, flags);
  1771. bna_enet_mtu_set(&bnad->bna.enet,
  1772. BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
  1773. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1774. for (rx_id = 0; rx_id < bnad->num_rx; rx_id++) {
  1775. count++;
  1776. current_err = bnad_setup_rx(bnad, rx_id);
  1777. if (current_err && !err) {
  1778. err = current_err;
  1779. netdev_err(netdev, "RXQ:%u setup failed\n", rx_id);
  1780. }
  1781. }
  1782. /* restore rx configuration */
  1783. if (bnad->rx_info[0].rx && !err) {
  1784. bnad_restore_vlans(bnad, 0);
  1785. bnad_enable_default_bcast(bnad);
  1786. spin_lock_irqsave(&bnad->bna_lock, flags);
  1787. bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
  1788. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1789. bnad_set_rx_mode(netdev);
  1790. }
  1791. return count;
  1792. }
  1793. /* Called with bnad_conf_lock() held */
  1794. void
  1795. bnad_destroy_rx(struct bnad *bnad, u32 rx_id)
  1796. {
  1797. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1798. struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
  1799. struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
  1800. unsigned long flags;
  1801. int to_del = 0;
  1802. if (!rx_info->rx)
  1803. return;
  1804. if (0 == rx_id) {
  1805. spin_lock_irqsave(&bnad->bna_lock, flags);
  1806. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED &&
  1807. test_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags)) {
  1808. clear_bit(BNAD_RF_DIM_TIMER_RUNNING, &bnad->run_flags);
  1809. to_del = 1;
  1810. }
  1811. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1812. if (to_del)
  1813. del_timer_sync(&bnad->dim_timer);
  1814. }
  1815. init_completion(&bnad->bnad_completions.rx_comp);
  1816. spin_lock_irqsave(&bnad->bna_lock, flags);
  1817. bna_rx_disable(rx_info->rx, BNA_HARD_CLEANUP, bnad_cb_rx_disabled);
  1818. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1819. wait_for_completion(&bnad->bnad_completions.rx_comp);
  1820. if (rx_info->rx_ctrl[0].ccb->intr_type == BNA_INTR_T_MSIX)
  1821. bnad_rx_msix_unregister(bnad, rx_info, rx_config->num_paths);
  1822. bnad_napi_delete(bnad, rx_id);
  1823. spin_lock_irqsave(&bnad->bna_lock, flags);
  1824. bna_rx_destroy(rx_info->rx);
  1825. rx_info->rx = NULL;
  1826. rx_info->rx_id = 0;
  1827. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1828. bnad_rx_res_free(bnad, res_info);
  1829. }
  1830. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1831. int
  1832. bnad_setup_rx(struct bnad *bnad, u32 rx_id)
  1833. {
  1834. int err;
  1835. struct bnad_rx_info *rx_info = &bnad->rx_info[rx_id];
  1836. struct bna_res_info *res_info = &bnad->rx_res_info[rx_id].res_info[0];
  1837. struct bna_intr_info *intr_info =
  1838. &res_info[BNA_RX_RES_T_INTR].res_u.intr_info;
  1839. struct bna_rx_config *rx_config = &bnad->rx_config[rx_id];
  1840. static const struct bna_rx_event_cbfn rx_cbfn = {
  1841. .rcb_setup_cbfn = NULL,
  1842. .rcb_destroy_cbfn = NULL,
  1843. .ccb_setup_cbfn = bnad_cb_ccb_setup,
  1844. .ccb_destroy_cbfn = bnad_cb_ccb_destroy,
  1845. .rx_stall_cbfn = bnad_cb_rx_stall,
  1846. .rx_cleanup_cbfn = bnad_cb_rx_cleanup,
  1847. .rx_post_cbfn = bnad_cb_rx_post,
  1848. };
  1849. struct bna_rx *rx;
  1850. unsigned long flags;
  1851. rx_info->rx_id = rx_id;
  1852. /* Initialize the Rx object configuration */
  1853. bnad_init_rx_config(bnad, rx_config);
  1854. /* Get BNA's resource requirement for one Rx object */
  1855. spin_lock_irqsave(&bnad->bna_lock, flags);
  1856. bna_rx_res_req(rx_config, res_info);
  1857. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1858. /* Fill Unmap Q memory requirements */
  1859. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPDQ],
  1860. rx_config->num_paths,
  1861. (rx_config->q0_depth *
  1862. sizeof(struct bnad_rx_unmap)) +
  1863. sizeof(struct bnad_rx_unmap_q));
  1864. if (rx_config->rxp_type != BNA_RXP_SINGLE) {
  1865. BNAD_FILL_UNMAPQ_MEM_REQ(&res_info[BNA_RX_RES_MEM_T_UNMAPHQ],
  1866. rx_config->num_paths,
  1867. (rx_config->q1_depth *
  1868. sizeof(struct bnad_rx_unmap) +
  1869. sizeof(struct bnad_rx_unmap_q)));
  1870. }
  1871. /* Allocate resource */
  1872. err = bnad_rx_res_alloc(bnad, res_info, rx_id);
  1873. if (err)
  1874. return err;
  1875. bnad_rx_ctrl_init(bnad, rx_id);
  1876. /* Ask BNA to create one Rx object, supplying required resources */
  1877. spin_lock_irqsave(&bnad->bna_lock, flags);
  1878. rx = bna_rx_create(&bnad->bna, bnad, rx_config, &rx_cbfn, res_info,
  1879. rx_info);
  1880. if (!rx) {
  1881. err = -ENOMEM;
  1882. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1883. goto err_return;
  1884. }
  1885. rx_info->rx = rx;
  1886. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1887. INIT_WORK(&rx_info->rx_cleanup_work,
  1888. (work_func_t)(bnad_rx_cleanup));
  1889. /*
  1890. * Init NAPI, so that state is set to NAPI_STATE_SCHED,
  1891. * so that IRQ handler cannot schedule NAPI at this point.
  1892. */
  1893. bnad_napi_add(bnad, rx_id);
  1894. /* Register ISR for the Rx object */
  1895. if (intr_info->intr_type == BNA_INTR_T_MSIX) {
  1896. err = bnad_rx_msix_register(bnad, rx_info, rx_id,
  1897. rx_config->num_paths);
  1898. if (err)
  1899. goto err_return;
  1900. }
  1901. spin_lock_irqsave(&bnad->bna_lock, flags);
  1902. if (0 == rx_id) {
  1903. /* Set up Dynamic Interrupt Moderation Vector */
  1904. if (bnad->cfg_flags & BNAD_CF_DIM_ENABLED)
  1905. bna_rx_dim_reconfig(&bnad->bna, bna_napi_dim_vector);
  1906. /* Enable VLAN filtering only on the default Rx */
  1907. bna_rx_vlanfilter_enable(rx);
  1908. /* Start the DIM timer */
  1909. bnad_dim_timer_start(bnad);
  1910. }
  1911. bna_rx_enable(rx);
  1912. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1913. return 0;
  1914. err_return:
  1915. bnad_destroy_rx(bnad, rx_id);
  1916. return err;
  1917. }
  1918. /* Called with conf_lock & bnad->bna_lock held */
  1919. void
  1920. bnad_tx_coalescing_timeo_set(struct bnad *bnad)
  1921. {
  1922. struct bnad_tx_info *tx_info;
  1923. tx_info = &bnad->tx_info[0];
  1924. if (!tx_info->tx)
  1925. return;
  1926. bna_tx_coalescing_timeo_set(tx_info->tx, bnad->tx_coalescing_timeo);
  1927. }
  1928. /* Called with conf_lock & bnad->bna_lock held */
  1929. void
  1930. bnad_rx_coalescing_timeo_set(struct bnad *bnad)
  1931. {
  1932. struct bnad_rx_info *rx_info;
  1933. int i;
  1934. for (i = 0; i < bnad->num_rx; i++) {
  1935. rx_info = &bnad->rx_info[i];
  1936. if (!rx_info->rx)
  1937. continue;
  1938. bna_rx_coalescing_timeo_set(rx_info->rx,
  1939. bnad->rx_coalescing_timeo);
  1940. }
  1941. }
  1942. /*
  1943. * Called with bnad->bna_lock held
  1944. */
  1945. int
  1946. bnad_mac_addr_set_locked(struct bnad *bnad, const u8 *mac_addr)
  1947. {
  1948. int ret;
  1949. if (!is_valid_ether_addr(mac_addr))
  1950. return -EADDRNOTAVAIL;
  1951. /* If datapath is down, pretend everything went through */
  1952. if (!bnad->rx_info[0].rx)
  1953. return 0;
  1954. ret = bna_rx_ucast_set(bnad->rx_info[0].rx, mac_addr);
  1955. if (ret != BNA_CB_SUCCESS)
  1956. return -EADDRNOTAVAIL;
  1957. return 0;
  1958. }
  1959. /* Should be called with conf_lock held */
  1960. int
  1961. bnad_enable_default_bcast(struct bnad *bnad)
  1962. {
  1963. struct bnad_rx_info *rx_info = &bnad->rx_info[0];
  1964. int ret;
  1965. unsigned long flags;
  1966. init_completion(&bnad->bnad_completions.mcast_comp);
  1967. spin_lock_irqsave(&bnad->bna_lock, flags);
  1968. ret = bna_rx_mcast_add(rx_info->rx, bnad_bcast_addr,
  1969. bnad_cb_rx_mcast_add);
  1970. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1971. if (ret == BNA_CB_SUCCESS)
  1972. wait_for_completion(&bnad->bnad_completions.mcast_comp);
  1973. else
  1974. return -ENODEV;
  1975. if (bnad->bnad_completions.mcast_comp_status != BNA_CB_SUCCESS)
  1976. return -ENODEV;
  1977. return 0;
  1978. }
  1979. /* Called with mutex_lock(&bnad->conf_mutex) held */
  1980. void
  1981. bnad_restore_vlans(struct bnad *bnad, u32 rx_id)
  1982. {
  1983. u16 vid;
  1984. unsigned long flags;
  1985. for_each_set_bit(vid, bnad->active_vlans, VLAN_N_VID) {
  1986. spin_lock_irqsave(&bnad->bna_lock, flags);
  1987. bna_rx_vlan_add(bnad->rx_info[rx_id].rx, vid);
  1988. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  1989. }
  1990. }
  1991. /* Statistics utilities */
  1992. void
  1993. bnad_netdev_qstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
  1994. {
  1995. int i, j;
  1996. for (i = 0; i < bnad->num_rx; i++) {
  1997. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  1998. if (bnad->rx_info[i].rx_ctrl[j].ccb) {
  1999. stats->rx_packets += bnad->rx_info[i].
  2000. rx_ctrl[j].ccb->rcb[0]->rxq->rx_packets;
  2001. stats->rx_bytes += bnad->rx_info[i].
  2002. rx_ctrl[j].ccb->rcb[0]->rxq->rx_bytes;
  2003. if (bnad->rx_info[i].rx_ctrl[j].ccb->rcb[1] &&
  2004. bnad->rx_info[i].rx_ctrl[j].ccb->
  2005. rcb[1]->rxq) {
  2006. stats->rx_packets +=
  2007. bnad->rx_info[i].rx_ctrl[j].
  2008. ccb->rcb[1]->rxq->rx_packets;
  2009. stats->rx_bytes +=
  2010. bnad->rx_info[i].rx_ctrl[j].
  2011. ccb->rcb[1]->rxq->rx_bytes;
  2012. }
  2013. }
  2014. }
  2015. }
  2016. for (i = 0; i < bnad->num_tx; i++) {
  2017. for (j = 0; j < bnad->num_txq_per_tx; j++) {
  2018. if (bnad->tx_info[i].tcb[j]) {
  2019. stats->tx_packets +=
  2020. bnad->tx_info[i].tcb[j]->txq->tx_packets;
  2021. stats->tx_bytes +=
  2022. bnad->tx_info[i].tcb[j]->txq->tx_bytes;
  2023. }
  2024. }
  2025. }
  2026. }
  2027. /*
  2028. * Must be called with the bna_lock held.
  2029. */
  2030. void
  2031. bnad_netdev_hwstats_fill(struct bnad *bnad, struct rtnl_link_stats64 *stats)
  2032. {
  2033. struct bfi_enet_stats_mac *mac_stats;
  2034. u32 bmap;
  2035. int i;
  2036. mac_stats = &bnad->stats.bna_stats->hw_stats.mac_stats;
  2037. stats->rx_errors =
  2038. mac_stats->rx_fcs_error + mac_stats->rx_alignment_error +
  2039. mac_stats->rx_frame_length_error + mac_stats->rx_code_error +
  2040. mac_stats->rx_undersize;
  2041. stats->tx_errors = mac_stats->tx_fcs_error +
  2042. mac_stats->tx_undersize;
  2043. stats->rx_dropped = mac_stats->rx_drop;
  2044. stats->tx_dropped = mac_stats->tx_drop;
  2045. stats->multicast = mac_stats->rx_multicast;
  2046. stats->collisions = mac_stats->tx_total_collision;
  2047. stats->rx_length_errors = mac_stats->rx_frame_length_error;
  2048. /* receive ring buffer overflow ?? */
  2049. stats->rx_crc_errors = mac_stats->rx_fcs_error;
  2050. stats->rx_frame_errors = mac_stats->rx_alignment_error;
  2051. /* recv'r fifo overrun */
  2052. bmap = bna_rx_rid_mask(&bnad->bna);
  2053. for (i = 0; bmap; i++) {
  2054. if (bmap & 1) {
  2055. stats->rx_fifo_errors +=
  2056. bnad->stats.bna_stats->
  2057. hw_stats.rxf_stats[i].frame_drops;
  2058. break;
  2059. }
  2060. bmap >>= 1;
  2061. }
  2062. }
  2063. static void
  2064. bnad_mbox_irq_sync(struct bnad *bnad)
  2065. {
  2066. u32 irq;
  2067. unsigned long flags;
  2068. spin_lock_irqsave(&bnad->bna_lock, flags);
  2069. if (bnad->cfg_flags & BNAD_CF_MSIX)
  2070. irq = bnad->msix_table[BNAD_MAILBOX_MSIX_INDEX].vector;
  2071. else
  2072. irq = bnad->pcidev->irq;
  2073. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2074. synchronize_irq(irq);
  2075. }
  2076. /* Utility used by bnad_start_xmit, for doing TSO */
  2077. static int
  2078. bnad_tso_prepare(struct bnad *bnad, struct sk_buff *skb)
  2079. {
  2080. int err;
  2081. err = skb_cow_head(skb, 0);
  2082. if (err < 0) {
  2083. BNAD_UPDATE_CTR(bnad, tso_err);
  2084. return err;
  2085. }
  2086. /*
  2087. * For TSO, the TCP checksum field is seeded with pseudo-header sum
  2088. * excluding the length field.
  2089. */
  2090. if (vlan_get_protocol(skb) == htons(ETH_P_IP)) {
  2091. struct iphdr *iph = ip_hdr(skb);
  2092. /* Do we really need these? */
  2093. iph->tot_len = 0;
  2094. iph->check = 0;
  2095. tcp_hdr(skb)->check =
  2096. ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
  2097. IPPROTO_TCP, 0);
  2098. BNAD_UPDATE_CTR(bnad, tso4);
  2099. } else {
  2100. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  2101. ipv6h->payload_len = 0;
  2102. tcp_hdr(skb)->check =
  2103. ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, 0,
  2104. IPPROTO_TCP, 0);
  2105. BNAD_UPDATE_CTR(bnad, tso6);
  2106. }
  2107. return 0;
  2108. }
  2109. /*
  2110. * Initialize Q numbers depending on Rx Paths
  2111. * Called with bnad->bna_lock held, because of cfg_flags
  2112. * access.
  2113. */
  2114. static void
  2115. bnad_q_num_init(struct bnad *bnad)
  2116. {
  2117. int rxps;
  2118. rxps = min((uint)num_online_cpus(),
  2119. (uint)(BNAD_MAX_RX * BNAD_MAX_RXP_PER_RX));
  2120. if (!(bnad->cfg_flags & BNAD_CF_MSIX))
  2121. rxps = 1; /* INTx */
  2122. bnad->num_rx = 1;
  2123. bnad->num_tx = 1;
  2124. bnad->num_rxp_per_rx = rxps;
  2125. bnad->num_txq_per_tx = BNAD_TXQ_NUM;
  2126. }
  2127. /*
  2128. * Adjusts the Q numbers, given a number of msix vectors
  2129. * Give preference to RSS as opposed to Tx priority Queues,
  2130. * in such a case, just use 1 Tx Q
  2131. * Called with bnad->bna_lock held b'cos of cfg_flags access
  2132. */
  2133. static void
  2134. bnad_q_num_adjust(struct bnad *bnad, int msix_vectors, int temp)
  2135. {
  2136. bnad->num_txq_per_tx = 1;
  2137. if ((msix_vectors >= (bnad->num_tx * bnad->num_txq_per_tx) +
  2138. bnad_rxqs_per_cq + BNAD_MAILBOX_MSIX_VECTORS) &&
  2139. (bnad->cfg_flags & BNAD_CF_MSIX)) {
  2140. bnad->num_rxp_per_rx = msix_vectors -
  2141. (bnad->num_tx * bnad->num_txq_per_tx) -
  2142. BNAD_MAILBOX_MSIX_VECTORS;
  2143. } else
  2144. bnad->num_rxp_per_rx = 1;
  2145. }
  2146. /* Enable / disable ioceth */
  2147. static int
  2148. bnad_ioceth_disable(struct bnad *bnad)
  2149. {
  2150. unsigned long flags;
  2151. int err = 0;
  2152. spin_lock_irqsave(&bnad->bna_lock, flags);
  2153. init_completion(&bnad->bnad_completions.ioc_comp);
  2154. bna_ioceth_disable(&bnad->bna.ioceth, BNA_HARD_CLEANUP);
  2155. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2156. wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
  2157. msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
  2158. err = bnad->bnad_completions.ioc_comp_status;
  2159. return err;
  2160. }
  2161. static int
  2162. bnad_ioceth_enable(struct bnad *bnad)
  2163. {
  2164. int err = 0;
  2165. unsigned long flags;
  2166. spin_lock_irqsave(&bnad->bna_lock, flags);
  2167. init_completion(&bnad->bnad_completions.ioc_comp);
  2168. bnad->bnad_completions.ioc_comp_status = BNA_CB_WAITING;
  2169. bna_ioceth_enable(&bnad->bna.ioceth);
  2170. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2171. wait_for_completion_timeout(&bnad->bnad_completions.ioc_comp,
  2172. msecs_to_jiffies(BNAD_IOCETH_TIMEOUT));
  2173. err = bnad->bnad_completions.ioc_comp_status;
  2174. return err;
  2175. }
  2176. /* Free BNA resources */
  2177. static void
  2178. bnad_res_free(struct bnad *bnad, struct bna_res_info *res_info,
  2179. u32 res_val_max)
  2180. {
  2181. int i;
  2182. for (i = 0; i < res_val_max; i++)
  2183. bnad_mem_free(bnad, &res_info[i].res_u.mem_info);
  2184. }
  2185. /* Allocates memory and interrupt resources for BNA */
  2186. static int
  2187. bnad_res_alloc(struct bnad *bnad, struct bna_res_info *res_info,
  2188. u32 res_val_max)
  2189. {
  2190. int i, err;
  2191. for (i = 0; i < res_val_max; i++) {
  2192. err = bnad_mem_alloc(bnad, &res_info[i].res_u.mem_info);
  2193. if (err)
  2194. goto err_return;
  2195. }
  2196. return 0;
  2197. err_return:
  2198. bnad_res_free(bnad, res_info, res_val_max);
  2199. return err;
  2200. }
  2201. /* Interrupt enable / disable */
  2202. static void
  2203. bnad_enable_msix(struct bnad *bnad)
  2204. {
  2205. int i, ret;
  2206. unsigned long flags;
  2207. spin_lock_irqsave(&bnad->bna_lock, flags);
  2208. if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
  2209. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2210. return;
  2211. }
  2212. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2213. if (bnad->msix_table)
  2214. return;
  2215. bnad->msix_table =
  2216. kcalloc(bnad->msix_num, sizeof(struct msix_entry), GFP_KERNEL);
  2217. if (!bnad->msix_table)
  2218. goto intx_mode;
  2219. for (i = 0; i < bnad->msix_num; i++)
  2220. bnad->msix_table[i].entry = i;
  2221. ret = pci_enable_msix_range(bnad->pcidev, bnad->msix_table,
  2222. 1, bnad->msix_num);
  2223. if (ret < 0) {
  2224. goto intx_mode;
  2225. } else if (ret < bnad->msix_num) {
  2226. dev_warn(&bnad->pcidev->dev,
  2227. "%d MSI-X vectors allocated < %d requested\n",
  2228. ret, bnad->msix_num);
  2229. spin_lock_irqsave(&bnad->bna_lock, flags);
  2230. /* ret = #of vectors that we got */
  2231. bnad_q_num_adjust(bnad, (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2,
  2232. (ret - BNAD_MAILBOX_MSIX_VECTORS) / 2);
  2233. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2234. bnad->msix_num = BNAD_NUM_TXQ + BNAD_NUM_RXP +
  2235. BNAD_MAILBOX_MSIX_VECTORS;
  2236. if (bnad->msix_num > ret) {
  2237. pci_disable_msix(bnad->pcidev);
  2238. goto intx_mode;
  2239. }
  2240. }
  2241. pci_intx(bnad->pcidev, 0);
  2242. return;
  2243. intx_mode:
  2244. dev_warn(&bnad->pcidev->dev,
  2245. "MSI-X enable failed - operating in INTx mode\n");
  2246. kfree(bnad->msix_table);
  2247. bnad->msix_table = NULL;
  2248. bnad->msix_num = 0;
  2249. spin_lock_irqsave(&bnad->bna_lock, flags);
  2250. bnad->cfg_flags &= ~BNAD_CF_MSIX;
  2251. bnad_q_num_init(bnad);
  2252. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2253. }
  2254. static void
  2255. bnad_disable_msix(struct bnad *bnad)
  2256. {
  2257. u32 cfg_flags;
  2258. unsigned long flags;
  2259. spin_lock_irqsave(&bnad->bna_lock, flags);
  2260. cfg_flags = bnad->cfg_flags;
  2261. if (bnad->cfg_flags & BNAD_CF_MSIX)
  2262. bnad->cfg_flags &= ~BNAD_CF_MSIX;
  2263. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2264. if (cfg_flags & BNAD_CF_MSIX) {
  2265. pci_disable_msix(bnad->pcidev);
  2266. kfree(bnad->msix_table);
  2267. bnad->msix_table = NULL;
  2268. }
  2269. }
  2270. /* Netdev entry points */
  2271. static int
  2272. bnad_open(struct net_device *netdev)
  2273. {
  2274. int err;
  2275. struct bnad *bnad = netdev_priv(netdev);
  2276. struct bna_pause_config pause_config;
  2277. unsigned long flags;
  2278. mutex_lock(&bnad->conf_mutex);
  2279. /* Tx */
  2280. err = bnad_setup_tx(bnad, 0);
  2281. if (err)
  2282. goto err_return;
  2283. /* Rx */
  2284. err = bnad_setup_rx(bnad, 0);
  2285. if (err)
  2286. goto cleanup_tx;
  2287. /* Port */
  2288. pause_config.tx_pause = 0;
  2289. pause_config.rx_pause = 0;
  2290. spin_lock_irqsave(&bnad->bna_lock, flags);
  2291. bna_enet_mtu_set(&bnad->bna.enet,
  2292. BNAD_FRAME_SIZE(bnad->netdev->mtu), NULL);
  2293. bna_enet_pause_config(&bnad->bna.enet, &pause_config);
  2294. bna_enet_enable(&bnad->bna.enet);
  2295. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2296. /* Enable broadcast */
  2297. bnad_enable_default_bcast(bnad);
  2298. /* Restore VLANs, if any */
  2299. bnad_restore_vlans(bnad, 0);
  2300. /* Set the UCAST address */
  2301. spin_lock_irqsave(&bnad->bna_lock, flags);
  2302. bnad_mac_addr_set_locked(bnad, netdev->dev_addr);
  2303. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2304. /* Start the stats timer */
  2305. bnad_stats_timer_start(bnad);
  2306. mutex_unlock(&bnad->conf_mutex);
  2307. return 0;
  2308. cleanup_tx:
  2309. bnad_destroy_tx(bnad, 0);
  2310. err_return:
  2311. mutex_unlock(&bnad->conf_mutex);
  2312. return err;
  2313. }
  2314. static int
  2315. bnad_stop(struct net_device *netdev)
  2316. {
  2317. struct bnad *bnad = netdev_priv(netdev);
  2318. unsigned long flags;
  2319. mutex_lock(&bnad->conf_mutex);
  2320. /* Stop the stats timer */
  2321. bnad_stats_timer_stop(bnad);
  2322. init_completion(&bnad->bnad_completions.enet_comp);
  2323. spin_lock_irqsave(&bnad->bna_lock, flags);
  2324. bna_enet_disable(&bnad->bna.enet, BNA_HARD_CLEANUP,
  2325. bnad_cb_enet_disabled);
  2326. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2327. wait_for_completion(&bnad->bnad_completions.enet_comp);
  2328. bnad_destroy_tx(bnad, 0);
  2329. bnad_destroy_rx(bnad, 0);
  2330. /* Synchronize mailbox IRQ */
  2331. bnad_mbox_irq_sync(bnad);
  2332. mutex_unlock(&bnad->conf_mutex);
  2333. return 0;
  2334. }
  2335. /* TX */
  2336. /* Returns 0 for success */
  2337. static int
  2338. bnad_txq_wi_prepare(struct bnad *bnad, struct bna_tcb *tcb,
  2339. struct sk_buff *skb, struct bna_txq_entry *txqent)
  2340. {
  2341. u16 flags = 0;
  2342. u32 gso_size;
  2343. u16 vlan_tag = 0;
  2344. if (skb_vlan_tag_present(skb)) {
  2345. vlan_tag = (u16)skb_vlan_tag_get(skb);
  2346. flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
  2347. }
  2348. if (test_bit(BNAD_RF_CEE_RUNNING, &bnad->run_flags)) {
  2349. vlan_tag = ((tcb->priority & 0x7) << VLAN_PRIO_SHIFT)
  2350. | (vlan_tag & 0x1fff);
  2351. flags |= (BNA_TXQ_WI_CF_INS_PRIO | BNA_TXQ_WI_CF_INS_VLAN);
  2352. }
  2353. txqent->hdr.wi.vlan_tag = htons(vlan_tag);
  2354. if (skb_is_gso(skb)) {
  2355. gso_size = skb_shinfo(skb)->gso_size;
  2356. if (unlikely(gso_size > bnad->netdev->mtu)) {
  2357. BNAD_UPDATE_CTR(bnad, tx_skb_mss_too_long);
  2358. return -EINVAL;
  2359. }
  2360. if (unlikely((gso_size + skb_transport_offset(skb) +
  2361. tcp_hdrlen(skb)) >= skb->len)) {
  2362. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND);
  2363. txqent->hdr.wi.lso_mss = 0;
  2364. BNAD_UPDATE_CTR(bnad, tx_skb_tso_too_short);
  2365. } else {
  2366. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND_LSO);
  2367. txqent->hdr.wi.lso_mss = htons(gso_size);
  2368. }
  2369. if (bnad_tso_prepare(bnad, skb)) {
  2370. BNAD_UPDATE_CTR(bnad, tx_skb_tso_prepare);
  2371. return -EINVAL;
  2372. }
  2373. flags |= (BNA_TXQ_WI_CF_IP_CKSUM | BNA_TXQ_WI_CF_TCP_CKSUM);
  2374. txqent->hdr.wi.l4_hdr_size_n_offset =
  2375. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET(
  2376. tcp_hdrlen(skb) >> 2, skb_transport_offset(skb)));
  2377. } else {
  2378. txqent->hdr.wi.opcode = htons(BNA_TXQ_WI_SEND);
  2379. txqent->hdr.wi.lso_mss = 0;
  2380. if (unlikely(skb->len > (bnad->netdev->mtu + VLAN_ETH_HLEN))) {
  2381. BNAD_UPDATE_CTR(bnad, tx_skb_non_tso_too_long);
  2382. return -EINVAL;
  2383. }
  2384. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2385. __be16 net_proto = vlan_get_protocol(skb);
  2386. u8 proto = 0;
  2387. if (net_proto == htons(ETH_P_IP))
  2388. proto = ip_hdr(skb)->protocol;
  2389. #ifdef NETIF_F_IPV6_CSUM
  2390. else if (net_proto == htons(ETH_P_IPV6)) {
  2391. /* nexthdr may not be TCP immediately. */
  2392. proto = ipv6_hdr(skb)->nexthdr;
  2393. }
  2394. #endif
  2395. if (proto == IPPROTO_TCP) {
  2396. flags |= BNA_TXQ_WI_CF_TCP_CKSUM;
  2397. txqent->hdr.wi.l4_hdr_size_n_offset =
  2398. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
  2399. (0, skb_transport_offset(skb)));
  2400. BNAD_UPDATE_CTR(bnad, tcpcsum_offload);
  2401. if (unlikely(skb_headlen(skb) <
  2402. skb_transport_offset(skb) +
  2403. tcp_hdrlen(skb))) {
  2404. BNAD_UPDATE_CTR(bnad, tx_skb_tcp_hdr);
  2405. return -EINVAL;
  2406. }
  2407. } else if (proto == IPPROTO_UDP) {
  2408. flags |= BNA_TXQ_WI_CF_UDP_CKSUM;
  2409. txqent->hdr.wi.l4_hdr_size_n_offset =
  2410. htons(BNA_TXQ_WI_L4_HDR_N_OFFSET
  2411. (0, skb_transport_offset(skb)));
  2412. BNAD_UPDATE_CTR(bnad, udpcsum_offload);
  2413. if (unlikely(skb_headlen(skb) <
  2414. skb_transport_offset(skb) +
  2415. sizeof(struct udphdr))) {
  2416. BNAD_UPDATE_CTR(bnad, tx_skb_udp_hdr);
  2417. return -EINVAL;
  2418. }
  2419. } else {
  2420. BNAD_UPDATE_CTR(bnad, tx_skb_csum_err);
  2421. return -EINVAL;
  2422. }
  2423. } else
  2424. txqent->hdr.wi.l4_hdr_size_n_offset = 0;
  2425. }
  2426. txqent->hdr.wi.flags = htons(flags);
  2427. txqent->hdr.wi.frame_length = htonl(skb->len);
  2428. return 0;
  2429. }
  2430. /*
  2431. * bnad_start_xmit : Netdev entry point for Transmit
  2432. * Called under lock held by net_device
  2433. */
  2434. static netdev_tx_t
  2435. bnad_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  2436. {
  2437. struct bnad *bnad = netdev_priv(netdev);
  2438. u32 txq_id = 0;
  2439. struct bna_tcb *tcb = NULL;
  2440. struct bnad_tx_unmap *unmap_q, *unmap, *head_unmap;
  2441. u32 prod, q_depth, vect_id;
  2442. u32 wis, vectors, len;
  2443. int i;
  2444. dma_addr_t dma_addr;
  2445. struct bna_txq_entry *txqent;
  2446. len = skb_headlen(skb);
  2447. /* Sanity checks for the skb */
  2448. if (unlikely(skb->len <= ETH_HLEN)) {
  2449. dev_kfree_skb_any(skb);
  2450. BNAD_UPDATE_CTR(bnad, tx_skb_too_short);
  2451. return NETDEV_TX_OK;
  2452. }
  2453. if (unlikely(len > BFI_TX_MAX_DATA_PER_VECTOR)) {
  2454. dev_kfree_skb_any(skb);
  2455. BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
  2456. return NETDEV_TX_OK;
  2457. }
  2458. if (unlikely(len == 0)) {
  2459. dev_kfree_skb_any(skb);
  2460. BNAD_UPDATE_CTR(bnad, tx_skb_headlen_zero);
  2461. return NETDEV_TX_OK;
  2462. }
  2463. tcb = bnad->tx_info[0].tcb[txq_id];
  2464. /*
  2465. * Takes care of the Tx that is scheduled between clearing the flag
  2466. * and the netif_tx_stop_all_queues() call.
  2467. */
  2468. if (unlikely(!tcb || !test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags))) {
  2469. dev_kfree_skb_any(skb);
  2470. BNAD_UPDATE_CTR(bnad, tx_skb_stopping);
  2471. return NETDEV_TX_OK;
  2472. }
  2473. q_depth = tcb->q_depth;
  2474. prod = tcb->producer_index;
  2475. unmap_q = tcb->unmap_q;
  2476. vectors = 1 + skb_shinfo(skb)->nr_frags;
  2477. wis = BNA_TXQ_WI_NEEDED(vectors); /* 4 vectors per work item */
  2478. if (unlikely(vectors > BFI_TX_MAX_VECTORS_PER_PKT)) {
  2479. dev_kfree_skb_any(skb);
  2480. BNAD_UPDATE_CTR(bnad, tx_skb_max_vectors);
  2481. return NETDEV_TX_OK;
  2482. }
  2483. /* Check for available TxQ resources */
  2484. if (unlikely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
  2485. if ((*tcb->hw_consumer_index != tcb->consumer_index) &&
  2486. !test_and_set_bit(BNAD_TXQ_FREE_SENT, &tcb->flags)) {
  2487. u32 sent;
  2488. sent = bnad_txcmpl_process(bnad, tcb);
  2489. if (likely(test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  2490. bna_ib_ack(tcb->i_dbell, sent);
  2491. smp_mb__before_atomic();
  2492. clear_bit(BNAD_TXQ_FREE_SENT, &tcb->flags);
  2493. } else {
  2494. netif_stop_queue(netdev);
  2495. BNAD_UPDATE_CTR(bnad, netif_queue_stop);
  2496. }
  2497. smp_mb();
  2498. /*
  2499. * Check again to deal with race condition between
  2500. * netif_stop_queue here, and netif_wake_queue in
  2501. * interrupt handler which is not inside netif tx lock.
  2502. */
  2503. if (likely(wis > BNA_QE_FREE_CNT(tcb, q_depth))) {
  2504. BNAD_UPDATE_CTR(bnad, netif_queue_stop);
  2505. return NETDEV_TX_BUSY;
  2506. } else {
  2507. netif_wake_queue(netdev);
  2508. BNAD_UPDATE_CTR(bnad, netif_queue_wakeup);
  2509. }
  2510. }
  2511. txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
  2512. head_unmap = &unmap_q[prod];
  2513. /* Program the opcode, flags, frame_len, num_vectors in WI */
  2514. if (bnad_txq_wi_prepare(bnad, tcb, skb, txqent)) {
  2515. dev_kfree_skb_any(skb);
  2516. return NETDEV_TX_OK;
  2517. }
  2518. txqent->hdr.wi.reserved = 0;
  2519. txqent->hdr.wi.num_vectors = vectors;
  2520. head_unmap->skb = skb;
  2521. head_unmap->nvecs = 0;
  2522. /* Program the vectors */
  2523. unmap = head_unmap;
  2524. dma_addr = dma_map_single(&bnad->pcidev->dev, skb->data,
  2525. len, DMA_TO_DEVICE);
  2526. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  2527. dev_kfree_skb_any(skb);
  2528. BNAD_UPDATE_CTR(bnad, tx_skb_map_failed);
  2529. return NETDEV_TX_OK;
  2530. }
  2531. BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[0].host_addr);
  2532. txqent->vector[0].length = htons(len);
  2533. dma_unmap_addr_set(&unmap->vectors[0], dma_addr, dma_addr);
  2534. head_unmap->nvecs++;
  2535. for (i = 0, vect_id = 0; i < vectors - 1; i++) {
  2536. const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  2537. u32 size = skb_frag_size(frag);
  2538. if (unlikely(size == 0)) {
  2539. /* Undo the changes starting at tcb->producer_index */
  2540. bnad_tx_buff_unmap(bnad, unmap_q, q_depth,
  2541. tcb->producer_index);
  2542. dev_kfree_skb_any(skb);
  2543. BNAD_UPDATE_CTR(bnad, tx_skb_frag_zero);
  2544. return NETDEV_TX_OK;
  2545. }
  2546. len += size;
  2547. vect_id++;
  2548. if (vect_id == BFI_TX_MAX_VECTORS_PER_WI) {
  2549. vect_id = 0;
  2550. BNA_QE_INDX_INC(prod, q_depth);
  2551. txqent = &((struct bna_txq_entry *)tcb->sw_q)[prod];
  2552. txqent->hdr.wi_ext.opcode = htons(BNA_TXQ_WI_EXTENSION);
  2553. unmap = &unmap_q[prod];
  2554. }
  2555. dma_addr = skb_frag_dma_map(&bnad->pcidev->dev, frag,
  2556. 0, size, DMA_TO_DEVICE);
  2557. if (dma_mapping_error(&bnad->pcidev->dev, dma_addr)) {
  2558. /* Undo the changes starting at tcb->producer_index */
  2559. bnad_tx_buff_unmap(bnad, unmap_q, q_depth,
  2560. tcb->producer_index);
  2561. dev_kfree_skb_any(skb);
  2562. BNAD_UPDATE_CTR(bnad, tx_skb_map_failed);
  2563. return NETDEV_TX_OK;
  2564. }
  2565. dma_unmap_len_set(&unmap->vectors[vect_id], dma_len, size);
  2566. BNA_SET_DMA_ADDR(dma_addr, &txqent->vector[vect_id].host_addr);
  2567. txqent->vector[vect_id].length = htons(size);
  2568. dma_unmap_addr_set(&unmap->vectors[vect_id], dma_addr,
  2569. dma_addr);
  2570. head_unmap->nvecs++;
  2571. }
  2572. if (unlikely(len != skb->len)) {
  2573. /* Undo the changes starting at tcb->producer_index */
  2574. bnad_tx_buff_unmap(bnad, unmap_q, q_depth, tcb->producer_index);
  2575. dev_kfree_skb_any(skb);
  2576. BNAD_UPDATE_CTR(bnad, tx_skb_len_mismatch);
  2577. return NETDEV_TX_OK;
  2578. }
  2579. BNA_QE_INDX_INC(prod, q_depth);
  2580. tcb->producer_index = prod;
  2581. wmb();
  2582. if (unlikely(!test_bit(BNAD_TXQ_TX_STARTED, &tcb->flags)))
  2583. return NETDEV_TX_OK;
  2584. skb_tx_timestamp(skb);
  2585. bna_txq_prod_indx_doorbell(tcb);
  2586. return NETDEV_TX_OK;
  2587. }
  2588. /*
  2589. * Used spin_lock to synchronize reading of stats structures, which
  2590. * is written by BNA under the same lock.
  2591. */
  2592. static void
  2593. bnad_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  2594. {
  2595. struct bnad *bnad = netdev_priv(netdev);
  2596. unsigned long flags;
  2597. spin_lock_irqsave(&bnad->bna_lock, flags);
  2598. bnad_netdev_qstats_fill(bnad, stats);
  2599. bnad_netdev_hwstats_fill(bnad, stats);
  2600. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2601. }
  2602. static void
  2603. bnad_set_rx_ucast_fltr(struct bnad *bnad)
  2604. {
  2605. struct net_device *netdev = bnad->netdev;
  2606. int uc_count = netdev_uc_count(netdev);
  2607. enum bna_cb_status ret;
  2608. u8 *mac_list;
  2609. struct netdev_hw_addr *ha;
  2610. int entry;
  2611. if (netdev_uc_empty(bnad->netdev)) {
  2612. bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL);
  2613. return;
  2614. }
  2615. if (uc_count > bna_attr(&bnad->bna)->num_ucmac)
  2616. goto mode_default;
  2617. mac_list = kcalloc(ETH_ALEN, uc_count, GFP_ATOMIC);
  2618. if (mac_list == NULL)
  2619. goto mode_default;
  2620. entry = 0;
  2621. netdev_for_each_uc_addr(ha, netdev) {
  2622. ether_addr_copy(&mac_list[entry * ETH_ALEN], &ha->addr[0]);
  2623. entry++;
  2624. }
  2625. ret = bna_rx_ucast_listset(bnad->rx_info[0].rx, entry, mac_list);
  2626. kfree(mac_list);
  2627. if (ret != BNA_CB_SUCCESS)
  2628. goto mode_default;
  2629. return;
  2630. /* ucast packets not in UCAM are routed to default function */
  2631. mode_default:
  2632. bnad->cfg_flags |= BNAD_CF_DEFAULT;
  2633. bna_rx_ucast_listset(bnad->rx_info[0].rx, 0, NULL);
  2634. }
  2635. static void
  2636. bnad_set_rx_mcast_fltr(struct bnad *bnad)
  2637. {
  2638. struct net_device *netdev = bnad->netdev;
  2639. int mc_count = netdev_mc_count(netdev);
  2640. enum bna_cb_status ret;
  2641. u8 *mac_list;
  2642. if (netdev->flags & IFF_ALLMULTI)
  2643. goto mode_allmulti;
  2644. if (netdev_mc_empty(netdev))
  2645. return;
  2646. if (mc_count > bna_attr(&bnad->bna)->num_mcmac)
  2647. goto mode_allmulti;
  2648. mac_list = kcalloc(mc_count + 1, ETH_ALEN, GFP_ATOMIC);
  2649. if (mac_list == NULL)
  2650. goto mode_allmulti;
  2651. ether_addr_copy(&mac_list[0], &bnad_bcast_addr[0]);
  2652. /* copy rest of the MCAST addresses */
  2653. bnad_netdev_mc_list_get(netdev, mac_list);
  2654. ret = bna_rx_mcast_listset(bnad->rx_info[0].rx, mc_count + 1, mac_list);
  2655. kfree(mac_list);
  2656. if (ret != BNA_CB_SUCCESS)
  2657. goto mode_allmulti;
  2658. return;
  2659. mode_allmulti:
  2660. bnad->cfg_flags |= BNAD_CF_ALLMULTI;
  2661. bna_rx_mcast_delall(bnad->rx_info[0].rx);
  2662. }
  2663. void
  2664. bnad_set_rx_mode(struct net_device *netdev)
  2665. {
  2666. struct bnad *bnad = netdev_priv(netdev);
  2667. enum bna_rxmode new_mode, mode_mask;
  2668. unsigned long flags;
  2669. spin_lock_irqsave(&bnad->bna_lock, flags);
  2670. if (bnad->rx_info[0].rx == NULL) {
  2671. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2672. return;
  2673. }
  2674. /* clear bnad flags to update it with new settings */
  2675. bnad->cfg_flags &= ~(BNAD_CF_PROMISC | BNAD_CF_DEFAULT |
  2676. BNAD_CF_ALLMULTI);
  2677. new_mode = 0;
  2678. if (netdev->flags & IFF_PROMISC) {
  2679. new_mode |= BNAD_RXMODE_PROMISC_DEFAULT;
  2680. bnad->cfg_flags |= BNAD_CF_PROMISC;
  2681. } else {
  2682. bnad_set_rx_mcast_fltr(bnad);
  2683. if (bnad->cfg_flags & BNAD_CF_ALLMULTI)
  2684. new_mode |= BNA_RXMODE_ALLMULTI;
  2685. bnad_set_rx_ucast_fltr(bnad);
  2686. if (bnad->cfg_flags & BNAD_CF_DEFAULT)
  2687. new_mode |= BNA_RXMODE_DEFAULT;
  2688. }
  2689. mode_mask = BNA_RXMODE_PROMISC | BNA_RXMODE_DEFAULT |
  2690. BNA_RXMODE_ALLMULTI;
  2691. bna_rx_mode_set(bnad->rx_info[0].rx, new_mode, mode_mask);
  2692. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2693. }
  2694. /*
  2695. * bna_lock is used to sync writes to netdev->addr
  2696. * conf_lock cannot be used since this call may be made
  2697. * in a non-blocking context.
  2698. */
  2699. static int
  2700. bnad_set_mac_address(struct net_device *netdev, void *addr)
  2701. {
  2702. int err;
  2703. struct bnad *bnad = netdev_priv(netdev);
  2704. struct sockaddr *sa = (struct sockaddr *)addr;
  2705. unsigned long flags;
  2706. spin_lock_irqsave(&bnad->bna_lock, flags);
  2707. err = bnad_mac_addr_set_locked(bnad, sa->sa_data);
  2708. if (!err)
  2709. ether_addr_copy(netdev->dev_addr, sa->sa_data);
  2710. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2711. return err;
  2712. }
  2713. static int
  2714. bnad_mtu_set(struct bnad *bnad, int frame_size)
  2715. {
  2716. unsigned long flags;
  2717. init_completion(&bnad->bnad_completions.mtu_comp);
  2718. spin_lock_irqsave(&bnad->bna_lock, flags);
  2719. bna_enet_mtu_set(&bnad->bna.enet, frame_size, bnad_cb_enet_mtu_set);
  2720. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2721. wait_for_completion(&bnad->bnad_completions.mtu_comp);
  2722. return bnad->bnad_completions.mtu_comp_status;
  2723. }
  2724. static int
  2725. bnad_change_mtu(struct net_device *netdev, int new_mtu)
  2726. {
  2727. int err, mtu;
  2728. struct bnad *bnad = netdev_priv(netdev);
  2729. u32 rx_count = 0, frame, new_frame;
  2730. mutex_lock(&bnad->conf_mutex);
  2731. mtu = netdev->mtu;
  2732. netdev->mtu = new_mtu;
  2733. frame = BNAD_FRAME_SIZE(mtu);
  2734. new_frame = BNAD_FRAME_SIZE(new_mtu);
  2735. /* check if multi-buffer needs to be enabled */
  2736. if (BNAD_PCI_DEV_IS_CAT2(bnad) &&
  2737. netif_running(bnad->netdev)) {
  2738. /* only when transition is over 4K */
  2739. if ((frame <= 4096 && new_frame > 4096) ||
  2740. (frame > 4096 && new_frame <= 4096))
  2741. rx_count = bnad_reinit_rx(bnad);
  2742. }
  2743. /* rx_count > 0 - new rx created
  2744. * - Linux set err = 0 and return
  2745. */
  2746. err = bnad_mtu_set(bnad, new_frame);
  2747. if (err)
  2748. err = -EBUSY;
  2749. mutex_unlock(&bnad->conf_mutex);
  2750. return err;
  2751. }
  2752. static int
  2753. bnad_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2754. {
  2755. struct bnad *bnad = netdev_priv(netdev);
  2756. unsigned long flags;
  2757. if (!bnad->rx_info[0].rx)
  2758. return 0;
  2759. mutex_lock(&bnad->conf_mutex);
  2760. spin_lock_irqsave(&bnad->bna_lock, flags);
  2761. bna_rx_vlan_add(bnad->rx_info[0].rx, vid);
  2762. set_bit(vid, bnad->active_vlans);
  2763. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2764. mutex_unlock(&bnad->conf_mutex);
  2765. return 0;
  2766. }
  2767. static int
  2768. bnad_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
  2769. {
  2770. struct bnad *bnad = netdev_priv(netdev);
  2771. unsigned long flags;
  2772. if (!bnad->rx_info[0].rx)
  2773. return 0;
  2774. mutex_lock(&bnad->conf_mutex);
  2775. spin_lock_irqsave(&bnad->bna_lock, flags);
  2776. clear_bit(vid, bnad->active_vlans);
  2777. bna_rx_vlan_del(bnad->rx_info[0].rx, vid);
  2778. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2779. mutex_unlock(&bnad->conf_mutex);
  2780. return 0;
  2781. }
  2782. static int bnad_set_features(struct net_device *dev, netdev_features_t features)
  2783. {
  2784. struct bnad *bnad = netdev_priv(dev);
  2785. netdev_features_t changed = features ^ dev->features;
  2786. if ((changed & NETIF_F_HW_VLAN_CTAG_RX) && netif_running(dev)) {
  2787. unsigned long flags;
  2788. spin_lock_irqsave(&bnad->bna_lock, flags);
  2789. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2790. bna_rx_vlan_strip_enable(bnad->rx_info[0].rx);
  2791. else
  2792. bna_rx_vlan_strip_disable(bnad->rx_info[0].rx);
  2793. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2794. }
  2795. return 0;
  2796. }
  2797. #ifdef CONFIG_NET_POLL_CONTROLLER
  2798. static void
  2799. bnad_netpoll(struct net_device *netdev)
  2800. {
  2801. struct bnad *bnad = netdev_priv(netdev);
  2802. struct bnad_rx_info *rx_info;
  2803. struct bnad_rx_ctrl *rx_ctrl;
  2804. u32 curr_mask;
  2805. int i, j;
  2806. if (!(bnad->cfg_flags & BNAD_CF_MSIX)) {
  2807. bna_intx_disable(&bnad->bna, curr_mask);
  2808. bnad_isr(bnad->pcidev->irq, netdev);
  2809. bna_intx_enable(&bnad->bna, curr_mask);
  2810. } else {
  2811. /*
  2812. * Tx processing may happen in sending context, so no need
  2813. * to explicitly process completions here
  2814. */
  2815. /* Rx processing */
  2816. for (i = 0; i < bnad->num_rx; i++) {
  2817. rx_info = &bnad->rx_info[i];
  2818. if (!rx_info->rx)
  2819. continue;
  2820. for (j = 0; j < bnad->num_rxp_per_rx; j++) {
  2821. rx_ctrl = &rx_info->rx_ctrl[j];
  2822. if (rx_ctrl->ccb)
  2823. bnad_netif_rx_schedule_poll(bnad,
  2824. rx_ctrl->ccb);
  2825. }
  2826. }
  2827. }
  2828. }
  2829. #endif
  2830. static const struct net_device_ops bnad_netdev_ops = {
  2831. .ndo_open = bnad_open,
  2832. .ndo_stop = bnad_stop,
  2833. .ndo_start_xmit = bnad_start_xmit,
  2834. .ndo_get_stats64 = bnad_get_stats64,
  2835. .ndo_set_rx_mode = bnad_set_rx_mode,
  2836. .ndo_validate_addr = eth_validate_addr,
  2837. .ndo_set_mac_address = bnad_set_mac_address,
  2838. .ndo_change_mtu = bnad_change_mtu,
  2839. .ndo_vlan_rx_add_vid = bnad_vlan_rx_add_vid,
  2840. .ndo_vlan_rx_kill_vid = bnad_vlan_rx_kill_vid,
  2841. .ndo_set_features = bnad_set_features,
  2842. #ifdef CONFIG_NET_POLL_CONTROLLER
  2843. .ndo_poll_controller = bnad_netpoll
  2844. #endif
  2845. };
  2846. static void
  2847. bnad_netdev_init(struct bnad *bnad, bool using_dac)
  2848. {
  2849. struct net_device *netdev = bnad->netdev;
  2850. netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
  2851. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2852. NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_HW_VLAN_CTAG_TX |
  2853. NETIF_F_HW_VLAN_CTAG_RX;
  2854. netdev->vlan_features = NETIF_F_SG | NETIF_F_HIGHDMA |
  2855. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2856. NETIF_F_TSO | NETIF_F_TSO6;
  2857. netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  2858. if (using_dac)
  2859. netdev->features |= NETIF_F_HIGHDMA;
  2860. netdev->mem_start = bnad->mmio_start;
  2861. netdev->mem_end = bnad->mmio_start + bnad->mmio_len - 1;
  2862. /* MTU range: 46 - 9000 */
  2863. netdev->min_mtu = ETH_ZLEN - ETH_HLEN;
  2864. netdev->max_mtu = BNAD_JUMBO_MTU;
  2865. netdev->netdev_ops = &bnad_netdev_ops;
  2866. bnad_set_ethtool_ops(netdev);
  2867. }
  2868. /*
  2869. * 1. Initialize the bnad structure
  2870. * 2. Setup netdev pointer in pci_dev
  2871. * 3. Initialize no. of TxQ & CQs & MSIX vectors
  2872. * 4. Initialize work queue.
  2873. */
  2874. static int
  2875. bnad_init(struct bnad *bnad,
  2876. struct pci_dev *pdev, struct net_device *netdev)
  2877. {
  2878. unsigned long flags;
  2879. SET_NETDEV_DEV(netdev, &pdev->dev);
  2880. pci_set_drvdata(pdev, netdev);
  2881. bnad->netdev = netdev;
  2882. bnad->pcidev = pdev;
  2883. bnad->mmio_start = pci_resource_start(pdev, 0);
  2884. bnad->mmio_len = pci_resource_len(pdev, 0);
  2885. bnad->bar0 = ioremap_nocache(bnad->mmio_start, bnad->mmio_len);
  2886. if (!bnad->bar0) {
  2887. dev_err(&pdev->dev, "ioremap for bar0 failed\n");
  2888. return -ENOMEM;
  2889. }
  2890. dev_info(&pdev->dev, "bar0 mapped to %p, len %llu\n", bnad->bar0,
  2891. (unsigned long long) bnad->mmio_len);
  2892. spin_lock_irqsave(&bnad->bna_lock, flags);
  2893. if (!bnad_msix_disable)
  2894. bnad->cfg_flags = BNAD_CF_MSIX;
  2895. bnad->cfg_flags |= BNAD_CF_DIM_ENABLED;
  2896. bnad_q_num_init(bnad);
  2897. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  2898. bnad->msix_num = (bnad->num_tx * bnad->num_txq_per_tx) +
  2899. (bnad->num_rx * bnad->num_rxp_per_rx) +
  2900. BNAD_MAILBOX_MSIX_VECTORS;
  2901. bnad->txq_depth = BNAD_TXQ_DEPTH;
  2902. bnad->rxq_depth = BNAD_RXQ_DEPTH;
  2903. bnad->tx_coalescing_timeo = BFI_TX_COALESCING_TIMEO;
  2904. bnad->rx_coalescing_timeo = BFI_RX_COALESCING_TIMEO;
  2905. sprintf(bnad->wq_name, "%s_wq_%d", BNAD_NAME, bnad->id);
  2906. bnad->work_q = create_singlethread_workqueue(bnad->wq_name);
  2907. if (!bnad->work_q) {
  2908. iounmap(bnad->bar0);
  2909. return -ENOMEM;
  2910. }
  2911. return 0;
  2912. }
  2913. /*
  2914. * Must be called after bnad_pci_uninit()
  2915. * so that iounmap() and pci_set_drvdata(NULL)
  2916. * happens only after PCI uninitialization.
  2917. */
  2918. static void
  2919. bnad_uninit(struct bnad *bnad)
  2920. {
  2921. if (bnad->work_q) {
  2922. flush_workqueue(bnad->work_q);
  2923. destroy_workqueue(bnad->work_q);
  2924. bnad->work_q = NULL;
  2925. }
  2926. if (bnad->bar0)
  2927. iounmap(bnad->bar0);
  2928. }
  2929. /*
  2930. * Initialize locks
  2931. a) Per ioceth mutes used for serializing configuration
  2932. changes from OS interface
  2933. b) spin lock used to protect bna state machine
  2934. */
  2935. static void
  2936. bnad_lock_init(struct bnad *bnad)
  2937. {
  2938. spin_lock_init(&bnad->bna_lock);
  2939. mutex_init(&bnad->conf_mutex);
  2940. }
  2941. static void
  2942. bnad_lock_uninit(struct bnad *bnad)
  2943. {
  2944. mutex_destroy(&bnad->conf_mutex);
  2945. }
  2946. /* PCI Initialization */
  2947. static int
  2948. bnad_pci_init(struct bnad *bnad,
  2949. struct pci_dev *pdev, bool *using_dac)
  2950. {
  2951. int err;
  2952. err = pci_enable_device(pdev);
  2953. if (err)
  2954. return err;
  2955. err = pci_request_regions(pdev, BNAD_NAME);
  2956. if (err)
  2957. goto disable_device;
  2958. if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
  2959. *using_dac = true;
  2960. } else {
  2961. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2962. if (err)
  2963. goto release_regions;
  2964. *using_dac = false;
  2965. }
  2966. pci_set_master(pdev);
  2967. return 0;
  2968. release_regions:
  2969. pci_release_regions(pdev);
  2970. disable_device:
  2971. pci_disable_device(pdev);
  2972. return err;
  2973. }
  2974. static void
  2975. bnad_pci_uninit(struct pci_dev *pdev)
  2976. {
  2977. pci_release_regions(pdev);
  2978. pci_disable_device(pdev);
  2979. }
  2980. static int
  2981. bnad_pci_probe(struct pci_dev *pdev,
  2982. const struct pci_device_id *pcidev_id)
  2983. {
  2984. bool using_dac;
  2985. int err;
  2986. struct bnad *bnad;
  2987. struct bna *bna;
  2988. struct net_device *netdev;
  2989. struct bfa_pcidev pcidev_info;
  2990. unsigned long flags;
  2991. mutex_lock(&bnad_fwimg_mutex);
  2992. if (!cna_get_firmware_buf(pdev)) {
  2993. mutex_unlock(&bnad_fwimg_mutex);
  2994. dev_err(&pdev->dev, "failed to load firmware image!\n");
  2995. return -ENODEV;
  2996. }
  2997. mutex_unlock(&bnad_fwimg_mutex);
  2998. /*
  2999. * Allocates sizeof(struct net_device + struct bnad)
  3000. * bnad = netdev->priv
  3001. */
  3002. netdev = alloc_etherdev(sizeof(struct bnad));
  3003. if (!netdev) {
  3004. err = -ENOMEM;
  3005. return err;
  3006. }
  3007. bnad = netdev_priv(netdev);
  3008. bnad_lock_init(bnad);
  3009. bnad->id = atomic_inc_return(&bna_id) - 1;
  3010. mutex_lock(&bnad->conf_mutex);
  3011. /*
  3012. * PCI initialization
  3013. * Output : using_dac = 1 for 64 bit DMA
  3014. * = 0 for 32 bit DMA
  3015. */
  3016. using_dac = false;
  3017. err = bnad_pci_init(bnad, pdev, &using_dac);
  3018. if (err)
  3019. goto unlock_mutex;
  3020. /*
  3021. * Initialize bnad structure
  3022. * Setup relation between pci_dev & netdev
  3023. */
  3024. err = bnad_init(bnad, pdev, netdev);
  3025. if (err)
  3026. goto pci_uninit;
  3027. /* Initialize netdev structure, set up ethtool ops */
  3028. bnad_netdev_init(bnad, using_dac);
  3029. /* Set link to down state */
  3030. netif_carrier_off(netdev);
  3031. /* Setup the debugfs node for this bfad */
  3032. if (bna_debugfs_enable)
  3033. bnad_debugfs_init(bnad);
  3034. /* Get resource requirement form bna */
  3035. spin_lock_irqsave(&bnad->bna_lock, flags);
  3036. bna_res_req(&bnad->res_info[0]);
  3037. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3038. /* Allocate resources from bna */
  3039. err = bnad_res_alloc(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3040. if (err)
  3041. goto drv_uninit;
  3042. bna = &bnad->bna;
  3043. /* Setup pcidev_info for bna_init() */
  3044. pcidev_info.pci_slot = PCI_SLOT(bnad->pcidev->devfn);
  3045. pcidev_info.pci_func = PCI_FUNC(bnad->pcidev->devfn);
  3046. pcidev_info.device_id = bnad->pcidev->device;
  3047. pcidev_info.pci_bar_kva = bnad->bar0;
  3048. spin_lock_irqsave(&bnad->bna_lock, flags);
  3049. bna_init(bna, bnad, &pcidev_info, &bnad->res_info[0]);
  3050. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3051. bnad->stats.bna_stats = &bna->stats;
  3052. bnad_enable_msix(bnad);
  3053. err = bnad_mbox_irq_alloc(bnad);
  3054. if (err)
  3055. goto res_free;
  3056. /* Set up timers */
  3057. timer_setup(&bnad->bna.ioceth.ioc.ioc_timer, bnad_ioc_timeout, 0);
  3058. timer_setup(&bnad->bna.ioceth.ioc.hb_timer, bnad_ioc_hb_check, 0);
  3059. timer_setup(&bnad->bna.ioceth.ioc.iocpf_timer, bnad_iocpf_timeout, 0);
  3060. timer_setup(&bnad->bna.ioceth.ioc.sem_timer, bnad_iocpf_sem_timeout,
  3061. 0);
  3062. /*
  3063. * Start the chip
  3064. * If the call back comes with error, we bail out.
  3065. * This is a catastrophic error.
  3066. */
  3067. err = bnad_ioceth_enable(bnad);
  3068. if (err) {
  3069. dev_err(&pdev->dev, "initialization failed err=%d\n", err);
  3070. goto probe_success;
  3071. }
  3072. spin_lock_irqsave(&bnad->bna_lock, flags);
  3073. if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
  3074. bna_num_rxp_set(bna, BNAD_NUM_RXP + 1)) {
  3075. bnad_q_num_adjust(bnad, bna_attr(bna)->num_txq - 1,
  3076. bna_attr(bna)->num_rxp - 1);
  3077. if (bna_num_txq_set(bna, BNAD_NUM_TXQ + 1) ||
  3078. bna_num_rxp_set(bna, BNAD_NUM_RXP + 1))
  3079. err = -EIO;
  3080. }
  3081. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3082. if (err)
  3083. goto disable_ioceth;
  3084. spin_lock_irqsave(&bnad->bna_lock, flags);
  3085. bna_mod_res_req(&bnad->bna, &bnad->mod_res_info[0]);
  3086. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3087. err = bnad_res_alloc(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3088. if (err) {
  3089. err = -EIO;
  3090. goto disable_ioceth;
  3091. }
  3092. spin_lock_irqsave(&bnad->bna_lock, flags);
  3093. bna_mod_init(&bnad->bna, &bnad->mod_res_info[0]);
  3094. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3095. /* Get the burnt-in mac */
  3096. spin_lock_irqsave(&bnad->bna_lock, flags);
  3097. bna_enet_perm_mac_get(&bna->enet, bnad->perm_addr);
  3098. bnad_set_netdev_perm_addr(bnad);
  3099. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3100. mutex_unlock(&bnad->conf_mutex);
  3101. /* Finally, reguister with net_device layer */
  3102. err = register_netdev(netdev);
  3103. if (err) {
  3104. dev_err(&pdev->dev, "registering net device failed\n");
  3105. goto probe_uninit;
  3106. }
  3107. set_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags);
  3108. return 0;
  3109. probe_success:
  3110. mutex_unlock(&bnad->conf_mutex);
  3111. return 0;
  3112. probe_uninit:
  3113. mutex_lock(&bnad->conf_mutex);
  3114. bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3115. disable_ioceth:
  3116. bnad_ioceth_disable(bnad);
  3117. del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
  3118. del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
  3119. del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
  3120. spin_lock_irqsave(&bnad->bna_lock, flags);
  3121. bna_uninit(bna);
  3122. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3123. bnad_mbox_irq_free(bnad);
  3124. bnad_disable_msix(bnad);
  3125. res_free:
  3126. bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3127. drv_uninit:
  3128. /* Remove the debugfs node for this bnad */
  3129. kfree(bnad->regdata);
  3130. bnad_debugfs_uninit(bnad);
  3131. bnad_uninit(bnad);
  3132. pci_uninit:
  3133. bnad_pci_uninit(pdev);
  3134. unlock_mutex:
  3135. mutex_unlock(&bnad->conf_mutex);
  3136. bnad_lock_uninit(bnad);
  3137. free_netdev(netdev);
  3138. return err;
  3139. }
  3140. static void
  3141. bnad_pci_remove(struct pci_dev *pdev)
  3142. {
  3143. struct net_device *netdev = pci_get_drvdata(pdev);
  3144. struct bnad *bnad;
  3145. struct bna *bna;
  3146. unsigned long flags;
  3147. if (!netdev)
  3148. return;
  3149. bnad = netdev_priv(netdev);
  3150. bna = &bnad->bna;
  3151. if (test_and_clear_bit(BNAD_RF_NETDEV_REGISTERED, &bnad->run_flags))
  3152. unregister_netdev(netdev);
  3153. mutex_lock(&bnad->conf_mutex);
  3154. bnad_ioceth_disable(bnad);
  3155. del_timer_sync(&bnad->bna.ioceth.ioc.ioc_timer);
  3156. del_timer_sync(&bnad->bna.ioceth.ioc.sem_timer);
  3157. del_timer_sync(&bnad->bna.ioceth.ioc.hb_timer);
  3158. spin_lock_irqsave(&bnad->bna_lock, flags);
  3159. bna_uninit(bna);
  3160. spin_unlock_irqrestore(&bnad->bna_lock, flags);
  3161. bnad_res_free(bnad, &bnad->mod_res_info[0], BNA_MOD_RES_T_MAX);
  3162. bnad_res_free(bnad, &bnad->res_info[0], BNA_RES_T_MAX);
  3163. bnad_mbox_irq_free(bnad);
  3164. bnad_disable_msix(bnad);
  3165. bnad_pci_uninit(pdev);
  3166. mutex_unlock(&bnad->conf_mutex);
  3167. bnad_lock_uninit(bnad);
  3168. /* Remove the debugfs node for this bnad */
  3169. kfree(bnad->regdata);
  3170. bnad_debugfs_uninit(bnad);
  3171. bnad_uninit(bnad);
  3172. free_netdev(netdev);
  3173. }
  3174. static const struct pci_device_id bnad_pci_id_table[] = {
  3175. {
  3176. PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
  3177. PCI_DEVICE_ID_BROCADE_CT),
  3178. .class = PCI_CLASS_NETWORK_ETHERNET << 8,
  3179. .class_mask = 0xffff00
  3180. },
  3181. {
  3182. PCI_DEVICE(PCI_VENDOR_ID_BROCADE,
  3183. BFA_PCI_DEVICE_ID_CT2),
  3184. .class = PCI_CLASS_NETWORK_ETHERNET << 8,
  3185. .class_mask = 0xffff00
  3186. },
  3187. {0, },
  3188. };
  3189. MODULE_DEVICE_TABLE(pci, bnad_pci_id_table);
  3190. static struct pci_driver bnad_pci_driver = {
  3191. .name = BNAD_NAME,
  3192. .id_table = bnad_pci_id_table,
  3193. .probe = bnad_pci_probe,
  3194. .remove = bnad_pci_remove,
  3195. };
  3196. static int __init
  3197. bnad_module_init(void)
  3198. {
  3199. int err;
  3200. pr_info("bna: QLogic BR-series 10G Ethernet driver - version: %s\n",
  3201. BNAD_VERSION);
  3202. bfa_nw_ioc_auto_recover(bnad_ioc_auto_recover);
  3203. err = pci_register_driver(&bnad_pci_driver);
  3204. if (err < 0) {
  3205. pr_err("bna: PCI driver registration failed err=%d\n", err);
  3206. return err;
  3207. }
  3208. return 0;
  3209. }
  3210. static void __exit
  3211. bnad_module_exit(void)
  3212. {
  3213. pci_unregister_driver(&bnad_pci_driver);
  3214. release_firmware(bfi_fw);
  3215. }
  3216. module_init(bnad_module_init);
  3217. module_exit(bnad_module_exit);
  3218. MODULE_AUTHOR("Brocade");
  3219. MODULE_LICENSE("GPL");
  3220. MODULE_DESCRIPTION("QLogic BR-series 10G PCIe Ethernet driver");
  3221. MODULE_VERSION(BNAD_VERSION);
  3222. MODULE_FIRMWARE(CNA_FW_FILE_CT);
  3223. MODULE_FIRMWARE(CNA_FW_FILE_CT2);