bnad.c 85 KB

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