2 * Copyright (c) 2007-2011, 2013, 2014, ETH Zurich.
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
15 #include <net_device_manager/net_device_manager.h>
17 #include <barrelfish/nameservice_client.h>
18 #include <barrelfish/debug.h>
19 #include <barrelfish/deferred.h>
23 # include <netif/e1000.h>
26 #include <if/e10k_defs.h>
27 #include <if/e10k_vf_defs.h>
28 #include <if/net_filter_defs.h>
29 #include <dev/e10k_dev.h>
35 //#define VTON_DCBOFF TODO use if VFs are enabled
38 //#define DEBUG(x...) printf("e10k: " x)
39 #define DEBUG(x...) do {} while (0)
46 struct e10k_binding *binding;
48 struct capref tx_frame;
49 struct capref txhwb_frame;
50 struct capref rx_frame;
75 MASK_L4PROTO = (1 << 0),
76 MASK_SRCIP = (1 << 1),
77 MASK_DSTIP = (1 << 2),
78 MASK_SRCPORT = (1 << 3),
79 MASK_DSTPORT = (1 << 4),
101 static union macentry mactable[128] = {
102 { .as8 = "\x0\x0\x0\x0\x0\x0" }, // First MAC is never set (loaded from card EEPROM)
104 { .as8 = "\x22\xc9\xfc\x96\x83\xfc" },
105 { .as8 = "\xce\x43\x5b\xf7\x3e\x60" },
106 { .as8 = "\x6a\xb0\x62\xf6\xa7\x21" },
107 { .as8 = "\xb2\xdf\xf9\x39\xc6\x10" },
108 { .as8 = "\x92\x77\xe7\x3f\x80\x30" },
109 { .as8 = "\xd6\x88\xd6\x86\x4a\x22" },
110 { .as8 = "\x7e\x64\xe9\x2e\xbe\x4b" },
111 { .as8 = "\xba\xac\x49\xd6\x3c\x77" },
113 // We set the rest to all zeroes
115 // Last MAC (127) never set (loaded from card EEPROM ... at least, it's already there)
118 static uint16_t credit_refill[128];
119 static uint32_t tx_rate[128];
121 // Hack for monolithic driver
122 void qd_main(void) __attribute__((weak));
123 void qd_argument(const char *arg) __attribute__((weak));
124 void qd_interrupt(bool is_rx, bool is_tx) __attribute__((weak));
125 void qd_queue_init_data(struct e10k_binding *b, struct capref registers,
126 uint64_t macaddr) __attribute__((weak));
127 void qd_queue_memory_registered(struct e10k_binding *b) __attribute__((weak));
128 void qd_write_queue_tails(struct e10k_binding *b) __attribute__((weak));
131 void cd_request_device_info(struct e10k_binding *b);
132 void cd_register_queue_memory(struct e10k_binding *b,
134 struct capref tx_frame,
135 struct capref txhwb_frame,
136 struct capref rx_frame,
140 uint8_t msix_intdest,
146 void cd_set_interrupt_rate(struct e10k_binding *b,
151 static void idc_write_queue_tails(struct e10k_binding *b);
152 static void stop_device(void);
154 static void device_init(void);
155 static void queue_hw_init(uint8_t n, bool set_tail);
156 //static void queue_hw_stop(uint8_t n);
157 static void interrupt_handler_msix(void* arg);
158 //static void interrupt_handler_msix_b(void* arg);
160 static void e10k_flt_ftqf_setup(int index, struct e10k_filter *filter);
161 //static void e10k_flt_etype_setup(int filter, int queue, uint16_t etype);
165 static const char *service_name = "e10k";
166 static int initialized = 0;
167 static bool exported = false;
168 static e10k_t *d = NULL;
169 static struct capref *regframe;
171 static bool use_interrupts = false;
172 static bool msix = false;
174 /** Specifies if RX/TX is currently enabled on the device. */
175 static bool rxtx_enabled = false;
177 // Management of MSI-X vectors
178 static struct bmallocator msix_alloc;
179 /** MSI-X vector used by cdriver */
180 static size_t cdriver_msix = -1;
181 static uint8_t cdriver_vector;
184 // State of queues and filters
185 static struct queue_state queues[128];
186 static struct e10k_filter filters[128];
188 static char buf[4096];
190 /* PCI device address passed on command line */
191 static uint32_t pci_bus = PCI_DONT_CARE;
192 static uint32_t pci_device = PCI_DONT_CARE;
193 static uint32_t pci_function = 0;
194 static uint32_t pci_deviceid = E10K_PCI_DEVID;
196 /* VFs alloacation data*/
197 static bool vf_used[63];
199 static void e10k_flt_ftqf_setup(int idx, struct e10k_filter* filter)
201 uint16_t m = filter->mask;
203 e10k_ftqf_t ftqf = 0;
204 e10k_l34timir_t timir = 0;
205 e10k_sdpqf_t sdpqf = 0;
209 if (!(m & MASK_SRCIP))
210 e10k_saqf_wr(d, idx, htonl(filter->src_ip));
211 if (!(m & MASK_DSTIP))
212 e10k_daqf_wr(d, idx, htonl(filter->dst_ip));
213 if (!(m & MASK_SRCPORT))
214 sdpqf = e10k_sdpqf_src_port_insert(sdpqf, htons(filter->src_port));
215 if (!(m & MASK_DSTPORT))
216 sdpqf = e10k_sdpqf_dst_port_insert(sdpqf, htons(filter->dst_port));
217 e10k_sdpqf_wr(d, idx, sdpqf);
220 if (!(m & MASK_L4PROTO)) {
221 switch (filter->l4_type) {
222 case L4_OTHER: p = e10k_l4other; break;
223 case L4_UDP: p = e10k_l4udp; break;
224 case L4_TCP: p = e10k_l4tcp; break;
225 case L4_SCTP: p = e10k_l4sctp; break;
226 default: assert(0); return;
228 ftqf = e10k_ftqf_protocol_insert(ftqf, p);
232 ftqf = e10k_ftqf_m_srcaddr_insert(ftqf, !!(m & MASK_SRCIP));
233 ftqf = e10k_ftqf_m_dstaddr_insert(ftqf, !!(m & MASK_DSTIP));
234 ftqf = e10k_ftqf_m_srcport_insert(ftqf, !!(m & MASK_SRCPORT));
235 ftqf = e10k_ftqf_m_dstport_insert(ftqf, !!(m & MASK_DSTPORT));
236 ftqf = e10k_ftqf_m_protocol_insert(ftqf, !!(m & MASK_L4PROTO));
239 // Configure destination queue and enable filter
240 timir = e10k_l34timir_rx_queue_insert(timir, filter->queue);
241 e10k_l34timir_wr(d, idx, timir);
243 ftqf = e10k_ftqf_priority_insert(ftqf, filter->priority);
244 ftqf = e10k_ftqf_pool_mask_insert(ftqf, 1);
245 ftqf = e10k_ftqf_queue_en_insert(ftqf, 1);
246 e10k_ftqf_wr(d, idx, ftqf);
250 static int ftqf_index = 0;
251 static int ftqf_alloc(void)
253 // FIXME: Do this reasonably
257 static errval_t reg_ftfq_filter(struct e10k_filter* f, uint64_t* fid)
261 DEBUG("reg_ftfq_filter: called\n");
263 if ((i = ftqf_alloc()) < 0) {
264 return FILTER_ERR_NOT_ENOUGH_MEMORY;
269 filters[i].enabled = true;
271 e10k_flt_ftqf_setup(i, f);
280 /****************************************************************************/
281 /* Net filter interface implementation */
282 /****************************************************************************/
285 static errval_t cb_install_filter(struct net_filter_binding *b,
286 net_filter_filter_type_t type,
296 struct e10k_filter f = {
297 .dst_port = dst_port,
298 .src_port = src_port,
301 .l4_type = (type == net_filter_PORT_TCP ? L4_TCP : L4_UDP),
307 f.mask = f.mask | MASK_SRCIP;
311 f.mask = f.mask | MASK_DSTIP;
315 f.mask = f.mask | MASK_DSTPORT;
319 f.mask = f.mask | MASK_SRCPORT;
324 err = reg_ftfq_filter(&f, fid);
325 DEBUG("filter registered: err=%s, fid=%"PRIu64"\n", err_getstring(err), *fid);
330 static errval_t cb_remove_filter(struct net_filter_binding *b,
331 net_filter_filter_type_t type,
335 if ((type == net_filter_PORT_UDP || type == net_filter_PORT_TCP)){
339 *err = NET_FILTER_ERR_NOT_FOUND;
342 DEBUG("unregister_filter: called (%"PRIx64")\n", filter_id);
346 static struct net_filter_rpc_rx_vtbl net_filter_rpc_rx_vtbl = {
347 .install_filter_ip_call = cb_install_filter,
348 .remove_filter_call = cb_remove_filter,
349 .install_filter_mac_call = NULL,
352 static void net_filter_export_cb(void *st, errval_t err, iref_t iref)
355 printf("exported net filter interface\n");
356 err = nameservice_register("net_filter_e10k", iref);
357 assert(err_is_ok(err));
358 DEBUG("Net filter interface exported\n");
362 static errval_t net_filter_connect_cb(void *st, struct net_filter_binding *b)
364 printf("New connection on net filter interface\n");
365 b->rpc_rx_vtbl = net_filter_rpc_rx_vtbl;
371 static void e10k_flt_etype_setup(int filter, int queue, uint16_t etype)
373 // Clear existing values
374 e10k_etqf_wr(d, filter, 0x0);
375 e10k_etqs_wr(d, filter, 0x0);
377 e10k_etqs_rx_queue_wrf(d, filter, queue);
378 e10k_etqs_queue_en_wrf(d, filter, 1);
380 e10k_etqf_etype_wrf(d, filter, etype);
381 e10k_etqf_filter_en_wrf(d, filter, 1);
385 static errval_t arp_filter(uint64_t qid, uint64_t* fid)
387 e10k_flt_etype_setup(0, (int) qid, 0x0806);
389 DEBUG("reg_arp_filter: called\n");
393 static errval_t reg_ftfq_filter(struct e10k_filter* f, uint64_t* fid)
397 DEBUG("reg_ftfq_filter: called\n");
399 if ((i = ftqf_alloc()) < 0) {
400 return ETHERSRV_ERR_NOT_ENOUGH_MEM;
405 filters[i].enabled = true;
407 e10k_flt_ftqf_setup(i, f);
414 static errval_t ipv4_tcp_port(uint64_t qid, uint16_t port, uint64_t* fid)
416 struct e10k_filter f = {
418 .mask = MASK_SRCIP | MASK_DSTIP | MASK_SRCPORT,
424 DEBUG("ipv4_tcp_port: called\n");
425 return reg_ftfq_filter(&f, fid);
428 static errval_t ipv4_udp_port(uint64_t qid, uint16_t port, uint64_t* fid)
430 struct e10k_filter f = {
432 .mask = MASK_SRCIP | MASK_DSTIP | MASK_SRCPORT,
438 DEBUG("ipv4_udp_port: called\n");
439 return reg_ftfq_filter( &f, fid);
442 static errval_t ipv4_tcp_conn(uint64_t qid,
443 uint32_t l_ip, uint16_t l_port,
444 uint32_t r_ip, uint16_t r_port,
447 struct e10k_filter f = {
458 DEBUG("ipv4_tcp_conn: called\n");
459 return reg_ftfq_filter(&f, fid);
462 static errval_t deregister_filter(uint64_t fid)
464 DEBUG("deregister_filter: called\n");
465 return LIB_ERR_NOT_IMPLEMENTED;
471 /** Enable RX operation for whole card. */
472 static void rx_enable(void)
474 e10k_secrxctrl_rx_dis_wrf(d, 1);
475 while (e10k_secrxstat_sr_rdy_rdf(d) == 0); // TODO: Timeout
476 e10k_rxctrl_rxen_wrf(d, 1);
477 e10k_secrxctrl_rx_dis_wrf(d, 0);
480 /** Disable RX operation for whole card. */
481 static void rx_disable(void)
483 e10k_secrxctrl_rx_dis_wrf(d, 1);
484 while (e10k_secrxstat_sr_rdy_rdf(d) == 0); // TODO: Timeout
485 e10k_rxctrl_rxen_wrf(d, 0);
486 e10k_secrxctrl_rx_dis_wrf(d, 0);
489 /** Enable TX operation for whole card. */
490 static void tx_enable(void)
492 e10k_dmatxctl_txen_wrf(d, 1);
495 /** Disable TX operation for whole card. */
496 static void tx_disable(void)
498 e10k_dmatxctl_txen_wrf(d, 0);
499 while (e10k_dmatxctl_txen_rdf(d) != 0); // TODO: timeout
503 static void setup_interrupt(size_t *msix_index, uint8_t core, uint8_t vector)
509 res = bmallocator_alloc(&msix_alloc, msix_index);
512 err = get_apicid_from_core(core, &dest);
513 assert(err_is_ok(err));
515 err = pci_msix_vector_init(*msix_index, dest, vector);
516 assert(err_is_ok(err));
518 DEBUG("e10k: MSI-X vector setup index=%"PRIx64", core=%d apic=%d swvec=%x\n",
519 *msix_index, core, dest, vector);
523 * Initialize hardware registers.
524 * Is also called after a reset of the device.
526 static void device_init(void)
532 bool initialized_before = initialized;
538 if (initialized_before) {
539 // Save queue heads and tails
540 for (i = 0; i < 128; i++) {
541 if (queues[i].enabled) {
542 queues[i].tx_head = e10k_tdh_rd(d, i);
544 queues[i].rx_head = e10k_rdh_1_rd(d, i);
546 queues[i].rx_head = e10k_rdh_2_rd(d, i - 64);
552 // Make a double reset to be sure
553 for (i = 0; i < 2; i++) {
554 // Issue Global reset
555 ctrl = e10k_ctrl_rd(d);
556 ctrl = e10k_ctrl_lrst_insert(ctrl, 1);
557 ctrl = e10k_ctrl_rst_insert(ctrl, 1);
558 e10k_ctrl_wr(d, ctrl);
559 while ((e10k_ctrl_rst_rdf(d) != 0) ||
560 (e10k_ctrl_lrst_rdf(d) != 0)); // TODO: Timeout
562 // Spec says 10, fbsd driver 50
565 DEBUG("Global reset done\n");
567 // Disable interrupts
568 e10k_eimc_cause_wrf(d, 0x7FFFFFFF);
571 // Let firmware know that we have taken over
572 e10k_ctrl_ext_drv_load_wrf(d, 1);
574 // NO Snoop disable (from FBSD)
575 // Without this, the driver only works on sbrinz1 if the receive buffers are
576 // mapped non cacheable. If the buffers are mapped cacheable, sometimes we
577 // seem to read old buffer contents, not sure exactly why, as far as
578 // understood this, No snoop should only be enabled by the device if it is
580 // TODO: Also check performance implications of this on gottardo and other
581 // machnies where it works without this.
582 e10k_ctrl_ext_ns_dis_wrf(d, 1);
584 // Initialize flow-control registers
585 for (i = 0; i < 8; i++) {
586 if (i < 4) e10k_fcttv_wr(d, i, 0x0);
587 e10k_fcrtl_wr(d, i, 0x0);
588 e10k_fcrth_wr(d, i, 0x0);
590 e10k_fcrtv_wr(d, 0x0);
591 e10k_fccfg_wr(d, 0x0);
596 // Wait for EEPROM auto read
597 while (e10k_eec_auto_rd_rdf(d) == 0); // TODO: Timeout
598 DEBUG("EEPROM auto read done\n");
600 // Wait for DMA initialization
601 while (e10k_rdrxctl_dma_initok_rdf(d) == 0); // TODO: Timeout
603 // Wait for link to come up
604 while (e10k_links_lnk_up_rdf(d) == 0); // TODO: Timeout
608 // Initialize interrupts
609 e10k_eicr_wr(d, 0xffffffff);
611 // Switch to MSI-X mode
612 e10k_gpie_msix_wrf(d, 1);
613 e10k_gpie_pba_sup_wrf(d, 1);
614 e10k_gpie_ocd_wrf(d, 1);
616 // Allocate msix vector for cdriver and set up handler
617 if (cdriver_msix == -1) {
618 err = pci_setup_inthandler(interrupt_handler_msix, NULL, &cdriver_vector);
619 assert(err_is_ok(err));
621 setup_interrupt(&cdriver_msix, disp_get_core_id(), cdriver_vector);
624 // Map management interrupts to our vector
625 e10k_ivar_misc_i_alloc0_wrf(d, cdriver_msix);
626 e10k_ivar_misc_i_alloc1_wrf(d, cdriver_msix);
627 e10k_ivar_misc_i_allocval0_wrf(d, 1);
628 e10k_ivar_misc_i_allocval1_wrf(d, 1);
630 // Enable auto masking of interrupt
631 e10k_gpie_eiame_wrf(d, 1);
632 e10k_eiamn_wr(d, cdriver_msix / 32, (1 << (cdriver_msix % 32)));
634 // Set no interrupt delay
635 e10k_eitr_l_wr(d, cdriver_msix, 0);
636 e10k_gpie_eimen_wrf(d, 1);
639 e10k_eimsn_wr(d, cdriver_msix / 32, (1 << (cdriver_msix % 32)));
641 // Set no Interrupt delay
642 e10k_eitr_l_wr(d, 0, 0);
643 e10k_gpie_eimen_wrf(d, 1);
645 // Enable all interrupts
646 e10k_eimc_wr(d, e10k_eims_rd(d));
647 e10k_eims_cause_wrf(d, 0x7fffffff);
650 // Just a guess for RSC delay
651 e10k_gpie_rsc_delay_wrf(d, 2);
653 // Initialize multiple register tables (MAC 0 and 127 are not set)
654 for (i = 0; i < 128; i++) {
655 /* uint64_t mac = e10k_ral_ral_rdf(d, i) | ((uint64_t) e10k_rah_rah_rdf(d, i) << 32); */
656 /* uint8_t *m = (uint8_t *)&mac; */
657 /* DEBUG("Old MAC %d: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx ... mac valid = %x\n", */
658 /* i, m[0], m[1], m[2], m[3], m[4], m[5], e10k_rah_av_rdf(d, 0)); */
660 if(i > 0 && i < 127) {
661 e10k_ral_wr(d, i, mactable[i].as64 & 0xffffffff);
662 e10k_rah_wr(d, i, mactable[i].as64 >> 32);
663 e10k_rah_av_wrf(d, i, 1);
665 /* mac = e10k_ral_ral_rdf(d, i) | ((uint64_t) e10k_rah_rah_rdf(d, i) << 32); */
666 /* m = (uint8_t *)&mac; */
667 /* DEBUG("New MAC %d: %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx ... mac valid = %x\n", */
668 /* i, m[0], m[1], m[2], m[3], m[4], m[5], e10k_rah_av_rdf(d, 0)); */
671 for (i = 0; i < 128; i++)
672 e10k_mta_bit_vec_wrf(d, i, 0);
673 for (i = 0; i < 128; i++)
674 e10k_vfta_vlan_flt_wrf(d, i, 0);
675 for (i = 0; i < 128; i++)
676 e10k_pfvlvfb_wr(d, i, 0);
678 for (i = 0; i < 64; i++) {
680 e10k_pfvlvf_vi_en_wrf(d, i, 1);
682 e10k_pfvlvf_vi_en_wrf(d, i, 0);
684 e10k_psrtype_wr(d, i, 0);
686 for (i = 0; i < 128; i++)
687 e10k_pfuta_wr(d, i, 0);
688 for (i = 0; i < 256; i++)
689 e10k_mpsar_pool_ena_wrf(d, i, 0);
691 // Program direct match MAC forwarding rules
692 // This setup will assign the first 64 MAC addresses each to a different
693 // RX pool. This assumes we have 64 VFs. The rest is set to filtered.
694 for(i = 0; i < 128; i++) {
696 // Pools < 32 (low bits)
697 e10k_mpsar_pool_ena_wrf(d, 2 * i, 1 << i);
698 e10k_mpsar_pool_ena_wrf(d, 2 * i + 1, 0);
700 // Pools >= 32 and < 64 (high bits)
701 e10k_mpsar_pool_ena_wrf(d, 2 * i, 0);
702 e10k_mpsar_pool_ena_wrf(d, 2 * i + 1, 1 << (i - 32));
704 // Pools >= 64 -> DROP
705 e10k_mpsar_pool_ena_wrf(d, 2 * i, 0);
706 e10k_mpsar_pool_ena_wrf(d, 2 * i + 1, 0);
710 for (i = 0; i < 128; i++) {
711 e10k_fhft_1_wr(d, i, 0);
713 e10k_fhft_2_wr(d, i, 0);
718 // Disallow per-queue RSC (not supported in SR-IOV mode)
719 e10k_rfctl_rsc_dis_wrf(d, 1);
721 // Allow for per-queue RSC
722 e10k_rfctl_rsc_dis_wrf(d, 0);
725 // Initialize RX filters
726 for (i = 0; i < 128; i++) {
727 e10k_ftqf_wr(d, i, 0);
728 e10k_saqf_wr(d, i, 0);
729 e10k_daqf_wr(d, i, 0);
730 e10k_sdpqf_wr(d, i, 0);
732 for (i = 0; i < 32; i++)
733 e10k_reta_wr(d, i, 0);
734 e10k_mcstctrl_mfe_wrf(d, 0);
737 e10k_fctrl_bam_wrf(d, 1);
739 // Enable Jumbo frames
740 e10k_hlreg0_jumboen_wrf(d, 1);
741 e10k_maxfrs_mfs_wrf(d, 15872);
743 // Make sure Rx CRC strip is consistently enabled in HLREG0 and RDRXCTL
744 e10k_hlreg0_rxcrcstrp_wrf(d, 1);
745 // Note: rscfrstsz has to be set to 0 (is mbz)
746 e10k_rdrxctl_t rdrxctl = e10k_rdrxctl_rd(d);
747 rdrxctl = e10k_rdrxctl_crcstrip_insert(rdrxctl, 1);
748 e10k_rdrxctl_wr(d, rdrxctl);
751 // Configure buffers etc. according to specification
752 // Section 4.6.11.3.4 (DCB, virtualization, no RSS)
753 // 1:1 from spec, though not sure if everything is necessary, but since
754 // initialization is still buggy, I'd rather be conservative and set some
755 // additional flags, even if they aren't strictly necessary.
756 e10k_rttdcs_arbdis_wrf(d, 1);
759 e10k_rxpbsize_size_wrf(d, 0, 0x200);
760 e10k_txpbsize_size_wrf(d, 0, 0xA0);
761 e10k_txpbthresh_thresh_wrf(d, 0, 0xA0);
762 for (i = 1; i < 8; i++) {
763 e10k_rxpbsize_size_wrf(d, i, 0x0);
764 e10k_txpbsize_size_wrf(d, i, 0x0);
765 e10k_txpbthresh_thresh_wrf(d, i, 0x0);
768 e10k_mrqc_mrque_wrf(d, e10k_vrt_only);
769 e10k_mtqc_rt_en_wrf(d, 0);
770 e10k_mtqc_vt_en_wrf(d, 1);
771 e10k_mtqc_num_tc_wrf(d, 1);
772 e10k_pfvtctl_vt_en_wrf(d, 1);
774 e10k_rxpbsize_size_wrf(d, 0, 0x200);
775 e10k_txpbsize_size_wrf(d, 0, 0xA0);
776 e10k_txpbthresh_thresh_wrf(d, 0, 0xA0);
777 for (i = 1; i < 8; i++) {
778 e10k_rxpbsize_size_wrf(d, i, 0x0);
779 e10k_txpbsize_size_wrf(d, i, 0x0);
780 e10k_txpbthresh_thresh_wrf(d, i, 0x0);
783 e10k_mrqc_mrque_wrf(d, e10k_no_rss);
784 e10k_mtqc_rt_en_wrf(d, 0);
785 e10k_mtqc_vt_en_wrf(d, 0);
786 e10k_mtqc_num_tc_wrf(d, 0);
787 e10k_pfvtctl_vt_en_wrf(d, 0);
789 e10k_rtrup2tc_wr(d, 0);
790 e10k_rttup2tc_wr(d, 0);
793 e10k_dtxmxszrq_max_bytes_wrf(d, 0xFFF);
795 e10k_dtxmxszrq_max_bytes_wrf(d, 0x010);
798 e10k_rttdcs_arbdis_wrf(d, 0);
800 for (i = 0; i < 128; i++) {
801 pfqde = e10k_pfqde_queue_idx_insert(0x0, i);
802 pfqde = e10k_pfqde_we_insert(pfqde, 1);
803 // XXX: Might want to set drop enable here
804 /* pfqde = e10k_pfqde_qde_insert(pfqde, 1); */
805 e10k_pfqde_wr(d, pfqde);
809 e10k_mflcn_rpfce_wrf(d, 0);
810 e10k_mflcn_rfce_wrf(d, 0);
811 e10k_fccfg_tfce_wrf(d, e10k_lfc_en);
813 e10k_mflcn_rpfce_wrf(d, 1);
814 e10k_mflcn_rfce_wrf(d, 0);
815 e10k_fccfg_tfce_wrf(d, e10k_pfc_en);
818 /* Causes ECC error (could be same problem as with l34timir (see e10k.dev) */
819 for (i = 0; i < 128; i++) {
820 e10k_rttdqsel_txdq_idx_wrf(d, i);
821 e10k_rttdt1c_wr(d, credit_refill[i]); // Credit refill x 64 bytes
822 e10k_rttbcnrc_wr(d, 0);
823 if(tx_rate[i] != 0) {
824 // Turn on rate scheduler for this queue and set rate factor
825 e10k_rttbcnrc_t rttbcnrc = 0;
826 // XXX: Assuming 10Gb/s link speed. Change if that's not correct.
827 uint32_t tx_factor = (10000 << 14) / tx_rate[i];
829 rttbcnrc = e10k_rttbcnrc_rf_dec_insert(rttbcnrc, tx_factor & 0x3fff);
830 rttbcnrc = e10k_rttbcnrc_rf_int_insert(rttbcnrc, tx_factor >> 14);
831 rttbcnrc = e10k_rttbcnrc_rs_ena_insert(rttbcnrc, 1);
832 e10k_rttbcnrc_wr(d, rttbcnrc);
834 printf("Setting rate for queue %d to %u\n", i, tx_rate[i]);
838 for (i = 0; i < 8; i++) {
839 e10k_rttdt2c_wr(d, i, 0);
840 e10k_rttpt2c_wr(d, i, 0);
841 e10k_rtrpt4c_wr(d, i, 0);
845 e10k_rttdcs_tdpac_wrf(d, 0);
846 e10k_rttdcs_vmpac_wrf(d, 1); // Remember to set RTTDT1C >= MTU when this is 1
848 e10k_rttdcs_tdrm_wrf(d, 0);
849 e10k_rttdcs_bdpm_wrf(d, 1);
850 e10k_rttdcs_bpbfsm_wrf(d, 0);
851 e10k_rttpcs_tppac_wrf(d, 0);
852 e10k_rttpcs_tprm_wrf(d, 0);
853 e10k_rttpcs_arbd_wrf(d, 0x224);
854 e10k_rtrpcs_rac_wrf(d, 0);
855 e10k_rtrpcs_rrm_wrf(d, 0);
858 e10k_rttdcs_tdpac_wrf(d, 0);
859 e10k_rttdcs_vmpac_wrf(d, 0);
860 e10k_rttdcs_tdrm_wrf(d, 0);
861 e10k_rttdcs_bdpm_wrf(d, 1);
862 e10k_rttdcs_bpbfsm_wrf(d, 1);
863 e10k_rttpcs_tppac_wrf(d, 0);
864 e10k_rttpcs_tprm_wrf(d, 0);
865 e10k_rttpcs_arbd_wrf(d, 0x224);
866 e10k_rtrpcs_rac_wrf(d, 0);
867 e10k_rtrpcs_rrm_wrf(d, 0);
870 // disable relaxed ordering
871 for (i = 0; i < 128; i++) {
872 e10k_dca_txctrl_txdesc_wbro_wrf(d, i, 0);
874 e10k_dca_rxctrl_1_rxhdr_ro_wrf(d, i, 0);
875 e10k_dca_rxctrl_1_rxdata_wrro_wrf(d, i, 0);
877 e10k_dca_rxctrl_2_rxhdr_ro_wrf(d, i - 64, 0);
878 e10k_dca_rxctrl_2_rxdata_wrro_wrf(d, i - 64, 0);
882 // disable all queues
883 for (i = 0; i < 128; i++) {
884 e10k_txdctl_enable_wrf(d, i, 0);
886 e10k_rxdctl_1_enable_wrf(d, i, 0);
888 e10k_rxdctl_2_enable_wrf(d, i - 64, 0);
892 for(i = 0; i < 64; i++) {
893 e10k_pfvml2flt_mpe_wrf(d, i, 1);
894 e10k_pfvml2flt_bam_wrf(d, i, 1);
895 e10k_pfvml2flt_aupe_wrf(d, i, 1);
899 // Enable DCA (Direct Cache Access)
901 e10k_dca_ctrl_t dca_ctrl = 0;
902 dca_ctrl = e10k_dca_ctrl_dca_mode_insert(dca_ctrl, e10k_dca10);
903 e10k_dca_ctrl_wr(d, dca_ctrl);
906 printf("DCA globally enabled\n");
909 DEBUG("Card initialized (%d)\n", initialized_before);
912 // Restore configuration
913 if (initialized_before) {
915 for (i = 0; i < 128; i++) {
916 if (filters[i].enabled) {
917 e10k_flt_ftqf_setup(i, filters + i);
922 for (i = 0; i < 128; i++) {
923 if (queues[i].enabled) {
924 queue_hw_init(i, true);
928 DEBUG("Configuration restored\n");
934 /** Initialize hardware queue n. */
935 static void queue_hw_init(uint8_t n, bool set_tail)
938 struct frame_identity frameid = { .base = 0, .bytes = 0 };
939 uint64_t tx_phys, txhwb_phys, rx_phys;
940 size_t tx_size, rx_size;
941 bool enable_global = !rxtx_enabled;
943 // Get physical addresses for rx/tx rings
944 r = invoke_frame_identify(queues[n].tx_frame, &frameid);
945 assert(err_is_ok(r));
946 tx_phys = frameid.base;
947 tx_size = frameid.bytes;
949 r = invoke_frame_identify(queues[n].rx_frame, &frameid);
950 assert(err_is_ok(r));
951 rx_phys = frameid.base;
952 rx_size = frameid.bytes;
954 DEBUG("tx.phys=%"PRIx64" tx.size=%"PRIu64"\n", tx_phys, tx_size);
955 DEBUG("rx.phys=%"PRIx64" rx.size=%"PRIu64"\n", rx_phys, rx_size);
958 // Initialize RX queue in HW
959 if (queues[n].rx_va) {
960 e10k_rdbal_1_wr(d, n, queues[n].rx_va);
961 e10k_rdbah_1_wr(d, n, (queues[n].rx_va) >> 32);
963 e10k_rdbal_1_wr(d, n, rx_phys);
964 e10k_rdbah_1_wr(d, n, rx_phys >> 32);
966 e10k_rdlen_1_wr(d, n, rx_size);
968 e10k_srrctl_1_bsz_pkt_wrf(d, n, queues[n].rxbufsz / 1024);
969 uint32_t hdrsz = queues[n].rxhdrsz;
973 assert(hdrsz % 64 == 0);
974 assert(hdrsz >= 128 && hdrsz <= 1024);
976 e10k_srrctl_1_bsz_hdr_wrf(d, n, hdrsz / 64);
977 // Enable header split if desired
978 if (queues[n].rxhdrsz != 0) {
979 e10k_srrctl_1_desctype_wrf(d, n, e10k_adv_hdrsp);
980 // Split packets after TCP, UDP, IP4, IP6 and L2 headers if we enable
982 e10k_psrtype_split_tcp_wrf(d, n, 1);
983 e10k_psrtype_split_udp_wrf(d, n, 1);
984 e10k_psrtype_split_ip4_wrf(d, n, 1);
985 e10k_psrtype_split_ip6_wrf(d, n, 1);
986 e10k_psrtype_split_l2_wrf(d, n, 1);
988 //e10k_srrctl_1_desctype_wrf(d, n, e10k_adv_1buf);
989 e10k_srrctl_1_desctype_wrf(d, n, e10k_legacy);
991 e10k_srrctl_1_bsz_hdr_wrf(d, n, 128 / 64); // TODO: Do 128 bytes suffice in
993 e10k_srrctl_1_drop_en_wrf(d, n, 1);
996 if (queues[n].use_rsc) {
997 USER_PANIC("RSC not supported in SR-IOV mode!\n");
998 e10k_rscctl_1_maxdesc_wrf(d, n, 3);
999 e10k_rscctl_1_rsc_en_wrf(d, n, 1);
1000 // TODO: (how) does this work for queues >=64?
1001 e10k_psrtype_split_tcp_wrf(d, n, 1); // needed for RSC
1003 e10k_rscctl_1_maxdesc_wrf(d, n, 0);
1004 e10k_rscctl_1_rsc_en_wrf(d, n, 0);
1007 // Initialize queue pointers (empty)
1008 e10k_rdt_1_wr(d, n, queues[n].rx_head);
1009 e10k_rdh_1_wr(d, n, queues[n].rx_head);
1012 // Open virtualization pool gate (assumes 64 VF mapping)
1013 e10k_pfvfre_wr(d, n / 64, e10k_pfvfre_rd(d, n / 64) | (1 << ((n / 2) % 32)));
1016 e10k_rxdctl_1_enable_wrf(d, n, 1);
1017 while (e10k_rxdctl_1_enable_rdf(d, n) == 0); // TODO: Timeout
1018 DEBUG("[%x] RX queue enabled\n", n);
1020 // Setup Interrupts for this queue
1021 if (queues[n].use_irq) {
1023 // Look for interrupt vector
1024 if (queues[n].msix_intvec != 0) {
1025 if (queues[n].msix_index == -1) {
1026 setup_interrupt(&queues[n].msix_index, queues[n].msix_intdest,
1027 queues[n].msix_intvec);
1029 rxv = txv = queues[n].msix_index;
1034 DEBUG("rxv=%d txv=%d\n", rxv, txv);
1036 // Setup mapping queue Rx/Tx -> interrupt
1039 e10k_ivar_i_alloc0_wrf(d, i, rxv);
1040 e10k_ivar_i_allocval0_wrf(d, i, 1);
1041 e10k_ivar_i_alloc1_wrf(d, i, txv);
1042 e10k_ivar_i_allocval1_wrf(d, i, 1);
1044 e10k_ivar_i_alloc2_wrf(d, i, rxv);
1045 e10k_ivar_i_allocval2_wrf(d, i, 1);
1046 e10k_ivar_i_alloc3_wrf(d, i, txv);
1047 e10k_ivar_i_allocval3_wrf(d, i, 1);
1049 if (queues[n].msix_intvec != 0) {
1050 e10k_eitr_l_wr(d, rxv, 0);
1052 // Enable autoclear (higher ones are always auto cleared)
1054 e10k_eiac_rtxq_wrf(d, e10k_eiac_rtxq_rdf(d) | (1 << rxv));
1059 // Make sure interrupt is cleared
1060 e10k_eicr_wr(d, 1 << rxv);
1064 e10k_eimsn_wr(d, rxv / 32, (1 << (rxv % 32)));
1068 if (enable_global) {
1069 DEBUG("[%x] Enabling RX globally...\n", n);
1071 DEBUG("[%x] RX globally enabled\n", n);
1076 // Enable DCA for this queue
1077 e10k_dca_rxctrl_t dca_rxctrl = 0;
1079 dca_rxctrl = e10k_dca_rxctrl_rxdca_desc_insert(dca_rxctrl, 1);
1080 dca_rxctrl = e10k_dca_rxctrl_rxdca_hdr_insert(dca_rxctrl, 1);
1081 dca_rxctrl = e10k_dca_rxctrl_rxdca_payl_insert(dca_rxctrl, 1);
1084 errval_t err = sys_debug_get_apic_id(&my_apic_id);
1085 assert(err_is_ok(err));
1087 dca_rxctrl = e10k_dca_rxctrl_cpuid_insert(dca_rxctrl, my_apic_id);
1090 e10k_dca_rxctrl_1_wr(d, n, dca_rxctrl);
1092 e10k_dca_rxctrl_2_wr(d, n - 64, dca_rxctrl);
1095 printf("DCA enabled on queue %d with APIC ID %d\n", n, my_apic_id);
1099 // Initialize TX queue in HW
1100 if (queues[n].rx_va) {
1101 e10k_tdbal_wr(d, n, queues[n].tx_va);
1102 e10k_tdbah_wr(d, n, (queues[n].tx_va) >> 32);
1104 e10k_tdbal_wr(d, n, tx_phys);
1105 e10k_tdbah_wr(d, n, tx_phys >> 32);
1107 e10k_tdlen_wr(d, n, tx_size);
1109 // Initialize TX head index write back
1110 if (!capref_is_null(queues[n].txhwb_frame)) {
1111 r = invoke_frame_identify(queues[n].txhwb_frame, &frameid);
1112 assert(err_is_ok(r));
1113 txhwb_phys = frameid.base;
1114 if (queues[n].rx_va) {
1115 e10k_tdwbal_headwb_low_wrf(d, n, (queues[n].txhwb_va) >> 2);
1116 e10k_tdwbah_headwb_high_wrf(d, n, (queues[n].txhwb_va) >> 32);
1118 e10k_tdwbal_headwb_low_wrf(d, n, txhwb_phys >> 2);
1119 e10k_tdwbah_headwb_high_wrf(d, n, txhwb_phys >> 32);
1121 e10k_tdwbal_headwb_en_wrf(d, n, 1);
1124 // Initialized by queue driver to avoid race conditions
1125 // Initialize queue pointers
1126 e10k_tdh_wr(d, n, queues[n].tx_head);
1127 e10k_tdt_wr(d, n, queues[n].tx_head);
1129 // Configure prefetch and writeback threshhold
1130 e10k_txdctl_pthresh_wrf(d, n, 8); // FIXME: Figure out what the right number
1132 e10k_txdctl_hthresh_wrf(d, n, 0);
1133 e10k_txdctl_wthresh_wrf(d, n, 0);
1135 if (enable_global) {
1136 DEBUG("[%x] Enabling TX globally...\n", n);
1138 rxtx_enabled = true;
1139 DEBUG("[%x] TX globally enabled\n", n);
1143 // Open virtualization pool gate (assumes 64 VF mapping)
1144 e10k_pfvfte_wr(d, n / 64, e10k_pfvfte_rd(d, n / 64) | (1 << ((n / 2) % 32)));
1147 e10k_txdctl_enable_wrf(d, n, 1);
1148 while (e10k_txdctl_enable_rdf(d, n) == 0); // TODO: Timeout
1149 DEBUG("[%x] TX queue enabled\n", n);
1151 // Some initialization stuff from BSD driver
1152 e10k_dca_txctrl_txdesc_wbro_wrf(d, n, 0);
1155 idc_write_queue_tails(queues[n].binding);
1161 static void queue_hw_stop(uint8_t n)
1163 // This process is described in 4.6.7.1.2
1165 // Disable TX for this queue
1166 e10k_txdctl_enable_wrf(d, n, 0);
1168 // TODO: Flush packet buffers
1169 // TODO: Remove all filters
1170 // TODO: With RSC we have to wait here (see spec), not used atm
1172 // Disable RX for this queue
1173 e10k_rxdctl_1_enable_wrf(d, n, 0);
1174 while (e10k_rxdctl_1_enable_rdf(d, n) != 0); // TODO: Timeout
1176 // A bit too much, but make sure memory is not used anymore
1182 /** Stop whole device. */
1183 static void stop_device(void)
1187 DEBUG("Stopping device\n");
1189 // Disable RX and TX
1192 rxtx_enabled = false;
1194 // Disable interrupts
1195 e10k_eimc_cause_wrf(d, 0x7FFFFFFF);
1198 // Disable each RX and TX queue
1199 for (i = 0; i < 128; i++) {
1200 e10k_txdctl_wr(d, i, e10k_txdctl_swflsh_insert(0x0, 1));
1203 e10k_rxdctl_1_wr(d, i, 0x0);
1205 e10k_rxdctl_2_wr(d, i - 64, 0x0);
1210 // From BSD driver (not in spec)
1213 // Master disable procedure
1214 e10k_ctrl_pcie_md_wrf(d, 1);
1215 while (e10k_status_pcie_mes_rdf(d) != 0); // TODO: Timeout
1216 DEBUG("Stopping device done\n");
1219 static void management_interrupt(e10k_eicr_t eicr)
1221 if (e10k_eicr_ecc_extract(eicr)) {
1222 DEBUG("##########################################\n");
1223 DEBUG("ECC Error, resetting device :-/\n");
1224 DEBUG("##########################################\n");
1226 } else if (eicr >> 16) {
1227 DEBUG("Interrupt: %x\n", eicr);
1228 e10k_eicr_prtval(buf, sizeof(buf), eicr);
1231 DEBUG("Weird management interrupt without cause: eicr=%x\n", eicr);
1235 static void interrupt_handler_msix(void* arg)
1237 DEBUG("e10k: MSI-X management interrupt\n");
1238 e10k_eicr_t eicr = e10k_eicr_rd(d);
1240 eicr &= ~(1 << cdriver_msix);
1241 management_interrupt(eicr);
1243 // Ensure management MSI-X vector is cleared
1244 e10k_eicr_wr(d, (1 << cdriver_msix));
1246 // Reenable interrupt
1247 e10k_eimsn_cause_wrf(d, cdriver_msix / 32, (1 << (cdriver_msix % 32)));
1250 /** Here are the global interrupts handled. */
1251 static void interrupt_handler(void* arg)
1253 DEBUG("e10k: received interrupt\n");
1254 e10k_eicr_t eicr = e10k_eicr_rd(d);
1257 management_interrupt(eicr);
1259 if (eicr & ((1 << QUEUE_INTRX) | (1 << QUEUE_INTTX))) {
1260 e10k_eicr_wr(d, eicr);
1261 qd_interrupt(!!(eicr & (1 << QUEUE_INTRX)),
1262 !!(eicr & (1 << QUEUE_INTTX)));
1265 // Reenable interrupt
1266 e10k_eimsn_cause_wrf(d, 0, 0x1);
1269 /******************************************************************************/
1270 /* Management interface implemetation */
1272 /** Send register cap and mac address to queue driver. */
1273 static void idc_queue_init_data(struct e10k_binding *b,
1274 struct capref registers,
1278 r = e10k_queue_init_data__tx(b, NOP_CONT, registers, macaddr);
1279 // TODO: handle busy
1280 assert(err_is_ok(r));
1283 /** Tell queue driver that we are done initializing the queue. */
1284 static void idc_queue_memory_registered(struct e10k_binding *b)
1287 r = e10k_queue_memory_registered__tx(b, NOP_CONT);
1288 // TODO: handle busy
1289 assert(err_is_ok(r));
1292 /** Send request to queue driver to rewrite the tail pointers of its queues. */
1293 static void idc_write_queue_tails(struct e10k_binding *b)
1297 qd_write_queue_tails(b);
1301 r = e10k_write_queue_tails__tx(b, NOP_CONT);
1302 // TODO: handle busy
1303 assert(err_is_ok(r));
1306 /** Request from queue driver for register memory cap */
1307 void cd_request_device_info(struct e10k_binding *b)
1309 assert(initialized);
1311 uint64_t d_mac = e10k_ral_ral_rdf(d, qi) | ((uint64_t) e10k_rah_rah_rdf(d, qi) << 32);
1312 DEBUG("mac valid = %x\n", e10k_rah_av_rdf(d, qi));
1314 uint64_t d_mac = e10k_ral_ral_rdf(d, 0) | ((uint64_t) e10k_rah_rah_rdf(d, 0) << 32);
1315 DEBUG("mac valid = %x\n", e10k_rah_av_rdf(d, 0));
1320 errval_t err = slot_alloc(&cr);
1321 assert(err_is_ok(err));
1322 err = cap_copy(cr, *regframe);
1323 assert(err_is_ok(err));
1324 qd_queue_init_data(b, cr, d_mac);
1327 idc_queue_init_data(b, *regframe, d_mac);
1330 /** Request from queue driver to initialize hardware queue. */
1331 void cd_register_queue_memory(struct e10k_binding *b,
1333 struct capref tx_frame,
1334 struct capref txhwb_frame,
1335 struct capref rx_frame,
1338 int16_t msix_intvec,
1339 uint8_t msix_intdest,
1346 DEBUG("register_queue_memory(%"PRIu8")\n", n);
1347 // TODO: Make sure that rxbufsz is a power of 2 >= 1024
1349 if (use_irq && msix_intvec != 0 && !msix) {
1350 printf("e10k: Queue %d requests MSI-X, but MSI-X is not enabled "
1351 " card driver. Ignoring queue\n", n);
1354 // Save state so we can restore the configuration in case we need to do a
1356 queues[n].enabled = true;
1357 queues[n].tx_frame = tx_frame;
1358 queues[n].txhwb_frame = txhwb_frame;
1359 queues[n].rx_frame = rx_frame;
1360 queues[n].tx_head = 0;
1361 queues[n].rx_head = 0;
1362 queues[n].rxbufsz = rxbufsz;
1363 queues[n].rxhdrsz = rxhdrsz;
1364 queues[n].msix_index = -1;
1365 queues[n].msix_intvec = msix_intvec;
1366 queues[n].msix_intdest = msix_intdest;
1367 queues[n].binding = b;
1368 queues[n].use_irq = use_irq;
1369 queues[n].use_rsc = use_rsc;
1370 queues[n].tx_va = tx_va;
1371 queues[n].rx_va = rx_va;
1372 queues[n].txhwb_va = txhwb_va;
1374 queue_hw_init(n, true);
1377 qd_queue_memory_registered(b);
1380 idc_queue_memory_registered(b);
1384 /** Request from queue driver to initialize hardware queue. */
1385 void cd_set_interrupt_rate(struct e10k_binding *b,
1389 DEBUG("set_interrupt_rate(%"PRIu8")\n", n);
1392 e10k_eitrn_t eitr = 0;
1393 eitr = e10k_eitrn_itr_int_insert(eitr, rate);
1395 i = (queues[n].msix_index == -1 ? 0 : queues[n].msix_index);
1397 e10k_eitr_l_wr(d, i, eitr);
1399 e10k_eitr_h_wr(d, i - 24, eitr);
1405 * Request from queue driver to stop hardware queue and free everything
1406 * associated with that queue.
1408 static errval_t idc_terminate_queue(struct e10k_binding *b, uint8_t n)
1410 DEBUG("idc_terminate_queue(q=%d)\n", n);
1414 queues[n].enabled = false;
1415 queues[n].binding = NULL;
1417 // TODO: Do we have to free the frame caps, or destroy the binding?
1421 static errval_t idc_register_port_filter(struct e10k_binding *b,
1425 e10k_port_type_t type,
1430 struct e10k_filter f = {
1432 .mask = MASK_SRCIP | MASK_DSTIP | MASK_SRCPORT,
1433 .l4_type = (type == e10k_PORT_TCP ? L4_TCP : L4_UDP),
1439 DEBUG("idc_register_port_filter: called (q=%d t=%d p=%d)\n",
1442 *err = reg_ftfq_filter(&f, filter);
1443 DEBUG("filter registered: err=%"PRIu64", fid=%"PRIu64"\n", *err, *filter);
1447 static errval_t idc_unregister_filter(struct e10k_binding *b,
1448 uint64_t filter, errval_t *err)
1450 DEBUG("unregister_filter: called (%"PRIx64")\n", filter);
1451 *err = LIB_ERR_NOT_IMPLEMENTED;
1455 static struct e10k_rx_vtbl rx_vtbl = {
1456 .request_device_info = cd_request_device_info,
1457 .register_queue_memory = cd_register_queue_memory,
1458 .set_interrupt_rate = cd_set_interrupt_rate,
1461 static struct e10k_rpc_rx_vtbl rpc_rx_vtbl = {
1462 .terminate_queue_call = idc_terminate_queue,
1463 .register_port_filter_call = idc_register_port_filter,
1464 .unregister_filter_call = idc_unregister_filter,
1468 static void export_cb(void *st, errval_t err, iref_t iref)
1470 const char *suffix = "_e10kmng";
1471 char name[strlen(service_name) + strlen(suffix) + 1];
1473 assert(err_is_ok(err));
1475 // Build label for interal management service
1476 sprintf(name, "%s%s", service_name, suffix);
1478 err = nameservice_register(name, iref);
1479 assert(err_is_ok(err));
1480 DEBUG("Management interface exported\n");
1483 static errval_t connect_cb(void *st, struct e10k_binding *b)
1485 DEBUG("New connection on management interface\n");
1486 b->rx_vtbl = rx_vtbl;
1487 b->rpc_rx_vtbl = rpc_rx_vtbl;
1492 * Initialize management interface for queue drivers.
1493 * This has to be done _after_ the hardware is initialized.
1495 static void initialize_mngif(void)
1499 r = e10k_export(NULL, export_cb, connect_cb, get_default_waitset(),
1500 IDC_BIND_FLAGS_DEFAULT);
1501 assert(err_is_ok(r));
1505 /****** VF/PF server interface *******/
1507 static void init_done_vf(struct e10k_vf_binding *b, uint8_t vfn)
1511 DEBUG("VF %d init done\n", vfn);
1513 // Enable correct pool for VF
1514 e10k_pfvfre_wr(d, vfn / 32, e10k_pfvfre_rd(d, vfn / 32) | (1 << (vfn % 32)));
1515 e10k_pfvfte_wr(d, vfn / 32, e10k_pfvfte_rd(d, vfn / 32) | (1 << (vfn % 32)));
1518 e10k_pfvflrec_wr(d, 0, 1 << vfn);
1520 e10k_pfvflrec_wr(d, 1, 1 << (vfn - 32));
1523 errval_t err = b->tx_vtbl.init_done_response(b, NOP_CONT);
1524 assert(err_is_ok(err));
1527 static void get_mac_address_vf(struct e10k_vf_binding *b, uint8_t vfn)
1529 assert(initialized);
1530 uint64_t d_mac = e10k_ral_ral_rdf(d, vfn) | ((uint64_t) e10k_rah_rah_rdf(d, vfn) << 32);
1531 errval_t err = b->tx_vtbl.get_mac_address_response(b, NOP_CONT, d_mac);
1532 assert(err_is_ok(err));
1535 static void request_vf_number(struct e10k_vf_binding *b)
1537 DEBUG("VF allocated\n");
1539 uint8_t vf_num = 255;
1540 for (int i = 0; i < 64; i++) {
1549 err = NIC_ERR_ALLOC_QUEUE;
1554 err = b->tx_vtbl.request_vf_number_response(b, NOP_CONT, vf_num, err);
1555 assert(err_is_ok(err));
1559 static errval_t cd_create_queue_rpc(struct e10k_vf_binding *b,
1560 struct capref tx_frame, struct capref txhwb_frame,
1561 struct capref rx_frame, uint32_t rxbufsz,
1562 int16_t msix_intvec, uint8_t msix_intdest,
1563 bool use_irq, bool use_rsc, uint64_t *mac,
1564 int32_t *qid, struct capref *regs, errval_t *ret_err)
1566 // TODO: Make sure that rxbufsz is a power of 2 >= 1024
1568 if (use_irq && msix_intvec != 0 && !msix) {
1569 printf("e10k: Queue requests MSI-X, but MSI-X is not enabled "
1570 " card driver. Ignoring queue\n");
1571 *ret_err = NIC_ERR_ALLOC_QUEUE;
1572 return NIC_ERR_ALLOC_QUEUE;
1577 for (int i = 0; i < 128; i++) {
1578 if (!queues[i].enabled) {
1579 queues[i].enabled = true;
1585 DEBUG("create queue(%"PRIu8")\n", n);
1588 *ret_err = NIC_ERR_ALLOC_QUEUE;
1589 return NIC_ERR_ALLOC_QUEUE;
1592 // Save state so we can restore the configuration in case we need to do a
1595 queues[n].tx_frame = tx_frame;
1596 queues[n].txhwb_frame = txhwb_frame;
1597 queues[n].rx_frame = rx_frame;
1598 queues[n].tx_head = 0;
1599 queues[n].rx_head = 0;
1600 queues[n].rxbufsz = rxbufsz;
1601 queues[n].msix_index = -1;
1602 queues[n].msix_intvec = msix_intvec;
1603 queues[n].msix_intdest = msix_intdest;
1604 queues[n].use_irq = use_irq;
1605 queues[n].use_rsc = use_rsc;
1607 queue_hw_init(n, false);
1609 // TODO for now vfn = 0
1610 uint64_t d_mac = e10k_ral_ral_rdf(d, 0) | ((uint64_t) e10k_rah_rah_rdf(d, 0) << 32);
1616 DEBUG("[%d] Queue int done\n", n);
1617 *ret_err = SYS_ERR_OK;
1621 static void cd_create_queue(struct e10k_vf_binding *b,
1622 struct capref tx_frame, struct capref txhwb_frame,
1623 struct capref rx_frame, uint32_t rxbufsz,
1624 int16_t msix_intvec, uint8_t msix_intdest,
1625 bool use_irq, bool use_rsc)
1634 err = cd_create_queue_rpc(b, tx_frame, txhwb_frame, rx_frame,
1635 rxbufsz, msix_intvec, msix_intdest, use_irq, use_rsc,
1636 &mac, &queueid, ®s, &err);
1638 err = b->tx_vtbl.create_queue_response(b, NOP_CONT, mac, queueid, regs, err);
1639 assert(err_is_ok(err));
1640 DEBUG("cd_create_queue end\n");
1643 static void vf_export_cb(void *st, errval_t err, iref_t iref)
1645 const char *suffix = "_vf";
1646 char name[strlen(service_name) + strlen(suffix) + 100];
1648 assert(err_is_ok(err));
1650 // Build label for interal management service
1651 sprintf(name, "%s%s%u", service_name, suffix, pci_function);
1653 err = nameservice_register(name, iref);
1654 assert(err_is_ok(err));
1655 DEBUG("VF/PF interface [%s] exported\n", name);
1659 static errval_t vf_connect_cb(void *st, struct e10k_vf_binding *b)
1661 DEBUG("New connection on VF/PF interface\n");
1663 b->rx_vtbl.create_queue_call = cd_create_queue;
1664 b->rx_vtbl.request_vf_number_call = request_vf_number;
1665 b->rx_vtbl.init_done_call = init_done_vf;
1666 b->rx_vtbl.get_mac_address_call = get_mac_address_vf;
1668 b->rpc_rx_vtbl.create_queue_call = cd_create_queue_rpc;
1675 * Initialize management interface for queue drivers.
1676 * This has to be done _after_ the hardware is initialized.
1678 static void initialize_vfif(void)
1682 r = e10k_vf_export(NULL, vf_export_cb, vf_connect_cb, get_default_waitset(),
1683 IDC_BIND_FLAGS_DEFAULT);
1684 assert(err_is_ok(r));
1686 r = net_filter_export(NULL, net_filter_export_cb, net_filter_connect_cb,
1687 get_default_waitset(), IDC_BIND_FLAGS_DEFAULT);
1688 assert(err_is_ok(r));
1691 /******************************************************************************/
1692 /* Initialization code for driver */
1694 /** Callback from pci to initialize a specific PCI device. */
1695 static void pci_init_card(struct device_mem* bar_info, int bar_count)
1700 assert(!initialized);
1702 d = malloc(sizeof(*d));
1704 // Map first BAR for register access
1705 assert(bar_count >= 1);
1706 map_device(&bar_info[0]);
1707 regframe = bar_info[0].frame_cap;
1708 DEBUG("BAR[0] mapped (v=%llx p=%llx l=%llx)\n",
1709 (unsigned long long) bar_info[0].vaddr,
1710 (unsigned long long) bar_info[0].paddr,
1711 (unsigned long long) bar_info[0].bytes);
1713 // Initialize Mackerel binding
1714 e10k_initialize(d, (void*) bar_info[0].vaddr);
1716 DEBUG("STATUS = %x\n", e10k_status_rd(d));
1718 // Initialize manager for MSI-X vectors
1720 DEBUG("Enabling MSI-X interrupts\n");
1721 uint16_t msix_count = 0;
1722 err = pci_msix_enable(&msix_count);
1723 assert(err_is_ok(err));
1724 assert(msix_count > 0);
1725 DEBUG("MSI-X #vecs=%d\n", msix_count);
1727 res = bmallocator_init(&msix_alloc, msix_count);
1730 DEBUG("Using legacy interrupts\n");
1733 // Initialize hardware registers etc.
1734 DEBUG("Initializing hardware\n");
1737 assert(initialized);
1740 DEBUG("SR-IOV device up routine\n");
1742 // Setup support for 64 VFs
1743 e10k_gcr_ext_vtmode_wrf(d, e10k_vt_64);
1744 e10k_gpie_vtmode_wrf(d, e10k_vt_64);
1746 // Enable virtualization, disable default pool, replication enable
1747 e10k_pfvtctl_t pfvtctl = e10k_pfvtctl_rd(d);
1748 pfvtctl = e10k_pfvtctl_vt_en_insert(pfvtctl, 1);
1749 pfvtctl = e10k_pfvtctl_def_pl_insert(pfvtctl, 0);
1750 pfvtctl = e10k_pfvtctl_dis_def_pl_insert(pfvtctl, 1);
1751 pfvtctl = e10k_pfvtctl_rpl_en_insert(pfvtctl, 1);
1752 e10k_pfvtctl_wr(d, pfvtctl);
1754 // Enable L2 loopback
1755 e10k_pfdtxgswc_lbe_wrf(d, 1);
1757 // TODO: Accept untagged packets in all VMDQ pools
1758 // TODO: Broadcast accept mode
1759 // TODO: Accept packets matching PFUTA table
1760 // TODO: Accept packets matching MTA table
1761 // TODO: Accept untagged packets enable
1762 // TODO: Strip VLAN tag for incoming packets
1764 DEBUG("STATUS = %x\n", e10k_status_rd(d));
1766 e10k_ctrl_ext_pfrstd_wrf(d, 1);
1770 // Now we initialize the management interface
1771 DEBUG("Initializing management interface\n");
1775 DEBUG("Initializing VF/PF interface\n");
1777 DEBUG("Done with initialization\n");
1781 /** Register with PCI */
1782 static void pci_register(void)
1786 r = pci_client_connect();
1787 assert(err_is_ok(r));
1788 DEBUG("connected to pci\n");
1790 r = pci_register_driver_irq(pci_init_card, PCI_CLASS_ETHERNET,
1791 PCI_DONT_CARE, PCI_DONT_CARE,
1792 PCI_VENDOR_INTEL, pci_deviceid,
1793 pci_bus, pci_device, pci_function,
1794 interrupt_handler, NULL);
1795 assert(err_is_ok(r));
1798 static void parse_cmdline(int argc, char **argv)
1802 for (i = 1; i < argc; i++) {
1803 if (strncmp(argv[i], "cardname=", strlen("cardname=")) == 0) {
1804 service_name = argv[i] + strlen("cardname=");
1805 } else if (strncmp(argv[i], "bus=", strlen("bus=")) == 0) {
1806 pci_bus = atol(argv[i] + strlen("bus="));
1807 } else if (strncmp(argv[i], "device=", strlen("device=")) == 0) {
1808 pci_device = atol(argv[i] + strlen("device="));
1809 } else if (strncmp(argv[i], "function=", strlen("function=")) == 0) {
1810 pci_function = atol(argv[i] + strlen("function="));
1811 } else if (strncmp(argv[i], "deviceid=", strlen("deviceid=")) == 0) {
1812 pci_deviceid = strtoul(argv[i] + strlen("deviceid="), NULL, 0);
1813 } else if (strncmp(argv[i], "msix=", strlen("msix=")) == 0) {
1814 msix = !!atol(argv[i] + strlen("msix="));
1815 // also pass this to queue driver
1816 qd_argument(argv[i]);
1817 } else if (strncmp(argv[i], "credit_refill[", strlen("credit_refill[") - 1) == 0) {
1818 // Controls the WRR (weighted round-robin) scheduler's credit refill rate
1819 // This seems to be per VM pool
1820 unsigned int entry, val;
1821 int r = sscanf(argv[i], "credit_refill[%u]=%u", &entry, &val);
1823 assert(entry < 128);
1824 assert(val < 0x3fff);
1825 credit_refill[entry] = val;
1826 } else if (strncmp(argv[i], "tx_rate[", strlen("tx_rate[") - 1) == 0) {
1827 // This is specified in Mbits/s and must be >= 10 and <= link speed (typically 10,000)
1828 // This seems to be per Tx queue
1829 unsigned int entry, val;
1830 int r = sscanf(argv[i], "tx_rate[%u]=%u", &entry, &val);
1832 assert(entry < 128);
1833 assert(val >= 10 && val <= 10000);
1834 tx_rate[entry] = val;
1836 qd_argument(argv[i]);
1842 static void eventloop(void)
1846 printf("Entering polling loop\n");
1847 ws = get_default_waitset();
1849 if (use_interrupts) {
1862 void qd_argument(const char *arg) { }
1863 void qd_interrupt(bool is_rx, bool is_tx) { }
1864 void qd_queue_init_data(struct e10k_binding *b, struct capref registers,
1865 uint64_t macaddr) { }
1866 void qd_queue_memory_registered(struct e10k_binding *b) { }
1867 void qd_write_queue_tails(struct e10k_binding *b) { }
1869 int main(int argc, char **argv)
1871 int e1000n_driver_init(int argc, char *argv[])
1874 DEBUG("PF driver started\n");
1875 // credit_refill value must be >= 1 for a queue to be able to send.
1876 // Set them all to 1 here. May be overridden via commandline.
1877 for(int i = 0; i < 128; i++) {
1878 credit_refill[i] = 1;
1881 memset(tx_rate, 0, sizeof(tx_rate));
1883 parse_cmdline(argc, argv);
1886 while (!initialized || !exported) {
1887 event_dispatch(get_default_waitset());
1890 DEBUG("e10k driver networking init \n");
1892 err = networking_init("e10k", NET_FLAGS_DO_DHCP | NET_FLAGS_POLLING |
1893 NET_FLAGS_DEFAULT_QUEUE);
1894 DEBUG("e10k driver networking init done with error: %s \n", err_getstring(err));
1895 assert(err_is_ok(err));