You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virtio-net.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* virtio-net.c - etherboot driver for virtio network interface
  2. *
  3. * (c) Copyright 2008 Bull S.A.S.
  4. *
  5. * Author: Laurent Vivier <Laurent.Vivier@bull.net>
  6. *
  7. * some parts from Linux Virtio PCI driver
  8. *
  9. * Copyright IBM Corp. 2007
  10. * Authors: Anthony Liguori <aliguori@us.ibm.com>
  11. *
  12. * some parts from Linux Virtio Ring
  13. *
  14. * Copyright Rusty Russell IBM Corporation 2007
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  17. * See the COPYING file in the top-level directory.
  18. *
  19. *
  20. */
  21. #include "etherboot.h"
  22. #include "nic.h"
  23. #include "virtio-ring.h"
  24. #include "virtio-pci.h"
  25. #include "virtio-net.h"
  26. #define BUG() do { \
  27. printf("BUG: failure at %s:%d/%s()!\n", \
  28. __FILE__, __LINE__, __FUNCTION__); \
  29. while(1); \
  30. } while (0)
  31. #define BUG_ON(condition) do { if (condition) BUG(); } while (0)
  32. /* Ethernet header */
  33. struct eth_hdr {
  34. unsigned char dst_addr[ETH_ALEN];
  35. unsigned char src_addr[ETH_ALEN];
  36. unsigned short type;
  37. };
  38. struct eth_frame {
  39. struct eth_hdr hdr;
  40. unsigned char data[ETH_FRAME_LEN];
  41. };
  42. typedef unsigned char virtio_queue_t[PAGE_MASK + vring_size(MAX_QUEUE_NUM)];
  43. /* TX: virtio header and eth buffer */
  44. static struct virtio_net_hdr tx_virtio_hdr;
  45. static struct eth_frame tx_eth_frame;
  46. /* RX: virtio headers and buffers */
  47. #define RX_BUF_NB 6
  48. static struct virtio_net_hdr rx_hdr[RX_BUF_NB];
  49. static unsigned char rx_buffer[RX_BUF_NB][ETH_FRAME_LEN];
  50. /* virtio queues and vrings */
  51. enum {
  52. RX_INDEX = 0,
  53. TX_INDEX,
  54. QUEUE_NB
  55. };
  56. static virtio_queue_t queue[QUEUE_NB];
  57. static struct vring vring[QUEUE_NB];
  58. static u16 free_head[QUEUE_NB];
  59. static u16 last_used_idx[QUEUE_NB];
  60. static u16 vdata[QUEUE_NB][MAX_QUEUE_NUM];
  61. /*
  62. * Virtio PCI interface
  63. *
  64. */
  65. static int vp_find_vq(struct nic *nic, int queue_index)
  66. {
  67. struct vring * vr = &vring[queue_index];
  68. u16 num;
  69. /* select the queue */
  70. outw(queue_index, nic->ioaddr + VIRTIO_PCI_QUEUE_SEL);
  71. /* check if the queue is available */
  72. num = inw(nic->ioaddr + VIRTIO_PCI_QUEUE_NUM);
  73. if (!num) {
  74. printf("ERROR: queue size is 0\n");
  75. return -1;
  76. }
  77. if (num > MAX_QUEUE_NUM) {
  78. printf("ERROR: queue size %d > %d\n", num, MAX_QUEUE_NUM);
  79. return -1;
  80. }
  81. /* check if the queue is already active */
  82. if (inl(nic->ioaddr + VIRTIO_PCI_QUEUE_PFN)) {
  83. printf("ERROR: queue already active\n");
  84. return -1;
  85. }
  86. /* initialize the queue */
  87. vring_init(vr, num, (unsigned char*)&queue[queue_index]);
  88. /* activate the queue
  89. *
  90. * NOTE: vr->desc is initialized by vring_init()
  91. */
  92. outl((unsigned long)virt_to_phys(vr->desc) >> PAGE_SHIFT,
  93. nic->ioaddr + VIRTIO_PCI_QUEUE_PFN);
  94. return num;
  95. }
  96. /*
  97. * Virtual ring management
  98. *
  99. */
  100. static void vring_enable_cb(int queue_index)
  101. {
  102. vring[queue_index].avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
  103. }
  104. static void vring_disable_cb(int queue_index)
  105. {
  106. vring[queue_index].avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
  107. }
  108. /*
  109. * vring_free
  110. *
  111. * put at the begin of the free list the current desc[head]
  112. */
  113. static void vring_detach(int queue_index, unsigned int head)
  114. {
  115. struct vring *vr = &vring[queue_index];
  116. unsigned int i;
  117. /* find end of given descriptor */
  118. i = head;
  119. while (vr->desc[i].flags & VRING_DESC_F_NEXT)
  120. i = vr->desc[i].next;
  121. /* link it with free list and point to it */
  122. vr->desc[i].next = free_head[queue_index];
  123. wmb();
  124. free_head[queue_index] = head;
  125. }
  126. /*
  127. * vring_more_used
  128. *
  129. * is there some used buffers ?
  130. *
  131. */
  132. static inline int vring_more_used(int queue_index)
  133. {
  134. wmb();
  135. return last_used_idx[queue_index] != vring[queue_index].used->idx;
  136. }
  137. /*
  138. * vring_get_buf
  139. *
  140. * get a buffer from the used list
  141. *
  142. */
  143. static int vring_get_buf(int queue_index, unsigned int *len)
  144. {
  145. struct vring *vr = &vring[queue_index];
  146. struct vring_used_elem *elem;
  147. u32 id;
  148. int ret;
  149. elem = &vr->used->ring[last_used_idx[queue_index] % vr->num];
  150. wmb();
  151. id = elem->id;
  152. if (len != NULL)
  153. *len = elem->len;
  154. ret = vdata[queue_index][id];
  155. vring_detach(queue_index, id);
  156. last_used_idx[queue_index]++;
  157. return ret;
  158. }
  159. static void vring_add_buf(int queue_index, int index, int num_added)
  160. {
  161. struct vring *vr = &vring[queue_index];
  162. int i, avail, head;
  163. BUG_ON(queue_index >= QUEUE_NB);
  164. head = free_head[queue_index];
  165. i = head;
  166. if (queue_index == TX_INDEX) {
  167. BUG_ON(index != 0);
  168. /* add header into vring */
  169. vr->desc[i].flags = VRING_DESC_F_NEXT;
  170. vr->desc[i].addr = (u64)virt_to_phys(&tx_virtio_hdr);
  171. vr->desc[i].len = sizeof(struct virtio_net_hdr);
  172. i = vr->desc[i].next;
  173. /* add frame buffer into vring */
  174. vr->desc[i].flags = 0;
  175. vr->desc[i].addr = (u64)virt_to_phys(&tx_eth_frame);
  176. vr->desc[i].len = ETH_FRAME_LEN;
  177. i = vr->desc[i].next;
  178. } else if (queue_index == RX_INDEX) {
  179. BUG_ON(index >= RX_BUF_NB);
  180. /* add header into vring */
  181. vr->desc[i].flags = VRING_DESC_F_NEXT|VRING_DESC_F_WRITE;
  182. vr->desc[i].addr = (u64)virt_to_phys(&rx_hdr[index]);
  183. vr->desc[i].len = sizeof(struct virtio_net_hdr);
  184. i = vr->desc[i].next;
  185. /* add frame buffer into vring */
  186. vr->desc[i].flags = VRING_DESC_F_WRITE;
  187. vr->desc[i].addr = (u64)virt_to_phys(&rx_buffer[index]);
  188. vr->desc[i].len = ETH_FRAME_LEN;
  189. i = vr->desc[i].next;
  190. }
  191. free_head[queue_index] = i;
  192. vdata[queue_index][head] = index;
  193. avail = (vr->avail->idx + num_added) % vr->num;
  194. vr->avail->ring[avail] = head;
  195. wmb();
  196. }
  197. static void vring_kick(struct nic *nic, int queue_index, int num_added)
  198. {
  199. struct vring *vr = &vring[queue_index];
  200. wmb();
  201. vr->avail->idx += num_added;
  202. mb();
  203. if (!(vr->used->flags & VRING_USED_F_NO_NOTIFY))
  204. vp_notify(nic, queue_index);
  205. }
  206. /*
  207. * virtnet_disable
  208. *
  209. * Turn off ethernet interface
  210. *
  211. */
  212. static void virtnet_disable(struct nic *nic)
  213. {
  214. int i;
  215. for (i = 0; i < QUEUE_NB; i++) {
  216. vring_disable_cb(i);
  217. vp_del_vq(nic, i);
  218. }
  219. vp_reset(nic);
  220. }
  221. /*
  222. * virtnet_poll
  223. *
  224. * Wait for a frame
  225. *
  226. * return true if there is a packet ready to read
  227. *
  228. * nic->packet should contain data on return
  229. * nic->packetlen should contain length of data
  230. *
  231. */
  232. static int virtnet_poll(struct nic *nic, int retrieve)
  233. {
  234. unsigned int len;
  235. u16 token;
  236. struct virtio_net_hdr *hdr;
  237. if (!vring_more_used(RX_INDEX))
  238. return 0;
  239. if (!retrieve)
  240. return 1;
  241. token = vring_get_buf(RX_INDEX, &len);
  242. BUG_ON(len > sizeof(struct virtio_net_hdr) + ETH_FRAME_LEN);
  243. hdr = &rx_hdr[token]; /* FIXME: check flags */
  244. len -= sizeof(struct virtio_net_hdr);
  245. nic->packetlen = len;
  246. memcpy(nic->packet, (char *)rx_buffer[token], nic->packetlen);
  247. /* add buffer to desc */
  248. vring_add_buf(RX_INDEX, token, 0);
  249. vring_kick(nic, RX_INDEX, 1);
  250. return 1;
  251. }
  252. /*
  253. *
  254. * virtnet_transmit
  255. *
  256. * Transmit a frame
  257. *
  258. */
  259. static void virtnet_transmit(struct nic *nic, const char *destaddr,
  260. unsigned int type, unsigned int len, const char *data)
  261. {
  262. /*
  263. * from http://www.etherboot.org/wiki/dev/devmanual :
  264. * "You do not need more than one transmit buffer."
  265. */
  266. /* FIXME: initialize header according to vp_get_features() */
  267. tx_virtio_hdr.flags = 0;
  268. tx_virtio_hdr.csum_offset = 0;
  269. tx_virtio_hdr.csum_start = 0;
  270. tx_virtio_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  271. tx_virtio_hdr.gso_size = 0;
  272. tx_virtio_hdr.hdr_len = 0;
  273. /* add ethernet frame into vring */
  274. BUG_ON(len > sizeof(tx_eth_frame.data));
  275. memcpy(tx_eth_frame.hdr.dst_addr, destaddr, ETH_ALEN);
  276. memcpy(tx_eth_frame.hdr.src_addr, nic->node_addr, ETH_ALEN);
  277. tx_eth_frame.hdr.type = htons(type);
  278. memcpy(tx_eth_frame.data, data, len);
  279. vring_add_buf(TX_INDEX, 0, 0);
  280. /*
  281. * http://www.etherboot.org/wiki/dev/devmanual
  282. *
  283. * "You should ensure the packet is fully transmitted
  284. * before returning from this routine"
  285. */
  286. while (vring_more_used(TX_INDEX)) {
  287. mb();
  288. udelay(10);
  289. }
  290. vring_kick(nic, TX_INDEX, 1);
  291. /* free desc */
  292. (void)vring_get_buf(TX_INDEX, NULL);
  293. }
  294. static void virtnet_irq(struct nic *nic __unused, irq_action_t action)
  295. {
  296. switch ( action ) {
  297. case DISABLE :
  298. vring_disable_cb(RX_INDEX);
  299. vring_disable_cb(TX_INDEX);
  300. break;
  301. case ENABLE :
  302. vring_enable_cb(RX_INDEX);
  303. vring_enable_cb(TX_INDEX);
  304. break;
  305. case FORCE :
  306. break;
  307. }
  308. }
  309. static void provide_buffers(struct nic *nic)
  310. {
  311. int i;
  312. for (i = 0; i < RX_BUF_NB; i++)
  313. vring_add_buf(RX_INDEX, i, i);
  314. /* nofify */
  315. vring_kick(nic, RX_INDEX, i);
  316. }
  317. static struct nic_operations virtnet_operations = {
  318. .connect = dummy_connect,
  319. .poll = virtnet_poll,
  320. .transmit = virtnet_transmit,
  321. .irq = virtnet_irq,
  322. };
  323. /*
  324. * virtnet_probe
  325. *
  326. * Look for a virtio network adapter
  327. *
  328. */
  329. static int virtnet_probe(struct nic *nic, struct pci_device *pci)
  330. {
  331. u32 features;
  332. int i;
  333. /* Mask the bit that says "this is an io addr" */
  334. nic->ioaddr = pci->ioaddr & ~3;
  335. /* Copy IRQ from PCI information */
  336. nic->irqno = pci->irq;
  337. printf("I/O address 0x%08x, IRQ #%d\n", nic->ioaddr, nic->irqno);
  338. adjust_pci_device(pci);
  339. vp_reset(nic);
  340. features = vp_get_features(nic);
  341. if (features & (1 << VIRTIO_NET_F_MAC)) {
  342. vp_get(nic, offsetof(struct virtio_net_config, mac),
  343. nic->node_addr, ETH_ALEN);
  344. printf("MAC address ");
  345. for (i = 0; i < ETH_ALEN; i++) {
  346. printf("%02x%c", nic->node_addr[i],
  347. (i == ETH_ALEN - 1) ? '\n' : ':');
  348. }
  349. }
  350. /* initialize emit/receive queue */
  351. for (i = 0; i < QUEUE_NB; i++) {
  352. free_head[i] = 0;
  353. last_used_idx[i] = 0;
  354. memset((char*)&queue[i], 0, sizeof(queue[i]));
  355. if (vp_find_vq(nic, i) == -1)
  356. printf("Cannot register queue #%d\n", i);
  357. }
  358. /* provide some receive buffers */
  359. provide_buffers(nic);
  360. /* define NIC interface */
  361. nic->nic_op = &virtnet_operations;
  362. /* driver is ready */
  363. vp_set_features(nic, features & (1 << VIRTIO_NET_F_MAC));
  364. vp_set_status(nic, VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK);
  365. return 1;
  366. }
  367. static struct pci_device_id virtnet_nics[] = {
  368. PCI_ROM(0x1af4, 0x1000, "virtio-net", "Virtio Network Interface"),
  369. };
  370. PCI_DRIVER ( virtnet_driver, virtnet_nics, PCI_NO_CLASS );
  371. DRIVER ( "VIRTIO-NET", nic_driver, pci_driver, virtnet_driver,
  372. virtnet_probe, virtnet_disable );