bnad.c 79 KB

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