bnad.c 86 KB

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