vmxnet3_drv.c 93 KB

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