vmxnet3_drv.c 66 KB

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