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