vmxnet3_drv.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319
  1. /*
  2. * Linux driver for VMware's vmxnet3 ethernet NIC.
  3. *
  4. * Copyright (C) 2008-2009, VMware, Inc. All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; version 2 of the License and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  13. * NON INFRINGEMENT. See the GNU General Public License for more
  14. * details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * The full GNU General Public License is included in this distribution in
  21. * the file called "COPYING".
  22. *
  23. * Maintained by: Shreyas Bhatewara <pv-drivers@vmware.com>
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <net/ip6_checksum.h>
  28. #include "vmxnet3_int.h"
  29. char vmxnet3_driver_name[] = "vmxnet3";
  30. #define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"
  31. /*
  32. * PCI Device ID Table
  33. * Last entry must be all 0s
  34. */
  35. static DEFINE_PCI_DEVICE_TABLE(vmxnet3_pciid_table) = {
  36. {PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_VMXNET3)},
  37. {0}
  38. };
  39. MODULE_DEVICE_TABLE(pci, vmxnet3_pciid_table);
  40. static atomic_t devices_found;
  41. #define VMXNET3_MAX_DEVICES 10
  42. static int enable_mq = 1;
  43. static int irq_share_mode;
  44. static void
  45. vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac);
  46. /*
  47. * Enable/Disable the given intr
  48. */
  49. static void
  50. vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
  51. {
  52. VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
  53. }
  54. static void
  55. vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
  56. {
  57. VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
  58. }
  59. /*
  60. * Enable/Disable all intrs used by the device
  61. */
  62. static void
  63. vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
  64. {
  65. int i;
  66. for (i = 0; i < adapter->intr.num_intrs; i++)
  67. vmxnet3_enable_intr(adapter, i);
  68. adapter->shared->devRead.intrConf.intrCtrl &=
  69. cpu_to_le32(~VMXNET3_IC_DISABLE_ALL);
  70. }
  71. static void
  72. vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
  73. {
  74. int i;
  75. adapter->shared->devRead.intrConf.intrCtrl |=
  76. cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
  77. for (i = 0; i < adapter->intr.num_intrs; i++)
  78. vmxnet3_disable_intr(adapter, i);
  79. }
  80. static void
  81. vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
  82. {
  83. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
  84. }
  85. static bool
  86. vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  87. {
  88. return tq->stopped;
  89. }
  90. static void
  91. vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  92. {
  93. tq->stopped = false;
  94. netif_start_subqueue(adapter->netdev, tq - adapter->tx_queue);
  95. }
  96. static void
  97. vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  98. {
  99. tq->stopped = false;
  100. netif_wake_subqueue(adapter->netdev, (tq - adapter->tx_queue));
  101. }
  102. static void
  103. vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  104. {
  105. tq->stopped = true;
  106. tq->num_stop++;
  107. netif_stop_subqueue(adapter->netdev, (tq - adapter->tx_queue));
  108. }
  109. /*
  110. * Check the link state. This may start or stop the tx queue.
  111. */
  112. static void
  113. vmxnet3_check_link(struct vmxnet3_adapter *adapter, bool affectTxQueue)
  114. {
  115. u32 ret;
  116. int i;
  117. unsigned long flags;
  118. spin_lock_irqsave(&adapter->cmd_lock, flags);
  119. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
  120. ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
  121. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  122. adapter->link_speed = ret >> 16;
  123. if (ret & 1) { /* Link is up. */
  124. printk(KERN_INFO "%s: NIC Link is Up %d Mbps\n",
  125. adapter->netdev->name, adapter->link_speed);
  126. if (!netif_carrier_ok(adapter->netdev))
  127. netif_carrier_on(adapter->netdev);
  128. if (affectTxQueue) {
  129. for (i = 0; i < adapter->num_tx_queues; i++)
  130. vmxnet3_tq_start(&adapter->tx_queue[i],
  131. adapter);
  132. }
  133. } else {
  134. printk(KERN_INFO "%s: NIC Link is Down\n",
  135. adapter->netdev->name);
  136. if (netif_carrier_ok(adapter->netdev))
  137. netif_carrier_off(adapter->netdev);
  138. if (affectTxQueue) {
  139. for (i = 0; i < adapter->num_tx_queues; i++)
  140. vmxnet3_tq_stop(&adapter->tx_queue[i], adapter);
  141. }
  142. }
  143. }
  144. static void
  145. vmxnet3_process_events(struct vmxnet3_adapter *adapter)
  146. {
  147. int i;
  148. unsigned long flags;
  149. u32 events = le32_to_cpu(adapter->shared->ecr);
  150. if (!events)
  151. return;
  152. vmxnet3_ack_events(adapter, events);
  153. /* Check if link state has changed */
  154. if (events & VMXNET3_ECR_LINK)
  155. vmxnet3_check_link(adapter, true);
  156. /* Check if there is an error on xmit/recv queues */
  157. if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
  158. spin_lock_irqsave(&adapter->cmd_lock, flags);
  159. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  160. VMXNET3_CMD_GET_QUEUE_STATUS);
  161. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  162. for (i = 0; i < adapter->num_tx_queues; i++)
  163. if (adapter->tqd_start[i].status.stopped)
  164. dev_err(&adapter->netdev->dev,
  165. "%s: tq[%d] error 0x%x\n",
  166. adapter->netdev->name, i, le32_to_cpu(
  167. adapter->tqd_start[i].status.error));
  168. for (i = 0; i < adapter->num_rx_queues; i++)
  169. if (adapter->rqd_start[i].status.stopped)
  170. dev_err(&adapter->netdev->dev,
  171. "%s: rq[%d] error 0x%x\n",
  172. adapter->netdev->name, i,
  173. adapter->rqd_start[i].status.error);
  174. schedule_work(&adapter->work);
  175. }
  176. }
  177. #ifdef __BIG_ENDIAN_BITFIELD
  178. /*
  179. * The device expects the bitfields in shared structures to be written in
  180. * little endian. When CPU is big endian, the following routines are used to
  181. * correctly read and write into ABI.
  182. * The general technique used here is : double word bitfields are defined in
  183. * opposite order for big endian architecture. Then before reading them in
  184. * driver the complete double word is translated using le32_to_cpu. Similarly
  185. * After the driver writes into bitfields, cpu_to_le32 is used to translate the
  186. * double words into required format.
  187. * In order to avoid touching bits in shared structure more than once, temporary
  188. * descriptors are used. These are passed as srcDesc to following functions.
  189. */
  190. static void vmxnet3_RxDescToCPU(const struct Vmxnet3_RxDesc *srcDesc,
  191. struct Vmxnet3_RxDesc *dstDesc)
  192. {
  193. u32 *src = (u32 *)srcDesc + 2;
  194. u32 *dst = (u32 *)dstDesc + 2;
  195. dstDesc->addr = le64_to_cpu(srcDesc->addr);
  196. *dst = le32_to_cpu(*src);
  197. dstDesc->ext1 = le32_to_cpu(srcDesc->ext1);
  198. }
  199. static void vmxnet3_TxDescToLe(const struct Vmxnet3_TxDesc *srcDesc,
  200. struct Vmxnet3_TxDesc *dstDesc)
  201. {
  202. int i;
  203. u32 *src = (u32 *)(srcDesc + 1);
  204. u32 *dst = (u32 *)(dstDesc + 1);
  205. /* Working backwards so that the gen bit is set at the end. */
  206. for (i = 2; i > 0; i--) {
  207. src--;
  208. dst--;
  209. *dst = cpu_to_le32(*src);
  210. }
  211. }
  212. static void vmxnet3_RxCompToCPU(const struct Vmxnet3_RxCompDesc *srcDesc,
  213. struct Vmxnet3_RxCompDesc *dstDesc)
  214. {
  215. int i = 0;
  216. u32 *src = (u32 *)srcDesc;
  217. u32 *dst = (u32 *)dstDesc;
  218. for (i = 0; i < sizeof(struct Vmxnet3_RxCompDesc) / sizeof(u32); i++) {
  219. *dst = le32_to_cpu(*src);
  220. src++;
  221. dst++;
  222. }
  223. }
  224. /* Used to read bitfield values from double words. */
  225. static u32 get_bitfield32(const __le32 *bitfield, u32 pos, u32 size)
  226. {
  227. u32 temp = le32_to_cpu(*bitfield);
  228. u32 mask = ((1 << size) - 1) << pos;
  229. temp &= mask;
  230. temp >>= pos;
  231. return temp;
  232. }
  233. #endif /* __BIG_ENDIAN_BITFIELD */
  234. #ifdef __BIG_ENDIAN_BITFIELD
  235. # define VMXNET3_TXDESC_GET_GEN(txdesc) get_bitfield32(((const __le32 *) \
  236. txdesc) + VMXNET3_TXD_GEN_DWORD_SHIFT, \
  237. VMXNET3_TXD_GEN_SHIFT, VMXNET3_TXD_GEN_SIZE)
  238. # define VMXNET3_TXDESC_GET_EOP(txdesc) get_bitfield32(((const __le32 *) \
  239. txdesc) + VMXNET3_TXD_EOP_DWORD_SHIFT, \
  240. VMXNET3_TXD_EOP_SHIFT, VMXNET3_TXD_EOP_SIZE)
  241. # define VMXNET3_TCD_GET_GEN(tcd) get_bitfield32(((const __le32 *)tcd) + \
  242. VMXNET3_TCD_GEN_DWORD_SHIFT, VMXNET3_TCD_GEN_SHIFT, \
  243. VMXNET3_TCD_GEN_SIZE)
  244. # define VMXNET3_TCD_GET_TXIDX(tcd) get_bitfield32((const __le32 *)tcd, \
  245. VMXNET3_TCD_TXIDX_SHIFT, VMXNET3_TCD_TXIDX_SIZE)
  246. # define vmxnet3_getRxComp(dstrcd, rcd, tmp) do { \
  247. (dstrcd) = (tmp); \
  248. vmxnet3_RxCompToCPU((rcd), (tmp)); \
  249. } while (0)
  250. # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) do { \
  251. (dstrxd) = (tmp); \
  252. vmxnet3_RxDescToCPU((rxd), (tmp)); \
  253. } while (0)
  254. #else
  255. # define VMXNET3_TXDESC_GET_GEN(txdesc) ((txdesc)->gen)
  256. # define VMXNET3_TXDESC_GET_EOP(txdesc) ((txdesc)->eop)
  257. # define VMXNET3_TCD_GET_GEN(tcd) ((tcd)->gen)
  258. # define VMXNET3_TCD_GET_TXIDX(tcd) ((tcd)->txdIdx)
  259. # define vmxnet3_getRxComp(dstrcd, rcd, tmp) (dstrcd) = (rcd)
  260. # define vmxnet3_getRxDesc(dstrxd, rxd, tmp) (dstrxd) = (rxd)
  261. #endif /* __BIG_ENDIAN_BITFIELD */
  262. static void
  263. vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
  264. struct pci_dev *pdev)
  265. {
  266. if (tbi->map_type == VMXNET3_MAP_SINGLE)
  267. pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
  268. PCI_DMA_TODEVICE);
  269. else if (tbi->map_type == VMXNET3_MAP_PAGE)
  270. pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
  271. PCI_DMA_TODEVICE);
  272. else
  273. BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
  274. tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
  275. }
  276. static int
  277. vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
  278. struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
  279. {
  280. struct sk_buff *skb;
  281. int entries = 0;
  282. /* no out of order completion */
  283. BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
  284. BUG_ON(VMXNET3_TXDESC_GET_EOP(&(tq->tx_ring.base[eop_idx].txd)) != 1);
  285. skb = tq->buf_info[eop_idx].skb;
  286. BUG_ON(skb == NULL);
  287. tq->buf_info[eop_idx].skb = NULL;
  288. VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
  289. while (tq->tx_ring.next2comp != eop_idx) {
  290. vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
  291. pdev);
  292. /* update next2comp w/o tx_lock. Since we are marking more,
  293. * instead of less, tx ring entries avail, the worst case is
  294. * that the tx routine incorrectly re-queues a pkt due to
  295. * insufficient tx ring entries.
  296. */
  297. vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
  298. entries++;
  299. }
  300. dev_kfree_skb_any(skb);
  301. return entries;
  302. }
  303. static int
  304. vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
  305. struct vmxnet3_adapter *adapter)
  306. {
  307. int completed = 0;
  308. union Vmxnet3_GenericDesc *gdesc;
  309. gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
  310. while (VMXNET3_TCD_GET_GEN(&gdesc->tcd) == tq->comp_ring.gen) {
  311. completed += vmxnet3_unmap_pkt(VMXNET3_TCD_GET_TXIDX(
  312. &gdesc->tcd), tq, adapter->pdev,
  313. adapter);
  314. vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
  315. gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
  316. }
  317. if (completed) {
  318. spin_lock(&tq->tx_lock);
  319. if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
  320. vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
  321. VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
  322. netif_carrier_ok(adapter->netdev))) {
  323. vmxnet3_tq_wake(tq, adapter);
  324. }
  325. spin_unlock(&tq->tx_lock);
  326. }
  327. return completed;
  328. }
  329. static void
  330. vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
  331. struct vmxnet3_adapter *adapter)
  332. {
  333. int i;
  334. while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
  335. struct vmxnet3_tx_buf_info *tbi;
  336. tbi = tq->buf_info + tq->tx_ring.next2comp;
  337. vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
  338. if (tbi->skb) {
  339. dev_kfree_skb_any(tbi->skb);
  340. tbi->skb = NULL;
  341. }
  342. vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
  343. }
  344. /* sanity check, verify all buffers are indeed unmapped and freed */
  345. for (i = 0; i < tq->tx_ring.size; i++) {
  346. BUG_ON(tq->buf_info[i].skb != NULL ||
  347. tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
  348. }
  349. tq->tx_ring.gen = VMXNET3_INIT_GEN;
  350. tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
  351. tq->comp_ring.gen = VMXNET3_INIT_GEN;
  352. tq->comp_ring.next2proc = 0;
  353. }
  354. static void
  355. vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
  356. struct vmxnet3_adapter *adapter)
  357. {
  358. if (tq->tx_ring.base) {
  359. pci_free_consistent(adapter->pdev, tq->tx_ring.size *
  360. sizeof(struct Vmxnet3_TxDesc),
  361. tq->tx_ring.base, tq->tx_ring.basePA);
  362. tq->tx_ring.base = NULL;
  363. }
  364. if (tq->data_ring.base) {
  365. pci_free_consistent(adapter->pdev, tq->data_ring.size *
  366. sizeof(struct Vmxnet3_TxDataDesc),
  367. tq->data_ring.base, tq->data_ring.basePA);
  368. tq->data_ring.base = NULL;
  369. }
  370. if (tq->comp_ring.base) {
  371. pci_free_consistent(adapter->pdev, tq->comp_ring.size *
  372. sizeof(struct Vmxnet3_TxCompDesc),
  373. tq->comp_ring.base, tq->comp_ring.basePA);
  374. tq->comp_ring.base = NULL;
  375. }
  376. kfree(tq->buf_info);
  377. tq->buf_info = NULL;
  378. }
  379. /* Destroy all tx queues */
  380. void
  381. vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter)
  382. {
  383. int i;
  384. for (i = 0; i < adapter->num_tx_queues; i++)
  385. vmxnet3_tq_destroy(&adapter->tx_queue[i], adapter);
  386. }
  387. static void
  388. vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
  389. struct vmxnet3_adapter *adapter)
  390. {
  391. int i;
  392. /* reset the tx ring contents to 0 and reset the tx ring states */
  393. memset(tq->tx_ring.base, 0, tq->tx_ring.size *
  394. sizeof(struct Vmxnet3_TxDesc));
  395. tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
  396. tq->tx_ring.gen = VMXNET3_INIT_GEN;
  397. memset(tq->data_ring.base, 0, tq->data_ring.size *
  398. sizeof(struct Vmxnet3_TxDataDesc));
  399. /* reset the tx comp ring contents to 0 and reset comp ring states */
  400. memset(tq->comp_ring.base, 0, tq->comp_ring.size *
  401. sizeof(struct Vmxnet3_TxCompDesc));
  402. tq->comp_ring.next2proc = 0;
  403. tq->comp_ring.gen = VMXNET3_INIT_GEN;
  404. /* reset the bookkeeping data */
  405. memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
  406. for (i = 0; i < tq->tx_ring.size; i++)
  407. tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
  408. /* stats are not reset */
  409. }
  410. static int
  411. vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
  412. struct vmxnet3_adapter *adapter)
  413. {
  414. BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
  415. tq->comp_ring.base || tq->buf_info);
  416. tq->tx_ring.base = pci_alloc_consistent(adapter->pdev, tq->tx_ring.size
  417. * sizeof(struct Vmxnet3_TxDesc),
  418. &tq->tx_ring.basePA);
  419. if (!tq->tx_ring.base) {
  420. printk(KERN_ERR "%s: failed to allocate tx ring\n",
  421. adapter->netdev->name);
  422. goto err;
  423. }
  424. tq->data_ring.base = pci_alloc_consistent(adapter->pdev,
  425. tq->data_ring.size *
  426. sizeof(struct Vmxnet3_TxDataDesc),
  427. &tq->data_ring.basePA);
  428. if (!tq->data_ring.base) {
  429. printk(KERN_ERR "%s: failed to allocate data ring\n",
  430. adapter->netdev->name);
  431. goto err;
  432. }
  433. tq->comp_ring.base = pci_alloc_consistent(adapter->pdev,
  434. tq->comp_ring.size *
  435. sizeof(struct Vmxnet3_TxCompDesc),
  436. &tq->comp_ring.basePA);
  437. if (!tq->comp_ring.base) {
  438. printk(KERN_ERR "%s: failed to allocate tx comp ring\n",
  439. adapter->netdev->name);
  440. goto err;
  441. }
  442. tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
  443. GFP_KERNEL);
  444. if (!tq->buf_info)
  445. goto err;
  446. return 0;
  447. err:
  448. vmxnet3_tq_destroy(tq, adapter);
  449. return -ENOMEM;
  450. }
  451. static void
  452. vmxnet3_tq_cleanup_all(struct vmxnet3_adapter *adapter)
  453. {
  454. int i;
  455. for (i = 0; i < adapter->num_tx_queues; i++)
  456. vmxnet3_tq_cleanup(&adapter->tx_queue[i], adapter);
  457. }
  458. /*
  459. * starting from ring->next2fill, allocate rx buffers for the given ring
  460. * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
  461. * are allocated or allocation fails
  462. */
  463. static int
  464. vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
  465. int num_to_alloc, struct vmxnet3_adapter *adapter)
  466. {
  467. int num_allocated = 0;
  468. struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
  469. struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
  470. u32 val;
  471. while (num_allocated <= num_to_alloc) {
  472. struct vmxnet3_rx_buf_info *rbi;
  473. union Vmxnet3_GenericDesc *gd;
  474. rbi = rbi_base + ring->next2fill;
  475. gd = ring->base + ring->next2fill;
  476. if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
  477. if (rbi->skb == NULL) {
  478. rbi->skb = dev_alloc_skb(rbi->len +
  479. NET_IP_ALIGN);
  480. if (unlikely(rbi->skb == NULL)) {
  481. rq->stats.rx_buf_alloc_failure++;
  482. break;
  483. }
  484. rbi->skb->dev = adapter->netdev;
  485. skb_reserve(rbi->skb, NET_IP_ALIGN);
  486. rbi->dma_addr = pci_map_single(adapter->pdev,
  487. rbi->skb->data, rbi->len,
  488. PCI_DMA_FROMDEVICE);
  489. } else {
  490. /* rx buffer skipped by the device */
  491. }
  492. val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
  493. } else {
  494. BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
  495. rbi->len != PAGE_SIZE);
  496. if (rbi->page == NULL) {
  497. rbi->page = alloc_page(GFP_ATOMIC);
  498. if (unlikely(rbi->page == NULL)) {
  499. rq->stats.rx_buf_alloc_failure++;
  500. break;
  501. }
  502. rbi->dma_addr = pci_map_page(adapter->pdev,
  503. rbi->page, 0, PAGE_SIZE,
  504. PCI_DMA_FROMDEVICE);
  505. } else {
  506. /* rx buffers skipped by the device */
  507. }
  508. val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
  509. }
  510. BUG_ON(rbi->dma_addr == 0);
  511. gd->rxd.addr = cpu_to_le64(rbi->dma_addr);
  512. gd->dword[2] = cpu_to_le32((!ring->gen << VMXNET3_RXD_GEN_SHIFT)
  513. | val | rbi->len);
  514. /* Fill the last buffer but dont mark it ready, or else the
  515. * device will think that the queue is full */
  516. if (num_allocated == num_to_alloc)
  517. break;
  518. gd->dword[2] |= cpu_to_le32(ring->gen << VMXNET3_RXD_GEN_SHIFT);
  519. num_allocated++;
  520. vmxnet3_cmd_ring_adv_next2fill(ring);
  521. }
  522. rq->uncommitted[ring_idx] += num_allocated;
  523. dev_dbg(&adapter->netdev->dev,
  524. "alloc_rx_buf: %d allocated, next2fill %u, next2comp "
  525. "%u, uncommitted %u\n", num_allocated, ring->next2fill,
  526. ring->next2comp, rq->uncommitted[ring_idx]);
  527. /* so that the device can distinguish a full ring and an empty ring */
  528. BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
  529. return num_allocated;
  530. }
  531. static void
  532. vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
  533. struct vmxnet3_rx_buf_info *rbi)
  534. {
  535. struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
  536. skb_shinfo(skb)->nr_frags;
  537. BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
  538. __skb_frag_set_page(frag, rbi->page);
  539. frag->page_offset = 0;
  540. skb_frag_size_set(frag, rcd->len);
  541. skb->data_len += rcd->len;
  542. skb->truesize += PAGE_SIZE;
  543. skb_shinfo(skb)->nr_frags++;
  544. }
  545. static void
  546. vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
  547. struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
  548. struct vmxnet3_adapter *adapter)
  549. {
  550. u32 dw2, len;
  551. unsigned long buf_offset;
  552. int i;
  553. union Vmxnet3_GenericDesc *gdesc;
  554. struct vmxnet3_tx_buf_info *tbi = NULL;
  555. BUG_ON(ctx->copy_size > skb_headlen(skb));
  556. /* use the previous gen bit for the SOP desc */
  557. dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
  558. ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
  559. gdesc = ctx->sop_txd; /* both loops below can be skipped */
  560. /* no need to map the buffer if headers are copied */
  561. if (ctx->copy_size) {
  562. ctx->sop_txd->txd.addr = cpu_to_le64(tq->data_ring.basePA +
  563. tq->tx_ring.next2fill *
  564. sizeof(struct Vmxnet3_TxDataDesc));
  565. ctx->sop_txd->dword[2] = cpu_to_le32(dw2 | ctx->copy_size);
  566. ctx->sop_txd->dword[3] = 0;
  567. tbi = tq->buf_info + tq->tx_ring.next2fill;
  568. tbi->map_type = VMXNET3_MAP_NONE;
  569. dev_dbg(&adapter->netdev->dev,
  570. "txd[%u]: 0x%Lx 0x%x 0x%x\n",
  571. tq->tx_ring.next2fill,
  572. le64_to_cpu(ctx->sop_txd->txd.addr),
  573. ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
  574. vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
  575. /* use the right gen for non-SOP desc */
  576. dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
  577. }
  578. /* linear part can use multiple tx desc if it's big */
  579. len = skb_headlen(skb) - ctx->copy_size;
  580. buf_offset = ctx->copy_size;
  581. while (len) {
  582. u32 buf_size;
  583. if (len < VMXNET3_MAX_TX_BUF_SIZE) {
  584. buf_size = len;
  585. dw2 |= len;
  586. } else {
  587. buf_size = VMXNET3_MAX_TX_BUF_SIZE;
  588. /* spec says that for TxDesc.len, 0 == 2^14 */
  589. }
  590. tbi = tq->buf_info + tq->tx_ring.next2fill;
  591. tbi->map_type = VMXNET3_MAP_SINGLE;
  592. tbi->dma_addr = pci_map_single(adapter->pdev,
  593. skb->data + buf_offset, buf_size,
  594. PCI_DMA_TODEVICE);
  595. tbi->len = buf_size;
  596. gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
  597. BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
  598. gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
  599. gdesc->dword[2] = cpu_to_le32(dw2);
  600. gdesc->dword[3] = 0;
  601. dev_dbg(&adapter->netdev->dev,
  602. "txd[%u]: 0x%Lx 0x%x 0x%x\n",
  603. tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
  604. le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
  605. vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
  606. dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
  607. len -= buf_size;
  608. buf_offset += buf_size;
  609. }
  610. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  611. const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  612. tbi = tq->buf_info + tq->tx_ring.next2fill;
  613. tbi->map_type = VMXNET3_MAP_PAGE;
  614. tbi->dma_addr = skb_frag_dma_map(&adapter->pdev->dev, frag,
  615. 0, skb_frag_size(frag),
  616. DMA_TO_DEVICE);
  617. tbi->len = skb_frag_size(frag);
  618. gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
  619. BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
  620. gdesc->txd.addr = cpu_to_le64(tbi->dma_addr);
  621. gdesc->dword[2] = cpu_to_le32(dw2 | skb_frag_size(frag));
  622. gdesc->dword[3] = 0;
  623. dev_dbg(&adapter->netdev->dev,
  624. "txd[%u]: 0x%llu %u %u\n",
  625. tq->tx_ring.next2fill, le64_to_cpu(gdesc->txd.addr),
  626. le32_to_cpu(gdesc->dword[2]), gdesc->dword[3]);
  627. vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
  628. dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
  629. }
  630. ctx->eop_txd = gdesc;
  631. /* set the last buf_info for the pkt */
  632. tbi->skb = skb;
  633. tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
  634. }
  635. /* Init all tx queues */
  636. static void
  637. vmxnet3_tq_init_all(struct vmxnet3_adapter *adapter)
  638. {
  639. int i;
  640. for (i = 0; i < adapter->num_tx_queues; i++)
  641. vmxnet3_tq_init(&adapter->tx_queue[i], adapter);
  642. }
  643. /*
  644. * parse and copy relevant protocol headers:
  645. * For a tso pkt, relevant headers are L2/3/4 including options
  646. * For a pkt requesting csum offloading, they are L2/3 and may include L4
  647. * if it's a TCP/UDP pkt
  648. *
  649. * Returns:
  650. * -1: error happens during parsing
  651. * 0: protocol headers parsed, but too big to be copied
  652. * 1: protocol headers parsed and copied
  653. *
  654. * Other effects:
  655. * 1. related *ctx fields are updated.
  656. * 2. ctx->copy_size is # of bytes copied
  657. * 3. the portion copied is guaranteed to be in the linear part
  658. *
  659. */
  660. static int
  661. vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
  662. struct vmxnet3_tx_ctx *ctx,
  663. struct vmxnet3_adapter *adapter)
  664. {
  665. struct Vmxnet3_TxDataDesc *tdd;
  666. if (ctx->mss) { /* TSO */
  667. ctx->eth_ip_hdr_size = skb_transport_offset(skb);
  668. ctx->l4_hdr_size = tcp_hdrlen(skb);
  669. ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
  670. } else {
  671. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  672. ctx->eth_ip_hdr_size = skb_checksum_start_offset(skb);
  673. if (ctx->ipv4) {
  674. const struct iphdr *iph = ip_hdr(skb);
  675. if (iph->protocol == IPPROTO_TCP)
  676. ctx->l4_hdr_size = tcp_hdrlen(skb);
  677. else if (iph->protocol == IPPROTO_UDP)
  678. ctx->l4_hdr_size = sizeof(struct udphdr);
  679. else
  680. ctx->l4_hdr_size = 0;
  681. } else {
  682. /* for simplicity, don't copy L4 headers */
  683. ctx->l4_hdr_size = 0;
  684. }
  685. ctx->copy_size = min(ctx->eth_ip_hdr_size +
  686. ctx->l4_hdr_size, skb->len);
  687. } else {
  688. ctx->eth_ip_hdr_size = 0;
  689. ctx->l4_hdr_size = 0;
  690. /* copy as much as allowed */
  691. ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
  692. , skb_headlen(skb));
  693. }
  694. /* make sure headers are accessible directly */
  695. if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
  696. goto err;
  697. }
  698. if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
  699. tq->stats.oversized_hdr++;
  700. ctx->copy_size = 0;
  701. return 0;
  702. }
  703. tdd = tq->data_ring.base + tq->tx_ring.next2fill;
  704. memcpy(tdd->data, skb->data, ctx->copy_size);
  705. dev_dbg(&adapter->netdev->dev,
  706. "copy %u bytes to dataRing[%u]\n",
  707. ctx->copy_size, tq->tx_ring.next2fill);
  708. return 1;
  709. err:
  710. return -1;
  711. }
  712. static void
  713. vmxnet3_prepare_tso(struct sk_buff *skb,
  714. struct vmxnet3_tx_ctx *ctx)
  715. {
  716. struct tcphdr *tcph = tcp_hdr(skb);
  717. if (ctx->ipv4) {
  718. struct iphdr *iph = ip_hdr(skb);
  719. iph->check = 0;
  720. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
  721. IPPROTO_TCP, 0);
  722. } else {
  723. struct ipv6hdr *iph = ipv6_hdr(skb);
  724. tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
  725. IPPROTO_TCP, 0);
  726. }
  727. }
  728. /*
  729. * Transmits a pkt thru a given tq
  730. * Returns:
  731. * NETDEV_TX_OK: descriptors are setup successfully
  732. * NETDEV_TX_OK: error occurred, the pkt is dropped
  733. * NETDEV_TX_BUSY: tx ring is full, queue is stopped
  734. *
  735. * Side-effects:
  736. * 1. tx ring may be changed
  737. * 2. tq stats may be updated accordingly
  738. * 3. shared->txNumDeferred may be updated
  739. */
  740. static int
  741. vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
  742. struct vmxnet3_adapter *adapter, struct net_device *netdev)
  743. {
  744. int ret;
  745. u32 count;
  746. unsigned long flags;
  747. struct vmxnet3_tx_ctx ctx;
  748. union Vmxnet3_GenericDesc *gdesc;
  749. #ifdef __BIG_ENDIAN_BITFIELD
  750. /* Use temporary descriptor to avoid touching bits multiple times */
  751. union Vmxnet3_GenericDesc tempTxDesc;
  752. #endif
  753. /* conservatively estimate # of descriptors to use */
  754. count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) +
  755. skb_shinfo(skb)->nr_frags + 1;
  756. ctx.ipv4 = (vlan_get_protocol(skb) == cpu_to_be16(ETH_P_IP));
  757. ctx.mss = skb_shinfo(skb)->gso_size;
  758. if (ctx.mss) {
  759. if (skb_header_cloned(skb)) {
  760. if (unlikely(pskb_expand_head(skb, 0, 0,
  761. GFP_ATOMIC) != 0)) {
  762. tq->stats.drop_tso++;
  763. goto drop_pkt;
  764. }
  765. tq->stats.copy_skb_header++;
  766. }
  767. vmxnet3_prepare_tso(skb, &ctx);
  768. } else {
  769. if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
  770. /* non-tso pkts must not use more than
  771. * VMXNET3_MAX_TXD_PER_PKT entries
  772. */
  773. if (skb_linearize(skb) != 0) {
  774. tq->stats.drop_too_many_frags++;
  775. goto drop_pkt;
  776. }
  777. tq->stats.linearized++;
  778. /* recalculate the # of descriptors to use */
  779. count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
  780. }
  781. }
  782. spin_lock_irqsave(&tq->tx_lock, flags);
  783. if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
  784. tq->stats.tx_ring_full++;
  785. dev_dbg(&adapter->netdev->dev,
  786. "tx queue stopped on %s, next2comp %u"
  787. " next2fill %u\n", adapter->netdev->name,
  788. tq->tx_ring.next2comp, tq->tx_ring.next2fill);
  789. vmxnet3_tq_stop(tq, adapter);
  790. spin_unlock_irqrestore(&tq->tx_lock, flags);
  791. return NETDEV_TX_BUSY;
  792. }
  793. ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
  794. if (ret >= 0) {
  795. BUG_ON(ret <= 0 && ctx.copy_size != 0);
  796. /* hdrs parsed, check against other limits */
  797. if (ctx.mss) {
  798. if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
  799. VMXNET3_MAX_TX_BUF_SIZE)) {
  800. goto hdr_too_big;
  801. }
  802. } else {
  803. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  804. if (unlikely(ctx.eth_ip_hdr_size +
  805. skb->csum_offset >
  806. VMXNET3_MAX_CSUM_OFFSET)) {
  807. goto hdr_too_big;
  808. }
  809. }
  810. }
  811. } else {
  812. tq->stats.drop_hdr_inspect_err++;
  813. goto unlock_drop_pkt;
  814. }
  815. /* fill tx descs related to addr & len */
  816. vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
  817. /* setup the EOP desc */
  818. ctx.eop_txd->dword[3] = cpu_to_le32(VMXNET3_TXD_CQ | VMXNET3_TXD_EOP);
  819. /* setup the SOP desc */
  820. #ifdef __BIG_ENDIAN_BITFIELD
  821. gdesc = &tempTxDesc;
  822. gdesc->dword[2] = ctx.sop_txd->dword[2];
  823. gdesc->dword[3] = ctx.sop_txd->dword[3];
  824. #else
  825. gdesc = ctx.sop_txd;
  826. #endif
  827. if (ctx.mss) {
  828. gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
  829. gdesc->txd.om = VMXNET3_OM_TSO;
  830. gdesc->txd.msscof = ctx.mss;
  831. le32_add_cpu(&tq->shared->txNumDeferred, (skb->len -
  832. gdesc->txd.hlen + ctx.mss - 1) / ctx.mss);
  833. } else {
  834. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  835. gdesc->txd.hlen = ctx.eth_ip_hdr_size;
  836. gdesc->txd.om = VMXNET3_OM_CSUM;
  837. gdesc->txd.msscof = ctx.eth_ip_hdr_size +
  838. skb->csum_offset;
  839. } else {
  840. gdesc->txd.om = 0;
  841. gdesc->txd.msscof = 0;
  842. }
  843. le32_add_cpu(&tq->shared->txNumDeferred, 1);
  844. }
  845. if (vlan_tx_tag_present(skb)) {
  846. gdesc->txd.ti = 1;
  847. gdesc->txd.tci = vlan_tx_tag_get(skb);
  848. }
  849. /* finally flips the GEN bit of the SOP desc. */
  850. gdesc->dword[2] = cpu_to_le32(le32_to_cpu(gdesc->dword[2]) ^
  851. VMXNET3_TXD_GEN);
  852. #ifdef __BIG_ENDIAN_BITFIELD
  853. /* Finished updating in bitfields of Tx Desc, so write them in original
  854. * place.
  855. */
  856. vmxnet3_TxDescToLe((struct Vmxnet3_TxDesc *)gdesc,
  857. (struct Vmxnet3_TxDesc *)ctx.sop_txd);
  858. gdesc = ctx.sop_txd;
  859. #endif
  860. dev_dbg(&adapter->netdev->dev,
  861. "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
  862. (u32)((union Vmxnet3_GenericDesc *)ctx.sop_txd -
  863. tq->tx_ring.base), le64_to_cpu(gdesc->txd.addr),
  864. le32_to_cpu(gdesc->dword[2]), le32_to_cpu(gdesc->dword[3]));
  865. spin_unlock_irqrestore(&tq->tx_lock, flags);
  866. if (le32_to_cpu(tq->shared->txNumDeferred) >=
  867. le32_to_cpu(tq->shared->txThreshold)) {
  868. tq->shared->txNumDeferred = 0;
  869. VMXNET3_WRITE_BAR0_REG(adapter,
  870. VMXNET3_REG_TXPROD + tq->qid * 8,
  871. tq->tx_ring.next2fill);
  872. }
  873. return NETDEV_TX_OK;
  874. hdr_too_big:
  875. tq->stats.drop_oversized_hdr++;
  876. unlock_drop_pkt:
  877. spin_unlock_irqrestore(&tq->tx_lock, flags);
  878. drop_pkt:
  879. tq->stats.drop_total++;
  880. dev_kfree_skb(skb);
  881. return NETDEV_TX_OK;
  882. }
  883. static netdev_tx_t
  884. vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  885. {
  886. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  887. BUG_ON(skb->queue_mapping > adapter->num_tx_queues);
  888. return vmxnet3_tq_xmit(skb,
  889. &adapter->tx_queue[skb->queue_mapping],
  890. adapter, netdev);
  891. }
  892. static void
  893. vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
  894. struct sk_buff *skb,
  895. union Vmxnet3_GenericDesc *gdesc)
  896. {
  897. if (!gdesc->rcd.cnc && adapter->netdev->features & NETIF_F_RXCSUM) {
  898. /* typical case: TCP/UDP over IP and both csums are correct */
  899. if ((le32_to_cpu(gdesc->dword[3]) & VMXNET3_RCD_CSUM_OK) ==
  900. VMXNET3_RCD_CSUM_OK) {
  901. skb->ip_summed = CHECKSUM_UNNECESSARY;
  902. BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
  903. BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
  904. BUG_ON(gdesc->rcd.frg);
  905. } else {
  906. if (gdesc->rcd.csum) {
  907. skb->csum = htons(gdesc->rcd.csum);
  908. skb->ip_summed = CHECKSUM_PARTIAL;
  909. } else {
  910. skb_checksum_none_assert(skb);
  911. }
  912. }
  913. } else {
  914. skb_checksum_none_assert(skb);
  915. }
  916. }
  917. static void
  918. vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
  919. struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
  920. {
  921. rq->stats.drop_err++;
  922. if (!rcd->fcs)
  923. rq->stats.drop_fcs++;
  924. rq->stats.drop_total++;
  925. /*
  926. * We do not unmap and chain the rx buffer to the skb.
  927. * We basically pretend this buffer is not used and will be recycled
  928. * by vmxnet3_rq_alloc_rx_buf()
  929. */
  930. /*
  931. * ctx->skb may be NULL if this is the first and the only one
  932. * desc for the pkt
  933. */
  934. if (ctx->skb)
  935. dev_kfree_skb_irq(ctx->skb);
  936. ctx->skb = NULL;
  937. }
  938. static int
  939. vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
  940. struct vmxnet3_adapter *adapter, int quota)
  941. {
  942. static const u32 rxprod_reg[2] = {
  943. VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2
  944. };
  945. u32 num_rxd = 0;
  946. bool skip_page_frags = false;
  947. struct Vmxnet3_RxCompDesc *rcd;
  948. struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
  949. #ifdef __BIG_ENDIAN_BITFIELD
  950. struct Vmxnet3_RxDesc rxCmdDesc;
  951. struct Vmxnet3_RxCompDesc rxComp;
  952. #endif
  953. vmxnet3_getRxComp(rcd, &rq->comp_ring.base[rq->comp_ring.next2proc].rcd,
  954. &rxComp);
  955. while (rcd->gen == rq->comp_ring.gen) {
  956. struct vmxnet3_rx_buf_info *rbi;
  957. struct sk_buff *skb, *new_skb = NULL;
  958. struct page *new_page = NULL;
  959. int num_to_alloc;
  960. struct Vmxnet3_RxDesc *rxd;
  961. u32 idx, ring_idx;
  962. struct vmxnet3_cmd_ring *ring = NULL;
  963. if (num_rxd >= quota) {
  964. /* we may stop even before we see the EOP desc of
  965. * the current pkt
  966. */
  967. break;
  968. }
  969. num_rxd++;
  970. BUG_ON(rcd->rqID != rq->qid && rcd->rqID != rq->qid2);
  971. idx = rcd->rxdIdx;
  972. ring_idx = rcd->rqID < adapter->num_rx_queues ? 0 : 1;
  973. ring = rq->rx_ring + ring_idx;
  974. vmxnet3_getRxDesc(rxd, &rq->rx_ring[ring_idx].base[idx].rxd,
  975. &rxCmdDesc);
  976. rbi = rq->buf_info[ring_idx] + idx;
  977. BUG_ON(rxd->addr != rbi->dma_addr ||
  978. rxd->len != rbi->len);
  979. if (unlikely(rcd->eop && rcd->err)) {
  980. vmxnet3_rx_error(rq, rcd, ctx, adapter);
  981. goto rcd_done;
  982. }
  983. if (rcd->sop) { /* first buf of the pkt */
  984. BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
  985. rcd->rqID != rq->qid);
  986. BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
  987. BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
  988. if (unlikely(rcd->len == 0)) {
  989. /* Pretend the rx buffer is skipped. */
  990. BUG_ON(!(rcd->sop && rcd->eop));
  991. dev_dbg(&adapter->netdev->dev,
  992. "rxRing[%u][%u] 0 length\n",
  993. ring_idx, idx);
  994. goto rcd_done;
  995. }
  996. skip_page_frags = false;
  997. ctx->skb = rbi->skb;
  998. new_skb = dev_alloc_skb(rbi->len + NET_IP_ALIGN);
  999. if (new_skb == NULL) {
  1000. /* Skb allocation failed, do not handover this
  1001. * skb to stack. Reuse it. Drop the existing pkt
  1002. */
  1003. rq->stats.rx_buf_alloc_failure++;
  1004. ctx->skb = NULL;
  1005. rq->stats.drop_total++;
  1006. skip_page_frags = true;
  1007. goto rcd_done;
  1008. }
  1009. pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
  1010. PCI_DMA_FROMDEVICE);
  1011. skb_put(ctx->skb, rcd->len);
  1012. /* Immediate refill */
  1013. new_skb->dev = adapter->netdev;
  1014. skb_reserve(new_skb, NET_IP_ALIGN);
  1015. rbi->skb = new_skb;
  1016. rbi->dma_addr = pci_map_single(adapter->pdev,
  1017. rbi->skb->data, rbi->len,
  1018. PCI_DMA_FROMDEVICE);
  1019. rxd->addr = cpu_to_le64(rbi->dma_addr);
  1020. rxd->len = rbi->len;
  1021. } else {
  1022. BUG_ON(ctx->skb == NULL && !skip_page_frags);
  1023. /* non SOP buffer must be type 1 in most cases */
  1024. BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE);
  1025. BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
  1026. /* If an sop buffer was dropped, skip all
  1027. * following non-sop fragments. They will be reused.
  1028. */
  1029. if (skip_page_frags)
  1030. goto rcd_done;
  1031. new_page = alloc_page(GFP_ATOMIC);
  1032. if (unlikely(new_page == NULL)) {
  1033. /* Replacement page frag could not be allocated.
  1034. * Reuse this page. Drop the pkt and free the
  1035. * skb which contained this page as a frag. Skip
  1036. * processing all the following non-sop frags.
  1037. */
  1038. rq->stats.rx_buf_alloc_failure++;
  1039. dev_kfree_skb(ctx->skb);
  1040. ctx->skb = NULL;
  1041. skip_page_frags = true;
  1042. goto rcd_done;
  1043. }
  1044. if (rcd->len) {
  1045. pci_unmap_page(adapter->pdev,
  1046. rbi->dma_addr, rbi->len,
  1047. PCI_DMA_FROMDEVICE);
  1048. vmxnet3_append_frag(ctx->skb, rcd, rbi);
  1049. }
  1050. /* Immediate refill */
  1051. rbi->page = new_page;
  1052. rbi->dma_addr = pci_map_page(adapter->pdev, rbi->page,
  1053. 0, PAGE_SIZE,
  1054. PCI_DMA_FROMDEVICE);
  1055. rxd->addr = cpu_to_le64(rbi->dma_addr);
  1056. rxd->len = rbi->len;
  1057. }
  1058. skb = ctx->skb;
  1059. if (rcd->eop) {
  1060. skb->len += skb->data_len;
  1061. vmxnet3_rx_csum(adapter, skb,
  1062. (union Vmxnet3_GenericDesc *)rcd);
  1063. skb->protocol = eth_type_trans(skb, adapter->netdev);
  1064. if (unlikely(rcd->ts))
  1065. __vlan_hwaccel_put_tag(skb, rcd->tci);
  1066. if (adapter->netdev->features & NETIF_F_LRO)
  1067. netif_receive_skb(skb);
  1068. else
  1069. napi_gro_receive(&rq->napi, skb);
  1070. ctx->skb = NULL;
  1071. }
  1072. rcd_done:
  1073. /* device may have skipped some rx descs */
  1074. ring->next2comp = idx;
  1075. num_to_alloc = vmxnet3_cmd_ring_desc_avail(ring);
  1076. ring = rq->rx_ring + ring_idx;
  1077. while (num_to_alloc) {
  1078. vmxnet3_getRxDesc(rxd, &ring->base[ring->next2fill].rxd,
  1079. &rxCmdDesc);
  1080. BUG_ON(!rxd->addr);
  1081. /* Recv desc is ready to be used by the device */
  1082. rxd->gen = ring->gen;
  1083. vmxnet3_cmd_ring_adv_next2fill(ring);
  1084. num_to_alloc--;
  1085. }
  1086. /* if needed, update the register */
  1087. if (unlikely(rq->shared->updateRxProd)) {
  1088. VMXNET3_WRITE_BAR0_REG(adapter,
  1089. rxprod_reg[ring_idx] + rq->qid * 8,
  1090. ring->next2fill);
  1091. rq->uncommitted[ring_idx] = 0;
  1092. }
  1093. vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
  1094. vmxnet3_getRxComp(rcd,
  1095. &rq->comp_ring.base[rq->comp_ring.next2proc].rcd, &rxComp);
  1096. }
  1097. return num_rxd;
  1098. }
  1099. static void
  1100. vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
  1101. struct vmxnet3_adapter *adapter)
  1102. {
  1103. u32 i, ring_idx;
  1104. struct Vmxnet3_RxDesc *rxd;
  1105. for (ring_idx = 0; ring_idx < 2; ring_idx++) {
  1106. for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
  1107. #ifdef __BIG_ENDIAN_BITFIELD
  1108. struct Vmxnet3_RxDesc rxDesc;
  1109. #endif
  1110. vmxnet3_getRxDesc(rxd,
  1111. &rq->rx_ring[ring_idx].base[i].rxd, &rxDesc);
  1112. if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
  1113. rq->buf_info[ring_idx][i].skb) {
  1114. pci_unmap_single(adapter->pdev, rxd->addr,
  1115. rxd->len, PCI_DMA_FROMDEVICE);
  1116. dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
  1117. rq->buf_info[ring_idx][i].skb = NULL;
  1118. } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
  1119. rq->buf_info[ring_idx][i].page) {
  1120. pci_unmap_page(adapter->pdev, rxd->addr,
  1121. rxd->len, PCI_DMA_FROMDEVICE);
  1122. put_page(rq->buf_info[ring_idx][i].page);
  1123. rq->buf_info[ring_idx][i].page = NULL;
  1124. }
  1125. }
  1126. rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
  1127. rq->rx_ring[ring_idx].next2fill =
  1128. rq->rx_ring[ring_idx].next2comp = 0;
  1129. rq->uncommitted[ring_idx] = 0;
  1130. }
  1131. rq->comp_ring.gen = VMXNET3_INIT_GEN;
  1132. rq->comp_ring.next2proc = 0;
  1133. }
  1134. static void
  1135. vmxnet3_rq_cleanup_all(struct vmxnet3_adapter *adapter)
  1136. {
  1137. int i;
  1138. for (i = 0; i < adapter->num_rx_queues; i++)
  1139. vmxnet3_rq_cleanup(&adapter->rx_queue[i], adapter);
  1140. }
  1141. void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
  1142. struct vmxnet3_adapter *adapter)
  1143. {
  1144. int i;
  1145. int j;
  1146. /* all rx buffers must have already been freed */
  1147. for (i = 0; i < 2; i++) {
  1148. if (rq->buf_info[i]) {
  1149. for (j = 0; j < rq->rx_ring[i].size; j++)
  1150. BUG_ON(rq->buf_info[i][j].page != NULL);
  1151. }
  1152. }
  1153. kfree(rq->buf_info[0]);
  1154. for (i = 0; i < 2; i++) {
  1155. if (rq->rx_ring[i].base) {
  1156. pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
  1157. * sizeof(struct Vmxnet3_RxDesc),
  1158. rq->rx_ring[i].base,
  1159. rq->rx_ring[i].basePA);
  1160. rq->rx_ring[i].base = NULL;
  1161. }
  1162. rq->buf_info[i] = NULL;
  1163. }
  1164. if (rq->comp_ring.base) {
  1165. pci_free_consistent(adapter->pdev, rq->comp_ring.size *
  1166. sizeof(struct Vmxnet3_RxCompDesc),
  1167. rq->comp_ring.base, rq->comp_ring.basePA);
  1168. rq->comp_ring.base = NULL;
  1169. }
  1170. }
  1171. static int
  1172. vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
  1173. struct vmxnet3_adapter *adapter)
  1174. {
  1175. int i;
  1176. /* initialize buf_info */
  1177. for (i = 0; i < rq->rx_ring[0].size; i++) {
  1178. /* 1st buf for a pkt is skbuff */
  1179. if (i % adapter->rx_buf_per_pkt == 0) {
  1180. rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
  1181. rq->buf_info[0][i].len = adapter->skb_buf_size;
  1182. } else { /* subsequent bufs for a pkt is frag */
  1183. rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
  1184. rq->buf_info[0][i].len = PAGE_SIZE;
  1185. }
  1186. }
  1187. for (i = 0; i < rq->rx_ring[1].size; i++) {
  1188. rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
  1189. rq->buf_info[1][i].len = PAGE_SIZE;
  1190. }
  1191. /* reset internal state and allocate buffers for both rings */
  1192. for (i = 0; i < 2; i++) {
  1193. rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
  1194. rq->uncommitted[i] = 0;
  1195. memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
  1196. sizeof(struct Vmxnet3_RxDesc));
  1197. rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
  1198. }
  1199. if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
  1200. adapter) == 0) {
  1201. /* at least has 1 rx buffer for the 1st ring */
  1202. return -ENOMEM;
  1203. }
  1204. vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
  1205. /* reset the comp ring */
  1206. rq->comp_ring.next2proc = 0;
  1207. memset(rq->comp_ring.base, 0, rq->comp_ring.size *
  1208. sizeof(struct Vmxnet3_RxCompDesc));
  1209. rq->comp_ring.gen = VMXNET3_INIT_GEN;
  1210. /* reset rxctx */
  1211. rq->rx_ctx.skb = NULL;
  1212. /* stats are not reset */
  1213. return 0;
  1214. }
  1215. static int
  1216. vmxnet3_rq_init_all(struct vmxnet3_adapter *adapter)
  1217. {
  1218. int i, err = 0;
  1219. for (i = 0; i < adapter->num_rx_queues; i++) {
  1220. err = vmxnet3_rq_init(&adapter->rx_queue[i], adapter);
  1221. if (unlikely(err)) {
  1222. dev_err(&adapter->netdev->dev, "%s: failed to "
  1223. "initialize rx queue%i\n",
  1224. adapter->netdev->name, i);
  1225. break;
  1226. }
  1227. }
  1228. return err;
  1229. }
  1230. static int
  1231. vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
  1232. {
  1233. int i;
  1234. size_t sz;
  1235. struct vmxnet3_rx_buf_info *bi;
  1236. for (i = 0; i < 2; i++) {
  1237. sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
  1238. rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
  1239. &rq->rx_ring[i].basePA);
  1240. if (!rq->rx_ring[i].base) {
  1241. printk(KERN_ERR "%s: failed to allocate rx ring %d\n",
  1242. adapter->netdev->name, i);
  1243. goto err;
  1244. }
  1245. }
  1246. sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
  1247. rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
  1248. &rq->comp_ring.basePA);
  1249. if (!rq->comp_ring.base) {
  1250. printk(KERN_ERR "%s: failed to allocate rx comp ring\n",
  1251. adapter->netdev->name);
  1252. goto err;
  1253. }
  1254. sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
  1255. rq->rx_ring[1].size);
  1256. bi = kzalloc(sz, GFP_KERNEL);
  1257. if (!bi)
  1258. goto err;
  1259. rq->buf_info[0] = bi;
  1260. rq->buf_info[1] = bi + rq->rx_ring[0].size;
  1261. return 0;
  1262. err:
  1263. vmxnet3_rq_destroy(rq, adapter);
  1264. return -ENOMEM;
  1265. }
  1266. static int
  1267. vmxnet3_rq_create_all(struct vmxnet3_adapter *adapter)
  1268. {
  1269. int i, err = 0;
  1270. for (i = 0; i < adapter->num_rx_queues; i++) {
  1271. err = vmxnet3_rq_create(&adapter->rx_queue[i], adapter);
  1272. if (unlikely(err)) {
  1273. dev_err(&adapter->netdev->dev,
  1274. "%s: failed to create rx queue%i\n",
  1275. adapter->netdev->name, i);
  1276. goto err_out;
  1277. }
  1278. }
  1279. return err;
  1280. err_out:
  1281. vmxnet3_rq_destroy_all(adapter);
  1282. return err;
  1283. }
  1284. /* Multiple queue aware polling function for tx and rx */
  1285. static int
  1286. vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
  1287. {
  1288. int rcd_done = 0, i;
  1289. if (unlikely(adapter->shared->ecr))
  1290. vmxnet3_process_events(adapter);
  1291. for (i = 0; i < adapter->num_tx_queues; i++)
  1292. vmxnet3_tq_tx_complete(&adapter->tx_queue[i], adapter);
  1293. for (i = 0; i < adapter->num_rx_queues; i++)
  1294. rcd_done += vmxnet3_rq_rx_complete(&adapter->rx_queue[i],
  1295. adapter, budget);
  1296. return rcd_done;
  1297. }
  1298. static int
  1299. vmxnet3_poll(struct napi_struct *napi, int budget)
  1300. {
  1301. struct vmxnet3_rx_queue *rx_queue = container_of(napi,
  1302. struct vmxnet3_rx_queue, napi);
  1303. int rxd_done;
  1304. rxd_done = vmxnet3_do_poll(rx_queue->adapter, budget);
  1305. if (rxd_done < budget) {
  1306. napi_complete(napi);
  1307. vmxnet3_enable_all_intrs(rx_queue->adapter);
  1308. }
  1309. return rxd_done;
  1310. }
  1311. /*
  1312. * NAPI polling function for MSI-X mode with multiple Rx queues
  1313. * Returns the # of the NAPI credit consumed (# of rx descriptors processed)
  1314. */
  1315. static int
  1316. vmxnet3_poll_rx_only(struct napi_struct *napi, int budget)
  1317. {
  1318. struct vmxnet3_rx_queue *rq = container_of(napi,
  1319. struct vmxnet3_rx_queue, napi);
  1320. struct vmxnet3_adapter *adapter = rq->adapter;
  1321. int rxd_done;
  1322. /* When sharing interrupt with corresponding tx queue, process
  1323. * tx completions in that queue as well
  1324. */
  1325. if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE) {
  1326. struct vmxnet3_tx_queue *tq =
  1327. &adapter->tx_queue[rq - adapter->rx_queue];
  1328. vmxnet3_tq_tx_complete(tq, adapter);
  1329. }
  1330. rxd_done = vmxnet3_rq_rx_complete(rq, adapter, budget);
  1331. if (rxd_done < budget) {
  1332. napi_complete(napi);
  1333. vmxnet3_enable_intr(adapter, rq->comp_ring.intr_idx);
  1334. }
  1335. return rxd_done;
  1336. }
  1337. #ifdef CONFIG_PCI_MSI
  1338. /*
  1339. * Handle completion interrupts on tx queues
  1340. * Returns whether or not the intr is handled
  1341. */
  1342. static irqreturn_t
  1343. vmxnet3_msix_tx(int irq, void *data)
  1344. {
  1345. struct vmxnet3_tx_queue *tq = data;
  1346. struct vmxnet3_adapter *adapter = tq->adapter;
  1347. if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
  1348. vmxnet3_disable_intr(adapter, tq->comp_ring.intr_idx);
  1349. /* Handle the case where only one irq is allocate for all tx queues */
  1350. if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
  1351. int i;
  1352. for (i = 0; i < adapter->num_tx_queues; i++) {
  1353. struct vmxnet3_tx_queue *txq = &adapter->tx_queue[i];
  1354. vmxnet3_tq_tx_complete(txq, adapter);
  1355. }
  1356. } else {
  1357. vmxnet3_tq_tx_complete(tq, adapter);
  1358. }
  1359. vmxnet3_enable_intr(adapter, tq->comp_ring.intr_idx);
  1360. return IRQ_HANDLED;
  1361. }
  1362. /*
  1363. * Handle completion interrupts on rx queues. Returns whether or not the
  1364. * intr is handled
  1365. */
  1366. static irqreturn_t
  1367. vmxnet3_msix_rx(int irq, void *data)
  1368. {
  1369. struct vmxnet3_rx_queue *rq = data;
  1370. struct vmxnet3_adapter *adapter = rq->adapter;
  1371. /* disable intr if needed */
  1372. if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
  1373. vmxnet3_disable_intr(adapter, rq->comp_ring.intr_idx);
  1374. napi_schedule(&rq->napi);
  1375. return IRQ_HANDLED;
  1376. }
  1377. /*
  1378. *----------------------------------------------------------------------------
  1379. *
  1380. * vmxnet3_msix_event --
  1381. *
  1382. * vmxnet3 msix event intr handler
  1383. *
  1384. * Result:
  1385. * whether or not the intr is handled
  1386. *
  1387. *----------------------------------------------------------------------------
  1388. */
  1389. static irqreturn_t
  1390. vmxnet3_msix_event(int irq, void *data)
  1391. {
  1392. struct net_device *dev = data;
  1393. struct vmxnet3_adapter *adapter = netdev_priv(dev);
  1394. /* disable intr if needed */
  1395. if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
  1396. vmxnet3_disable_intr(adapter, adapter->intr.event_intr_idx);
  1397. if (adapter->shared->ecr)
  1398. vmxnet3_process_events(adapter);
  1399. vmxnet3_enable_intr(adapter, adapter->intr.event_intr_idx);
  1400. return IRQ_HANDLED;
  1401. }
  1402. #endif /* CONFIG_PCI_MSI */
  1403. /* Interrupt handler for vmxnet3 */
  1404. static irqreturn_t
  1405. vmxnet3_intr(int irq, void *dev_id)
  1406. {
  1407. struct net_device *dev = dev_id;
  1408. struct vmxnet3_adapter *adapter = netdev_priv(dev);
  1409. if (adapter->intr.type == VMXNET3_IT_INTX) {
  1410. u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
  1411. if (unlikely(icr == 0))
  1412. /* not ours */
  1413. return IRQ_NONE;
  1414. }
  1415. /* disable intr if needed */
  1416. if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
  1417. vmxnet3_disable_all_intrs(adapter);
  1418. napi_schedule(&adapter->rx_queue[0].napi);
  1419. return IRQ_HANDLED;
  1420. }
  1421. #ifdef CONFIG_NET_POLL_CONTROLLER
  1422. /* netpoll callback. */
  1423. static void
  1424. vmxnet3_netpoll(struct net_device *netdev)
  1425. {
  1426. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1427. switch (adapter->intr.type) {
  1428. #ifdef CONFIG_PCI_MSI
  1429. case VMXNET3_IT_MSIX: {
  1430. int i;
  1431. for (i = 0; i < adapter->num_rx_queues; i++)
  1432. vmxnet3_msix_rx(0, &adapter->rx_queue[i]);
  1433. break;
  1434. }
  1435. #endif
  1436. case VMXNET3_IT_MSI:
  1437. default:
  1438. vmxnet3_intr(0, adapter->netdev);
  1439. break;
  1440. }
  1441. }
  1442. #endif /* CONFIG_NET_POLL_CONTROLLER */
  1443. static int
  1444. vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
  1445. {
  1446. struct vmxnet3_intr *intr = &adapter->intr;
  1447. int err = 0, i;
  1448. int vector = 0;
  1449. #ifdef CONFIG_PCI_MSI
  1450. if (adapter->intr.type == VMXNET3_IT_MSIX) {
  1451. for (i = 0; i < adapter->num_tx_queues; i++) {
  1452. if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
  1453. sprintf(adapter->tx_queue[i].name, "%s-tx-%d",
  1454. adapter->netdev->name, vector);
  1455. err = request_irq(
  1456. intr->msix_entries[vector].vector,
  1457. vmxnet3_msix_tx, 0,
  1458. adapter->tx_queue[i].name,
  1459. &adapter->tx_queue[i]);
  1460. } else {
  1461. sprintf(adapter->tx_queue[i].name, "%s-rxtx-%d",
  1462. adapter->netdev->name, vector);
  1463. }
  1464. if (err) {
  1465. dev_err(&adapter->netdev->dev,
  1466. "Failed to request irq for MSIX, %s, "
  1467. "error %d\n",
  1468. adapter->tx_queue[i].name, err);
  1469. return err;
  1470. }
  1471. /* Handle the case where only 1 MSIx was allocated for
  1472. * all tx queues */
  1473. if (adapter->share_intr == VMXNET3_INTR_TXSHARE) {
  1474. for (; i < adapter->num_tx_queues; i++)
  1475. adapter->tx_queue[i].comp_ring.intr_idx
  1476. = vector;
  1477. vector++;
  1478. break;
  1479. } else {
  1480. adapter->tx_queue[i].comp_ring.intr_idx
  1481. = vector++;
  1482. }
  1483. }
  1484. if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE)
  1485. vector = 0;
  1486. for (i = 0; i < adapter->num_rx_queues; i++) {
  1487. if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE)
  1488. sprintf(adapter->rx_queue[i].name, "%s-rx-%d",
  1489. adapter->netdev->name, vector);
  1490. else
  1491. sprintf(adapter->rx_queue[i].name, "%s-rxtx-%d",
  1492. adapter->netdev->name, vector);
  1493. err = request_irq(intr->msix_entries[vector].vector,
  1494. vmxnet3_msix_rx, 0,
  1495. adapter->rx_queue[i].name,
  1496. &(adapter->rx_queue[i]));
  1497. if (err) {
  1498. printk(KERN_ERR "Failed to request irq for MSIX"
  1499. ", %s, error %d\n",
  1500. adapter->rx_queue[i].name, err);
  1501. return err;
  1502. }
  1503. adapter->rx_queue[i].comp_ring.intr_idx = vector++;
  1504. }
  1505. sprintf(intr->event_msi_vector_name, "%s-event-%d",
  1506. adapter->netdev->name, vector);
  1507. err = request_irq(intr->msix_entries[vector].vector,
  1508. vmxnet3_msix_event, 0,
  1509. intr->event_msi_vector_name, adapter->netdev);
  1510. intr->event_intr_idx = vector;
  1511. } else if (intr->type == VMXNET3_IT_MSI) {
  1512. adapter->num_rx_queues = 1;
  1513. err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
  1514. adapter->netdev->name, adapter->netdev);
  1515. } else {
  1516. #endif
  1517. adapter->num_rx_queues = 1;
  1518. err = request_irq(adapter->pdev->irq, vmxnet3_intr,
  1519. IRQF_SHARED, adapter->netdev->name,
  1520. adapter->netdev);
  1521. #ifdef CONFIG_PCI_MSI
  1522. }
  1523. #endif
  1524. intr->num_intrs = vector + 1;
  1525. if (err) {
  1526. printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
  1527. ":%d\n", adapter->netdev->name, intr->type, err);
  1528. } else {
  1529. /* Number of rx queues will not change after this */
  1530. for (i = 0; i < adapter->num_rx_queues; i++) {
  1531. struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
  1532. rq->qid = i;
  1533. rq->qid2 = i + adapter->num_rx_queues;
  1534. }
  1535. /* init our intr settings */
  1536. for (i = 0; i < intr->num_intrs; i++)
  1537. intr->mod_levels[i] = UPT1_IML_ADAPTIVE;
  1538. if (adapter->intr.type != VMXNET3_IT_MSIX) {
  1539. adapter->intr.event_intr_idx = 0;
  1540. for (i = 0; i < adapter->num_tx_queues; i++)
  1541. adapter->tx_queue[i].comp_ring.intr_idx = 0;
  1542. adapter->rx_queue[0].comp_ring.intr_idx = 0;
  1543. }
  1544. printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
  1545. "allocated\n", adapter->netdev->name, intr->type,
  1546. intr->mask_mode, intr->num_intrs);
  1547. }
  1548. return err;
  1549. }
  1550. static void
  1551. vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
  1552. {
  1553. struct vmxnet3_intr *intr = &adapter->intr;
  1554. BUG_ON(intr->type == VMXNET3_IT_AUTO || intr->num_intrs <= 0);
  1555. switch (intr->type) {
  1556. #ifdef CONFIG_PCI_MSI
  1557. case VMXNET3_IT_MSIX:
  1558. {
  1559. int i, vector = 0;
  1560. if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE) {
  1561. for (i = 0; i < adapter->num_tx_queues; i++) {
  1562. free_irq(intr->msix_entries[vector++].vector,
  1563. &(adapter->tx_queue[i]));
  1564. if (adapter->share_intr == VMXNET3_INTR_TXSHARE)
  1565. break;
  1566. }
  1567. }
  1568. for (i = 0; i < adapter->num_rx_queues; i++) {
  1569. free_irq(intr->msix_entries[vector++].vector,
  1570. &(adapter->rx_queue[i]));
  1571. }
  1572. free_irq(intr->msix_entries[vector].vector,
  1573. adapter->netdev);
  1574. BUG_ON(vector >= intr->num_intrs);
  1575. break;
  1576. }
  1577. #endif
  1578. case VMXNET3_IT_MSI:
  1579. free_irq(adapter->pdev->irq, adapter->netdev);
  1580. break;
  1581. case VMXNET3_IT_INTX:
  1582. free_irq(adapter->pdev->irq, adapter->netdev);
  1583. break;
  1584. default:
  1585. BUG_ON(true);
  1586. }
  1587. }
  1588. static void
  1589. vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
  1590. {
  1591. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1592. u16 vid;
  1593. /* allow untagged pkts */
  1594. VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
  1595. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  1596. VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
  1597. }
  1598. static int
  1599. vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  1600. {
  1601. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1602. if (!(netdev->flags & IFF_PROMISC)) {
  1603. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1604. unsigned long flags;
  1605. VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
  1606. spin_lock_irqsave(&adapter->cmd_lock, flags);
  1607. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1608. VMXNET3_CMD_UPDATE_VLAN_FILTERS);
  1609. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  1610. }
  1611. set_bit(vid, adapter->active_vlans);
  1612. return 0;
  1613. }
  1614. static int
  1615. vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  1616. {
  1617. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1618. if (!(netdev->flags & IFF_PROMISC)) {
  1619. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1620. unsigned long flags;
  1621. VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
  1622. spin_lock_irqsave(&adapter->cmd_lock, flags);
  1623. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1624. VMXNET3_CMD_UPDATE_VLAN_FILTERS);
  1625. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  1626. }
  1627. clear_bit(vid, adapter->active_vlans);
  1628. return 0;
  1629. }
  1630. static u8 *
  1631. vmxnet3_copy_mc(struct net_device *netdev)
  1632. {
  1633. u8 *buf = NULL;
  1634. u32 sz = netdev_mc_count(netdev) * ETH_ALEN;
  1635. /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
  1636. if (sz <= 0xffff) {
  1637. /* We may be called with BH disabled */
  1638. buf = kmalloc(sz, GFP_ATOMIC);
  1639. if (buf) {
  1640. struct netdev_hw_addr *ha;
  1641. int i = 0;
  1642. netdev_for_each_mc_addr(ha, netdev)
  1643. memcpy(buf + i++ * ETH_ALEN, ha->addr,
  1644. ETH_ALEN);
  1645. }
  1646. }
  1647. return buf;
  1648. }
  1649. static void
  1650. vmxnet3_set_mc(struct net_device *netdev)
  1651. {
  1652. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1653. unsigned long flags;
  1654. struct Vmxnet3_RxFilterConf *rxConf =
  1655. &adapter->shared->devRead.rxFilterConf;
  1656. u8 *new_table = NULL;
  1657. u32 new_mode = VMXNET3_RXM_UCAST;
  1658. if (netdev->flags & IFF_PROMISC) {
  1659. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1660. memset(vfTable, 0, VMXNET3_VFT_SIZE * sizeof(*vfTable));
  1661. new_mode |= VMXNET3_RXM_PROMISC;
  1662. } else {
  1663. vmxnet3_restore_vlan(adapter);
  1664. }
  1665. if (netdev->flags & IFF_BROADCAST)
  1666. new_mode |= VMXNET3_RXM_BCAST;
  1667. if (netdev->flags & IFF_ALLMULTI)
  1668. new_mode |= VMXNET3_RXM_ALL_MULTI;
  1669. else
  1670. if (!netdev_mc_empty(netdev)) {
  1671. new_table = vmxnet3_copy_mc(netdev);
  1672. if (new_table) {
  1673. new_mode |= VMXNET3_RXM_MCAST;
  1674. rxConf->mfTableLen = cpu_to_le16(
  1675. netdev_mc_count(netdev) * ETH_ALEN);
  1676. rxConf->mfTablePA = cpu_to_le64(virt_to_phys(
  1677. new_table));
  1678. } else {
  1679. printk(KERN_INFO "%s: failed to copy mcast list"
  1680. ", setting ALL_MULTI\n", netdev->name);
  1681. new_mode |= VMXNET3_RXM_ALL_MULTI;
  1682. }
  1683. }
  1684. if (!(new_mode & VMXNET3_RXM_MCAST)) {
  1685. rxConf->mfTableLen = 0;
  1686. rxConf->mfTablePA = 0;
  1687. }
  1688. spin_lock_irqsave(&adapter->cmd_lock, flags);
  1689. if (new_mode != rxConf->rxMode) {
  1690. rxConf->rxMode = cpu_to_le32(new_mode);
  1691. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1692. VMXNET3_CMD_UPDATE_RX_MODE);
  1693. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1694. VMXNET3_CMD_UPDATE_VLAN_FILTERS);
  1695. }
  1696. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1697. VMXNET3_CMD_UPDATE_MAC_FILTERS);
  1698. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  1699. kfree(new_table);
  1700. }
  1701. void
  1702. vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter)
  1703. {
  1704. int i;
  1705. for (i = 0; i < adapter->num_rx_queues; i++)
  1706. vmxnet3_rq_destroy(&adapter->rx_queue[i], adapter);
  1707. }
  1708. /*
  1709. * Set up driver_shared based on settings in adapter.
  1710. */
  1711. static void
  1712. vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
  1713. {
  1714. struct Vmxnet3_DriverShared *shared = adapter->shared;
  1715. struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
  1716. struct Vmxnet3_TxQueueConf *tqc;
  1717. struct Vmxnet3_RxQueueConf *rqc;
  1718. int i;
  1719. memset(shared, 0, sizeof(*shared));
  1720. /* driver settings */
  1721. shared->magic = cpu_to_le32(VMXNET3_REV1_MAGIC);
  1722. devRead->misc.driverInfo.version = cpu_to_le32(
  1723. VMXNET3_DRIVER_VERSION_NUM);
  1724. devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
  1725. VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
  1726. devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
  1727. *((u32 *)&devRead->misc.driverInfo.gos) = cpu_to_le32(
  1728. *((u32 *)&devRead->misc.driverInfo.gos));
  1729. devRead->misc.driverInfo.vmxnet3RevSpt = cpu_to_le32(1);
  1730. devRead->misc.driverInfo.uptVerSpt = cpu_to_le32(1);
  1731. devRead->misc.ddPA = cpu_to_le64(virt_to_phys(adapter));
  1732. devRead->misc.ddLen = cpu_to_le32(sizeof(struct vmxnet3_adapter));
  1733. /* set up feature flags */
  1734. if (adapter->netdev->features & NETIF_F_RXCSUM)
  1735. devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
  1736. if (adapter->netdev->features & NETIF_F_LRO) {
  1737. devRead->misc.uptFeatures |= UPT1_F_LRO;
  1738. devRead->misc.maxNumRxSG = cpu_to_le16(1 + MAX_SKB_FRAGS);
  1739. }
  1740. if (adapter->netdev->features & NETIF_F_HW_VLAN_RX)
  1741. devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
  1742. devRead->misc.mtu = cpu_to_le32(adapter->netdev->mtu);
  1743. devRead->misc.queueDescPA = cpu_to_le64(adapter->queue_desc_pa);
  1744. devRead->misc.queueDescLen = cpu_to_le32(
  1745. adapter->num_tx_queues * sizeof(struct Vmxnet3_TxQueueDesc) +
  1746. adapter->num_rx_queues * sizeof(struct Vmxnet3_RxQueueDesc));
  1747. /* tx queue settings */
  1748. devRead->misc.numTxQueues = adapter->num_tx_queues;
  1749. for (i = 0; i < adapter->num_tx_queues; i++) {
  1750. struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
  1751. BUG_ON(adapter->tx_queue[i].tx_ring.base == NULL);
  1752. tqc = &adapter->tqd_start[i].conf;
  1753. tqc->txRingBasePA = cpu_to_le64(tq->tx_ring.basePA);
  1754. tqc->dataRingBasePA = cpu_to_le64(tq->data_ring.basePA);
  1755. tqc->compRingBasePA = cpu_to_le64(tq->comp_ring.basePA);
  1756. tqc->ddPA = cpu_to_le64(virt_to_phys(tq->buf_info));
  1757. tqc->txRingSize = cpu_to_le32(tq->tx_ring.size);
  1758. tqc->dataRingSize = cpu_to_le32(tq->data_ring.size);
  1759. tqc->compRingSize = cpu_to_le32(tq->comp_ring.size);
  1760. tqc->ddLen = cpu_to_le32(
  1761. sizeof(struct vmxnet3_tx_buf_info) *
  1762. tqc->txRingSize);
  1763. tqc->intrIdx = tq->comp_ring.intr_idx;
  1764. }
  1765. /* rx queue settings */
  1766. devRead->misc.numRxQueues = adapter->num_rx_queues;
  1767. for (i = 0; i < adapter->num_rx_queues; i++) {
  1768. struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
  1769. rqc = &adapter->rqd_start[i].conf;
  1770. rqc->rxRingBasePA[0] = cpu_to_le64(rq->rx_ring[0].basePA);
  1771. rqc->rxRingBasePA[1] = cpu_to_le64(rq->rx_ring[1].basePA);
  1772. rqc->compRingBasePA = cpu_to_le64(rq->comp_ring.basePA);
  1773. rqc->ddPA = cpu_to_le64(virt_to_phys(
  1774. rq->buf_info));
  1775. rqc->rxRingSize[0] = cpu_to_le32(rq->rx_ring[0].size);
  1776. rqc->rxRingSize[1] = cpu_to_le32(rq->rx_ring[1].size);
  1777. rqc->compRingSize = cpu_to_le32(rq->comp_ring.size);
  1778. rqc->ddLen = cpu_to_le32(
  1779. sizeof(struct vmxnet3_rx_buf_info) *
  1780. (rqc->rxRingSize[0] +
  1781. rqc->rxRingSize[1]));
  1782. rqc->intrIdx = rq->comp_ring.intr_idx;
  1783. }
  1784. #ifdef VMXNET3_RSS
  1785. memset(adapter->rss_conf, 0, sizeof(*adapter->rss_conf));
  1786. if (adapter->rss) {
  1787. struct UPT1_RSSConf *rssConf = adapter->rss_conf;
  1788. devRead->misc.uptFeatures |= UPT1_F_RSS;
  1789. devRead->misc.numRxQueues = adapter->num_rx_queues;
  1790. rssConf->hashType = UPT1_RSS_HASH_TYPE_TCP_IPV4 |
  1791. UPT1_RSS_HASH_TYPE_IPV4 |
  1792. UPT1_RSS_HASH_TYPE_TCP_IPV6 |
  1793. UPT1_RSS_HASH_TYPE_IPV6;
  1794. rssConf->hashFunc = UPT1_RSS_HASH_FUNC_TOEPLITZ;
  1795. rssConf->hashKeySize = UPT1_RSS_MAX_KEY_SIZE;
  1796. rssConf->indTableSize = VMXNET3_RSS_IND_TABLE_SIZE;
  1797. get_random_bytes(&rssConf->hashKey[0], rssConf->hashKeySize);
  1798. for (i = 0; i < rssConf->indTableSize; i++)
  1799. rssConf->indTable[i] = ethtool_rxfh_indir_default(
  1800. i, adapter->num_rx_queues);
  1801. devRead->rssConfDesc.confVer = 1;
  1802. devRead->rssConfDesc.confLen = sizeof(*rssConf);
  1803. devRead->rssConfDesc.confPA = virt_to_phys(rssConf);
  1804. }
  1805. #endif /* VMXNET3_RSS */
  1806. /* intr settings */
  1807. devRead->intrConf.autoMask = adapter->intr.mask_mode ==
  1808. VMXNET3_IMM_AUTO;
  1809. devRead->intrConf.numIntrs = adapter->intr.num_intrs;
  1810. for (i = 0; i < adapter->intr.num_intrs; i++)
  1811. devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
  1812. devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
  1813. devRead->intrConf.intrCtrl |= cpu_to_le32(VMXNET3_IC_DISABLE_ALL);
  1814. /* rx filter settings */
  1815. devRead->rxFilterConf.rxMode = 0;
  1816. vmxnet3_restore_vlan(adapter);
  1817. vmxnet3_write_mac_addr(adapter, adapter->netdev->dev_addr);
  1818. /* the rest are already zeroed */
  1819. }
  1820. int
  1821. vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
  1822. {
  1823. int err, i;
  1824. u32 ret;
  1825. unsigned long flags;
  1826. dev_dbg(&adapter->netdev->dev, "%s: skb_buf_size %d, rx_buf_per_pkt %d,"
  1827. " ring sizes %u %u %u\n", adapter->netdev->name,
  1828. adapter->skb_buf_size, adapter->rx_buf_per_pkt,
  1829. adapter->tx_queue[0].tx_ring.size,
  1830. adapter->rx_queue[0].rx_ring[0].size,
  1831. adapter->rx_queue[0].rx_ring[1].size);
  1832. vmxnet3_tq_init_all(adapter);
  1833. err = vmxnet3_rq_init_all(adapter);
  1834. if (err) {
  1835. printk(KERN_ERR "Failed to init rx queue for %s: error %d\n",
  1836. adapter->netdev->name, err);
  1837. goto rq_err;
  1838. }
  1839. err = vmxnet3_request_irqs(adapter);
  1840. if (err) {
  1841. printk(KERN_ERR "Failed to setup irq for %s: error %d\n",
  1842. adapter->netdev->name, err);
  1843. goto irq_err;
  1844. }
  1845. vmxnet3_setup_driver_shared(adapter);
  1846. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, VMXNET3_GET_ADDR_LO(
  1847. adapter->shared_pa));
  1848. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, VMXNET3_GET_ADDR_HI(
  1849. adapter->shared_pa));
  1850. spin_lock_irqsave(&adapter->cmd_lock, flags);
  1851. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1852. VMXNET3_CMD_ACTIVATE_DEV);
  1853. ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
  1854. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  1855. if (ret != 0) {
  1856. printk(KERN_ERR "Failed to activate dev %s: error %u\n",
  1857. adapter->netdev->name, ret);
  1858. err = -EINVAL;
  1859. goto activate_err;
  1860. }
  1861. for (i = 0; i < adapter->num_rx_queues; i++) {
  1862. VMXNET3_WRITE_BAR0_REG(adapter,
  1863. VMXNET3_REG_RXPROD + i * VMXNET3_REG_ALIGN,
  1864. adapter->rx_queue[i].rx_ring[0].next2fill);
  1865. VMXNET3_WRITE_BAR0_REG(adapter, (VMXNET3_REG_RXPROD2 +
  1866. (i * VMXNET3_REG_ALIGN)),
  1867. adapter->rx_queue[i].rx_ring[1].next2fill);
  1868. }
  1869. /* Apply the rx filter settins last. */
  1870. vmxnet3_set_mc(adapter->netdev);
  1871. /*
  1872. * Check link state when first activating device. It will start the
  1873. * tx queue if the link is up.
  1874. */
  1875. vmxnet3_check_link(adapter, true);
  1876. for (i = 0; i < adapter->num_rx_queues; i++)
  1877. napi_enable(&adapter->rx_queue[i].napi);
  1878. vmxnet3_enable_all_intrs(adapter);
  1879. clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
  1880. return 0;
  1881. activate_err:
  1882. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
  1883. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
  1884. vmxnet3_free_irqs(adapter);
  1885. irq_err:
  1886. rq_err:
  1887. /* free up buffers we allocated */
  1888. vmxnet3_rq_cleanup_all(adapter);
  1889. return err;
  1890. }
  1891. void
  1892. vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
  1893. {
  1894. unsigned long flags;
  1895. spin_lock_irqsave(&adapter->cmd_lock, flags);
  1896. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
  1897. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  1898. }
  1899. int
  1900. vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
  1901. {
  1902. int i;
  1903. unsigned long flags;
  1904. if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
  1905. return 0;
  1906. spin_lock_irqsave(&adapter->cmd_lock, flags);
  1907. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1908. VMXNET3_CMD_QUIESCE_DEV);
  1909. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  1910. vmxnet3_disable_all_intrs(adapter);
  1911. for (i = 0; i < adapter->num_rx_queues; i++)
  1912. napi_disable(&adapter->rx_queue[i].napi);
  1913. netif_tx_disable(adapter->netdev);
  1914. adapter->link_speed = 0;
  1915. netif_carrier_off(adapter->netdev);
  1916. vmxnet3_tq_cleanup_all(adapter);
  1917. vmxnet3_rq_cleanup_all(adapter);
  1918. vmxnet3_free_irqs(adapter);
  1919. return 0;
  1920. }
  1921. static void
  1922. vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
  1923. {
  1924. u32 tmp;
  1925. tmp = *(u32 *)mac;
  1926. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
  1927. tmp = (mac[5] << 8) | mac[4];
  1928. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
  1929. }
  1930. static int
  1931. vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
  1932. {
  1933. struct sockaddr *addr = p;
  1934. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1935. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1936. vmxnet3_write_mac_addr(adapter, addr->sa_data);
  1937. return 0;
  1938. }
  1939. /* ==================== initialization and cleanup routines ============ */
  1940. static int
  1941. vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
  1942. {
  1943. int err;
  1944. unsigned long mmio_start, mmio_len;
  1945. struct pci_dev *pdev = adapter->pdev;
  1946. err = pci_enable_device(pdev);
  1947. if (err) {
  1948. printk(KERN_ERR "Failed to enable adapter %s: error %d\n",
  1949. pci_name(pdev), err);
  1950. return err;
  1951. }
  1952. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
  1953. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  1954. printk(KERN_ERR "pci_set_consistent_dma_mask failed "
  1955. "for adapter %s\n", pci_name(pdev));
  1956. err = -EIO;
  1957. goto err_set_mask;
  1958. }
  1959. *dma64 = true;
  1960. } else {
  1961. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
  1962. printk(KERN_ERR "pci_set_dma_mask failed for adapter "
  1963. "%s\n", pci_name(pdev));
  1964. err = -EIO;
  1965. goto err_set_mask;
  1966. }
  1967. *dma64 = false;
  1968. }
  1969. err = pci_request_selected_regions(pdev, (1 << 2) - 1,
  1970. vmxnet3_driver_name);
  1971. if (err) {
  1972. printk(KERN_ERR "Failed to request region for adapter %s: "
  1973. "error %d\n", pci_name(pdev), err);
  1974. goto err_set_mask;
  1975. }
  1976. pci_set_master(pdev);
  1977. mmio_start = pci_resource_start(pdev, 0);
  1978. mmio_len = pci_resource_len(pdev, 0);
  1979. adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
  1980. if (!adapter->hw_addr0) {
  1981. printk(KERN_ERR "Failed to map bar0 for adapter %s\n",
  1982. pci_name(pdev));
  1983. err = -EIO;
  1984. goto err_ioremap;
  1985. }
  1986. mmio_start = pci_resource_start(pdev, 1);
  1987. mmio_len = pci_resource_len(pdev, 1);
  1988. adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
  1989. if (!adapter->hw_addr1) {
  1990. printk(KERN_ERR "Failed to map bar1 for adapter %s\n",
  1991. pci_name(pdev));
  1992. err = -EIO;
  1993. goto err_bar1;
  1994. }
  1995. return 0;
  1996. err_bar1:
  1997. iounmap(adapter->hw_addr0);
  1998. err_ioremap:
  1999. pci_release_selected_regions(pdev, (1 << 2) - 1);
  2000. err_set_mask:
  2001. pci_disable_device(pdev);
  2002. return err;
  2003. }
  2004. static void
  2005. vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
  2006. {
  2007. BUG_ON(!adapter->pdev);
  2008. iounmap(adapter->hw_addr0);
  2009. iounmap(adapter->hw_addr1);
  2010. pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
  2011. pci_disable_device(adapter->pdev);
  2012. }
  2013. static void
  2014. vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
  2015. {
  2016. size_t sz, i, ring0_size, ring1_size, comp_size;
  2017. struct vmxnet3_rx_queue *rq = &adapter->rx_queue[0];
  2018. if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
  2019. VMXNET3_MAX_ETH_HDR_SIZE) {
  2020. adapter->skb_buf_size = adapter->netdev->mtu +
  2021. VMXNET3_MAX_ETH_HDR_SIZE;
  2022. if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
  2023. adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
  2024. adapter->rx_buf_per_pkt = 1;
  2025. } else {
  2026. adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
  2027. sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
  2028. VMXNET3_MAX_ETH_HDR_SIZE;
  2029. adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
  2030. }
  2031. /*
  2032. * for simplicity, force the ring0 size to be a multiple of
  2033. * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
  2034. */
  2035. sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
  2036. ring0_size = adapter->rx_queue[0].rx_ring[0].size;
  2037. ring0_size = (ring0_size + sz - 1) / sz * sz;
  2038. ring0_size = min_t(u32, ring0_size, VMXNET3_RX_RING_MAX_SIZE /
  2039. sz * sz);
  2040. ring1_size = adapter->rx_queue[0].rx_ring[1].size;
  2041. comp_size = ring0_size + ring1_size;
  2042. for (i = 0; i < adapter->num_rx_queues; i++) {
  2043. rq = &adapter->rx_queue[i];
  2044. rq->rx_ring[0].size = ring0_size;
  2045. rq->rx_ring[1].size = ring1_size;
  2046. rq->comp_ring.size = comp_size;
  2047. }
  2048. }
  2049. int
  2050. vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
  2051. u32 rx_ring_size, u32 rx_ring2_size)
  2052. {
  2053. int err = 0, i;
  2054. for (i = 0; i < adapter->num_tx_queues; i++) {
  2055. struct vmxnet3_tx_queue *tq = &adapter->tx_queue[i];
  2056. tq->tx_ring.size = tx_ring_size;
  2057. tq->data_ring.size = tx_ring_size;
  2058. tq->comp_ring.size = tx_ring_size;
  2059. tq->shared = &adapter->tqd_start[i].ctrl;
  2060. tq->stopped = true;
  2061. tq->adapter = adapter;
  2062. tq->qid = i;
  2063. err = vmxnet3_tq_create(tq, adapter);
  2064. /*
  2065. * Too late to change num_tx_queues. We cannot do away with
  2066. * lesser number of queues than what we asked for
  2067. */
  2068. if (err)
  2069. goto queue_err;
  2070. }
  2071. adapter->rx_queue[0].rx_ring[0].size = rx_ring_size;
  2072. adapter->rx_queue[0].rx_ring[1].size = rx_ring2_size;
  2073. vmxnet3_adjust_rx_ring_size(adapter);
  2074. for (i = 0; i < adapter->num_rx_queues; i++) {
  2075. struct vmxnet3_rx_queue *rq = &adapter->rx_queue[i];
  2076. /* qid and qid2 for rx queues will be assigned later when num
  2077. * of rx queues is finalized after allocating intrs */
  2078. rq->shared = &adapter->rqd_start[i].ctrl;
  2079. rq->adapter = adapter;
  2080. err = vmxnet3_rq_create(rq, adapter);
  2081. if (err) {
  2082. if (i == 0) {
  2083. printk(KERN_ERR "Could not allocate any rx"
  2084. "queues. Aborting.\n");
  2085. goto queue_err;
  2086. } else {
  2087. printk(KERN_INFO "Number of rx queues changed "
  2088. "to : %d.\n", i);
  2089. adapter->num_rx_queues = i;
  2090. err = 0;
  2091. break;
  2092. }
  2093. }
  2094. }
  2095. return err;
  2096. queue_err:
  2097. vmxnet3_tq_destroy_all(adapter);
  2098. return err;
  2099. }
  2100. static int
  2101. vmxnet3_open(struct net_device *netdev)
  2102. {
  2103. struct vmxnet3_adapter *adapter;
  2104. int err, i;
  2105. adapter = netdev_priv(netdev);
  2106. for (i = 0; i < adapter->num_tx_queues; i++)
  2107. spin_lock_init(&adapter->tx_queue[i].tx_lock);
  2108. err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
  2109. VMXNET3_DEF_RX_RING_SIZE,
  2110. VMXNET3_DEF_RX_RING_SIZE);
  2111. if (err)
  2112. goto queue_err;
  2113. err = vmxnet3_activate_dev(adapter);
  2114. if (err)
  2115. goto activate_err;
  2116. return 0;
  2117. activate_err:
  2118. vmxnet3_rq_destroy_all(adapter);
  2119. vmxnet3_tq_destroy_all(adapter);
  2120. queue_err:
  2121. return err;
  2122. }
  2123. static int
  2124. vmxnet3_close(struct net_device *netdev)
  2125. {
  2126. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  2127. /*
  2128. * Reset_work may be in the middle of resetting the device, wait for its
  2129. * completion.
  2130. */
  2131. while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
  2132. msleep(1);
  2133. vmxnet3_quiesce_dev(adapter);
  2134. vmxnet3_rq_destroy_all(adapter);
  2135. vmxnet3_tq_destroy_all(adapter);
  2136. clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
  2137. return 0;
  2138. }
  2139. void
  2140. vmxnet3_force_close(struct vmxnet3_adapter *adapter)
  2141. {
  2142. int i;
  2143. /*
  2144. * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
  2145. * vmxnet3_close() will deadlock.
  2146. */
  2147. BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
  2148. /* we need to enable NAPI, otherwise dev_close will deadlock */
  2149. for (i = 0; i < adapter->num_rx_queues; i++)
  2150. napi_enable(&adapter->rx_queue[i].napi);
  2151. dev_close(adapter->netdev);
  2152. }
  2153. static int
  2154. vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
  2155. {
  2156. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  2157. int err = 0;
  2158. if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
  2159. return -EINVAL;
  2160. netdev->mtu = new_mtu;
  2161. /*
  2162. * Reset_work may be in the middle of resetting the device, wait for its
  2163. * completion.
  2164. */
  2165. while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
  2166. msleep(1);
  2167. if (netif_running(netdev)) {
  2168. vmxnet3_quiesce_dev(adapter);
  2169. vmxnet3_reset_dev(adapter);
  2170. /* we need to re-create the rx queue based on the new mtu */
  2171. vmxnet3_rq_destroy_all(adapter);
  2172. vmxnet3_adjust_rx_ring_size(adapter);
  2173. err = vmxnet3_rq_create_all(adapter);
  2174. if (err) {
  2175. printk(KERN_ERR "%s: failed to re-create rx queues,"
  2176. " error %d. Closing it.\n", netdev->name, err);
  2177. goto out;
  2178. }
  2179. err = vmxnet3_activate_dev(adapter);
  2180. if (err) {
  2181. printk(KERN_ERR "%s: failed to re-activate, error %d. "
  2182. "Closing it\n", netdev->name, err);
  2183. goto out;
  2184. }
  2185. }
  2186. out:
  2187. clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
  2188. if (err)
  2189. vmxnet3_force_close(adapter);
  2190. return err;
  2191. }
  2192. static void
  2193. vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
  2194. {
  2195. struct net_device *netdev = adapter->netdev;
  2196. netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM |
  2197. NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX |
  2198. NETIF_F_HW_VLAN_RX | NETIF_F_TSO | NETIF_F_TSO6 |
  2199. NETIF_F_LRO;
  2200. if (dma64)
  2201. netdev->hw_features |= NETIF_F_HIGHDMA;
  2202. netdev->vlan_features = netdev->hw_features &
  2203. ~(NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX);
  2204. netdev->features = netdev->hw_features | NETIF_F_HW_VLAN_FILTER;
  2205. netdev_info(adapter->netdev,
  2206. "features: sg csum vlan jf tso tsoIPv6 lro%s\n",
  2207. dma64 ? " highDMA" : "");
  2208. }
  2209. static void
  2210. vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
  2211. {
  2212. u32 tmp;
  2213. tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
  2214. *(u32 *)mac = tmp;
  2215. tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
  2216. mac[4] = tmp & 0xff;
  2217. mac[5] = (tmp >> 8) & 0xff;
  2218. }
  2219. #ifdef CONFIG_PCI_MSI
  2220. /*
  2221. * Enable MSIx vectors.
  2222. * Returns :
  2223. * 0 on successful enabling of required vectors,
  2224. * VMXNET3_LINUX_MIN_MSIX_VECT when only minimum number of vectors required
  2225. * could be enabled.
  2226. * number of vectors which can be enabled otherwise (this number is smaller
  2227. * than VMXNET3_LINUX_MIN_MSIX_VECT)
  2228. */
  2229. static int
  2230. vmxnet3_acquire_msix_vectors(struct vmxnet3_adapter *adapter,
  2231. int vectors)
  2232. {
  2233. int err = 0, vector_threshold;
  2234. vector_threshold = VMXNET3_LINUX_MIN_MSIX_VECT;
  2235. while (vectors >= vector_threshold) {
  2236. err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
  2237. vectors);
  2238. if (!err) {
  2239. adapter->intr.num_intrs = vectors;
  2240. return 0;
  2241. } else if (err < 0) {
  2242. netdev_err(adapter->netdev,
  2243. "Failed to enable MSI-X, error: %d\n", err);
  2244. vectors = 0;
  2245. } else if (err < vector_threshold) {
  2246. break;
  2247. } else {
  2248. /* If fails to enable required number of MSI-x vectors
  2249. * try enabling minimum number of vectors required.
  2250. */
  2251. netdev_err(adapter->netdev,
  2252. "Failed to enable %d MSI-X, trying %d instead\n",
  2253. vectors, vector_threshold);
  2254. vectors = vector_threshold;
  2255. }
  2256. }
  2257. netdev_info(adapter->netdev,
  2258. "Number of MSI-X interrupts which can be allocated are lower than min threshold required.\n");
  2259. return err;
  2260. }
  2261. #endif /* CONFIG_PCI_MSI */
  2262. static void
  2263. vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
  2264. {
  2265. u32 cfg;
  2266. unsigned long flags;
  2267. /* intr settings */
  2268. spin_lock_irqsave(&adapter->cmd_lock, flags);
  2269. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  2270. VMXNET3_CMD_GET_CONF_INTR);
  2271. cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
  2272. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  2273. adapter->intr.type = cfg & 0x3;
  2274. adapter->intr.mask_mode = (cfg >> 2) & 0x3;
  2275. if (adapter->intr.type == VMXNET3_IT_AUTO) {
  2276. adapter->intr.type = VMXNET3_IT_MSIX;
  2277. }
  2278. #ifdef CONFIG_PCI_MSI
  2279. if (adapter->intr.type == VMXNET3_IT_MSIX) {
  2280. int vector, err = 0;
  2281. adapter->intr.num_intrs = (adapter->share_intr ==
  2282. VMXNET3_INTR_TXSHARE) ? 1 :
  2283. adapter->num_tx_queues;
  2284. adapter->intr.num_intrs += (adapter->share_intr ==
  2285. VMXNET3_INTR_BUDDYSHARE) ? 0 :
  2286. adapter->num_rx_queues;
  2287. adapter->intr.num_intrs += 1; /* for link event */
  2288. adapter->intr.num_intrs = (adapter->intr.num_intrs >
  2289. VMXNET3_LINUX_MIN_MSIX_VECT
  2290. ? adapter->intr.num_intrs :
  2291. VMXNET3_LINUX_MIN_MSIX_VECT);
  2292. for (vector = 0; vector < adapter->intr.num_intrs; vector++)
  2293. adapter->intr.msix_entries[vector].entry = vector;
  2294. err = vmxnet3_acquire_msix_vectors(adapter,
  2295. adapter->intr.num_intrs);
  2296. /* If we cannot allocate one MSIx vector per queue
  2297. * then limit the number of rx queues to 1
  2298. */
  2299. if (err == VMXNET3_LINUX_MIN_MSIX_VECT) {
  2300. if (adapter->share_intr != VMXNET3_INTR_BUDDYSHARE
  2301. || adapter->num_rx_queues != 1) {
  2302. adapter->share_intr = VMXNET3_INTR_TXSHARE;
  2303. printk(KERN_ERR "Number of rx queues : 1\n");
  2304. adapter->num_rx_queues = 1;
  2305. adapter->intr.num_intrs =
  2306. VMXNET3_LINUX_MIN_MSIX_VECT;
  2307. }
  2308. return;
  2309. }
  2310. if (!err)
  2311. return;
  2312. /* If we cannot allocate MSIx vectors use only one rx queue */
  2313. netdev_info(adapter->netdev,
  2314. "Failed to enable MSI-X, error %d . Limiting #rx queues to 1, try MSI.\n",
  2315. err);
  2316. adapter->intr.type = VMXNET3_IT_MSI;
  2317. }
  2318. if (adapter->intr.type == VMXNET3_IT_MSI) {
  2319. int err;
  2320. err = pci_enable_msi(adapter->pdev);
  2321. if (!err) {
  2322. adapter->num_rx_queues = 1;
  2323. adapter->intr.num_intrs = 1;
  2324. return;
  2325. }
  2326. }
  2327. #endif /* CONFIG_PCI_MSI */
  2328. adapter->num_rx_queues = 1;
  2329. printk(KERN_INFO "Using INTx interrupt, #Rx queues: 1.\n");
  2330. adapter->intr.type = VMXNET3_IT_INTX;
  2331. /* INT-X related setting */
  2332. adapter->intr.num_intrs = 1;
  2333. }
  2334. static void
  2335. vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
  2336. {
  2337. if (adapter->intr.type == VMXNET3_IT_MSIX)
  2338. pci_disable_msix(adapter->pdev);
  2339. else if (adapter->intr.type == VMXNET3_IT_MSI)
  2340. pci_disable_msi(adapter->pdev);
  2341. else
  2342. BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
  2343. }
  2344. static void
  2345. vmxnet3_tx_timeout(struct net_device *netdev)
  2346. {
  2347. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  2348. adapter->tx_timeout_count++;
  2349. printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
  2350. schedule_work(&adapter->work);
  2351. netif_wake_queue(adapter->netdev);
  2352. }
  2353. static void
  2354. vmxnet3_reset_work(struct work_struct *data)
  2355. {
  2356. struct vmxnet3_adapter *adapter;
  2357. adapter = container_of(data, struct vmxnet3_adapter, work);
  2358. /* if another thread is resetting the device, no need to proceed */
  2359. if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
  2360. return;
  2361. /* if the device is closed, we must leave it alone */
  2362. rtnl_lock();
  2363. if (netif_running(adapter->netdev)) {
  2364. printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
  2365. vmxnet3_quiesce_dev(adapter);
  2366. vmxnet3_reset_dev(adapter);
  2367. vmxnet3_activate_dev(adapter);
  2368. } else {
  2369. printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
  2370. }
  2371. rtnl_unlock();
  2372. clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
  2373. }
  2374. static int __devinit
  2375. vmxnet3_probe_device(struct pci_dev *pdev,
  2376. const struct pci_device_id *id)
  2377. {
  2378. static const struct net_device_ops vmxnet3_netdev_ops = {
  2379. .ndo_open = vmxnet3_open,
  2380. .ndo_stop = vmxnet3_close,
  2381. .ndo_start_xmit = vmxnet3_xmit_frame,
  2382. .ndo_set_mac_address = vmxnet3_set_mac_addr,
  2383. .ndo_change_mtu = vmxnet3_change_mtu,
  2384. .ndo_set_features = vmxnet3_set_features,
  2385. .ndo_get_stats64 = vmxnet3_get_stats64,
  2386. .ndo_tx_timeout = vmxnet3_tx_timeout,
  2387. .ndo_set_rx_mode = vmxnet3_set_mc,
  2388. .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
  2389. .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
  2390. #ifdef CONFIG_NET_POLL_CONTROLLER
  2391. .ndo_poll_controller = vmxnet3_netpoll,
  2392. #endif
  2393. };
  2394. int err;
  2395. bool dma64 = false; /* stupid gcc */
  2396. u32 ver;
  2397. struct net_device *netdev;
  2398. struct vmxnet3_adapter *adapter;
  2399. u8 mac[ETH_ALEN];
  2400. int size;
  2401. int num_tx_queues;
  2402. int num_rx_queues;
  2403. if (!pci_msi_enabled())
  2404. enable_mq = 0;
  2405. #ifdef VMXNET3_RSS
  2406. if (enable_mq)
  2407. num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
  2408. (int)num_online_cpus());
  2409. else
  2410. #endif
  2411. num_rx_queues = 1;
  2412. num_rx_queues = rounddown_pow_of_two(num_rx_queues);
  2413. if (enable_mq)
  2414. num_tx_queues = min(VMXNET3_DEVICE_MAX_TX_QUEUES,
  2415. (int)num_online_cpus());
  2416. else
  2417. num_tx_queues = 1;
  2418. num_tx_queues = rounddown_pow_of_two(num_tx_queues);
  2419. netdev = alloc_etherdev_mq(sizeof(struct vmxnet3_adapter),
  2420. max(num_tx_queues, num_rx_queues));
  2421. printk(KERN_INFO "# of Tx queues : %d, # of Rx queues : %d\n",
  2422. num_tx_queues, num_rx_queues);
  2423. if (!netdev)
  2424. return -ENOMEM;
  2425. pci_set_drvdata(pdev, netdev);
  2426. adapter = netdev_priv(netdev);
  2427. adapter->netdev = netdev;
  2428. adapter->pdev = pdev;
  2429. spin_lock_init(&adapter->cmd_lock);
  2430. adapter->shared = pci_alloc_consistent(adapter->pdev,
  2431. sizeof(struct Vmxnet3_DriverShared),
  2432. &adapter->shared_pa);
  2433. if (!adapter->shared) {
  2434. printk(KERN_ERR "Failed to allocate memory for %s\n",
  2435. pci_name(pdev));
  2436. err = -ENOMEM;
  2437. goto err_alloc_shared;
  2438. }
  2439. adapter->num_rx_queues = num_rx_queues;
  2440. adapter->num_tx_queues = num_tx_queues;
  2441. size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
  2442. size += sizeof(struct Vmxnet3_RxQueueDesc) * adapter->num_rx_queues;
  2443. adapter->tqd_start = pci_alloc_consistent(adapter->pdev, size,
  2444. &adapter->queue_desc_pa);
  2445. if (!adapter->tqd_start) {
  2446. printk(KERN_ERR "Failed to allocate memory for %s\n",
  2447. pci_name(pdev));
  2448. err = -ENOMEM;
  2449. goto err_alloc_queue_desc;
  2450. }
  2451. adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start +
  2452. adapter->num_tx_queues);
  2453. adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
  2454. if (adapter->pm_conf == NULL) {
  2455. err = -ENOMEM;
  2456. goto err_alloc_pm;
  2457. }
  2458. #ifdef VMXNET3_RSS
  2459. adapter->rss_conf = kmalloc(sizeof(struct UPT1_RSSConf), GFP_KERNEL);
  2460. if (adapter->rss_conf == NULL) {
  2461. err = -ENOMEM;
  2462. goto err_alloc_rss;
  2463. }
  2464. #endif /* VMXNET3_RSS */
  2465. err = vmxnet3_alloc_pci_resources(adapter, &dma64);
  2466. if (err < 0)
  2467. goto err_alloc_pci;
  2468. ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
  2469. if (ver & 1) {
  2470. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
  2471. } else {
  2472. printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
  2473. " %s\n", ver, pci_name(pdev));
  2474. err = -EBUSY;
  2475. goto err_ver;
  2476. }
  2477. ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
  2478. if (ver & 1) {
  2479. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
  2480. } else {
  2481. printk(KERN_ERR "Incompatible upt version (0x%x) for "
  2482. "adapter %s\n", ver, pci_name(pdev));
  2483. err = -EBUSY;
  2484. goto err_ver;
  2485. }
  2486. SET_NETDEV_DEV(netdev, &pdev->dev);
  2487. vmxnet3_declare_features(adapter, dma64);
  2488. adapter->dev_number = atomic_read(&devices_found);
  2489. adapter->share_intr = irq_share_mode;
  2490. if (adapter->share_intr == VMXNET3_INTR_BUDDYSHARE &&
  2491. adapter->num_tx_queues != adapter->num_rx_queues)
  2492. adapter->share_intr = VMXNET3_INTR_DONTSHARE;
  2493. vmxnet3_alloc_intr_resources(adapter);
  2494. #ifdef VMXNET3_RSS
  2495. if (adapter->num_rx_queues > 1 &&
  2496. adapter->intr.type == VMXNET3_IT_MSIX) {
  2497. adapter->rss = true;
  2498. printk(KERN_INFO "RSS is enabled.\n");
  2499. } else {
  2500. adapter->rss = false;
  2501. }
  2502. #endif
  2503. vmxnet3_read_mac_addr(adapter, mac);
  2504. memcpy(netdev->dev_addr, mac, netdev->addr_len);
  2505. netdev->netdev_ops = &vmxnet3_netdev_ops;
  2506. vmxnet3_set_ethtool_ops(netdev);
  2507. netdev->watchdog_timeo = 5 * HZ;
  2508. INIT_WORK(&adapter->work, vmxnet3_reset_work);
  2509. if (adapter->intr.type == VMXNET3_IT_MSIX) {
  2510. int i;
  2511. for (i = 0; i < adapter->num_rx_queues; i++) {
  2512. netif_napi_add(adapter->netdev,
  2513. &adapter->rx_queue[i].napi,
  2514. vmxnet3_poll_rx_only, 64);
  2515. }
  2516. } else {
  2517. netif_napi_add(adapter->netdev, &adapter->rx_queue[0].napi,
  2518. vmxnet3_poll, 64);
  2519. }
  2520. netif_set_real_num_tx_queues(adapter->netdev, adapter->num_tx_queues);
  2521. netif_set_real_num_rx_queues(adapter->netdev, adapter->num_rx_queues);
  2522. err = register_netdev(netdev);
  2523. if (err) {
  2524. printk(KERN_ERR "Failed to register adapter %s\n",
  2525. pci_name(pdev));
  2526. goto err_register;
  2527. }
  2528. set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
  2529. vmxnet3_check_link(adapter, false);
  2530. atomic_inc(&devices_found);
  2531. return 0;
  2532. err_register:
  2533. vmxnet3_free_intr_resources(adapter);
  2534. err_ver:
  2535. vmxnet3_free_pci_resources(adapter);
  2536. err_alloc_pci:
  2537. #ifdef VMXNET3_RSS
  2538. kfree(adapter->rss_conf);
  2539. err_alloc_rss:
  2540. #endif
  2541. kfree(adapter->pm_conf);
  2542. err_alloc_pm:
  2543. pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
  2544. adapter->queue_desc_pa);
  2545. err_alloc_queue_desc:
  2546. pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
  2547. adapter->shared, adapter->shared_pa);
  2548. err_alloc_shared:
  2549. pci_set_drvdata(pdev, NULL);
  2550. free_netdev(netdev);
  2551. return err;
  2552. }
  2553. static void __devexit
  2554. vmxnet3_remove_device(struct pci_dev *pdev)
  2555. {
  2556. struct net_device *netdev = pci_get_drvdata(pdev);
  2557. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  2558. int size = 0;
  2559. int num_rx_queues;
  2560. #ifdef VMXNET3_RSS
  2561. if (enable_mq)
  2562. num_rx_queues = min(VMXNET3_DEVICE_MAX_RX_QUEUES,
  2563. (int)num_online_cpus());
  2564. else
  2565. #endif
  2566. num_rx_queues = 1;
  2567. num_rx_queues = rounddown_pow_of_two(num_rx_queues);
  2568. cancel_work_sync(&adapter->work);
  2569. unregister_netdev(netdev);
  2570. vmxnet3_free_intr_resources(adapter);
  2571. vmxnet3_free_pci_resources(adapter);
  2572. #ifdef VMXNET3_RSS
  2573. kfree(adapter->rss_conf);
  2574. #endif
  2575. kfree(adapter->pm_conf);
  2576. size = sizeof(struct Vmxnet3_TxQueueDesc) * adapter->num_tx_queues;
  2577. size += sizeof(struct Vmxnet3_RxQueueDesc) * num_rx_queues;
  2578. pci_free_consistent(adapter->pdev, size, adapter->tqd_start,
  2579. adapter->queue_desc_pa);
  2580. pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
  2581. adapter->shared, adapter->shared_pa);
  2582. free_netdev(netdev);
  2583. }
  2584. #ifdef CONFIG_PM
  2585. static int
  2586. vmxnet3_suspend(struct device *device)
  2587. {
  2588. struct pci_dev *pdev = to_pci_dev(device);
  2589. struct net_device *netdev = pci_get_drvdata(pdev);
  2590. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  2591. struct Vmxnet3_PMConf *pmConf;
  2592. struct ethhdr *ehdr;
  2593. struct arphdr *ahdr;
  2594. u8 *arpreq;
  2595. struct in_device *in_dev;
  2596. struct in_ifaddr *ifa;
  2597. unsigned long flags;
  2598. int i = 0;
  2599. if (!netif_running(netdev))
  2600. return 0;
  2601. for (i = 0; i < adapter->num_rx_queues; i++)
  2602. napi_disable(&adapter->rx_queue[i].napi);
  2603. vmxnet3_disable_all_intrs(adapter);
  2604. vmxnet3_free_irqs(adapter);
  2605. vmxnet3_free_intr_resources(adapter);
  2606. netif_device_detach(netdev);
  2607. netif_tx_stop_all_queues(netdev);
  2608. /* Create wake-up filters. */
  2609. pmConf = adapter->pm_conf;
  2610. memset(pmConf, 0, sizeof(*pmConf));
  2611. if (adapter->wol & WAKE_UCAST) {
  2612. pmConf->filters[i].patternSize = ETH_ALEN;
  2613. pmConf->filters[i].maskSize = 1;
  2614. memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
  2615. pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
  2616. pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
  2617. i++;
  2618. }
  2619. if (adapter->wol & WAKE_ARP) {
  2620. in_dev = in_dev_get(netdev);
  2621. if (!in_dev)
  2622. goto skip_arp;
  2623. ifa = (struct in_ifaddr *)in_dev->ifa_list;
  2624. if (!ifa)
  2625. goto skip_arp;
  2626. pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
  2627. sizeof(struct arphdr) + /* ARP header */
  2628. 2 * ETH_ALEN + /* 2 Ethernet addresses*/
  2629. 2 * sizeof(u32); /*2 IPv4 addresses */
  2630. pmConf->filters[i].maskSize =
  2631. (pmConf->filters[i].patternSize - 1) / 8 + 1;
  2632. /* ETH_P_ARP in Ethernet header. */
  2633. ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
  2634. ehdr->h_proto = htons(ETH_P_ARP);
  2635. /* ARPOP_REQUEST in ARP header. */
  2636. ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
  2637. ahdr->ar_op = htons(ARPOP_REQUEST);
  2638. arpreq = (u8 *)(ahdr + 1);
  2639. /* The Unicast IPv4 address in 'tip' field. */
  2640. arpreq += 2 * ETH_ALEN + sizeof(u32);
  2641. *(u32 *)arpreq = ifa->ifa_address;
  2642. /* The mask for the relevant bits. */
  2643. pmConf->filters[i].mask[0] = 0x00;
  2644. pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
  2645. pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
  2646. pmConf->filters[i].mask[3] = 0x00;
  2647. pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
  2648. pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
  2649. in_dev_put(in_dev);
  2650. pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
  2651. i++;
  2652. }
  2653. skip_arp:
  2654. if (adapter->wol & WAKE_MAGIC)
  2655. pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
  2656. pmConf->numFilters = i;
  2657. adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
  2658. adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
  2659. *pmConf));
  2660. adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
  2661. pmConf));
  2662. spin_lock_irqsave(&adapter->cmd_lock, flags);
  2663. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  2664. VMXNET3_CMD_UPDATE_PMCFG);
  2665. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  2666. pci_save_state(pdev);
  2667. pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
  2668. adapter->wol);
  2669. pci_disable_device(pdev);
  2670. pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
  2671. return 0;
  2672. }
  2673. static int
  2674. vmxnet3_resume(struct device *device)
  2675. {
  2676. int err, i = 0;
  2677. unsigned long flags;
  2678. struct pci_dev *pdev = to_pci_dev(device);
  2679. struct net_device *netdev = pci_get_drvdata(pdev);
  2680. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  2681. struct Vmxnet3_PMConf *pmConf;
  2682. if (!netif_running(netdev))
  2683. return 0;
  2684. /* Destroy wake-up filters. */
  2685. pmConf = adapter->pm_conf;
  2686. memset(pmConf, 0, sizeof(*pmConf));
  2687. adapter->shared->devRead.pmConfDesc.confVer = cpu_to_le32(1);
  2688. adapter->shared->devRead.pmConfDesc.confLen = cpu_to_le32(sizeof(
  2689. *pmConf));
  2690. adapter->shared->devRead.pmConfDesc.confPA = cpu_to_le64(virt_to_phys(
  2691. pmConf));
  2692. netif_device_attach(netdev);
  2693. pci_set_power_state(pdev, PCI_D0);
  2694. pci_restore_state(pdev);
  2695. err = pci_enable_device_mem(pdev);
  2696. if (err != 0)
  2697. return err;
  2698. pci_enable_wake(pdev, PCI_D0, 0);
  2699. spin_lock_irqsave(&adapter->cmd_lock, flags);
  2700. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  2701. VMXNET3_CMD_UPDATE_PMCFG);
  2702. spin_unlock_irqrestore(&adapter->cmd_lock, flags);
  2703. vmxnet3_alloc_intr_resources(adapter);
  2704. vmxnet3_request_irqs(adapter);
  2705. for (i = 0; i < adapter->num_rx_queues; i++)
  2706. napi_enable(&adapter->rx_queue[i].napi);
  2707. vmxnet3_enable_all_intrs(adapter);
  2708. return 0;
  2709. }
  2710. static const struct dev_pm_ops vmxnet3_pm_ops = {
  2711. .suspend = vmxnet3_suspend,
  2712. .resume = vmxnet3_resume,
  2713. };
  2714. #endif
  2715. static struct pci_driver vmxnet3_driver = {
  2716. .name = vmxnet3_driver_name,
  2717. .id_table = vmxnet3_pciid_table,
  2718. .probe = vmxnet3_probe_device,
  2719. .remove = __devexit_p(vmxnet3_remove_device),
  2720. #ifdef CONFIG_PM
  2721. .driver.pm = &vmxnet3_pm_ops,
  2722. #endif
  2723. };
  2724. static int __init
  2725. vmxnet3_init_module(void)
  2726. {
  2727. printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
  2728. VMXNET3_DRIVER_VERSION_REPORT);
  2729. return pci_register_driver(&vmxnet3_driver);
  2730. }
  2731. module_init(vmxnet3_init_module);
  2732. static void
  2733. vmxnet3_exit_module(void)
  2734. {
  2735. pci_unregister_driver(&vmxnet3_driver);
  2736. }
  2737. module_exit(vmxnet3_exit_module);
  2738. MODULE_AUTHOR("VMware, Inc.");
  2739. MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
  2740. MODULE_LICENSE("GPL v2");
  2741. MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);