staging: unisys: visorbus: renamed structures in controlvmchannel.h to match driver...
[linux-2.6-block.git] / drivers / staging / unisys / visorbus / visorchipset.c
CommitLineData
12e364b9
KC
1/* visorchipset_main.c
2 *
6f14cc18 3 * Copyright (C) 2010 - 2015 UNISYS CORPORATION
12e364b9
KC
4 * All rights reserved.
5 *
6f14cc18
BR
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
12e364b9
KC
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
14 * details.
15 */
16
55c67dca 17#include <linux/acpi.h>
46168810 18#include <linux/ctype.h>
e3420ed6
EA
19#include <linux/fs.h>
20#include <linux/mm.h>
12e364b9
KC
21#include <linux/nls.h>
22#include <linux/netdevice.h>
90addb02 23#include <linux/uuid.h>
1ba00980 24#include <linux/crash_dump.h>
12e364b9 25
55c67dca
PB
26#include "visorbus.h"
27#include "visorbus_private.h"
5f3a7e36 28#include "vmcallinterface.h"
55c67dca 29
3d118a42 30#define CURRENT_FILE_PC VISOR_BUS_PC_visorchipset_c
12e364b9 31
ccc0d7bf 32#define POLLJIFFIES_CONTROLVMCHANNEL_FAST 1
12e364b9
KC
33#define POLLJIFFIES_CONTROLVMCHANNEL_SLOW 100
34
2c7e1d4e 35#define MAX_CONTROLVM_PAYLOAD_BYTES (1024 * 128)
2ee0deec 36
a27ded92 37#define UNISYS_VISOR_LEAF_ID 0x40000000
d5b3f1dc
EA
38
39/* The s-Par leaf ID returns "UnisysSpar64" encoded across ebx, ecx, edx */
a27ded92
SW
40#define UNISYS_VISOR_ID_EBX 0x73696e55
41#define UNISYS_VISOR_ID_ECX 0x70537379
42#define UNISYS_VISOR_ID_EDX 0x34367261
d5b3f1dc 43
ec17f452
DB
44/*
45 * When the controlvm channel is idle for at least MIN_IDLE_SECONDS,
46 * we switch to slow polling mode. As soon as we get a controlvm
47 * message, we switch back to fast polling mode.
48 */
12e364b9 49#define MIN_IDLE_SECONDS 10
12e364b9 50
46168810
EA
51struct parser_context {
52 unsigned long allocbytes;
53 unsigned long param_bytes;
54 u8 *curr;
55 unsigned long bytes_remaining;
56 bool byte_stream;
57 char data[0];
58};
59
bb1fb8ac
SW
60struct vmcall_controlvm_addr {
61 struct vmcall_io_controlvm_addr_params params;
62 int err;
63 u64 physaddr;
64};
65
765b2f82
SW
66struct visorchipset_device {
67 struct acpi_device *acpi_device;
68 unsigned long poll_jiffies;
69 /* when we got our last controlvm message */
70 unsigned long most_recent_message_jiffies;
71 struct delayed_work periodic_controlvm_work;
765b2f82
SW
72 struct visorchannel *controlvm_channel;
73 unsigned long controlvm_payload_bytes_buffered;
74 /*
75 * The following variables are used to handle the scenario where we are
76 * unable to offload the payload from a controlvm message due to memory
77 * requirements. In this scenario, we simply stash the controlvm
78 * message, then attempt to process it again the next time
79 * controlvm_periodic_work() runs.
80 */
81 struct controlvm_message controlvm_pending_msg;
82 bool controlvm_pending_msg_valid;
bb1fb8ac 83 struct vmcall_controlvm_addr controlvm_addr;
765b2f82 84};
12e364b9 85
765b2f82 86static struct visorchipset_device *chipset_dev;
12e364b9 87
12e364b9
KC
88struct parahotplug_request {
89 struct list_head list;
90 int id;
91 unsigned long expiration;
3ab47701 92 struct controlvm_message msg;
12e364b9
KC
93};
94
19f6634f
BR
95/* prototypes for attributes */
96static ssize_t toolaction_show(struct device *dev,
84efd207
DK
97 struct device_attribute *attr,
98 char *buf)
99{
100 u8 tool_action = 0;
002a5abb
DK
101 int err;
102
103 err = visorchannel_read(chipset_dev->controlvm_channel,
545f0913 104 offsetof(struct visor_controlvm_channel,
002a5abb
DK
105 tool_action),
106 &tool_action, sizeof(u8));
107 if (err)
108 return err;
84efd207 109
746fb137 110 return sprintf(buf, "%u\n", tool_action);
84efd207
DK
111}
112
19f6634f 113static ssize_t toolaction_store(struct device *dev,
8e76e695 114 struct device_attribute *attr,
84efd207
DK
115 const char *buf, size_t count)
116{
117 u8 tool_action;
dc35cdf3 118 int err;
84efd207
DK
119
120 if (kstrtou8(buf, 10, &tool_action))
121 return -EINVAL;
122
545f0913
SW
123 err = visorchannel_write(chipset_dev->controlvm_channel,
124 offsetof(struct visor_controlvm_channel,
125 tool_action),
126 &tool_action, sizeof(u8));
84efd207 127
dc35cdf3
DK
128 if (err)
129 return err;
84efd207
DK
130 return count;
131}
19f6634f
BR
132static DEVICE_ATTR_RW(toolaction);
133
54b31229 134static ssize_t boottotool_show(struct device *dev,
1b1d463d
DK
135 struct device_attribute *attr,
136 char *buf)
137{
545f0913 138 struct efi_visor_indication efi_visor_indication;
0b01c6ce
DK
139 int err;
140
141 err = visorchannel_read(chipset_dev->controlvm_channel,
545f0913
SW
142 offsetof(struct visor_controlvm_channel,
143 efi_visor_ind),
144 &efi_visor_indication,
145 sizeof(struct efi_visor_indication));
1b1d463d 146
0b01c6ce
DK
147 if (err)
148 return err;
545f0913 149 return sprintf(buf, "%u\n", efi_visor_indication.boot_to_tool);
1b1d463d
DK
150}
151
54b31229 152static ssize_t boottotool_store(struct device *dev,
1b1d463d
DK
153 struct device_attribute *attr,
154 const char *buf, size_t count)
155{
b309266e 156 int val, err;
545f0913 157 struct efi_visor_indication efi_visor_indication;
1b1d463d
DK
158
159 if (kstrtoint(buf, 10, &val))
160 return -EINVAL;
161
545f0913
SW
162 efi_visor_indication.boot_to_tool = val;
163 err = visorchannel_write(chipset_dev->controlvm_channel,
164 offsetof(struct visor_controlvm_channel,
165 efi_visor_ind),
166 &(efi_visor_indication),
167 sizeof(struct efi_visor_indication));
1b1d463d 168
b309266e
DK
169 if (err)
170 return err;
1b1d463d
DK
171 return count;
172}
54b31229
BR
173static DEVICE_ATTR_RW(boottotool);
174
422af17c 175static ssize_t error_show(struct device *dev, struct device_attribute *attr,
8a4a8a03
DK
176 char *buf)
177{
178 u32 error = 0;
d9857c79 179 int err;
8a4a8a03 180
d9857c79 181 err = visorchannel_read(chipset_dev->controlvm_channel,
545f0913 182 offsetof(struct visor_controlvm_channel,
d9857c79
DK
183 installation_error),
184 &error, sizeof(u32));
185 if (err)
186 return err;
746fb137 187 return sprintf(buf, "%i\n", error);
8a4a8a03
DK
188}
189
422af17c 190static ssize_t error_store(struct device *dev, struct device_attribute *attr,
8a4a8a03
DK
191 const char *buf, size_t count)
192{
193 u32 error;
ea295857 194 int err;
8a4a8a03
DK
195
196 if (kstrtou32(buf, 10, &error))
197 return -EINVAL;
198
545f0913
SW
199 err = visorchannel_write(chipset_dev->controlvm_channel,
200 offsetof(struct visor_controlvm_channel,
201 installation_error),
202 &error, sizeof(u32));
ea295857
DK
203 if (err)
204 return err;
8a4a8a03
DK
205 return count;
206}
422af17c
BR
207static DEVICE_ATTR_RW(error);
208
209static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
79730c7c
DK
210 char *buf)
211{
212 u32 text_id = 0;
0d406436
DK
213 int err;
214
545f0913
SW
215 err = visorchannel_read(chipset_dev->controlvm_channel,
216 offsetof(struct visor_controlvm_channel,
217 installation_text_id),
218 &text_id, sizeof(u32));
0d406436
DK
219 if (err)
220 return err;
79730c7c 221
746fb137 222 return sprintf(buf, "%i\n", text_id);
79730c7c
DK
223}
224
422af17c 225static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
79730c7c
DK
226 const char *buf, size_t count)
227{
228 u32 text_id;
08a55d2d 229 int err;
79730c7c
DK
230
231 if (kstrtou32(buf, 10, &text_id))
232 return -EINVAL;
233
545f0913
SW
234 err = visorchannel_write(chipset_dev->controlvm_channel,
235 offsetof(struct visor_controlvm_channel,
236 installation_text_id),
237 &text_id, sizeof(u32));
08a55d2d
DK
238 if (err)
239 return err;
79730c7c
DK
240 return count;
241}
422af17c
BR
242static DEVICE_ATTR_RW(textid);
243
244static ssize_t remaining_steps_show(struct device *dev,
97f792ee
DK
245 struct device_attribute *attr, char *buf)
246{
247 u16 remaining_steps = 0;
c53578bd
DK
248 int err;
249
250 err = visorchannel_read(chipset_dev->controlvm_channel,
545f0913 251 offsetof(struct visor_controlvm_channel,
c53578bd
DK
252 installation_remaining_steps),
253 &remaining_steps, sizeof(u16));
254 if (err)
255 return err;
97f792ee 256
746fb137 257 return sprintf(buf, "%hu\n", remaining_steps);
97f792ee
DK
258}
259
422af17c 260static ssize_t remaining_steps_store(struct device *dev,
8e76e695 261 struct device_attribute *attr,
97f792ee
DK
262 const char *buf, size_t count)
263{
264 u16 remaining_steps;
e030d39d 265 int err;
97f792ee
DK
266
267 if (kstrtou16(buf, 10, &remaining_steps))
268 return -EINVAL;
269
545f0913
SW
270 err = visorchannel_write(chipset_dev->controlvm_channel,
271 offsetof(struct visor_controlvm_channel,
272 installation_remaining_steps),
273 &remaining_steps, sizeof(u16));
e030d39d
DK
274 if (err)
275 return err;
97f792ee
DK
276 return count;
277}
422af17c
BR
278static DEVICE_ATTR_RW(remaining_steps);
279
464129ed 280static uuid_le
46168810
EA
281parser_id_get(struct parser_context *ctx)
282{
545f0913 283 struct visor_controlvm_parameters_header *phdr = NULL;
46168810 284
545f0913 285 phdr = (struct visor_controlvm_parameters_header *)(ctx->data);
46168810
EA
286 return phdr->id;
287}
288
464129ed 289static void parser_done(struct parser_context *ctx)
46168810 290{
765b2f82 291 chipset_dev->controlvm_payload_bytes_buffered -= ctx->param_bytes;
46168810
EA
292 kfree(ctx);
293}
294
464129ed 295static void *
46168810
EA
296parser_string_get(struct parser_context *ctx)
297{
298 u8 *pscan;
299 unsigned long nscan;
300 int value_length = -1;
301 void *value = NULL;
302 int i;
303
46168810
EA
304 pscan = ctx->curr;
305 nscan = ctx->bytes_remaining;
306 if (nscan == 0)
307 return NULL;
308 if (!pscan)
309 return NULL;
310 for (i = 0, value_length = -1; i < nscan; i++)
311 if (pscan[i] == '\0') {
312 value_length = i;
313 break;
314 }
315 if (value_length < 0) /* '\0' was not included in the length */
316 value_length = nscan;
8c8c975f 317 value = kmalloc(value_length + 1, GFP_KERNEL);
e4a3dd33 318 if (!value)
46168810
EA
319 return NULL;
320 if (value_length > 0)
321 memcpy(value, pscan, value_length);
0e7bf2f4 322 ((u8 *)(value))[value_length] = '\0';
46168810
EA
323 return value;
324}
325
046f93dc
DK
326static void *
327parser_name_get(struct parser_context *ctx)
328{
545f0913 329 struct visor_controlvm_parameters_header *phdr = NULL;
046f93dc 330
545f0913 331 phdr = (struct visor_controlvm_parameters_header *)(ctx->data);
308ee8aa
DB
332
333 if (phdr->name_offset + phdr->name_length > ctx->param_bytes)
334 return NULL;
335
046f93dc
DK
336 ctx->curr = ctx->data + phdr->name_offset;
337 ctx->bytes_remaining = phdr->name_length;
338 return parser_string_get(ctx);
339}
340
ab0592b9
DZ
341struct visor_busdev {
342 u32 bus_no;
343 u32 dev_no;
344};
345
346static int match_visorbus_dev_by_id(struct device *dev, void *data)
347{
348 struct visor_device *vdev = to_visor_device(dev);
7f44582e 349 struct visor_busdev *id = data;
ab0592b9
DZ
350 u32 bus_no = id->bus_no;
351 u32 dev_no = id->dev_no;
352
65bd6e46
DZ
353 if ((vdev->chipset_bus_no == bus_no) &&
354 (vdev->chipset_dev_no == dev_no))
ab0592b9
DZ
355 return 1;
356
357 return 0;
358}
d1e08637 359
ab0592b9
DZ
360struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no,
361 struct visor_device *from)
362{
363 struct device *dev;
364 struct device *dev_start = NULL;
365 struct visor_device *vdev = NULL;
366 struct visor_busdev id = {
367 .bus_no = bus_no,
368 .dev_no = dev_no
369 };
370
371 if (from)
372 dev_start = &from->device;
373 dev = bus_find_device(&visorbus_type, dev_start, (void *)&id,
374 match_visorbus_dev_by_id);
375 if (dev)
376 vdev = to_visor_device(dev);
377 return vdev;
378}
ab0592b9 379
5f251395
DK
380static void
381controlvm_init_response(struct controlvm_message *msg,
382 struct controlvm_message_header *msg_hdr, int response)
383{
384 memset(msg, 0, sizeof(struct controlvm_message));
385 memcpy(&msg->hdr, msg_hdr, sizeof(struct controlvm_message_header));
386 msg->hdr.payload_bytes = 0;
387 msg->hdr.payload_vm_offset = 0;
388 msg->hdr.payload_max_bytes = 0;
389 if (response < 0) {
390 msg->hdr.flags.failed = 1;
391 msg->hdr.completion_status = (u32)(-response);
392 }
393}
394
1d7f5522 395static int
5f251395
DK
396controlvm_respond_chipset_init(struct controlvm_message_header *msg_hdr,
397 int response,
398 enum ultra_chipset_feature features)
399{
400 struct controlvm_message outmsg;
401
402 controlvm_init_response(&outmsg, msg_hdr, response);
403 outmsg.cmd.init_chipset.features = features;
765b2f82 404 return visorchannel_signalinsert(chipset_dev->controlvm_channel,
1d7f5522 405 CONTROLVM_QUEUE_REQUEST, &outmsg);
5f251395
DK
406}
407
79c3f971 408static int
3ab47701 409chipset_init(struct controlvm_message *inmsg)
12e364b9
KC
410{
411 static int chipset_inited;
b9b141e8 412 enum ultra_chipset_feature features = 0;
12e364b9 413 int rc = CONTROLVM_RESP_SUCCESS;
79c3f971 414 int res = 0;
12e364b9 415
12e364b9 416 if (chipset_inited) {
98f9ed9e 417 rc = -CONTROLVM_RESP_ALREADY_DONE;
79c3f971 418 res = -EIO;
5233d1eb 419 goto out_respond;
12e364b9
KC
420 }
421 chipset_inited = 1;
12e364b9 422
ec17f452
DB
423 /*
424 * Set features to indicate we support parahotplug (if Command
2ee0d052
EA
425 * also supports it).
426 */
0762188b
DB
427 features = inmsg->cmd.init_chipset.features &
428 ULTRA_CHIPSET_FEATURE_PARA_HOTPLUG;
12e364b9 429
ec17f452
DB
430 /*
431 * Set the "reply" bit so Command knows this is a
2ee0d052
EA
432 * features-aware driver.
433 */
12e364b9
KC
434 features |= ULTRA_CHIPSET_FEATURE_REPLY;
435
5233d1eb 436out_respond:
98d7b594 437 if (inmsg->hdr.flags.response_expected)
79c3f971
DK
438 res = controlvm_respond_chipset_init(&inmsg->hdr, rc, features);
439
440 return res;
12e364b9
KC
441}
442
2d26aeb7 443static int
4c0e65f8 444controlvm_respond(struct controlvm_message_header *msg_hdr, int response,
545f0913 445 struct visor_segment_state *state)
12e364b9 446{
3ab47701 447 struct controlvm_message outmsg;
26eb2c0c 448
b3168c70 449 controlvm_init_response(&outmsg, msg_hdr, response);
2098dbd1 450 if (outmsg.hdr.flags.test_message == 1)
2d26aeb7 451 return -EINVAL;
2098dbd1 452
4c0e65f8
DK
453 if (state) {
454 outmsg.cmd.device_change_state.state = *state;
455 outmsg.cmd.device_change_state.flags.phys_device = 1;
456 }
26eb2c0c 457
765b2f82 458 return visorchannel_signalinsert(chipset_dev->controlvm_channel,
2c4ef563 459 CONTROLVM_QUEUE_REQUEST, &outmsg);
12e364b9
KC
460}
461
2ee0deec
PB
462enum crash_obj_type {
463 CRASH_DEV,
464 CRASH_BUS,
465};
466
8dff01f7 467static int
603a1989 468save_crash_message(struct controlvm_message *msg, enum crash_obj_type cr_type)
12c957dc
TS
469{
470 u32 local_crash_msg_offset;
471 u16 local_crash_msg_count;
8dff01f7 472 int err;
12c957dc 473
765b2f82 474 err = visorchannel_read(chipset_dev->controlvm_channel,
545f0913 475 offsetof(struct visor_controlvm_channel,
8dff01f7
DK
476 saved_crash_message_count),
477 &local_crash_msg_count, sizeof(u16));
478 if (err) {
35301b87
DK
479 dev_err(&chipset_dev->acpi_device->dev,
480 "failed to read message count\n");
8dff01f7 481 return err;
12c957dc
TS
482 }
483
484 if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
35301b87
DK
485 dev_err(&chipset_dev->acpi_device->dev,
486 "invalid number of messages\n");
8dff01f7 487 return -EIO;
12c957dc
TS
488 }
489
765b2f82 490 err = visorchannel_read(chipset_dev->controlvm_channel,
545f0913 491 offsetof(struct visor_controlvm_channel,
8dff01f7
DK
492 saved_crash_message_offset),
493 &local_crash_msg_offset, sizeof(u32));
494 if (err) {
35301b87
DK
495 dev_err(&chipset_dev->acpi_device->dev,
496 "failed to read offset\n");
8dff01f7 497 return err;
12c957dc
TS
498 }
499
603a1989 500 switch (cr_type) {
36309d3b
DB
501 case CRASH_DEV:
502 local_crash_msg_offset += sizeof(struct controlvm_message);
765b2f82 503 err = visorchannel_write(chipset_dev->controlvm_channel,
8dff01f7
DK
504 local_crash_msg_offset,
505 msg,
36309d3b 506 sizeof(struct controlvm_message));
8dff01f7 507 if (err) {
35301b87
DK
508 dev_err(&chipset_dev->acpi_device->dev,
509 "failed to write dev msg\n");
8dff01f7 510 return err;
12c957dc 511 }
36309d3b
DB
512 break;
513 case CRASH_BUS:
765b2f82 514 err = visorchannel_write(chipset_dev->controlvm_channel,
8dff01f7
DK
515 local_crash_msg_offset,
516 msg,
517 sizeof(struct controlvm_message));
518 if (err) {
35301b87
DK
519 dev_err(&chipset_dev->acpi_device->dev,
520 "failed to write bus msg\n");
8dff01f7 521 return err;
12c957dc 522 }
36309d3b
DB
523 break;
524 default:
35301b87
DK
525 dev_err(&chipset_dev->acpi_device->dev,
526 "Invalid crash_obj_type\n");
36309d3b 527 break;
12c957dc 528 }
8dff01f7 529 return 0;
12c957dc
TS
530}
531
734ad93a 532static int
4fb2539c
DK
533controlvm_responder(enum controlvm_id cmd_id,
534 struct controlvm_message_header *pending_msg_hdr,
535 int response)
12e364b9 536{
e4a3dd33 537 if (!pending_msg_hdr)
734ad93a 538 return -EIO;
12e364b9 539
0274b5ae 540 if (pending_msg_hdr->id != (u32)cmd_id)
734ad93a 541 return -EINVAL;
0aca7844 542
4c0e65f8 543 return controlvm_respond(pending_msg_hdr, response, NULL);
12e364b9
KC
544}
545
68f99d49 546static int
fbb31f48 547device_changestate_responder(enum controlvm_id cmd_id,
a298bc0b 548 struct visor_device *p, int response,
545f0913 549 struct visor_segment_state response_state)
12e364b9 550{
3ab47701 551 struct controlvm_message outmsg;
a298bc0b
DZ
552 u32 bus_no = p->chipset_bus_no;
553 u32 dev_no = p->chipset_dev_no;
12e364b9 554
e4a3dd33 555 if (!p->pending_msg_hdr)
68f99d49 556 return -EIO;
0274b5ae 557 if (p->pending_msg_hdr->id != cmd_id)
68f99d49 558 return -EINVAL;
12e364b9 559
0274b5ae 560 controlvm_init_response(&outmsg, p->pending_msg_hdr, response);
12e364b9 561
fbb31f48
BR
562 outmsg.cmd.device_change_state.bus_no = bus_no;
563 outmsg.cmd.device_change_state.dev_no = dev_no;
564 outmsg.cmd.device_change_state.state = response_state;
12e364b9 565
765b2f82 566 return visorchannel_signalinsert(chipset_dev->controlvm_channel,
68f99d49 567 CONTROLVM_QUEUE_REQUEST, &outmsg);
12e364b9
KC
568}
569
33161a29 570static int
ec17cb8a 571visorbus_create(struct controlvm_message *inmsg)
12e364b9 572{
2ea5117b 573 struct controlvm_message_packet *cmd = &inmsg->cmd;
8f334e30 574 struct controlvm_message_header *pmsg_hdr = NULL;
52063eca 575 u32 bus_no = cmd->create_bus.bus_no;
d32517e3 576 struct visor_device *bus_info;
b32c4997 577 struct visorchannel *visorchannel;
33161a29 578 int err;
12e364b9 579
d32517e3 580 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
6c5fed35 581 if (bus_info && (bus_info->state.created == 1)) {
055bc909 582 dev_err(&chipset_dev->acpi_device->dev,
ec17cb8a 583 "failed visorbus_create: already exists\n");
33161a29
DK
584 err = -EEXIST;
585 goto err_respond;
12e364b9 586 }
33161a29 587
6c5fed35
BR
588 bus_info = kzalloc(sizeof(*bus_info), GFP_KERNEL);
589 if (!bus_info) {
33161a29
DK
590 err = -ENOMEM;
591 goto err_respond;
12e364b9
KC
592 }
593
4abce83d 594 INIT_LIST_HEAD(&bus_info->list_all);
d32517e3
DZ
595 bus_info->chipset_bus_no = bus_no;
596 bus_info->chipset_dev_no = BUS_ROOT_DEVICE;
12e364b9 597
300ed612
DK
598 if (uuid_le_cmp(cmd->create_bus.bus_inst_uuid, spar_siovm_uuid) == 0) {
599 err = save_crash_message(inmsg, CRASH_BUS);
600 if (err)
601 goto err_free_bus_info;
602 }
12e364b9 603
8f334e30
DK
604 if (inmsg->hdr.flags.response_expected == 1) {
605 pmsg_hdr = kzalloc(sizeof(*pmsg_hdr),
606 GFP_KERNEL);
607 if (!pmsg_hdr) {
33161a29
DK
608 err = -ENOMEM;
609 goto err_free_bus_info;
8f334e30
DK
610 }
611
612 memcpy(pmsg_hdr, &inmsg->hdr,
613 sizeof(struct controlvm_message_header));
614 bus_info->pending_msg_hdr = pmsg_hdr;
615 }
616
33161a29
DK
617 visorchannel = visorchannel_create(cmd->create_bus.channel_addr,
618 cmd->create_bus.channel_bytes,
619 GFP_KERNEL,
620 cmd->create_bus.bus_data_type_uuid);
621
622 if (!visorchannel) {
33161a29
DK
623 err = -ENOMEM;
624 goto err_free_pending_msg;
625 }
626 bus_info->visorchannel = visorchannel;
627
4f96c730
SW
628 /* Response will be handled by visorchipset_bus_create */
629 err = visorchipset_bus_create(bus_info);
630 /* If visorchipset_bus_create didn't respond, need to respond here */
621f5e18
DK
631 if (err)
632 goto err_destroy_channel;
8f334e30 633
33161a29
DK
634 return 0;
635
621f5e18
DK
636err_destroy_channel:
637 visorchannel_destroy(visorchannel);
638
33161a29
DK
639err_free_pending_msg:
640 kfree(bus_info->pending_msg_hdr);
8f334e30 641
33161a29 642err_free_bus_info:
8f334e30 643 kfree(bus_info);
12e364b9 644
33161a29 645err_respond:
8f334e30 646 if (inmsg->hdr.flags.response_expected == 1)
4fb2539c 647 controlvm_responder(inmsg->hdr.id, &inmsg->hdr, err);
33161a29 648 return err;
12e364b9
KC
649}
650
30f6c3f5 651static int
ec17cb8a 652visorbus_destroy(struct controlvm_message *inmsg)
12e364b9 653{
2ea5117b 654 struct controlvm_message_packet *cmd = &inmsg->cmd;
3e0e8db9 655 struct controlvm_message_header *pmsg_hdr = NULL;
52063eca 656 u32 bus_no = cmd->destroy_bus.bus_no;
d32517e3 657 struct visor_device *bus_info;
30f6c3f5 658 int err;
12e364b9 659
d32517e3 660 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
3e0e8db9 661 if (!bus_info) {
30f6c3f5
DK
662 err = -ENODEV;
663 goto err_respond;
3e0e8db9
DK
664 }
665 if (bus_info->state.created == 0) {
30f6c3f5
DK
666 err = -ENOENT;
667 goto err_respond;
3e0e8db9
DK
668 }
669 if (bus_info->pending_msg_hdr) {
670 /* only non-NULL if dev is still waiting on a response */
30f6c3f5
DK
671 err = -EEXIST;
672 goto err_respond;
3e0e8db9
DK
673 }
674 if (inmsg->hdr.flags.response_expected == 1) {
675 pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
676 if (!pmsg_hdr) {
30f6c3f5
DK
677 err = -ENOMEM;
678 goto err_respond;
3e0e8db9
DK
679 }
680
681 memcpy(pmsg_hdr, &inmsg->hdr,
682 sizeof(struct controlvm_message_header));
683 bus_info->pending_msg_hdr = pmsg_hdr;
684 }
12e364b9 685
4f96c730
SW
686 /* Response will be handled by visorchipset_bus_destroy */
687 visorchipset_bus_destroy(bus_info);
30f6c3f5 688 return 0;
3e0e8db9 689
30f6c3f5 690err_respond:
3e0e8db9 691 if (inmsg->hdr.flags.response_expected == 1)
4fb2539c 692 controlvm_responder(inmsg->hdr.id, &inmsg->hdr, err);
30f6c3f5 693 return err;
12e364b9
KC
694}
695
c71529fe 696static int
ec17cb8a
SW
697visorbus_configure(struct controlvm_message *inmsg,
698 struct parser_context *parser_ctx)
12e364b9 699{
2ea5117b 700 struct controlvm_message_packet *cmd = &inmsg->cmd;
e82ba62e 701 u32 bus_no;
d32517e3 702 struct visor_device *bus_info;
c71529fe 703 int err = 0;
12e364b9 704
654bada0 705 bus_no = cmd->configure_bus.bus_no;
d32517e3 706 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
654bada0 707 if (!bus_info) {
c71529fe
DK
708 err = -EINVAL;
709 goto err_respond;
654bada0 710 } else if (bus_info->state.created == 0) {
c71529fe
DK
711 err = -EINVAL;
712 goto err_respond;
e4a3dd33 713 } else if (bus_info->pending_msg_hdr) {
c71529fe
DK
714 err = -EIO;
715 goto err_respond;
12e364b9 716 }
c71529fe
DK
717
718 err = visorchannel_set_clientpartition
719 (bus_info->visorchannel,
720 cmd->configure_bus.guest_handle);
721 if (err)
722 goto err_respond;
723
046f93dc
DK
724 if (parser_ctx) {
725 bus_info->partition_uuid = parser_id_get(parser_ctx);
726 bus_info->name = parser_name_get(parser_ctx);
727 }
c71529fe 728
c71529fe 729 if (inmsg->hdr.flags.response_expected == 1)
4fb2539c 730 controlvm_responder(inmsg->hdr.id, &inmsg->hdr, err);
c71529fe
DK
731 return 0;
732
733err_respond:
71a0265d 734 dev_err(&chipset_dev->acpi_device->dev,
ec17cb8a 735 "visorbus_configure exited with err: %d\n", err);
b6b057d8 736 if (inmsg->hdr.flags.response_expected == 1)
4fb2539c 737 controlvm_responder(inmsg->hdr.id, &inmsg->hdr, err);
c71529fe 738 return err;
12e364b9
KC
739}
740
ad2a7d65 741static int
8b0a6cfa 742visorbus_device_create(struct controlvm_message *inmsg)
12e364b9 743{
2ea5117b 744 struct controlvm_message_packet *cmd = &inmsg->cmd;
5a80e98a 745 struct controlvm_message_header *pmsg_hdr = NULL;
52063eca
JS
746 u32 bus_no = cmd->create_device.bus_no;
747 u32 dev_no = cmd->create_device.dev_no;
a298bc0b 748 struct visor_device *dev_info = NULL;
d32517e3 749 struct visor_device *bus_info;
b32c4997 750 struct visorchannel *visorchannel;
ad2a7d65 751 int err;
12e364b9 752
a298bc0b
DZ
753 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
754 if (!bus_info) {
a8c26e4b
DK
755 dev_err(&chipset_dev->acpi_device->dev,
756 "failed to get bus by id: %d\n", bus_no);
ad2a7d65
DK
757 err = -ENODEV;
758 goto err_respond;
12e364b9 759 }
a298bc0b
DZ
760
761 if (bus_info->state.created == 0) {
a8c26e4b
DK
762 dev_err(&chipset_dev->acpi_device->dev,
763 "bus not created, id: %d\n", bus_no);
ad2a7d65
DK
764 err = -EINVAL;
765 goto err_respond;
12e364b9 766 }
a298bc0b
DZ
767
768 dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
769 if (dev_info && (dev_info->state.created == 1)) {
a8c26e4b
DK
770 dev_err(&chipset_dev->acpi_device->dev,
771 "failed to get bus by id: %d/%d\n", bus_no, dev_no);
ad2a7d65
DK
772 err = -EEXIST;
773 goto err_respond;
12e364b9 774 }
a298bc0b 775
c60c8e26
BR
776 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
777 if (!dev_info) {
ad2a7d65
DK
778 err = -ENOMEM;
779 goto err_respond;
12e364b9 780 }
97a84f12 781
a298bc0b
DZ
782 dev_info->chipset_bus_no = bus_no;
783 dev_info->chipset_dev_no = dev_no;
784 dev_info->inst = cmd->create_device.dev_inst_uuid;
785
786 /* not sure where the best place to set the 'parent' */
787 dev_info->device.parent = &bus_info->device;
788
a3ef1a8e
DK
789 visorchannel =
790 visorchannel_create_with_lock(cmd->create_device.channel_addr,
791 cmd->create_device.channel_bytes,
792 GFP_KERNEL,
793 cmd->create_device.data_type_uuid);
b32c4997
DZ
794
795 if (!visorchannel) {
a8c26e4b
DK
796 dev_err(&chipset_dev->acpi_device->dev,
797 "failed to create visorchannel: %d/%d\n",
798 bus_no, dev_no);
ad2a7d65
DK
799 err = -ENOMEM;
800 goto err_free_dev_info;
b32c4997
DZ
801 }
802 dev_info->visorchannel = visorchannel;
803 dev_info->channel_type_guid = cmd->create_device.data_type_uuid;
12c957dc 804 if (uuid_le_cmp(cmd->create_device.data_type_uuid,
ad2a7d65
DK
805 spar_vhba_channel_protocol_uuid) == 0) {
806 err = save_crash_message(inmsg, CRASH_DEV);
807 if (err)
3f49a21d 808 goto err_destroy_visorchannel;
ad2a7d65 809 }
12c957dc 810
5a80e98a
DK
811 if (inmsg->hdr.flags.response_expected == 1) {
812 pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
813 if (!pmsg_hdr) {
ad2a7d65 814 err = -ENOMEM;
3f49a21d 815 goto err_destroy_visorchannel;
5a80e98a
DK
816 }
817
818 memcpy(pmsg_hdr, &inmsg->hdr,
819 sizeof(struct controlvm_message_header));
820 dev_info->pending_msg_hdr = pmsg_hdr;
821 }
c0b44136
SW
822 /* visorchipset_device_create will send response */
823 err = visorchipset_device_create(dev_info);
3f49a21d
DK
824 if (err)
825 goto err_destroy_visorchannel;
826
ad2a7d65 827 return 0;
5a80e98a 828
3f49a21d
DK
829err_destroy_visorchannel:
830 visorchannel_destroy(visorchannel);
831
ad2a7d65 832err_free_dev_info:
5a80e98a
DK
833 kfree(dev_info);
834
ad2a7d65 835err_respond:
5a80e98a 836 if (inmsg->hdr.flags.response_expected == 1)
4fb2539c 837 controlvm_responder(inmsg->hdr.id, &inmsg->hdr, err);
ad2a7d65 838 return err;
12e364b9
KC
839}
840
40fc79f9 841static int
8b0a6cfa 842visorbus_device_changestate(struct controlvm_message *inmsg)
12e364b9 843{
2ea5117b 844 struct controlvm_message_packet *cmd = &inmsg->cmd;
8e609b5b 845 struct controlvm_message_header *pmsg_hdr = NULL;
52063eca
JS
846 u32 bus_no = cmd->device_change_state.bus_no;
847 u32 dev_no = cmd->device_change_state.dev_no;
545f0913 848 struct visor_segment_state state = cmd->device_change_state.state;
a298bc0b 849 struct visor_device *dev_info;
b4a8e6ae 850 int err = 0;
12e364b9 851
a298bc0b 852 dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
0278a905 853 if (!dev_info) {
40fc79f9 854 err = -ENODEV;
0825f191
DK
855 goto err_respond;
856 }
857 if (dev_info->state.created == 0) {
40fc79f9 858 err = -EINVAL;
0825f191 859 goto err_respond;
12e364b9 860 }
8e609b5b
DK
861 if (dev_info->pending_msg_hdr) {
862 /* only non-NULL if dev is still waiting on a response */
40fc79f9 863 err = -EIO;
8e609b5b
DK
864 goto err_respond;
865 }
866 if (inmsg->hdr.flags.response_expected == 1) {
867 pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
868 if (!pmsg_hdr) {
40fc79f9 869 err = -ENOMEM;
8e609b5b
DK
870 goto err_respond;
871 }
872
873 memcpy(pmsg_hdr, &inmsg->hdr,
874 sizeof(struct controlvm_message_header));
875 dev_info->pending_msg_hdr = pmsg_hdr;
876 }
877
878 if (state.alive == segment_state_running.alive &&
879 state.operating == segment_state_running.operating)
c0b44136
SW
880 /* Response will be sent from visorchipset_device_resume */
881 err = visorchipset_device_resume(dev_info);
8e609b5b
DK
882 /* ServerNotReady / ServerLost / SegmentStateStandby */
883 else if (state.alive == segment_state_standby.alive &&
884 state.operating == segment_state_standby.operating)
885 /*
886 * technically this is standby case where server is lost.
c0b44136 887 * Response will be sent from visorchipset_device_pause.
8e609b5b 888 */
c0b44136 889 err = visorchipset_device_pause(dev_info);
b4a8e6ae
DK
890 if (err)
891 goto err_respond;
892
40fc79f9 893 return 0;
0825f191
DK
894
895err_respond:
03662df8 896 dev_err(&chipset_dev->acpi_device->dev, "failed: %d\n", err);
8e609b5b 897 if (inmsg->hdr.flags.response_expected == 1)
4fb2539c 898 controlvm_responder(inmsg->hdr.id, &inmsg->hdr, err);
40fc79f9 899 return err;
12e364b9
KC
900}
901
e7954918 902static int
8b0a6cfa 903visorbus_device_destroy(struct controlvm_message *inmsg)
12e364b9 904{
2ea5117b 905 struct controlvm_message_packet *cmd = &inmsg->cmd;
9e9eec6b 906 struct controlvm_message_header *pmsg_hdr = NULL;
52063eca
JS
907 u32 bus_no = cmd->destroy_device.bus_no;
908 u32 dev_no = cmd->destroy_device.dev_no;
a298bc0b 909 struct visor_device *dev_info;
e7954918 910 int err;
12e364b9 911
a298bc0b 912 dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
9e9eec6b 913 if (!dev_info) {
e7954918 914 err = -ENODEV;
9e9eec6b
DK
915 goto err_respond;
916 }
917 if (dev_info->state.created == 0) {
e7954918 918 err = -EINVAL;
9e9eec6b
DK
919 goto err_respond;
920 }
12e364b9 921
9e9eec6b
DK
922 if (dev_info->pending_msg_hdr) {
923 /* only non-NULL if dev is still waiting on a response */
e7954918 924 err = -EIO;
9e9eec6b
DK
925 goto err_respond;
926 }
927 if (inmsg->hdr.flags.response_expected == 1) {
928 pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
929 if (!pmsg_hdr) {
e7954918 930 err = -ENOMEM;
9e9eec6b
DK
931 goto err_respond;
932 }
933
934 memcpy(pmsg_hdr, &inmsg->hdr,
935 sizeof(struct controlvm_message_header));
936 dev_info->pending_msg_hdr = pmsg_hdr;
937 }
938
c0b44136 939 visorchipset_device_destroy(dev_info);
e7954918 940 return 0;
9e9eec6b
DK
941
942err_respond:
943 if (inmsg->hdr.flags.response_expected == 1)
4fb2539c 944 controlvm_responder(inmsg->hdr.id, &inmsg->hdr, err);
e7954918 945 return err;
12e364b9
KC
946}
947
12e364b9 948/*
5d501ef4
DB
949 * The general parahotplug flow works as follows. The visorchipset receives
950 * a DEVICE_CHANGESTATE message from Command specifying a physical device
951 * to enable or disable. The CONTROLVM message handler calls
952 * parahotplug_process_message, which then adds the message to a global list
953 * and kicks off a udev event which causes a user level script to enable or
954 * disable the specified device. The udev script then writes to
955 * /sys/devices/platform/visorchipset/parahotplug, which causes the
956 * parahotplug store functions to get called, at which point the
957 * appropriate CONTROLVM message is retrieved from the list and responded
958 * to.
12e364b9
KC
959 */
960
961#define PARAHOTPLUG_TIMEOUT_MS 2000
962
04dbfea6 963/*
5d501ef4
DB
964 * parahotplug_next_id() - generate unique int to match an outstanding
965 * CONTROLVM message with a udev script /sys
966 * response
ec17f452
DB
967 *
968 * Return: a unique integer value
12e364b9
KC
969 */
970static int
971parahotplug_next_id(void)
972{
973 static atomic_t id = ATOMIC_INIT(0);
26eb2c0c 974
12e364b9
KC
975 return atomic_inc_return(&id);
976}
977
04dbfea6 978/*
ec17f452
DB
979 * parahotplug_next_expiration() - returns the time (in jiffies) when a
980 * CONTROLVM message on the list should expire
981 * -- PARAHOTPLUG_TIMEOUT_MS in the future
982 *
983 * Return: expected expiration time (in jiffies)
12e364b9
KC
984 */
985static unsigned long
986parahotplug_next_expiration(void)
987{
2cc1a1b3 988 return jiffies + msecs_to_jiffies(PARAHOTPLUG_TIMEOUT_MS);
12e364b9
KC
989}
990
04dbfea6 991/*
ec17f452
DB
992 * parahotplug_request_create() - create a parahotplug_request, which is
993 * basically a wrapper for a CONTROLVM_MESSAGE
994 * that we can stick on a list
995 * @msg: the message to insert in the request
996 *
997 * Return: the request containing the provided message
12e364b9
KC
998 */
999static struct parahotplug_request *
3ab47701 1000parahotplug_request_create(struct controlvm_message *msg)
12e364b9 1001{
ea0dcfcf
QL
1002 struct parahotplug_request *req;
1003
8c8c975f 1004 req = kmalloc(sizeof(*req), GFP_KERNEL);
38f736e9 1005 if (!req)
12e364b9
KC
1006 return NULL;
1007
1008 req->id = parahotplug_next_id();
1009 req->expiration = parahotplug_next_expiration();
1010 req->msg = *msg;
1011
1012 return req;
1013}
1014
04dbfea6 1015/*
ec17f452
DB
1016 * parahotplug_request_destroy() - free a parahotplug_request
1017 * @req: the request to deallocate
12e364b9
KC
1018 */
1019static void
1020parahotplug_request_destroy(struct parahotplug_request *req)
1021{
1022 kfree(req);
1023}
1024
51319662
DK
1025static LIST_HEAD(parahotplug_request_list);
1026static DEFINE_SPINLOCK(parahotplug_request_list_lock); /* lock for above */
1027
04dbfea6 1028/*
ec17f452
DB
1029 * parahotplug_request_complete() - mark request as complete
1030 * @id: the id of the request
1031 * @active: indicates whether the request is assigned to active partition
1032 *
5d501ef4 1033 * Called from the /sys handler, which means the user script has
ec17f452 1034 * finished the enable/disable. Find the matching identifier, and
12e364b9 1035 * respond to the CONTROLVM message with success.
ec17f452
DB
1036 *
1037 * Return: 0 on success or -EINVAL on failure
12e364b9
KC
1038 */
1039static int
b06bdf7d 1040parahotplug_request_complete(int id, u16 active)
12e364b9 1041{
e82ba62e
JS
1042 struct list_head *pos;
1043 struct list_head *tmp;
12e364b9 1044
ddf5de53 1045 spin_lock(&parahotplug_request_list_lock);
12e364b9
KC
1046
1047 /* Look for a request matching "id". */
ddf5de53 1048 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
12e364b9
KC
1049 struct parahotplug_request *req =
1050 list_entry(pos, struct parahotplug_request, list);
1051 if (req->id == id) {
ec17f452
DB
1052 /*
1053 * Found a match. Remove it from the list and
12e364b9
KC
1054 * respond.
1055 */
1056 list_del(pos);
ddf5de53 1057 spin_unlock(&parahotplug_request_list_lock);
2ea5117b 1058 req->msg.cmd.device_change_state.state.active = active;
98d7b594 1059 if (req->msg.hdr.flags.response_expected)
4c0e65f8
DK
1060 controlvm_respond(
1061 &req->msg.hdr, CONTROLVM_RESP_SUCCESS,
1062 &req->msg.cmd.device_change_state.state);
12e364b9
KC
1063 parahotplug_request_destroy(req);
1064 return 0;
1065 }
1066 }
1067
ddf5de53 1068 spin_unlock(&parahotplug_request_list_lock);
119296ea 1069 return -EINVAL;
12e364b9
KC
1070}
1071
04dbfea6 1072/*
ebeff055
DK
1073 * devicedisabled_store() - disables the hotplug device
1074 * @dev: sysfs interface variable not utilized in this function
1075 * @attr: sysfs interface variable not utilized in this function
1076 * @buf: buffer containing the device id
1077 * @count: the size of the buffer
1078 *
1079 * The parahotplug/devicedisabled interface gets called by our support script
1080 * when an SR-IOV device has been shut down. The ID is passed to the script
1081 * and then passed back when the device has been removed.
1082 *
1083 * Return: the size of the buffer for success or negative for error
1084 */
1085static ssize_t devicedisabled_store(struct device *dev,
1086 struct device_attribute *attr,
1087 const char *buf, size_t count)
1088{
1089 unsigned int id;
1090 int err;
1091
1092 if (kstrtouint(buf, 10, &id))
1093 return -EINVAL;
1094
1095 err = parahotplug_request_complete(id, 0);
1096 if (err < 0)
1097 return err;
1098 return count;
1099}
1100static DEVICE_ATTR_WO(devicedisabled);
1101
04dbfea6 1102/*
ebeff055
DK
1103 * deviceenabled_store() - enables the hotplug device
1104 * @dev: sysfs interface variable not utilized in this function
1105 * @attr: sysfs interface variable not utilized in this function
1106 * @buf: buffer containing the device id
1107 * @count: the size of the buffer
1108 *
1109 * The parahotplug/deviceenabled interface gets called by our support script
1110 * when an SR-IOV device has been recovered. The ID is passed to the script
1111 * and then passed back when the device has been brought back up.
1112 *
1113 * Return: the size of the buffer for success or negative for error
1114 */
1115static ssize_t deviceenabled_store(struct device *dev,
1116 struct device_attribute *attr,
1117 const char *buf, size_t count)
1118{
1119 unsigned int id;
1120
1121 if (kstrtouint(buf, 10, &id))
1122 return -EINVAL;
1123
1124 parahotplug_request_complete(id, 1);
1125 return count;
1126}
1127static DEVICE_ATTR_WO(deviceenabled);
1128
1129static struct attribute *visorchipset_install_attrs[] = {
1130 &dev_attr_toolaction.attr,
1131 &dev_attr_boottotool.attr,
1132 &dev_attr_error.attr,
1133 &dev_attr_textid.attr,
1134 &dev_attr_remaining_steps.attr,
1135 NULL
1136};
1137
a2d1e428 1138static const struct attribute_group visorchipset_install_group = {
ebeff055
DK
1139 .name = "install",
1140 .attrs = visorchipset_install_attrs
1141};
1142
1143static struct attribute *visorchipset_parahotplug_attrs[] = {
1144 &dev_attr_devicedisabled.attr,
1145 &dev_attr_deviceenabled.attr,
1146 NULL
1147};
1148
1149static struct attribute_group visorchipset_parahotplug_group = {
1150 .name = "parahotplug",
1151 .attrs = visorchipset_parahotplug_attrs
1152};
1153
1154static const struct attribute_group *visorchipset_dev_groups[] = {
1155 &visorchipset_install_group,
1156 &visorchipset_parahotplug_group,
1157 NULL
1158};
1159
04dbfea6 1160/*
ebeff055
DK
1161 * parahotplug_request_kickoff() - initiate parahotplug request
1162 * @req: the request to initiate
1163 *
1164 * Cause uevent to run the user level script to do the disable/enable specified
1165 * in the parahotplug_request.
1166 */
ae0fa822 1167static int
ebeff055
DK
1168parahotplug_request_kickoff(struct parahotplug_request *req)
1169{
1170 struct controlvm_message_packet *cmd = &req->msg.cmd;
1171 char env_cmd[40], env_id[40], env_state[40], env_bus[40], env_dev[40],
1172 env_func[40];
1173 char *envp[] = {
1174 env_cmd, env_id, env_state, env_bus, env_dev, env_func, NULL
1175 };
1176
c5a28902
SW
1177 sprintf(env_cmd, "VISOR_PARAHOTPLUG=1");
1178 sprintf(env_id, "VISOR_PARAHOTPLUG_ID=%d", req->id);
1179 sprintf(env_state, "VISOR_PARAHOTPLUG_STATE=%d",
ebeff055 1180 cmd->device_change_state.state.active);
c5a28902 1181 sprintf(env_bus, "VISOR_PARAHOTPLUG_BUS=%d",
ebeff055 1182 cmd->device_change_state.bus_no);
c5a28902 1183 sprintf(env_dev, "VISOR_PARAHOTPLUG_DEVICE=%d",
ebeff055 1184 cmd->device_change_state.dev_no >> 3);
c5a28902 1185 sprintf(env_func, "VISOR_PARAHOTPLUG_FUNCTION=%d",
ebeff055
DK
1186 cmd->device_change_state.dev_no & 0x7);
1187
ae0fa822
DK
1188 return kobject_uevent_env(&chipset_dev->acpi_device->dev.kobj,
1189 KOBJ_CHANGE, envp);
ebeff055
DK
1190}
1191
04dbfea6 1192/*
ec17f452
DB
1193 * parahotplug_process_message() - enables or disables a PCI device by kicking
1194 * off a udev script
1195 * @inmsg: the message indicating whether to enable or disable
12e364b9 1196 */
114d5dcf 1197static int
3ab47701 1198parahotplug_process_message(struct controlvm_message *inmsg)
12e364b9
KC
1199{
1200 struct parahotplug_request *req;
ae0fa822 1201 int err;
12e364b9
KC
1202
1203 req = parahotplug_request_create(inmsg);
1204
38f736e9 1205 if (!req)
114d5dcf 1206 return -ENOMEM;
12e364b9 1207
d02bde9d
DK
1208 /*
1209 * For enable messages, just respond with success right away, we don't
1210 * need to wait to see if the enable was successful.
1211 */
2ea5117b 1212 if (inmsg->cmd.device_change_state.state.active) {
ae0fa822
DK
1213 err = parahotplug_request_kickoff(req);
1214 if (err)
1215 goto err_respond;
4c0e65f8
DK
1216 controlvm_respond(&inmsg->hdr, CONTROLVM_RESP_SUCCESS,
1217 &inmsg->cmd.device_change_state.state);
12e364b9 1218 parahotplug_request_destroy(req);
ae0fa822 1219 return 0;
12e364b9 1220 }
ae0fa822
DK
1221
1222 /*
1223 * For disable messages, add the request to the
1224 * request list before kicking off the udev script. It
1225 * won't get responded to until the script has
1226 * indicated it's done.
1227 */
1228 spin_lock(&parahotplug_request_list_lock);
1229 list_add_tail(&req->list, &parahotplug_request_list);
1230 spin_unlock(&parahotplug_request_list_lock);
1231
1232 err = parahotplug_request_kickoff(req);
1233 if (err)
1234 goto err_respond;
114d5dcf 1235 return 0;
ae0fa822
DK
1236
1237err_respond:
4c0e65f8
DK
1238 controlvm_respond(&inmsg->hdr, err,
1239 &inmsg->cmd.device_change_state.state);
ae0fa822 1240 return err;
12e364b9
KC
1241}
1242
7289a8dd
DB
1243/*
1244 * chipset_ready_uevent() - sends chipset_ready action
ebeff055
DK
1245 *
1246 * Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
1247 *
7289a8dd 1248 * Return: 0 on success, negative on failure
ebeff055
DK
1249 */
1250static int
7289a8dd 1251chipset_ready_uevent(struct controlvm_message_header *msg_hdr)
ebeff055 1252{
deeeca6d
DK
1253 int res;
1254
1255 res = kobject_uevent(&chipset_dev->acpi_device->dev.kobj,
1256 KOBJ_ONLINE);
7289a8dd
DB
1257
1258 if (msg_hdr->flags.response_expected)
4c0e65f8 1259 controlvm_respond(msg_hdr, res, NULL);
7289a8dd 1260
deeeca6d 1261 return res;
ebeff055
DK
1262}
1263
7289a8dd
DB
1264/*
1265 * chipset_selftest_uevent() - sends chipset_selftest action
1266 *
1267 * Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
1268 *
1269 * Return: 0 on success, negative on failure
1270 */
ebeff055 1271static int
7289a8dd 1272chipset_selftest_uevent(struct controlvm_message_header *msg_hdr)
ebeff055
DK
1273{
1274 char env_selftest[20];
1275 char *envp[] = { env_selftest, NULL };
deeeca6d 1276 int res;
ebeff055
DK
1277
1278 sprintf(env_selftest, "SPARSP_SELFTEST=%d", 1);
deeeca6d
DK
1279 res = kobject_uevent_env(&chipset_dev->acpi_device->dev.kobj,
1280 KOBJ_CHANGE, envp);
7289a8dd
DB
1281
1282 if (msg_hdr->flags.response_expected)
4c0e65f8 1283 controlvm_respond(msg_hdr, res, NULL);
7289a8dd 1284
deeeca6d 1285 return res;
ebeff055
DK
1286}
1287
7289a8dd
DB
1288/*
1289 * chipset_notready_uevent() - sends chipset_notready action
ebeff055
DK
1290 *
1291 * Send ACTION=offline for DEVPATH=/sys/devices/platform/visorchipset.
1292 *
7289a8dd 1293 * Return: 0 on success, negative on failure
ebeff055
DK
1294 */
1295static int
7289a8dd 1296chipset_notready_uevent(struct controlvm_message_header *msg_hdr)
ebeff055 1297{
deeeca6d 1298 int res;
ebeff055 1299
deeeca6d
DK
1300 res = kobject_uevent(&chipset_dev->acpi_device->dev.kobj,
1301 KOBJ_OFFLINE);
ebeff055 1302 if (msg_hdr->flags.response_expected)
4c0e65f8 1303 controlvm_respond(msg_hdr, res, NULL);
ebeff055 1304
deeeca6d 1305 return res;
ebeff055
DK
1306}
1307
88845f40
DK
1308static int unisys_vmcall(unsigned long tuple, unsigned long param)
1309{
1310 int result = 0;
1311 unsigned int cpuid_eax, cpuid_ebx, cpuid_ecx, cpuid_edx;
1312 unsigned long reg_ebx;
1313 unsigned long reg_ecx;
1314
1315 reg_ebx = param & 0xFFFFFFFF;
1316 reg_ecx = param >> 32;
1317
1318 cpuid(0x00000001, &cpuid_eax, &cpuid_ebx, &cpuid_ecx, &cpuid_edx);
1319 if (!(cpuid_ecx & 0x80000000))
1320 return -EPERM;
1321
1322 __asm__ __volatile__(".byte 0x00f, 0x001, 0x0c1" : "=a"(result) :
1323 "a"(tuple), "b"(reg_ebx), "c"(reg_ecx));
1324
bd801a07
DK
1325 if (result)
1326 goto error;
1327
1328 return 0;
1329
1330error: /* Need to convert from VMCALL error codes to Linux */
1331 switch (result) {
1332 case VMCALL_RESULT_INVALID_PARAM:
1333 return -EINVAL;
1334 case VMCALL_RESULT_DATA_UNAVAILABLE:
1335 return -ENODEV;
1336 default:
1337 return -EFAULT;
1338 }
88845f40 1339}
7d91a1f1 1340static unsigned int
5f3a7e36
DK
1341issue_vmcall_io_controlvm_addr(u64 *control_addr, u32 *control_bytes)
1342{
bb1fb8ac
SW
1343 chipset_dev->controlvm_addr.physaddr = virt_to_phys(
1344 &chipset_dev->controlvm_addr.params);
1345 chipset_dev->controlvm_addr.err = unisys_vmcall(VMCALL_CONTROLVM_ADDR,
1346 chipset_dev->controlvm_addr.physaddr);
1347 if (chipset_dev->controlvm_addr.err)
1348 return chipset_dev->controlvm_addr.err;
5f3a7e36 1349
bb1fb8ac
SW
1350 *control_addr = chipset_dev->controlvm_addr.params.address;
1351 *control_bytes = chipset_dev->controlvm_addr.params.channel_bytes;
bd801a07
DK
1352
1353 return 0;
5f3a7e36
DK
1354}
1355
d5b3f1dc 1356static u64 controlvm_get_channel_address(void)
524b0b63 1357{
5fc0229a 1358 u64 addr = 0;
b3c55b13 1359 u32 size = 0;
524b0b63 1360
bd801a07 1361 if (issue_vmcall_io_controlvm_addr(&addr, &size))
524b0b63 1362 return 0;
0aca7844 1363
524b0b63
BR
1364 return addr;
1365}
1366
12e364b9
KC
1367static void
1368setup_crash_devices_work_queue(struct work_struct *work)
1369{
e6bdb904
BR
1370 struct controlvm_message local_crash_bus_msg;
1371 struct controlvm_message local_crash_dev_msg;
3ab47701 1372 struct controlvm_message msg;
e6bdb904
BR
1373 u32 local_crash_msg_offset;
1374 u16 local_crash_msg_count;
12e364b9 1375
12e364b9 1376 /* send init chipset msg */
98d7b594 1377 msg.hdr.id = CONTROLVM_CHIPSET_INIT;
2ea5117b
BR
1378 msg.cmd.init_chipset.bus_count = 23;
1379 msg.cmd.init_chipset.switch_count = 0;
12e364b9
KC
1380
1381 chipset_init(&msg);
1382
12e364b9 1383 /* get saved message count */
765b2f82 1384 if (visorchannel_read(chipset_dev->controlvm_channel,
545f0913 1385 offsetof(struct visor_controlvm_channel,
d19642f6 1386 saved_crash_message_count),
e6bdb904 1387 &local_crash_msg_count, sizeof(u16)) < 0) {
0f7453af
DK
1388 dev_err(&chipset_dev->acpi_device->dev,
1389 "failed to read channel\n");
12e364b9
KC
1390 return;
1391 }
1392
e6bdb904 1393 if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
0f7453af
DK
1394 dev_err(&chipset_dev->acpi_device->dev,
1395 "invalid count\n");
12e364b9
KC
1396 return;
1397 }
1398
1399 /* get saved crash message offset */
765b2f82 1400 if (visorchannel_read(chipset_dev->controlvm_channel,
545f0913 1401 offsetof(struct visor_controlvm_channel,
d19642f6 1402 saved_crash_message_offset),
e6bdb904 1403 &local_crash_msg_offset, sizeof(u32)) < 0) {
0f7453af
DK
1404 dev_err(&chipset_dev->acpi_device->dev,
1405 "failed to read channel\n");
12e364b9
KC
1406 return;
1407 }
1408
1409 /* read create device message for storage bus offset */
765b2f82 1410 if (visorchannel_read(chipset_dev->controlvm_channel,
e6bdb904
BR
1411 local_crash_msg_offset,
1412 &local_crash_bus_msg,
3ab47701 1413 sizeof(struct controlvm_message)) < 0) {
0f7453af
DK
1414 dev_err(&chipset_dev->acpi_device->dev,
1415 "failed to read channel\n");
12e364b9
KC
1416 return;
1417 }
1418
1419 /* read create device message for storage device */
765b2f82 1420 if (visorchannel_read(chipset_dev->controlvm_channel,
e6bdb904 1421 local_crash_msg_offset +
3ab47701 1422 sizeof(struct controlvm_message),
e6bdb904 1423 &local_crash_dev_msg,
3ab47701 1424 sizeof(struct controlvm_message)) < 0) {
0f7453af
DK
1425 dev_err(&chipset_dev->acpi_device->dev,
1426 "failed to read channel\n");
12e364b9
KC
1427 return;
1428 }
1429
1430 /* reuse IOVM create bus message */
d9b89ef1 1431 if (!local_crash_bus_msg.cmd.create_bus.channel_addr) {
0f7453af
DK
1432 dev_err(&chipset_dev->acpi_device->dev,
1433 "no valid create_bus message\n");
12e364b9
KC
1434 return;
1435 }
ec17cb8a 1436 visorbus_create(&local_crash_bus_msg);
12e364b9
KC
1437
1438 /* reuse create device message for storage device */
d9b89ef1 1439 if (!local_crash_dev_msg.cmd.create_device.channel_addr) {
0f7453af
DK
1440 dev_err(&chipset_dev->acpi_device->dev,
1441 "no valid create_device message\n");
12e364b9
KC
1442 return;
1443 }
8b0a6cfa 1444 visorbus_device_create(&local_crash_dev_msg);
12e364b9
KC
1445}
1446
87241ab8 1447void
63847f17 1448visorbus_create_response(struct visor_device *bus_info, int response)
12e364b9 1449{
4b4fd43a 1450 if (response >= 0)
0274b5ae 1451 bus_info->state.created = 1;
0274b5ae 1452
4fb2539c
DK
1453 controlvm_responder(CONTROLVM_BUS_CREATE, bus_info->pending_msg_hdr,
1454 response);
0274b5ae
DZ
1455
1456 kfree(bus_info->pending_msg_hdr);
1457 bus_info->pending_msg_hdr = NULL;
12e364b9
KC
1458}
1459
87241ab8 1460void
63847f17 1461visorbus_destroy_response(struct visor_device *bus_info, int response)
12e364b9 1462{
4fb2539c
DK
1463 controlvm_responder(CONTROLVM_BUS_DESTROY, bus_info->pending_msg_hdr,
1464 response);
0274b5ae
DZ
1465
1466 kfree(bus_info->pending_msg_hdr);
1467 bus_info->pending_msg_hdr = NULL;
12e364b9
KC
1468}
1469
87241ab8 1470void
f8b5a21f 1471visorbus_device_create_response(struct visor_device *dev_info, int response)
12e364b9 1472{
0274b5ae
DZ
1473 if (response >= 0)
1474 dev_info->state.created = 1;
1475
4fb2539c
DK
1476 controlvm_responder(CONTROLVM_DEVICE_CREATE, dev_info->pending_msg_hdr,
1477 response);
0274b5ae
DZ
1478
1479 kfree(dev_info->pending_msg_hdr);
addce19f 1480 dev_info->pending_msg_hdr = NULL;
12e364b9
KC
1481}
1482
87241ab8 1483void
f8b5a21f 1484visorbus_device_destroy_response(struct visor_device *dev_info, int response)
12e364b9 1485{
4fb2539c
DK
1486 controlvm_responder(CONTROLVM_DEVICE_DESTROY, dev_info->pending_msg_hdr,
1487 response);
0274b5ae
DZ
1488
1489 kfree(dev_info->pending_msg_hdr);
1490 dev_info->pending_msg_hdr = NULL;
12e364b9
KC
1491}
1492
87241ab8 1493void
f8b5a21f 1494visorbus_device_pause_response(struct visor_device *dev_info, int response)
12e364b9 1495{
12e364b9 1496 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
b4b598fd 1497 dev_info, response,
bd0d2dcc 1498 segment_state_standby);
0274b5ae
DZ
1499
1500 kfree(dev_info->pending_msg_hdr);
1501 dev_info->pending_msg_hdr = NULL;
12e364b9 1502}
12e364b9 1503
87241ab8 1504void
f8b5a21f 1505visorbus_device_resume_response(struct visor_device *dev_info, int response)
12e364b9
KC
1506{
1507 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
b4b598fd 1508 dev_info, response,
bd0d2dcc 1509 segment_state_running);
0274b5ae
DZ
1510
1511 kfree(dev_info->pending_msg_hdr);
1512 dev_info->pending_msg_hdr = NULL;
12e364b9
KC
1513}
1514
612b81c9
DK
1515static struct parser_context *
1516parser_init_byte_stream(u64 addr, u32 bytes, bool local, bool *retry)
1517{
1518 int allocbytes = sizeof(struct parser_context) + bytes;
1519 struct parser_context *ctx;
1520
3e4273db 1521 *retry = false;
612b81c9
DK
1522
1523 /*
1524 * alloc an 0 extra byte to ensure payload is
1525 * '\0'-terminated
1526 */
1527 allocbytes++;
765b2f82 1528 if ((chipset_dev->controlvm_payload_bytes_buffered + bytes)
612b81c9 1529 > MAX_CONTROLVM_PAYLOAD_BYTES) {
3e4273db 1530 *retry = true;
612b81c9
DK
1531 return NULL;
1532 }
8c8c975f 1533 ctx = kzalloc(allocbytes, GFP_KERNEL);
612b81c9 1534 if (!ctx) {
3e4273db 1535 *retry = true;
612b81c9
DK
1536 return NULL;
1537 }
1538
1539 ctx->allocbytes = allocbytes;
1540 ctx->param_bytes = bytes;
1541 ctx->curr = NULL;
1542 ctx->bytes_remaining = 0;
1543 ctx->byte_stream = false;
1544 if (local) {
1545 void *p;
1546
1547 if (addr > virt_to_phys(high_memory - 1))
1548 goto err_finish_ctx;
1549 p = __va((unsigned long)(addr));
1550 memcpy(ctx->data, p, bytes);
1551 } else {
1552 void *mapping = memremap(addr, bytes, MEMREMAP_WB);
1553
1554 if (!mapping)
1555 goto err_finish_ctx;
1556 memcpy(ctx->data, mapping, bytes);
1557 memunmap(mapping);
1558 }
1559
1560 ctx->byte_stream = true;
765b2f82 1561 chipset_dev->controlvm_payload_bytes_buffered += ctx->param_bytes;
612b81c9
DK
1562
1563 return ctx;
1564
1565err_finish_ctx:
1566 parser_done(ctx);
1567 return NULL;
1568}
1569
04dbfea6 1570/*
511474a5
DK
1571 * handle_command() - process a controlvm message
1572 * @inmsg: the message to process
1573 * @channel_addr: address of the controlvm channel
1574 *
1575 * Return:
25a5128e
DK
1576 * 0 - Successfully processed the message
1577 * -EAGAIN - ControlVM message was not processed and should be retried
1578 * reading the next controlvm message; a scenario where this can
1579 * occur is when we need to throttle the allocation of memory in
1580 * which to copy out controlvm payload data.
1581 * < 0 - error: ControlVM message was processed but an error occurred.
511474a5 1582 */
25a5128e 1583static int
511474a5
DK
1584handle_command(struct controlvm_message inmsg, u64 channel_addr)
1585{
1586 struct controlvm_message_packet *cmd = &inmsg.cmd;
1587 u64 parm_addr;
1588 u32 parm_bytes;
1589 struct parser_context *parser_ctx = NULL;
1590 bool local_addr;
1591 struct controlvm_message ackmsg;
25a5128e 1592 int err = 0;
511474a5
DK
1593
1594 /* create parsing context if necessary */
1595 local_addr = (inmsg.hdr.flags.test_message == 1);
1596 if (channel_addr == 0)
25a5128e
DK
1597 return -EINVAL;
1598
511474a5
DK
1599 parm_addr = channel_addr + inmsg.hdr.payload_vm_offset;
1600 parm_bytes = inmsg.hdr.payload_bytes;
1601
1602 /*
1603 * Parameter and channel addresses within test messages actually lie
1604 * within our OS-controlled memory. We need to know that, because it
1605 * makes a difference in how we compute the virtual address.
1606 */
1607 if (parm_addr && parm_bytes) {
1608 bool retry = false;
1609
1610 parser_ctx =
1611 parser_init_byte_stream(parm_addr, parm_bytes,
1612 local_addr, &retry);
1613 if (!parser_ctx && retry)
25a5128e 1614 return -EAGAIN;
511474a5
DK
1615 }
1616
1617 if (!local_addr) {
1618 controlvm_init_response(&ackmsg, &inmsg.hdr,
1619 CONTROLVM_RESP_SUCCESS);
25a5128e
DK
1620 err = visorchannel_signalinsert(chipset_dev->controlvm_channel,
1621 CONTROLVM_QUEUE_ACK,
1622 &ackmsg);
1623 if (err)
1624 return err;
511474a5
DK
1625 }
1626 switch (inmsg.hdr.id) {
1627 case CONTROLVM_CHIPSET_INIT:
25a5128e 1628 err = chipset_init(&inmsg);
511474a5
DK
1629 break;
1630 case CONTROLVM_BUS_CREATE:
ec17cb8a 1631 err = visorbus_create(&inmsg);
511474a5
DK
1632 break;
1633 case CONTROLVM_BUS_DESTROY:
ec17cb8a 1634 err = visorbus_destroy(&inmsg);
511474a5
DK
1635 break;
1636 case CONTROLVM_BUS_CONFIGURE:
ec17cb8a 1637 err = visorbus_configure(&inmsg, parser_ctx);
511474a5
DK
1638 break;
1639 case CONTROLVM_DEVICE_CREATE:
8b0a6cfa 1640 err = visorbus_device_create(&inmsg);
511474a5
DK
1641 break;
1642 case CONTROLVM_DEVICE_CHANGESTATE:
1643 if (cmd->device_change_state.flags.phys_device) {
25a5128e 1644 err = parahotplug_process_message(&inmsg);
511474a5
DK
1645 } else {
1646 /*
1647 * save the hdr and cmd structures for later use
1648 * when sending back the response to Command
1649 */
8b0a6cfa 1650 err = visorbus_device_changestate(&inmsg);
511474a5
DK
1651 break;
1652 }
1653 break;
1654 case CONTROLVM_DEVICE_DESTROY:
8b0a6cfa 1655 err = visorbus_device_destroy(&inmsg);
511474a5
DK
1656 break;
1657 case CONTROLVM_DEVICE_CONFIGURE:
25a5128e 1658 /* no op just send a respond that we passed */
511474a5 1659 if (inmsg.hdr.flags.response_expected)
4c0e65f8
DK
1660 controlvm_respond(&inmsg.hdr, CONTROLVM_RESP_SUCCESS,
1661 NULL);
511474a5
DK
1662 break;
1663 case CONTROLVM_CHIPSET_READY:
25a5128e 1664 err = chipset_ready_uevent(&inmsg.hdr);
511474a5
DK
1665 break;
1666 case CONTROLVM_CHIPSET_SELFTEST:
25a5128e 1667 err = chipset_selftest_uevent(&inmsg.hdr);
511474a5
DK
1668 break;
1669 case CONTROLVM_CHIPSET_STOP:
25a5128e 1670 err = chipset_notready_uevent(&inmsg.hdr);
511474a5
DK
1671 break;
1672 default:
25a5128e 1673 err = -ENOMSG;
511474a5 1674 if (inmsg.hdr.flags.response_expected)
25a5128e 1675 controlvm_respond(&inmsg.hdr,
4c0e65f8 1676 -CONTROLVM_RESP_ID_UNKNOWN, NULL);
511474a5
DK
1677 break;
1678 }
1679
1680 if (parser_ctx) {
1681 parser_done(parser_ctx);
1682 parser_ctx = NULL;
1683 }
25a5128e 1684 return err;
511474a5
DK
1685}
1686
04dbfea6 1687/*
8a285327
DK
1688 * read_controlvm_event() - retreives the next message from the
1689 * CONTROLVM_QUEUE_EVENT queue in the controlvm
1690 * channel
1691 * @msg: pointer to the retrieved message
1692 *
25a5128e 1693 * Return: 0 if valid message was retrieved or -error
8a285327 1694 */
25a5128e 1695static int
8a285327
DK
1696read_controlvm_event(struct controlvm_message *msg)
1697{
25a5128e
DK
1698 int err;
1699
1700 err = visorchannel_signalremove(chipset_dev->controlvm_channel,
1701 CONTROLVM_QUEUE_EVENT, msg);
1702 if (err)
1703 return err;
1704
1705 /* got a message */
1706 if (msg->hdr.flags.test_message == 1)
1707 return -EINVAL;
1708
1709 return 0;
8a285327
DK
1710}
1711
04dbfea6 1712/*
a9c73937
DK
1713 * parahotplug_process_list() - remove any request from the list that's been on
1714 * there too long and respond with an error
1715 */
1716static void
1717parahotplug_process_list(void)
1718{
1719 struct list_head *pos;
1720 struct list_head *tmp;
1721
1722 spin_lock(&parahotplug_request_list_lock);
1723
1724 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
1725 struct parahotplug_request *req =
1726 list_entry(pos, struct parahotplug_request, list);
1727
1728 if (!time_after_eq(jiffies, req->expiration))
1729 continue;
1730
1731 list_del(pos);
1732 if (req->msg.hdr.flags.response_expected)
4c0e65f8 1733 controlvm_respond(
a9c73937 1734 &req->msg.hdr,
98f9ed9e 1735 CONTROLVM_RESP_DEVICE_UDEV_TIMEOUT,
4c0e65f8 1736 &req->msg.cmd.device_change_state.state);
a9c73937
DK
1737 parahotplug_request_destroy(req);
1738 }
1739
1740 spin_unlock(&parahotplug_request_list_lock);
1741}
1742
3d8394c8
DK
1743static void
1744controlvm_periodic_work(struct work_struct *work)
1745{
1746 struct controlvm_message inmsg;
04dbc09b 1747 int count = 0;
fbc1023a
DK
1748 int err;
1749
1750 /* Drain the RESPONSE queue make it empty */
1751 do {
1752 err = visorchannel_signalremove(chipset_dev->controlvm_channel,
1753 CONTROLVM_QUEUE_RESPONSE,
1754 &inmsg);
04dbc09b 1755 } while ((!err) && (++count < CONTROLVM_MESSAGE_MAX));
fbc1023a
DK
1756
1757 if (err != -EAGAIN)
1758 goto schedule_out;
1759
1760 if (chipset_dev->controlvm_pending_msg_valid) {
1761 /*
1762 * we throttled processing of a prior
1763 * msg, so try to process it again
1764 * rather than reading a new one
1765 */
1766 inmsg = chipset_dev->controlvm_pending_msg;
1767 chipset_dev->controlvm_pending_msg_valid = false;
1768 err = 0;
1769 } else {
1770 err = read_controlvm_event(&inmsg);
3d8394c8
DK
1771 }
1772
fbc1023a 1773 while (!err) {
765b2f82 1774 chipset_dev->most_recent_message_jiffies = jiffies;
fbc1023a
DK
1775 err = handle_command(inmsg,
1776 visorchannel_get_physaddr
1777 (chipset_dev->controlvm_channel));
1778 if (err == -EAGAIN) {
765b2f82
SW
1779 chipset_dev->controlvm_pending_msg = inmsg;
1780 chipset_dev->controlvm_pending_msg_valid = true;
fbc1023a 1781 break;
3d8394c8 1782 }
fbc1023a
DK
1783
1784 err = read_controlvm_event(&inmsg);
3d8394c8
DK
1785 }
1786
1787 /* parahotplug_worker */
1788 parahotplug_process_list();
1789
fbc1023a 1790schedule_out:
765b2f82
SW
1791 if (time_after(jiffies, chipset_dev->most_recent_message_jiffies +
1792 (HZ * MIN_IDLE_SECONDS))) {
3d8394c8
DK
1793 /*
1794 * it's been longer than MIN_IDLE_SECONDS since we
1795 * processed our last controlvm message; slow down the
1796 * polling
1797 */
765b2f82
SW
1798 if (chipset_dev->poll_jiffies !=
1799 POLLJIFFIES_CONTROLVMCHANNEL_SLOW)
1800 chipset_dev->poll_jiffies =
1801 POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
3d8394c8 1802 } else {
765b2f82
SW
1803 if (chipset_dev->poll_jiffies !=
1804 POLLJIFFIES_CONTROLVMCHANNEL_FAST)
1805 chipset_dev->poll_jiffies =
1806 POLLJIFFIES_CONTROLVMCHANNEL_FAST;
3d8394c8
DK
1807 }
1808
765b2f82
SW
1809 schedule_delayed_work(&chipset_dev->periodic_controlvm_work,
1810 chipset_dev->poll_jiffies);
3d8394c8
DK
1811}
1812
55c67dca
PB
1813static int
1814visorchipset_init(struct acpi_device *acpi_device)
12e364b9 1815{
1366a3db 1816 int err = -ENODEV;
d5b3f1dc 1817 u64 addr;
c5a28902 1818 uuid_le uuid = VISOR_CONTROLVM_CHANNEL_UUID;
765b2f82 1819 struct visorchannel *controlvm_channel;
d3368a58 1820
765b2f82
SW
1821 chipset_dev = kzalloc(sizeof(*chipset_dev), GFP_KERNEL);
1822 if (!chipset_dev)
1366a3db
DK
1823 goto error;
1824
bb1fb8ac
SW
1825 addr = controlvm_get_channel_address();
1826 if (!addr)
1827 goto error;
1828
765b2f82
SW
1829 acpi_device->driver_data = chipset_dev;
1830
1831 chipset_dev->acpi_device = acpi_device;
1832 chipset_dev->poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
1833 controlvm_channel = visorchannel_create_with_lock(addr,
1834 0, GFP_KERNEL, uuid);
1835
1836 if (!controlvm_channel)
1837 goto error_free_chipset_dev;
1838
1839 chipset_dev->controlvm_channel = controlvm_channel;
15c012d5
SW
1840
1841 err = sysfs_create_groups(&chipset_dev->acpi_device->dev.kobj,
1842 visorchipset_dev_groups);
1843 if (err < 0)
1844 goto error_destroy_channel;
1845
c5a28902 1846 if (!VISOR_CONTROLVM_CHANNEL_OK_CLIENT(
d308d2b5 1847 visorchannel_get_header(controlvm_channel)))
15c012d5 1848 goto error_delete_groups;
8a1182eb 1849
4da3336c
DK
1850 /* if booting in a crash kernel */
1851 if (is_kdump_kernel())
765b2f82 1852 INIT_DELAYED_WORK(&chipset_dev->periodic_controlvm_work,
4da3336c
DK
1853 setup_crash_devices_work_queue);
1854 else
765b2f82 1855 INIT_DELAYED_WORK(&chipset_dev->periodic_controlvm_work,
4da3336c 1856 controlvm_periodic_work);
4da3336c 1857
765b2f82
SW
1858 chipset_dev->most_recent_message_jiffies = jiffies;
1859 chipset_dev->poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
1860 schedule_delayed_work(&chipset_dev->periodic_controlvm_work,
1861 chipset_dev->poll_jiffies);
12e364b9 1862
1366a3db
DK
1863 err = visorbus_init();
1864 if (err < 0)
15c012d5 1865 goto error_cancel_work;
12e364b9 1866
1366a3db
DK
1867 return 0;
1868
1366a3db 1869error_cancel_work:
765b2f82 1870 cancel_delayed_work_sync(&chipset_dev->periodic_controlvm_work);
1366a3db 1871
15c012d5
SW
1872error_delete_groups:
1873 sysfs_remove_groups(&chipset_dev->acpi_device->dev.kobj,
1874 visorchipset_dev_groups);
1875
1366a3db 1876error_destroy_channel:
765b2f82
SW
1877 visorchannel_destroy(chipset_dev->controlvm_channel);
1878
1879error_free_chipset_dev:
1880 kfree(chipset_dev);
1366a3db
DK
1881
1882error:
372b9f22 1883 dev_err(&acpi_device->dev, "failed with error %d\n", err);
1366a3db 1884 return err;
e3420ed6
EA
1885}
1886
55c67dca
PB
1887static int
1888visorchipset_exit(struct acpi_device *acpi_device)
12e364b9 1889{
c79b28f7 1890 visorbus_exit();
765b2f82 1891 cancel_delayed_work_sync(&chipset_dev->periodic_controlvm_work);
15c012d5
SW
1892 sysfs_remove_groups(&chipset_dev->acpi_device->dev.kobj,
1893 visorchipset_dev_groups);
1894
765b2f82 1895 visorchannel_destroy(chipset_dev->controlvm_channel);
765b2f82
SW
1896 kfree(chipset_dev);
1897
55c67dca
PB
1898 return 0;
1899}
1900
1901static const struct acpi_device_id unisys_device_ids[] = {
1902 {"PNP0A07", 0},
1903 {"", 0},
1904};
55c67dca
PB
1905
1906static struct acpi_driver unisys_acpi_driver = {
1907 .name = "unisys_acpi",
1908 .class = "unisys_acpi_class",
1909 .owner = THIS_MODULE,
1910 .ids = unisys_device_ids,
1911 .ops = {
1912 .add = visorchipset_init,
1913 .remove = visorchipset_exit,
027b03e7 1914 },
55c67dca 1915};
1fc07f99
DK
1916
1917MODULE_DEVICE_TABLE(acpi, unisys_device_ids);
1918
c1d28da7 1919static __init int visorutil_spar_detect(void)
d5b3f1dc
EA
1920{
1921 unsigned int eax, ebx, ecx, edx;
1922
0c9f3536 1923 if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) {
d5b3f1dc 1924 /* check the ID */
a27ded92
SW
1925 cpuid(UNISYS_VISOR_LEAF_ID, &eax, &ebx, &ecx, &edx);
1926 return (ebx == UNISYS_VISOR_ID_EBX) &&
1927 (ecx == UNISYS_VISOR_ID_ECX) &&
1928 (edx == UNISYS_VISOR_ID_EDX);
d5b3f1dc
EA
1929 } else {
1930 return 0;
1931 }
1932}
55c67dca
PB
1933
1934static int init_unisys(void)
1935{
1936 int result;
35e606de 1937
d5b3f1dc 1938 if (!visorutil_spar_detect())
55c67dca
PB
1939 return -ENODEV;
1940
1941 result = acpi_bus_register_driver(&unisys_acpi_driver);
1942 if (result)
1943 return -ENODEV;
1944
1945 pr_info("Unisys Visorchipset Driver Loaded.\n");
1946 return 0;
1947};
1948
1949static void exit_unisys(void)
1950{
1951 acpi_bus_unregister_driver(&unisys_acpi_driver);
12e364b9
KC
1952}
1953
55c67dca
PB
1954module_init(init_unisys);
1955module_exit(exit_unisys);
12e364b9
KC
1956
1957MODULE_AUTHOR("Unisys");
1958MODULE_LICENSE("GPL");
bff8c1a1 1959MODULE_DESCRIPTION("s-Par visorbus driver for virtual device buses");