bnad.c 79 KB

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