staging: unisys: Fix clean up path
[deliverable/linux.git] / drivers / staging / unisys / visorbus / visorchipset.c
CommitLineData
12e364b9
KC
1/* visorchipset_main.c
2 *
f6d0c1e6 3 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
12e364b9
KC
4 * All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
15 * details.
16 */
17
55c67dca 18#include <linux/acpi.h>
c0a14641 19#include <linux/cdev.h>
46168810 20#include <linux/ctype.h>
e3420ed6
EA
21#include <linux/fs.h>
22#include <linux/mm.h>
12e364b9
KC
23#include <linux/nls.h>
24#include <linux/netdevice.h>
25#include <linux/platform_device.h>
90addb02 26#include <linux/uuid.h>
1ba00980 27#include <linux/crash_dump.h>
12e364b9 28
5f3a7e36 29#include "channel_guid.h"
55c67dca
PB
30#include "controlvmchannel.h"
31#include "controlvmcompletionstatus.h"
32#include "guestlinuxdebug.h"
33#include "periodic_work.h"
55c67dca
PB
34#include "version.h"
35#include "visorbus.h"
36#include "visorbus_private.h"
5f3a7e36 37#include "vmcallinterface.h"
55c67dca 38
12e364b9 39#define CURRENT_FILE_PC VISOR_CHIPSET_PC_visorchipset_main_c
12e364b9
KC
40
41#define MAX_NAME_SIZE 128
42#define MAX_IP_SIZE 50
43#define MAXOUTSTANDINGCHANNELCOMMAND 256
44#define POLLJIFFIES_CONTROLVMCHANNEL_FAST 1
45#define POLLJIFFIES_CONTROLVMCHANNEL_SLOW 100
46
46168810 47#define MAX_CONTROLVM_PAYLOAD_BYTES (1024*128)
2ee0deec
PB
48
49#define VISORCHIPSET_MMAP_CONTROLCHANOFFSET 0x00000000
50
d5b3f1dc
EA
51
52#define UNISYS_SPAR_LEAF_ID 0x40000000
53
54/* The s-Par leaf ID returns "UnisysSpar64" encoded across ebx, ecx, edx */
55#define UNISYS_SPAR_ID_EBX 0x73696e55
56#define UNISYS_SPAR_ID_ECX 0x70537379
57#define UNISYS_SPAR_ID_EDX 0x34367261
58
b615d628
JS
59/*
60 * Module parameters
61 */
b615d628 62static int visorchipset_major;
4da3336c 63static int visorchipset_visorbusregwait = 1; /* default is on */
b615d628 64static int visorchipset_holdchipsetready;
46168810 65static unsigned long controlvm_payload_bytes_buffered;
b615d628 66
e3420ed6
EA
67static int
68visorchipset_open(struct inode *inode, struct file *file)
69{
70 unsigned minor_number = iminor(inode);
71
72 if (minor_number)
73 return -ENODEV;
74 file->private_data = NULL;
75 return 0;
76}
77
78static int
79visorchipset_release(struct inode *inode, struct file *file)
80{
81 return 0;
82}
83
12e364b9
KC
84/* When the controlvm channel is idle for at least MIN_IDLE_SECONDS,
85* we switch to slow polling mode. As soon as we get a controlvm
86* message, we switch back to fast polling mode.
87*/
88#define MIN_IDLE_SECONDS 10
52063eca
JS
89static unsigned long poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
90static unsigned long most_recent_message_jiffies; /* when we got our last
bd5b9b32 91 * controlvm message */
4da3336c 92static int visorbusregistered;
12e364b9
KC
93
94#define MAX_CHIPSET_EVENTS 2
c242233e 95static u8 chipset_events[MAX_CHIPSET_EVENTS] = { 0, 0 };
12e364b9 96
46168810
EA
97struct parser_context {
98 unsigned long allocbytes;
99 unsigned long param_bytes;
100 u8 *curr;
101 unsigned long bytes_remaining;
102 bool byte_stream;
103 char data[0];
104};
105
9232d2d6
BR
106static struct delayed_work periodic_controlvm_work;
107static struct workqueue_struct *periodic_controlvm_workqueue;
8f1947ac 108static DEFINE_SEMAPHORE(notifier_lock);
12e364b9 109
e3420ed6
EA
110static struct cdev file_cdev;
111static struct visorchannel **file_controlvm_channel;
da021f02 112static struct controlvm_message_header g_chipset_msg_hdr;
4f44b72d 113static struct controlvm_message_packet g_devicechangestate_packet;
12e364b9 114
1390b88c
BR
115static LIST_HEAD(bus_info_list);
116static LIST_HEAD(dev_info_list);
12e364b9 117
c3d9a224 118static struct visorchannel *controlvm_channel;
12e364b9 119
84982fbf 120/* Manages the request payload in the controlvm channel */
c1f834eb 121struct visor_controlvm_payload_info {
c242233e 122 u8 __iomem *ptr; /* pointer to base address of payload pool */
5fc0229a 123 u64 offset; /* offset from beginning of controlvm
12e364b9 124 * channel to beginning of payload * pool */
b3c55b13 125 u32 bytes; /* number of bytes in payload pool */
c1f834eb
JS
126};
127
128static struct visor_controlvm_payload_info controlvm_payload_info;
12e364b9 129
12e364b9
KC
130/* The following globals are used to handle the scenario where we are unable to
131 * offload the payload from a controlvm message due to memory requirements. In
132 * this scenario, we simply stash the controlvm message, then attempt to
133 * process it again the next time controlvm_periodic_work() runs.
134 */
7166ed19 135static struct controlvm_message controlvm_pending_msg;
c79b28f7 136static bool controlvm_pending_msg_valid;
12e364b9 137
12e364b9
KC
138/* This identifies a data buffer that has been received via a controlvm messages
139 * in a remote --> local CONTROLVM_TRANSMIT_FILE conversation.
140 */
141struct putfile_buffer_entry {
142 struct list_head next; /* putfile_buffer_entry list */
317d9614 143 struct parser_context *parser_ctx; /* points to input data buffer */
12e364b9
KC
144};
145
146/* List of struct putfile_request *, via next_putfile_request member.
147 * Each entry in this list identifies an outstanding TRANSMIT_FILE
148 * conversation.
149 */
1eee0011 150static LIST_HEAD(putfile_request_list);
12e364b9
KC
151
152/* This describes a buffer and its current state of transfer (e.g., how many
153 * bytes have already been supplied as putfile data, and how many bytes are
154 * remaining) for a putfile_request.
155 */
156struct putfile_active_buffer {
157 /* a payload from a controlvm message, containing a file data buffer */
317d9614 158 struct parser_context *parser_ctx;
12e364b9
KC
159 /* points within data area of parser_ctx to next byte of data */
160 u8 *pnext;
161 /* # bytes left from <pnext> to the end of this data buffer */
162 size_t bytes_remaining;
163};
164
165#define PUTFILE_REQUEST_SIG 0x0906101302281211
166/* This identifies a single remote --> local CONTROLVM_TRANSMIT_FILE
167 * conversation. Structs of this type are dynamically linked into
168 * <Putfile_request_list>.
169 */
170struct putfile_request {
171 u64 sig; /* PUTFILE_REQUEST_SIG */
172
173 /* header from original TransmitFile request */
98d7b594 174 struct controlvm_message_header controlvm_header;
12e364b9
KC
175 u64 file_request_number; /* from original TransmitFile request */
176
177 /* link to next struct putfile_request */
178 struct list_head next_putfile_request;
179
180 /* most-recent sequence number supplied via a controlvm message */
181 u64 data_sequence_number;
182
183 /* head of putfile_buffer_entry list, which describes the data to be
184 * supplied as putfile data;
185 * - this list is added to when controlvm messages come in that supply
186 * file data
187 * - this list is removed from via the hotplug program that is actually
188 * consuming these buffers to write as file data */
189 struct list_head input_buffer_list;
190 spinlock_t req_list_lock; /* lock for input_buffer_list */
191
192 /* waiters for input_buffer_list to go non-empty */
193 wait_queue_head_t input_buffer_wq;
194
195 /* data not yet read within current putfile_buffer_entry */
196 struct putfile_active_buffer active_buf;
197
198 /* <0 = failed, 0 = in-progress, >0 = successful; */
199 /* note that this must be set with req_list_lock, and if you set <0, */
200 /* it is your responsibility to also free up all of the other objects */
201 /* in this struct (like input_buffer_list, active_buf.parser_ctx) */
202 /* before releasing the lock */
203 int completion_status;
204};
205
12e364b9
KC
206struct parahotplug_request {
207 struct list_head list;
208 int id;
209 unsigned long expiration;
3ab47701 210 struct controlvm_message msg;
12e364b9
KC
211};
212
ddf5de53
BR
213static LIST_HEAD(parahotplug_request_list);
214static DEFINE_SPINLOCK(parahotplug_request_list_lock); /* lock for above */
12e364b9
KC
215static void parahotplug_process_list(void);
216
217/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
218 * CONTROLVM_REPORTEVENT.
219 */
4da3336c 220static struct visorchipset_busdev_notifiers busdev_notifiers;
12e364b9 221
d32517e3
DZ
222static void bus_create_response(struct visor_device *p, int response);
223static void bus_destroy_response(struct visor_device *p, int response);
a298bc0b
DZ
224static void device_create_response(struct visor_device *p, int response);
225static void device_destroy_response(struct visor_device *p, int response);
226static void device_resume_response(struct visor_device *p, int response);
12e364b9 227
a298bc0b
DZ
228static void visorchipset_device_pause_response(struct visor_device *p,
229 int response);
2ee0deec 230
8e3fedd6 231static struct visorchipset_busdev_responders busdev_responders = {
12e364b9
KC
232 .bus_create = bus_create_response,
233 .bus_destroy = bus_destroy_response,
234 .device_create = device_create_response,
235 .device_destroy = device_destroy_response,
927c7927 236 .device_pause = visorchipset_device_pause_response,
12e364b9
KC
237 .device_resume = device_resume_response,
238};
239
240/* info for /dev/visorchipset */
5aa8ae57 241static dev_t major_dev = -1; /**< indicates major num for device */
12e364b9 242
19f6634f
BR
243/* prototypes for attributes */
244static ssize_t toolaction_show(struct device *dev,
8e76e695 245 struct device_attribute *attr, char *buf);
19f6634f 246static ssize_t toolaction_store(struct device *dev,
8e76e695
BR
247 struct device_attribute *attr,
248 const char *buf, size_t count);
19f6634f
BR
249static DEVICE_ATTR_RW(toolaction);
250
54b31229 251static ssize_t boottotool_show(struct device *dev,
8e76e695 252 struct device_attribute *attr, char *buf);
54b31229 253static ssize_t boottotool_store(struct device *dev,
8e76e695
BR
254 struct device_attribute *attr, const char *buf,
255 size_t count);
54b31229
BR
256static DEVICE_ATTR_RW(boottotool);
257
422af17c 258static ssize_t error_show(struct device *dev, struct device_attribute *attr,
8e76e695 259 char *buf);
422af17c 260static ssize_t error_store(struct device *dev, struct device_attribute *attr,
8e76e695 261 const char *buf, size_t count);
422af17c
BR
262static DEVICE_ATTR_RW(error);
263
264static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
8e76e695 265 char *buf);
422af17c 266static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
8e76e695 267 const char *buf, size_t count);
422af17c
BR
268static DEVICE_ATTR_RW(textid);
269
270static ssize_t remaining_steps_show(struct device *dev,
8e76e695 271 struct device_attribute *attr, char *buf);
422af17c 272static ssize_t remaining_steps_store(struct device *dev,
8e76e695
BR
273 struct device_attribute *attr,
274 const char *buf, size_t count);
422af17c
BR
275static DEVICE_ATTR_RW(remaining_steps);
276
18b87ed1 277static ssize_t chipsetready_store(struct device *dev,
8e76e695
BR
278 struct device_attribute *attr,
279 const char *buf, size_t count);
18b87ed1
BR
280static DEVICE_ATTR_WO(chipsetready);
281
e56fa7cd 282static ssize_t devicedisabled_store(struct device *dev,
8e76e695
BR
283 struct device_attribute *attr,
284 const char *buf, size_t count);
e56fa7cd
BR
285static DEVICE_ATTR_WO(devicedisabled);
286
287static ssize_t deviceenabled_store(struct device *dev,
8e76e695
BR
288 struct device_attribute *attr,
289 const char *buf, size_t count);
e56fa7cd
BR
290static DEVICE_ATTR_WO(deviceenabled);
291
19f6634f
BR
292static struct attribute *visorchipset_install_attrs[] = {
293 &dev_attr_toolaction.attr,
54b31229 294 &dev_attr_boottotool.attr,
422af17c
BR
295 &dev_attr_error.attr,
296 &dev_attr_textid.attr,
297 &dev_attr_remaining_steps.attr,
19f6634f
BR
298 NULL
299};
300
301static struct attribute_group visorchipset_install_group = {
302 .name = "install",
303 .attrs = visorchipset_install_attrs
304};
305
18b87ed1
BR
306static struct attribute *visorchipset_guest_attrs[] = {
307 &dev_attr_chipsetready.attr,
308 NULL
309};
310
311static struct attribute_group visorchipset_guest_group = {
312 .name = "guest",
313 .attrs = visorchipset_guest_attrs
314};
315
e56fa7cd
BR
316static struct attribute *visorchipset_parahotplug_attrs[] = {
317 &dev_attr_devicedisabled.attr,
318 &dev_attr_deviceenabled.attr,
319 NULL
320};
321
322static struct attribute_group visorchipset_parahotplug_group = {
323 .name = "parahotplug",
324 .attrs = visorchipset_parahotplug_attrs
325};
326
19f6634f
BR
327static const struct attribute_group *visorchipset_dev_groups[] = {
328 &visorchipset_install_group,
18b87ed1 329 &visorchipset_guest_group,
e56fa7cd 330 &visorchipset_parahotplug_group,
19f6634f
BR
331 NULL
332};
333
04dacacc
DZ
334static void visorchipset_dev_release(struct device *dev)
335{
336}
337
12e364b9 338/* /sys/devices/platform/visorchipset */
eb34e877 339static struct platform_device visorchipset_platform_device = {
12e364b9
KC
340 .name = "visorchipset",
341 .id = -1,
19f6634f 342 .dev.groups = visorchipset_dev_groups,
04dacacc 343 .dev.release = visorchipset_dev_release,
12e364b9
KC
344};
345
346/* Function prototypes */
b3168c70 347static void controlvm_respond(struct controlvm_message_header *msg_hdr,
98d7b594
BR
348 int response);
349static void controlvm_respond_chipset_init(
b3168c70 350 struct controlvm_message_header *msg_hdr, int response,
98d7b594
BR
351 enum ultra_chipset_feature features);
352static void controlvm_respond_physdev_changestate(
b3168c70 353 struct controlvm_message_header *msg_hdr, int response,
98d7b594 354 struct spar_segment_state state);
12e364b9 355
46168810 356
2ee0deec
PB
357static void parser_done(struct parser_context *ctx);
358
46168810 359static struct parser_context *
fbf35536 360parser_init_byte_stream(u64 addr, u32 bytes, bool local, bool *retry)
46168810
EA
361{
362 int allocbytes = sizeof(struct parser_context) + bytes;
363 struct parser_context *rc = NULL;
364 struct parser_context *ctx = NULL;
46168810
EA
365
366 if (retry)
367 *retry = false;
cc55b5c5
JS
368
369 /*
370 * alloc an 0 extra byte to ensure payload is
371 * '\0'-terminated
372 */
373 allocbytes++;
46168810
EA
374 if ((controlvm_payload_bytes_buffered + bytes)
375 > MAX_CONTROLVM_PAYLOAD_BYTES) {
376 if (retry)
377 *retry = true;
378 rc = NULL;
379 goto cleanup;
380 }
381 ctx = kzalloc(allocbytes, GFP_KERNEL|__GFP_NORETRY);
382 if (!ctx) {
383 if (retry)
384 *retry = true;
385 rc = NULL;
386 goto cleanup;
387 }
388
389 ctx->allocbytes = allocbytes;
390 ctx->param_bytes = bytes;
391 ctx->curr = NULL;
392 ctx->bytes_remaining = 0;
393 ctx->byte_stream = false;
394 if (local) {
395 void *p;
396
397 if (addr > virt_to_phys(high_memory - 1)) {
398 rc = NULL;
399 goto cleanup;
400 }
401 p = __va((unsigned long) (addr));
402 memcpy(ctx->data, p, bytes);
403 } else {
dd412751
JS
404 void __iomem *mapping;
405
406 if (!request_mem_region(addr, bytes, "visorchipset")) {
46168810
EA
407 rc = NULL;
408 goto cleanup;
409 }
712c03dc 410
dd412751
JS
411 mapping = ioremap_cache(addr, bytes);
412 if (!mapping) {
413 release_mem_region(addr, bytes);
46168810
EA
414 rc = NULL;
415 goto cleanup;
416 }
dd412751
JS
417 memcpy_fromio(ctx->data, mapping, bytes);
418 release_mem_region(addr, bytes);
46168810 419 }
46168810 420
cc55b5c5 421 ctx->byte_stream = true;
46168810
EA
422 rc = ctx;
423cleanup:
46168810
EA
424 if (rc) {
425 controlvm_payload_bytes_buffered += ctx->param_bytes;
426 } else {
427 if (ctx) {
428 parser_done(ctx);
429 ctx = NULL;
430 }
431 }
432 return rc;
433}
434
464129ed 435static uuid_le
46168810
EA
436parser_id_get(struct parser_context *ctx)
437{
438 struct spar_controlvm_parameters_header *phdr = NULL;
439
440 if (ctx == NULL)
441 return NULL_UUID_LE;
442 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
443 return phdr->id;
444}
445
2ee0deec
PB
446/** Describes the state from the perspective of which controlvm messages have
447 * been received for a bus or device.
448 */
449
450enum PARSER_WHICH_STRING {
451 PARSERSTRING_INITIATOR,
452 PARSERSTRING_TARGET,
453 PARSERSTRING_CONNECTION,
454 PARSERSTRING_NAME, /* TODO: only PARSERSTRING_NAME is used ? */
455};
456
464129ed 457static void
2ee0deec
PB
458parser_param_start(struct parser_context *ctx,
459 enum PARSER_WHICH_STRING which_string)
46168810
EA
460{
461 struct spar_controlvm_parameters_header *phdr = NULL;
462
463 if (ctx == NULL)
464 goto Away;
465 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
466 switch (which_string) {
467 case PARSERSTRING_INITIATOR:
468 ctx->curr = ctx->data + phdr->initiator_offset;
469 ctx->bytes_remaining = phdr->initiator_length;
470 break;
471 case PARSERSTRING_TARGET:
472 ctx->curr = ctx->data + phdr->target_offset;
473 ctx->bytes_remaining = phdr->target_length;
474 break;
475 case PARSERSTRING_CONNECTION:
476 ctx->curr = ctx->data + phdr->connection_offset;
477 ctx->bytes_remaining = phdr->connection_length;
478 break;
479 case PARSERSTRING_NAME:
480 ctx->curr = ctx->data + phdr->name_offset;
481 ctx->bytes_remaining = phdr->name_length;
482 break;
483 default:
484 break;
485 }
486
487Away:
488 return;
489}
490
464129ed 491static void parser_done(struct parser_context *ctx)
46168810
EA
492{
493 if (!ctx)
494 return;
495 controlvm_payload_bytes_buffered -= ctx->param_bytes;
496 kfree(ctx);
497}
498
464129ed 499static void *
46168810
EA
500parser_string_get(struct parser_context *ctx)
501{
502 u8 *pscan;
503 unsigned long nscan;
504 int value_length = -1;
505 void *value = NULL;
506 int i;
507
508 if (!ctx)
509 return NULL;
510 pscan = ctx->curr;
511 nscan = ctx->bytes_remaining;
512 if (nscan == 0)
513 return NULL;
514 if (!pscan)
515 return NULL;
516 for (i = 0, value_length = -1; i < nscan; i++)
517 if (pscan[i] == '\0') {
518 value_length = i;
519 break;
520 }
521 if (value_length < 0) /* '\0' was not included in the length */
522 value_length = nscan;
523 value = kmalloc(value_length + 1, GFP_KERNEL|__GFP_NORETRY);
524 if (value == NULL)
525 return NULL;
526 if (value_length > 0)
527 memcpy(value, pscan, value_length);
528 ((u8 *) (value))[value_length] = '\0';
529 return value;
530}
531
532
d746cb55
VB
533static ssize_t toolaction_show(struct device *dev,
534 struct device_attribute *attr,
535 char *buf)
19f6634f 536{
01f4d85a 537 u8 tool_action;
19f6634f 538
c3d9a224 539 visorchannel_read(controlvm_channel,
d19642f6 540 offsetof(struct spar_controlvm_channel_protocol,
8e76e695 541 tool_action), &tool_action, sizeof(u8));
01f4d85a 542 return scnprintf(buf, PAGE_SIZE, "%u\n", tool_action);
19f6634f
BR
543}
544
d746cb55
VB
545static ssize_t toolaction_store(struct device *dev,
546 struct device_attribute *attr,
547 const char *buf, size_t count)
19f6634f 548{
01f4d85a 549 u8 tool_action;
66e24b76 550 int ret;
19f6634f 551
ebec8967 552 if (kstrtou8(buf, 10, &tool_action))
66e24b76
BR
553 return -EINVAL;
554
c3d9a224 555 ret = visorchannel_write(controlvm_channel,
8e76e695
BR
556 offsetof(struct spar_controlvm_channel_protocol,
557 tool_action),
01f4d85a 558 &tool_action, sizeof(u8));
66e24b76
BR
559
560 if (ret)
561 return ret;
e22a4a0f 562 return count;
19f6634f
BR
563}
564
d746cb55
VB
565static ssize_t boottotool_show(struct device *dev,
566 struct device_attribute *attr,
567 char *buf)
54b31229 568{
365522d9 569 struct efi_spar_indication efi_spar_indication;
54b31229 570
c3d9a224 571 visorchannel_read(controlvm_channel,
8e76e695
BR
572 offsetof(struct spar_controlvm_channel_protocol,
573 efi_spar_ind), &efi_spar_indication,
574 sizeof(struct efi_spar_indication));
54b31229 575 return scnprintf(buf, PAGE_SIZE, "%u\n",
8e76e695 576 efi_spar_indication.boot_to_tool);
54b31229
BR
577}
578
d746cb55
VB
579static ssize_t boottotool_store(struct device *dev,
580 struct device_attribute *attr,
581 const char *buf, size_t count)
54b31229 582{
66e24b76 583 int val, ret;
365522d9 584 struct efi_spar_indication efi_spar_indication;
54b31229 585
ebec8967 586 if (kstrtoint(buf, 10, &val))
66e24b76
BR
587 return -EINVAL;
588
365522d9 589 efi_spar_indication.boot_to_tool = val;
c3d9a224 590 ret = visorchannel_write(controlvm_channel,
d19642f6 591 offsetof(struct spar_controlvm_channel_protocol,
8e76e695
BR
592 efi_spar_ind), &(efi_spar_indication),
593 sizeof(struct efi_spar_indication));
66e24b76
BR
594
595 if (ret)
596 return ret;
e22a4a0f 597 return count;
54b31229 598}
422af17c
BR
599
600static ssize_t error_show(struct device *dev, struct device_attribute *attr,
8e76e695 601 char *buf)
422af17c
BR
602{
603 u32 error;
604
8e76e695
BR
605 visorchannel_read(controlvm_channel,
606 offsetof(struct spar_controlvm_channel_protocol,
607 installation_error),
608 &error, sizeof(u32));
422af17c
BR
609 return scnprintf(buf, PAGE_SIZE, "%i\n", error);
610}
611
612static ssize_t error_store(struct device *dev, struct device_attribute *attr,
8e76e695 613 const char *buf, size_t count)
422af17c
BR
614{
615 u32 error;
66e24b76 616 int ret;
422af17c 617
ebec8967 618 if (kstrtou32(buf, 10, &error))
66e24b76
BR
619 return -EINVAL;
620
c3d9a224 621 ret = visorchannel_write(controlvm_channel,
8e76e695
BR
622 offsetof(struct spar_controlvm_channel_protocol,
623 installation_error),
624 &error, sizeof(u32));
66e24b76
BR
625 if (ret)
626 return ret;
e22a4a0f 627 return count;
422af17c
BR
628}
629
630static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
8e76e695 631 char *buf)
422af17c 632{
10dbf0e3 633 u32 text_id;
422af17c 634
8e76e695
BR
635 visorchannel_read(controlvm_channel,
636 offsetof(struct spar_controlvm_channel_protocol,
637 installation_text_id),
638 &text_id, sizeof(u32));
10dbf0e3 639 return scnprintf(buf, PAGE_SIZE, "%i\n", text_id);
422af17c
BR
640}
641
642static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
8e76e695 643 const char *buf, size_t count)
422af17c 644{
10dbf0e3 645 u32 text_id;
66e24b76 646 int ret;
422af17c 647
ebec8967 648 if (kstrtou32(buf, 10, &text_id))
66e24b76
BR
649 return -EINVAL;
650
c3d9a224 651 ret = visorchannel_write(controlvm_channel,
8e76e695
BR
652 offsetof(struct spar_controlvm_channel_protocol,
653 installation_text_id),
654 &text_id, sizeof(u32));
66e24b76
BR
655 if (ret)
656 return ret;
e22a4a0f 657 return count;
422af17c
BR
658}
659
422af17c 660static ssize_t remaining_steps_show(struct device *dev,
8e76e695 661 struct device_attribute *attr, char *buf)
422af17c 662{
ee8da290 663 u16 remaining_steps;
422af17c 664
c3d9a224 665 visorchannel_read(controlvm_channel,
8e76e695
BR
666 offsetof(struct spar_controlvm_channel_protocol,
667 installation_remaining_steps),
668 &remaining_steps, sizeof(u16));
ee8da290 669 return scnprintf(buf, PAGE_SIZE, "%hu\n", remaining_steps);
422af17c
BR
670}
671
672static ssize_t remaining_steps_store(struct device *dev,
8e76e695
BR
673 struct device_attribute *attr,
674 const char *buf, size_t count)
422af17c 675{
ee8da290 676 u16 remaining_steps;
66e24b76 677 int ret;
422af17c 678
ebec8967 679 if (kstrtou16(buf, 10, &remaining_steps))
66e24b76
BR
680 return -EINVAL;
681
c3d9a224 682 ret = visorchannel_write(controlvm_channel,
8e76e695
BR
683 offsetof(struct spar_controlvm_channel_protocol,
684 installation_remaining_steps),
685 &remaining_steps, sizeof(u16));
66e24b76
BR
686 if (ret)
687 return ret;
e22a4a0f 688 return count;
422af17c
BR
689}
690
ab0592b9
DZ
691struct visor_busdev {
692 u32 bus_no;
693 u32 dev_no;
694};
695
696static int match_visorbus_dev_by_id(struct device *dev, void *data)
697{
698 struct visor_device *vdev = to_visor_device(dev);
699 struct visor_busdev *id = (struct visor_busdev *)data;
700 u32 bus_no = id->bus_no;
701 u32 dev_no = id->dev_no;
702
65bd6e46
DZ
703 if ((vdev->chipset_bus_no == bus_no) &&
704 (vdev->chipset_dev_no == dev_no))
ab0592b9
DZ
705 return 1;
706
707 return 0;
708}
709struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no,
710 struct visor_device *from)
711{
712 struct device *dev;
713 struct device *dev_start = NULL;
714 struct visor_device *vdev = NULL;
715 struct visor_busdev id = {
716 .bus_no = bus_no,
717 .dev_no = dev_no
718 };
719
720 if (from)
721 dev_start = &from->device;
722 dev = bus_find_device(&visorbus_type, dev_start, (void *)&id,
723 match_visorbus_dev_by_id);
724 if (dev)
725 vdev = to_visor_device(dev);
726 return vdev;
727}
728EXPORT_SYMBOL(visorbus_get_device_by_id);
729
c242233e 730static u8
12e364b9
KC
731check_chipset_events(void)
732{
733 int i;
c242233e 734 u8 send_msg = 1;
12e364b9
KC
735 /* Check events to determine if response should be sent */
736 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
737 send_msg &= chipset_events[i];
738 return send_msg;
739}
740
741static void
742clear_chipset_events(void)
743{
744 int i;
745 /* Clear chipset_events */
746 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
747 chipset_events[i] = 0;
748}
749
750void
4da3336c 751visorchipset_register_busdev(
fe90d892 752 struct visorchipset_busdev_notifiers *notifiers,
929aa8ae 753 struct visorchipset_busdev_responders *responders,
1e7a59c1 754 struct ultra_vbus_deviceinfo *driver_info)
12e364b9 755{
8f1947ac 756 down(&notifier_lock);
38f736e9 757 if (!notifiers) {
4da3336c
DK
758 memset(&busdev_notifiers, 0,
759 sizeof(busdev_notifiers));
760 visorbusregistered = 0; /* clear flag */
12e364b9 761 } else {
4da3336c
DK
762 busdev_notifiers = *notifiers;
763 visorbusregistered = 1; /* set flag */
12e364b9
KC
764 }
765 if (responders)
8e3fedd6 766 *responders = busdev_responders;
1e7a59c1
BR
767 if (driver_info)
768 bus_device_info_init(driver_info, "chipset", "visorchipset",
8e76e695 769 VERSION, NULL);
12e364b9 770
8f1947ac 771 up(&notifier_lock);
12e364b9 772}
4da3336c 773EXPORT_SYMBOL_GPL(visorchipset_register_busdev);
12e364b9 774
12e364b9 775static void
3ab47701 776chipset_init(struct controlvm_message *inmsg)
12e364b9
KC
777{
778 static int chipset_inited;
b9b141e8 779 enum ultra_chipset_feature features = 0;
12e364b9
KC
780 int rc = CONTROLVM_RESP_SUCCESS;
781
782 POSTCODE_LINUX_2(CHIPSET_INIT_ENTRY_PC, POSTCODE_SEVERITY_INFO);
783 if (chipset_inited) {
22ad57ba 784 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
e3199b2e 785 goto cleanup;
12e364b9
KC
786 }
787 chipset_inited = 1;
788 POSTCODE_LINUX_2(CHIPSET_INIT_EXIT_PC, POSTCODE_SEVERITY_INFO);
789
790 /* Set features to indicate we support parahotplug (if Command
791 * also supports it). */
792 features =
2ea5117b 793 inmsg->cmd.init_chipset.
12e364b9
KC
794 features & ULTRA_CHIPSET_FEATURE_PARA_HOTPLUG;
795
796 /* Set the "reply" bit so Command knows this is a
797 * features-aware driver. */
798 features |= ULTRA_CHIPSET_FEATURE_REPLY;
799
e3199b2e 800cleanup:
98d7b594 801 if (inmsg->hdr.flags.response_expected)
12e364b9
KC
802 controlvm_respond_chipset_init(&inmsg->hdr, rc, features);
803}
804
805static void
3ab47701 806controlvm_init_response(struct controlvm_message *msg,
b3168c70 807 struct controlvm_message_header *msg_hdr, int response)
12e364b9 808{
3ab47701 809 memset(msg, 0, sizeof(struct controlvm_message));
b3168c70 810 memcpy(&msg->hdr, msg_hdr, sizeof(struct controlvm_message_header));
98d7b594
BR
811 msg->hdr.payload_bytes = 0;
812 msg->hdr.payload_vm_offset = 0;
813 msg->hdr.payload_max_bytes = 0;
12e364b9 814 if (response < 0) {
98d7b594
BR
815 msg->hdr.flags.failed = 1;
816 msg->hdr.completion_status = (u32) (-response);
12e364b9
KC
817 }
818}
819
820static void
b3168c70 821controlvm_respond(struct controlvm_message_header *msg_hdr, int response)
12e364b9 822{
3ab47701 823 struct controlvm_message outmsg;
26eb2c0c 824
b3168c70 825 controlvm_init_response(&outmsg, msg_hdr, response);
2098dbd1 826 if (outmsg.hdr.flags.test_message == 1)
12e364b9 827 return;
2098dbd1 828
c3d9a224 829 if (!visorchannel_signalinsert(controlvm_channel,
12e364b9 830 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
12e364b9
KC
831 return;
832 }
833}
834
835static void
b3168c70 836controlvm_respond_chipset_init(struct controlvm_message_header *msg_hdr,
98d7b594 837 int response,
b9b141e8 838 enum ultra_chipset_feature features)
12e364b9 839{
3ab47701 840 struct controlvm_message outmsg;
26eb2c0c 841
b3168c70 842 controlvm_init_response(&outmsg, msg_hdr, response);
2ea5117b 843 outmsg.cmd.init_chipset.features = features;
c3d9a224 844 if (!visorchannel_signalinsert(controlvm_channel,
12e364b9 845 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
12e364b9
KC
846 return;
847 }
848}
849
98d7b594 850static void controlvm_respond_physdev_changestate(
b3168c70 851 struct controlvm_message_header *msg_hdr, int response,
98d7b594 852 struct spar_segment_state state)
12e364b9 853{
3ab47701 854 struct controlvm_message outmsg;
26eb2c0c 855
b3168c70 856 controlvm_init_response(&outmsg, msg_hdr, response);
2ea5117b
BR
857 outmsg.cmd.device_change_state.state = state;
858 outmsg.cmd.device_change_state.flags.phys_device = 1;
c3d9a224 859 if (!visorchannel_signalinsert(controlvm_channel,
12e364b9 860 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
12e364b9
KC
861 return;
862 }
863}
864
2ee0deec
PB
865enum crash_obj_type {
866 CRASH_DEV,
867 CRASH_BUS,
868};
869
12e364b9 870static void
0274b5ae
DZ
871bus_responder(enum controlvm_id cmd_id,
872 struct controlvm_message_header *pending_msg_hdr,
3032aedd 873 int response)
12e364b9 874{
0274b5ae
DZ
875 if (pending_msg_hdr == NULL)
876 return; /* no controlvm response needed */
12e364b9 877
0274b5ae 878 if (pending_msg_hdr->id != (u32)cmd_id)
12e364b9 879 return;
0aca7844 880
0274b5ae 881 controlvm_respond(pending_msg_hdr, response);
12e364b9
KC
882}
883
884static void
fbb31f48 885device_changestate_responder(enum controlvm_id cmd_id,
a298bc0b 886 struct visor_device *p, int response,
fbb31f48 887 struct spar_segment_state response_state)
12e364b9 888{
3ab47701 889 struct controlvm_message outmsg;
a298bc0b
DZ
890 u32 bus_no = p->chipset_bus_no;
891 u32 dev_no = p->chipset_dev_no;
12e364b9 892
0274b5ae 893 if (p->pending_msg_hdr == NULL)
12e364b9 894 return; /* no controlvm response needed */
0274b5ae 895 if (p->pending_msg_hdr->id != cmd_id)
12e364b9 896 return;
12e364b9 897
0274b5ae 898 controlvm_init_response(&outmsg, p->pending_msg_hdr, response);
12e364b9 899
fbb31f48
BR
900 outmsg.cmd.device_change_state.bus_no = bus_no;
901 outmsg.cmd.device_change_state.dev_no = dev_no;
902 outmsg.cmd.device_change_state.state = response_state;
12e364b9 903
c3d9a224 904 if (!visorchannel_signalinsert(controlvm_channel,
0aca7844 905 CONTROLVM_QUEUE_REQUEST, &outmsg))
12e364b9 906 return;
12e364b9
KC
907}
908
909static void
0274b5ae
DZ
910device_responder(enum controlvm_id cmd_id,
911 struct controlvm_message_header *pending_msg_hdr,
b4b598fd 912 int response)
12e364b9 913{
0274b5ae 914 if (pending_msg_hdr == NULL)
12e364b9 915 return; /* no controlvm response needed */
0aca7844 916
0274b5ae 917 if (pending_msg_hdr->id != (u32)cmd_id)
12e364b9 918 return;
0aca7844 919
0274b5ae 920 controlvm_respond(pending_msg_hdr, response);
12e364b9
KC
921}
922
923static void
d32517e3 924bus_epilog(struct visor_device *bus_info,
2836c6a8 925 u32 cmd, struct controlvm_message_header *msg_hdr,
f4c11551 926 int response, bool need_response)
12e364b9 927{
f4c11551 928 bool notified = false;
0274b5ae 929 struct controlvm_message_header *pmsg_hdr = NULL;
12e364b9 930
0274b5ae
DZ
931 if (!bus_info) {
932 /* relying on a valid passed in response code */
933 /* be lazy and re-use msg_hdr for this failure, is this ok?? */
934 pmsg_hdr = msg_hdr;
935 goto away;
936 }
937
938 if (bus_info->pending_msg_hdr) {
939 /* only non-NULL if dev is still waiting on a response */
940 response = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
941 pmsg_hdr = bus_info->pending_msg_hdr;
942 goto away;
943 }
0aca7844 944
2836c6a8 945 if (need_response) {
0274b5ae
DZ
946 pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
947 if (!pmsg_hdr) {
948 response = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
949 goto away;
950 }
951
952 memcpy(pmsg_hdr, msg_hdr,
98d7b594 953 sizeof(struct controlvm_message_header));
0274b5ae 954 bus_info->pending_msg_hdr = pmsg_hdr;
75c1f8b7 955 }
12e364b9 956
8f1947ac 957 down(&notifier_lock);
12e364b9
KC
958 if (response == CONTROLVM_RESP_SUCCESS) {
959 switch (cmd) {
960 case CONTROLVM_BUS_CREATE:
4da3336c 961 if (busdev_notifiers.bus_create) {
3032aedd 962 (*busdev_notifiers.bus_create) (bus_info);
f4c11551 963 notified = true;
12e364b9
KC
964 }
965 break;
966 case CONTROLVM_BUS_DESTROY:
4da3336c 967 if (busdev_notifiers.bus_destroy) {
3032aedd 968 (*busdev_notifiers.bus_destroy) (bus_info);
f4c11551 969 notified = true;
12e364b9
KC
970 }
971 break;
972 }
973 }
0274b5ae 974away:
12e364b9
KC
975 if (notified)
976 /* The callback function just called above is responsible
929aa8ae 977 * for calling the appropriate visorchipset_busdev_responders
12e364b9
KC
978 * function, which will call bus_responder()
979 */
980 ;
981 else
0274b5ae
DZ
982 /*
983 * Do not kfree(pmsg_hdr) as this is the failure path.
984 * The success path ('notified') will call the responder
985 * directly and kfree() there.
986 */
987 bus_responder(cmd, pmsg_hdr, response);
8f1947ac 988 up(&notifier_lock);
12e364b9
KC
989}
990
991static void
a298bc0b 992device_epilog(struct visor_device *dev_info,
b4b598fd 993 struct spar_segment_state state, u32 cmd,
2836c6a8 994 struct controlvm_message_header *msg_hdr, int response,
f4c11551 995 bool need_response, bool for_visorbus)
12e364b9 996{
e82ba62e 997 struct visorchipset_busdev_notifiers *notifiers;
f4c11551 998 bool notified = false;
0274b5ae 999 struct controlvm_message_header *pmsg_hdr = NULL;
12e364b9 1000
4da3336c
DK
1001 notifiers = &busdev_notifiers;
1002
0274b5ae
DZ
1003 if (!dev_info) {
1004 /* relying on a valid passed in response code */
1005 /* be lazy and re-use msg_hdr for this failure, is this ok?? */
1006 pmsg_hdr = msg_hdr;
1007 goto away;
1008 }
1009
1010 if (dev_info->pending_msg_hdr) {
1011 /* only non-NULL if dev is still waiting on a response */
1012 response = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
1013 pmsg_hdr = dev_info->pending_msg_hdr;
1014 goto away;
1015 }
1016
2836c6a8 1017 if (need_response) {
0274b5ae
DZ
1018 pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
1019 if (!pmsg_hdr) {
1020 response = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
1021 goto away;
1022 }
1023
1024 memcpy(pmsg_hdr, msg_hdr,
98d7b594 1025 sizeof(struct controlvm_message_header));
0274b5ae 1026 dev_info->pending_msg_hdr = pmsg_hdr;
75c1f8b7 1027 }
12e364b9 1028
8f1947ac 1029 down(&notifier_lock);
12e364b9
KC
1030 if (response >= 0) {
1031 switch (cmd) {
1032 case CONTROLVM_DEVICE_CREATE:
1033 if (notifiers->device_create) {
b4b598fd 1034 (*notifiers->device_create) (dev_info);
f4c11551 1035 notified = true;
12e364b9
KC
1036 }
1037 break;
1038 case CONTROLVM_DEVICE_CHANGESTATE:
1039 /* ServerReady / ServerRunning / SegmentStateRunning */
bd0d2dcc
BR
1040 if (state.alive == segment_state_running.alive &&
1041 state.operating ==
1042 segment_state_running.operating) {
12e364b9 1043 if (notifiers->device_resume) {
b4b598fd 1044 (*notifiers->device_resume) (dev_info);
f4c11551 1045 notified = true;
12e364b9
KC
1046 }
1047 }
1048 /* ServerNotReady / ServerLost / SegmentStateStandby */
bd0d2dcc 1049 else if (state.alive == segment_state_standby.alive &&
3f833b54 1050 state.operating ==
bd0d2dcc 1051 segment_state_standby.operating) {
12e364b9
KC
1052 /* technically this is standby case
1053 * where server is lost
1054 */
1055 if (notifiers->device_pause) {
b4b598fd 1056 (*notifiers->device_pause) (dev_info);
f4c11551 1057 notified = true;
12e364b9 1058 }
12e364b9
KC
1059 }
1060 break;
1061 case CONTROLVM_DEVICE_DESTROY:
1062 if (notifiers->device_destroy) {
b4b598fd 1063 (*notifiers->device_destroy) (dev_info);
f4c11551 1064 notified = true;
12e364b9
KC
1065 }
1066 break;
1067 }
1068 }
0274b5ae 1069away:
12e364b9
KC
1070 if (notified)
1071 /* The callback function just called above is responsible
929aa8ae 1072 * for calling the appropriate visorchipset_busdev_responders
12e364b9
KC
1073 * function, which will call device_responder()
1074 */
1075 ;
1076 else
0274b5ae
DZ
1077 /*
1078 * Do not kfree(pmsg_hdr) as this is the failure path.
1079 * The success path ('notified') will call the responder
1080 * directly and kfree() there.
1081 */
1082 device_responder(cmd, pmsg_hdr, response);
8f1947ac 1083 up(&notifier_lock);
12e364b9
KC
1084}
1085
1086static void
3ab47701 1087bus_create(struct controlvm_message *inmsg)
12e364b9 1088{
2ea5117b 1089 struct controlvm_message_packet *cmd = &inmsg->cmd;
52063eca 1090 u32 bus_no = cmd->create_bus.bus_no;
12e364b9 1091 int rc = CONTROLVM_RESP_SUCCESS;
d32517e3 1092 struct visor_device *bus_info;
b32c4997 1093 struct visorchannel *visorchannel;
12e364b9 1094
d32517e3 1095 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
6c5fed35
BR
1096 if (bus_info && (bus_info->state.created == 1)) {
1097 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
12e364b9 1098 POSTCODE_SEVERITY_ERR);
22ad57ba 1099 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
6c5fed35 1100 goto cleanup;
12e364b9 1101 }
6c5fed35
BR
1102 bus_info = kzalloc(sizeof(*bus_info), GFP_KERNEL);
1103 if (!bus_info) {
1104 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
12e364b9 1105 POSTCODE_SEVERITY_ERR);
22ad57ba 1106 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
6c5fed35 1107 goto cleanup;
12e364b9
KC
1108 }
1109
d32517e3
DZ
1110 bus_info->chipset_bus_no = bus_no;
1111 bus_info->chipset_dev_no = BUS_ROOT_DEVICE;
12e364b9 1112
6c5fed35 1113 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
12e364b9 1114
b32c4997
DZ
1115 visorchannel = visorchannel_create(cmd->create_bus.channel_addr,
1116 cmd->create_bus.channel_bytes,
1117 GFP_KERNEL,
1118 cmd->create_bus.bus_data_type_uuid);
1119
1120 if (!visorchannel) {
1121 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1122 POSTCODE_SEVERITY_ERR);
1123 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
1124 kfree(bus_info);
1125 bus_info = NULL;
1126 goto cleanup;
1127 }
1128 bus_info->visorchannel = visorchannel;
12e364b9 1129
6c5fed35 1130 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
12e364b9 1131
6c5fed35 1132cleanup:
3032aedd 1133 bus_epilog(bus_info, CONTROLVM_BUS_CREATE, &inmsg->hdr,
98d7b594 1134 rc, inmsg->hdr.flags.response_expected == 1);
12e364b9
KC
1135}
1136
1137static void
3ab47701 1138bus_destroy(struct controlvm_message *inmsg)
12e364b9 1139{
2ea5117b 1140 struct controlvm_message_packet *cmd = &inmsg->cmd;
52063eca 1141 u32 bus_no = cmd->destroy_bus.bus_no;
d32517e3 1142 struct visor_device *bus_info;
12e364b9
KC
1143 int rc = CONTROLVM_RESP_SUCCESS;
1144
d32517e3 1145 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
dff54cd6 1146 if (!bus_info)
22ad57ba 1147 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
dff54cd6 1148 else if (bus_info->state.created == 0)
22ad57ba 1149 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
12e364b9 1150
3032aedd 1151 bus_epilog(bus_info, CONTROLVM_BUS_DESTROY, &inmsg->hdr,
98d7b594 1152 rc, inmsg->hdr.flags.response_expected == 1);
d32517e3
DZ
1153
1154 /* bus_info is freed as part of the busdevice_release function */
12e364b9
KC
1155}
1156
1157static void
317d9614
BR
1158bus_configure(struct controlvm_message *inmsg,
1159 struct parser_context *parser_ctx)
12e364b9 1160{
2ea5117b 1161 struct controlvm_message_packet *cmd = &inmsg->cmd;
e82ba62e 1162 u32 bus_no;
d32517e3 1163 struct visor_device *bus_info;
12e364b9 1164 int rc = CONTROLVM_RESP_SUCCESS;
12e364b9 1165
654bada0
BR
1166 bus_no = cmd->configure_bus.bus_no;
1167 POSTCODE_LINUX_3(BUS_CONFIGURE_ENTRY_PC, bus_no,
1168 POSTCODE_SEVERITY_INFO);
12e364b9 1169
d32517e3 1170 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
654bada0
BR
1171 if (!bus_info) {
1172 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
12e364b9 1173 POSTCODE_SEVERITY_ERR);
22ad57ba 1174 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
654bada0
BR
1175 } else if (bus_info->state.created == 0) {
1176 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
12e364b9 1177 POSTCODE_SEVERITY_ERR);
22ad57ba 1178 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
0274b5ae 1179 } else if (bus_info->pending_msg_hdr != NULL) {
654bada0 1180 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
12e364b9 1181 POSTCODE_SEVERITY_ERR);
22ad57ba 1182 rc = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
654bada0 1183 } else {
b32c4997
DZ
1184 visorchannel_set_clientpartition(bus_info->visorchannel,
1185 cmd->configure_bus.guest_handle);
654bada0
BR
1186 bus_info->partition_uuid = parser_id_get(parser_ctx);
1187 parser_param_start(parser_ctx, PARSERSTRING_NAME);
1188 bus_info->name = parser_string_get(parser_ctx);
1189
654bada0
BR
1190 POSTCODE_LINUX_3(BUS_CONFIGURE_EXIT_PC, bus_no,
1191 POSTCODE_SEVERITY_INFO);
12e364b9 1192 }
3032aedd 1193 bus_epilog(bus_info, CONTROLVM_BUS_CONFIGURE, &inmsg->hdr,
98d7b594 1194 rc, inmsg->hdr.flags.response_expected == 1);
12e364b9
KC
1195}
1196
1197static void
3ab47701 1198my_device_create(struct controlvm_message *inmsg)
12e364b9 1199{
2ea5117b 1200 struct controlvm_message_packet *cmd = &inmsg->cmd;
52063eca
JS
1201 u32 bus_no = cmd->create_device.bus_no;
1202 u32 dev_no = cmd->create_device.dev_no;
a298bc0b 1203 struct visor_device *dev_info = NULL;
d32517e3 1204 struct visor_device *bus_info;
b32c4997 1205 struct visorchannel *visorchannel;
12e364b9
KC
1206 int rc = CONTROLVM_RESP_SUCCESS;
1207
a298bc0b
DZ
1208 bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
1209 if (!bus_info) {
c60c8e26 1210 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
12e364b9 1211 POSTCODE_SEVERITY_ERR);
a298bc0b 1212 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
c60c8e26 1213 goto cleanup;
12e364b9 1214 }
a298bc0b
DZ
1215
1216 if (bus_info->state.created == 0) {
c60c8e26 1217 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
12e364b9 1218 POSTCODE_SEVERITY_ERR);
22ad57ba 1219 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
c60c8e26 1220 goto cleanup;
12e364b9 1221 }
a298bc0b
DZ
1222
1223 dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
1224 if (dev_info && (dev_info->state.created == 1)) {
c60c8e26 1225 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
12e364b9 1226 POSTCODE_SEVERITY_ERR);
a298bc0b 1227 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
c60c8e26 1228 goto cleanup;
12e364b9 1229 }
a298bc0b 1230
c60c8e26
BR
1231 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
1232 if (!dev_info) {
1233 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
12e364b9 1234 POSTCODE_SEVERITY_ERR);
22ad57ba 1235 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
c60c8e26 1236 goto cleanup;
12e364b9 1237 }
97a84f12 1238
a298bc0b
DZ
1239 dev_info->chipset_bus_no = bus_no;
1240 dev_info->chipset_dev_no = dev_no;
1241 dev_info->inst = cmd->create_device.dev_inst_uuid;
1242
1243 /* not sure where the best place to set the 'parent' */
1244 dev_info->device.parent = &bus_info->device;
1245
c60c8e26 1246 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
12e364b9
KC
1247 POSTCODE_SEVERITY_INFO);
1248
b32c4997
DZ
1249 visorchannel = visorchannel_create(cmd->create_device.channel_addr,
1250 cmd->create_device.channel_bytes,
1251 GFP_KERNEL,
1252 cmd->create_device.data_type_uuid);
1253
1254 if (!visorchannel) {
1255 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1256 POSTCODE_SEVERITY_ERR);
1257 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
1258 kfree(dev_info);
1259 dev_info = NULL;
1260 goto cleanup;
1261 }
1262 dev_info->visorchannel = visorchannel;
1263 dev_info->channel_type_guid = cmd->create_device.data_type_uuid;
c60c8e26 1264 POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
12e364b9 1265 POSTCODE_SEVERITY_INFO);
c60c8e26 1266cleanup:
b4b598fd 1267 device_epilog(dev_info, segment_state_running,
12e364b9 1268 CONTROLVM_DEVICE_CREATE, &inmsg->hdr, rc,
4da3336c 1269 inmsg->hdr.flags.response_expected == 1, 1);
12e364b9
KC
1270}
1271
1272static void
3ab47701 1273my_device_changestate(struct controlvm_message *inmsg)
12e364b9 1274{
2ea5117b 1275 struct controlvm_message_packet *cmd = &inmsg->cmd;
52063eca
JS
1276 u32 bus_no = cmd->device_change_state.bus_no;
1277 u32 dev_no = cmd->device_change_state.dev_no;
2ea5117b 1278 struct spar_segment_state state = cmd->device_change_state.state;
a298bc0b 1279 struct visor_device *dev_info;
12e364b9
KC
1280 int rc = CONTROLVM_RESP_SUCCESS;
1281
a298bc0b 1282 dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
0278a905
BR
1283 if (!dev_info) {
1284 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
12e364b9 1285 POSTCODE_SEVERITY_ERR);
22ad57ba 1286 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
0278a905
BR
1287 } else if (dev_info->state.created == 0) {
1288 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
12e364b9 1289 POSTCODE_SEVERITY_ERR);
22ad57ba 1290 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
12e364b9 1291 }
0278a905 1292 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
b4b598fd 1293 device_epilog(dev_info, state,
0278a905 1294 CONTROLVM_DEVICE_CHANGESTATE, &inmsg->hdr, rc,
4da3336c 1295 inmsg->hdr.flags.response_expected == 1, 1);
12e364b9
KC
1296}
1297
1298static void
3ab47701 1299my_device_destroy(struct controlvm_message *inmsg)
12e364b9 1300{
2ea5117b 1301 struct controlvm_message_packet *cmd = &inmsg->cmd;
52063eca
JS
1302 u32 bus_no = cmd->destroy_device.bus_no;
1303 u32 dev_no = cmd->destroy_device.dev_no;
a298bc0b 1304 struct visor_device *dev_info;
12e364b9
KC
1305 int rc = CONTROLVM_RESP_SUCCESS;
1306
a298bc0b 1307 dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
61715c8b 1308 if (!dev_info)
22ad57ba 1309 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
61715c8b 1310 else if (dev_info->state.created == 0)
22ad57ba 1311 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
12e364b9 1312
61715c8b 1313 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
b4b598fd 1314 device_epilog(dev_info, segment_state_running,
12e364b9 1315 CONTROLVM_DEVICE_DESTROY, &inmsg->hdr, rc,
4da3336c 1316 inmsg->hdr.flags.response_expected == 1, 1);
12e364b9
KC
1317}
1318
1319/* When provided with the physical address of the controlvm channel
1320 * (phys_addr), the offset to the payload area we need to manage
1321 * (offset), and the size of this payload area (bytes), fills in the
f4c11551 1322 * controlvm_payload_info struct. Returns true for success or false
12e364b9
KC
1323 * for failure.
1324 */
1325static int
d5b3f1dc 1326initialize_controlvm_payload_info(u64 phys_addr, u64 offset, u32 bytes,
c1f834eb 1327 struct visor_controlvm_payload_info *info)
12e364b9 1328{
c242233e 1329 u8 __iomem *payload = NULL;
12e364b9
KC
1330 int rc = CONTROLVM_RESP_SUCCESS;
1331
38f736e9 1332 if (!info) {
22ad57ba 1333 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
f118a39b 1334 goto cleanup;
12e364b9 1335 }
c1f834eb 1336 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
12e364b9 1337 if ((offset == 0) || (bytes == 0)) {
22ad57ba 1338 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
f118a39b 1339 goto cleanup;
12e364b9
KC
1340 }
1341 payload = ioremap_cache(phys_addr + offset, bytes);
38f736e9 1342 if (!payload) {
22ad57ba 1343 rc = -CONTROLVM_RESP_ERROR_IOREMAP_FAILED;
f118a39b 1344 goto cleanup;
12e364b9
KC
1345 }
1346
1347 info->offset = offset;
1348 info->bytes = bytes;
1349 info->ptr = payload;
12e364b9 1350
f118a39b 1351cleanup:
12e364b9 1352 if (rc < 0) {
f118a39b 1353 if (payload) {
12e364b9
KC
1354 iounmap(payload);
1355 payload = NULL;
1356 }
1357 }
1358 return rc;
1359}
1360
1361static void
c1f834eb 1362destroy_controlvm_payload_info(struct visor_controlvm_payload_info *info)
12e364b9 1363{
597c338f 1364 if (info->ptr) {
12e364b9
KC
1365 iounmap(info->ptr);
1366 info->ptr = NULL;
1367 }
c1f834eb 1368 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
12e364b9
KC
1369}
1370
1371static void
1372initialize_controlvm_payload(void)
1373{
d5b3f1dc 1374 u64 phys_addr = visorchannel_get_physaddr(controlvm_channel);
cafefc0c
BR
1375 u64 payload_offset = 0;
1376 u32 payload_bytes = 0;
26eb2c0c 1377
c3d9a224 1378 if (visorchannel_read(controlvm_channel,
d19642f6
BR
1379 offsetof(struct spar_controlvm_channel_protocol,
1380 request_payload_offset),
cafefc0c 1381 &payload_offset, sizeof(payload_offset)) < 0) {
12e364b9
KC
1382 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1383 POSTCODE_SEVERITY_ERR);
1384 return;
1385 }
c3d9a224 1386 if (visorchannel_read(controlvm_channel,
d19642f6
BR
1387 offsetof(struct spar_controlvm_channel_protocol,
1388 request_payload_bytes),
cafefc0c 1389 &payload_bytes, sizeof(payload_bytes)) < 0) {
12e364b9
KC
1390 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1391 POSTCODE_SEVERITY_ERR);
1392 return;
1393 }
1394 initialize_controlvm_payload_info(phys_addr,
cafefc0c 1395 payload_offset, payload_bytes,
84982fbf 1396 &controlvm_payload_info);
12e364b9
KC
1397}
1398
1399/* Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
1400 * Returns CONTROLVM_RESP_xxx code.
1401 */
d3368a58 1402static int
12e364b9
KC
1403visorchipset_chipset_ready(void)
1404{
eb34e877 1405 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_ONLINE);
12e364b9
KC
1406 return CONTROLVM_RESP_SUCCESS;
1407}
12e364b9 1408
d3368a58 1409static int
12e364b9
KC
1410visorchipset_chipset_selftest(void)
1411{
1412 char env_selftest[20];
1413 char *envp[] = { env_selftest, NULL };
26eb2c0c 1414
12e364b9 1415 sprintf(env_selftest, "SPARSP_SELFTEST=%d", 1);
eb34e877 1416 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
12e364b9
KC
1417 envp);
1418 return CONTROLVM_RESP_SUCCESS;
1419}
12e364b9
KC
1420
1421/* Send ACTION=offline for DEVPATH=/sys/devices/platform/visorchipset.
1422 * Returns CONTROLVM_RESP_xxx code.
1423 */
d3368a58 1424static int
12e364b9
KC
1425visorchipset_chipset_notready(void)
1426{
eb34e877 1427 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_OFFLINE);
12e364b9
KC
1428 return CONTROLVM_RESP_SUCCESS;
1429}
12e364b9
KC
1430
1431static void
77a0449d 1432chipset_ready(struct controlvm_message_header *msg_hdr)
12e364b9
KC
1433{
1434 int rc = visorchipset_chipset_ready();
26eb2c0c 1435
12e364b9
KC
1436 if (rc != CONTROLVM_RESP_SUCCESS)
1437 rc = -rc;
77a0449d
BR
1438 if (msg_hdr->flags.response_expected && !visorchipset_holdchipsetready)
1439 controlvm_respond(msg_hdr, rc);
1440 if (msg_hdr->flags.response_expected && visorchipset_holdchipsetready) {
12e364b9
KC
1441 /* Send CHIPSET_READY response when all modules have been loaded
1442 * and disks mounted for the partition
1443 */
77a0449d 1444 g_chipset_msg_hdr = *msg_hdr;
12e364b9
KC
1445 }
1446}
1447
1448static void
77a0449d 1449chipset_selftest(struct controlvm_message_header *msg_hdr)
12e364b9
KC
1450{
1451 int rc = visorchipset_chipset_selftest();
26eb2c0c 1452
12e364b9
KC
1453 if (rc != CONTROLVM_RESP_SUCCESS)
1454 rc = -rc;
77a0449d
BR
1455 if (msg_hdr->flags.response_expected)
1456 controlvm_respond(msg_hdr, rc);
12e364b9
KC
1457}
1458
1459static void
77a0449d 1460chipset_notready(struct controlvm_message_header *msg_hdr)
12e364b9
KC
1461{
1462 int rc = visorchipset_chipset_notready();
26eb2c0c 1463
12e364b9
KC
1464 if (rc != CONTROLVM_RESP_SUCCESS)
1465 rc = -rc;
77a0449d
BR
1466 if (msg_hdr->flags.response_expected)
1467 controlvm_respond(msg_hdr, rc);
12e364b9
KC
1468}
1469
1470/* This is your "one-stop" shop for grabbing the next message from the
1471 * CONTROLVM_QUEUE_EVENT queue in the controlvm channel.
1472 */
f4c11551 1473static bool
3ab47701 1474read_controlvm_event(struct controlvm_message *msg)
12e364b9 1475{
c3d9a224 1476 if (visorchannel_signalremove(controlvm_channel,
12e364b9
KC
1477 CONTROLVM_QUEUE_EVENT, msg)) {
1478 /* got a message */
0aca7844 1479 if (msg->hdr.flags.test_message == 1)
f4c11551
JS
1480 return false;
1481 return true;
12e364b9 1482 }
f4c11551 1483 return false;
12e364b9
KC
1484}
1485
1486/*
1487 * The general parahotplug flow works as follows. The visorchipset
1488 * driver receives a DEVICE_CHANGESTATE message from Command
1489 * specifying a physical device to enable or disable. The CONTROLVM
1490 * message handler calls parahotplug_process_message, which then adds
1491 * the message to a global list and kicks off a udev event which
1492 * causes a user level script to enable or disable the specified
1493 * device. The udev script then writes to
1494 * /proc/visorchipset/parahotplug, which causes parahotplug_proc_write
1495 * to get called, at which point the appropriate CONTROLVM message is
1496 * retrieved from the list and responded to.
1497 */
1498
1499#define PARAHOTPLUG_TIMEOUT_MS 2000
1500
1501/*
1502 * Generate unique int to match an outstanding CONTROLVM message with a
1503 * udev script /proc response
1504 */
1505static int
1506parahotplug_next_id(void)
1507{
1508 static atomic_t id = ATOMIC_INIT(0);
26eb2c0c 1509
12e364b9
KC
1510 return atomic_inc_return(&id);
1511}
1512
1513/*
1514 * Returns the time (in jiffies) when a CONTROLVM message on the list
1515 * should expire -- PARAHOTPLUG_TIMEOUT_MS in the future
1516 */
1517static unsigned long
1518parahotplug_next_expiration(void)
1519{
2cc1a1b3 1520 return jiffies + msecs_to_jiffies(PARAHOTPLUG_TIMEOUT_MS);
12e364b9
KC
1521}
1522
1523/*
1524 * Create a parahotplug_request, which is basically a wrapper for a
1525 * CONTROLVM_MESSAGE that we can stick on a list
1526 */
1527static struct parahotplug_request *
3ab47701 1528parahotplug_request_create(struct controlvm_message *msg)
12e364b9 1529{
ea0dcfcf
QL
1530 struct parahotplug_request *req;
1531
6a55e3c3 1532 req = kmalloc(sizeof(*req), GFP_KERNEL | __GFP_NORETRY);
38f736e9 1533 if (!req)
12e364b9
KC
1534 return NULL;
1535
1536 req->id = parahotplug_next_id();
1537 req->expiration = parahotplug_next_expiration();
1538 req->msg = *msg;
1539
1540 return req;
1541}
1542
1543/*
1544 * Free a parahotplug_request.
1545 */
1546static void
1547parahotplug_request_destroy(struct parahotplug_request *req)
1548{
1549 kfree(req);
1550}
1551
1552/*
1553 * Cause uevent to run the user level script to do the disable/enable
1554 * specified in (the CONTROLVM message in) the specified
1555 * parahotplug_request
1556 */
1557static void
1558parahotplug_request_kickoff(struct parahotplug_request *req)
1559{
2ea5117b 1560 struct controlvm_message_packet *cmd = &req->msg.cmd;
12e364b9
KC
1561 char env_cmd[40], env_id[40], env_state[40], env_bus[40], env_dev[40],
1562 env_func[40];
1563 char *envp[] = {
1564 env_cmd, env_id, env_state, env_bus, env_dev, env_func, NULL
1565 };
1566
1567 sprintf(env_cmd, "SPAR_PARAHOTPLUG=1");
1568 sprintf(env_id, "SPAR_PARAHOTPLUG_ID=%d", req->id);
1569 sprintf(env_state, "SPAR_PARAHOTPLUG_STATE=%d",
2ea5117b 1570 cmd->device_change_state.state.active);
12e364b9 1571 sprintf(env_bus, "SPAR_PARAHOTPLUG_BUS=%d",
2ea5117b 1572 cmd->device_change_state.bus_no);
12e364b9 1573 sprintf(env_dev, "SPAR_PARAHOTPLUG_DEVICE=%d",
2ea5117b 1574 cmd->device_change_state.dev_no >> 3);
12e364b9 1575 sprintf(env_func, "SPAR_PARAHOTPLUG_FUNCTION=%d",
2ea5117b 1576 cmd->device_change_state.dev_no & 0x7);
12e364b9 1577
eb34e877 1578 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
12e364b9
KC
1579 envp);
1580}
1581
1582/*
1583 * Remove any request from the list that's been on there too long and
1584 * respond with an error.
1585 */
1586static void
1587parahotplug_process_list(void)
1588{
e82ba62e
JS
1589 struct list_head *pos;
1590 struct list_head *tmp;
12e364b9 1591
ddf5de53 1592 spin_lock(&parahotplug_request_list_lock);
12e364b9 1593
ddf5de53 1594 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
12e364b9
KC
1595 struct parahotplug_request *req =
1596 list_entry(pos, struct parahotplug_request, list);
55b33413
BR
1597
1598 if (!time_after_eq(jiffies, req->expiration))
1599 continue;
1600
1601 list_del(pos);
1602 if (req->msg.hdr.flags.response_expected)
1603 controlvm_respond_physdev_changestate(
1604 &req->msg.hdr,
1605 CONTROLVM_RESP_ERROR_DEVICE_UDEV_TIMEOUT,
1606 req->msg.cmd.device_change_state.state);
1607 parahotplug_request_destroy(req);
12e364b9
KC
1608 }
1609
ddf5de53 1610 spin_unlock(&parahotplug_request_list_lock);
12e364b9
KC
1611}
1612
1613/*
1614 * Called from the /proc handler, which means the user script has
1615 * finished the enable/disable. Find the matching identifier, and
1616 * respond to the CONTROLVM message with success.
1617 */
1618static int
b06bdf7d 1619parahotplug_request_complete(int id, u16 active)
12e364b9 1620{
e82ba62e
JS
1621 struct list_head *pos;
1622 struct list_head *tmp;
12e364b9 1623
ddf5de53 1624 spin_lock(&parahotplug_request_list_lock);
12e364b9
KC
1625
1626 /* Look for a request matching "id". */
ddf5de53 1627 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
12e364b9
KC
1628 struct parahotplug_request *req =
1629 list_entry(pos, struct parahotplug_request, list);
1630 if (req->id == id) {
1631 /* Found a match. Remove it from the list and
1632 * respond.
1633 */
1634 list_del(pos);
ddf5de53 1635 spin_unlock(&parahotplug_request_list_lock);
2ea5117b 1636 req->msg.cmd.device_change_state.state.active = active;
98d7b594 1637 if (req->msg.hdr.flags.response_expected)
12e364b9
KC
1638 controlvm_respond_physdev_changestate(
1639 &req->msg.hdr, CONTROLVM_RESP_SUCCESS,
2ea5117b 1640 req->msg.cmd.device_change_state.state);
12e364b9
KC
1641 parahotplug_request_destroy(req);
1642 return 0;
1643 }
1644 }
1645
ddf5de53 1646 spin_unlock(&parahotplug_request_list_lock);
12e364b9
KC
1647 return -1;
1648}
1649
1650/*
1651 * Enables or disables a PCI device by kicking off a udev script
1652 */
bd5b9b32 1653static void
3ab47701 1654parahotplug_process_message(struct controlvm_message *inmsg)
12e364b9
KC
1655{
1656 struct parahotplug_request *req;
1657
1658 req = parahotplug_request_create(inmsg);
1659
38f736e9 1660 if (!req)
12e364b9 1661 return;
12e364b9 1662
2ea5117b 1663 if (inmsg->cmd.device_change_state.state.active) {
12e364b9
KC
1664 /* For enable messages, just respond with success
1665 * right away. This is a bit of a hack, but there are
1666 * issues with the early enable messages we get (with
1667 * either the udev script not detecting that the device
1668 * is up, or not getting called at all). Fortunately
1669 * the messages that get lost don't matter anyway, as
1670 * devices are automatically enabled at
1671 * initialization.
1672 */
1673 parahotplug_request_kickoff(req);
1674 controlvm_respond_physdev_changestate(&inmsg->hdr,
8e76e695
BR
1675 CONTROLVM_RESP_SUCCESS,
1676 inmsg->cmd.device_change_state.state);
12e364b9
KC
1677 parahotplug_request_destroy(req);
1678 } else {
1679 /* For disable messages, add the request to the
1680 * request list before kicking off the udev script. It
1681 * won't get responded to until the script has
1682 * indicated it's done.
1683 */
ddf5de53
BR
1684 spin_lock(&parahotplug_request_list_lock);
1685 list_add_tail(&req->list, &parahotplug_request_list);
1686 spin_unlock(&parahotplug_request_list_lock);
12e364b9
KC
1687
1688 parahotplug_request_kickoff(req);
1689 }
1690}
1691
12e364b9
KC
1692/* Process a controlvm message.
1693 * Return result:
779d0752 1694 * false - this function will return false only in the case where the
12e364b9
KC
1695 * controlvm message was NOT processed, but processing must be
1696 * retried before reading the next controlvm message; a
1697 * scenario where this can occur is when we need to throttle
1698 * the allocation of memory in which to copy out controlvm
1699 * payload data
f4c11551 1700 * true - processing of the controlvm message completed,
12e364b9
KC
1701 * either successfully or with an error.
1702 */
f4c11551 1703static bool
d5b3f1dc 1704handle_command(struct controlvm_message inmsg, u64 channel_addr)
12e364b9 1705{
2ea5117b 1706 struct controlvm_message_packet *cmd = &inmsg.cmd;
e82ba62e
JS
1707 u64 parm_addr;
1708 u32 parm_bytes;
317d9614 1709 struct parser_context *parser_ctx = NULL;
e82ba62e 1710 bool local_addr;
3ab47701 1711 struct controlvm_message ackmsg;
12e364b9
KC
1712
1713 /* create parsing context if necessary */
818352a8 1714 local_addr = (inmsg.hdr.flags.test_message == 1);
0aca7844 1715 if (channel_addr == 0)
f4c11551 1716 return true;
818352a8
BR
1717 parm_addr = channel_addr + inmsg.hdr.payload_vm_offset;
1718 parm_bytes = inmsg.hdr.payload_bytes;
12e364b9
KC
1719
1720 /* Parameter and channel addresses within test messages actually lie
1721 * within our OS-controlled memory. We need to know that, because it
1722 * makes a difference in how we compute the virtual address.
1723 */
ebec8967 1724 if (parm_addr && parm_bytes) {
f4c11551 1725 bool retry = false;
26eb2c0c 1726
12e364b9 1727 parser_ctx =
818352a8
BR
1728 parser_init_byte_stream(parm_addr, parm_bytes,
1729 local_addr, &retry);
1b08872e 1730 if (!parser_ctx && retry)
f4c11551 1731 return false;
12e364b9
KC
1732 }
1733
818352a8 1734 if (!local_addr) {
12e364b9
KC
1735 controlvm_init_response(&ackmsg, &inmsg.hdr,
1736 CONTROLVM_RESP_SUCCESS);
c3d9a224
BR
1737 if (controlvm_channel)
1738 visorchannel_signalinsert(controlvm_channel,
1b08872e
BR
1739 CONTROLVM_QUEUE_ACK,
1740 &ackmsg);
12e364b9 1741 }
98d7b594 1742 switch (inmsg.hdr.id) {
12e364b9 1743 case CONTROLVM_CHIPSET_INIT:
12e364b9
KC
1744 chipset_init(&inmsg);
1745 break;
1746 case CONTROLVM_BUS_CREATE:
12e364b9
KC
1747 bus_create(&inmsg);
1748 break;
1749 case CONTROLVM_BUS_DESTROY:
12e364b9
KC
1750 bus_destroy(&inmsg);
1751 break;
1752 case CONTROLVM_BUS_CONFIGURE:
12e364b9
KC
1753 bus_configure(&inmsg, parser_ctx);
1754 break;
1755 case CONTROLVM_DEVICE_CREATE:
12e364b9
KC
1756 my_device_create(&inmsg);
1757 break;
1758 case CONTROLVM_DEVICE_CHANGESTATE:
2ea5117b 1759 if (cmd->device_change_state.flags.phys_device) {
12e364b9
KC
1760 parahotplug_process_message(&inmsg);
1761 } else {
12e364b9
KC
1762 /* save the hdr and cmd structures for later use */
1763 /* when sending back the response to Command */
1764 my_device_changestate(&inmsg);
4f44b72d 1765 g_devicechangestate_packet = inmsg.cmd;
12e364b9
KC
1766 break;
1767 }
1768 break;
1769 case CONTROLVM_DEVICE_DESTROY:
12e364b9
KC
1770 my_device_destroy(&inmsg);
1771 break;
1772 case CONTROLVM_DEVICE_CONFIGURE:
12e364b9 1773 /* no op for now, just send a respond that we passed */
98d7b594 1774 if (inmsg.hdr.flags.response_expected)
12e364b9
KC
1775 controlvm_respond(&inmsg.hdr, CONTROLVM_RESP_SUCCESS);
1776 break;
1777 case CONTROLVM_CHIPSET_READY:
12e364b9
KC
1778 chipset_ready(&inmsg.hdr);
1779 break;
1780 case CONTROLVM_CHIPSET_SELFTEST:
12e364b9
KC
1781 chipset_selftest(&inmsg.hdr);
1782 break;
1783 case CONTROLVM_CHIPSET_STOP:
12e364b9
KC
1784 chipset_notready(&inmsg.hdr);
1785 break;
1786 default:
98d7b594 1787 if (inmsg.hdr.flags.response_expected)
12e364b9 1788 controlvm_respond(&inmsg.hdr,
818352a8 1789 -CONTROLVM_RESP_ERROR_MESSAGE_ID_UNKNOWN);
12e364b9
KC
1790 break;
1791 }
1792
38f736e9 1793 if (parser_ctx) {
12e364b9
KC
1794 parser_done(parser_ctx);
1795 parser_ctx = NULL;
1796 }
f4c11551 1797 return true;
12e364b9
KC
1798}
1799
5f3a7e36
DK
1800static inline unsigned int
1801issue_vmcall_io_controlvm_addr(u64 *control_addr, u32 *control_bytes)
1802{
1803 struct vmcall_io_controlvm_addr_params params;
1804 int result = VMCALL_SUCCESS;
1805 u64 physaddr;
1806
1807 physaddr = virt_to_phys(&params);
1808 ISSUE_IO_VMCALL(VMCALL_IO_CONTROLVM_ADDR, physaddr, result);
1809 if (VMCALL_SUCCESSFUL(result)) {
1810 *control_addr = params.address;
1811 *control_bytes = params.channel_bytes;
1812 }
1813 return result;
1814}
1815
d5b3f1dc 1816static u64 controlvm_get_channel_address(void)
524b0b63 1817{
5fc0229a 1818 u64 addr = 0;
b3c55b13 1819 u32 size = 0;
524b0b63 1820
0aca7844 1821 if (!VMCALL_SUCCESSFUL(issue_vmcall_io_controlvm_addr(&addr, &size)))
524b0b63 1822 return 0;
0aca7844 1823
524b0b63
BR
1824 return addr;
1825}
1826
12e364b9
KC
1827static void
1828controlvm_periodic_work(struct work_struct *work)
1829{
3ab47701 1830 struct controlvm_message inmsg;
f4c11551
JS
1831 bool got_command = false;
1832 bool handle_command_failed = false;
1c1ed292 1833 static u64 poll_count;
12e364b9
KC
1834
1835 /* make sure visorbus server is registered for controlvm callbacks */
4da3336c 1836 if (visorchipset_visorbusregwait && !visorbusregistered)
1c1ed292 1837 goto cleanup;
12e364b9 1838
1c1ed292
BR
1839 poll_count++;
1840 if (poll_count >= 250)
12e364b9
KC
1841 ; /* keep going */
1842 else
1c1ed292 1843 goto cleanup;
12e364b9
KC
1844
1845 /* Check events to determine if response to CHIPSET_READY
1846 * should be sent
1847 */
0639ba67
BR
1848 if (visorchipset_holdchipsetready &&
1849 (g_chipset_msg_hdr.id != CONTROLVM_INVALID)) {
12e364b9 1850 if (check_chipset_events() == 1) {
da021f02 1851 controlvm_respond(&g_chipset_msg_hdr, 0);
12e364b9 1852 clear_chipset_events();
da021f02 1853 memset(&g_chipset_msg_hdr, 0,
98d7b594 1854 sizeof(struct controlvm_message_header));
12e364b9
KC
1855 }
1856 }
1857
c3d9a224 1858 while (visorchannel_signalremove(controlvm_channel,
8a1182eb 1859 CONTROLVM_QUEUE_RESPONSE,
c3d9a224
BR
1860 &inmsg))
1861 ;
1c1ed292 1862 if (!got_command) {
7166ed19 1863 if (controlvm_pending_msg_valid) {
8a1182eb
BR
1864 /* we throttled processing of a prior
1865 * msg, so try to process it again
1866 * rather than reading a new one
1867 */
7166ed19 1868 inmsg = controlvm_pending_msg;
f4c11551 1869 controlvm_pending_msg_valid = false;
1c1ed292 1870 got_command = true;
75c1f8b7 1871 } else {
1c1ed292 1872 got_command = read_controlvm_event(&inmsg);
75c1f8b7 1873 }
8a1182eb 1874 }
12e364b9 1875
f4c11551 1876 handle_command_failed = false;
1c1ed292 1877 while (got_command && (!handle_command_failed)) {
b53e0e93 1878 most_recent_message_jiffies = jiffies;
8a1182eb
BR
1879 if (handle_command(inmsg,
1880 visorchannel_get_physaddr
c3d9a224 1881 (controlvm_channel)))
1c1ed292 1882 got_command = read_controlvm_event(&inmsg);
8a1182eb
BR
1883 else {
1884 /* this is a scenario where throttling
1885 * is required, but probably NOT an
1886 * error...; we stash the current
1887 * controlvm msg so we will attempt to
1888 * reprocess it on our next loop
1889 */
f4c11551 1890 handle_command_failed = true;
7166ed19 1891 controlvm_pending_msg = inmsg;
f4c11551 1892 controlvm_pending_msg_valid = true;
12e364b9
KC
1893 }
1894 }
1895
1896 /* parahotplug_worker */
1897 parahotplug_process_list();
1898
1c1ed292 1899cleanup:
12e364b9
KC
1900
1901 if (time_after(jiffies,
b53e0e93 1902 most_recent_message_jiffies + (HZ * MIN_IDLE_SECONDS))) {
12e364b9
KC
1903 /* it's been longer than MIN_IDLE_SECONDS since we
1904 * processed our last controlvm message; slow down the
1905 * polling
1906 */
911e213e
BR
1907 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_SLOW)
1908 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
12e364b9 1909 } else {
911e213e
BR
1910 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_FAST)
1911 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
12e364b9
KC
1912 }
1913
9232d2d6
BR
1914 queue_delayed_work(periodic_controlvm_workqueue,
1915 &periodic_controlvm_work, poll_jiffies);
12e364b9
KC
1916}
1917
1918static void
1919setup_crash_devices_work_queue(struct work_struct *work)
1920{
e6bdb904
BR
1921 struct controlvm_message local_crash_bus_msg;
1922 struct controlvm_message local_crash_dev_msg;
3ab47701 1923 struct controlvm_message msg;
e6bdb904
BR
1924 u32 local_crash_msg_offset;
1925 u16 local_crash_msg_count;
12e364b9 1926
4da3336c
DK
1927 /* make sure visorbus is registered for controlvm callbacks */
1928 if (visorchipset_visorbusregwait && !visorbusregistered)
e6bdb904 1929 goto cleanup;
12e364b9
KC
1930
1931 POSTCODE_LINUX_2(CRASH_DEV_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1932
1933 /* send init chipset msg */
98d7b594 1934 msg.hdr.id = CONTROLVM_CHIPSET_INIT;
2ea5117b
BR
1935 msg.cmd.init_chipset.bus_count = 23;
1936 msg.cmd.init_chipset.switch_count = 0;
12e364b9
KC
1937
1938 chipset_init(&msg);
1939
12e364b9 1940 /* get saved message count */
c3d9a224 1941 if (visorchannel_read(controlvm_channel,
d19642f6
BR
1942 offsetof(struct spar_controlvm_channel_protocol,
1943 saved_crash_message_count),
e6bdb904 1944 &local_crash_msg_count, sizeof(u16)) < 0) {
12e364b9
KC
1945 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
1946 POSTCODE_SEVERITY_ERR);
1947 return;
1948 }
1949
e6bdb904 1950 if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
12e364b9 1951 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
e6bdb904 1952 local_crash_msg_count,
12e364b9
KC
1953 POSTCODE_SEVERITY_ERR);
1954 return;
1955 }
1956
1957 /* get saved crash message offset */
c3d9a224 1958 if (visorchannel_read(controlvm_channel,
d19642f6
BR
1959 offsetof(struct spar_controlvm_channel_protocol,
1960 saved_crash_message_offset),
e6bdb904 1961 &local_crash_msg_offset, sizeof(u32)) < 0) {
12e364b9
KC
1962 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
1963 POSTCODE_SEVERITY_ERR);
1964 return;
1965 }
1966
1967 /* read create device message for storage bus offset */
c3d9a224 1968 if (visorchannel_read(controlvm_channel,
e6bdb904
BR
1969 local_crash_msg_offset,
1970 &local_crash_bus_msg,
3ab47701 1971 sizeof(struct controlvm_message)) < 0) {
12e364b9
KC
1972 POSTCODE_LINUX_2(CRASH_DEV_RD_BUS_FAIULRE_PC,
1973 POSTCODE_SEVERITY_ERR);
1974 return;
1975 }
1976
1977 /* read create device message for storage device */
c3d9a224 1978 if (visorchannel_read(controlvm_channel,
e6bdb904 1979 local_crash_msg_offset +
3ab47701 1980 sizeof(struct controlvm_message),
e6bdb904 1981 &local_crash_dev_msg,
3ab47701 1982 sizeof(struct controlvm_message)) < 0) {
12e364b9
KC
1983 POSTCODE_LINUX_2(CRASH_DEV_RD_DEV_FAIULRE_PC,
1984 POSTCODE_SEVERITY_ERR);
1985 return;
1986 }
1987
1988 /* reuse IOVM create bus message */
ebec8967 1989 if (local_crash_bus_msg.cmd.create_bus.channel_addr) {
e6bdb904 1990 bus_create(&local_crash_bus_msg);
75c1f8b7 1991 } else {
12e364b9
KC
1992 POSTCODE_LINUX_2(CRASH_DEV_BUS_NULL_FAILURE_PC,
1993 POSTCODE_SEVERITY_ERR);
1994 return;
1995 }
1996
1997 /* reuse create device message for storage device */
ebec8967 1998 if (local_crash_dev_msg.cmd.create_device.channel_addr) {
e6bdb904 1999 my_device_create(&local_crash_dev_msg);
75c1f8b7 2000 } else {
12e364b9
KC
2001 POSTCODE_LINUX_2(CRASH_DEV_DEV_NULL_FAILURE_PC,
2002 POSTCODE_SEVERITY_ERR);
2003 return;
2004 }
12e364b9
KC
2005 POSTCODE_LINUX_2(CRASH_DEV_EXIT_PC, POSTCODE_SEVERITY_INFO);
2006 return;
2007
e6bdb904 2008cleanup:
12e364b9 2009
911e213e 2010 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
12e364b9 2011
9232d2d6
BR
2012 queue_delayed_work(periodic_controlvm_workqueue,
2013 &periodic_controlvm_work, poll_jiffies);
12e364b9
KC
2014}
2015
2016static void
d32517e3 2017bus_create_response(struct visor_device *bus_info, int response)
12e364b9 2018{
0274b5ae
DZ
2019 if (response >= 0) {
2020 bus_info->state.created = 1;
0274b5ae
DZ
2021 }
2022
2023 bus_responder(CONTROLVM_BUS_CREATE, bus_info->pending_msg_hdr,
2024 response);
2025
2026 kfree(bus_info->pending_msg_hdr);
2027 bus_info->pending_msg_hdr = NULL;
12e364b9
KC
2028}
2029
2030static void
d32517e3 2031bus_destroy_response(struct visor_device *bus_info, int response)
12e364b9 2032{
0274b5ae
DZ
2033 bus_responder(CONTROLVM_BUS_DESTROY, bus_info->pending_msg_hdr,
2034 response);
2035
2036 kfree(bus_info->pending_msg_hdr);
2037 bus_info->pending_msg_hdr = NULL;
12e364b9
KC
2038}
2039
2040static void
a298bc0b 2041device_create_response(struct visor_device *dev_info, int response)
12e364b9 2042{
0274b5ae
DZ
2043 if (response >= 0)
2044 dev_info->state.created = 1;
2045
2046 device_responder(CONTROLVM_DEVICE_CREATE, dev_info->pending_msg_hdr,
2047 response);
2048
2049 kfree(dev_info->pending_msg_hdr);
12e364b9
KC
2050}
2051
2052static void
a298bc0b 2053device_destroy_response(struct visor_device *dev_info, int response)
12e364b9 2054{
0274b5ae
DZ
2055 device_responder(CONTROLVM_DEVICE_DESTROY, dev_info->pending_msg_hdr,
2056 response);
2057
2058 kfree(dev_info->pending_msg_hdr);
2059 dev_info->pending_msg_hdr = NULL;
12e364b9
KC
2060}
2061
d3368a58 2062static void
a298bc0b 2063visorchipset_device_pause_response(struct visor_device *dev_info,
b4b598fd 2064 int response)
12e364b9 2065{
12e364b9 2066 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
b4b598fd 2067 dev_info, response,
bd0d2dcc 2068 segment_state_standby);
0274b5ae
DZ
2069
2070 kfree(dev_info->pending_msg_hdr);
2071 dev_info->pending_msg_hdr = NULL;
12e364b9 2072}
12e364b9
KC
2073
2074static void
a298bc0b 2075device_resume_response(struct visor_device *dev_info, int response)
12e364b9
KC
2076{
2077 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
b4b598fd 2078 dev_info, response,
bd0d2dcc 2079 segment_state_running);
0274b5ae
DZ
2080
2081 kfree(dev_info->pending_msg_hdr);
2082 dev_info->pending_msg_hdr = NULL;
12e364b9
KC
2083}
2084
18b87ed1 2085static ssize_t chipsetready_store(struct device *dev,
8e76e695
BR
2086 struct device_attribute *attr,
2087 const char *buf, size_t count)
12e364b9 2088{
18b87ed1 2089 char msgtype[64];
12e364b9 2090
66e24b76
BR
2091 if (sscanf(buf, "%63s", msgtype) != 1)
2092 return -EINVAL;
2093
ebec8967 2094 if (!strcmp(msgtype, "CALLHOMEDISK_MOUNTED")) {
66e24b76
BR
2095 chipset_events[0] = 1;
2096 return count;
ebec8967 2097 } else if (!strcmp(msgtype, "MODULES_LOADED")) {
66e24b76
BR
2098 chipset_events[1] = 1;
2099 return count;
e22a4a0f
BR
2100 }
2101 return -EINVAL;
12e364b9
KC
2102}
2103
e56fa7cd
BR
2104/* The parahotplug/devicedisabled interface gets called by our support script
2105 * when an SR-IOV device has been shut down. The ID is passed to the script
2106 * and then passed back when the device has been removed.
2107 */
2108static ssize_t devicedisabled_store(struct device *dev,
8e76e695
BR
2109 struct device_attribute *attr,
2110 const char *buf, size_t count)
e56fa7cd 2111{
94217363 2112 unsigned int id;
e56fa7cd 2113
ebec8967 2114 if (kstrtouint(buf, 10, &id))
e56fa7cd
BR
2115 return -EINVAL;
2116
2117 parahotplug_request_complete(id, 0);
2118 return count;
2119}
2120
2121/* The parahotplug/deviceenabled interface gets called by our support script
2122 * when an SR-IOV device has been recovered. The ID is passed to the script
2123 * and then passed back when the device has been brought back up.
2124 */
2125static ssize_t deviceenabled_store(struct device *dev,
8e76e695
BR
2126 struct device_attribute *attr,
2127 const char *buf, size_t count)
e56fa7cd 2128{
94217363 2129 unsigned int id;
e56fa7cd 2130
ebec8967 2131 if (kstrtouint(buf, 10, &id))
e56fa7cd
BR
2132 return -EINVAL;
2133
2134 parahotplug_request_complete(id, 1);
2135 return count;
2136}
2137
e3420ed6
EA
2138static int
2139visorchipset_mmap(struct file *file, struct vm_area_struct *vma)
2140{
2141 unsigned long physaddr = 0;
2142 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
780fcad3 2143 u64 addr = 0;
e3420ed6
EA
2144
2145 /* sv_enable_dfp(); */
2146 if (offset & (PAGE_SIZE - 1))
2147 return -ENXIO; /* need aligned offsets */
2148
2149 switch (offset) {
2150 case VISORCHIPSET_MMAP_CONTROLCHANOFFSET:
2151 vma->vm_flags |= VM_IO;
2152 if (!*file_controlvm_channel)
2153 return -ENXIO;
2154
2155 visorchannel_read(*file_controlvm_channel,
2156 offsetof(struct spar_controlvm_channel_protocol,
2157 gp_control_channel),
2158 &addr, sizeof(addr));
2159 if (!addr)
2160 return -ENXIO;
2161
2162 physaddr = (unsigned long)addr;
2163 if (remap_pfn_range(vma, vma->vm_start,
2164 physaddr >> PAGE_SHIFT,
2165 vma->vm_end - vma->vm_start,
2166 /*pgprot_noncached */
2167 (vma->vm_page_prot))) {
2168 return -EAGAIN;
2169 }
2170 break;
2171 default:
2172 return -ENXIO;
2173 }
2174 return 0;
2175}
2176
5f3a7e36
DK
2177static inline s64 issue_vmcall_query_guest_virtual_time_offset(void)
2178{
2179 u64 result = VMCALL_SUCCESS;
2180 u64 physaddr = 0;
2181
2182 ISSUE_IO_VMCALL(VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET, physaddr,
2183 result);
2184 return result;
2185}
2186
2187static inline int issue_vmcall_update_physical_time(u64 adjustment)
2188{
2189 int result = VMCALL_SUCCESS;
2190
2191 ISSUE_IO_VMCALL(VMCALL_UPDATE_PHYSICAL_TIME, adjustment, result);
2192 return result;
2193}
2194
e3420ed6
EA
2195static long visorchipset_ioctl(struct file *file, unsigned int cmd,
2196 unsigned long arg)
2197{
2198 s64 adjustment;
2199 s64 vrtc_offset;
2200
2201 switch (cmd) {
2202 case VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET:
2203 /* get the physical rtc offset */
2204 vrtc_offset = issue_vmcall_query_guest_virtual_time_offset();
2205 if (copy_to_user((void __user *)arg, &vrtc_offset,
2206 sizeof(vrtc_offset))) {
2207 return -EFAULT;
2208 }
d5b3f1dc 2209 return 0;
e3420ed6
EA
2210 case VMCALL_UPDATE_PHYSICAL_TIME:
2211 if (copy_from_user(&adjustment, (void __user *)arg,
2212 sizeof(adjustment))) {
2213 return -EFAULT;
2214 }
2215 return issue_vmcall_update_physical_time(adjustment);
2216 default:
2217 return -EFAULT;
2218 }
2219}
2220
2221static const struct file_operations visorchipset_fops = {
2222 .owner = THIS_MODULE,
2223 .open = visorchipset_open,
2224 .read = NULL,
2225 .write = NULL,
2226 .unlocked_ioctl = visorchipset_ioctl,
2227 .release = visorchipset_release,
2228 .mmap = visorchipset_mmap,
2229};
2230
0f570fc0 2231static int
e3420ed6
EA
2232visorchipset_file_init(dev_t major_dev, struct visorchannel **controlvm_channel)
2233{
2234 int rc = 0;
2235
2236 file_controlvm_channel = controlvm_channel;
2237 cdev_init(&file_cdev, &visorchipset_fops);
2238 file_cdev.owner = THIS_MODULE;
2239 if (MAJOR(major_dev) == 0) {
46168810 2240 rc = alloc_chrdev_region(&major_dev, 0, 1, "visorchipset");
e3420ed6
EA
2241 /* dynamic major device number registration required */
2242 if (rc < 0)
2243 return rc;
2244 } else {
2245 /* static major device number registration required */
46168810 2246 rc = register_chrdev_region(major_dev, 1, "visorchipset");
e3420ed6
EA
2247 if (rc < 0)
2248 return rc;
2249 }
2250 rc = cdev_add(&file_cdev, MKDEV(MAJOR(major_dev), 0), 1);
2251 if (rc < 0) {
2252 unregister_chrdev_region(major_dev, 1);
2253 return rc;
2254 }
2255 return 0;
2256}
2257
55c67dca
PB
2258static int
2259visorchipset_init(struct acpi_device *acpi_device)
12e364b9 2260{
33078257 2261 int rc = 0;
d5b3f1dc 2262 u64 addr;
d3368a58
JS
2263 int tmp_sz = sizeof(struct spar_controlvm_channel_protocol);
2264 uuid_le uuid = SPAR_CONTROLVM_CHANNEL_PROTOCOL_UUID;
2265
2266 addr = controlvm_get_channel_address();
2267 if (!addr)
2268 return -ENODEV;
12e364b9 2269
4da3336c 2270 memset(&busdev_notifiers, 0, sizeof(busdev_notifiers));
84982fbf 2271 memset(&controlvm_payload_info, 0, sizeof(controlvm_payload_info));
12e364b9 2272
d3368a58
JS
2273 controlvm_channel = visorchannel_create_with_lock(addr, tmp_sz,
2274 GFP_KERNEL, uuid);
2275 if (SPAR_CONTROLVM_CHANNEL_OK_CLIENT(
2276 visorchannel_get_header(controlvm_channel))) {
2277 initialize_controlvm_payload();
8a1182eb 2278 } else {
d3368a58
JS
2279 visorchannel_destroy(controlvm_channel);
2280 controlvm_channel = NULL;
8a1182eb
BR
2281 return -ENODEV;
2282 }
2283
5aa8ae57
BR
2284 major_dev = MKDEV(visorchipset_major, 0);
2285 rc = visorchipset_file_init(major_dev, &controlvm_channel);
4cb005a9 2286 if (rc < 0) {
4cb005a9 2287 POSTCODE_LINUX_2(CHIPSET_INIT_FAILURE_PC, DIAG_SEVERITY_ERR);
a6a3989b 2288 goto cleanup;
4cb005a9 2289 }
9f8d0e8b 2290
da021f02 2291 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
12e364b9 2292
4da3336c
DK
2293 /* if booting in a crash kernel */
2294 if (is_kdump_kernel())
2295 INIT_DELAYED_WORK(&periodic_controlvm_work,
2296 setup_crash_devices_work_queue);
2297 else
2298 INIT_DELAYED_WORK(&periodic_controlvm_work,
2299 controlvm_periodic_work);
2300 periodic_controlvm_workqueue =
2301 create_singlethread_workqueue("visorchipset_controlvm");
2302
2303 if (!periodic_controlvm_workqueue) {
2304 POSTCODE_LINUX_2(CREATE_WORKQUEUE_FAILED_PC,
2305 DIAG_SEVERITY_ERR);
2306 rc = -ENOMEM;
2307 goto cleanup;
2308 }
2309 most_recent_message_jiffies = jiffies;
2310 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
2311 rc = queue_delayed_work(periodic_controlvm_workqueue,
2312 &periodic_controlvm_work, poll_jiffies);
2313 if (rc < 0) {
2314 POSTCODE_LINUX_2(QUEUE_DELAYED_WORK_PC,
2315 DIAG_SEVERITY_ERR);
2316 goto cleanup;
12e364b9
KC
2317 }
2318
eb34e877
BR
2319 visorchipset_platform_device.dev.devt = major_dev;
2320 if (platform_device_register(&visorchipset_platform_device) < 0) {
4cb005a9
KC
2321 POSTCODE_LINUX_2(DEVICE_REGISTER_FAILURE_PC, DIAG_SEVERITY_ERR);
2322 rc = -1;
a6a3989b 2323 goto cleanup;
4cb005a9 2324 }
12e364b9 2325 POSTCODE_LINUX_2(CHIPSET_INIT_SUCCESS_PC, POSTCODE_SEVERITY_INFO);
c79b28f7
PB
2326
2327 rc = visorbus_init();
a6a3989b 2328cleanup:
12e364b9 2329 if (rc) {
12e364b9
KC
2330 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
2331 POSTCODE_SEVERITY_ERR);
2332 }
2333 return rc;
2334}
2335
0f570fc0 2336static void
e3420ed6
EA
2337visorchipset_file_cleanup(dev_t major_dev)
2338{
2339 if (file_cdev.ops)
2340 cdev_del(&file_cdev);
2341 file_cdev.ops = NULL;
2342 unregister_chrdev_region(major_dev, 1);
2343}
2344
55c67dca
PB
2345static int
2346visorchipset_exit(struct acpi_device *acpi_device)
12e364b9 2347{
12e364b9
KC
2348 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
2349
c79b28f7
PB
2350 visorbus_exit();
2351
4da3336c
DK
2352 cancel_delayed_work(&periodic_controlvm_work);
2353 flush_workqueue(periodic_controlvm_workqueue);
2354 destroy_workqueue(periodic_controlvm_workqueue);
2355 periodic_controlvm_workqueue = NULL;
2356 destroy_controlvm_payload_info(&controlvm_payload_info);
1783319f 2357
da021f02 2358 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
12e364b9 2359
c3d9a224 2360 visorchannel_destroy(controlvm_channel);
8a1182eb 2361
addceb12 2362 visorchipset_file_cleanup(visorchipset_platform_device.dev.devt);
04dacacc 2363 platform_device_unregister(&visorchipset_platform_device);
12e364b9 2364 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
55c67dca
PB
2365
2366 return 0;
2367}
2368
2369static const struct acpi_device_id unisys_device_ids[] = {
2370 {"PNP0A07", 0},
2371 {"", 0},
2372};
55c67dca
PB
2373
2374static struct acpi_driver unisys_acpi_driver = {
2375 .name = "unisys_acpi",
2376 .class = "unisys_acpi_class",
2377 .owner = THIS_MODULE,
2378 .ids = unisys_device_ids,
2379 .ops = {
2380 .add = visorchipset_init,
2381 .remove = visorchipset_exit,
2382 },
2383};
d5b3f1dc
EA
2384static __init uint32_t visorutil_spar_detect(void)
2385{
2386 unsigned int eax, ebx, ecx, edx;
2387
2388 if (cpu_has_hypervisor) {
2389 /* check the ID */
2390 cpuid(UNISYS_SPAR_LEAF_ID, &eax, &ebx, &ecx, &edx);
2391 return (ebx == UNISYS_SPAR_ID_EBX) &&
2392 (ecx == UNISYS_SPAR_ID_ECX) &&
2393 (edx == UNISYS_SPAR_ID_EDX);
2394 } else {
2395 return 0;
2396 }
2397}
55c67dca
PB
2398
2399static int init_unisys(void)
2400{
2401 int result;
d5b3f1dc 2402 if (!visorutil_spar_detect())
55c67dca
PB
2403 return -ENODEV;
2404
2405 result = acpi_bus_register_driver(&unisys_acpi_driver);
2406 if (result)
2407 return -ENODEV;
2408
2409 pr_info("Unisys Visorchipset Driver Loaded.\n");
2410 return 0;
2411};
2412
2413static void exit_unisys(void)
2414{
2415 acpi_bus_unregister_driver(&unisys_acpi_driver);
12e364b9
KC
2416}
2417
12e364b9 2418module_param_named(major, visorchipset_major, int, S_IRUGO);
b615d628
JS
2419MODULE_PARM_DESC(visorchipset_major,
2420 "major device number to use for the device node");
4da3336c
DK
2421module_param_named(visorbusregwait, visorchipset_visorbusregwait, int, S_IRUGO);
2422MODULE_PARM_DESC(visorchipset_visorbusreqwait,
12e364b9 2423 "1 to have the module wait for the visor bus to register");
12e364b9
KC
2424module_param_named(holdchipsetready, visorchipset_holdchipsetready,
2425 int, S_IRUGO);
2426MODULE_PARM_DESC(visorchipset_holdchipsetready,
2427 "1 to hold response to CHIPSET_READY");
b615d628 2428
55c67dca
PB
2429module_init(init_unisys);
2430module_exit(exit_unisys);
12e364b9
KC
2431
2432MODULE_AUTHOR("Unisys");
2433MODULE_LICENSE("GPL");
2434MODULE_DESCRIPTION("Supervisor chipset driver for service partition: ver "
2435 VERSION);
2436MODULE_VERSION(VERSION);
This page took 0.447219 seconds and 5 git commands to generate.