bnad.c 78 KB

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