bnad.c 79 KB

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