bnad.c 79 KB

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