bnad.c 78 KB

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