bnad.c 86 KB

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