bnad.c 84 KB

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