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);
990 e10k_srrctl_1_bsz_hdr_wrf(d, n, 128 / 64); // TODO: Do 128 bytes suffice in
992 e10k_srrctl_1_drop_en_wrf(d, n, 1);
995 if (queues[n].use_rsc) {
996 USER_PANIC("RSC not supported in SR-IOV mode!\n");
997 e10k_rscctl_1_maxdesc_wrf(d, n, 3);
998 e10k_rscctl_1_rsc_en_wrf(d, n, 1);
999 // TODO: (how) does this work for queues >=64?
1000 e10k_psrtype_split_tcp_wrf(d, n, 1); // needed for RSC
1002 e10k_rscctl_1_maxdesc_wrf(d, n, 0);
1003 e10k_rscctl_1_rsc_en_wrf(d, n, 0);
1006 // Initialize queue pointers (empty)
1007 e10k_rdt_1_wr(d, n, queues[n].rx_head);
1008 e10k_rdh_1_wr(d, n, queues[n].rx_head);
1011 // Open virtualization pool gate (assumes 64 VF mapping)
1012 e10k_pfvfre_wr(d, n / 64, e10k_pfvfre_rd(d, n / 64) | (1 << ((n / 2) % 32)));
1015 e10k_rxdctl_1_enable_wrf(d, n, 1);
1016 while (e10k_rxdctl_1_enable_rdf(d, n) == 0); // TODO: Timeout
1017 DEBUG("[%x] RX queue enabled\n", n);
1019 // Setup Interrupts for this queue
1020 if (queues[n].use_irq) {
1022 // Look for interrupt vector
1023 if (queues[n].msix_intvec != 0) {
1024 if (queues[n].msix_index == -1) {
1025 setup_interrupt(&queues[n].msix_index, queues[n].msix_intdest,
1026 queues[n].msix_intvec);
1028 rxv = txv = queues[n].msix_index;
1033 DEBUG("rxv=%d txv=%d\n", rxv, txv);
1035 // Setup mapping queue Rx/Tx -> interrupt
1038 e10k_ivar_i_alloc0_wrf(d, i, rxv);
1039 e10k_ivar_i_allocval0_wrf(d, i, 1);
1040 e10k_ivar_i_alloc1_wrf(d, i, txv);
1041 e10k_ivar_i_allocval1_wrf(d, i, 1);
1043 e10k_ivar_i_alloc2_wrf(d, i, rxv);
1044 e10k_ivar_i_allocval2_wrf(d, i, 1);
1045 e10k_ivar_i_alloc3_wrf(d, i, txv);
1046 e10k_ivar_i_allocval3_wrf(d, i, 1);
1048 if (queues[n].msix_intvec != 0) {
1049 e10k_eitr_l_wr(d, rxv, 0);
1051 // Enable autoclear (higher ones are always auto cleared)
1053 e10k_eiac_rtxq_wrf(d, e10k_eiac_rtxq_rdf(d) | (1 << rxv));
1058 // Make sure interrupt is cleared
1059 e10k_eicr_wr(d, 1 << rxv);
1063 e10k_eimsn_wr(d, rxv / 32, (1 << (rxv % 32)));
1067 if (enable_global) {
1068 DEBUG("[%x] Enabling RX globally...\n", n);
1070 DEBUG("[%x] RX globally enabled\n", n);
1075 // Enable DCA for this queue
1076 e10k_dca_rxctrl_t dca_rxctrl = 0;
1078 dca_rxctrl = e10k_dca_rxctrl_rxdca_desc_insert(dca_rxctrl, 1);
1079 dca_rxctrl = e10k_dca_rxctrl_rxdca_hdr_insert(dca_rxctrl, 1);
1080 dca_rxctrl = e10k_dca_rxctrl_rxdca_payl_insert(dca_rxctrl, 1);
1083 errval_t err = sys_debug_get_apic_id(&my_apic_id);
1084 assert(err_is_ok(err));
1086 dca_rxctrl = e10k_dca_rxctrl_cpuid_insert(dca_rxctrl, my_apic_id);
1089 e10k_dca_rxctrl_1_wr(d, n, dca_rxctrl);
1091 e10k_dca_rxctrl_2_wr(d, n - 64, dca_rxctrl);
1094 printf("DCA enabled on queue %d with APIC ID %d\n", n, my_apic_id);
1098 // Initialize TX queue in HW
1099 if (queues[n].rx_va) {
1100 e10k_tdbal_wr(d, n, queues[n].tx_va);
1101 e10k_tdbah_wr(d, n, (queues[n].tx_va) >> 32);
1103 e10k_tdbal_wr(d, n, tx_phys);
1104 e10k_tdbah_wr(d, n, tx_phys >> 32);
1106 e10k_tdlen_wr(d, n, tx_size);
1108 // Initialize TX head index write back
1109 if (!capref_is_null(queues[n].txhwb_frame)) {
1110 r = invoke_frame_identify(queues[n].txhwb_frame, &frameid);
1111 assert(err_is_ok(r));
1112 txhwb_phys = frameid.base;
1113 if (queues[n].rx_va) {
1114 e10k_tdwbal_headwb_low_wrf(d, n, (queues[n].txhwb_va) >> 2);
1115 e10k_tdwbah_headwb_high_wrf(d, n, (queues[n].txhwb_va) >> 32);
1117 e10k_tdwbal_headwb_low_wrf(d, n, txhwb_phys >> 2);
1118 e10k_tdwbah_headwb_high_wrf(d, n, txhwb_phys >> 32);
1120 e10k_tdwbal_headwb_en_wrf(d, n, 1);
1123 // Initialized by queue driver to avoid race conditions
1124 // Initialize queue pointers
1125 e10k_tdh_wr(d, n, queues[n].tx_head);
1126 e10k_tdt_wr(d, n, queues[n].tx_head);
1128 // Configure prefetch and writeback threshhold
1129 e10k_txdctl_pthresh_wrf(d, n, 8); // FIXME: Figure out what the right number
1131 e10k_txdctl_hthresh_wrf(d, n, 0);
1132 e10k_txdctl_wthresh_wrf(d, n, 0);
1134 if (enable_global) {
1135 DEBUG("[%x] Enabling TX globally...\n", n);
1137 rxtx_enabled = true;
1138 DEBUG("[%x] TX globally enabled\n", n);
1142 // Open virtualization pool gate (assumes 64 VF mapping)
1143 e10k_pfvfte_wr(d, n / 64, e10k_pfvfte_rd(d, n / 64) | (1 << ((n / 2) % 32)));
1146 e10k_txdctl_enable_wrf(d, n, 1);
1147 while (e10k_txdctl_enable_rdf(d, n) == 0); // TODO: Timeout
1148 DEBUG("[%x] TX queue enabled\n", n);
1150 // Some initialization stuff from BSD driver
1151 e10k_dca_txctrl_txdesc_wbro_wrf(d, n, 0);
1154 idc_write_queue_tails(queues[n].binding);
1160 static void queue_hw_stop(uint8_t n)
1162 // This process is described in 4.6.7.1.2
1164 // Disable TX for this queue
1165 e10k_txdctl_enable_wrf(d, n, 0);
1167 // TODO: Flush packet buffers
1168 // TODO: Remove all filters
1169 // TODO: With RSC we have to wait here (see spec), not used atm
1171 // Disable RX for this queue
1172 e10k_rxdctl_1_enable_wrf(d, n, 0);
1173 while (e10k_rxdctl_1_enable_rdf(d, n) != 0); // TODO: Timeout
1175 // A bit too much, but make sure memory is not used anymore
1181 /** Stop whole device. */
1182 static void stop_device(void)
1186 DEBUG("Stopping device\n");
1188 // Disable RX and TX
1191 rxtx_enabled = false;
1193 // Disable interrupts
1194 e10k_eimc_cause_wrf(d, 0x7FFFFFFF);
1197 // Disable each RX and TX queue
1198 for (i = 0; i < 128; i++) {
1199 e10k_txdctl_wr(d, i, e10k_txdctl_swflsh_insert(0x0, 1));
1202 e10k_rxdctl_1_wr(d, i, 0x0);
1204 e10k_rxdctl_2_wr(d, i - 64, 0x0);
1209 // From BSD driver (not in spec)
1212 // Master disable procedure
1213 e10k_ctrl_pcie_md_wrf(d, 1);
1214 while (e10k_status_pcie_mes_rdf(d) != 0); // TODO: Timeout
1215 DEBUG("Stopping device done\n");
1218 static void management_interrupt(e10k_eicr_t eicr)
1220 if (e10k_eicr_ecc_extract(eicr)) {
1221 DEBUG("##########################################\n");
1222 DEBUG("ECC Error, resetting device :-/\n");
1223 DEBUG("##########################################\n");
1225 } else if (eicr >> 16) {
1226 DEBUG("Interrupt: %x\n", eicr);
1227 e10k_eicr_prtval(buf, sizeof(buf), eicr);
1230 DEBUG("Weird management interrupt without cause: eicr=%x\n", eicr);
1234 static void interrupt_handler_msix(void* arg)
1236 DEBUG("e10k: MSI-X management interrupt\n");
1237 e10k_eicr_t eicr = e10k_eicr_rd(d);
1239 eicr &= ~(1 << cdriver_msix);
1240 management_interrupt(eicr);
1242 // Ensure management MSI-X vector is cleared
1243 e10k_eicr_wr(d, (1 << cdriver_msix));
1245 // Reenable interrupt
1246 e10k_eimsn_cause_wrf(d, cdriver_msix / 32, (1 << (cdriver_msix % 32)));
1249 /** Here are the global interrupts handled. */
1250 static void interrupt_handler(void* arg)
1252 DEBUG("e10k: received interrupt\n");
1253 e10k_eicr_t eicr = e10k_eicr_rd(d);
1256 management_interrupt(eicr);
1258 if (eicr & ((1 << QUEUE_INTRX) | (1 << QUEUE_INTTX))) {
1259 e10k_eicr_wr(d, eicr);
1260 qd_interrupt(!!(eicr & (1 << QUEUE_INTRX)),
1261 !!(eicr & (1 << QUEUE_INTTX)));
1264 // Reenable interrupt
1265 e10k_eimsn_cause_wrf(d, 0, 0x1);
1268 /******************************************************************************/
1269 /* Management interface implemetation */
1271 /** Send register cap and mac address to queue driver. */
1272 static void idc_queue_init_data(struct e10k_binding *b,
1273 struct capref registers,
1277 r = e10k_queue_init_data__tx(b, NOP_CONT, registers, macaddr);
1278 // TODO: handle busy
1279 assert(err_is_ok(r));
1282 /** Tell queue driver that we are done initializing the queue. */
1283 static void idc_queue_memory_registered(struct e10k_binding *b)
1286 r = e10k_queue_memory_registered__tx(b, NOP_CONT);
1287 // TODO: handle busy
1288 assert(err_is_ok(r));
1291 /** Send request to queue driver to rewrite the tail pointers of its queues. */
1292 static void idc_write_queue_tails(struct e10k_binding *b)
1296 qd_write_queue_tails(b);
1300 r = e10k_write_queue_tails__tx(b, NOP_CONT);
1301 // TODO: handle busy
1302 assert(err_is_ok(r));
1305 /** Request from queue driver for register memory cap */
1306 void cd_request_device_info(struct e10k_binding *b)
1308 assert(initialized);
1310 uint64_t d_mac = e10k_ral_ral_rdf(d, qi) | ((uint64_t) e10k_rah_rah_rdf(d, qi) << 32);
1311 DEBUG("mac valid = %x\n", e10k_rah_av_rdf(d, qi));
1313 uint64_t d_mac = e10k_ral_ral_rdf(d, 0) | ((uint64_t) e10k_rah_rah_rdf(d, 0) << 32);
1314 DEBUG("mac valid = %x\n", e10k_rah_av_rdf(d, 0));
1319 errval_t err = slot_alloc(&cr);
1320 assert(err_is_ok(err));
1321 err = cap_copy(cr, *regframe);
1322 assert(err_is_ok(err));
1323 qd_queue_init_data(b, cr, d_mac);
1326 idc_queue_init_data(b, *regframe, d_mac);
1329 /** Request from queue driver to initialize hardware queue. */
1330 void cd_register_queue_memory(struct e10k_binding *b,
1332 struct capref tx_frame,
1333 struct capref txhwb_frame,
1334 struct capref rx_frame,
1337 int16_t msix_intvec,
1338 uint8_t msix_intdest,
1345 DEBUG("register_queue_memory(%"PRIu8")\n", n);
1346 // TODO: Make sure that rxbufsz is a power of 2 >= 1024
1348 if (use_irq && msix_intvec != 0 && !msix) {
1349 printf("e10k: Queue %d requests MSI-X, but MSI-X is not enabled "
1350 " card driver. Ignoring queue\n", n);
1353 // Save state so we can restore the configuration in case we need to do a
1355 queues[n].enabled = true;
1356 queues[n].tx_frame = tx_frame;
1357 queues[n].txhwb_frame = txhwb_frame;
1358 queues[n].rx_frame = rx_frame;
1359 queues[n].tx_head = 0;
1360 queues[n].rx_head = 0;
1361 queues[n].rxbufsz = rxbufsz;
1362 queues[n].rxhdrsz = rxhdrsz;
1363 queues[n].msix_index = -1;
1364 queues[n].msix_intvec = msix_intvec;
1365 queues[n].msix_intdest = msix_intdest;
1366 queues[n].binding = b;
1367 queues[n].use_irq = use_irq;
1368 queues[n].use_rsc = use_rsc;
1369 queues[n].tx_va = tx_va;
1370 queues[n].rx_va = rx_va;
1371 queues[n].txhwb_va = txhwb_va;
1373 queue_hw_init(n, true);
1376 qd_queue_memory_registered(b);
1379 idc_queue_memory_registered(b);
1383 /** Request from queue driver to initialize hardware queue. */
1384 void cd_set_interrupt_rate(struct e10k_binding *b,
1388 DEBUG("set_interrupt_rate(%"PRIu8")\n", n);
1391 e10k_eitrn_t eitr = 0;
1392 eitr = e10k_eitrn_itr_int_insert(eitr, rate);
1394 i = (queues[n].msix_index == -1 ? 0 : queues[n].msix_index);
1396 e10k_eitr_l_wr(d, i, eitr);
1398 e10k_eitr_h_wr(d, i - 24, eitr);
1404 * Request from queue driver to stop hardware queue and free everything
1405 * associated with that queue.
1407 static errval_t idc_terminate_queue(struct e10k_binding *b, uint8_t n)
1409 DEBUG("idc_terminate_queue(q=%d)\n", n);
1413 queues[n].enabled = false;
1414 queues[n].binding = NULL;
1416 // TODO: Do we have to free the frame caps, or destroy the binding?
1420 static errval_t idc_register_port_filter(struct e10k_binding *b,
1424 e10k_port_type_t type,
1429 struct e10k_filter f = {
1431 .mask = MASK_SRCIP | MASK_DSTIP | MASK_SRCPORT,
1432 .l4_type = (type == e10k_PORT_TCP ? L4_TCP : L4_UDP),
1438 DEBUG("idc_register_port_filter: called (q=%d t=%d p=%d)\n",
1441 *err = reg_ftfq_filter(&f, filter);
1442 DEBUG("filter registered: err=%"PRIu64", fid=%"PRIu64"\n", *err, *filter);
1446 static errval_t idc_unregister_filter(struct e10k_binding *b,
1447 uint64_t filter, errval_t *err)
1449 DEBUG("unregister_filter: called (%"PRIx64")\n", filter);
1450 *err = LIB_ERR_NOT_IMPLEMENTED;
1454 static struct e10k_rx_vtbl rx_vtbl = {
1455 .request_device_info = cd_request_device_info,
1456 .register_queue_memory = cd_register_queue_memory,
1457 .set_interrupt_rate = cd_set_interrupt_rate,
1460 static struct e10k_rpc_rx_vtbl rpc_rx_vtbl = {
1461 .terminate_queue_call = idc_terminate_queue,
1462 .register_port_filter_call = idc_register_port_filter,
1463 .unregister_filter_call = idc_unregister_filter,
1467 static void export_cb(void *st, errval_t err, iref_t iref)
1469 const char *suffix = "_e10kmng";
1470 char name[strlen(service_name) + strlen(suffix) + 1];
1472 assert(err_is_ok(err));
1474 // Build label for interal management service
1475 sprintf(name, "%s%s", service_name, suffix);
1477 err = nameservice_register(name, iref);
1478 assert(err_is_ok(err));
1479 DEBUG("Management interface exported\n");
1482 static errval_t connect_cb(void *st, struct e10k_binding *b)
1484 DEBUG("New connection on management interface\n");
1485 b->rx_vtbl = rx_vtbl;
1486 b->rpc_rx_vtbl = rpc_rx_vtbl;
1491 * Initialize management interface for queue drivers.
1492 * This has to be done _after_ the hardware is initialized.
1494 static void initialize_mngif(void)
1498 r = e10k_export(NULL, export_cb, connect_cb, get_default_waitset(),
1499 IDC_BIND_FLAGS_DEFAULT);
1500 assert(err_is_ok(r));
1504 /****** VF/PF server interface *******/
1506 static void init_done_vf(struct e10k_vf_binding *b, uint8_t vfn)
1510 DEBUG("VF %d init done\n", vfn);
1512 // Enable correct pool for VF
1513 e10k_pfvfre_wr(d, vfn / 32, e10k_pfvfre_rd(d, vfn / 32) | (1 << (vfn % 32)));
1514 e10k_pfvfte_wr(d, vfn / 32, e10k_pfvfte_rd(d, vfn / 32) | (1 << (vfn % 32)));
1517 e10k_pfvflrec_wr(d, 0, 1 << vfn);
1519 e10k_pfvflrec_wr(d, 1, 1 << (vfn - 32));
1522 errval_t err = b->tx_vtbl.init_done_response(b, NOP_CONT);
1523 assert(err_is_ok(err));
1526 static void get_mac_address_vf(struct e10k_vf_binding *b, uint8_t vfn)
1528 assert(initialized);
1529 uint64_t d_mac = e10k_ral_ral_rdf(d, vfn) | ((uint64_t) e10k_rah_rah_rdf(d, vfn) << 32);
1530 errval_t err = b->tx_vtbl.get_mac_address_response(b, NOP_CONT, d_mac);
1531 assert(err_is_ok(err));
1534 static void request_vf_number(struct e10k_vf_binding *b)
1536 DEBUG("VF allocated\n");
1538 uint8_t vf_num = 255;
1539 for (int i = 0; i < 64; i++) {
1548 err = NIC_ERR_ALLOC_QUEUE;
1553 err = b->tx_vtbl.request_vf_number_response(b, NOP_CONT, vf_num, err);
1554 assert(err_is_ok(err));
1558 static errval_t cd_create_queue_rpc(struct e10k_vf_binding *b,
1559 struct capref tx_frame, struct capref txhwb_frame,
1560 struct capref rx_frame, uint32_t rxbufsz,
1561 int16_t msix_intvec, uint8_t msix_intdest,
1562 bool use_irq, bool use_rsc, uint64_t *mac,
1563 int32_t *qid, struct capref *regs, errval_t *ret_err)
1565 // TODO: Make sure that rxbufsz is a power of 2 >= 1024
1567 if (use_irq && msix_intvec != 0 && !msix) {
1568 printf("e10k: Queue requests MSI-X, but MSI-X is not enabled "
1569 " card driver. Ignoring queue\n");
1570 *ret_err = NIC_ERR_ALLOC_QUEUE;
1571 return NIC_ERR_ALLOC_QUEUE;
1576 for (int i = 0; i < 128; i++) {
1577 if (!queues[i].enabled) {
1578 queues[i].enabled = true;
1584 DEBUG("create queue(%"PRIu8")\n", n);
1587 *ret_err = NIC_ERR_ALLOC_QUEUE;
1588 return NIC_ERR_ALLOC_QUEUE;
1591 // Save state so we can restore the configuration in case we need to do a
1594 queues[n].tx_frame = tx_frame;
1595 queues[n].txhwb_frame = txhwb_frame;
1596 queues[n].rx_frame = rx_frame;
1597 queues[n].tx_head = 0;
1598 queues[n].rx_head = 0;
1599 queues[n].rxbufsz = rxbufsz;
1600 queues[n].msix_index = -1;
1601 queues[n].msix_intvec = msix_intvec;
1602 queues[n].msix_intdest = msix_intdest;
1603 queues[n].use_irq = use_irq;
1604 queues[n].use_rsc = use_rsc;
1606 queue_hw_init(n, false);
1608 // TODO for now vfn = 0
1609 uint64_t d_mac = e10k_ral_ral_rdf(d, 0) | ((uint64_t) e10k_rah_rah_rdf(d, 0) << 32);
1615 DEBUG("[%d] Queue int done\n", n);
1616 *ret_err = SYS_ERR_OK;
1620 static void cd_create_queue(struct e10k_vf_binding *b,
1621 struct capref tx_frame, struct capref txhwb_frame,
1622 struct capref rx_frame, uint32_t rxbufsz,
1623 int16_t msix_intvec, uint8_t msix_intdest,
1624 bool use_irq, bool use_rsc)
1633 err = cd_create_queue_rpc(b, tx_frame, txhwb_frame, rx_frame,
1634 rxbufsz, msix_intvec, msix_intdest, use_irq, use_rsc,
1635 &mac, &queueid, ®s, &err);
1637 err = b->tx_vtbl.create_queue_response(b, NOP_CONT, mac, queueid, regs, err);
1638 assert(err_is_ok(err));
1639 DEBUG("cd_create_queue end\n");
1642 static void vf_export_cb(void *st, errval_t err, iref_t iref)
1644 const char *suffix = "_vf";
1645 char name[strlen(service_name) + strlen(suffix) + 100];
1647 assert(err_is_ok(err));
1649 // Build label for interal management service
1650 sprintf(name, "%s%s%u", service_name, suffix, pci_function);
1652 err = nameservice_register(name, iref);
1653 assert(err_is_ok(err));
1654 DEBUG("VF/PF interface [%s] exported\n", name);
1658 static errval_t vf_connect_cb(void *st, struct e10k_vf_binding *b)
1660 DEBUG("New connection on VF/PF interface\n");
1662 b->rx_vtbl.create_queue_call = cd_create_queue;
1663 b->rx_vtbl.request_vf_number_call = request_vf_number;
1664 b->rx_vtbl.init_done_call = init_done_vf;
1665 b->rx_vtbl.get_mac_address_call = get_mac_address_vf;
1667 b->rpc_rx_vtbl.create_queue_call = cd_create_queue_rpc;
1674 * Initialize management interface for queue drivers.
1675 * This has to be done _after_ the hardware is initialized.
1677 static void initialize_vfif(void)
1681 r = e10k_vf_export(NULL, vf_export_cb, vf_connect_cb, get_default_waitset(),
1682 IDC_BIND_FLAGS_DEFAULT);
1683 assert(err_is_ok(r));
1685 r = net_filter_export(NULL, net_filter_export_cb, net_filter_connect_cb,
1686 get_default_waitset(), IDC_BIND_FLAGS_DEFAULT);
1687 assert(err_is_ok(r));
1690 /******************************************************************************/
1691 /* Initialization code for driver */
1693 /** Callback from pci to initialize a specific PCI device. */
1694 static void pci_init_card(struct device_mem* bar_info, int bar_count)
1699 assert(!initialized);
1701 d = malloc(sizeof(*d));
1703 // Map first BAR for register access
1704 assert(bar_count >= 1);
1705 map_device(&bar_info[0]);
1706 regframe = bar_info[0].frame_cap;
1707 DEBUG("BAR[0] mapped (v=%llx p=%llx l=%llx)\n",
1708 (unsigned long long) bar_info[0].vaddr,
1709 (unsigned long long) bar_info[0].paddr,
1710 (unsigned long long) bar_info[0].bytes);
1712 // Initialize Mackerel binding
1713 e10k_initialize(d, (void*) bar_info[0].vaddr);
1715 DEBUG("STATUS = %x\n", e10k_status_rd(d));
1717 // Initialize manager for MSI-X vectors
1719 DEBUG("Enabling MSI-X interrupts\n");
1720 uint16_t msix_count = 0;
1721 err = pci_msix_enable(&msix_count);
1722 assert(err_is_ok(err));
1723 assert(msix_count > 0);
1724 DEBUG("MSI-X #vecs=%d\n", msix_count);
1726 res = bmallocator_init(&msix_alloc, msix_count);
1729 DEBUG("Using legacy interrupts\n");
1732 // Initialize hardware registers etc.
1733 DEBUG("Initializing hardware\n");
1736 assert(initialized);
1739 DEBUG("SR-IOV device up routine\n");
1741 // Setup support for 64 VFs
1742 e10k_gcr_ext_vtmode_wrf(d, e10k_vt_64);
1743 e10k_gpie_vtmode_wrf(d, e10k_vt_64);
1745 // Enable virtualization, disable default pool, replication enable
1746 e10k_pfvtctl_t pfvtctl = e10k_pfvtctl_rd(d);
1747 pfvtctl = e10k_pfvtctl_vt_en_insert(pfvtctl, 1);
1748 pfvtctl = e10k_pfvtctl_def_pl_insert(pfvtctl, 0);
1749 pfvtctl = e10k_pfvtctl_dis_def_pl_insert(pfvtctl, 1);
1750 pfvtctl = e10k_pfvtctl_rpl_en_insert(pfvtctl, 1);
1751 e10k_pfvtctl_wr(d, pfvtctl);
1753 // Enable L2 loopback
1754 e10k_pfdtxgswc_lbe_wrf(d, 1);
1756 // TODO: Accept untagged packets in all VMDQ pools
1757 // TODO: Broadcast accept mode
1758 // TODO: Accept packets matching PFUTA table
1759 // TODO: Accept packets matching MTA table
1760 // TODO: Accept untagged packets enable
1761 // TODO: Strip VLAN tag for incoming packets
1763 DEBUG("STATUS = %x\n", e10k_status_rd(d));
1765 e10k_ctrl_ext_pfrstd_wrf(d, 1);
1769 // Now we initialize the management interface
1770 DEBUG("Initializing management interface\n");
1774 DEBUG("Initializing VF/PF interface\n");
1776 DEBUG("Done with initialization\n");
1780 /** Register with PCI */
1781 static void pci_register(void)
1785 r = pci_client_connect();
1786 assert(err_is_ok(r));
1787 DEBUG("connected to pci\n");
1789 r = pci_register_driver_irq(pci_init_card, PCI_CLASS_ETHERNET,
1790 PCI_DONT_CARE, PCI_DONT_CARE,
1791 PCI_VENDOR_INTEL, pci_deviceid,
1792 pci_bus, pci_device, pci_function,
1793 interrupt_handler, NULL);
1794 assert(err_is_ok(r));
1797 static void parse_cmdline(int argc, char **argv)
1801 for (i = 1; i < argc; i++) {
1802 if (strncmp(argv[i], "cardname=", strlen("cardname=")) == 0) {
1803 service_name = argv[i] + strlen("cardname=");
1804 } else if (strncmp(argv[i], "bus=", strlen("bus=")) == 0) {
1805 pci_bus = atol(argv[i] + strlen("bus="));
1806 } else if (strncmp(argv[i], "device=", strlen("device=")) == 0) {
1807 pci_device = atol(argv[i] + strlen("device="));
1808 } else if (strncmp(argv[i], "function=", strlen("function=")) == 0) {
1809 pci_function = atol(argv[i] + strlen("function="));
1810 } else if (strncmp(argv[i], "deviceid=", strlen("deviceid=")) == 0) {
1811 pci_deviceid = strtoul(argv[i] + strlen("deviceid="), NULL, 0);
1812 } else if (strncmp(argv[i], "msix=", strlen("msix=")) == 0) {
1813 msix = !!atol(argv[i] + strlen("msix="));
1814 // also pass this to queue driver
1815 qd_argument(argv[i]);
1816 } else if (strncmp(argv[i], "credit_refill[", strlen("credit_refill[") - 1) == 0) {
1817 // Controls the WRR (weighted round-robin) scheduler's credit refill rate
1818 // This seems to be per VM pool
1819 unsigned int entry, val;
1820 int r = sscanf(argv[i], "credit_refill[%u]=%u", &entry, &val);
1822 assert(entry < 128);
1823 assert(val < 0x3fff);
1824 credit_refill[entry] = val;
1825 } else if (strncmp(argv[i], "tx_rate[", strlen("tx_rate[") - 1) == 0) {
1826 // This is specified in Mbits/s and must be >= 10 and <= link speed (typically 10,000)
1827 // This seems to be per Tx queue
1828 unsigned int entry, val;
1829 int r = sscanf(argv[i], "tx_rate[%u]=%u", &entry, &val);
1831 assert(entry < 128);
1832 assert(val >= 10 && val <= 10000);
1833 tx_rate[entry] = val;
1835 qd_argument(argv[i]);
1841 static void eventloop(void)
1845 printf("Entering polling loop\n");
1846 ws = get_default_waitset();
1848 if (use_interrupts) {
1861 void qd_argument(const char *arg) { }
1862 void qd_interrupt(bool is_rx, bool is_tx) { }
1863 void qd_queue_init_data(struct e10k_binding *b, struct capref registers,
1864 uint64_t macaddr) { }
1865 void qd_queue_memory_registered(struct e10k_binding *b) { }
1866 void qd_write_queue_tails(struct e10k_binding *b) { }
1868 int main(int argc, char **argv)
1870 int e1000n_driver_init(int argc, char *argv[])
1873 DEBUG("PF driver started\n");
1874 // credit_refill value must be >= 1 for a queue to be able to send.
1875 // Set them all to 1 here. May be overridden via commandline.
1876 for(int i = 0; i < 128; i++) {
1877 credit_refill[i] = 1;
1880 memset(tx_rate, 0, sizeof(tx_rate));
1882 parse_cmdline(argc, argv);
1885 while (!initialized || !exported) {
1886 event_dispatch(get_default_waitset());
1889 DEBUG("e10k driver networking init \n");
1891 err = networking_init("e10k", NET_FLAGS_DO_DHCP | NET_FLAGS_POLLING |
1892 NET_FLAGS_DEFAULT_QUEUE);
1893 DEBUG("e10k driver networking init done with error: %s \n", err_getstring(err));
1894 assert(err_is_ok(err));