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