adapter.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * This file is part of the Chelsio T4 PCI-E SR-IOV Virtual Function Ethernet
  3. * driver for Linux.
  4. *
  5. * Copyright (c) 2009-2010 Chelsio Communications, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. /*
  36. * This file should not be included directly. Include t4vf_common.h instead.
  37. */
  38. #ifndef __CXGB4VF_ADAPTER_H__
  39. #define __CXGB4VF_ADAPTER_H__
  40. #include <linux/etherdevice.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/pci.h>
  43. #include <linux/spinlock.h>
  44. #include <linux/skbuff.h>
  45. #include <linux/if_ether.h>
  46. #include <linux/netdevice.h>
  47. #include "../cxgb4/t4_hw.h"
  48. /*
  49. * Constants of the implementation.
  50. */
  51. enum {
  52. MAX_NPORTS = 1, /* max # of "ports" */
  53. MAX_PORT_QSETS = 8, /* max # of Queue Sets / "port" */
  54. MAX_ETH_QSETS = MAX_NPORTS*MAX_PORT_QSETS,
  55. /*
  56. * MSI-X interrupt index usage.
  57. */
  58. MSIX_FW = 0, /* MSI-X index for firmware Q */
  59. MSIX_IQFLINT = 1, /* MSI-X index base for Ingress Qs */
  60. MSIX_EXTRAS = 1,
  61. MSIX_ENTRIES = MAX_ETH_QSETS + MSIX_EXTRAS,
  62. /*
  63. * The maximum number of Ingress and Egress Queues is determined by
  64. * the maximum number of "Queue Sets" which we support plus any
  65. * ancillary queues. Each "Queue Set" requires one Ingress Queue
  66. * for RX Packet Ingress Event notifications and two Egress Queues for
  67. * a Free List and an Ethernet TX list.
  68. */
  69. INGQ_EXTRAS = 2, /* firmware event queue and */
  70. /* forwarded interrupts */
  71. MAX_INGQ = MAX_ETH_QSETS+INGQ_EXTRAS,
  72. MAX_EGRQ = MAX_ETH_QSETS*2,
  73. };
  74. /*
  75. * Forward structure definition references.
  76. */
  77. struct adapter;
  78. struct sge_eth_rxq;
  79. struct sge_rspq;
  80. /*
  81. * Per-"port" information. This is really per-Virtual Interface information
  82. * but the use of the "port" nomanclature makes it easier to go back and forth
  83. * between the PF and VF drivers ...
  84. */
  85. struct port_info {
  86. struct adapter *adapter; /* our adapter */
  87. u32 vlan_id; /* vlan id for VST */
  88. u16 viid; /* virtual interface ID */
  89. int xact_addr_filt; /* index of our MAC address filter */
  90. u16 rss_size; /* size of VI's RSS table slice */
  91. u8 pidx; /* index into adapter port[] */
  92. s8 mdio_addr;
  93. u8 port_type; /* firmware port type */
  94. u8 mod_type; /* firmware module type */
  95. u8 port_id; /* physical port ID */
  96. u8 nqsets; /* # of "Queue Sets" */
  97. u8 first_qset; /* index of first "Queue Set" */
  98. struct link_config link_cfg; /* physical port configuration */
  99. };
  100. /*
  101. * Scatter Gather Engine resources for the "adapter". Our ingress and egress
  102. * queues are organized into "Queue Sets" with one ingress and one egress
  103. * queue per Queue Set. These Queue Sets are aportionable between the "ports"
  104. * (Virtual Interfaces). One extra ingress queue is used to receive
  105. * asynchronous messages from the firmware. Note that the "Queue IDs" that we
  106. * use here are really "Relative Queue IDs" which are returned as part of the
  107. * firmware command to allocate queues. These queue IDs are relative to the
  108. * absolute Queue ID base of the section of the Queue ID space allocated to
  109. * the PF/VF.
  110. */
  111. /*
  112. * SGE free-list queue state.
  113. */
  114. struct rx_sw_desc;
  115. struct sge_fl {
  116. unsigned int avail; /* # of available RX buffers */
  117. unsigned int pend_cred; /* new buffers since last FL DB ring */
  118. unsigned int cidx; /* consumer index */
  119. unsigned int pidx; /* producer index */
  120. unsigned long alloc_failed; /* # of buffer allocation failures */
  121. unsigned long large_alloc_failed;
  122. unsigned long starving; /* # of times FL was found starving */
  123. /*
  124. * Write-once/infrequently fields.
  125. * -------------------------------
  126. */
  127. unsigned int cntxt_id; /* SGE relative QID for the free list */
  128. unsigned int abs_id; /* SGE absolute QID for the free list */
  129. unsigned int size; /* capacity of free list */
  130. struct rx_sw_desc *sdesc; /* address of SW RX descriptor ring */
  131. __be64 *desc; /* address of HW RX descriptor ring */
  132. dma_addr_t addr; /* PCI bus address of hardware ring */
  133. void __iomem *bar2_addr; /* address of BAR2 Queue registers */
  134. unsigned int bar2_qid; /* Queue ID for BAR2 Queue registers */
  135. };
  136. /*
  137. * An ingress packet gather list.
  138. */
  139. struct pkt_gl {
  140. struct page_frag frags[MAX_SKB_FRAGS];
  141. void *va; /* virtual address of first byte */
  142. unsigned int nfrags; /* # of fragments */
  143. unsigned int tot_len; /* total length of fragments */
  144. };
  145. typedef int (*rspq_handler_t)(struct sge_rspq *, const __be64 *,
  146. const struct pkt_gl *);
  147. /*
  148. * State for an SGE Response Queue.
  149. */
  150. struct sge_rspq {
  151. struct napi_struct napi; /* NAPI scheduling control */
  152. const __be64 *cur_desc; /* current descriptor in queue */
  153. unsigned int cidx; /* consumer index */
  154. u8 gen; /* current generation bit */
  155. u8 next_intr_params; /* holdoff params for next interrupt */
  156. int offset; /* offset into current FL buffer */
  157. unsigned int unhandled_irqs; /* bogus interrupts */
  158. /*
  159. * Write-once/infrequently fields.
  160. * -------------------------------
  161. */
  162. u8 intr_params; /* interrupt holdoff parameters */
  163. u8 pktcnt_idx; /* interrupt packet threshold */
  164. u8 idx; /* queue index within its group */
  165. u16 cntxt_id; /* SGE rel QID for the response Q */
  166. u16 abs_id; /* SGE abs QID for the response Q */
  167. __be64 *desc; /* address of hardware response ring */
  168. dma_addr_t phys_addr; /* PCI bus address of ring */
  169. void __iomem *bar2_addr; /* address of BAR2 Queue registers */
  170. unsigned int bar2_qid; /* Queue ID for BAR2 Queue registers */
  171. unsigned int iqe_len; /* entry size */
  172. unsigned int size; /* capcity of response Q */
  173. struct adapter *adapter; /* our adapter */
  174. struct net_device *netdev; /* associated net device */
  175. rspq_handler_t handler; /* the handler for this response Q */
  176. };
  177. /*
  178. * Ethernet queue statistics
  179. */
  180. struct sge_eth_stats {
  181. unsigned long pkts; /* # of ethernet packets */
  182. unsigned long lro_pkts; /* # of LRO super packets */
  183. unsigned long lro_merged; /* # of wire packets merged by LRO */
  184. unsigned long rx_cso; /* # of Rx checksum offloads */
  185. unsigned long vlan_ex; /* # of Rx VLAN extractions */
  186. unsigned long rx_drops; /* # of packets dropped due to no mem */
  187. };
  188. /*
  189. * State for an Ethernet Receive Queue.
  190. */
  191. struct sge_eth_rxq {
  192. struct sge_rspq rspq; /* Response Queue */
  193. struct sge_fl fl; /* Free List */
  194. struct sge_eth_stats stats; /* receive statistics */
  195. };
  196. /*
  197. * SGE Transmit Queue state. This contains all of the resources associated
  198. * with the hardware status of a TX Queue which is a circular ring of hardware
  199. * TX Descriptors. For convenience, it also contains a pointer to a parallel
  200. * "Software Descriptor" array but we don't know anything about it here other
  201. * than its type name.
  202. */
  203. struct tx_desc {
  204. /*
  205. * Egress Queues are measured in units of SGE_EQ_IDXSIZE by the
  206. * hardware: Sizes, Producer and Consumer indices, etc.
  207. */
  208. __be64 flit[SGE_EQ_IDXSIZE/sizeof(__be64)];
  209. };
  210. struct tx_sw_desc;
  211. struct sge_txq {
  212. unsigned int in_use; /* # of in-use TX descriptors */
  213. unsigned int size; /* # of descriptors */
  214. unsigned int cidx; /* SW consumer index */
  215. unsigned int pidx; /* producer index */
  216. unsigned long stops; /* # of times queue has been stopped */
  217. unsigned long restarts; /* # of queue restarts */
  218. /*
  219. * Write-once/infrequently fields.
  220. * -------------------------------
  221. */
  222. unsigned int cntxt_id; /* SGE relative QID for the TX Q */
  223. unsigned int abs_id; /* SGE absolute QID for the TX Q */
  224. struct tx_desc *desc; /* address of HW TX descriptor ring */
  225. struct tx_sw_desc *sdesc; /* address of SW TX descriptor ring */
  226. struct sge_qstat *stat; /* queue status entry */
  227. dma_addr_t phys_addr; /* PCI bus address of hardware ring */
  228. void __iomem *bar2_addr; /* address of BAR2 Queue registers */
  229. unsigned int bar2_qid; /* Queue ID for BAR2 Queue registers */
  230. };
  231. /*
  232. * State for an Ethernet Transmit Queue.
  233. */
  234. struct sge_eth_txq {
  235. struct sge_txq q; /* SGE TX Queue */
  236. struct netdev_queue *txq; /* associated netdev TX queue */
  237. unsigned long tso; /* # of TSO requests */
  238. unsigned long tx_cso; /* # of TX checksum offloads */
  239. unsigned long vlan_ins; /* # of TX VLAN insertions */
  240. unsigned long mapping_err; /* # of I/O MMU packet mapping errors */
  241. };
  242. /*
  243. * The complete set of Scatter/Gather Engine resources.
  244. */
  245. struct sge {
  246. /*
  247. * Our "Queue Sets" ...
  248. */
  249. struct sge_eth_txq ethtxq[MAX_ETH_QSETS];
  250. struct sge_eth_rxq ethrxq[MAX_ETH_QSETS];
  251. /*
  252. * Extra ingress queues for asynchronous firmware events and
  253. * forwarded interrupts (when in MSI mode).
  254. */
  255. struct sge_rspq fw_evtq ____cacheline_aligned_in_smp;
  256. struct sge_rspq intrq ____cacheline_aligned_in_smp;
  257. spinlock_t intrq_lock;
  258. /*
  259. * State for managing "starving Free Lists" -- Free Lists which have
  260. * fallen below a certain threshold of buffers available to the
  261. * hardware and attempts to refill them up to that threshold have
  262. * failed. We have a regular "slow tick" timer process which will
  263. * make periodic attempts to refill these starving Free Lists ...
  264. */
  265. DECLARE_BITMAP(starving_fl, MAX_EGRQ);
  266. struct timer_list rx_timer;
  267. /*
  268. * State for cleaning up completed TX descriptors.
  269. */
  270. struct timer_list tx_timer;
  271. /*
  272. * Write-once/infrequently fields.
  273. * -------------------------------
  274. */
  275. u16 max_ethqsets; /* # of available Ethernet queue sets */
  276. u16 ethqsets; /* # of active Ethernet queue sets */
  277. u16 ethtxq_rover; /* Tx queue to clean up next */
  278. u16 timer_val[SGE_NTIMERS]; /* interrupt holdoff timer array */
  279. u8 counter_val[SGE_NCOUNTERS]; /* interrupt RX threshold array */
  280. /* Decoded Adapter Parameters.
  281. */
  282. u32 fl_pg_order; /* large page allocation size */
  283. u32 stat_len; /* length of status page at ring end */
  284. u32 pktshift; /* padding between CPL & packet data */
  285. u32 fl_align; /* response queue message alignment */
  286. u32 fl_starve_thres; /* Free List starvation threshold */
  287. /*
  288. * Reverse maps from Absolute Queue IDs to associated queue pointers.
  289. * The absolute Queue IDs are in a compact range which start at a
  290. * [potentially large] Base Queue ID. We perform the reverse map by
  291. * first converting the Absolute Queue ID into a Relative Queue ID by
  292. * subtracting off the Base Queue ID and then use a Relative Queue ID
  293. * indexed table to get the pointer to the corresponding software
  294. * queue structure.
  295. */
  296. unsigned int egr_base;
  297. unsigned int ingr_base;
  298. void *egr_map[MAX_EGRQ];
  299. struct sge_rspq *ingr_map[MAX_INGQ];
  300. };
  301. /*
  302. * Utility macros to convert Absolute- to Relative-Queue indices and Egress-
  303. * and Ingress-Queues. The EQ_MAP() and IQ_MAP() macros which provide
  304. * pointers to Ingress- and Egress-Queues can be used as both L- and R-values
  305. */
  306. #define EQ_IDX(s, abs_id) ((unsigned int)((abs_id) - (s)->egr_base))
  307. #define IQ_IDX(s, abs_id) ((unsigned int)((abs_id) - (s)->ingr_base))
  308. #define EQ_MAP(s, abs_id) ((s)->egr_map[EQ_IDX(s, abs_id)])
  309. #define IQ_MAP(s, abs_id) ((s)->ingr_map[IQ_IDX(s, abs_id)])
  310. /*
  311. * Macro to iterate across Queue Sets ("rxq" is a historic misnomer).
  312. */
  313. #define for_each_ethrxq(sge, iter) \
  314. for (iter = 0; iter < (sge)->ethqsets; iter++)
  315. struct hash_mac_addr {
  316. struct list_head list;
  317. u8 addr[ETH_ALEN];
  318. unsigned int iface_mac;
  319. };
  320. struct mbox_list {
  321. struct list_head list;
  322. };
  323. /*
  324. * Per-"adapter" (Virtual Function) information.
  325. */
  326. struct adapter {
  327. /* PCI resources */
  328. void __iomem *regs;
  329. void __iomem *bar2;
  330. struct pci_dev *pdev;
  331. struct device *pdev_dev;
  332. /* "adapter" resources */
  333. unsigned long registered_device_map;
  334. unsigned long open_device_map;
  335. unsigned long flags;
  336. struct adapter_params params;
  337. /* queue and interrupt resources */
  338. struct {
  339. unsigned short vec;
  340. char desc[22];
  341. } msix_info[MSIX_ENTRIES];
  342. struct sge sge;
  343. /* Linux network device resources */
  344. struct net_device *port[MAX_NPORTS];
  345. const char *name;
  346. unsigned int msg_enable;
  347. /* debugfs resources */
  348. struct dentry *debugfs_root;
  349. /* various locks */
  350. spinlock_t stats_lock;
  351. /* lock for mailbox cmd list */
  352. spinlock_t mbox_lock;
  353. struct mbox_list mlist;
  354. /* support for mailbox command/reply logging */
  355. #define T4VF_OS_LOG_MBOX_CMDS 256
  356. struct mbox_cmd_log *mbox_log;
  357. /* list of MAC addresses in MPS Hash */
  358. struct list_head mac_hlist;
  359. };
  360. enum { /* adapter flags */
  361. CXGB4VF_FULL_INIT_DONE = (1UL << 0),
  362. CXGB4VF_USING_MSI = (1UL << 1),
  363. CXGB4VF_USING_MSIX = (1UL << 2),
  364. CXGB4VF_QUEUES_BOUND = (1UL << 3),
  365. CXGB4VF_ROOT_NO_RELAXED_ORDERING = (1UL << 4),
  366. CXGB4VF_FW_OK = (1UL << 5),
  367. };
  368. /*
  369. * The following register read/write routine definitions are required by
  370. * the common code.
  371. */
  372. /**
  373. * t4_read_reg - read a HW register
  374. * @adapter: the adapter
  375. * @reg_addr: the register address
  376. *
  377. * Returns the 32-bit value of the given HW register.
  378. */
  379. static inline u32 t4_read_reg(struct adapter *adapter, u32 reg_addr)
  380. {
  381. return readl(adapter->regs + reg_addr);
  382. }
  383. /**
  384. * t4_write_reg - write a HW register
  385. * @adapter: the adapter
  386. * @reg_addr: the register address
  387. * @val: the value to write
  388. *
  389. * Write a 32-bit value into the given HW register.
  390. */
  391. static inline void t4_write_reg(struct adapter *adapter, u32 reg_addr, u32 val)
  392. {
  393. writel(val, adapter->regs + reg_addr);
  394. }
  395. #ifndef readq
  396. static inline u64 readq(const volatile void __iomem *addr)
  397. {
  398. return readl(addr) + ((u64)readl(addr + 4) << 32);
  399. }
  400. static inline void writeq(u64 val, volatile void __iomem *addr)
  401. {
  402. writel(val, addr);
  403. writel(val >> 32, addr + 4);
  404. }
  405. #endif
  406. /**
  407. * t4_read_reg64 - read a 64-bit HW register
  408. * @adapter: the adapter
  409. * @reg_addr: the register address
  410. *
  411. * Returns the 64-bit value of the given HW register.
  412. */
  413. static inline u64 t4_read_reg64(struct adapter *adapter, u32 reg_addr)
  414. {
  415. return readq(adapter->regs + reg_addr);
  416. }
  417. /**
  418. * t4_write_reg64 - write a 64-bit HW register
  419. * @adapter: the adapter
  420. * @reg_addr: the register address
  421. * @val: the value to write
  422. *
  423. * Write a 64-bit value into the given HW register.
  424. */
  425. static inline void t4_write_reg64(struct adapter *adapter, u32 reg_addr,
  426. u64 val)
  427. {
  428. writeq(val, adapter->regs + reg_addr);
  429. }
  430. /**
  431. * port_name - return the string name of a port
  432. * @adapter: the adapter
  433. * @pidx: the port index
  434. *
  435. * Return the string name of the selected port.
  436. */
  437. static inline const char *port_name(struct adapter *adapter, int pidx)
  438. {
  439. return adapter->port[pidx]->name;
  440. }
  441. /**
  442. * t4_os_set_hw_addr - store a port's MAC address in SW
  443. * @adapter: the adapter
  444. * @pidx: the port index
  445. * @hw_addr: the Ethernet address
  446. *
  447. * Store the Ethernet address of the given port in SW. Called by the common
  448. * code when it retrieves a port's Ethernet address from EEPROM.
  449. */
  450. static inline void t4_os_set_hw_addr(struct adapter *adapter, int pidx,
  451. u8 hw_addr[])
  452. {
  453. eth_hw_addr_set(adapter->port[pidx], hw_addr);
  454. }
  455. /**
  456. * netdev2pinfo - return the port_info structure associated with a net_device
  457. * @dev: the netdev
  458. *
  459. * Return the struct port_info associated with a net_device
  460. */
  461. static inline struct port_info *netdev2pinfo(const struct net_device *dev)
  462. {
  463. return netdev_priv(dev);
  464. }
  465. /**
  466. * adap2pinfo - return the port_info of a port
  467. * @adap: the adapter
  468. * @pidx: the port index
  469. *
  470. * Return the port_info structure for the adapter.
  471. */
  472. static inline struct port_info *adap2pinfo(struct adapter *adapter, int pidx)
  473. {
  474. return netdev_priv(adapter->port[pidx]);
  475. }
  476. /**
  477. * netdev2adap - return the adapter structure associated with a net_device
  478. * @dev: the netdev
  479. *
  480. * Return the struct adapter associated with a net_device
  481. */
  482. static inline struct adapter *netdev2adap(const struct net_device *dev)
  483. {
  484. return netdev2pinfo(dev)->adapter;
  485. }
  486. /*
  487. * OS "Callback" function declarations. These are functions that the OS code
  488. * is "contracted" to provide for the common code.
  489. */
  490. void t4vf_os_link_changed(struct adapter *, int, int);
  491. void t4vf_os_portmod_changed(struct adapter *, int);
  492. /*
  493. * SGE function prototype declarations.
  494. */
  495. int t4vf_sge_alloc_rxq(struct adapter *, struct sge_rspq *, bool,
  496. struct net_device *, int,
  497. struct sge_fl *, rspq_handler_t);
  498. int t4vf_sge_alloc_eth_txq(struct adapter *, struct sge_eth_txq *,
  499. struct net_device *, struct netdev_queue *,
  500. unsigned int);
  501. void t4vf_free_sge_resources(struct adapter *);
  502. netdev_tx_t t4vf_eth_xmit(struct sk_buff *, struct net_device *);
  503. int t4vf_ethrx_handler(struct sge_rspq *, const __be64 *,
  504. const struct pkt_gl *);
  505. irq_handler_t t4vf_intr_handler(struct adapter *);
  506. irqreturn_t t4vf_sge_intr_msix(int, void *);
  507. int t4vf_sge_init(struct adapter *);
  508. void t4vf_sge_start(struct adapter *);
  509. void t4vf_sge_stop(struct adapter *);
  510. #endif /* __CXGB4VF_ADAPTER_H__ */