iflib.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*-
  2. * Copyright (c) 2014-2017, Matthew Macy (mmacy@mattmacy.io)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Neither the name of Matthew Macy nor the names of its
  12. * contributors may be used to endorse or promote products derived from
  13. * this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef __IFLIB_H_
  28. #define __IFLIB_H_
  29. #include <sys/kobj.h>
  30. #include <sys/bus.h>
  31. #include <sys/cpuset.h>
  32. #include <machine/bus.h>
  33. #include <sys/nv.h>
  34. #include <sys/gtaskqueue.h>
  35. /*
  36. * The value type for indexing, limits max descriptors
  37. * to 65535 can be conditionally redefined to uint32_t
  38. * in the future if the need arises.
  39. */
  40. typedef uint16_t qidx_t;
  41. #define QIDX_INVALID 0xFFFF
  42. struct iflib_ctx;
  43. typedef struct iflib_ctx *if_ctx_t;
  44. struct if_shared_ctx;
  45. typedef const struct if_shared_ctx *if_shared_ctx_t;
  46. struct if_int_delay_info;
  47. typedef struct if_int_delay_info *if_int_delay_info_t;
  48. /*
  49. * File organization:
  50. * - public structures
  51. * - iflib accessors
  52. * - iflib utility functions
  53. * - iflib core functions
  54. */
  55. typedef struct if_rxd_frag {
  56. uint8_t irf_flid;
  57. qidx_t irf_idx;
  58. uint16_t irf_len;
  59. } *if_rxd_frag_t;
  60. /* bnxt supports 64 with hardware LRO enabled */
  61. #define IFLIB_MAX_RX_SEGS 64
  62. typedef struct if_rxd_info {
  63. /* set by iflib */
  64. uint16_t iri_qsidx; /* qset index */
  65. uint16_t iri_vtag; /* vlan tag - if flag set */
  66. /* XXX redundant with the new irf_len field */
  67. uint16_t iri_len; /* packet length */
  68. qidx_t iri_cidx; /* consumer index of cq */
  69. if_t iri_ifp; /* driver may have >1 iface per softc */
  70. /* updated by driver */
  71. if_rxd_frag_t iri_frags;
  72. uint32_t iri_flowid; /* RSS hash for packet */
  73. uint32_t iri_csum_flags; /* m_pkthdr csum flags */
  74. uint32_t iri_csum_data; /* m_pkthdr csum data */
  75. uint8_t iri_flags; /* mbuf flags for packet */
  76. uint8_t iri_nfrags; /* number of fragments in packet */
  77. uint8_t iri_rsstype; /* RSS hash type */
  78. uint8_t iri_pad; /* any padding in the received data */
  79. } *if_rxd_info_t;
  80. typedef struct if_rxd_update {
  81. uint64_t *iru_paddrs;
  82. qidx_t *iru_idxs;
  83. qidx_t iru_pidx;
  84. uint16_t iru_qsidx;
  85. uint16_t iru_count;
  86. uint16_t iru_buf_size;
  87. uint8_t iru_flidx;
  88. } *if_rxd_update_t;
  89. #define IPI_TX_INTR 0x1 /* send an interrupt when this packet is sent */
  90. #define IPI_TX_IPV4 0x2 /* ethertype IPv4 */
  91. #define IPI_TX_IPV6 0x4 /* ethertype IPv6 */
  92. typedef struct if_pkt_info {
  93. bus_dma_segment_t *ipi_segs; /* physical addresses */
  94. uint32_t ipi_len; /* packet length */
  95. uint16_t ipi_qsidx; /* queue set index */
  96. qidx_t ipi_nsegs; /* number of segments */
  97. qidx_t ipi_ndescs; /* number of descriptors used by encap */
  98. uint16_t ipi_flags; /* iflib per-packet flags */
  99. qidx_t ipi_pidx; /* start pidx for encap */
  100. qidx_t ipi_new_pidx; /* next available pidx post-encap */
  101. /* offload handling */
  102. uint8_t ipi_ehdrlen; /* ether header length */
  103. uint8_t ipi_ip_hlen; /* ip header length */
  104. uint8_t ipi_tcp_hlen; /* tcp header length */
  105. uint8_t ipi_ipproto; /* ip protocol */
  106. uint32_t ipi_csum_flags; /* packet checksum flags */
  107. uint16_t ipi_tso_segsz; /* tso segment size */
  108. uint16_t ipi_vtag; /* VLAN tag */
  109. uint16_t ipi_etype; /* ether header type */
  110. uint8_t ipi_tcp_hflags; /* tcp header flags */
  111. uint8_t ipi_mflags; /* packet mbuf flags */
  112. uint32_t ipi_tcp_seq; /* tcp seqno */
  113. uint8_t ipi_ip_tos; /* IP ToS field data */
  114. uint8_t __spare0__;
  115. uint16_t __spare1__;
  116. } *if_pkt_info_t;
  117. typedef struct if_irq {
  118. struct resource *ii_res;
  119. int __spare0__;
  120. void *ii_tag;
  121. } *if_irq_t;
  122. struct if_int_delay_info {
  123. if_ctx_t iidi_ctx; /* Back-pointer to the iflib ctx (softc) */
  124. int iidi_offset; /* Register offset to read/write */
  125. int iidi_value; /* Current value in usecs */
  126. struct sysctl_oid *iidi_oidp;
  127. struct sysctl_req *iidi_req;
  128. };
  129. typedef enum {
  130. IFLIB_INTR_LEGACY,
  131. IFLIB_INTR_MSI,
  132. IFLIB_INTR_MSIX
  133. } iflib_intr_mode_t;
  134. /*
  135. * This really belongs in pciio.h or some place more general
  136. * but this is the only consumer for now.
  137. */
  138. typedef struct pci_vendor_info {
  139. uint32_t pvi_vendor_id;
  140. uint32_t pvi_device_id;
  141. uint32_t pvi_subvendor_id;
  142. uint32_t pvi_subdevice_id;
  143. uint32_t pvi_rev_id;
  144. uint32_t pvi_class_mask;
  145. const char *pvi_name;
  146. } pci_vendor_info_t;
  147. #define PVID(vendor, devid, name) {vendor, devid, 0, 0, 0, 0, name}
  148. #define PVID_OEM(vendor, devid, svid, sdevid, revid, name) {vendor, devid, svid, sdevid, revid, 0, name}
  149. #define PVID_END {0, 0, 0, 0, 0, 0, NULL}
  150. /* No drivers in tree currently match on anything except vendor:device. */
  151. #define IFLIB_PNP_DESCR "U32:vendor;U32:device;U32:#;U32:#;" \
  152. "U32:#;U32:#;D:#"
  153. #define IFLIB_PNP_INFO(b, u, t) \
  154. MODULE_PNP_INFO(IFLIB_PNP_DESCR, b, u, t, nitems(t) - 1)
  155. typedef struct if_txrx {
  156. int (*ift_txd_encap) (void *, if_pkt_info_t);
  157. void (*ift_txd_flush) (void *, uint16_t, qidx_t pidx);
  158. int (*ift_txd_credits_update) (void *, uint16_t qsidx, bool clear);
  159. int (*ift_rxd_available) (void *, uint16_t qsidx, qidx_t pidx, qidx_t budget);
  160. int (*ift_rxd_pkt_get) (void *, if_rxd_info_t ri);
  161. void (*ift_rxd_refill) (void * , if_rxd_update_t iru);
  162. void (*ift_rxd_flush) (void *, uint16_t qsidx, uint8_t flidx, qidx_t pidx);
  163. int (*ift_legacy_intr) (void *);
  164. qidx_t (*ift_txq_select) (void *, struct mbuf *);
  165. qidx_t (*ift_txq_select_v2) (void *, struct mbuf *, if_pkt_info_t);
  166. } *if_txrx_t;
  167. typedef struct if_softc_ctx {
  168. int isc_vectors;
  169. int isc_nrxqsets;
  170. int isc_ntxqsets;
  171. uint16_t __spare0__;
  172. uint32_t __spare1__;
  173. int isc_msix_bar; /* can be model specific - initialize in attach_pre */
  174. int isc_tx_nsegments; /* can be model specific - initialize in attach_pre */
  175. int isc_ntxd[8];
  176. int isc_nrxd[8];
  177. uint32_t isc_txqsizes[8];
  178. uint32_t isc_rxqsizes[8];
  179. /* is there such thing as a descriptor that is more than 248 bytes ? */
  180. uint8_t isc_txd_size[8];
  181. uint8_t isc_rxd_size[8];
  182. int isc_tx_tso_segments_max;
  183. int isc_tx_tso_size_max;
  184. int isc_tx_tso_segsize_max;
  185. int isc_tx_csum_flags;
  186. int isc_capabilities;
  187. int isc_capenable;
  188. int isc_rss_table_size;
  189. int isc_rss_table_mask;
  190. int isc_nrxqsets_max;
  191. int isc_ntxqsets_max;
  192. uint32_t __spare2__;
  193. iflib_intr_mode_t isc_intr;
  194. uint16_t isc_rxd_buf_size[8]; /* set at init time by driver, 0
  195. means use iflib-calculated size
  196. based on isc_max_frame_size */
  197. uint16_t isc_max_frame_size; /* set at init time by driver */
  198. uint16_t isc_min_frame_size; /* set at init time by driver, only used if
  199. IFLIB_NEED_ETHER_PAD is set. */
  200. uint32_t isc_pause_frames; /* set by driver for iflib_timer to detect */
  201. uint32_t __spare3__;
  202. uint32_t __spare4__;
  203. uint32_t __spare5__;
  204. uint32_t __spare6__;
  205. uint32_t __spare7__;
  206. uint32_t __spare8__;
  207. caddr_t __spare9__;
  208. int isc_disable_msix;
  209. if_txrx_t isc_txrx;
  210. struct ifmedia *isc_media;
  211. bus_size_t isc_dma_width; /* device dma width in bits, 0 means
  212. use BUS_SPACE_MAXADDR instead */
  213. } *if_softc_ctx_t;
  214. /*
  215. * Initialization values for device
  216. */
  217. struct if_shared_ctx {
  218. unsigned isc_magic;
  219. driver_t *isc_driver;
  220. bus_size_t isc_q_align;
  221. bus_size_t isc_tx_maxsize;
  222. bus_size_t isc_tx_maxsegsize;
  223. bus_size_t isc_tso_maxsize;
  224. bus_size_t isc_tso_maxsegsize;
  225. bus_size_t isc_rx_maxsize;
  226. bus_size_t isc_rx_maxsegsize;
  227. int isc_rx_nsegments;
  228. int isc_admin_intrcnt; /* # of admin/link interrupts */
  229. /* fields necessary for probe */
  230. const pci_vendor_info_t *isc_vendor_info;
  231. const char *isc_driver_version;
  232. /* optional function to transform the read values to match the table*/
  233. void (*isc_parse_devinfo) (uint16_t *device_id, uint16_t *subvendor_id,
  234. uint16_t *subdevice_id, uint16_t *rev_id);
  235. int isc_nrxd_min[8];
  236. int isc_nrxd_default[8];
  237. int isc_nrxd_max[8];
  238. int isc_ntxd_min[8];
  239. int isc_ntxd_default[8];
  240. int isc_ntxd_max[8];
  241. /* actively used during operation */
  242. int isc_nfl __aligned(CACHE_LINE_SIZE);
  243. int isc_ntxqs; /* # of tx queues per tx qset - usually 1 */
  244. int isc_nrxqs; /* # of rx queues per rx qset - intel 1, chelsio 2, broadcom 3 */
  245. int __spare0__;
  246. int isc_tx_reclaim_thresh;
  247. int isc_flags;
  248. };
  249. typedef struct iflib_dma_info {
  250. bus_addr_t idi_paddr;
  251. caddr_t idi_vaddr;
  252. bus_dma_tag_t idi_tag;
  253. bus_dmamap_t idi_map;
  254. uint32_t idi_size;
  255. } *iflib_dma_info_t;
  256. #define IFLIB_MAGIC 0xCAFEF00D
  257. typedef enum {
  258. /* Interrupt or softirq handles only receive */
  259. IFLIB_INTR_RX,
  260. /* Interrupt or softirq handles only transmit */
  261. IFLIB_INTR_TX,
  262. /*
  263. * Interrupt will check for both pending receive
  264. * and available tx credits and dispatch a task
  265. * for one or both depending on the disposition
  266. * of the respective queues.
  267. */
  268. IFLIB_INTR_RXTX,
  269. /*
  270. * Other interrupt - typically link status and
  271. * or error conditions.
  272. */
  273. IFLIB_INTR_ADMIN,
  274. /* Softirq (task) for iov handling */
  275. IFLIB_INTR_IOV,
  276. } iflib_intr_type_t;
  277. /*
  278. * Interface has a separate completion queue for RX
  279. */
  280. #define IFLIB_HAS_RXCQ 0x01
  281. /*
  282. * Driver has already allocated vectors
  283. */
  284. #define IFLIB_SKIP_MSIX 0x02
  285. /*
  286. * Interface is a virtual function
  287. */
  288. #define IFLIB_IS_VF 0x04
  289. /*
  290. * Interface has a separate completion queue for TX
  291. */
  292. #define IFLIB_HAS_TXCQ 0x08
  293. /*
  294. * Interface does checksum in place
  295. */
  296. #define IFLIB_NEED_SCRATCH 0x10
  297. /*
  298. * Interface doesn't expect in_pseudo for th_sum
  299. */
  300. #define IFLIB_TSO_INIT_IP 0x20
  301. /*
  302. * Interface doesn't align IP header
  303. */
  304. #define IFLIB_DO_RX_FIXUP 0x40
  305. /*
  306. * Driver needs csum zeroed for offloading
  307. */
  308. #define IFLIB_NEED_ZERO_CSUM 0x80
  309. /*
  310. * Driver needs frames padded to some minimum length
  311. */
  312. #define IFLIB_NEED_ETHER_PAD 0x100
  313. #define IFLIB_SPARE7 0x200
  314. #define IFLIB_SPARE6 0x400
  315. #define IFLIB_SPARE5 0x800
  316. #define IFLIB_SPARE4 0x1000
  317. #define IFLIB_SPARE3 0x2000
  318. #define IFLIB_SPARE2 0x4000
  319. #define IFLIB_SPARE1 0x8000
  320. /*
  321. * Interface needs admin task to ignore interface up/down status
  322. */
  323. #define IFLIB_ADMIN_ALWAYS_RUN 0x10000
  324. /*
  325. * Driver will pass the media
  326. */
  327. #define IFLIB_DRIVER_MEDIA 0x20000
  328. /*
  329. * When using a single hardware interrupt for the interface, only process RX
  330. * interrupts instead of doing combined RX/TX processing.
  331. */
  332. #define IFLIB_SINGLE_IRQ_RX_ONLY 0x40000
  333. #define IFLIB_SPARE0 0x80000
  334. /*
  335. * Interface has an admin completion queue
  336. */
  337. #define IFLIB_HAS_ADMINCQ 0x100000
  338. /*
  339. * Interface needs to preserve TX ring indices across restarts.
  340. */
  341. #define IFLIB_PRESERVE_TX_INDICES 0x200000
  342. /* The following IFLIB_FEATURE_* defines are for driver modules to determine
  343. * what features this version of iflib supports. They shall be defined to the
  344. * first __FreeBSD_version that introduced the feature.
  345. */
  346. /*
  347. * Driver can set its own TX queue selection function
  348. * as ift_txq_select in struct if_txrx
  349. */
  350. #define IFLIB_FEATURE_QUEUE_SELECT 1400050
  351. /*
  352. * Driver can set its own TX queue selection function
  353. * as ift_txq_select_v2 in struct if_txrx. This includes
  354. * having iflib send L3+ extra header information to the
  355. * function.
  356. */
  357. #define IFLIB_FEATURE_QUEUE_SELECT_V2 1400073
  358. /*
  359. * Driver can create subinterfaces with their own Tx/Rx queues
  360. * that all share a single device (or commonly, port)
  361. */
  362. #define IFLIB_FEATURE_SUB_INTERFACES 1500014
  363. /*
  364. * These enum values are used in iflib_needs_restart to indicate to iflib
  365. * functions whether or not the interface needs restarting when certain events
  366. * happen.
  367. */
  368. enum iflib_restart_event {
  369. IFLIB_RESTART_VLAN_CONFIG,
  370. };
  371. /*
  372. * field accessors
  373. */
  374. void *iflib_get_softc(if_ctx_t ctx);
  375. device_t iflib_get_dev(if_ctx_t ctx);
  376. if_t iflib_get_ifp(if_ctx_t ctx);
  377. struct ifmedia *iflib_get_media(if_ctx_t ctx);
  378. if_softc_ctx_t iflib_get_softc_ctx(if_ctx_t ctx);
  379. if_shared_ctx_t iflib_get_sctx(if_ctx_t ctx);
  380. void iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN]);
  381. void iflib_request_reset(if_ctx_t ctx);
  382. uint8_t iflib_in_detach(if_ctx_t ctx);
  383. uint32_t iflib_get_rx_mbuf_sz(if_ctx_t ctx);
  384. /*
  385. * If the driver can plug cleanly in to newbus use these
  386. */
  387. int iflib_device_probe(device_t);
  388. int iflib_device_attach(device_t);
  389. int iflib_device_detach(device_t);
  390. int iflib_device_suspend(device_t);
  391. int iflib_device_resume(device_t);
  392. int iflib_device_shutdown(device_t);
  393. /*
  394. * Use this instead of iflib_device_probe if the driver should report
  395. * BUS_PROBE_VENDOR instead of BUS_PROBE_DEFAULT. (For example, an out-of-tree
  396. * driver based on iflib).
  397. */
  398. int iflib_device_probe_vendor(device_t);
  399. int iflib_device_iov_init(device_t, uint16_t, const nvlist_t *);
  400. void iflib_device_iov_uninit(device_t);
  401. int iflib_device_iov_add_vf(device_t, uint16_t, const nvlist_t *);
  402. /*
  403. * If the driver can't plug cleanly in to newbus
  404. * use these
  405. */
  406. int iflib_device_register(device_t dev, void *softc, if_shared_ctx_t sctx, if_ctx_t *ctxp);
  407. int iflib_device_deregister(if_ctx_t);
  408. int iflib_irq_alloc(if_ctx_t, if_irq_t, int, driver_filter_t, void *filter_arg,
  409. driver_intr_t, void *arg, const char *name);
  410. int iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
  411. iflib_intr_type_t type, driver_filter_t *filter,
  412. void *filter_arg, int qid, const char *name);
  413. void iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq,
  414. iflib_intr_type_t type, void *arg, int qid,
  415. const char *name);
  416. void iflib_irq_free(if_ctx_t ctx, if_irq_t irq);
  417. void iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu,
  418. const char *name);
  419. void iflib_config_gtask_init(void *ctx, struct grouptask *gtask,
  420. gtask_fn_t *fn, const char *name);
  421. void iflib_config_gtask_deinit(struct grouptask *gtask);
  422. void iflib_tx_intr_deferred(if_ctx_t ctx, int txqid);
  423. void iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid);
  424. void iflib_admin_intr_deferred(if_ctx_t ctx);
  425. void iflib_iov_intr_deferred(if_ctx_t ctx);
  426. void iflib_link_state_change(if_ctx_t ctx, int linkstate, uint64_t baudrate);
  427. int iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags);
  428. int iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags);
  429. void iflib_dma_free(iflib_dma_info_t dma);
  430. int iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count);
  431. void iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count);
  432. struct sx *iflib_ctx_lock_get(if_ctx_t);
  433. void iflib_led_create(if_ctx_t ctx);
  434. void iflib_add_int_delay_sysctl(if_ctx_t, const char *, const char *,
  435. if_int_delay_info_t, int, int);
  436. uint16_t iflib_get_extra_msix_vectors_sysctl(if_ctx_t ctx);
  437. /*
  438. * Sub-interface support
  439. */
  440. int iflib_irq_alloc_generic_subctx(if_ctx_t ctx, if_ctx_t subctx, if_irq_t irq,
  441. int rid, iflib_intr_type_t type,
  442. driver_filter_t *filter, void *filter_arg,
  443. int qid, const char *name);
  444. #endif /* __IFLIB_H_ */