1 /* 2 * libqos driver framework 3 * 4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License version 2.1 as published by the Free Software Foundation. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, see <http://www.gnu.org/licenses/> 17 */ 18 19 #include "qemu/osdep.h" 20 #include "hw/net/e1000_regs.h" 21 #include "../libqtest.h" 22 #include "pci-pc.h" 23 #include "qemu/sockets.h" 24 #include "qemu/iov.h" 25 #include "qemu/module.h" 26 #include "qemu/bitops.h" 27 #include "libqos-malloc.h" 28 #include "qgraph.h" 29 #include "e1000e.h" 30 31 #define E1000E_IVAR_TEST_CFG \ 32 (E1000E_RX0_MSG_ID | E1000_IVAR_INT_ALLOC_VALID | \ 33 ((E1000E_TX0_MSG_ID | E1000_IVAR_INT_ALLOC_VALID) << 8) | \ 34 ((E1000E_OTHER_MSG_ID | E1000_IVAR_INT_ALLOC_VALID) << 16) | \ 35 E1000_IVAR_TX_INT_EVERY_WB) 36 37 #define E1000E_RING_LEN (0x1000) 38 39 static void e1000e_macreg_write(QE1000E *d, uint32_t reg, uint32_t val) 40 { 41 QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e); 42 qpci_io_writel(&d_pci->pci_dev, d_pci->mac_regs, reg, val); 43 } 44 45 static uint32_t e1000e_macreg_read(QE1000E *d, uint32_t reg) 46 { 47 QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e); 48 return qpci_io_readl(&d_pci->pci_dev, d_pci->mac_regs, reg); 49 } 50 51 void e1000e_tx_ring_push(QE1000E *d, void *descr) 52 { 53 QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e); 54 uint32_t tail = e1000e_macreg_read(d, E1000E_TDT); 55 uint32_t len = e1000e_macreg_read(d, E1000E_TDLEN) / E1000_RING_DESC_LEN; 56 57 qtest_memwrite(d_pci->pci_dev.bus->qts, 58 d->tx_ring + tail * E1000_RING_DESC_LEN, 59 descr, E1000_RING_DESC_LEN); 60 e1000e_macreg_write(d, E1000E_TDT, (tail + 1) % len); 61 62 /* Read WB data for the packet transmitted */ 63 qtest_memread(d_pci->pci_dev.bus->qts, 64 d->tx_ring + tail * E1000_RING_DESC_LEN, 65 descr, E1000_RING_DESC_LEN); 66 } 67 68 void e1000e_rx_ring_push(QE1000E *d, void *descr) 69 { 70 QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e); 71 uint32_t tail = e1000e_macreg_read(d, E1000E_RDT); 72 uint32_t len = e1000e_macreg_read(d, E1000E_RDLEN) / E1000_RING_DESC_LEN; 73 74 qtest_memwrite(d_pci->pci_dev.bus->qts, 75 d->rx_ring + tail * E1000_RING_DESC_LEN, 76 descr, E1000_RING_DESC_LEN); 77 e1000e_macreg_write(d, E1000E_RDT, (tail + 1) % len); 78 79 /* Read WB data for the packet received */ 80 qtest_memread(d_pci->pci_dev.bus->qts, 81 d->rx_ring + tail * E1000_RING_DESC_LEN, 82 descr, E1000_RING_DESC_LEN); 83 } 84 85 static void e1000e_foreach_callback(QPCIDevice *dev, int devfn, void *data) 86 { 87 QPCIDevice *res = data; 88 memcpy(res, dev, sizeof(QPCIDevice)); 89 g_free(dev); 90 } 91 92 void e1000e_wait_isr(QE1000E *d, uint16_t msg_id) 93 { 94 QE1000E_PCI *d_pci = container_of(d, QE1000E_PCI, e1000e); 95 guint64 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; 96 97 do { 98 if (qpci_msix_pending(&d_pci->pci_dev, msg_id)) { 99 return; 100 } 101 qtest_clock_step(d_pci->pci_dev.bus->qts, 10000); 102 } while (g_get_monotonic_time() < end_time); 103 104 g_error("Timeout expired"); 105 } 106 107 static void e1000e_pci_destructor(QOSGraphObject *obj) 108 { 109 QE1000E_PCI *epci = (QE1000E_PCI *) obj; 110 qpci_iounmap(&epci->pci_dev, epci->mac_regs); 111 qpci_msix_disable(&epci->pci_dev); 112 } 113 114 static void e1000e_pci_start_hw(QOSGraphObject *obj) 115 { 116 QE1000E_PCI *d = (QE1000E_PCI *) obj; 117 uint32_t val; 118 119 /* Enable the device */ 120 qpci_device_enable(&d->pci_dev); 121 122 /* Reset the device */ 123 val = e1000e_macreg_read(&d->e1000e, E1000_CTRL); 124 e1000e_macreg_write(&d->e1000e, E1000_CTRL, val | E1000_CTRL_RST); 125 126 /* Enable and configure MSI-X */ 127 qpci_msix_enable(&d->pci_dev); 128 e1000e_macreg_write(&d->e1000e, E1000_IVAR, E1000E_IVAR_TEST_CFG); 129 130 /* Check the device status - link and speed */ 131 val = e1000e_macreg_read(&d->e1000e, E1000_STATUS); 132 g_assert_cmphex(val & (E1000_STATUS_LU | E1000_STATUS_LAN_INIT_DONE), 133 ==, E1000_STATUS_LU | E1000_STATUS_LAN_INIT_DONE); 134 135 /* Initialize TX/RX logic */ 136 e1000e_macreg_write(&d->e1000e, E1000_RCTL, 0); 137 e1000e_macreg_write(&d->e1000e, E1000_TCTL, 0); 138 139 /* Notify the device that the driver is ready */ 140 val = e1000e_macreg_read(&d->e1000e, E1000_CTRL_EXT); 141 e1000e_macreg_write(&d->e1000e, E1000_CTRL_EXT, 142 val | E1000_CTRL_EXT_DRV_LOAD); 143 144 e1000e_macreg_write(&d->e1000e, E1000_TDBAL, 145 (uint32_t) d->e1000e.tx_ring); 146 e1000e_macreg_write(&d->e1000e, E1000_TDBAH, 147 (uint32_t) (d->e1000e.tx_ring >> 32)); 148 e1000e_macreg_write(&d->e1000e, E1000E_TDLEN, E1000E_RING_LEN); 149 e1000e_macreg_write(&d->e1000e, E1000E_TDT, 0); 150 e1000e_macreg_write(&d->e1000e, E1000_TDH, 0); 151 152 /* Enable transmit */ 153 e1000e_macreg_write(&d->e1000e, E1000_TCTL, E1000_TCTL_EN); 154 e1000e_macreg_write(&d->e1000e, E1000_RDBAL, 155 (uint32_t)d->e1000e.rx_ring); 156 e1000e_macreg_write(&d->e1000e, E1000_RDBAH, 157 (uint32_t)(d->e1000e.rx_ring >> 32)); 158 e1000e_macreg_write(&d->e1000e, E1000E_RDLEN, E1000E_RING_LEN); 159 e1000e_macreg_write(&d->e1000e, E1000E_RDT, 0); 160 e1000e_macreg_write(&d->e1000e, E1000_RDH, 0); 161 162 /* Enable receive */ 163 e1000e_macreg_write(&d->e1000e, E1000_RFCTL, E1000_RFCTL_EXTEN); 164 e1000e_macreg_write(&d->e1000e, E1000_RCTL, E1000_RCTL_EN | 165 E1000_RCTL_UPE | 166 E1000_RCTL_MPE); 167 168 /* Enable all interrupts */ 169 e1000e_macreg_write(&d->e1000e, E1000_IMS, 0xFFFFFFFF); 170 171 } 172 173 static void *e1000e_pci_get_driver(void *obj, const char *interface) 174 { 175 QE1000E_PCI *epci = obj; 176 if (!g_strcmp0(interface, "e1000e-if")) { 177 return &epci->e1000e; 178 } 179 180 /* implicit contains */ 181 if (!g_strcmp0(interface, "pci-device")) { 182 return &epci->pci_dev; 183 } 184 185 fprintf(stderr, "%s not present in e1000e\n", interface); 186 g_assert_not_reached(); 187 } 188 189 static void *e1000e_pci_create(void *pci_bus, QGuestAllocator *alloc, 190 void *addr) 191 { 192 QE1000E_PCI *d = g_new0(QE1000E_PCI, 1); 193 QPCIBus *bus = pci_bus; 194 QPCIAddress *address = addr; 195 196 qpci_device_foreach(bus, address->vendor_id, address->device_id, 197 e1000e_foreach_callback, &d->pci_dev); 198 199 /* Map BAR0 (mac registers) */ 200 d->mac_regs = qpci_iomap(&d->pci_dev, 0, NULL); 201 202 /* Allocate and setup TX ring */ 203 d->e1000e.tx_ring = guest_alloc(alloc, E1000E_RING_LEN); 204 g_assert(d->e1000e.tx_ring != 0); 205 206 /* Allocate and setup RX ring */ 207 d->e1000e.rx_ring = guest_alloc(alloc, E1000E_RING_LEN); 208 g_assert(d->e1000e.rx_ring != 0); 209 210 d->obj.get_driver = e1000e_pci_get_driver; 211 d->obj.start_hw = e1000e_pci_start_hw; 212 d->obj.destructor = e1000e_pci_destructor; 213 214 return &d->obj; 215 } 216 217 static void e1000e_register_nodes(void) 218 { 219 QPCIAddress addr = { 220 .vendor_id = 0x8086, 221 .device_id = 0x10D3, 222 }; 223 224 /* FIXME: every test using this node needs to setup a -netdev socket,id=hs0 225 * otherwise QEMU is not going to start */ 226 QOSGraphEdgeOptions opts = { 227 .extra_device_opts = "netdev=hs0", 228 }; 229 add_qpci_address(&opts, &addr); 230 231 qos_node_create_driver("e1000e", e1000e_pci_create); 232 qos_node_consumes("e1000e", "pci-bus", &opts); 233 } 234 235 libqos_init(e1000e_register_nodes); 236