bnad.c 79 KB

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