1 /* 2 * Virtio-net driver for the s390-ccw firmware 3 * 4 * Copyright 2017 Thomas Huth, Red Hat Inc. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License as published by the 8 * Free Software Foundation; either version 2 of the License, or (at your 9 * option) any later version. 10 */ 11 12 #include <stdint.h> 13 #include <stdbool.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <unistd.h> 18 #include <sys/socket.h> 19 #include <ethernet.h> 20 #include "s390-ccw.h" 21 #include "virtio.h" 22 #include "s390-time.h" 23 24 #ifndef DEBUG_VIRTIO_NET 25 #define DEBUG_VIRTIO_NET 0 26 #endif 27 28 #define VIRTIO_NET_F_MAC_BIT (1 << 5) 29 30 #define VQ_RX 0 /* Receive queue */ 31 #define VQ_TX 1 /* Transmit queue */ 32 33 struct VirtioNetHdr { 34 uint8_t flags; 35 uint8_t gso_type; 36 uint16_t hdr_len; 37 uint16_t gso_size; 38 uint16_t csum_start; 39 uint16_t csum_offset; 40 /*uint16_t num_buffers;*/ /* Only with VIRTIO_NET_F_MRG_RXBUF or VIRTIO1 */ 41 }; 42 typedef struct VirtioNetHdr VirtioNetHdr; 43 44 static uint16_t rx_last_idx; /* Last index in receive queue "used" ring */ 45 46 int virtio_net_init(void *mac_addr) 47 { 48 VDev *vdev = virtio_get_device(); 49 VRing *rxvq = &vdev->vrings[VQ_RX]; 50 void *buf; 51 int i; 52 53 vdev->guest_features[0] = VIRTIO_NET_F_MAC_BIT; 54 virtio_setup_ccw(vdev); 55 56 IPL_assert(vdev->guest_features[0] & VIRTIO_NET_F_MAC_BIT, 57 "virtio-net device does not support the MAC address feature"); 58 memcpy(mac_addr, vdev->config.net.mac, ETH_ALEN); 59 60 for (i = 0; i < 64; i++) { 61 buf = malloc(ETH_MTU_SIZE + sizeof(VirtioNetHdr)); 62 IPL_assert(buf != NULL, "Can not allocate memory for receive buffers"); 63 vring_send_buf(rxvq, buf, ETH_MTU_SIZE + sizeof(VirtioNetHdr), 64 VRING_DESC_F_WRITE); 65 } 66 vring_notify(rxvq); 67 68 return 0; 69 } 70 71 int send(int fd, const void *buf, int len, int flags) 72 { 73 VirtioNetHdr tx_hdr; 74 VDev *vdev = virtio_get_device(); 75 VRing *txvq = &vdev->vrings[VQ_TX]; 76 77 /* Set up header - we do not use anything special, so simply clear it */ 78 memset(&tx_hdr, 0, sizeof(tx_hdr)); 79 80 vring_send_buf(txvq, &tx_hdr, sizeof(tx_hdr), VRING_DESC_F_NEXT); 81 vring_send_buf(txvq, (void *)buf, len, VRING_HIDDEN_IS_CHAIN); 82 while (!vr_poll(txvq)) { 83 yield(); 84 } 85 if (drain_irqs(txvq->schid)) { 86 puts("send: drain irqs failed"); 87 return -1; 88 } 89 90 return len; 91 } 92 93 int recv(int fd, void *buf, int maxlen, int flags) 94 { 95 VDev *vdev = virtio_get_device(); 96 VRing *rxvq = &vdev->vrings[VQ_RX]; 97 int len, id; 98 uint8_t *pkt; 99 100 if (rx_last_idx == rxvq->used->idx) { 101 return 0; 102 } 103 104 len = rxvq->used->ring[rx_last_idx % rxvq->num].len - sizeof(VirtioNetHdr); 105 if (len > maxlen) { 106 puts("virtio-net: Receive buffer too small"); 107 len = maxlen; 108 } 109 id = rxvq->used->ring[rx_last_idx % rxvq->num].id % rxvq->num; 110 pkt = (uint8_t *)(rxvq->desc[id].addr + sizeof(VirtioNetHdr)); 111 112 #if DEBUG_VIRTIO_NET /* Dump packet */ 113 int i; 114 printf("\nbuf %p: len=%i\n", (void *)rxvq->desc[id].addr, len); 115 for (i = 0; i < 64; i++) { 116 printf(" %02x", pkt[i]); 117 if ((i % 16) == 15) { 118 printf("\n"); 119 } 120 } 121 printf("\n"); 122 #endif 123 124 /* Copy data to destination buffer */ 125 memcpy(buf, pkt, len); 126 127 /* Mark buffer as available to the host again */ 128 rxvq->avail->ring[rxvq->avail->idx % rxvq->num] = id; 129 rxvq->avail->idx = rxvq->avail->idx + 1; 130 vring_notify(rxvq); 131 132 /* Move index to next entry */ 133 rx_last_idx = rx_last_idx + 1; 134 135 return len; 136 } 137