Revert xen/io/ring.h of "Clean up a few header guard symbols"
[qemu/ar7.git] / hw / scsi / mptconfig.c
blob87a416a5cb5cbb808449c77ba8a505f7b82513b5
1 /*
2 * QEMU LSI SAS1068 Host Bus Adapter emulation - configuration pages
4 * Copyright (c) 2016 Red Hat, Inc.
6 * Author: Paolo Bonzini
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 #include "qemu/osdep.h"
19 #include "hw/hw.h"
20 #include "hw/pci/pci.h"
21 #include "hw/scsi/scsi.h"
23 #include "mptsas.h"
24 #include "mpi.h"
25 #include "trace.h"
27 /* Generic functions for marshaling and unmarshaling. */
29 #define repl1(x) x
30 #define repl2(x) x x
31 #define repl3(x) x x x
32 #define repl4(x) x x x x
33 #define repl5(x) x x x x x
34 #define repl6(x) x x x x x x
35 #define repl7(x) x x x x x x x
36 #define repl8(x) x x x x x x x x
38 #define repl(n, x) glue(repl, n)(x)
40 typedef union PackValue {
41 uint64_t ll;
42 char *str;
43 } PackValue;
45 static size_t vfill(uint8_t *data, size_t size, const char *fmt, va_list ap)
47 size_t ofs;
48 PackValue val;
49 const char *p;
51 ofs = 0;
52 p = fmt;
53 while (*p) {
54 memset(&val, 0, sizeof(val));
55 switch (*p) {
56 case '*':
57 p++;
58 break;
59 case 'b':
60 case 'w':
61 case 'l':
62 val.ll = va_arg(ap, int);
63 break;
64 case 'q':
65 val.ll = va_arg(ap, int64_t);
66 break;
67 case 's':
68 val.str = va_arg(ap, void *);
69 break;
71 switch (*p++) {
72 case 'b':
73 if (data) {
74 stb_p(data + ofs, val.ll);
76 ofs++;
77 break;
78 case 'w':
79 if (data) {
80 stw_le_p(data + ofs, val.ll);
82 ofs += 2;
83 break;
84 case 'l':
85 if (data) {
86 stl_le_p(data + ofs, val.ll);
88 ofs += 4;
89 break;
90 case 'q':
91 if (data) {
92 stq_le_p(data + ofs, val.ll);
94 ofs += 8;
95 break;
96 case 's':
98 int cnt = atoi(p);
99 if (data) {
100 if (val.str) {
101 strncpy((void *)data + ofs, val.str, cnt);
102 } else {
103 memset((void *)data + ofs, 0, cnt);
106 ofs += cnt;
107 break;
112 return ofs;
115 static size_t vpack(uint8_t **p_data, const char *fmt, va_list ap1)
117 size_t size = 0;
118 uint8_t *data = NULL;
120 if (p_data) {
121 va_list ap2;
123 va_copy(ap2, ap1);
124 size = vfill(NULL, 0, fmt, ap2);
125 *p_data = data = g_malloc(size);
126 va_end(ap2);
128 return vfill(data, size, fmt, ap1);
131 static size_t fill(uint8_t *data, size_t size, const char *fmt, ...)
133 va_list ap;
134 size_t ret;
136 va_start(ap, fmt);
137 ret = vfill(data, size, fmt, ap);
138 va_end(ap);
140 return ret;
143 /* Functions to build the page header and fill in the length, always used
144 * through the macros.
147 #define MPTSAS_CONFIG_PACK(number, type, version, fmt, ...) \
148 mptsas_config_pack(data, "b*bbb" fmt, version, number, type, \
149 ## __VA_ARGS__)
151 static size_t mptsas_config_pack(uint8_t **data, const char *fmt, ...)
153 va_list ap;
154 size_t ret;
156 va_start(ap, fmt);
157 ret = vpack(data, fmt, ap);
158 va_end(ap);
160 if (data) {
161 assert(ret / 4 < 256 && (ret % 4) == 0);
162 stb_p(*data + 1, ret / 4);
164 return ret;
167 #define MPTSAS_CONFIG_PACK_EXT(number, type, version, fmt, ...) \
168 mptsas_config_pack_ext(data, "b*bbb*wb*b" fmt, version, number, \
169 MPI_CONFIG_PAGETYPE_EXTENDED, type, ## __VA_ARGS__)
171 static size_t mptsas_config_pack_ext(uint8_t **data, const char *fmt, ...)
173 va_list ap;
174 size_t ret;
176 va_start(ap, fmt);
177 ret = vpack(data, fmt, ap);
178 va_end(ap);
180 if (data) {
181 assert(ret < 65536 && (ret % 4) == 0);
182 stw_le_p(*data + 4, ret / 4);
184 return ret;
187 /* Manufacturing pages */
189 static
190 size_t mptsas_config_manufacturing_0(MPTSASState *s, uint8_t **data, int address)
192 return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
193 "s16s8s16s16s16",
194 "QEMU MPT Fusion",
195 "2.5",
196 "QEMU MPT Fusion",
197 "QEMU",
198 "0000111122223333");
201 static
202 size_t mptsas_config_manufacturing_1(MPTSASState *s, uint8_t **data, int address)
204 /* VPD - all zeros */
205 return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
206 "*s256");
209 static
210 size_t mptsas_config_manufacturing_2(MPTSASState *s, uint8_t **data, int address)
212 PCIDeviceClass *pcic = PCI_DEVICE_GET_CLASS(s);
213 return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
214 "wb*b*l",
215 pcic->device_id, pcic->revision);
218 static
219 size_t mptsas_config_manufacturing_3(MPTSASState *s, uint8_t **data, int address)
221 PCIDeviceClass *pcic = PCI_DEVICE_GET_CLASS(s);
222 return MPTSAS_CONFIG_PACK(3, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
223 "wb*b*l",
224 pcic->device_id, pcic->revision);
227 static
228 size_t mptsas_config_manufacturing_4(MPTSASState *s, uint8_t **data, int address)
230 /* All zeros */
231 return MPTSAS_CONFIG_PACK(4, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x05,
232 "*l*b*b*b*b*b*b*w*s56*l*l*l*l*l*l"
233 "*b*b*w*b*b*w*l*l");
236 static
237 size_t mptsas_config_manufacturing_5(MPTSASState *s, uint8_t **data, int address)
239 return MPTSAS_CONFIG_PACK(5, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x02,
240 "q*b*b*w*l*l", s->sas_addr);
243 static
244 size_t mptsas_config_manufacturing_6(MPTSASState *s, uint8_t **data, int address)
246 return MPTSAS_CONFIG_PACK(6, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
247 "*l");
250 static
251 size_t mptsas_config_manufacturing_7(MPTSASState *s, uint8_t **data, int address)
253 return MPTSAS_CONFIG_PACK(7, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
254 "*l*l*l*s16*b*b*w", MPTSAS_NUM_PORTS);
257 static
258 size_t mptsas_config_manufacturing_8(MPTSASState *s, uint8_t **data, int address)
260 return MPTSAS_CONFIG_PACK(8, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
261 "*l");
264 static
265 size_t mptsas_config_manufacturing_9(MPTSASState *s, uint8_t **data, int address)
267 return MPTSAS_CONFIG_PACK(9, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
268 "*l");
271 static
272 size_t mptsas_config_manufacturing_10(MPTSASState *s, uint8_t **data, int address)
274 return MPTSAS_CONFIG_PACK(10, MPI_CONFIG_PAGETYPE_MANUFACTURING, 0x00,
275 "*l");
278 /* I/O unit pages */
280 static
281 size_t mptsas_config_io_unit_0(MPTSASState *s, uint8_t **data, int address)
283 PCIDevice *pci = PCI_DEVICE(s);
284 uint64_t unique_value = 0x53504D554D4551LL; /* "QEMUMPTx" */
286 unique_value |= (uint64_t)pci->devfn << 56;
287 return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x00,
288 "q", unique_value);
291 static
292 size_t mptsas_config_io_unit_1(MPTSASState *s, uint8_t **data, int address)
294 return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x02, "l",
295 0x41 /* single function, RAID disabled */ );
298 static
299 size_t mptsas_config_io_unit_2(MPTSASState *s, uint8_t **data, int address)
301 PCIDevice *pci = PCI_DEVICE(s);
302 uint8_t devfn = pci->devfn;
303 return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x02,
304 "llbbw*b*b*w*b*b*w*b*b*w*l",
305 0, 0x100, 0 /* pci bus? */, devfn, 0);
308 static
309 size_t mptsas_config_io_unit_3(MPTSASState *s, uint8_t **data, int address)
311 return MPTSAS_CONFIG_PACK(3, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x01,
312 "*b*b*w*l");
315 static
316 size_t mptsas_config_io_unit_4(MPTSASState *s, uint8_t **data, int address)
318 return MPTSAS_CONFIG_PACK(4, MPI_CONFIG_PAGETYPE_IO_UNIT, 0x00, "*l*l*q");
321 /* I/O controller pages */
323 static
324 size_t mptsas_config_ioc_0(MPTSASState *s, uint8_t **data, int address)
326 PCIDeviceClass *pcic = PCI_DEVICE_GET_CLASS(s);
328 return MPTSAS_CONFIG_PACK(0, MPI_CONFIG_PAGETYPE_IOC, 0x01,
329 "*l*lwwb*b*b*blww",
330 pcic->vendor_id, pcic->device_id, pcic->revision,
331 pcic->class_id, pcic->subsystem_vendor_id,
332 pcic->subsystem_id);
335 static
336 size_t mptsas_config_ioc_1(MPTSASState *s, uint8_t **data, int address)
338 return MPTSAS_CONFIG_PACK(1, MPI_CONFIG_PAGETYPE_IOC, 0x03,
339 "*l*l*b*b*b*b");
342 static
343 size_t mptsas_config_ioc_2(MPTSASState *s, uint8_t **data, int address)
345 return MPTSAS_CONFIG_PACK(2, MPI_CONFIG_PAGETYPE_IOC, 0x04,
346 "*l*b*b*b*b");
349 static
350 size_t mptsas_config_ioc_3(MPTSASState *s, uint8_t **data, int address)
352 return MPTSAS_CONFIG_PACK(3, MPI_CONFIG_PAGETYPE_IOC, 0x00,
353 "*b*b*w");
356 static
357 size_t mptsas_config_ioc_4(MPTSASState *s, uint8_t **data, int address)
359 return MPTSAS_CONFIG_PACK(4, MPI_CONFIG_PAGETYPE_IOC, 0x00,
360 "*b*b*w");
363 static
364 size_t mptsas_config_ioc_5(MPTSASState *s, uint8_t **data, int address)
366 return MPTSAS_CONFIG_PACK(5, MPI_CONFIG_PAGETYPE_IOC, 0x00,
367 "*l*b*b*w");
370 static
371 size_t mptsas_config_ioc_6(MPTSASState *s, uint8_t **data, int address)
373 return MPTSAS_CONFIG_PACK(6, MPI_CONFIG_PAGETYPE_IOC, 0x01,
374 "*l*b*b*b*b*b*b*b*b*b*b*w*l*l*l*l*b*b*w"
375 "*w*w*w*w*l*l*l");
378 /* SAS I/O unit pages (extended) */
380 #define MPTSAS_CONFIG_SAS_IO_UNIT_0_SIZE 16
382 #define MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION 0x02
383 #define MPI_SAS_IOUNIT0_RATE_1_5 0x08
384 #define MPI_SAS_IOUNIT0_RATE_3_0 0x09
386 #define MPI_SAS_DEVICE_INFO_NO_DEVICE 0x00000000
387 #define MPI_SAS_DEVICE_INFO_END_DEVICE 0x00000001
388 #define MPI_SAS_DEVICE_INFO_SSP_TARGET 0x00000400
390 #define MPI_SAS_DEVICE0_ASTATUS_NO_ERRORS 0x00
392 #define MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT 0x0001
393 #define MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED 0x0002
394 #define MPI_SAS_DEVICE0_FLAGS_MAPPING_PERSISTENT 0x0004
398 static SCSIDevice *mptsas_phy_get_device(MPTSASState *s, int i,
399 int *phy_handle, int *dev_handle)
401 SCSIDevice *d = scsi_device_find(&s->bus, 0, i, 0);
403 if (phy_handle) {
404 *phy_handle = i + 1;
406 if (dev_handle) {
407 *dev_handle = d ? i + 1 + MPTSAS_NUM_PORTS : 0;
409 return d;
412 static
413 size_t mptsas_config_sas_io_unit_0(MPTSASState *s, uint8_t **data, int address)
415 size_t size = MPTSAS_CONFIG_PACK_EXT(0, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 0x04,
416 "*w*wb*b*w"
417 repl(MPTSAS_NUM_PORTS, "*s16"),
418 MPTSAS_NUM_PORTS);
420 if (data) {
421 size_t ofs = size - MPTSAS_NUM_PORTS * MPTSAS_CONFIG_SAS_IO_UNIT_0_SIZE;
422 int i;
424 for (i = 0; i < MPTSAS_NUM_PORTS; i++) {
425 int phy_handle, dev_handle;
426 SCSIDevice *dev = mptsas_phy_get_device(s, i, &phy_handle, &dev_handle);
428 fill(*data + ofs, MPTSAS_CONFIG_SAS_IO_UNIT_0_SIZE,
429 "bbbblwwl", i, 0, 0,
430 (dev
431 ? MPI_SAS_IOUNIT0_RATE_3_0
432 : MPI_SAS_IOUNIT0_RATE_FAILED_SPEED_NEGOTIATION),
433 (dev
434 ? MPI_SAS_DEVICE_INFO_END_DEVICE | MPI_SAS_DEVICE_INFO_SSP_TARGET
435 : MPI_SAS_DEVICE_INFO_NO_DEVICE),
436 dev_handle,
437 dev_handle,
439 ofs += MPTSAS_CONFIG_SAS_IO_UNIT_0_SIZE;
441 assert(ofs == size);
443 return size;
446 #define MPTSAS_CONFIG_SAS_IO_UNIT_1_SIZE 12
448 static
449 size_t mptsas_config_sas_io_unit_1(MPTSASState *s, uint8_t **data, int address)
451 size_t size = MPTSAS_CONFIG_PACK_EXT(1, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 0x07,
452 "*w*w*w*wb*b*b*b"
453 repl(MPTSAS_NUM_PORTS, "*s12"),
454 MPTSAS_NUM_PORTS);
456 if (data) {
457 size_t ofs = size - MPTSAS_NUM_PORTS * MPTSAS_CONFIG_SAS_IO_UNIT_1_SIZE;
458 int i;
460 for (i = 0; i < MPTSAS_NUM_PORTS; i++) {
461 SCSIDevice *dev = mptsas_phy_get_device(s, i, NULL, NULL);
462 fill(*data + ofs, MPTSAS_CONFIG_SAS_IO_UNIT_1_SIZE,
463 "bbbblww", i, 0, 0,
464 (MPI_SAS_IOUNIT0_RATE_3_0 << 4) | MPI_SAS_IOUNIT0_RATE_1_5,
465 (dev
466 ? MPI_SAS_DEVICE_INFO_END_DEVICE | MPI_SAS_DEVICE_INFO_SSP_TARGET
467 : MPI_SAS_DEVICE_INFO_NO_DEVICE),
468 0, 0);
469 ofs += MPTSAS_CONFIG_SAS_IO_UNIT_1_SIZE;
471 assert(ofs == size);
473 return size;
476 static
477 size_t mptsas_config_sas_io_unit_2(MPTSASState *s, uint8_t **data, int address)
479 return MPTSAS_CONFIG_PACK_EXT(2, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 0x06,
480 "*b*b*w*w*w*b*b*w");
483 static
484 size_t mptsas_config_sas_io_unit_3(MPTSASState *s, uint8_t **data, int address)
486 return MPTSAS_CONFIG_PACK_EXT(3, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT, 0x06,
487 "*l*l*l*l*l*l*l*l*l");
490 /* SAS PHY pages (extended) */
492 static int mptsas_phy_addr_get(MPTSASState *s, int address)
494 int i;
495 if ((address >> MPI_SAS_PHY_PGAD_FORM_SHIFT) == 0) {
496 i = address & 255;
497 } else if ((address >> MPI_SAS_PHY_PGAD_FORM_SHIFT) == 1) {
498 i = address & 65535;
499 } else {
500 return -EINVAL;
503 if (i >= MPTSAS_NUM_PORTS) {
504 return -EINVAL;
507 return i;
510 static
511 size_t mptsas_config_phy_0(MPTSASState *s, uint8_t **data, int address)
513 int phy_handle = -1;
514 int dev_handle = -1;
515 int i = mptsas_phy_addr_get(s, address);
516 SCSIDevice *dev;
518 if (i < 0) {
519 trace_mptsas_config_sas_phy(s, address, i, phy_handle, dev_handle, 0);
520 return i;
523 dev = mptsas_phy_get_device(s, i, &phy_handle, &dev_handle);
524 trace_mptsas_config_sas_phy(s, address, i, phy_handle, dev_handle, 0);
526 return MPTSAS_CONFIG_PACK_EXT(0, MPI_CONFIG_EXTPAGETYPE_SAS_PHY, 0x01,
527 "w*wqwb*blbb*b*b*l",
528 dev_handle, s->sas_addr, dev_handle, i,
529 (dev
530 ? MPI_SAS_DEVICE_INFO_END_DEVICE /* | MPI_SAS_DEVICE_INFO_SSP_TARGET?? */
531 : MPI_SAS_DEVICE_INFO_NO_DEVICE),
532 (MPI_SAS_IOUNIT0_RATE_3_0 << 4) | MPI_SAS_IOUNIT0_RATE_1_5,
533 (MPI_SAS_IOUNIT0_RATE_3_0 << 4) | MPI_SAS_IOUNIT0_RATE_1_5);
536 static
537 size_t mptsas_config_phy_1(MPTSASState *s, uint8_t **data, int address)
539 int phy_handle = -1;
540 int dev_handle = -1;
541 int i = mptsas_phy_addr_get(s, address);
543 if (i < 0) {
544 trace_mptsas_config_sas_phy(s, address, i, phy_handle, dev_handle, 1);
545 return i;
548 (void) mptsas_phy_get_device(s, i, &phy_handle, &dev_handle);
549 trace_mptsas_config_sas_phy(s, address, i, phy_handle, dev_handle, 1);
551 return MPTSAS_CONFIG_PACK_EXT(1, MPI_CONFIG_EXTPAGETYPE_SAS_PHY, 0x01,
552 "*l*l*l*l*l");
555 /* SAS device pages (extended) */
557 static int mptsas_device_addr_get(MPTSASState *s, int address)
559 uint32_t handle, i;
560 uint32_t form = address >> MPI_SAS_PHY_PGAD_FORM_SHIFT;
561 if (form == MPI_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE) {
562 handle = address & MPI_SAS_DEVICE_PGAD_GNH_HANDLE_MASK;
563 do {
564 if (handle == 65535) {
565 handle = MPTSAS_NUM_PORTS + 1;
566 } else {
567 ++handle;
569 i = handle - 1 - MPTSAS_NUM_PORTS;
570 } while (i < MPTSAS_NUM_PORTS && !scsi_device_find(&s->bus, 0, i, 0));
572 } else if (form == MPI_SAS_DEVICE_PGAD_FORM_BUS_TARGET_ID) {
573 if (address & MPI_SAS_DEVICE_PGAD_BT_BUS_MASK) {
574 return -EINVAL;
576 i = address & MPI_SAS_DEVICE_PGAD_BT_TID_MASK;
578 } else if (form == MPI_SAS_DEVICE_PGAD_FORM_HANDLE) {
579 handle = address & MPI_SAS_DEVICE_PGAD_H_HANDLE_MASK;
580 i = handle - 1 - MPTSAS_NUM_PORTS;
582 } else {
583 return -EINVAL;
586 if (i >= MPTSAS_NUM_PORTS) {
587 return -EINVAL;
590 return i;
593 static
594 size_t mptsas_config_sas_device_0(MPTSASState *s, uint8_t **data, int address)
596 int phy_handle = -1;
597 int dev_handle = -1;
598 int i = mptsas_device_addr_get(s, address);
599 SCSIDevice *dev = mptsas_phy_get_device(s, i, &phy_handle, &dev_handle);
601 trace_mptsas_config_sas_device(s, address, i, phy_handle, dev_handle, 0);
602 if (!dev) {
603 return -ENOENT;
606 return MPTSAS_CONFIG_PACK_EXT(0, MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE, 0x05,
607 "*w*wqwbbwbblwb*b",
608 dev->wwn, phy_handle, i,
609 MPI_SAS_DEVICE0_ASTATUS_NO_ERRORS,
610 dev_handle, i, 0,
611 MPI_SAS_DEVICE_INFO_END_DEVICE | MPI_SAS_DEVICE_INFO_SSP_TARGET,
612 (MPI_SAS_DEVICE0_FLAGS_DEVICE_PRESENT |
613 MPI_SAS_DEVICE0_FLAGS_DEVICE_MAPPED |
614 MPI_SAS_DEVICE0_FLAGS_MAPPING_PERSISTENT), i);
617 static
618 size_t mptsas_config_sas_device_1(MPTSASState *s, uint8_t **data, int address)
620 int phy_handle = -1;
621 int dev_handle = -1;
622 int i = mptsas_device_addr_get(s, address);
623 SCSIDevice *dev = mptsas_phy_get_device(s, i, &phy_handle, &dev_handle);
625 trace_mptsas_config_sas_device(s, address, i, phy_handle, dev_handle, 1);
626 if (!dev) {
627 return -ENOENT;
630 return MPTSAS_CONFIG_PACK_EXT(1, MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE, 0x00,
631 "*lq*lwbb*s20",
632 dev->wwn, dev_handle, i, 0);
635 static
636 size_t mptsas_config_sas_device_2(MPTSASState *s, uint8_t **data, int address)
638 int phy_handle = -1;
639 int dev_handle = -1;
640 int i = mptsas_device_addr_get(s, address);
641 SCSIDevice *dev = mptsas_phy_get_device(s, i, &phy_handle, &dev_handle);
643 trace_mptsas_config_sas_device(s, address, i, phy_handle, dev_handle, 2);
644 if (!dev) {
645 return -ENOENT;
648 return MPTSAS_CONFIG_PACK_EXT(2, MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE, 0x01,
649 "ql", dev->wwn, 0);
652 typedef struct MPTSASConfigPage {
653 uint8_t number;
654 uint8_t type;
655 size_t (*mpt_config_build)(MPTSASState *s, uint8_t **data, int address);
656 } MPTSASConfigPage;
658 static const MPTSASConfigPage mptsas_config_pages[] = {
660 0, MPI_CONFIG_PAGETYPE_MANUFACTURING,
661 mptsas_config_manufacturing_0,
662 }, {
663 1, MPI_CONFIG_PAGETYPE_MANUFACTURING,
664 mptsas_config_manufacturing_1,
665 }, {
666 2, MPI_CONFIG_PAGETYPE_MANUFACTURING,
667 mptsas_config_manufacturing_2,
668 }, {
669 3, MPI_CONFIG_PAGETYPE_MANUFACTURING,
670 mptsas_config_manufacturing_3,
671 }, {
672 4, MPI_CONFIG_PAGETYPE_MANUFACTURING,
673 mptsas_config_manufacturing_4,
674 }, {
675 5, MPI_CONFIG_PAGETYPE_MANUFACTURING,
676 mptsas_config_manufacturing_5,
677 }, {
678 6, MPI_CONFIG_PAGETYPE_MANUFACTURING,
679 mptsas_config_manufacturing_6,
680 }, {
681 7, MPI_CONFIG_PAGETYPE_MANUFACTURING,
682 mptsas_config_manufacturing_7,
683 }, {
684 8, MPI_CONFIG_PAGETYPE_MANUFACTURING,
685 mptsas_config_manufacturing_8,
686 }, {
687 9, MPI_CONFIG_PAGETYPE_MANUFACTURING,
688 mptsas_config_manufacturing_9,
689 }, {
690 10, MPI_CONFIG_PAGETYPE_MANUFACTURING,
691 mptsas_config_manufacturing_10,
692 }, {
693 0, MPI_CONFIG_PAGETYPE_IO_UNIT,
694 mptsas_config_io_unit_0,
695 }, {
696 1, MPI_CONFIG_PAGETYPE_IO_UNIT,
697 mptsas_config_io_unit_1,
698 }, {
699 2, MPI_CONFIG_PAGETYPE_IO_UNIT,
700 mptsas_config_io_unit_2,
701 }, {
702 3, MPI_CONFIG_PAGETYPE_IO_UNIT,
703 mptsas_config_io_unit_3,
704 }, {
705 4, MPI_CONFIG_PAGETYPE_IO_UNIT,
706 mptsas_config_io_unit_4,
707 }, {
708 0, MPI_CONFIG_PAGETYPE_IOC,
709 mptsas_config_ioc_0,
710 }, {
711 1, MPI_CONFIG_PAGETYPE_IOC,
712 mptsas_config_ioc_1,
713 }, {
714 2, MPI_CONFIG_PAGETYPE_IOC,
715 mptsas_config_ioc_2,
716 }, {
717 3, MPI_CONFIG_PAGETYPE_IOC,
718 mptsas_config_ioc_3,
719 }, {
720 4, MPI_CONFIG_PAGETYPE_IOC,
721 mptsas_config_ioc_4,
722 }, {
723 5, MPI_CONFIG_PAGETYPE_IOC,
724 mptsas_config_ioc_5,
725 }, {
726 6, MPI_CONFIG_PAGETYPE_IOC,
727 mptsas_config_ioc_6,
728 }, {
729 0, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
730 mptsas_config_sas_io_unit_0,
731 }, {
732 1, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
733 mptsas_config_sas_io_unit_1,
734 }, {
735 2, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
736 mptsas_config_sas_io_unit_2,
737 }, {
738 3, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
739 mptsas_config_sas_io_unit_3,
740 }, {
741 0, MPI_CONFIG_EXTPAGETYPE_SAS_PHY,
742 mptsas_config_phy_0,
743 }, {
744 1, MPI_CONFIG_EXTPAGETYPE_SAS_PHY,
745 mptsas_config_phy_1,
746 }, {
747 0, MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE,
748 mptsas_config_sas_device_0,
749 }, {
750 1, MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE,
751 mptsas_config_sas_device_1,
752 }, {
753 2, MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE,
754 mptsas_config_sas_device_2,
758 static const MPTSASConfigPage *mptsas_find_config_page(int type, int number)
760 const MPTSASConfigPage *page;
761 int i;
763 for (i = 0; i < ARRAY_SIZE(mptsas_config_pages); i++) {
764 page = &mptsas_config_pages[i];
765 if (page->type == type && page->number == number) {
766 return page;
770 return NULL;
773 void mptsas_process_config(MPTSASState *s, MPIMsgConfig *req)
775 PCIDevice *pci = PCI_DEVICE(s);
777 MPIMsgConfigReply reply;
778 const MPTSASConfigPage *page;
779 size_t length;
780 uint8_t type;
781 uint8_t *data = NULL;
782 uint32_t flags_and_length;
783 uint32_t dmalen;
784 uint64_t pa;
786 mptsas_fix_config_endianness(req);
788 QEMU_BUILD_BUG_ON(sizeof(s->doorbell_msg) < sizeof(*req));
789 QEMU_BUILD_BUG_ON(sizeof(s->doorbell_reply) < sizeof(reply));
791 /* Copy common bits from the request into the reply. */
792 memset(&reply, 0, sizeof(reply));
793 reply.Action = req->Action;
794 reply.Function = req->Function;
795 reply.MsgContext = req->MsgContext;
796 reply.MsgLength = sizeof(reply) / 4;
797 reply.PageType = req->PageType;
798 reply.PageNumber = req->PageNumber;
799 reply.PageLength = req->PageLength;
800 reply.PageVersion = req->PageVersion;
802 type = req->PageType & MPI_CONFIG_PAGETYPE_MASK;
803 if (type == MPI_CONFIG_PAGETYPE_EXTENDED) {
804 type = req->ExtPageType;
805 if (type <= MPI_CONFIG_PAGETYPE_MASK) {
806 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_TYPE;
807 goto out;
810 reply.ExtPageType = req->ExtPageType;
813 page = mptsas_find_config_page(type, req->PageNumber);
815 switch(req->Action) {
816 case MPI_CONFIG_ACTION_PAGE_DEFAULT:
817 case MPI_CONFIG_ACTION_PAGE_HEADER:
818 case MPI_CONFIG_ACTION_PAGE_READ_NVRAM:
819 case MPI_CONFIG_ACTION_PAGE_READ_CURRENT:
820 case MPI_CONFIG_ACTION_PAGE_READ_DEFAULT:
821 case MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT:
822 case MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM:
823 break;
825 default:
826 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_ACTION;
827 goto out;
830 if (!page) {
831 page = mptsas_find_config_page(type, 1);
832 if (page) {
833 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_PAGE;
834 } else {
835 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_TYPE;
837 goto out;
840 if (req->Action == MPI_CONFIG_ACTION_PAGE_DEFAULT ||
841 req->Action == MPI_CONFIG_ACTION_PAGE_HEADER) {
842 length = page->mpt_config_build(s, NULL, req->PageAddress);
843 if ((ssize_t)length < 0) {
844 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_PAGE;
845 goto out;
846 } else {
847 goto done;
851 if (req->Action == MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT ||
852 req->Action == MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM) {
853 length = page->mpt_config_build(s, NULL, req->PageAddress);
854 if ((ssize_t)length < 0) {
855 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_PAGE;
856 } else {
857 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_CANT_COMMIT;
859 goto out;
862 flags_and_length = req->PageBufferSGE.FlagsLength;
863 dmalen = flags_and_length & MPI_SGE_LENGTH_MASK;
864 if (dmalen == 0) {
865 length = page->mpt_config_build(s, NULL, req->PageAddress);
866 if ((ssize_t)length < 0) {
867 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_PAGE;
868 goto out;
869 } else {
870 goto done;
874 if (flags_and_length & MPI_SGE_FLAGS_64_BIT_ADDRESSING) {
875 pa = req->PageBufferSGE.u.Address64;
876 } else {
877 pa = req->PageBufferSGE.u.Address32;
880 /* Only read actions left. */
881 length = page->mpt_config_build(s, &data, req->PageAddress);
882 if ((ssize_t)length < 0) {
883 reply.IOCStatus = MPI_IOCSTATUS_CONFIG_INVALID_PAGE;
884 goto out;
885 } else {
886 assert(data[2] == page->number);
887 pci_dma_write(pci, pa, data, MIN(length, dmalen));
888 goto done;
891 abort();
893 done:
894 if (type > MPI_CONFIG_PAGETYPE_MASK) {
895 reply.ExtPageLength = length / 4;
896 reply.ExtPageType = req->ExtPageType;
897 } else {
898 reply.PageLength = length / 4;
901 out:
902 mptsas_fix_config_reply_endianness(&reply);
903 mptsas_reply(s, (MPIDefaultReply *)&reply);
904 g_free(data);