vmxnet3_int.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * Linux driver for VMware's vmxnet3 ethernet NIC.
  3. *
  4. * Copyright (C) 2008-2022, 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: [email protected]
  24. *
  25. */
  26. #ifndef _VMXNET3_INT_H
  27. #define _VMXNET3_INT_H
  28. #include <linux/bitops.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/delay.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/pci.h>
  33. #include <linux/compiler.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/ioport.h>
  37. #include <linux/highmem.h>
  38. #include <linux/timer.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/uaccess.h>
  43. #include <asm/dma.h>
  44. #include <asm/page.h>
  45. #include <linux/tcp.h>
  46. #include <linux/udp.h>
  47. #include <linux/ip.h>
  48. #include <linux/ipv6.h>
  49. #include <linux/in.h>
  50. #include <linux/etherdevice.h>
  51. #include <asm/checksum.h>
  52. #include <linux/if_vlan.h>
  53. #include <linux/if_arp.h>
  54. #include <linux/inetdevice.h>
  55. #include <linux/log2.h>
  56. #include "vmxnet3_defs.h"
  57. #ifdef DEBUG
  58. # define VMXNET3_DRIVER_VERSION_REPORT VMXNET3_DRIVER_VERSION_STRING"-NAPI(debug)"
  59. #else
  60. # define VMXNET3_DRIVER_VERSION_REPORT VMXNET3_DRIVER_VERSION_STRING"-NAPI"
  61. #endif
  62. /*
  63. * Version numbers
  64. */
  65. #define VMXNET3_DRIVER_VERSION_STRING "1.7.0.0-k"
  66. /* Each byte of this 32-bit integer encodes a version number in
  67. * VMXNET3_DRIVER_VERSION_STRING.
  68. */
  69. #define VMXNET3_DRIVER_VERSION_NUM 0x01070000
  70. #if defined(CONFIG_PCI_MSI)
  71. /* RSS only makes sense if MSI-X is supported. */
  72. #define VMXNET3_RSS
  73. #endif
  74. #define VMXNET3_REV_7 6 /* Vmxnet3 Rev. 7 */
  75. #define VMXNET3_REV_6 5 /* Vmxnet3 Rev. 6 */
  76. #define VMXNET3_REV_5 4 /* Vmxnet3 Rev. 5 */
  77. #define VMXNET3_REV_4 3 /* Vmxnet3 Rev. 4 */
  78. #define VMXNET3_REV_3 2 /* Vmxnet3 Rev. 3 */
  79. #define VMXNET3_REV_2 1 /* Vmxnet3 Rev. 2 */
  80. #define VMXNET3_REV_1 0 /* Vmxnet3 Rev. 1 */
  81. /*
  82. * Capabilities
  83. */
  84. enum {
  85. VMNET_CAP_SG = 0x0001, /* Can do scatter-gather transmits. */
  86. VMNET_CAP_IP4_CSUM = 0x0002, /* Can checksum only TCP/UDP over
  87. * IPv4 */
  88. VMNET_CAP_HW_CSUM = 0x0004, /* Can checksum all packets. */
  89. VMNET_CAP_HIGH_DMA = 0x0008, /* Can DMA to high memory. */
  90. VMNET_CAP_TOE = 0x0010, /* Supports TCP/IP offload. */
  91. VMNET_CAP_TSO = 0x0020, /* Supports TCP Segmentation
  92. * offload */
  93. VMNET_CAP_SW_TSO = 0x0040, /* Supports SW TCP Segmentation */
  94. VMNET_CAP_VMXNET_APROM = 0x0080, /* Vmxnet APROM support */
  95. VMNET_CAP_HW_TX_VLAN = 0x0100, /* Can we do VLAN tagging in HW */
  96. VMNET_CAP_HW_RX_VLAN = 0x0200, /* Can we do VLAN untagging in HW */
  97. VMNET_CAP_SW_VLAN = 0x0400, /* VLAN tagging/untagging in SW */
  98. VMNET_CAP_WAKE_PCKT_RCV = 0x0800, /* Can wake on network packet recv? */
  99. VMNET_CAP_ENABLE_INT_INLINE = 0x1000, /* Enable Interrupt Inline */
  100. VMNET_CAP_ENABLE_HEADER_COPY = 0x2000, /* copy header for vmkernel */
  101. VMNET_CAP_TX_CHAIN = 0x4000, /* Guest can use multiple tx entries
  102. * for a pkt */
  103. VMNET_CAP_RX_CHAIN = 0x8000, /* pkt can span multiple rx entries */
  104. VMNET_CAP_LPD = 0x10000, /* large pkt delivery */
  105. VMNET_CAP_BPF = 0x20000, /* BPF Support in VMXNET Virtual HW*/
  106. VMNET_CAP_SG_SPAN_PAGES = 0x40000, /* Scatter-gather can span multiple*/
  107. /* pages transmits */
  108. VMNET_CAP_IP6_CSUM = 0x80000, /* Can do IPv6 csum offload. */
  109. VMNET_CAP_TSO6 = 0x100000, /* TSO seg. offload for IPv6 pkts. */
  110. VMNET_CAP_TSO256k = 0x200000, /* Can do TSO seg offload for */
  111. /* pkts up to 256kB. */
  112. VMNET_CAP_UPT = 0x400000 /* Support UPT */
  113. };
  114. /*
  115. * Maximum devices supported.
  116. */
  117. #define MAX_ETHERNET_CARDS 10
  118. #define MAX_PCI_PASSTHRU_DEVICE 6
  119. struct vmxnet3_cmd_ring {
  120. union Vmxnet3_GenericDesc *base;
  121. u32 size;
  122. u32 next2fill;
  123. u32 next2comp;
  124. u8 gen;
  125. u8 isOutOfOrder;
  126. dma_addr_t basePA;
  127. };
  128. static inline void
  129. vmxnet3_cmd_ring_adv_next2fill(struct vmxnet3_cmd_ring *ring)
  130. {
  131. ring->next2fill++;
  132. if (unlikely(ring->next2fill == ring->size)) {
  133. ring->next2fill = 0;
  134. VMXNET3_FLIP_RING_GEN(ring->gen);
  135. }
  136. }
  137. static inline void
  138. vmxnet3_cmd_ring_adv_next2comp(struct vmxnet3_cmd_ring *ring)
  139. {
  140. VMXNET3_INC_RING_IDX_ONLY(ring->next2comp, ring->size);
  141. }
  142. static inline int
  143. vmxnet3_cmd_ring_desc_avail(struct vmxnet3_cmd_ring *ring)
  144. {
  145. return (ring->next2comp > ring->next2fill ? 0 : ring->size) +
  146. ring->next2comp - ring->next2fill - 1;
  147. }
  148. struct vmxnet3_comp_ring {
  149. union Vmxnet3_GenericDesc *base;
  150. u32 size;
  151. u32 next2proc;
  152. u8 gen;
  153. u8 intr_idx;
  154. dma_addr_t basePA;
  155. };
  156. static inline void
  157. vmxnet3_comp_ring_adv_next2proc(struct vmxnet3_comp_ring *ring)
  158. {
  159. ring->next2proc++;
  160. if (unlikely(ring->next2proc == ring->size)) {
  161. ring->next2proc = 0;
  162. VMXNET3_FLIP_RING_GEN(ring->gen);
  163. }
  164. }
  165. struct vmxnet3_tx_data_ring {
  166. struct Vmxnet3_TxDataDesc *base;
  167. u32 size;
  168. dma_addr_t basePA;
  169. };
  170. enum vmxnet3_buf_map_type {
  171. VMXNET3_MAP_INVALID = 0,
  172. VMXNET3_MAP_NONE,
  173. VMXNET3_MAP_SINGLE,
  174. VMXNET3_MAP_PAGE,
  175. };
  176. struct vmxnet3_tx_buf_info {
  177. u32 map_type;
  178. u16 len;
  179. u16 sop_idx;
  180. dma_addr_t dma_addr;
  181. struct sk_buff *skb;
  182. };
  183. struct vmxnet3_tq_driver_stats {
  184. u64 drop_total; /* # of pkts dropped by the driver, the
  185. * counters below track droppings due to
  186. * different reasons
  187. */
  188. u64 drop_too_many_frags;
  189. u64 drop_oversized_hdr;
  190. u64 drop_hdr_inspect_err;
  191. u64 drop_tso;
  192. u64 tx_ring_full;
  193. u64 linearized; /* # of pkts linearized */
  194. u64 copy_skb_header; /* # of times we have to copy skb header */
  195. u64 oversized_hdr;
  196. };
  197. struct vmxnet3_tx_ctx {
  198. bool ipv4;
  199. bool ipv6;
  200. u16 mss;
  201. u32 l4_offset; /* only valid for pkts requesting tso or csum
  202. * offloading. For encap offload, it refers to
  203. * inner L4 offset i.e. it includes outer header
  204. * encap header and inner eth and ip header size
  205. */
  206. u32 l4_hdr_size; /* only valid if mss != 0
  207. * Refers to inner L4 hdr size for encap
  208. * offload
  209. */
  210. u32 copy_size; /* # of bytes copied into the data ring */
  211. union Vmxnet3_GenericDesc *sop_txd;
  212. union Vmxnet3_GenericDesc *eop_txd;
  213. };
  214. struct vmxnet3_tx_queue {
  215. char name[IFNAMSIZ+8]; /* To identify interrupt */
  216. struct vmxnet3_adapter *adapter;
  217. spinlock_t tx_lock;
  218. struct vmxnet3_cmd_ring tx_ring;
  219. struct vmxnet3_tx_buf_info *buf_info;
  220. struct vmxnet3_tx_data_ring data_ring;
  221. struct vmxnet3_comp_ring comp_ring;
  222. struct Vmxnet3_TxQueueCtrl *shared;
  223. struct vmxnet3_tq_driver_stats stats;
  224. bool stopped;
  225. int num_stop; /* # of times the queue is
  226. * stopped */
  227. int qid;
  228. u16 txdata_desc_size;
  229. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  230. enum vmxnet3_rx_buf_type {
  231. VMXNET3_RX_BUF_NONE = 0,
  232. VMXNET3_RX_BUF_SKB = 1,
  233. VMXNET3_RX_BUF_PAGE = 2
  234. };
  235. #define VMXNET3_RXD_COMP_PENDING 0
  236. #define VMXNET3_RXD_COMP_DONE 1
  237. struct vmxnet3_rx_buf_info {
  238. enum vmxnet3_rx_buf_type buf_type;
  239. u16 len;
  240. u8 comp_state;
  241. union {
  242. struct sk_buff *skb;
  243. struct page *page;
  244. };
  245. dma_addr_t dma_addr;
  246. };
  247. struct vmxnet3_rx_ctx {
  248. struct sk_buff *skb;
  249. u32 sop_idx;
  250. };
  251. struct vmxnet3_rq_driver_stats {
  252. u64 drop_total;
  253. u64 drop_err;
  254. u64 drop_fcs;
  255. u64 rx_buf_alloc_failure;
  256. };
  257. struct vmxnet3_rx_data_ring {
  258. Vmxnet3_RxDataDesc *base;
  259. dma_addr_t basePA;
  260. u16 desc_size;
  261. };
  262. struct vmxnet3_rx_queue {
  263. char name[IFNAMSIZ + 8]; /* To identify interrupt */
  264. struct vmxnet3_adapter *adapter;
  265. struct napi_struct napi;
  266. struct vmxnet3_cmd_ring rx_ring[2];
  267. struct vmxnet3_rx_data_ring data_ring;
  268. struct vmxnet3_comp_ring comp_ring;
  269. struct vmxnet3_rx_ctx rx_ctx;
  270. u32 qid; /* rqID in RCD for buffer from 1st ring */
  271. u32 qid2; /* rqID in RCD for buffer from 2nd ring */
  272. u32 dataRingQid; /* rqID in RCD for buffer from data ring */
  273. struct vmxnet3_rx_buf_info *buf_info[2];
  274. struct Vmxnet3_RxQueueCtrl *shared;
  275. struct vmxnet3_rq_driver_stats stats;
  276. } __attribute__((__aligned__(SMP_CACHE_BYTES)));
  277. #define VMXNET3_DEVICE_MAX_TX_QUEUES 32
  278. #define VMXNET3_DEVICE_MAX_RX_QUEUES 32 /* Keep this value as a power of 2 */
  279. #define VMXNET3_DEVICE_DEFAULT_TX_QUEUES 8
  280. #define VMXNET3_DEVICE_DEFAULT_RX_QUEUES 8 /* Keep this value as a power of 2 */
  281. /* Should be less than UPT1_RSS_MAX_IND_TABLE_SIZE */
  282. #define VMXNET3_RSS_IND_TABLE_SIZE (VMXNET3_DEVICE_MAX_RX_QUEUES * 4)
  283. #define VMXNET3_LINUX_MAX_MSIX_VECT (VMXNET3_DEVICE_MAX_TX_QUEUES + \
  284. VMXNET3_DEVICE_MAX_RX_QUEUES + 1)
  285. #define VMXNET3_LINUX_MIN_MSIX_VECT 3 /* 1 for tx, 1 for rx pair and 1 for event */
  286. struct vmxnet3_intr {
  287. enum vmxnet3_intr_mask_mode mask_mode;
  288. enum vmxnet3_intr_type type; /* MSI-X, MSI, or INTx? */
  289. u8 num_intrs; /* # of intr vectors */
  290. u8 event_intr_idx; /* idx of the intr vector for event */
  291. u8 mod_levels[VMXNET3_LINUX_MAX_MSIX_VECT]; /* moderation level */
  292. char event_msi_vector_name[IFNAMSIZ+17];
  293. #ifdef CONFIG_PCI_MSI
  294. struct msix_entry msix_entries[VMXNET3_LINUX_MAX_MSIX_VECT];
  295. #endif
  296. };
  297. /* Interrupt sharing schemes, share_intr */
  298. #define VMXNET3_INTR_BUDDYSHARE 0 /* Corresponding tx,rx queues share irq */
  299. #define VMXNET3_INTR_TXSHARE 1 /* All tx queues share one irq */
  300. #define VMXNET3_INTR_DONTSHARE 2 /* each queue has its own irq */
  301. #define VMXNET3_STATE_BIT_RESETTING 0
  302. #define VMXNET3_STATE_BIT_QUIESCED 1
  303. struct vmxnet3_adapter {
  304. struct vmxnet3_tx_queue tx_queue[VMXNET3_DEVICE_MAX_TX_QUEUES];
  305. struct vmxnet3_rx_queue rx_queue[VMXNET3_DEVICE_MAX_RX_QUEUES];
  306. unsigned long active_vlans[BITS_TO_LONGS(VLAN_N_VID)];
  307. struct vmxnet3_intr intr;
  308. spinlock_t cmd_lock;
  309. struct Vmxnet3_DriverShared *shared;
  310. struct Vmxnet3_PMConf *pm_conf;
  311. struct Vmxnet3_TxQueueDesc *tqd_start; /* all tx queue desc */
  312. struct Vmxnet3_RxQueueDesc *rqd_start; /* all rx queue desc */
  313. struct net_device *netdev;
  314. struct pci_dev *pdev;
  315. u8 __iomem *hw_addr0; /* for BAR 0 */
  316. u8 __iomem *hw_addr1; /* for BAR 1 */
  317. u8 version;
  318. #ifdef VMXNET3_RSS
  319. struct UPT1_RSSConf *rss_conf;
  320. bool rss;
  321. #endif
  322. u32 num_rx_queues;
  323. u32 num_tx_queues;
  324. /* rx buffer related */
  325. unsigned skb_buf_size;
  326. int rx_buf_per_pkt; /* only apply to the 1st ring */
  327. dma_addr_t shared_pa;
  328. dma_addr_t queue_desc_pa;
  329. dma_addr_t coal_conf_pa;
  330. /* Wake-on-LAN */
  331. u32 wol;
  332. /* Link speed */
  333. u32 link_speed; /* in mbps */
  334. u64 tx_timeout_count;
  335. /* Ring sizes */
  336. u32 tx_ring_size;
  337. u32 rx_ring_size;
  338. u32 rx_ring2_size;
  339. /* Size of buffer in the data ring */
  340. u16 txdata_desc_size;
  341. u16 rxdata_desc_size;
  342. bool rxdataring_enabled;
  343. bool default_rss_fields;
  344. enum Vmxnet3_RSSField rss_fields;
  345. struct work_struct work;
  346. unsigned long state; /* VMXNET3_STATE_BIT_xxx */
  347. int share_intr;
  348. struct Vmxnet3_CoalesceScheme *coal_conf;
  349. bool default_coal_mode;
  350. dma_addr_t adapter_pa;
  351. dma_addr_t pm_conf_pa;
  352. dma_addr_t rss_conf_pa;
  353. bool queuesExtEnabled;
  354. struct Vmxnet3_RingBufferSize ringBufSize;
  355. u32 devcap_supported[8];
  356. u32 ptcap_supported[8];
  357. u32 dev_caps[8];
  358. u16 tx_prod_offset;
  359. u16 rx_prod_offset;
  360. u16 rx_prod2_offset;
  361. };
  362. #define VMXNET3_WRITE_BAR0_REG(adapter, reg, val) \
  363. writel((val), (adapter)->hw_addr0 + (reg))
  364. #define VMXNET3_READ_BAR0_REG(adapter, reg) \
  365. readl((adapter)->hw_addr0 + (reg))
  366. #define VMXNET3_WRITE_BAR1_REG(adapter, reg, val) \
  367. writel((val), (adapter)->hw_addr1 + (reg))
  368. #define VMXNET3_READ_BAR1_REG(adapter, reg) \
  369. readl((adapter)->hw_addr1 + (reg))
  370. #define VMXNET3_WAKE_QUEUE_THRESHOLD(tq) (5)
  371. #define VMXNET3_RX_ALLOC_THRESHOLD(rq, ring_idx, adapter) \
  372. ((rq)->rx_ring[ring_idx].size >> 3)
  373. #define VMXNET3_GET_ADDR_LO(dma) ((u32)(dma))
  374. #define VMXNET3_GET_ADDR_HI(dma) ((u32)(((u64)(dma)) >> 32))
  375. #define VMXNET3_VERSION_GE_2(adapter) \
  376. (adapter->version >= VMXNET3_REV_2 + 1)
  377. #define VMXNET3_VERSION_GE_3(adapter) \
  378. (adapter->version >= VMXNET3_REV_3 + 1)
  379. #define VMXNET3_VERSION_GE_4(adapter) \
  380. (adapter->version >= VMXNET3_REV_4 + 1)
  381. #define VMXNET3_VERSION_GE_5(adapter) \
  382. (adapter->version >= VMXNET3_REV_5 + 1)
  383. #define VMXNET3_VERSION_GE_6(adapter) \
  384. (adapter->version >= VMXNET3_REV_6 + 1)
  385. #define VMXNET3_VERSION_GE_7(adapter) \
  386. (adapter->version >= VMXNET3_REV_7 + 1)
  387. /* must be a multiple of VMXNET3_RING_SIZE_ALIGN */
  388. #define VMXNET3_DEF_TX_RING_SIZE 512
  389. #define VMXNET3_DEF_RX_RING_SIZE 1024
  390. #define VMXNET3_DEF_RX_RING2_SIZE 512
  391. #define VMXNET3_DEF_RXDATA_DESC_SIZE 128
  392. #define VMXNET3_MAX_ETH_HDR_SIZE 22
  393. #define VMXNET3_MAX_SKB_BUF_SIZE (3*1024)
  394. #define VMXNET3_GET_RING_IDX(adapter, rqID) \
  395. ((rqID >= adapter->num_rx_queues && \
  396. rqID < 2 * adapter->num_rx_queues) ? 1 : 0) \
  397. #define VMXNET3_RX_DATA_RING(adapter, rqID) \
  398. (rqID >= 2 * adapter->num_rx_queues && \
  399. rqID < 3 * adapter->num_rx_queues) \
  400. #define VMXNET3_COAL_STATIC_DEFAULT_DEPTH 64
  401. #define VMXNET3_COAL_RBC_RATE(usecs) (1000000 / usecs)
  402. #define VMXNET3_COAL_RBC_USECS(rbc_rate) (1000000 / rbc_rate)
  403. #define VMXNET3_RSS_FIELDS_DEFAULT (VMXNET3_RSS_FIELDS_TCPIP4 | \
  404. VMXNET3_RSS_FIELDS_TCPIP6)
  405. int
  406. vmxnet3_quiesce_dev(struct vmxnet3_adapter *adapter);
  407. int
  408. vmxnet3_activate_dev(struct vmxnet3_adapter *adapter);
  409. void
  410. vmxnet3_force_close(struct vmxnet3_adapter *adapter);
  411. void
  412. vmxnet3_reset_dev(struct vmxnet3_adapter *adapter);
  413. void
  414. vmxnet3_tq_destroy_all(struct vmxnet3_adapter *adapter);
  415. void
  416. vmxnet3_rq_destroy_all(struct vmxnet3_adapter *adapter);
  417. netdev_features_t
  418. vmxnet3_fix_features(struct net_device *netdev, netdev_features_t features);
  419. netdev_features_t
  420. vmxnet3_features_check(struct sk_buff *skb,
  421. struct net_device *netdev, netdev_features_t features);
  422. int
  423. vmxnet3_set_features(struct net_device *netdev, netdev_features_t features);
  424. int
  425. vmxnet3_create_queues(struct vmxnet3_adapter *adapter,
  426. u32 tx_ring_size, u32 rx_ring_size, u32 rx_ring2_size,
  427. u16 txdata_desc_size, u16 rxdata_desc_size);
  428. void vmxnet3_set_ethtool_ops(struct net_device *netdev);
  429. void vmxnet3_get_stats64(struct net_device *dev,
  430. struct rtnl_link_stats64 *stats);
  431. bool vmxnet3_check_ptcapability(u32 cap_supported, u32 cap);
  432. extern char vmxnet3_driver_name[];
  433. #endif