1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "hw.h"
28#include "net/net.h"
29#include "hw/qdev.h"
30#include "hw/spapr.h"
31#include "hw/spapr_vio.h"
32
33#include <libfdt.h>
34
35#define ETH_ALEN 6
36#define MAX_PACKET_SIZE 65536
37
38
39
40#ifdef DEBUG
41#define dprintf(fmt...) do { fprintf(stderr, fmt); } while (0)
42#else
43#define dprintf(fmt...)
44#endif
45
46
47
48
49
50typedef uint64_t vlan_bd_t;
51
52#define VLAN_BD_VALID 0x8000000000000000ULL
53#define VLAN_BD_TOGGLE 0x4000000000000000ULL
54#define VLAN_BD_NO_CSUM 0x0200000000000000ULL
55#define VLAN_BD_CSUM_GOOD 0x0100000000000000ULL
56#define VLAN_BD_LEN_MASK 0x00ffffff00000000ULL
57#define VLAN_BD_LEN(bd) (((bd) & VLAN_BD_LEN_MASK) >> 32)
58#define VLAN_BD_ADDR_MASK 0x00000000ffffffffULL
59#define VLAN_BD_ADDR(bd) ((bd) & VLAN_BD_ADDR_MASK)
60
61#define VLAN_VALID_BD(addr, len) (VLAN_BD_VALID | \
62 (((len) << 32) & VLAN_BD_LEN_MASK) | \
63 (addr & VLAN_BD_ADDR_MASK))
64
65#define VLAN_RXQC_TOGGLE 0x80
66#define VLAN_RXQC_VALID 0x40
67#define VLAN_RXQC_NO_CSUM 0x02
68#define VLAN_RXQC_CSUM_GOOD 0x01
69
70#define VLAN_RQ_ALIGNMENT 16
71#define VLAN_RXQ_BD_OFF 0
72#define VLAN_FILTER_BD_OFF 8
73#define VLAN_RX_BDS_OFF 16
74#define VLAN_MAX_BUFS ((SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF) / 8)
75
76typedef struct VIOsPAPRVLANDevice {
77 VIOsPAPRDevice sdev;
78 NICConf nicconf;
79 NICState *nic;
80 int isopen;
81 target_ulong buf_list;
82 int add_buf_ptr, use_buf_ptr, rx_bufs;
83 target_ulong rxq_ptr;
84} VIOsPAPRVLANDevice;
85
86static int spapr_vlan_can_receive(NetClientState *nc)
87{
88 VIOsPAPRVLANDevice *dev = qemu_get_nic_opaque(nc);
89
90 return (dev->isopen && dev->rx_bufs > 0);
91}
92
93static ssize_t spapr_vlan_receive(NetClientState *nc, const uint8_t *buf,
94 size_t size)
95{
96 VIOsPAPRDevice *sdev = qemu_get_nic_opaque(nc);
97 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
98 vlan_bd_t rxq_bd = vio_ldq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF);
99 vlan_bd_t bd;
100 int buf_ptr = dev->use_buf_ptr;
101 uint64_t handle;
102 uint8_t control;
103
104 dprintf("spapr_vlan_receive() [%s] rx_bufs=%d\n", sdev->qdev.id,
105 dev->rx_bufs);
106
107 if (!dev->isopen) {
108 return -1;
109 }
110
111 if (!dev->rx_bufs) {
112 return -1;
113 }
114
115 do {
116 buf_ptr += 8;
117 if (buf_ptr >= SPAPR_TCE_PAGE_SIZE) {
118 buf_ptr = VLAN_RX_BDS_OFF;
119 }
120
121 bd = vio_ldq(sdev, dev->buf_list + buf_ptr);
122 dprintf("use_buf_ptr=%d bd=0x%016llx\n",
123 buf_ptr, (unsigned long long)bd);
124 } while ((!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8)))
125 && (buf_ptr != dev->use_buf_ptr));
126
127 if (!(bd & VLAN_BD_VALID) || (VLAN_BD_LEN(bd) < (size + 8))) {
128
129 return -1;
130 }
131
132
133 dev->rx_bufs--;
134 dev->use_buf_ptr = buf_ptr;
135 vio_stq(sdev, dev->buf_list + dev->use_buf_ptr, 0);
136
137 dprintf("Found buffer: ptr=%d num=%d\n", dev->use_buf_ptr, dev->rx_bufs);
138
139
140 if (spapr_vio_dma_write(sdev, VLAN_BD_ADDR(bd) + 8, buf, size) < 0) {
141 return -1;
142 }
143
144 dprintf("spapr_vlan_receive: DMA write completed\n");
145
146
147 control = VLAN_RXQC_TOGGLE | VLAN_RXQC_VALID;
148 if (rxq_bd & VLAN_BD_TOGGLE) {
149 control ^= VLAN_RXQC_TOGGLE;
150 }
151
152 handle = vio_ldq(sdev, VLAN_BD_ADDR(bd));
153 vio_stq(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 8, handle);
154 vio_stl(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 4, size);
155 vio_sth(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr + 2, 8);
156 vio_stb(sdev, VLAN_BD_ADDR(rxq_bd) + dev->rxq_ptr, control);
157
158 dprintf("wrote rxq entry (ptr=0x%llx): 0x%016llx 0x%016llx\n",
159 (unsigned long long)dev->rxq_ptr,
160 (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
161 dev->rxq_ptr),
162 (unsigned long long)vio_ldq(sdev, VLAN_BD_ADDR(rxq_bd) +
163 dev->rxq_ptr + 8));
164
165 dev->rxq_ptr += 16;
166 if (dev->rxq_ptr >= VLAN_BD_LEN(rxq_bd)) {
167 dev->rxq_ptr = 0;
168 vio_stq(sdev, dev->buf_list + VLAN_RXQ_BD_OFF, rxq_bd ^ VLAN_BD_TOGGLE);
169 }
170
171 if (sdev->signal_state & 1) {
172 qemu_irq_pulse(spapr_vio_qirq(sdev));
173 }
174
175 return size;
176}
177
178static NetClientInfo net_spapr_vlan_info = {
179 .type = NET_CLIENT_OPTIONS_KIND_NIC,
180 .size = sizeof(NICState),
181 .can_receive = spapr_vlan_can_receive,
182 .receive = spapr_vlan_receive,
183};
184
185static void spapr_vlan_reset(VIOsPAPRDevice *sdev)
186{
187 VIOsPAPRVLANDevice *dev = DO_UPCAST(VIOsPAPRVLANDevice, sdev, sdev);
188
189 dev->buf_list = 0;
190 dev->rx_bufs = 0;
191 dev->isopen = 0;
192}
193
194static int spapr_vlan_init(VIOsPAPRDevice *sdev)
195{
196 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
197
198 qemu_macaddr_default_if_unset(&dev->nicconf.macaddr);
199
200 dev->nic = qemu_new_nic(&net_spapr_vlan_info, &dev->nicconf,
201 object_get_typename(OBJECT(sdev)), sdev->qdev.id, dev);
202 qemu_format_nic_info_str(qemu_get_queue(dev->nic), dev->nicconf.macaddr.a);
203
204 return 0;
205}
206
207void spapr_vlan_create(VIOsPAPRBus *bus, NICInfo *nd)
208{
209 DeviceState *dev;
210
211 dev = qdev_create(&bus->bus, "spapr-vlan");
212
213 qdev_set_nic_properties(dev, nd);
214
215 qdev_init_nofail(dev);
216}
217
218static int spapr_vlan_devnode(VIOsPAPRDevice *dev, void *fdt, int node_off)
219{
220 VIOsPAPRVLANDevice *vdev = (VIOsPAPRVLANDevice *)dev;
221 uint8_t padded_mac[8] = {0, 0};
222 int ret;
223
224
225
226
227
228
229
230
231
232
233
234 memcpy(&padded_mac[2], &vdev->nicconf.macaddr, ETH_ALEN);
235 ret = fdt_setprop(fdt, node_off, "local-mac-address",
236 padded_mac, sizeof(padded_mac));
237 if (ret < 0) {
238 return ret;
239 }
240
241 ret = fdt_setprop_cell(fdt, node_off, "ibm,mac-address-filters", 0);
242 if (ret < 0) {
243 return ret;
244 }
245
246 return 0;
247}
248
249static int check_bd(VIOsPAPRVLANDevice *dev, vlan_bd_t bd,
250 target_ulong alignment)
251{
252 if ((VLAN_BD_ADDR(bd) % alignment)
253 || (VLAN_BD_LEN(bd) % alignment)) {
254 return -1;
255 }
256
257 if (!spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
258 VLAN_BD_LEN(bd), DMA_DIRECTION_FROM_DEVICE)
259 || !spapr_vio_dma_valid(&dev->sdev, VLAN_BD_ADDR(bd),
260 VLAN_BD_LEN(bd), DMA_DIRECTION_TO_DEVICE)) {
261 return -1;
262 }
263
264 return 0;
265}
266
267static target_ulong h_register_logical_lan(PowerPCCPU *cpu,
268 sPAPREnvironment *spapr,
269 target_ulong opcode,
270 target_ulong *args)
271{
272 target_ulong reg = args[0];
273 target_ulong buf_list = args[1];
274 target_ulong rec_queue = args[2];
275 target_ulong filter_list = args[3];
276 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
277 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
278 vlan_bd_t filter_list_bd;
279
280 if (!dev) {
281 return H_PARAMETER;
282 }
283
284 if (dev->isopen) {
285 hcall_dprintf("H_REGISTER_LOGICAL_LAN called twice without "
286 "H_FREE_LOGICAL_LAN\n");
287 return H_RESOURCE;
288 }
289
290 if (check_bd(dev, VLAN_VALID_BD(buf_list, SPAPR_TCE_PAGE_SIZE),
291 SPAPR_TCE_PAGE_SIZE) < 0) {
292 hcall_dprintf("Bad buf_list 0x" TARGET_FMT_lx "\n", buf_list);
293 return H_PARAMETER;
294 }
295
296 filter_list_bd = VLAN_VALID_BD(filter_list, SPAPR_TCE_PAGE_SIZE);
297 if (check_bd(dev, filter_list_bd, SPAPR_TCE_PAGE_SIZE) < 0) {
298 hcall_dprintf("Bad filter_list 0x" TARGET_FMT_lx "\n", filter_list);
299 return H_PARAMETER;
300 }
301
302 if (!(rec_queue & VLAN_BD_VALID)
303 || (check_bd(dev, rec_queue, VLAN_RQ_ALIGNMENT) < 0)) {
304 hcall_dprintf("Bad receive queue\n");
305 return H_PARAMETER;
306 }
307
308 dev->buf_list = buf_list;
309 sdev->signal_state = 0;
310
311 rec_queue &= ~VLAN_BD_TOGGLE;
312
313
314 vio_stq(sdev, buf_list, rec_queue);
315 vio_stq(sdev, buf_list + 8, filter_list_bd);
316 spapr_vio_dma_set(sdev, buf_list + VLAN_RX_BDS_OFF, 0,
317 SPAPR_TCE_PAGE_SIZE - VLAN_RX_BDS_OFF);
318 dev->add_buf_ptr = VLAN_RX_BDS_OFF - 8;
319 dev->use_buf_ptr = VLAN_RX_BDS_OFF - 8;
320 dev->rx_bufs = 0;
321 dev->rxq_ptr = 0;
322
323
324 spapr_vio_dma_set(sdev, VLAN_BD_ADDR(rec_queue), 0, VLAN_BD_LEN(rec_queue));
325
326 dev->isopen = 1;
327 return H_SUCCESS;
328}
329
330
331static target_ulong h_free_logical_lan(PowerPCCPU *cpu, sPAPREnvironment *spapr,
332 target_ulong opcode, target_ulong *args)
333{
334 target_ulong reg = args[0];
335 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
336 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
337
338 if (!dev) {
339 return H_PARAMETER;
340 }
341
342 if (!dev->isopen) {
343 hcall_dprintf("H_FREE_LOGICAL_LAN called without "
344 "H_REGISTER_LOGICAL_LAN\n");
345 return H_RESOURCE;
346 }
347
348 spapr_vlan_reset(sdev);
349 return H_SUCCESS;
350}
351
352static target_ulong h_add_logical_lan_buffer(PowerPCCPU *cpu,
353 sPAPREnvironment *spapr,
354 target_ulong opcode,
355 target_ulong *args)
356{
357 target_ulong reg = args[0];
358 target_ulong buf = args[1];
359 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
360 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
361 vlan_bd_t bd;
362
363 dprintf("H_ADD_LOGICAL_LAN_BUFFER(0x" TARGET_FMT_lx
364 ", 0x" TARGET_FMT_lx ")\n", reg, buf);
365
366 if (!sdev) {
367 hcall_dprintf("Bad device\n");
368 return H_PARAMETER;
369 }
370
371 if ((check_bd(dev, buf, 4) < 0)
372 || (VLAN_BD_LEN(buf) < 16)) {
373 hcall_dprintf("Bad buffer enqueued\n");
374 return H_PARAMETER;
375 }
376
377 if (!dev->isopen || dev->rx_bufs >= VLAN_MAX_BUFS) {
378 return H_RESOURCE;
379 }
380
381 do {
382 dev->add_buf_ptr += 8;
383 if (dev->add_buf_ptr >= SPAPR_TCE_PAGE_SIZE) {
384 dev->add_buf_ptr = VLAN_RX_BDS_OFF;
385 }
386
387 bd = vio_ldq(sdev, dev->buf_list + dev->add_buf_ptr);
388 } while (bd & VLAN_BD_VALID);
389
390 vio_stq(sdev, dev->buf_list + dev->add_buf_ptr, buf);
391
392 dev->rx_bufs++;
393
394 dprintf("h_add_logical_lan_buffer(): Added buf ptr=%d rx_bufs=%d"
395 " bd=0x%016llx\n", dev->add_buf_ptr, dev->rx_bufs,
396 (unsigned long long)buf);
397
398 return H_SUCCESS;
399}
400
401static target_ulong h_send_logical_lan(PowerPCCPU *cpu, sPAPREnvironment *spapr,
402 target_ulong opcode, target_ulong *args)
403{
404 target_ulong reg = args[0];
405 target_ulong *bufs = args + 1;
406 target_ulong continue_token = args[7];
407 VIOsPAPRDevice *sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
408 VIOsPAPRVLANDevice *dev = (VIOsPAPRVLANDevice *)sdev;
409 unsigned total_len;
410 uint8_t *lbuf, *p;
411 int i, nbufs;
412 int ret;
413
414 dprintf("H_SEND_LOGICAL_LAN(0x" TARGET_FMT_lx ", <bufs>, 0x"
415 TARGET_FMT_lx ")\n", reg, continue_token);
416
417 if (!sdev) {
418 return H_PARAMETER;
419 }
420
421 dprintf("rxbufs = %d\n", dev->rx_bufs);
422
423 if (!dev->isopen) {
424 return H_DROPPED;
425 }
426
427 if (continue_token) {
428 return H_HARDWARE;
429 }
430
431 total_len = 0;
432 for (i = 0; i < 6; i++) {
433 dprintf(" buf desc: 0x" TARGET_FMT_lx "\n", bufs[i]);
434 if (!(bufs[i] & VLAN_BD_VALID)) {
435 break;
436 }
437 total_len += VLAN_BD_LEN(bufs[i]);
438 }
439
440 nbufs = i;
441 dprintf("h_send_logical_lan() %d buffers, total length 0x%x\n",
442 nbufs, total_len);
443
444 if (total_len == 0) {
445 return H_SUCCESS;
446 }
447
448 if (total_len > MAX_PACKET_SIZE) {
449
450 return H_RESOURCE;
451 }
452
453 lbuf = alloca(total_len);
454 p = lbuf;
455 for (i = 0; i < nbufs; i++) {
456 ret = spapr_vio_dma_read(sdev, VLAN_BD_ADDR(bufs[i]),
457 p, VLAN_BD_LEN(bufs[i]));
458 if (ret < 0) {
459 return ret;
460 }
461
462 p += VLAN_BD_LEN(bufs[i]);
463 }
464
465 qemu_send_packet(qemu_get_queue(dev->nic), lbuf, total_len);
466
467 return H_SUCCESS;
468}
469
470static target_ulong h_multicast_ctrl(PowerPCCPU *cpu, sPAPREnvironment *spapr,
471 target_ulong opcode, target_ulong *args)
472{
473 target_ulong reg = args[0];
474 VIOsPAPRDevice *dev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
475
476 if (!dev) {
477 return H_PARAMETER;
478 }
479
480 return H_SUCCESS;
481}
482
483static Property spapr_vlan_properties[] = {
484 DEFINE_SPAPR_PROPERTIES(VIOsPAPRVLANDevice, sdev),
485 DEFINE_NIC_PROPERTIES(VIOsPAPRVLANDevice, nicconf),
486 DEFINE_PROP_END_OF_LIST(),
487};
488
489static void spapr_vlan_class_init(ObjectClass *klass, void *data)
490{
491 DeviceClass *dc = DEVICE_CLASS(klass);
492 VIOsPAPRDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
493
494 k->init = spapr_vlan_init;
495 k->reset = spapr_vlan_reset;
496 k->devnode = spapr_vlan_devnode;
497 k->dt_name = "l-lan";
498 k->dt_type = "network";
499 k->dt_compatible = "IBM,l-lan";
500 k->signal_mask = 0x1;
501 dc->props = spapr_vlan_properties;
502 k->rtce_window_size = 0x10000000;
503}
504
505static const TypeInfo spapr_vlan_info = {
506 .name = "spapr-vlan",
507 .parent = TYPE_VIO_SPAPR_DEVICE,
508 .instance_size = sizeof(VIOsPAPRVLANDevice),
509 .class_init = spapr_vlan_class_init,
510};
511
512static void spapr_vlan_register_types(void)
513{
514 spapr_register_hypercall(H_REGISTER_LOGICAL_LAN, h_register_logical_lan);
515 spapr_register_hypercall(H_FREE_LOGICAL_LAN, h_free_logical_lan);
516 spapr_register_hypercall(H_SEND_LOGICAL_LAN, h_send_logical_lan);
517 spapr_register_hypercall(H_ADD_LOGICAL_LAN_BUFFER,
518 h_add_logical_lan_buffer);
519 spapr_register_hypercall(H_MULTICAST_CTRL, h_multicast_ctrl);
520 type_register_static(&spapr_vlan_info);
521}
522
523type_init(spapr_vlan_register_types)
524