bnad.c 85 KB

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