bnad.c 85 KB

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