vmxnet3_drv.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556
  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 "vmxnet3_int.h"
  27. char vmxnet3_driver_name[] = "vmxnet3";
  28. #define VMXNET3_DRIVER_DESC "VMware vmxnet3 virtual NIC driver"
  29. /*
  30. * PCI Device ID Table
  31. * Last entry must be all 0s
  32. */
  33. static const struct pci_device_id vmxnet3_pciid_table[] = {
  34. {PCI_VDEVICE(VMWARE, PCI_DEVICE_ID_VMWARE_VMXNET3)},
  35. {0}
  36. };
  37. MODULE_DEVICE_TABLE(pci, vmxnet3_pciid_table);
  38. static atomic_t devices_found;
  39. /*
  40. * Enable/Disable the given intr
  41. */
  42. static void
  43. vmxnet3_enable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
  44. {
  45. VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 0);
  46. }
  47. static void
  48. vmxnet3_disable_intr(struct vmxnet3_adapter *adapter, unsigned intr_idx)
  49. {
  50. VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_IMR + intr_idx * 8, 1);
  51. }
  52. /*
  53. * Enable/Disable all intrs used by the device
  54. */
  55. static void
  56. vmxnet3_enable_all_intrs(struct vmxnet3_adapter *adapter)
  57. {
  58. int i;
  59. for (i = 0; i < adapter->intr.num_intrs; i++)
  60. vmxnet3_enable_intr(adapter, i);
  61. }
  62. static void
  63. vmxnet3_disable_all_intrs(struct vmxnet3_adapter *adapter)
  64. {
  65. int i;
  66. for (i = 0; i < adapter->intr.num_intrs; i++)
  67. vmxnet3_disable_intr(adapter, i);
  68. }
  69. static void
  70. vmxnet3_ack_events(struct vmxnet3_adapter *adapter, u32 events)
  71. {
  72. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_ECR, events);
  73. }
  74. static bool
  75. vmxnet3_tq_stopped(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  76. {
  77. return netif_queue_stopped(adapter->netdev);
  78. }
  79. static void
  80. vmxnet3_tq_start(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  81. {
  82. tq->stopped = false;
  83. netif_start_queue(adapter->netdev);
  84. }
  85. static void
  86. vmxnet3_tq_wake(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  87. {
  88. tq->stopped = false;
  89. netif_wake_queue(adapter->netdev);
  90. }
  91. static void
  92. vmxnet3_tq_stop(struct vmxnet3_tx_queue *tq, struct vmxnet3_adapter *adapter)
  93. {
  94. tq->stopped = true;
  95. tq->num_stop++;
  96. netif_stop_queue(adapter->netdev);
  97. }
  98. /*
  99. * Check the link state. This may start or stop the tx queue.
  100. */
  101. static void
  102. vmxnet3_check_link(struct vmxnet3_adapter *adapter)
  103. {
  104. u32 ret;
  105. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_GET_LINK);
  106. ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
  107. adapter->link_speed = ret >> 16;
  108. if (ret & 1) { /* Link is up. */
  109. printk(KERN_INFO "%s: NIC Link is Up %d Mbps\n",
  110. adapter->netdev->name, adapter->link_speed);
  111. if (!netif_carrier_ok(adapter->netdev))
  112. netif_carrier_on(adapter->netdev);
  113. vmxnet3_tq_start(&adapter->tx_queue, adapter);
  114. } else {
  115. printk(KERN_INFO "%s: NIC Link is Down\n",
  116. adapter->netdev->name);
  117. if (netif_carrier_ok(adapter->netdev))
  118. netif_carrier_off(adapter->netdev);
  119. vmxnet3_tq_stop(&adapter->tx_queue, adapter);
  120. }
  121. }
  122. static void
  123. vmxnet3_process_events(struct vmxnet3_adapter *adapter)
  124. {
  125. u32 events = adapter->shared->ecr;
  126. if (!events)
  127. return;
  128. vmxnet3_ack_events(adapter, events);
  129. /* Check if link state has changed */
  130. if (events & VMXNET3_ECR_LINK)
  131. vmxnet3_check_link(adapter);
  132. /* Check if there is an error on xmit/recv queues */
  133. if (events & (VMXNET3_ECR_TQERR | VMXNET3_ECR_RQERR)) {
  134. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  135. VMXNET3_CMD_GET_QUEUE_STATUS);
  136. if (adapter->tqd_start->status.stopped) {
  137. printk(KERN_ERR "%s: tq error 0x%x\n",
  138. adapter->netdev->name,
  139. adapter->tqd_start->status.error);
  140. }
  141. if (adapter->rqd_start->status.stopped) {
  142. printk(KERN_ERR "%s: rq error 0x%x\n",
  143. adapter->netdev->name,
  144. adapter->rqd_start->status.error);
  145. }
  146. schedule_work(&adapter->work);
  147. }
  148. }
  149. static void
  150. vmxnet3_unmap_tx_buf(struct vmxnet3_tx_buf_info *tbi,
  151. struct pci_dev *pdev)
  152. {
  153. if (tbi->map_type == VMXNET3_MAP_SINGLE)
  154. pci_unmap_single(pdev, tbi->dma_addr, tbi->len,
  155. PCI_DMA_TODEVICE);
  156. else if (tbi->map_type == VMXNET3_MAP_PAGE)
  157. pci_unmap_page(pdev, tbi->dma_addr, tbi->len,
  158. PCI_DMA_TODEVICE);
  159. else
  160. BUG_ON(tbi->map_type != VMXNET3_MAP_NONE);
  161. tbi->map_type = VMXNET3_MAP_NONE; /* to help debugging */
  162. }
  163. static int
  164. vmxnet3_unmap_pkt(u32 eop_idx, struct vmxnet3_tx_queue *tq,
  165. struct pci_dev *pdev, struct vmxnet3_adapter *adapter)
  166. {
  167. struct sk_buff *skb;
  168. int entries = 0;
  169. /* no out of order completion */
  170. BUG_ON(tq->buf_info[eop_idx].sop_idx != tq->tx_ring.next2comp);
  171. BUG_ON(tq->tx_ring.base[eop_idx].txd.eop != 1);
  172. skb = tq->buf_info[eop_idx].skb;
  173. BUG_ON(skb == NULL);
  174. tq->buf_info[eop_idx].skb = NULL;
  175. VMXNET3_INC_RING_IDX_ONLY(eop_idx, tq->tx_ring.size);
  176. while (tq->tx_ring.next2comp != eop_idx) {
  177. vmxnet3_unmap_tx_buf(tq->buf_info + tq->tx_ring.next2comp,
  178. pdev);
  179. /* update next2comp w/o tx_lock. Since we are marking more,
  180. * instead of less, tx ring entries avail, the worst case is
  181. * that the tx routine incorrectly re-queues a pkt due to
  182. * insufficient tx ring entries.
  183. */
  184. vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
  185. entries++;
  186. }
  187. dev_kfree_skb_any(skb);
  188. return entries;
  189. }
  190. static int
  191. vmxnet3_tq_tx_complete(struct vmxnet3_tx_queue *tq,
  192. struct vmxnet3_adapter *adapter)
  193. {
  194. int completed = 0;
  195. union Vmxnet3_GenericDesc *gdesc;
  196. gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
  197. while (gdesc->tcd.gen == tq->comp_ring.gen) {
  198. completed += vmxnet3_unmap_pkt(gdesc->tcd.txdIdx, tq,
  199. adapter->pdev, adapter);
  200. vmxnet3_comp_ring_adv_next2proc(&tq->comp_ring);
  201. gdesc = tq->comp_ring.base + tq->comp_ring.next2proc;
  202. }
  203. if (completed) {
  204. spin_lock(&tq->tx_lock);
  205. if (unlikely(vmxnet3_tq_stopped(tq, adapter) &&
  206. vmxnet3_cmd_ring_desc_avail(&tq->tx_ring) >
  207. VMXNET3_WAKE_QUEUE_THRESHOLD(tq) &&
  208. netif_carrier_ok(adapter->netdev))) {
  209. vmxnet3_tq_wake(tq, adapter);
  210. }
  211. spin_unlock(&tq->tx_lock);
  212. }
  213. return completed;
  214. }
  215. static void
  216. vmxnet3_tq_cleanup(struct vmxnet3_tx_queue *tq,
  217. struct vmxnet3_adapter *adapter)
  218. {
  219. int i;
  220. while (tq->tx_ring.next2comp != tq->tx_ring.next2fill) {
  221. struct vmxnet3_tx_buf_info *tbi;
  222. union Vmxnet3_GenericDesc *gdesc;
  223. tbi = tq->buf_info + tq->tx_ring.next2comp;
  224. gdesc = tq->tx_ring.base + tq->tx_ring.next2comp;
  225. vmxnet3_unmap_tx_buf(tbi, adapter->pdev);
  226. if (tbi->skb) {
  227. dev_kfree_skb_any(tbi->skb);
  228. tbi->skb = NULL;
  229. }
  230. vmxnet3_cmd_ring_adv_next2comp(&tq->tx_ring);
  231. }
  232. /* sanity check, verify all buffers are indeed unmapped and freed */
  233. for (i = 0; i < tq->tx_ring.size; i++) {
  234. BUG_ON(tq->buf_info[i].skb != NULL ||
  235. tq->buf_info[i].map_type != VMXNET3_MAP_NONE);
  236. }
  237. tq->tx_ring.gen = VMXNET3_INIT_GEN;
  238. tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
  239. tq->comp_ring.gen = VMXNET3_INIT_GEN;
  240. tq->comp_ring.next2proc = 0;
  241. }
  242. void
  243. vmxnet3_tq_destroy(struct vmxnet3_tx_queue *tq,
  244. struct vmxnet3_adapter *adapter)
  245. {
  246. if (tq->tx_ring.base) {
  247. pci_free_consistent(adapter->pdev, tq->tx_ring.size *
  248. sizeof(struct Vmxnet3_TxDesc),
  249. tq->tx_ring.base, tq->tx_ring.basePA);
  250. tq->tx_ring.base = NULL;
  251. }
  252. if (tq->data_ring.base) {
  253. pci_free_consistent(adapter->pdev, tq->data_ring.size *
  254. sizeof(struct Vmxnet3_TxDataDesc),
  255. tq->data_ring.base, tq->data_ring.basePA);
  256. tq->data_ring.base = NULL;
  257. }
  258. if (tq->comp_ring.base) {
  259. pci_free_consistent(adapter->pdev, tq->comp_ring.size *
  260. sizeof(struct Vmxnet3_TxCompDesc),
  261. tq->comp_ring.base, tq->comp_ring.basePA);
  262. tq->comp_ring.base = NULL;
  263. }
  264. kfree(tq->buf_info);
  265. tq->buf_info = NULL;
  266. }
  267. static void
  268. vmxnet3_tq_init(struct vmxnet3_tx_queue *tq,
  269. struct vmxnet3_adapter *adapter)
  270. {
  271. int i;
  272. /* reset the tx ring contents to 0 and reset the tx ring states */
  273. memset(tq->tx_ring.base, 0, tq->tx_ring.size *
  274. sizeof(struct Vmxnet3_TxDesc));
  275. tq->tx_ring.next2fill = tq->tx_ring.next2comp = 0;
  276. tq->tx_ring.gen = VMXNET3_INIT_GEN;
  277. memset(tq->data_ring.base, 0, tq->data_ring.size *
  278. sizeof(struct Vmxnet3_TxDataDesc));
  279. /* reset the tx comp ring contents to 0 and reset comp ring states */
  280. memset(tq->comp_ring.base, 0, tq->comp_ring.size *
  281. sizeof(struct Vmxnet3_TxCompDesc));
  282. tq->comp_ring.next2proc = 0;
  283. tq->comp_ring.gen = VMXNET3_INIT_GEN;
  284. /* reset the bookkeeping data */
  285. memset(tq->buf_info, 0, sizeof(tq->buf_info[0]) * tq->tx_ring.size);
  286. for (i = 0; i < tq->tx_ring.size; i++)
  287. tq->buf_info[i].map_type = VMXNET3_MAP_NONE;
  288. /* stats are not reset */
  289. }
  290. static int
  291. vmxnet3_tq_create(struct vmxnet3_tx_queue *tq,
  292. struct vmxnet3_adapter *adapter)
  293. {
  294. BUG_ON(tq->tx_ring.base || tq->data_ring.base ||
  295. tq->comp_ring.base || tq->buf_info);
  296. tq->tx_ring.base = pci_alloc_consistent(adapter->pdev, tq->tx_ring.size
  297. * sizeof(struct Vmxnet3_TxDesc),
  298. &tq->tx_ring.basePA);
  299. if (!tq->tx_ring.base) {
  300. printk(KERN_ERR "%s: failed to allocate tx ring\n",
  301. adapter->netdev->name);
  302. goto err;
  303. }
  304. tq->data_ring.base = pci_alloc_consistent(adapter->pdev,
  305. tq->data_ring.size *
  306. sizeof(struct Vmxnet3_TxDataDesc),
  307. &tq->data_ring.basePA);
  308. if (!tq->data_ring.base) {
  309. printk(KERN_ERR "%s: failed to allocate data ring\n",
  310. adapter->netdev->name);
  311. goto err;
  312. }
  313. tq->comp_ring.base = pci_alloc_consistent(adapter->pdev,
  314. tq->comp_ring.size *
  315. sizeof(struct Vmxnet3_TxCompDesc),
  316. &tq->comp_ring.basePA);
  317. if (!tq->comp_ring.base) {
  318. printk(KERN_ERR "%s: failed to allocate tx comp ring\n",
  319. adapter->netdev->name);
  320. goto err;
  321. }
  322. tq->buf_info = kcalloc(tq->tx_ring.size, sizeof(tq->buf_info[0]),
  323. GFP_KERNEL);
  324. if (!tq->buf_info) {
  325. printk(KERN_ERR "%s: failed to allocate tx bufinfo\n",
  326. adapter->netdev->name);
  327. goto err;
  328. }
  329. return 0;
  330. err:
  331. vmxnet3_tq_destroy(tq, adapter);
  332. return -ENOMEM;
  333. }
  334. /*
  335. * starting from ring->next2fill, allocate rx buffers for the given ring
  336. * of the rx queue and update the rx desc. stop after @num_to_alloc buffers
  337. * are allocated or allocation fails
  338. */
  339. static int
  340. vmxnet3_rq_alloc_rx_buf(struct vmxnet3_rx_queue *rq, u32 ring_idx,
  341. int num_to_alloc, struct vmxnet3_adapter *adapter)
  342. {
  343. int num_allocated = 0;
  344. struct vmxnet3_rx_buf_info *rbi_base = rq->buf_info[ring_idx];
  345. struct vmxnet3_cmd_ring *ring = &rq->rx_ring[ring_idx];
  346. u32 val;
  347. while (num_allocated < num_to_alloc) {
  348. struct vmxnet3_rx_buf_info *rbi;
  349. union Vmxnet3_GenericDesc *gd;
  350. rbi = rbi_base + ring->next2fill;
  351. gd = ring->base + ring->next2fill;
  352. if (rbi->buf_type == VMXNET3_RX_BUF_SKB) {
  353. if (rbi->skb == NULL) {
  354. rbi->skb = dev_alloc_skb(rbi->len +
  355. NET_IP_ALIGN);
  356. if (unlikely(rbi->skb == NULL)) {
  357. rq->stats.rx_buf_alloc_failure++;
  358. break;
  359. }
  360. rbi->skb->dev = adapter->netdev;
  361. skb_reserve(rbi->skb, NET_IP_ALIGN);
  362. rbi->dma_addr = pci_map_single(adapter->pdev,
  363. rbi->skb->data, rbi->len,
  364. PCI_DMA_FROMDEVICE);
  365. } else {
  366. /* rx buffer skipped by the device */
  367. }
  368. val = VMXNET3_RXD_BTYPE_HEAD << VMXNET3_RXD_BTYPE_SHIFT;
  369. } else {
  370. BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_PAGE ||
  371. rbi->len != PAGE_SIZE);
  372. if (rbi->page == NULL) {
  373. rbi->page = alloc_page(GFP_ATOMIC);
  374. if (unlikely(rbi->page == NULL)) {
  375. rq->stats.rx_buf_alloc_failure++;
  376. break;
  377. }
  378. rbi->dma_addr = pci_map_page(adapter->pdev,
  379. rbi->page, 0, PAGE_SIZE,
  380. PCI_DMA_FROMDEVICE);
  381. } else {
  382. /* rx buffers skipped by the device */
  383. }
  384. val = VMXNET3_RXD_BTYPE_BODY << VMXNET3_RXD_BTYPE_SHIFT;
  385. }
  386. BUG_ON(rbi->dma_addr == 0);
  387. gd->rxd.addr = rbi->dma_addr;
  388. gd->dword[2] = (ring->gen << VMXNET3_RXD_GEN_SHIFT) | val |
  389. rbi->len;
  390. num_allocated++;
  391. vmxnet3_cmd_ring_adv_next2fill(ring);
  392. }
  393. rq->uncommitted[ring_idx] += num_allocated;
  394. dprintk(KERN_ERR "alloc_rx_buf: %d allocated, next2fill %u, next2comp "
  395. "%u, uncommited %u\n", num_allocated, ring->next2fill,
  396. ring->next2comp, rq->uncommitted[ring_idx]);
  397. /* so that the device can distinguish a full ring and an empty ring */
  398. BUG_ON(num_allocated != 0 && ring->next2fill == ring->next2comp);
  399. return num_allocated;
  400. }
  401. static void
  402. vmxnet3_append_frag(struct sk_buff *skb, struct Vmxnet3_RxCompDesc *rcd,
  403. struct vmxnet3_rx_buf_info *rbi)
  404. {
  405. struct skb_frag_struct *frag = skb_shinfo(skb)->frags +
  406. skb_shinfo(skb)->nr_frags;
  407. BUG_ON(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS);
  408. frag->page = rbi->page;
  409. frag->page_offset = 0;
  410. frag->size = rcd->len;
  411. skb->data_len += frag->size;
  412. skb_shinfo(skb)->nr_frags++;
  413. }
  414. static void
  415. vmxnet3_map_pkt(struct sk_buff *skb, struct vmxnet3_tx_ctx *ctx,
  416. struct vmxnet3_tx_queue *tq, struct pci_dev *pdev,
  417. struct vmxnet3_adapter *adapter)
  418. {
  419. u32 dw2, len;
  420. unsigned long buf_offset;
  421. int i;
  422. union Vmxnet3_GenericDesc *gdesc;
  423. struct vmxnet3_tx_buf_info *tbi = NULL;
  424. BUG_ON(ctx->copy_size > skb_headlen(skb));
  425. /* use the previous gen bit for the SOP desc */
  426. dw2 = (tq->tx_ring.gen ^ 0x1) << VMXNET3_TXD_GEN_SHIFT;
  427. ctx->sop_txd = tq->tx_ring.base + tq->tx_ring.next2fill;
  428. gdesc = ctx->sop_txd; /* both loops below can be skipped */
  429. /* no need to map the buffer if headers are copied */
  430. if (ctx->copy_size) {
  431. ctx->sop_txd->txd.addr = tq->data_ring.basePA +
  432. tq->tx_ring.next2fill *
  433. sizeof(struct Vmxnet3_TxDataDesc);
  434. ctx->sop_txd->dword[2] = dw2 | ctx->copy_size;
  435. ctx->sop_txd->dword[3] = 0;
  436. tbi = tq->buf_info + tq->tx_ring.next2fill;
  437. tbi->map_type = VMXNET3_MAP_NONE;
  438. dprintk(KERN_ERR "txd[%u]: 0x%Lx 0x%x 0x%x\n",
  439. tq->tx_ring.next2fill, ctx->sop_txd->txd.addr,
  440. ctx->sop_txd->dword[2], ctx->sop_txd->dword[3]);
  441. vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
  442. /* use the right gen for non-SOP desc */
  443. dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
  444. }
  445. /* linear part can use multiple tx desc if it's big */
  446. len = skb_headlen(skb) - ctx->copy_size;
  447. buf_offset = ctx->copy_size;
  448. while (len) {
  449. u32 buf_size;
  450. buf_size = len > VMXNET3_MAX_TX_BUF_SIZE ?
  451. VMXNET3_MAX_TX_BUF_SIZE : len;
  452. tbi = tq->buf_info + tq->tx_ring.next2fill;
  453. tbi->map_type = VMXNET3_MAP_SINGLE;
  454. tbi->dma_addr = pci_map_single(adapter->pdev,
  455. skb->data + buf_offset, buf_size,
  456. PCI_DMA_TODEVICE);
  457. tbi->len = buf_size; /* this automatically convert 2^14 to 0 */
  458. gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
  459. BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
  460. gdesc->txd.addr = tbi->dma_addr;
  461. gdesc->dword[2] = dw2 | buf_size;
  462. gdesc->dword[3] = 0;
  463. dprintk(KERN_ERR "txd[%u]: 0x%Lx 0x%x 0x%x\n",
  464. tq->tx_ring.next2fill, gdesc->txd.addr,
  465. gdesc->dword[2], gdesc->dword[3]);
  466. vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
  467. dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
  468. len -= buf_size;
  469. buf_offset += buf_size;
  470. }
  471. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  472. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  473. tbi = tq->buf_info + tq->tx_ring.next2fill;
  474. tbi->map_type = VMXNET3_MAP_PAGE;
  475. tbi->dma_addr = pci_map_page(adapter->pdev, frag->page,
  476. frag->page_offset, frag->size,
  477. PCI_DMA_TODEVICE);
  478. tbi->len = frag->size;
  479. gdesc = tq->tx_ring.base + tq->tx_ring.next2fill;
  480. BUG_ON(gdesc->txd.gen == tq->tx_ring.gen);
  481. gdesc->txd.addr = tbi->dma_addr;
  482. gdesc->dword[2] = dw2 | frag->size;
  483. gdesc->dword[3] = 0;
  484. dprintk(KERN_ERR "txd[%u]: 0x%llu %u %u\n",
  485. tq->tx_ring.next2fill, gdesc->txd.addr,
  486. gdesc->dword[2], gdesc->dword[3]);
  487. vmxnet3_cmd_ring_adv_next2fill(&tq->tx_ring);
  488. dw2 = tq->tx_ring.gen << VMXNET3_TXD_GEN_SHIFT;
  489. }
  490. ctx->eop_txd = gdesc;
  491. /* set the last buf_info for the pkt */
  492. tbi->skb = skb;
  493. tbi->sop_idx = ctx->sop_txd - tq->tx_ring.base;
  494. }
  495. /*
  496. * parse and copy relevant protocol headers:
  497. * For a tso pkt, relevant headers are L2/3/4 including options
  498. * For a pkt requesting csum offloading, they are L2/3 and may include L4
  499. * if it's a TCP/UDP pkt
  500. *
  501. * Returns:
  502. * -1: error happens during parsing
  503. * 0: protocol headers parsed, but too big to be copied
  504. * 1: protocol headers parsed and copied
  505. *
  506. * Other effects:
  507. * 1. related *ctx fields are updated.
  508. * 2. ctx->copy_size is # of bytes copied
  509. * 3. the portion copied is guaranteed to be in the linear part
  510. *
  511. */
  512. static int
  513. vmxnet3_parse_and_copy_hdr(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
  514. struct vmxnet3_tx_ctx *ctx,
  515. struct vmxnet3_adapter *adapter)
  516. {
  517. struct Vmxnet3_TxDataDesc *tdd;
  518. if (ctx->mss) {
  519. ctx->eth_ip_hdr_size = skb_transport_offset(skb);
  520. ctx->l4_hdr_size = ((struct tcphdr *)
  521. skb_transport_header(skb))->doff * 4;
  522. ctx->copy_size = ctx->eth_ip_hdr_size + ctx->l4_hdr_size;
  523. } else {
  524. unsigned int pull_size;
  525. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  526. ctx->eth_ip_hdr_size = skb_transport_offset(skb);
  527. if (ctx->ipv4) {
  528. struct iphdr *iph = (struct iphdr *)
  529. skb_network_header(skb);
  530. if (iph->protocol == IPPROTO_TCP) {
  531. pull_size = ctx->eth_ip_hdr_size +
  532. sizeof(struct tcphdr);
  533. if (unlikely(!pskb_may_pull(skb,
  534. pull_size))) {
  535. goto err;
  536. }
  537. ctx->l4_hdr_size = ((struct tcphdr *)
  538. skb_transport_header(skb))->doff * 4;
  539. } else if (iph->protocol == IPPROTO_UDP) {
  540. ctx->l4_hdr_size =
  541. sizeof(struct udphdr);
  542. } else {
  543. ctx->l4_hdr_size = 0;
  544. }
  545. } else {
  546. /* for simplicity, don't copy L4 headers */
  547. ctx->l4_hdr_size = 0;
  548. }
  549. ctx->copy_size = ctx->eth_ip_hdr_size +
  550. ctx->l4_hdr_size;
  551. } else {
  552. ctx->eth_ip_hdr_size = 0;
  553. ctx->l4_hdr_size = 0;
  554. /* copy as much as allowed */
  555. ctx->copy_size = min((unsigned int)VMXNET3_HDR_COPY_SIZE
  556. , skb_headlen(skb));
  557. }
  558. /* make sure headers are accessible directly */
  559. if (unlikely(!pskb_may_pull(skb, ctx->copy_size)))
  560. goto err;
  561. }
  562. if (unlikely(ctx->copy_size > VMXNET3_HDR_COPY_SIZE)) {
  563. tq->stats.oversized_hdr++;
  564. ctx->copy_size = 0;
  565. return 0;
  566. }
  567. tdd = tq->data_ring.base + tq->tx_ring.next2fill;
  568. memcpy(tdd->data, skb->data, ctx->copy_size);
  569. dprintk(KERN_ERR "copy %u bytes to dataRing[%u]\n",
  570. ctx->copy_size, tq->tx_ring.next2fill);
  571. return 1;
  572. err:
  573. return -1;
  574. }
  575. static void
  576. vmxnet3_prepare_tso(struct sk_buff *skb,
  577. struct vmxnet3_tx_ctx *ctx)
  578. {
  579. struct tcphdr *tcph = (struct tcphdr *)skb_transport_header(skb);
  580. if (ctx->ipv4) {
  581. struct iphdr *iph = (struct iphdr *)skb_network_header(skb);
  582. iph->check = 0;
  583. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, 0,
  584. IPPROTO_TCP, 0);
  585. } else {
  586. struct ipv6hdr *iph = (struct ipv6hdr *)skb_network_header(skb);
  587. tcph->check = ~csum_ipv6_magic(&iph->saddr, &iph->daddr, 0,
  588. IPPROTO_TCP, 0);
  589. }
  590. }
  591. /*
  592. * Transmits a pkt thru a given tq
  593. * Returns:
  594. * NETDEV_TX_OK: descriptors are setup successfully
  595. * NETDEV_TX_OK: error occured, the pkt is dropped
  596. * NETDEV_TX_BUSY: tx ring is full, queue is stopped
  597. *
  598. * Side-effects:
  599. * 1. tx ring may be changed
  600. * 2. tq stats may be updated accordingly
  601. * 3. shared->txNumDeferred may be updated
  602. */
  603. static int
  604. vmxnet3_tq_xmit(struct sk_buff *skb, struct vmxnet3_tx_queue *tq,
  605. struct vmxnet3_adapter *adapter, struct net_device *netdev)
  606. {
  607. int ret;
  608. u32 count;
  609. unsigned long flags;
  610. struct vmxnet3_tx_ctx ctx;
  611. union Vmxnet3_GenericDesc *gdesc;
  612. /* conservatively estimate # of descriptors to use */
  613. count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) +
  614. skb_shinfo(skb)->nr_frags + 1;
  615. ctx.ipv4 = (skb->protocol == __constant_ntohs(ETH_P_IP));
  616. ctx.mss = skb_shinfo(skb)->gso_size;
  617. if (ctx.mss) {
  618. if (skb_header_cloned(skb)) {
  619. if (unlikely(pskb_expand_head(skb, 0, 0,
  620. GFP_ATOMIC) != 0)) {
  621. tq->stats.drop_tso++;
  622. goto drop_pkt;
  623. }
  624. tq->stats.copy_skb_header++;
  625. }
  626. vmxnet3_prepare_tso(skb, &ctx);
  627. } else {
  628. if (unlikely(count > VMXNET3_MAX_TXD_PER_PKT)) {
  629. /* non-tso pkts must not use more than
  630. * VMXNET3_MAX_TXD_PER_PKT entries
  631. */
  632. if (skb_linearize(skb) != 0) {
  633. tq->stats.drop_too_many_frags++;
  634. goto drop_pkt;
  635. }
  636. tq->stats.linearized++;
  637. /* recalculate the # of descriptors to use */
  638. count = VMXNET3_TXD_NEEDED(skb_headlen(skb)) + 1;
  639. }
  640. }
  641. ret = vmxnet3_parse_and_copy_hdr(skb, tq, &ctx, adapter);
  642. if (ret >= 0) {
  643. BUG_ON(ret <= 0 && ctx.copy_size != 0);
  644. /* hdrs parsed, check against other limits */
  645. if (ctx.mss) {
  646. if (unlikely(ctx.eth_ip_hdr_size + ctx.l4_hdr_size >
  647. VMXNET3_MAX_TX_BUF_SIZE)) {
  648. goto hdr_too_big;
  649. }
  650. } else {
  651. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  652. if (unlikely(ctx.eth_ip_hdr_size +
  653. skb->csum_offset >
  654. VMXNET3_MAX_CSUM_OFFSET)) {
  655. goto hdr_too_big;
  656. }
  657. }
  658. }
  659. } else {
  660. tq->stats.drop_hdr_inspect_err++;
  661. goto drop_pkt;
  662. }
  663. spin_lock_irqsave(&tq->tx_lock, flags);
  664. if (count > vmxnet3_cmd_ring_desc_avail(&tq->tx_ring)) {
  665. tq->stats.tx_ring_full++;
  666. dprintk(KERN_ERR "tx queue stopped on %s, next2comp %u"
  667. " next2fill %u\n", adapter->netdev->name,
  668. tq->tx_ring.next2comp, tq->tx_ring.next2fill);
  669. vmxnet3_tq_stop(tq, adapter);
  670. spin_unlock_irqrestore(&tq->tx_lock, flags);
  671. return NETDEV_TX_BUSY;
  672. }
  673. /* fill tx descs related to addr & len */
  674. vmxnet3_map_pkt(skb, &ctx, tq, adapter->pdev, adapter);
  675. /* setup the EOP desc */
  676. ctx.eop_txd->dword[3] = VMXNET3_TXD_CQ | VMXNET3_TXD_EOP;
  677. /* setup the SOP desc */
  678. gdesc = ctx.sop_txd;
  679. if (ctx.mss) {
  680. gdesc->txd.hlen = ctx.eth_ip_hdr_size + ctx.l4_hdr_size;
  681. gdesc->txd.om = VMXNET3_OM_TSO;
  682. gdesc->txd.msscof = ctx.mss;
  683. tq->shared->txNumDeferred += (skb->len - gdesc->txd.hlen +
  684. ctx.mss - 1) / ctx.mss;
  685. } else {
  686. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  687. gdesc->txd.hlen = ctx.eth_ip_hdr_size;
  688. gdesc->txd.om = VMXNET3_OM_CSUM;
  689. gdesc->txd.msscof = ctx.eth_ip_hdr_size +
  690. skb->csum_offset;
  691. } else {
  692. gdesc->txd.om = 0;
  693. gdesc->txd.msscof = 0;
  694. }
  695. tq->shared->txNumDeferred++;
  696. }
  697. if (vlan_tx_tag_present(skb)) {
  698. gdesc->txd.ti = 1;
  699. gdesc->txd.tci = vlan_tx_tag_get(skb);
  700. }
  701. wmb();
  702. /* finally flips the GEN bit of the SOP desc */
  703. gdesc->dword[2] ^= VMXNET3_TXD_GEN;
  704. dprintk(KERN_ERR "txd[%u]: SOP 0x%Lx 0x%x 0x%x\n",
  705. (u32)((union Vmxnet3_GenericDesc *)ctx.sop_txd -
  706. tq->tx_ring.base), gdesc->txd.addr, gdesc->dword[2],
  707. gdesc->dword[3]);
  708. spin_unlock_irqrestore(&tq->tx_lock, flags);
  709. if (tq->shared->txNumDeferred >= tq->shared->txThreshold) {
  710. tq->shared->txNumDeferred = 0;
  711. VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_TXPROD,
  712. tq->tx_ring.next2fill);
  713. }
  714. netdev->trans_start = jiffies;
  715. return NETDEV_TX_OK;
  716. hdr_too_big:
  717. tq->stats.drop_oversized_hdr++;
  718. drop_pkt:
  719. tq->stats.drop_total++;
  720. dev_kfree_skb(skb);
  721. return NETDEV_TX_OK;
  722. }
  723. static netdev_tx_t
  724. vmxnet3_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  725. {
  726. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  727. struct vmxnet3_tx_queue *tq = &adapter->tx_queue;
  728. return vmxnet3_tq_xmit(skb, tq, adapter, netdev);
  729. }
  730. static void
  731. vmxnet3_rx_csum(struct vmxnet3_adapter *adapter,
  732. struct sk_buff *skb,
  733. union Vmxnet3_GenericDesc *gdesc)
  734. {
  735. if (!gdesc->rcd.cnc && adapter->rxcsum) {
  736. /* typical case: TCP/UDP over IP and both csums are correct */
  737. if ((gdesc->dword[3] & VMXNET3_RCD_CSUM_OK) ==
  738. VMXNET3_RCD_CSUM_OK) {
  739. skb->ip_summed = CHECKSUM_UNNECESSARY;
  740. BUG_ON(!(gdesc->rcd.tcp || gdesc->rcd.udp));
  741. BUG_ON(!(gdesc->rcd.v4 || gdesc->rcd.v6));
  742. BUG_ON(gdesc->rcd.frg);
  743. } else {
  744. if (gdesc->rcd.csum) {
  745. skb->csum = htons(gdesc->rcd.csum);
  746. skb->ip_summed = CHECKSUM_PARTIAL;
  747. } else {
  748. skb->ip_summed = CHECKSUM_NONE;
  749. }
  750. }
  751. } else {
  752. skb->ip_summed = CHECKSUM_NONE;
  753. }
  754. }
  755. static void
  756. vmxnet3_rx_error(struct vmxnet3_rx_queue *rq, struct Vmxnet3_RxCompDesc *rcd,
  757. struct vmxnet3_rx_ctx *ctx, struct vmxnet3_adapter *adapter)
  758. {
  759. rq->stats.drop_err++;
  760. if (!rcd->fcs)
  761. rq->stats.drop_fcs++;
  762. rq->stats.drop_total++;
  763. /*
  764. * We do not unmap and chain the rx buffer to the skb.
  765. * We basically pretend this buffer is not used and will be recycled
  766. * by vmxnet3_rq_alloc_rx_buf()
  767. */
  768. /*
  769. * ctx->skb may be NULL if this is the first and the only one
  770. * desc for the pkt
  771. */
  772. if (ctx->skb)
  773. dev_kfree_skb_irq(ctx->skb);
  774. ctx->skb = NULL;
  775. }
  776. static int
  777. vmxnet3_rq_rx_complete(struct vmxnet3_rx_queue *rq,
  778. struct vmxnet3_adapter *adapter, int quota)
  779. {
  780. static u32 rxprod_reg[2] = {VMXNET3_REG_RXPROD, VMXNET3_REG_RXPROD2};
  781. u32 num_rxd = 0;
  782. struct Vmxnet3_RxCompDesc *rcd;
  783. struct vmxnet3_rx_ctx *ctx = &rq->rx_ctx;
  784. rcd = &rq->comp_ring.base[rq->comp_ring.next2proc].rcd;
  785. while (rcd->gen == rq->comp_ring.gen) {
  786. struct vmxnet3_rx_buf_info *rbi;
  787. struct sk_buff *skb;
  788. int num_to_alloc;
  789. struct Vmxnet3_RxDesc *rxd;
  790. u32 idx, ring_idx;
  791. if (num_rxd >= quota) {
  792. /* we may stop even before we see the EOP desc of
  793. * the current pkt
  794. */
  795. break;
  796. }
  797. num_rxd++;
  798. idx = rcd->rxdIdx;
  799. ring_idx = rcd->rqID == rq->qid ? 0 : 1;
  800. rxd = &rq->rx_ring[ring_idx].base[idx].rxd;
  801. rbi = rq->buf_info[ring_idx] + idx;
  802. BUG_ON(rxd->addr != rbi->dma_addr || rxd->len != rbi->len);
  803. if (unlikely(rcd->eop && rcd->err)) {
  804. vmxnet3_rx_error(rq, rcd, ctx, adapter);
  805. goto rcd_done;
  806. }
  807. if (rcd->sop) { /* first buf of the pkt */
  808. BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_HEAD ||
  809. rcd->rqID != rq->qid);
  810. BUG_ON(rbi->buf_type != VMXNET3_RX_BUF_SKB);
  811. BUG_ON(ctx->skb != NULL || rbi->skb == NULL);
  812. if (unlikely(rcd->len == 0)) {
  813. /* Pretend the rx buffer is skipped. */
  814. BUG_ON(!(rcd->sop && rcd->eop));
  815. dprintk(KERN_ERR "rxRing[%u][%u] 0 length\n",
  816. ring_idx, idx);
  817. goto rcd_done;
  818. }
  819. ctx->skb = rbi->skb;
  820. rbi->skb = NULL;
  821. pci_unmap_single(adapter->pdev, rbi->dma_addr, rbi->len,
  822. PCI_DMA_FROMDEVICE);
  823. skb_put(ctx->skb, rcd->len);
  824. } else {
  825. BUG_ON(ctx->skb == NULL);
  826. /* non SOP buffer must be type 1 in most cases */
  827. if (rbi->buf_type == VMXNET3_RX_BUF_PAGE) {
  828. BUG_ON(rxd->btype != VMXNET3_RXD_BTYPE_BODY);
  829. if (rcd->len) {
  830. pci_unmap_page(adapter->pdev,
  831. rbi->dma_addr, rbi->len,
  832. PCI_DMA_FROMDEVICE);
  833. vmxnet3_append_frag(ctx->skb, rcd, rbi);
  834. rbi->page = NULL;
  835. }
  836. } else {
  837. /*
  838. * The only time a non-SOP buffer is type 0 is
  839. * when it's EOP and error flag is raised, which
  840. * has already been handled.
  841. */
  842. BUG_ON(true);
  843. }
  844. }
  845. skb = ctx->skb;
  846. if (rcd->eop) {
  847. skb->len += skb->data_len;
  848. skb->truesize += skb->data_len;
  849. vmxnet3_rx_csum(adapter, skb,
  850. (union Vmxnet3_GenericDesc *)rcd);
  851. skb->protocol = eth_type_trans(skb, adapter->netdev);
  852. if (unlikely(adapter->vlan_grp && rcd->ts)) {
  853. vlan_hwaccel_receive_skb(skb,
  854. adapter->vlan_grp, rcd->tci);
  855. } else {
  856. netif_receive_skb(skb);
  857. }
  858. adapter->netdev->last_rx = jiffies;
  859. ctx->skb = NULL;
  860. }
  861. rcd_done:
  862. /* device may skip some rx descs */
  863. rq->rx_ring[ring_idx].next2comp = idx;
  864. VMXNET3_INC_RING_IDX_ONLY(rq->rx_ring[ring_idx].next2comp,
  865. rq->rx_ring[ring_idx].size);
  866. /* refill rx buffers frequently to avoid starving the h/w */
  867. num_to_alloc = vmxnet3_cmd_ring_desc_avail(rq->rx_ring +
  868. ring_idx);
  869. if (unlikely(num_to_alloc > VMXNET3_RX_ALLOC_THRESHOLD(rq,
  870. ring_idx, adapter))) {
  871. vmxnet3_rq_alloc_rx_buf(rq, ring_idx, num_to_alloc,
  872. adapter);
  873. /* if needed, update the register */
  874. if (unlikely(rq->shared->updateRxProd)) {
  875. VMXNET3_WRITE_BAR0_REG(adapter,
  876. rxprod_reg[ring_idx] + rq->qid * 8,
  877. rq->rx_ring[ring_idx].next2fill);
  878. rq->uncommitted[ring_idx] = 0;
  879. }
  880. }
  881. vmxnet3_comp_ring_adv_next2proc(&rq->comp_ring);
  882. rcd = &rq->comp_ring.base[rq->comp_ring.next2proc].rcd;
  883. }
  884. return num_rxd;
  885. }
  886. static void
  887. vmxnet3_rq_cleanup(struct vmxnet3_rx_queue *rq,
  888. struct vmxnet3_adapter *adapter)
  889. {
  890. u32 i, ring_idx;
  891. struct Vmxnet3_RxDesc *rxd;
  892. for (ring_idx = 0; ring_idx < 2; ring_idx++) {
  893. for (i = 0; i < rq->rx_ring[ring_idx].size; i++) {
  894. rxd = &rq->rx_ring[ring_idx].base[i].rxd;
  895. if (rxd->btype == VMXNET3_RXD_BTYPE_HEAD &&
  896. rq->buf_info[ring_idx][i].skb) {
  897. pci_unmap_single(adapter->pdev, rxd->addr,
  898. rxd->len, PCI_DMA_FROMDEVICE);
  899. dev_kfree_skb(rq->buf_info[ring_idx][i].skb);
  900. rq->buf_info[ring_idx][i].skb = NULL;
  901. } else if (rxd->btype == VMXNET3_RXD_BTYPE_BODY &&
  902. rq->buf_info[ring_idx][i].page) {
  903. pci_unmap_page(adapter->pdev, rxd->addr,
  904. rxd->len, PCI_DMA_FROMDEVICE);
  905. put_page(rq->buf_info[ring_idx][i].page);
  906. rq->buf_info[ring_idx][i].page = NULL;
  907. }
  908. }
  909. rq->rx_ring[ring_idx].gen = VMXNET3_INIT_GEN;
  910. rq->rx_ring[ring_idx].next2fill =
  911. rq->rx_ring[ring_idx].next2comp = 0;
  912. rq->uncommitted[ring_idx] = 0;
  913. }
  914. rq->comp_ring.gen = VMXNET3_INIT_GEN;
  915. rq->comp_ring.next2proc = 0;
  916. }
  917. void vmxnet3_rq_destroy(struct vmxnet3_rx_queue *rq,
  918. struct vmxnet3_adapter *adapter)
  919. {
  920. int i;
  921. int j;
  922. /* all rx buffers must have already been freed */
  923. for (i = 0; i < 2; i++) {
  924. if (rq->buf_info[i]) {
  925. for (j = 0; j < rq->rx_ring[i].size; j++)
  926. BUG_ON(rq->buf_info[i][j].page != NULL);
  927. }
  928. }
  929. kfree(rq->buf_info[0]);
  930. for (i = 0; i < 2; i++) {
  931. if (rq->rx_ring[i].base) {
  932. pci_free_consistent(adapter->pdev, rq->rx_ring[i].size
  933. * sizeof(struct Vmxnet3_RxDesc),
  934. rq->rx_ring[i].base,
  935. rq->rx_ring[i].basePA);
  936. rq->rx_ring[i].base = NULL;
  937. }
  938. rq->buf_info[i] = NULL;
  939. }
  940. if (rq->comp_ring.base) {
  941. pci_free_consistent(adapter->pdev, rq->comp_ring.size *
  942. sizeof(struct Vmxnet3_RxCompDesc),
  943. rq->comp_ring.base, rq->comp_ring.basePA);
  944. rq->comp_ring.base = NULL;
  945. }
  946. }
  947. static int
  948. vmxnet3_rq_init(struct vmxnet3_rx_queue *rq,
  949. struct vmxnet3_adapter *adapter)
  950. {
  951. int i;
  952. /* initialize buf_info */
  953. for (i = 0; i < rq->rx_ring[0].size; i++) {
  954. /* 1st buf for a pkt is skbuff */
  955. if (i % adapter->rx_buf_per_pkt == 0) {
  956. rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_SKB;
  957. rq->buf_info[0][i].len = adapter->skb_buf_size;
  958. } else { /* subsequent bufs for a pkt is frag */
  959. rq->buf_info[0][i].buf_type = VMXNET3_RX_BUF_PAGE;
  960. rq->buf_info[0][i].len = PAGE_SIZE;
  961. }
  962. }
  963. for (i = 0; i < rq->rx_ring[1].size; i++) {
  964. rq->buf_info[1][i].buf_type = VMXNET3_RX_BUF_PAGE;
  965. rq->buf_info[1][i].len = PAGE_SIZE;
  966. }
  967. /* reset internal state and allocate buffers for both rings */
  968. for (i = 0; i < 2; i++) {
  969. rq->rx_ring[i].next2fill = rq->rx_ring[i].next2comp = 0;
  970. rq->uncommitted[i] = 0;
  971. memset(rq->rx_ring[i].base, 0, rq->rx_ring[i].size *
  972. sizeof(struct Vmxnet3_RxDesc));
  973. rq->rx_ring[i].gen = VMXNET3_INIT_GEN;
  974. }
  975. if (vmxnet3_rq_alloc_rx_buf(rq, 0, rq->rx_ring[0].size - 1,
  976. adapter) == 0) {
  977. /* at least has 1 rx buffer for the 1st ring */
  978. return -ENOMEM;
  979. }
  980. vmxnet3_rq_alloc_rx_buf(rq, 1, rq->rx_ring[1].size - 1, adapter);
  981. /* reset the comp ring */
  982. rq->comp_ring.next2proc = 0;
  983. memset(rq->comp_ring.base, 0, rq->comp_ring.size *
  984. sizeof(struct Vmxnet3_RxCompDesc));
  985. rq->comp_ring.gen = VMXNET3_INIT_GEN;
  986. /* reset rxctx */
  987. rq->rx_ctx.skb = NULL;
  988. /* stats are not reset */
  989. return 0;
  990. }
  991. static int
  992. vmxnet3_rq_create(struct vmxnet3_rx_queue *rq, struct vmxnet3_adapter *adapter)
  993. {
  994. int i;
  995. size_t sz;
  996. struct vmxnet3_rx_buf_info *bi;
  997. for (i = 0; i < 2; i++) {
  998. sz = rq->rx_ring[i].size * sizeof(struct Vmxnet3_RxDesc);
  999. rq->rx_ring[i].base = pci_alloc_consistent(adapter->pdev, sz,
  1000. &rq->rx_ring[i].basePA);
  1001. if (!rq->rx_ring[i].base) {
  1002. printk(KERN_ERR "%s: failed to allocate rx ring %d\n",
  1003. adapter->netdev->name, i);
  1004. goto err;
  1005. }
  1006. }
  1007. sz = rq->comp_ring.size * sizeof(struct Vmxnet3_RxCompDesc);
  1008. rq->comp_ring.base = pci_alloc_consistent(adapter->pdev, sz,
  1009. &rq->comp_ring.basePA);
  1010. if (!rq->comp_ring.base) {
  1011. printk(KERN_ERR "%s: failed to allocate rx comp ring\n",
  1012. adapter->netdev->name);
  1013. goto err;
  1014. }
  1015. sz = sizeof(struct vmxnet3_rx_buf_info) * (rq->rx_ring[0].size +
  1016. rq->rx_ring[1].size);
  1017. bi = kmalloc(sz, GFP_KERNEL);
  1018. if (!bi) {
  1019. printk(KERN_ERR "%s: failed to allocate rx bufinfo\n",
  1020. adapter->netdev->name);
  1021. goto err;
  1022. }
  1023. memset(bi, 0, sz);
  1024. rq->buf_info[0] = bi;
  1025. rq->buf_info[1] = bi + rq->rx_ring[0].size;
  1026. return 0;
  1027. err:
  1028. vmxnet3_rq_destroy(rq, adapter);
  1029. return -ENOMEM;
  1030. }
  1031. static int
  1032. vmxnet3_do_poll(struct vmxnet3_adapter *adapter, int budget)
  1033. {
  1034. if (unlikely(adapter->shared->ecr))
  1035. vmxnet3_process_events(adapter);
  1036. vmxnet3_tq_tx_complete(&adapter->tx_queue, adapter);
  1037. return vmxnet3_rq_rx_complete(&adapter->rx_queue, adapter, budget);
  1038. }
  1039. static int
  1040. vmxnet3_poll(struct napi_struct *napi, int budget)
  1041. {
  1042. struct vmxnet3_adapter *adapter = container_of(napi,
  1043. struct vmxnet3_adapter, napi);
  1044. int rxd_done;
  1045. rxd_done = vmxnet3_do_poll(adapter, budget);
  1046. if (rxd_done < budget) {
  1047. napi_complete(napi);
  1048. vmxnet3_enable_intr(adapter, 0);
  1049. }
  1050. return rxd_done;
  1051. }
  1052. /* Interrupt handler for vmxnet3 */
  1053. static irqreturn_t
  1054. vmxnet3_intr(int irq, void *dev_id)
  1055. {
  1056. struct net_device *dev = dev_id;
  1057. struct vmxnet3_adapter *adapter = netdev_priv(dev);
  1058. if (unlikely(adapter->intr.type == VMXNET3_IT_INTX)) {
  1059. u32 icr = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_ICR);
  1060. if (unlikely(icr == 0))
  1061. /* not ours */
  1062. return IRQ_NONE;
  1063. }
  1064. /* disable intr if needed */
  1065. if (adapter->intr.mask_mode == VMXNET3_IMM_ACTIVE)
  1066. vmxnet3_disable_intr(adapter, 0);
  1067. napi_schedule(&adapter->napi);
  1068. return IRQ_HANDLED;
  1069. }
  1070. #ifdef CONFIG_NET_POLL_CONTROLLER
  1071. /* netpoll callback. */
  1072. static void
  1073. vmxnet3_netpoll(struct net_device *netdev)
  1074. {
  1075. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1076. int irq;
  1077. if (adapter->intr.type == VMXNET3_IT_MSIX)
  1078. irq = adapter->intr.msix_entries[0].vector;
  1079. else
  1080. irq = adapter->pdev->irq;
  1081. disable_irq(irq);
  1082. vmxnet3_intr(irq, netdev);
  1083. enable_irq(irq);
  1084. }
  1085. #endif
  1086. static int
  1087. vmxnet3_request_irqs(struct vmxnet3_adapter *adapter)
  1088. {
  1089. int err;
  1090. if (adapter->intr.type == VMXNET3_IT_MSIX) {
  1091. /* we only use 1 MSI-X vector */
  1092. err = request_irq(adapter->intr.msix_entries[0].vector,
  1093. vmxnet3_intr, 0, adapter->netdev->name,
  1094. adapter->netdev);
  1095. } else if (adapter->intr.type == VMXNET3_IT_MSI) {
  1096. err = request_irq(adapter->pdev->irq, vmxnet3_intr, 0,
  1097. adapter->netdev->name, adapter->netdev);
  1098. } else {
  1099. err = request_irq(adapter->pdev->irq, vmxnet3_intr,
  1100. IRQF_SHARED, adapter->netdev->name,
  1101. adapter->netdev);
  1102. }
  1103. if (err)
  1104. printk(KERN_ERR "Failed to request irq %s (intr type:%d), error"
  1105. ":%d\n", adapter->netdev->name, adapter->intr.type, err);
  1106. if (!err) {
  1107. int i;
  1108. /* init our intr settings */
  1109. for (i = 0; i < adapter->intr.num_intrs; i++)
  1110. adapter->intr.mod_levels[i] = UPT1_IML_ADAPTIVE;
  1111. /* next setup intr index for all intr sources */
  1112. adapter->tx_queue.comp_ring.intr_idx = 0;
  1113. adapter->rx_queue.comp_ring.intr_idx = 0;
  1114. adapter->intr.event_intr_idx = 0;
  1115. printk(KERN_INFO "%s: intr type %u, mode %u, %u vectors "
  1116. "allocated\n", adapter->netdev->name, adapter->intr.type,
  1117. adapter->intr.mask_mode, adapter->intr.num_intrs);
  1118. }
  1119. return err;
  1120. }
  1121. static void
  1122. vmxnet3_free_irqs(struct vmxnet3_adapter *adapter)
  1123. {
  1124. BUG_ON(adapter->intr.type == VMXNET3_IT_AUTO ||
  1125. adapter->intr.num_intrs <= 0);
  1126. switch (adapter->intr.type) {
  1127. case VMXNET3_IT_MSIX:
  1128. {
  1129. int i;
  1130. for (i = 0; i < adapter->intr.num_intrs; i++)
  1131. free_irq(adapter->intr.msix_entries[i].vector,
  1132. adapter->netdev);
  1133. break;
  1134. }
  1135. case VMXNET3_IT_MSI:
  1136. free_irq(adapter->pdev->irq, adapter->netdev);
  1137. break;
  1138. case VMXNET3_IT_INTX:
  1139. free_irq(adapter->pdev->irq, adapter->netdev);
  1140. break;
  1141. default:
  1142. BUG_ON(true);
  1143. }
  1144. }
  1145. static void
  1146. vmxnet3_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
  1147. {
  1148. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1149. struct Vmxnet3_DriverShared *shared = adapter->shared;
  1150. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1151. if (grp) {
  1152. /* add vlan rx stripping. */
  1153. if (adapter->netdev->features & NETIF_F_HW_VLAN_RX) {
  1154. int i;
  1155. struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
  1156. adapter->vlan_grp = grp;
  1157. /* update FEATURES to device */
  1158. devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
  1159. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1160. VMXNET3_CMD_UPDATE_FEATURE);
  1161. /*
  1162. * Clear entire vfTable; then enable untagged pkts.
  1163. * Note: setting one entry in vfTable to non-zero turns
  1164. * on VLAN rx filtering.
  1165. */
  1166. for (i = 0; i < VMXNET3_VFT_SIZE; i++)
  1167. vfTable[i] = 0;
  1168. VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
  1169. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1170. VMXNET3_CMD_UPDATE_VLAN_FILTERS);
  1171. } else {
  1172. printk(KERN_ERR "%s: vlan_rx_register when device has "
  1173. "no NETIF_F_HW_VLAN_RX\n", netdev->name);
  1174. }
  1175. } else {
  1176. /* remove vlan rx stripping. */
  1177. struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
  1178. adapter->vlan_grp = NULL;
  1179. if (devRead->misc.uptFeatures & UPT1_F_RXVLAN) {
  1180. int i;
  1181. for (i = 0; i < VMXNET3_VFT_SIZE; i++) {
  1182. /* clear entire vfTable; this also disables
  1183. * VLAN rx filtering
  1184. */
  1185. vfTable[i] = 0;
  1186. }
  1187. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1188. VMXNET3_CMD_UPDATE_VLAN_FILTERS);
  1189. /* update FEATURES to device */
  1190. devRead->misc.uptFeatures &= ~UPT1_F_RXVLAN;
  1191. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1192. VMXNET3_CMD_UPDATE_FEATURE);
  1193. }
  1194. }
  1195. }
  1196. static void
  1197. vmxnet3_restore_vlan(struct vmxnet3_adapter *adapter)
  1198. {
  1199. if (adapter->vlan_grp) {
  1200. u16 vid;
  1201. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1202. bool activeVlan = false;
  1203. for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
  1204. if (vlan_group_get_device(adapter->vlan_grp, vid)) {
  1205. VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
  1206. activeVlan = true;
  1207. }
  1208. }
  1209. if (activeVlan) {
  1210. /* continue to allow untagged pkts */
  1211. VMXNET3_SET_VFTABLE_ENTRY(vfTable, 0);
  1212. }
  1213. }
  1214. }
  1215. static void
  1216. vmxnet3_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  1217. {
  1218. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1219. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1220. VMXNET3_SET_VFTABLE_ENTRY(vfTable, vid);
  1221. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1222. VMXNET3_CMD_UPDATE_VLAN_FILTERS);
  1223. }
  1224. static void
  1225. vmxnet3_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  1226. {
  1227. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1228. u32 *vfTable = adapter->shared->devRead.rxFilterConf.vfTable;
  1229. VMXNET3_CLEAR_VFTABLE_ENTRY(vfTable, vid);
  1230. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1231. VMXNET3_CMD_UPDATE_VLAN_FILTERS);
  1232. }
  1233. static u8 *
  1234. vmxnet3_copy_mc(struct net_device *netdev)
  1235. {
  1236. u8 *buf = NULL;
  1237. u32 sz = netdev->mc_count * ETH_ALEN;
  1238. /* struct Vmxnet3_RxFilterConf.mfTableLen is u16. */
  1239. if (sz <= 0xffff) {
  1240. /* We may be called with BH disabled */
  1241. buf = kmalloc(sz, GFP_ATOMIC);
  1242. if (buf) {
  1243. int i;
  1244. struct dev_mc_list *mc = netdev->mc_list;
  1245. for (i = 0; i < netdev->mc_count; i++) {
  1246. BUG_ON(!mc);
  1247. memcpy(buf + i * ETH_ALEN, mc->dmi_addr,
  1248. ETH_ALEN);
  1249. mc = mc->next;
  1250. }
  1251. }
  1252. }
  1253. return buf;
  1254. }
  1255. static void
  1256. vmxnet3_set_mc(struct net_device *netdev)
  1257. {
  1258. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1259. struct Vmxnet3_RxFilterConf *rxConf =
  1260. &adapter->shared->devRead.rxFilterConf;
  1261. u8 *new_table = NULL;
  1262. u32 new_mode = VMXNET3_RXM_UCAST;
  1263. if (netdev->flags & IFF_PROMISC)
  1264. new_mode |= VMXNET3_RXM_PROMISC;
  1265. if (netdev->flags & IFF_BROADCAST)
  1266. new_mode |= VMXNET3_RXM_BCAST;
  1267. if (netdev->flags & IFF_ALLMULTI)
  1268. new_mode |= VMXNET3_RXM_ALL_MULTI;
  1269. else
  1270. if (netdev->mc_count > 0) {
  1271. new_table = vmxnet3_copy_mc(netdev);
  1272. if (new_table) {
  1273. new_mode |= VMXNET3_RXM_MCAST;
  1274. rxConf->mfTableLen = netdev->mc_count *
  1275. ETH_ALEN;
  1276. rxConf->mfTablePA = virt_to_phys(new_table);
  1277. } else {
  1278. printk(KERN_INFO "%s: failed to copy mcast list"
  1279. ", setting ALL_MULTI\n", netdev->name);
  1280. new_mode |= VMXNET3_RXM_ALL_MULTI;
  1281. }
  1282. }
  1283. if (!(new_mode & VMXNET3_RXM_MCAST)) {
  1284. rxConf->mfTableLen = 0;
  1285. rxConf->mfTablePA = 0;
  1286. }
  1287. if (new_mode != rxConf->rxMode) {
  1288. rxConf->rxMode = new_mode;
  1289. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1290. VMXNET3_CMD_UPDATE_RX_MODE);
  1291. }
  1292. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1293. VMXNET3_CMD_UPDATE_MAC_FILTERS);
  1294. kfree(new_table);
  1295. }
  1296. /*
  1297. * Set up driver_shared based on settings in adapter.
  1298. */
  1299. static void
  1300. vmxnet3_setup_driver_shared(struct vmxnet3_adapter *adapter)
  1301. {
  1302. struct Vmxnet3_DriverShared *shared = adapter->shared;
  1303. struct Vmxnet3_DSDevRead *devRead = &shared->devRead;
  1304. struct Vmxnet3_TxQueueConf *tqc;
  1305. struct Vmxnet3_RxQueueConf *rqc;
  1306. int i;
  1307. memset(shared, 0, sizeof(*shared));
  1308. /* driver settings */
  1309. shared->magic = VMXNET3_REV1_MAGIC;
  1310. devRead->misc.driverInfo.version = VMXNET3_DRIVER_VERSION_NUM;
  1311. devRead->misc.driverInfo.gos.gosBits = (sizeof(void *) == 4 ?
  1312. VMXNET3_GOS_BITS_32 : VMXNET3_GOS_BITS_64);
  1313. devRead->misc.driverInfo.gos.gosType = VMXNET3_GOS_TYPE_LINUX;
  1314. devRead->misc.driverInfo.vmxnet3RevSpt = 1;
  1315. devRead->misc.driverInfo.uptVerSpt = 1;
  1316. devRead->misc.ddPA = virt_to_phys(adapter);
  1317. devRead->misc.ddLen = sizeof(struct vmxnet3_adapter);
  1318. /* set up feature flags */
  1319. if (adapter->rxcsum)
  1320. devRead->misc.uptFeatures |= UPT1_F_RXCSUM;
  1321. if (adapter->lro) {
  1322. devRead->misc.uptFeatures |= UPT1_F_LRO;
  1323. devRead->misc.maxNumRxSG = 1 + MAX_SKB_FRAGS;
  1324. }
  1325. if ((adapter->netdev->features & NETIF_F_HW_VLAN_RX)
  1326. && adapter->vlan_grp) {
  1327. devRead->misc.uptFeatures |= UPT1_F_RXVLAN;
  1328. }
  1329. devRead->misc.mtu = adapter->netdev->mtu;
  1330. devRead->misc.queueDescPA = adapter->queue_desc_pa;
  1331. devRead->misc.queueDescLen = sizeof(struct Vmxnet3_TxQueueDesc) +
  1332. sizeof(struct Vmxnet3_RxQueueDesc);
  1333. /* tx queue settings */
  1334. BUG_ON(adapter->tx_queue.tx_ring.base == NULL);
  1335. devRead->misc.numTxQueues = 1;
  1336. tqc = &adapter->tqd_start->conf;
  1337. tqc->txRingBasePA = adapter->tx_queue.tx_ring.basePA;
  1338. tqc->dataRingBasePA = adapter->tx_queue.data_ring.basePA;
  1339. tqc->compRingBasePA = adapter->tx_queue.comp_ring.basePA;
  1340. tqc->ddPA = virt_to_phys(adapter->tx_queue.buf_info);
  1341. tqc->txRingSize = adapter->tx_queue.tx_ring.size;
  1342. tqc->dataRingSize = adapter->tx_queue.data_ring.size;
  1343. tqc->compRingSize = adapter->tx_queue.comp_ring.size;
  1344. tqc->ddLen = sizeof(struct vmxnet3_tx_buf_info) *
  1345. tqc->txRingSize;
  1346. tqc->intrIdx = adapter->tx_queue.comp_ring.intr_idx;
  1347. /* rx queue settings */
  1348. devRead->misc.numRxQueues = 1;
  1349. rqc = &adapter->rqd_start->conf;
  1350. rqc->rxRingBasePA[0] = adapter->rx_queue.rx_ring[0].basePA;
  1351. rqc->rxRingBasePA[1] = adapter->rx_queue.rx_ring[1].basePA;
  1352. rqc->compRingBasePA = adapter->rx_queue.comp_ring.basePA;
  1353. rqc->ddPA = virt_to_phys(adapter->rx_queue.buf_info);
  1354. rqc->rxRingSize[0] = adapter->rx_queue.rx_ring[0].size;
  1355. rqc->rxRingSize[1] = adapter->rx_queue.rx_ring[1].size;
  1356. rqc->compRingSize = adapter->rx_queue.comp_ring.size;
  1357. rqc->ddLen = sizeof(struct vmxnet3_rx_buf_info) *
  1358. (rqc->rxRingSize[0] + rqc->rxRingSize[1]);
  1359. rqc->intrIdx = adapter->rx_queue.comp_ring.intr_idx;
  1360. /* intr settings */
  1361. devRead->intrConf.autoMask = adapter->intr.mask_mode ==
  1362. VMXNET3_IMM_AUTO;
  1363. devRead->intrConf.numIntrs = adapter->intr.num_intrs;
  1364. for (i = 0; i < adapter->intr.num_intrs; i++)
  1365. devRead->intrConf.modLevels[i] = adapter->intr.mod_levels[i];
  1366. devRead->intrConf.eventIntrIdx = adapter->intr.event_intr_idx;
  1367. /* rx filter settings */
  1368. devRead->rxFilterConf.rxMode = 0;
  1369. vmxnet3_restore_vlan(adapter);
  1370. /* the rest are already zeroed */
  1371. }
  1372. int
  1373. vmxnet3_activate_dev(struct vmxnet3_adapter *adapter)
  1374. {
  1375. int err;
  1376. u32 ret;
  1377. dprintk(KERN_ERR "%s: skb_buf_size %d, rx_buf_per_pkt %d, ring sizes"
  1378. " %u %u %u\n", adapter->netdev->name, adapter->skb_buf_size,
  1379. adapter->rx_buf_per_pkt, adapter->tx_queue.tx_ring.size,
  1380. adapter->rx_queue.rx_ring[0].size,
  1381. adapter->rx_queue.rx_ring[1].size);
  1382. vmxnet3_tq_init(&adapter->tx_queue, adapter);
  1383. err = vmxnet3_rq_init(&adapter->rx_queue, adapter);
  1384. if (err) {
  1385. printk(KERN_ERR "Failed to init rx queue for %s: error %d\n",
  1386. adapter->netdev->name, err);
  1387. goto rq_err;
  1388. }
  1389. err = vmxnet3_request_irqs(adapter);
  1390. if (err) {
  1391. printk(KERN_ERR "Failed to setup irq for %s: error %d\n",
  1392. adapter->netdev->name, err);
  1393. goto irq_err;
  1394. }
  1395. vmxnet3_setup_driver_shared(adapter);
  1396. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL,
  1397. VMXNET3_GET_ADDR_LO(adapter->shared_pa));
  1398. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH,
  1399. VMXNET3_GET_ADDR_HI(adapter->shared_pa));
  1400. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1401. VMXNET3_CMD_ACTIVATE_DEV);
  1402. ret = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
  1403. if (ret != 0) {
  1404. printk(KERN_ERR "Failed to activate dev %s: error %u\n",
  1405. adapter->netdev->name, ret);
  1406. err = -EINVAL;
  1407. goto activate_err;
  1408. }
  1409. VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_RXPROD,
  1410. adapter->rx_queue.rx_ring[0].next2fill);
  1411. VMXNET3_WRITE_BAR0_REG(adapter, VMXNET3_REG_RXPROD2,
  1412. adapter->rx_queue.rx_ring[1].next2fill);
  1413. /* Apply the rx filter settins last. */
  1414. vmxnet3_set_mc(adapter->netdev);
  1415. /*
  1416. * Check link state when first activating device. It will start the
  1417. * tx queue if the link is up.
  1418. */
  1419. vmxnet3_check_link(adapter);
  1420. napi_enable(&adapter->napi);
  1421. vmxnet3_enable_all_intrs(adapter);
  1422. clear_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
  1423. return 0;
  1424. activate_err:
  1425. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAL, 0);
  1426. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_DSAH, 0);
  1427. vmxnet3_free_irqs(adapter);
  1428. irq_err:
  1429. rq_err:
  1430. /* free up buffers we allocated */
  1431. vmxnet3_rq_cleanup(&adapter->rx_queue, adapter);
  1432. return err;
  1433. }
  1434. void
  1435. vmxnet3_reset_dev(struct vmxnet3_adapter *adapter)
  1436. {
  1437. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD, VMXNET3_CMD_RESET_DEV);
  1438. }
  1439. int
  1440. vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter)
  1441. {
  1442. if (test_and_set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state))
  1443. return 0;
  1444. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1445. VMXNET3_CMD_QUIESCE_DEV);
  1446. vmxnet3_disable_all_intrs(adapter);
  1447. napi_disable(&adapter->napi);
  1448. netif_tx_disable(adapter->netdev);
  1449. adapter->link_speed = 0;
  1450. netif_carrier_off(adapter->netdev);
  1451. vmxnet3_tq_cleanup(&adapter->tx_queue, adapter);
  1452. vmxnet3_rq_cleanup(&adapter->rx_queue, adapter);
  1453. vmxnet3_free_irqs(adapter);
  1454. return 0;
  1455. }
  1456. static void
  1457. vmxnet3_write_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
  1458. {
  1459. u32 tmp;
  1460. tmp = *(u32 *)mac;
  1461. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACL, tmp);
  1462. tmp = (mac[5] << 8) | mac[4];
  1463. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_MACH, tmp);
  1464. }
  1465. static int
  1466. vmxnet3_set_mac_addr(struct net_device *netdev, void *p)
  1467. {
  1468. struct sockaddr *addr = p;
  1469. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1470. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1471. vmxnet3_write_mac_addr(adapter, addr->sa_data);
  1472. return 0;
  1473. }
  1474. /* ==================== initialization and cleanup routines ============ */
  1475. static int
  1476. vmxnet3_alloc_pci_resources(struct vmxnet3_adapter *adapter, bool *dma64)
  1477. {
  1478. int err;
  1479. unsigned long mmio_start, mmio_len;
  1480. struct pci_dev *pdev = adapter->pdev;
  1481. err = pci_enable_device(pdev);
  1482. if (err) {
  1483. printk(KERN_ERR "Failed to enable adapter %s: error %d\n",
  1484. pci_name(pdev), err);
  1485. return err;
  1486. }
  1487. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) == 0) {
  1488. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
  1489. printk(KERN_ERR "pci_set_consistent_dma_mask failed "
  1490. "for adapter %s\n", pci_name(pdev));
  1491. err = -EIO;
  1492. goto err_set_mask;
  1493. }
  1494. *dma64 = true;
  1495. } else {
  1496. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
  1497. printk(KERN_ERR "pci_set_dma_mask failed for adapter "
  1498. "%s\n", pci_name(pdev));
  1499. err = -EIO;
  1500. goto err_set_mask;
  1501. }
  1502. *dma64 = false;
  1503. }
  1504. err = pci_request_selected_regions(pdev, (1 << 2) - 1,
  1505. vmxnet3_driver_name);
  1506. if (err) {
  1507. printk(KERN_ERR "Failed to request region for adapter %s: "
  1508. "error %d\n", pci_name(pdev), err);
  1509. goto err_set_mask;
  1510. }
  1511. pci_set_master(pdev);
  1512. mmio_start = pci_resource_start(pdev, 0);
  1513. mmio_len = pci_resource_len(pdev, 0);
  1514. adapter->hw_addr0 = ioremap(mmio_start, mmio_len);
  1515. if (!adapter->hw_addr0) {
  1516. printk(KERN_ERR "Failed to map bar0 for adapter %s\n",
  1517. pci_name(pdev));
  1518. err = -EIO;
  1519. goto err_ioremap;
  1520. }
  1521. mmio_start = pci_resource_start(pdev, 1);
  1522. mmio_len = pci_resource_len(pdev, 1);
  1523. adapter->hw_addr1 = ioremap(mmio_start, mmio_len);
  1524. if (!adapter->hw_addr1) {
  1525. printk(KERN_ERR "Failed to map bar1 for adapter %s\n",
  1526. pci_name(pdev));
  1527. err = -EIO;
  1528. goto err_bar1;
  1529. }
  1530. return 0;
  1531. err_bar1:
  1532. iounmap(adapter->hw_addr0);
  1533. err_ioremap:
  1534. pci_release_selected_regions(pdev, (1 << 2) - 1);
  1535. err_set_mask:
  1536. pci_disable_device(pdev);
  1537. return err;
  1538. }
  1539. static void
  1540. vmxnet3_free_pci_resources(struct vmxnet3_adapter *adapter)
  1541. {
  1542. BUG_ON(!adapter->pdev);
  1543. iounmap(adapter->hw_addr0);
  1544. iounmap(adapter->hw_addr1);
  1545. pci_release_selected_regions(adapter->pdev, (1 << 2) - 1);
  1546. pci_disable_device(adapter->pdev);
  1547. }
  1548. static void
  1549. vmxnet3_adjust_rx_ring_size(struct vmxnet3_adapter *adapter)
  1550. {
  1551. size_t sz;
  1552. if (adapter->netdev->mtu <= VMXNET3_MAX_SKB_BUF_SIZE -
  1553. VMXNET3_MAX_ETH_HDR_SIZE) {
  1554. adapter->skb_buf_size = adapter->netdev->mtu +
  1555. VMXNET3_MAX_ETH_HDR_SIZE;
  1556. if (adapter->skb_buf_size < VMXNET3_MIN_T0_BUF_SIZE)
  1557. adapter->skb_buf_size = VMXNET3_MIN_T0_BUF_SIZE;
  1558. adapter->rx_buf_per_pkt = 1;
  1559. } else {
  1560. adapter->skb_buf_size = VMXNET3_MAX_SKB_BUF_SIZE;
  1561. sz = adapter->netdev->mtu - VMXNET3_MAX_SKB_BUF_SIZE +
  1562. VMXNET3_MAX_ETH_HDR_SIZE;
  1563. adapter->rx_buf_per_pkt = 1 + (sz + PAGE_SIZE - 1) / PAGE_SIZE;
  1564. }
  1565. /*
  1566. * for simplicity, force the ring0 size to be a multiple of
  1567. * rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN
  1568. */
  1569. sz = adapter->rx_buf_per_pkt * VMXNET3_RING_SIZE_ALIGN;
  1570. adapter->rx_queue.rx_ring[0].size = (adapter->rx_queue.rx_ring[0].size +
  1571. sz - 1) / sz * sz;
  1572. adapter->rx_queue.rx_ring[0].size = min_t(u32,
  1573. adapter->rx_queue.rx_ring[0].size,
  1574. VMXNET3_RX_RING_MAX_SIZE / sz * sz);
  1575. }
  1576. int
  1577. vmxnet3_create_queues(struct vmxnet3_adapter *adapter, u32 tx_ring_size,
  1578. u32 rx_ring_size, u32 rx_ring2_size)
  1579. {
  1580. int err;
  1581. adapter->tx_queue.tx_ring.size = tx_ring_size;
  1582. adapter->tx_queue.data_ring.size = tx_ring_size;
  1583. adapter->tx_queue.comp_ring.size = tx_ring_size;
  1584. adapter->tx_queue.shared = &adapter->tqd_start->ctrl;
  1585. adapter->tx_queue.stopped = true;
  1586. err = vmxnet3_tq_create(&adapter->tx_queue, adapter);
  1587. if (err)
  1588. return err;
  1589. adapter->rx_queue.rx_ring[0].size = rx_ring_size;
  1590. adapter->rx_queue.rx_ring[1].size = rx_ring2_size;
  1591. vmxnet3_adjust_rx_ring_size(adapter);
  1592. adapter->rx_queue.comp_ring.size = adapter->rx_queue.rx_ring[0].size +
  1593. adapter->rx_queue.rx_ring[1].size;
  1594. adapter->rx_queue.qid = 0;
  1595. adapter->rx_queue.qid2 = 1;
  1596. adapter->rx_queue.shared = &adapter->rqd_start->ctrl;
  1597. err = vmxnet3_rq_create(&adapter->rx_queue, adapter);
  1598. if (err)
  1599. vmxnet3_tq_destroy(&adapter->tx_queue, adapter);
  1600. return err;
  1601. }
  1602. static int
  1603. vmxnet3_open(struct net_device *netdev)
  1604. {
  1605. struct vmxnet3_adapter *adapter;
  1606. int err;
  1607. adapter = netdev_priv(netdev);
  1608. spin_lock_init(&adapter->tx_queue.tx_lock);
  1609. err = vmxnet3_create_queues(adapter, VMXNET3_DEF_TX_RING_SIZE,
  1610. VMXNET3_DEF_RX_RING_SIZE,
  1611. VMXNET3_DEF_RX_RING_SIZE);
  1612. if (err)
  1613. goto queue_err;
  1614. err = vmxnet3_activate_dev(adapter);
  1615. if (err)
  1616. goto activate_err;
  1617. return 0;
  1618. activate_err:
  1619. vmxnet3_rq_destroy(&adapter->rx_queue, adapter);
  1620. vmxnet3_tq_destroy(&adapter->tx_queue, adapter);
  1621. queue_err:
  1622. return err;
  1623. }
  1624. static int
  1625. vmxnet3_close(struct net_device *netdev)
  1626. {
  1627. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1628. /*
  1629. * Reset_work may be in the middle of resetting the device, wait for its
  1630. * completion.
  1631. */
  1632. while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
  1633. msleep(1);
  1634. vmxnet3_quiesce_dev(adapter);
  1635. vmxnet3_rq_destroy(&adapter->rx_queue, adapter);
  1636. vmxnet3_tq_destroy(&adapter->tx_queue, adapter);
  1637. clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
  1638. return 0;
  1639. }
  1640. void
  1641. vmxnet3_force_close(struct vmxnet3_adapter *adapter)
  1642. {
  1643. /*
  1644. * we must clear VMXNET3_STATE_BIT_RESETTING, otherwise
  1645. * vmxnet3_close() will deadlock.
  1646. */
  1647. BUG_ON(test_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state));
  1648. /* we need to enable NAPI, otherwise dev_close will deadlock */
  1649. napi_enable(&adapter->napi);
  1650. dev_close(adapter->netdev);
  1651. }
  1652. static int
  1653. vmxnet3_change_mtu(struct net_device *netdev, int new_mtu)
  1654. {
  1655. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1656. int err = 0;
  1657. if (new_mtu < VMXNET3_MIN_MTU || new_mtu > VMXNET3_MAX_MTU)
  1658. return -EINVAL;
  1659. if (new_mtu > 1500 && !adapter->jumbo_frame)
  1660. return -EINVAL;
  1661. netdev->mtu = new_mtu;
  1662. /*
  1663. * Reset_work may be in the middle of resetting the device, wait for its
  1664. * completion.
  1665. */
  1666. while (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
  1667. msleep(1);
  1668. if (netif_running(netdev)) {
  1669. vmxnet3_quiesce_dev(adapter);
  1670. vmxnet3_reset_dev(adapter);
  1671. /* we need to re-create the rx queue based on the new mtu */
  1672. vmxnet3_rq_destroy(&adapter->rx_queue, adapter);
  1673. vmxnet3_adjust_rx_ring_size(adapter);
  1674. adapter->rx_queue.comp_ring.size =
  1675. adapter->rx_queue.rx_ring[0].size +
  1676. adapter->rx_queue.rx_ring[1].size;
  1677. err = vmxnet3_rq_create(&adapter->rx_queue, adapter);
  1678. if (err) {
  1679. printk(KERN_ERR "%s: failed to re-create rx queue,"
  1680. " error %d. Closing it.\n", netdev->name, err);
  1681. goto out;
  1682. }
  1683. err = vmxnet3_activate_dev(adapter);
  1684. if (err) {
  1685. printk(KERN_ERR "%s: failed to re-activate, error %d. "
  1686. "Closing it\n", netdev->name, err);
  1687. goto out;
  1688. }
  1689. }
  1690. out:
  1691. clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
  1692. if (err)
  1693. vmxnet3_force_close(adapter);
  1694. return err;
  1695. }
  1696. static void
  1697. vmxnet3_declare_features(struct vmxnet3_adapter *adapter, bool dma64)
  1698. {
  1699. struct net_device *netdev = adapter->netdev;
  1700. netdev->features = NETIF_F_SG |
  1701. NETIF_F_HW_CSUM |
  1702. NETIF_F_HW_VLAN_TX |
  1703. NETIF_F_HW_VLAN_RX |
  1704. NETIF_F_HW_VLAN_FILTER |
  1705. NETIF_F_TSO |
  1706. NETIF_F_TSO6 |
  1707. NETIF_F_LRO;
  1708. printk(KERN_INFO "features: sg csum vlan jf tso tsoIPv6 lro");
  1709. adapter->rxcsum = true;
  1710. adapter->jumbo_frame = true;
  1711. adapter->lro = true;
  1712. if (dma64) {
  1713. netdev->features |= NETIF_F_HIGHDMA;
  1714. printk(" highDMA");
  1715. }
  1716. netdev->vlan_features = netdev->features;
  1717. printk("\n");
  1718. }
  1719. static void
  1720. vmxnet3_read_mac_addr(struct vmxnet3_adapter *adapter, u8 *mac)
  1721. {
  1722. u32 tmp;
  1723. tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACL);
  1724. *(u32 *)mac = tmp;
  1725. tmp = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_MACH);
  1726. mac[4] = tmp & 0xff;
  1727. mac[5] = (tmp >> 8) & 0xff;
  1728. }
  1729. static void
  1730. vmxnet3_alloc_intr_resources(struct vmxnet3_adapter *adapter)
  1731. {
  1732. u32 cfg;
  1733. /* intr settings */
  1734. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  1735. VMXNET3_CMD_GET_CONF_INTR);
  1736. cfg = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_CMD);
  1737. adapter->intr.type = cfg & 0x3;
  1738. adapter->intr.mask_mode = (cfg >> 2) & 0x3;
  1739. if (adapter->intr.type == VMXNET3_IT_AUTO) {
  1740. int err;
  1741. adapter->intr.msix_entries[0].entry = 0;
  1742. err = pci_enable_msix(adapter->pdev, adapter->intr.msix_entries,
  1743. VMXNET3_LINUX_MAX_MSIX_VECT);
  1744. if (!err) {
  1745. adapter->intr.num_intrs = 1;
  1746. adapter->intr.type = VMXNET3_IT_MSIX;
  1747. return;
  1748. }
  1749. err = pci_enable_msi(adapter->pdev);
  1750. if (!err) {
  1751. adapter->intr.num_intrs = 1;
  1752. adapter->intr.type = VMXNET3_IT_MSI;
  1753. return;
  1754. }
  1755. }
  1756. adapter->intr.type = VMXNET3_IT_INTX;
  1757. /* INT-X related setting */
  1758. adapter->intr.num_intrs = 1;
  1759. }
  1760. static void
  1761. vmxnet3_free_intr_resources(struct vmxnet3_adapter *adapter)
  1762. {
  1763. if (adapter->intr.type == VMXNET3_IT_MSIX)
  1764. pci_disable_msix(adapter->pdev);
  1765. else if (adapter->intr.type == VMXNET3_IT_MSI)
  1766. pci_disable_msi(adapter->pdev);
  1767. else
  1768. BUG_ON(adapter->intr.type != VMXNET3_IT_INTX);
  1769. }
  1770. static void
  1771. vmxnet3_tx_timeout(struct net_device *netdev)
  1772. {
  1773. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1774. adapter->tx_timeout_count++;
  1775. printk(KERN_ERR "%s: tx hang\n", adapter->netdev->name);
  1776. schedule_work(&adapter->work);
  1777. }
  1778. static void
  1779. vmxnet3_reset_work(struct work_struct *data)
  1780. {
  1781. struct vmxnet3_adapter *adapter;
  1782. adapter = container_of(data, struct vmxnet3_adapter, work);
  1783. /* if another thread is resetting the device, no need to proceed */
  1784. if (test_and_set_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state))
  1785. return;
  1786. /* if the device is closed, we must leave it alone */
  1787. if (netif_running(adapter->netdev)) {
  1788. printk(KERN_INFO "%s: resetting\n", adapter->netdev->name);
  1789. vmxnet3_quiesce_dev(adapter);
  1790. vmxnet3_reset_dev(adapter);
  1791. vmxnet3_activate_dev(adapter);
  1792. } else {
  1793. printk(KERN_INFO "%s: already closed\n", adapter->netdev->name);
  1794. }
  1795. clear_bit(VMXNET3_STATE_BIT_RESETTING, &adapter->state);
  1796. }
  1797. static int __devinit
  1798. vmxnet3_probe_device(struct pci_dev *pdev,
  1799. const struct pci_device_id *id)
  1800. {
  1801. static const struct net_device_ops vmxnet3_netdev_ops = {
  1802. .ndo_open = vmxnet3_open,
  1803. .ndo_stop = vmxnet3_close,
  1804. .ndo_start_xmit = vmxnet3_xmit_frame,
  1805. .ndo_set_mac_address = vmxnet3_set_mac_addr,
  1806. .ndo_change_mtu = vmxnet3_change_mtu,
  1807. .ndo_get_stats = vmxnet3_get_stats,
  1808. .ndo_tx_timeout = vmxnet3_tx_timeout,
  1809. .ndo_set_multicast_list = vmxnet3_set_mc,
  1810. .ndo_vlan_rx_register = vmxnet3_vlan_rx_register,
  1811. .ndo_vlan_rx_add_vid = vmxnet3_vlan_rx_add_vid,
  1812. .ndo_vlan_rx_kill_vid = vmxnet3_vlan_rx_kill_vid,
  1813. #ifdef CONFIG_NET_POLL_CONTROLLER
  1814. .ndo_poll_controller = vmxnet3_netpoll,
  1815. #endif
  1816. };
  1817. int err;
  1818. bool dma64 = false; /* stupid gcc */
  1819. u32 ver;
  1820. struct net_device *netdev;
  1821. struct vmxnet3_adapter *adapter;
  1822. u8 mac[ETH_ALEN];
  1823. netdev = alloc_etherdev(sizeof(struct vmxnet3_adapter));
  1824. if (!netdev) {
  1825. printk(KERN_ERR "Failed to alloc ethernet device for adapter "
  1826. "%s\n", pci_name(pdev));
  1827. return -ENOMEM;
  1828. }
  1829. pci_set_drvdata(pdev, netdev);
  1830. adapter = netdev_priv(netdev);
  1831. adapter->netdev = netdev;
  1832. adapter->pdev = pdev;
  1833. adapter->shared = pci_alloc_consistent(adapter->pdev,
  1834. sizeof(struct Vmxnet3_DriverShared),
  1835. &adapter->shared_pa);
  1836. if (!adapter->shared) {
  1837. printk(KERN_ERR "Failed to allocate memory for %s\n",
  1838. pci_name(pdev));
  1839. err = -ENOMEM;
  1840. goto err_alloc_shared;
  1841. }
  1842. adapter->tqd_start = pci_alloc_consistent(adapter->pdev,
  1843. sizeof(struct Vmxnet3_TxQueueDesc) +
  1844. sizeof(struct Vmxnet3_RxQueueDesc),
  1845. &adapter->queue_desc_pa);
  1846. if (!adapter->tqd_start) {
  1847. printk(KERN_ERR "Failed to allocate memory for %s\n",
  1848. pci_name(pdev));
  1849. err = -ENOMEM;
  1850. goto err_alloc_queue_desc;
  1851. }
  1852. adapter->rqd_start = (struct Vmxnet3_RxQueueDesc *)(adapter->tqd_start
  1853. + 1);
  1854. adapter->pm_conf = kmalloc(sizeof(struct Vmxnet3_PMConf), GFP_KERNEL);
  1855. if (adapter->pm_conf == NULL) {
  1856. printk(KERN_ERR "Failed to allocate memory for %s\n",
  1857. pci_name(pdev));
  1858. err = -ENOMEM;
  1859. goto err_alloc_pm;
  1860. }
  1861. err = vmxnet3_alloc_pci_resources(adapter, &dma64);
  1862. if (err < 0)
  1863. goto err_alloc_pci;
  1864. ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_VRRS);
  1865. if (ver & 1) {
  1866. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_VRRS, 1);
  1867. } else {
  1868. printk(KERN_ERR "Incompatible h/w version (0x%x) for adapter"
  1869. " %s\n", ver, pci_name(pdev));
  1870. err = -EBUSY;
  1871. goto err_ver;
  1872. }
  1873. ver = VMXNET3_READ_BAR1_REG(adapter, VMXNET3_REG_UVRS);
  1874. if (ver & 1) {
  1875. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_UVRS, 1);
  1876. } else {
  1877. printk(KERN_ERR "Incompatible upt version (0x%x) for "
  1878. "adapter %s\n", ver, pci_name(pdev));
  1879. err = -EBUSY;
  1880. goto err_ver;
  1881. }
  1882. vmxnet3_declare_features(adapter, dma64);
  1883. adapter->dev_number = atomic_read(&devices_found);
  1884. vmxnet3_alloc_intr_resources(adapter);
  1885. vmxnet3_read_mac_addr(adapter, mac);
  1886. memcpy(netdev->dev_addr, mac, netdev->addr_len);
  1887. netdev->netdev_ops = &vmxnet3_netdev_ops;
  1888. netdev->watchdog_timeo = 5 * HZ;
  1889. vmxnet3_set_ethtool_ops(netdev);
  1890. INIT_WORK(&adapter->work, vmxnet3_reset_work);
  1891. netif_napi_add(netdev, &adapter->napi, vmxnet3_poll, 64);
  1892. SET_NETDEV_DEV(netdev, &pdev->dev);
  1893. err = register_netdev(netdev);
  1894. if (err) {
  1895. printk(KERN_ERR "Failed to register adapter %s\n",
  1896. pci_name(pdev));
  1897. goto err_register;
  1898. }
  1899. set_bit(VMXNET3_STATE_BIT_QUIESCED, &adapter->state);
  1900. atomic_inc(&devices_found);
  1901. return 0;
  1902. err_register:
  1903. vmxnet3_free_intr_resources(adapter);
  1904. err_ver:
  1905. vmxnet3_free_pci_resources(adapter);
  1906. err_alloc_pci:
  1907. kfree(adapter->pm_conf);
  1908. err_alloc_pm:
  1909. pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_TxQueueDesc) +
  1910. sizeof(struct Vmxnet3_RxQueueDesc),
  1911. adapter->tqd_start, adapter->queue_desc_pa);
  1912. err_alloc_queue_desc:
  1913. pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
  1914. adapter->shared, adapter->shared_pa);
  1915. err_alloc_shared:
  1916. pci_set_drvdata(pdev, NULL);
  1917. free_netdev(netdev);
  1918. return err;
  1919. }
  1920. static void __devexit
  1921. vmxnet3_remove_device(struct pci_dev *pdev)
  1922. {
  1923. struct net_device *netdev = pci_get_drvdata(pdev);
  1924. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1925. flush_scheduled_work();
  1926. unregister_netdev(netdev);
  1927. vmxnet3_free_intr_resources(adapter);
  1928. vmxnet3_free_pci_resources(adapter);
  1929. kfree(adapter->pm_conf);
  1930. pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_TxQueueDesc) +
  1931. sizeof(struct Vmxnet3_RxQueueDesc),
  1932. adapter->tqd_start, adapter->queue_desc_pa);
  1933. pci_free_consistent(adapter->pdev, sizeof(struct Vmxnet3_DriverShared),
  1934. adapter->shared, adapter->shared_pa);
  1935. free_netdev(netdev);
  1936. }
  1937. #ifdef CONFIG_PM
  1938. static int
  1939. vmxnet3_suspend(struct device *device)
  1940. {
  1941. struct pci_dev *pdev = to_pci_dev(device);
  1942. struct net_device *netdev = pci_get_drvdata(pdev);
  1943. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  1944. struct Vmxnet3_PMConf *pmConf;
  1945. struct ethhdr *ehdr;
  1946. struct arphdr *ahdr;
  1947. u8 *arpreq;
  1948. struct in_device *in_dev;
  1949. struct in_ifaddr *ifa;
  1950. int i = 0;
  1951. if (!netif_running(netdev))
  1952. return 0;
  1953. vmxnet3_disable_all_intrs(adapter);
  1954. vmxnet3_free_irqs(adapter);
  1955. vmxnet3_free_intr_resources(adapter);
  1956. netif_device_detach(netdev);
  1957. netif_stop_queue(netdev);
  1958. /* Create wake-up filters. */
  1959. pmConf = adapter->pm_conf;
  1960. memset(pmConf, 0, sizeof(*pmConf));
  1961. if (adapter->wol & WAKE_UCAST) {
  1962. pmConf->filters[i].patternSize = ETH_ALEN;
  1963. pmConf->filters[i].maskSize = 1;
  1964. memcpy(pmConf->filters[i].pattern, netdev->dev_addr, ETH_ALEN);
  1965. pmConf->filters[i].mask[0] = 0x3F; /* LSB ETH_ALEN bits */
  1966. pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
  1967. i++;
  1968. }
  1969. if (adapter->wol & WAKE_ARP) {
  1970. in_dev = in_dev_get(netdev);
  1971. if (!in_dev)
  1972. goto skip_arp;
  1973. ifa = (struct in_ifaddr *)in_dev->ifa_list;
  1974. if (!ifa)
  1975. goto skip_arp;
  1976. pmConf->filters[i].patternSize = ETH_HLEN + /* Ethernet header*/
  1977. sizeof(struct arphdr) + /* ARP header */
  1978. 2 * ETH_ALEN + /* 2 Ethernet addresses*/
  1979. 2 * sizeof(u32); /*2 IPv4 addresses */
  1980. pmConf->filters[i].maskSize =
  1981. (pmConf->filters[i].patternSize - 1) / 8 + 1;
  1982. /* ETH_P_ARP in Ethernet header. */
  1983. ehdr = (struct ethhdr *)pmConf->filters[i].pattern;
  1984. ehdr->h_proto = htons(ETH_P_ARP);
  1985. /* ARPOP_REQUEST in ARP header. */
  1986. ahdr = (struct arphdr *)&pmConf->filters[i].pattern[ETH_HLEN];
  1987. ahdr->ar_op = htons(ARPOP_REQUEST);
  1988. arpreq = (u8 *)(ahdr + 1);
  1989. /* The Unicast IPv4 address in 'tip' field. */
  1990. arpreq += 2 * ETH_ALEN + sizeof(u32);
  1991. *(u32 *)arpreq = ifa->ifa_address;
  1992. /* The mask for the relevant bits. */
  1993. pmConf->filters[i].mask[0] = 0x00;
  1994. pmConf->filters[i].mask[1] = 0x30; /* ETH_P_ARP */
  1995. pmConf->filters[i].mask[2] = 0x30; /* ARPOP_REQUEST */
  1996. pmConf->filters[i].mask[3] = 0x00;
  1997. pmConf->filters[i].mask[4] = 0xC0; /* IPv4 TIP */
  1998. pmConf->filters[i].mask[5] = 0x03; /* IPv4 TIP */
  1999. in_dev_put(in_dev);
  2000. pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_FILTER;
  2001. i++;
  2002. }
  2003. skip_arp:
  2004. if (adapter->wol & WAKE_MAGIC)
  2005. pmConf->wakeUpEvents |= VMXNET3_PM_WAKEUP_MAGIC;
  2006. pmConf->numFilters = i;
  2007. adapter->shared->devRead.pmConfDesc.confVer = 1;
  2008. adapter->shared->devRead.pmConfDesc.confLen = sizeof(*pmConf);
  2009. adapter->shared->devRead.pmConfDesc.confPA = virt_to_phys(pmConf);
  2010. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  2011. VMXNET3_CMD_UPDATE_PMCFG);
  2012. pci_save_state(pdev);
  2013. pci_enable_wake(pdev, pci_choose_state(pdev, PMSG_SUSPEND),
  2014. adapter->wol);
  2015. pci_disable_device(pdev);
  2016. pci_set_power_state(pdev, pci_choose_state(pdev, PMSG_SUSPEND));
  2017. return 0;
  2018. }
  2019. static int
  2020. vmxnet3_resume(struct device *device)
  2021. {
  2022. int err;
  2023. struct pci_dev *pdev = to_pci_dev(device);
  2024. struct net_device *netdev = pci_get_drvdata(pdev);
  2025. struct vmxnet3_adapter *adapter = netdev_priv(netdev);
  2026. struct Vmxnet3_PMConf *pmConf;
  2027. if (!netif_running(netdev))
  2028. return 0;
  2029. /* Destroy wake-up filters. */
  2030. pmConf = adapter->pm_conf;
  2031. memset(pmConf, 0, sizeof(*pmConf));
  2032. adapter->shared->devRead.pmConfDesc.confVer = 1;
  2033. adapter->shared->devRead.pmConfDesc.confLen = sizeof(*pmConf);
  2034. adapter->shared->devRead.pmConfDesc.confPA = virt_to_phys(pmConf);
  2035. netif_device_attach(netdev);
  2036. pci_set_power_state(pdev, PCI_D0);
  2037. pci_restore_state(pdev);
  2038. err = pci_enable_device_mem(pdev);
  2039. if (err != 0)
  2040. return err;
  2041. pci_enable_wake(pdev, PCI_D0, 0);
  2042. VMXNET3_WRITE_BAR1_REG(adapter, VMXNET3_REG_CMD,
  2043. VMXNET3_CMD_UPDATE_PMCFG);
  2044. vmxnet3_alloc_intr_resources(adapter);
  2045. vmxnet3_request_irqs(adapter);
  2046. vmxnet3_enable_all_intrs(adapter);
  2047. return 0;
  2048. }
  2049. static struct dev_pm_ops vmxnet3_pm_ops = {
  2050. .suspend = vmxnet3_suspend,
  2051. .resume = vmxnet3_resume,
  2052. };
  2053. #endif
  2054. static struct pci_driver vmxnet3_driver = {
  2055. .name = vmxnet3_driver_name,
  2056. .id_table = vmxnet3_pciid_table,
  2057. .probe = vmxnet3_probe_device,
  2058. .remove = __devexit_p(vmxnet3_remove_device),
  2059. #ifdef CONFIG_PM
  2060. .driver.pm = &vmxnet3_pm_ops,
  2061. #endif
  2062. };
  2063. static int __init
  2064. vmxnet3_init_module(void)
  2065. {
  2066. printk(KERN_INFO "%s - version %s\n", VMXNET3_DRIVER_DESC,
  2067. VMXNET3_DRIVER_VERSION_REPORT);
  2068. return pci_register_driver(&vmxnet3_driver);
  2069. }
  2070. module_init(vmxnet3_init_module);
  2071. static void
  2072. vmxnet3_exit_module(void)
  2073. {
  2074. pci_unregister_driver(&vmxnet3_driver);
  2075. }
  2076. module_exit(vmxnet3_exit_module);
  2077. MODULE_AUTHOR("VMware, Inc.");
  2078. MODULE_DESCRIPTION(VMXNET3_DRIVER_DESC);
  2079. MODULE_LICENSE("GPL v2");
  2080. MODULE_VERSION(VMXNET3_DRIVER_VERSION_STRING);