bnad.c 78 KB

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