bnad.c 85 KB

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