bnad.c 86 KB

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