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