staging: gdm724x: fix misplaced open braces
[deliverable/linux.git] / drivers / staging / unisys / visorchipset / visorchipset_main.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
18#include "globals.h"
12e364b9
KC
19#include "visorchipset.h"
20#include "procobjecttree.h"
21#include "visorchannel.h"
22#include "periodic_work.h"
23#include "testing.h"
24#include "file.h"
25#include "parser.h"
26#include "uniklog.h"
27#include "uisutils.h"
12e364b9
KC
28#include "controlvmcompletionstatus.h"
29#include "guestlinuxdebug.h"
12e364b9
KC
30
31#include <linux/nls.h>
32#include <linux/netdevice.h>
33#include <linux/platform_device.h>
90addb02 34#include <linux/uuid.h>
12e364b9
KC
35
36#define CURRENT_FILE_PC VISOR_CHIPSET_PC_visorchipset_main_c
37#define TEST_VNIC_PHYSITF "eth0" /* physical network itf for
38 * vnic loopback test */
39#define TEST_VNIC_SWITCHNO 1
40#define TEST_VNIC_BUSNO 9
41
42#define MAX_NAME_SIZE 128
43#define MAX_IP_SIZE 50
44#define MAXOUTSTANDINGCHANNELCOMMAND 256
45#define POLLJIFFIES_CONTROLVMCHANNEL_FAST 1
46#define POLLJIFFIES_CONTROLVMCHANNEL_SLOW 100
47
48/* When the controlvm channel is idle for at least MIN_IDLE_SECONDS,
49* we switch to slow polling mode. As soon as we get a controlvm
50* message, we switch back to fast polling mode.
51*/
52#define MIN_IDLE_SECONDS 10
bd5b9b32
KC
53static ulong Poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
54static ulong Most_recent_message_jiffies; /* when we got our last
55 * controlvm message */
12e364b9
KC
56static inline char *
57NONULLSTR(char *s)
58{
59 if (s)
60 return s;
61 else
62 return "";
63}
64
65static int serverregistered;
66static int clientregistered;
67
68#define MAX_CHIPSET_EVENTS 2
69static U8 chipset_events[MAX_CHIPSET_EVENTS] = { 0, 0 };
70
71static struct delayed_work Periodic_controlvm_work;
72static struct workqueue_struct *Periodic_controlvm_workqueue;
bd5b9b32 73static DEFINE_SEMAPHORE(NotifierLock);
12e364b9
KC
74
75typedef struct {
76 CONTROLVM_MESSAGE message;
77 unsigned int crc;
78} MESSAGE_ENVELOPE;
79
80static CONTROLVM_MESSAGE_HEADER g_DiagMsgHdr;
81static CONTROLVM_MESSAGE_HEADER g_ChipSetMsgHdr;
82static CONTROLVM_MESSAGE_HEADER g_DelDumpMsgHdr;
90addb02 83static const uuid_le UltraDiagPoolChannelProtocolGuid =
12e364b9
KC
84 ULTRA_DIAG_POOL_CHANNEL_PROTOCOL_GUID;
85/* 0xffffff is an invalid Bus/Device number */
86static ulong g_diagpoolBusNo = 0xffffff;
87static ulong g_diagpoolDevNo = 0xffffff;
88static CONTROLVM_MESSAGE_PACKET g_DeviceChangeStatePacket;
89
90/* Only VNIC and VHBA channels are sent to visorclientbus (aka
91 * "visorhackbus")
92 */
93#define FOR_VISORHACKBUS(channel_type_guid) \
90addb02
BR
94 (((uuid_le_cmp(channel_type_guid, UltraVnicChannelProtocolGuid) == 0)\
95 || (uuid_le_cmp(channel_type_guid, UltraVhbaChannelProtocolGuid) == 0)))
12e364b9
KC
96#define FOR_VISORBUS(channel_type_guid) (!(FOR_VISORHACKBUS(channel_type_guid)))
97
98#define is_diagpool_channel(channel_type_guid) \
90addb02 99 (uuid_le_cmp(channel_type_guid, UltraDiagPoolChannelProtocolGuid) == 0)
12e364b9
KC
100
101typedef enum {
102 PARTPROP_invalid,
103 PARTPROP_name,
104 PARTPROP_description,
105 PARTPROP_handle,
106 PARTPROP_busNumber,
107 /* add new properties above, but don't forget to change
108 * InitPartitionProperties() and show_partition_property() also...
109 */
110 PARTPROP_last
111} PARTITION_property;
112static const char *PartitionTypeNames[] = { "partition", NULL };
113
114static char *PartitionPropertyNames[PARTPROP_last + 1];
115static void
116InitPartitionProperties(void)
117{
118 char **p = PartitionPropertyNames;
119 p[PARTPROP_invalid] = "";
120 p[PARTPROP_name] = "name";
121 p[PARTPROP_description] = "description";
122 p[PARTPROP_handle] = "handle";
123 p[PARTPROP_busNumber] = "busNumber";
124 p[PARTPROP_last] = NULL;
125}
126
12e364b9 127static MYPROCTYPE *PartitionType;
12e364b9 128
12e364b9
KC
129#define VISORCHIPSET_PARAHOTPLUG_PROC_ENTRY_FN "parahotplug"
130static struct proc_dir_entry *parahotplug_proc_dir;
131
132static LIST_HEAD(BusInfoList);
133static LIST_HEAD(DevInfoList);
134
135static struct proc_dir_entry *ProcDir;
136static VISORCHANNEL *ControlVm_channel;
137
138static ssize_t visorchipset_proc_read_writeonly(struct file *file,
139 char __user *buf,
140 size_t len, loff_t *offset);
12e364b9 141
12e364b9 142typedef struct {
bd5b9b32 143 U8 __iomem *ptr; /* pointer to base address of payload pool */
12e364b9
KC
144 U64 offset; /* offset from beginning of controlvm
145 * channel to beginning of payload * pool */
146 U32 bytes; /* number of bytes in payload pool */
147} CONTROLVM_PAYLOAD_INFO;
148
149/* Manages the request payload in the controlvm channel */
150static CONTROLVM_PAYLOAD_INFO ControlVm_payload_info;
151
152static pCHANNEL_HEADER Test_Vnic_channel;
153
154typedef struct {
155 CONTROLVM_MESSAGE_HEADER Dumpcapture_header;
156 CONTROLVM_MESSAGE_HEADER Gettextdump_header;
157 CONTROLVM_MESSAGE_HEADER Dumpcomplete_header;
158 BOOL Gettextdump_outstanding;
159 u32 crc32;
160 ulong length;
161 atomic_t buffers_in_use;
162 ulong destination;
163} LIVEDUMP_INFO;
164/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
165 * CONTROLVM_DUMP_GETTEXTDUMP / CONTROLVM_DUMP_COMPLETE conversation.
166 */
167static LIVEDUMP_INFO LiveDump_info;
168
169/* The following globals are used to handle the scenario where we are unable to
170 * offload the payload from a controlvm message due to memory requirements. In
171 * this scenario, we simply stash the controlvm message, then attempt to
172 * process it again the next time controlvm_periodic_work() runs.
173 */
174static CONTROLVM_MESSAGE ControlVm_Pending_Msg;
175static BOOL ControlVm_Pending_Msg_Valid = FALSE;
176
177/* Pool of struct putfile_buffer_entry, for keeping track of pending (incoming)
178 * TRANSMIT_FILE PutFile payloads.
179 */
180static struct kmem_cache *Putfile_buffer_list_pool;
181static const char Putfile_buffer_list_pool_name[] =
182 "controlvm_putfile_buffer_list_pool";
183
184/* This identifies a data buffer that has been received via a controlvm messages
185 * in a remote --> local CONTROLVM_TRANSMIT_FILE conversation.
186 */
187struct putfile_buffer_entry {
188 struct list_head next; /* putfile_buffer_entry list */
189 PARSER_CONTEXT *parser_ctx; /* points to buffer containing input data */
190};
191
192/* List of struct putfile_request *, via next_putfile_request member.
193 * Each entry in this list identifies an outstanding TRANSMIT_FILE
194 * conversation.
195 */
196static LIST_HEAD(Putfile_request_list);
197
198/* This describes a buffer and its current state of transfer (e.g., how many
199 * bytes have already been supplied as putfile data, and how many bytes are
200 * remaining) for a putfile_request.
201 */
202struct putfile_active_buffer {
203 /* a payload from a controlvm message, containing a file data buffer */
204 PARSER_CONTEXT *parser_ctx;
205 /* points within data area of parser_ctx to next byte of data */
206 u8 *pnext;
207 /* # bytes left from <pnext> to the end of this data buffer */
208 size_t bytes_remaining;
209};
210
211#define PUTFILE_REQUEST_SIG 0x0906101302281211
212/* This identifies a single remote --> local CONTROLVM_TRANSMIT_FILE
213 * conversation. Structs of this type are dynamically linked into
214 * <Putfile_request_list>.
215 */
216struct putfile_request {
217 u64 sig; /* PUTFILE_REQUEST_SIG */
218
219 /* header from original TransmitFile request */
220 CONTROLVM_MESSAGE_HEADER controlvm_header;
221 u64 file_request_number; /* from original TransmitFile request */
222
223 /* link to next struct putfile_request */
224 struct list_head next_putfile_request;
225
226 /* most-recent sequence number supplied via a controlvm message */
227 u64 data_sequence_number;
228
229 /* head of putfile_buffer_entry list, which describes the data to be
230 * supplied as putfile data;
231 * - this list is added to when controlvm messages come in that supply
232 * file data
233 * - this list is removed from via the hotplug program that is actually
234 * consuming these buffers to write as file data */
235 struct list_head input_buffer_list;
236 spinlock_t req_list_lock; /* lock for input_buffer_list */
237
238 /* waiters for input_buffer_list to go non-empty */
239 wait_queue_head_t input_buffer_wq;
240
241 /* data not yet read within current putfile_buffer_entry */
242 struct putfile_active_buffer active_buf;
243
244 /* <0 = failed, 0 = in-progress, >0 = successful; */
245 /* note that this must be set with req_list_lock, and if you set <0, */
246 /* it is your responsibility to also free up all of the other objects */
247 /* in this struct (like input_buffer_list, active_buf.parser_ctx) */
248 /* before releasing the lock */
249 int completion_status;
250};
251
bd5b9b32 252static atomic_t Visorchipset_cache_buffers_in_use = ATOMIC_INIT(0);
12e364b9
KC
253
254struct parahotplug_request {
255 struct list_head list;
256 int id;
257 unsigned long expiration;
258 CONTROLVM_MESSAGE msg;
259};
260
261static LIST_HEAD(Parahotplug_request_list);
262static DEFINE_SPINLOCK(Parahotplug_request_list_lock); /* lock for above */
263static void parahotplug_process_list(void);
264
265/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
266 * CONTROLVM_REPORTEVENT.
267 */
268static VISORCHIPSET_BUSDEV_NOTIFIERS BusDev_Server_Notifiers;
269static VISORCHIPSET_BUSDEV_NOTIFIERS BusDev_Client_Notifiers;
270
271static void bus_create_response(ulong busNo, int response);
272static void bus_destroy_response(ulong busNo, int response);
273static void device_create_response(ulong busNo, ulong devNo, int response);
274static void device_destroy_response(ulong busNo, ulong devNo, int response);
275static void device_resume_response(ulong busNo, ulong devNo, int response);
276
277static VISORCHIPSET_BUSDEV_RESPONDERS BusDev_Responders = {
278 .bus_create = bus_create_response,
279 .bus_destroy = bus_destroy_response,
280 .device_create = device_create_response,
281 .device_destroy = device_destroy_response,
927c7927 282 .device_pause = visorchipset_device_pause_response,
12e364b9
KC
283 .device_resume = device_resume_response,
284};
285
286/* info for /dev/visorchipset */
287static dev_t MajorDev = -1; /**< indicates major num for device */
288
19f6634f
BR
289/* prototypes for attributes */
290static ssize_t toolaction_show(struct device *dev,
291 struct device_attribute *attr, char *buf);
292static ssize_t toolaction_store(struct device *dev,
293 struct device_attribute *attr, const char *buf, size_t count);
294static DEVICE_ATTR_RW(toolaction);
295
54b31229
BR
296static ssize_t boottotool_show(struct device *dev,
297 struct device_attribute *attr, char *buf);
298static ssize_t boottotool_store(struct device *dev,
299 struct device_attribute *attr, const char *buf, size_t count);
300static DEVICE_ATTR_RW(boottotool);
301
422af17c
BR
302static ssize_t error_show(struct device *dev, struct device_attribute *attr,
303 char *buf);
304static ssize_t error_store(struct device *dev, struct device_attribute *attr,
305 const char *buf, size_t count);
306static DEVICE_ATTR_RW(error);
307
308static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
309 char *buf);
310static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
311 const char *buf, size_t count);
312static DEVICE_ATTR_RW(textid);
313
314static ssize_t remaining_steps_show(struct device *dev,
315 struct device_attribute *attr, char *buf);
316static ssize_t remaining_steps_store(struct device *dev,
317 struct device_attribute *attr, const char *buf, size_t count);
318static DEVICE_ATTR_RW(remaining_steps);
319
18b87ed1
BR
320static ssize_t chipsetready_store(struct device *dev,
321 struct device_attribute *attr, const char *buf, size_t count);
322static DEVICE_ATTR_WO(chipsetready);
323
19f6634f
BR
324static struct attribute *visorchipset_install_attrs[] = {
325 &dev_attr_toolaction.attr,
54b31229 326 &dev_attr_boottotool.attr,
422af17c
BR
327 &dev_attr_error.attr,
328 &dev_attr_textid.attr,
329 &dev_attr_remaining_steps.attr,
19f6634f
BR
330 NULL
331};
332
333static struct attribute_group visorchipset_install_group = {
334 .name = "install",
335 .attrs = visorchipset_install_attrs
336};
337
18b87ed1
BR
338static struct attribute *visorchipset_guest_attrs[] = {
339 &dev_attr_chipsetready.attr,
340 NULL
341};
342
343static struct attribute_group visorchipset_guest_group = {
344 .name = "guest",
345 .attrs = visorchipset_guest_attrs
346};
347
19f6634f
BR
348static const struct attribute_group *visorchipset_dev_groups[] = {
349 &visorchipset_install_group,
18b87ed1 350 &visorchipset_guest_group,
19f6634f
BR
351 NULL
352};
353
12e364b9
KC
354/* /sys/devices/platform/visorchipset */
355static struct platform_device Visorchipset_platform_device = {
356 .name = "visorchipset",
357 .id = -1,
19f6634f 358 .dev.groups = visorchipset_dev_groups,
12e364b9
KC
359};
360
361/* Function prototypes */
362static void controlvm_respond(CONTROLVM_MESSAGE_HEADER *msgHdr, int response);
363static void controlvm_respond_chipset_init(CONTROLVM_MESSAGE_HEADER *msgHdr,
364 int response,
365 ULTRA_CHIPSET_FEATURE features);
366static void controlvm_respond_physdev_changestate(CONTROLVM_MESSAGE_HEADER *
367 msgHdr, int response,
368 ULTRA_SEGMENT_STATE state);
369
19f6634f
BR
370ssize_t toolaction_show(struct device *dev, struct device_attribute *attr,
371 char *buf)
372{
373 U8 toolAction;
374
375 visorchannel_read(ControlVm_channel,
376 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
377 ToolAction), &toolAction, sizeof(U8));
378 return scnprintf(buf, PAGE_SIZE, "%u\n", toolAction);
379}
380
381ssize_t toolaction_store(struct device *dev, struct device_attribute *attr,
382 const char *buf, size_t count)
383{
384 U8 toolAction;
385
386 if (sscanf(buf, "%hhu\n", &toolAction) == 1) {
387 if (visorchannel_write(ControlVm_channel,
388 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
389 ToolAction),
390 &toolAction, sizeof(U8)) < 0)
391 return -EFAULT;
392 else
393 return count;
394 } else
395 return -EIO;
396}
397
54b31229
BR
398ssize_t boottotool_show(struct device *dev, struct device_attribute *attr,
399 char *buf)
400{
401 ULTRA_EFI_SPAR_INDICATION efiSparIndication;
402
403 visorchannel_read(ControlVm_channel,
404 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
405 EfiSparIndication), &efiSparIndication,
406 sizeof(ULTRA_EFI_SPAR_INDICATION));
407 return scnprintf(buf, PAGE_SIZE, "%u\n",
408 efiSparIndication.BootToTool);
409}
410
411ssize_t boottotool_store(struct device *dev, struct device_attribute *attr,
412 const char *buf, size_t count)
413{
414 int val;
415 ULTRA_EFI_SPAR_INDICATION efiSparIndication;
416
417 if (sscanf(buf, "%u\n", &val) == 1) {
418 efiSparIndication.BootToTool = val;
419 if (visorchannel_write(ControlVm_channel,
420 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
421 EfiSparIndication),
422 &(efiSparIndication),
423 sizeof(ULTRA_EFI_SPAR_INDICATION)) < 0)
424 return -EFAULT;
425 else
426 return count;
427 } else
428 return -EIO;
429}
422af17c
BR
430
431static ssize_t error_show(struct device *dev, struct device_attribute *attr,
432 char *buf)
433{
434 u32 error;
435
436 visorchannel_read(ControlVm_channel, offsetof(
437 ULTRA_CONTROLVM_CHANNEL_PROTOCOL, InstallationError),
438 &error, sizeof(u32));
439 return scnprintf(buf, PAGE_SIZE, "%i\n", error);
440}
441
442static ssize_t error_store(struct device *dev, struct device_attribute *attr,
443 const char *buf, size_t count)
444{
445 u32 error;
446
447 if (sscanf(buf, "%i\n", &error) == 1) {
448 if (visorchannel_write(ControlVm_channel,
449 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
450 InstallationError),
451 &error, sizeof(u32)) == 1) {
452 return count;
453 }
454 }
455 return -EIO;
456}
457
458static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
459 char *buf)
460{
461 u32 textId;
462
463 visorchannel_read(ControlVm_channel, offsetof(
464 ULTRA_CONTROLVM_CHANNEL_PROTOCOL, InstallationTextId),
465 &textId, sizeof(u32));
466 return scnprintf(buf, PAGE_SIZE, "%i\n", textId);
467}
468
469static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
470 const char *buf, size_t count)
471{
472 u32 textId;
473
474 if (sscanf(buf, "%i\n", &textId) == 1) {
475 if (visorchannel_write(ControlVm_channel,
476 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
477 InstallationTextId),
478 &textId, sizeof(u32)) == 1) {
479 return count;
480 }
481 }
482 return -EIO;
483}
484
485
486static ssize_t remaining_steps_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488{
489 u16 remainingSteps;
490
491 visorchannel_read(ControlVm_channel,
492 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
493 InstallationRemainingSteps),
494 &remainingSteps,
495 sizeof(u16));
496 return scnprintf(buf, PAGE_SIZE, "%hu\n", remainingSteps);
497}
498
499static ssize_t remaining_steps_store(struct device *dev,
500 struct device_attribute *attr, const char *buf, size_t count)
501{
502 u16 remainingSteps;
503
504 if (sscanf(buf, "%hu\n", &remainingSteps) == 1) {
505 if (visorchannel_write(ControlVm_channel,
506 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
507 InstallationRemainingSteps),
508 &remainingSteps, sizeof(u16)) == 1) {
509 return count;
510 }
511 }
512 return -EIO;
513}
514
12e364b9
KC
515static void
516show_partition_property(struct seq_file *f, void *ctx, int property)
517{
518 VISORCHIPSET_BUS_INFO *info = (VISORCHIPSET_BUS_INFO *) (ctx);
519
520 switch (property) {
521 case PARTPROP_name:
522 seq_printf(f, "%s\n", NONULLSTR(info->name));
523 break;
524 case PARTPROP_description:
525 seq_printf(f, "%s\n", NONULLSTR(info->description));
526 break;
527 case PARTPROP_handle:
528 seq_printf(f, "0x%-16.16Lx\n", info->partitionHandle);
529 break;
530 case PARTPROP_busNumber:
531 seq_printf(f, "%d\n", info->busNo);
532 break;
533 default:
534 seq_printf(f, "(%d??)\n", property);
535 break;
536 }
537}
538
12e364b9
KC
539static void
540proc_Init(void)
541{
542 if (ProcDir == NULL) {
543 ProcDir = proc_mkdir(MYDRVNAME, NULL);
544 if (ProcDir == NULL) {
545 LOGERR("failed to create /proc directory %s",
546 MYDRVNAME);
547 POSTCODE_LINUX_2(CHIPSET_INIT_FAILURE_PC,
548 POSTCODE_SEVERITY_ERR);
549 }
550 }
551}
552
553static void
554proc_DeInit(void)
555{
556 if (ProcDir != NULL)
557 remove_proc_entry(MYDRVNAME, NULL);
558 ProcDir = NULL;
559}
560
561#if 0
562static void
563testUnicode(void)
564{
565 wchar_t unicodeString[] = { 'a', 'b', 'c', 0 };
566 char s[sizeof(unicodeString) * NLS_MAX_CHARSET_SIZE];
567 wchar_t unicode2[99];
568
569 /* NOTE: Either due to a bug, or feature I don't understand, the
570 * kernel utf8_mbstowcs() and utf_wcstombs() do NOT copy the
571 * trailed NUL byte!! REALLY!!!!! Arrrrgggghhhhh
572 */
573
574 LOGINF("sizeof(wchar_t) = %d", sizeof(wchar_t));
575 LOGINF("utf8_wcstombs=%d",
576 chrs = utf8_wcstombs(s, unicodeString, sizeof(s)));
577 if (chrs >= 0)
578 s[chrs] = '\0'; /* GRRRRRRRR */
579 LOGINF("s='%s'", s);
580 LOGINF("utf8_mbstowcs=%d", chrs = utf8_mbstowcs(unicode2, s, 100));
581 if (chrs >= 0)
582 unicode2[chrs] = 0; /* GRRRRRRRR */
583 if (memcmp(unicodeString, unicode2, sizeof(unicodeString)) == 0)
584 LOGINF("strings match... good");
585 else
586 LOGINF("strings did not match!!");
587}
588#endif
589
590static void
591busInfo_clear(void *v)
592{
593 VISORCHIPSET_BUS_INFO *p = (VISORCHIPSET_BUS_INFO *) (v);
594
595 if (p->procObject) {
927c7927 596 visor_proc_DestroyObject(p->procObject);
12e364b9
KC
597 p->procObject = NULL;
598 }
599 kfree(p->name);
600 p->name = NULL;
601
602 kfree(p->description);
603 p->description = NULL;
604
605 p->state.created = 0;
606 memset(p, 0, sizeof(VISORCHIPSET_BUS_INFO));
607}
608
609static void
610devInfo_clear(void *v)
611{
612 VISORCHIPSET_DEVICE_INFO *p = (VISORCHIPSET_DEVICE_INFO *) (v);
613 p->state.created = 0;
614 memset(p, 0, sizeof(VISORCHIPSET_DEVICE_INFO));
615}
616
617static U8
618check_chipset_events(void)
619{
620 int i;
621 U8 send_msg = 1;
622 /* Check events to determine if response should be sent */
623 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
624 send_msg &= chipset_events[i];
625 return send_msg;
626}
627
628static void
629clear_chipset_events(void)
630{
631 int i;
632 /* Clear chipset_events */
633 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
634 chipset_events[i] = 0;
635}
636
637void
638visorchipset_register_busdev_server(VISORCHIPSET_BUSDEV_NOTIFIERS *notifiers,
639 VISORCHIPSET_BUSDEV_RESPONDERS *responders,
640 ULTRA_VBUS_DEVICEINFO *driverInfo)
641{
642 LOCKSEM_UNINTERRUPTIBLE(&NotifierLock);
643 if (notifiers == NULL) {
644 memset(&BusDev_Server_Notifiers, 0,
645 sizeof(BusDev_Server_Notifiers));
646 serverregistered = 0; /* clear flag */
647 } else {
648 BusDev_Server_Notifiers = *notifiers;
649 serverregistered = 1; /* set flag */
650 }
651 if (responders)
652 *responders = BusDev_Responders;
653 if (driverInfo)
654 BusDeviceInfo_Init(driverInfo, "chipset", "visorchipset",
836bee9e 655 VERSION, NULL);
12e364b9
KC
656
657 UNLOCKSEM(&NotifierLock);
658}
659EXPORT_SYMBOL_GPL(visorchipset_register_busdev_server);
660
661void
662visorchipset_register_busdev_client(VISORCHIPSET_BUSDEV_NOTIFIERS *notifiers,
663 VISORCHIPSET_BUSDEV_RESPONDERS *responders,
664 ULTRA_VBUS_DEVICEINFO *driverInfo)
665{
666 LOCKSEM_UNINTERRUPTIBLE(&NotifierLock);
667 if (notifiers == NULL) {
668 memset(&BusDev_Client_Notifiers, 0,
669 sizeof(BusDev_Client_Notifiers));
670 clientregistered = 0; /* clear flag */
671 } else {
672 BusDev_Client_Notifiers = *notifiers;
673 clientregistered = 1; /* set flag */
674 }
675 if (responders)
676 *responders = BusDev_Responders;
677 if (driverInfo)
678 BusDeviceInfo_Init(driverInfo, "chipset(bolts)", "visorchipset",
836bee9e 679 VERSION, NULL);
12e364b9
KC
680 UNLOCKSEM(&NotifierLock);
681}
682EXPORT_SYMBOL_GPL(visorchipset_register_busdev_client);
683
684static void
685cleanup_controlvm_structures(void)
686{
e6b1ea77
DC
687 VISORCHIPSET_BUS_INFO *bi, *tmp_bi;
688 VISORCHIPSET_DEVICE_INFO *di, *tmp_di;
12e364b9 689
e6b1ea77 690 list_for_each_entry_safe(bi, tmp_bi, &BusInfoList, entry) {
12e364b9
KC
691 busInfo_clear(bi);
692 list_del(&bi->entry);
693 kfree(bi);
694 }
695
e6b1ea77 696 list_for_each_entry_safe(di, tmp_di, &DevInfoList, entry) {
12e364b9
KC
697 devInfo_clear(di);
698 list_del(&di->entry);
699 kfree(di);
700 }
701}
702
703static void
704chipset_init(CONTROLVM_MESSAGE *inmsg)
705{
706 static int chipset_inited;
707 ULTRA_CHIPSET_FEATURE features = 0;
708 int rc = CONTROLVM_RESP_SUCCESS;
709
710 POSTCODE_LINUX_2(CHIPSET_INIT_ENTRY_PC, POSTCODE_SEVERITY_INFO);
711 if (chipset_inited) {
712 LOGERR("CONTROLVM_CHIPSET_INIT Failed: Already Done.");
22ad57ba
KC
713 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
714 goto Away;
12e364b9
KC
715 }
716 chipset_inited = 1;
717 POSTCODE_LINUX_2(CHIPSET_INIT_EXIT_PC, POSTCODE_SEVERITY_INFO);
718
719 /* Set features to indicate we support parahotplug (if Command
720 * also supports it). */
721 features =
722 inmsg->cmd.initChipset.
723 features & ULTRA_CHIPSET_FEATURE_PARA_HOTPLUG;
724
725 /* Set the "reply" bit so Command knows this is a
726 * features-aware driver. */
727 features |= ULTRA_CHIPSET_FEATURE_REPLY;
728
729Away:
730 if (rc < 0)
731 cleanup_controlvm_structures();
732 if (inmsg->hdr.Flags.responseExpected)
733 controlvm_respond_chipset_init(&inmsg->hdr, rc, features);
734}
735
736static void
737controlvm_init_response(CONTROLVM_MESSAGE *msg,
738 CONTROLVM_MESSAGE_HEADER *msgHdr, int response)
739{
740 memset(msg, 0, sizeof(CONTROLVM_MESSAGE));
741 memcpy(&msg->hdr, msgHdr, sizeof(CONTROLVM_MESSAGE_HEADER));
742 msg->hdr.PayloadBytes = 0;
743 msg->hdr.PayloadVmOffset = 0;
744 msg->hdr.PayloadMaxBytes = 0;
745 if (response < 0) {
746 msg->hdr.Flags.failed = 1;
747 msg->hdr.CompletionStatus = (U32) (-response);
748 }
749}
750
751static void
752controlvm_respond(CONTROLVM_MESSAGE_HEADER *msgHdr, int response)
753{
754 CONTROLVM_MESSAGE outmsg;
12e364b9
KC
755 controlvm_init_response(&outmsg, msgHdr, response);
756 /* For DiagPool channel DEVICE_CHANGESTATE, we need to send
757 * back the deviceChangeState structure in the packet. */
758 if (msgHdr->Id == CONTROLVM_DEVICE_CHANGESTATE
759 && g_DeviceChangeStatePacket.deviceChangeState.busNo ==
760 g_diagpoolBusNo
761 && g_DeviceChangeStatePacket.deviceChangeState.devNo ==
762 g_diagpoolDevNo)
763 outmsg.cmd = g_DeviceChangeStatePacket;
764 if (outmsg.hdr.Flags.testMessage == 1) {
765 LOGINF("%s controlvm_msg=0x%x response=%d for test message",
766 __func__, outmsg.hdr.Id, response);
767 return;
768 }
769 if (!visorchannel_signalinsert(ControlVm_channel,
770 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
771 LOGERR("signalinsert failed!");
772 return;
773 }
774}
775
776static void
777controlvm_respond_chipset_init(CONTROLVM_MESSAGE_HEADER *msgHdr, int response,
778 ULTRA_CHIPSET_FEATURE features)
779{
780 CONTROLVM_MESSAGE outmsg;
12e364b9
KC
781 controlvm_init_response(&outmsg, msgHdr, response);
782 outmsg.cmd.initChipset.features = features;
783 if (!visorchannel_signalinsert(ControlVm_channel,
784 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
785 LOGERR("signalinsert failed!");
786 return;
787 }
788}
789
790static void
791controlvm_respond_physdev_changestate(CONTROLVM_MESSAGE_HEADER *msgHdr,
792 int response, ULTRA_SEGMENT_STATE state)
793{
794 CONTROLVM_MESSAGE outmsg;
12e364b9
KC
795 controlvm_init_response(&outmsg, msgHdr, response);
796 outmsg.cmd.deviceChangeState.state = state;
797 outmsg.cmd.deviceChangeState.flags.physicalDevice = 1;
798 if (!visorchannel_signalinsert(ControlVm_channel,
799 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
800 LOGERR("signalinsert failed!");
801 return;
802 }
803}
804
805void
806visorchipset_save_message(CONTROLVM_MESSAGE *msg, CRASH_OBJ_TYPE type)
807{
808 U32 localSavedCrashMsgOffset;
809 U16 localSavedCrashMsgCount;
810
811 /* get saved message count */
812 if (visorchannel_read(ControlVm_channel,
813 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
814 SavedCrashMsgCount),
815 &localSavedCrashMsgCount, sizeof(U16)) < 0) {
816 LOGERR("failed to get Saved Message Count");
817 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
818 POSTCODE_SEVERITY_ERR);
819 return;
820 }
821
822 if (localSavedCrashMsgCount != CONTROLVM_CRASHMSG_MAX) {
823 LOGERR("Saved Message Count incorrect %d",
824 localSavedCrashMsgCount);
825 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
826 localSavedCrashMsgCount,
827 POSTCODE_SEVERITY_ERR);
828 return;
829 }
830
831 /* get saved crash message offset */
832 if (visorchannel_read(ControlVm_channel,
833 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
834 SavedCrashMsgOffset),
835 &localSavedCrashMsgOffset, sizeof(U32)) < 0) {
836 LOGERR("failed to get Saved Message Offset");
837 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
838 POSTCODE_SEVERITY_ERR);
839 return;
840 }
841
842 if (type == CRASH_bus) {
843 if (visorchannel_write(ControlVm_channel,
844 localSavedCrashMsgOffset,
845 msg, sizeof(CONTROLVM_MESSAGE)) < 0) {
846 LOGERR("SAVE_MSG_BUS_FAILURE: Failed to write CrashCreateBusMsg!");
847 POSTCODE_LINUX_2(SAVE_MSG_BUS_FAILURE_PC,
848 POSTCODE_SEVERITY_ERR);
849 return;
850 }
851 } else {
852 if (visorchannel_write(ControlVm_channel,
853 localSavedCrashMsgOffset +
854 sizeof(CONTROLVM_MESSAGE), msg,
855 sizeof(CONTROLVM_MESSAGE)) < 0) {
856 LOGERR("SAVE_MSG_DEV_FAILURE: Failed to write CrashCreateDevMsg!");
857 POSTCODE_LINUX_2(SAVE_MSG_DEV_FAILURE_PC,
858 POSTCODE_SEVERITY_ERR);
859 return;
860 }
861 }
862}
863EXPORT_SYMBOL_GPL(visorchipset_save_message);
864
865static void
866bus_responder(CONTROLVM_ID cmdId, ulong busNo, int response)
867{
868 VISORCHIPSET_BUS_INFO *p = NULL;
869 BOOL need_clear = FALSE;
870
871 p = findbus(&BusInfoList, busNo);
872 if (!p) {
873 LOGERR("internal error busNo=%lu", busNo);
874 return;
875 }
876 if (response < 0) {
877 if ((cmdId == CONTROLVM_BUS_CREATE) &&
878 (response != (-CONTROLVM_RESP_ERROR_ALREADY_DONE)))
879 /* undo the row we just created... */
880 delbusdevices(&DevInfoList, busNo);
881 } else {
882 if (cmdId == CONTROLVM_BUS_CREATE)
883 p->state.created = 1;
884 if (cmdId == CONTROLVM_BUS_DESTROY)
885 need_clear = TRUE;
886 }
887
888 if (p->pendingMsgHdr.Id == CONTROLVM_INVALID) {
889 LOGERR("bus_responder no pending msg");
890 return; /* no controlvm response needed */
891 }
892 if (p->pendingMsgHdr.Id != (U32) cmdId) {
893 LOGERR("expected=%d, found=%d", cmdId, p->pendingMsgHdr.Id);
894 return;
895 }
896 controlvm_respond(&p->pendingMsgHdr, response);
897 p->pendingMsgHdr.Id = CONTROLVM_INVALID;
898 if (need_clear) {
899 busInfo_clear(p);
900 delbusdevices(&DevInfoList, busNo);
901 }
902}
903
904static void
905device_changestate_responder(CONTROLVM_ID cmdId,
906 ulong busNo, ulong devNo, int response,
907 ULTRA_SEGMENT_STATE responseState)
908{
909 VISORCHIPSET_DEVICE_INFO *p = NULL;
910 CONTROLVM_MESSAGE outmsg;
911
12e364b9
KC
912 p = finddevice(&DevInfoList, busNo, devNo);
913 if (!p) {
914 LOGERR("internal error; busNo=%lu, devNo=%lu", busNo, devNo);
915 return;
916 }
917 if (p->pendingMsgHdr.Id == CONTROLVM_INVALID) {
918 LOGERR("device_responder no pending msg");
919 return; /* no controlvm response needed */
920 }
921 if (p->pendingMsgHdr.Id != cmdId) {
922 LOGERR("expected=%d, found=%d", cmdId, p->pendingMsgHdr.Id);
923 return;
924 }
925
926 controlvm_init_response(&outmsg, &p->pendingMsgHdr, response);
927
928 outmsg.cmd.deviceChangeState.busNo = busNo;
929 outmsg.cmd.deviceChangeState.devNo = devNo;
930 outmsg.cmd.deviceChangeState.state = responseState;
931
932 if (!visorchannel_signalinsert(ControlVm_channel,
933 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
934 LOGERR("signalinsert failed!");
935 return;
936 }
937
938 p->pendingMsgHdr.Id = CONTROLVM_INVALID;
939}
940
941static void
942device_responder(CONTROLVM_ID cmdId, ulong busNo, ulong devNo, int response)
943{
944 VISORCHIPSET_DEVICE_INFO *p = NULL;
945 BOOL need_clear = FALSE;
946
947 p = finddevice(&DevInfoList, busNo, devNo);
948 if (!p) {
949 LOGERR("internal error; busNo=%lu, devNo=%lu", busNo, devNo);
950 return;
951 }
952 if (response >= 0) {
953 if (cmdId == CONTROLVM_DEVICE_CREATE)
954 p->state.created = 1;
955 if (cmdId == CONTROLVM_DEVICE_DESTROY)
956 need_clear = TRUE;
957 }
958
959 if (p->pendingMsgHdr.Id == CONTROLVM_INVALID) {
960 LOGERR("device_responder no pending msg");
961 return; /* no controlvm response needed */
962 }
963 if (p->pendingMsgHdr.Id != (U32) cmdId) {
964 LOGERR("expected=%d, found=%d", cmdId, p->pendingMsgHdr.Id);
965 return;
966 }
967 controlvm_respond(&p->pendingMsgHdr, response);
968 p->pendingMsgHdr.Id = CONTROLVM_INVALID;
969 if (need_clear)
970 devInfo_clear(p);
971}
972
973static void
974bus_epilog(U32 busNo,
975 U32 cmd, CONTROLVM_MESSAGE_HEADER *msgHdr,
976 int response, BOOL needResponse)
977{
978 BOOL notified = FALSE;
979
980 VISORCHIPSET_BUS_INFO *pBusInfo = findbus(&BusInfoList, busNo);
981
982 if (!pBusInfo) {
983 LOGERR("HUH? bad busNo=%d", busNo);
984 return;
985 }
986 if (needResponse) {
987 memcpy(&pBusInfo->pendingMsgHdr, msgHdr,
988 sizeof(CONTROLVM_MESSAGE_HEADER));
989 } else
990 pBusInfo->pendingMsgHdr.Id = CONTROLVM_INVALID;
991
992 LOCKSEM_UNINTERRUPTIBLE(&NotifierLock);
993 if (response == CONTROLVM_RESP_SUCCESS) {
994 switch (cmd) {
995 case CONTROLVM_BUS_CREATE:
996 /* We can't tell from the bus_create
997 * information which of our 2 bus flavors the
998 * devices on this bus will ultimately end up.
999 * FORTUNATELY, it turns out it is harmless to
1000 * send the bus_create to both of them. We can
1001 * narrow things down a little bit, though,
1002 * because we know: - BusDev_Server can handle
1003 * either server or client devices
1004 * - BusDev_Client can handle ONLY client
1005 * devices */
1006 if (BusDev_Server_Notifiers.bus_create) {
1007 (*BusDev_Server_Notifiers.bus_create) (busNo);
1008 notified = TRUE;
1009 }
1010 if ((!pBusInfo->flags.server) /*client */ &&
1011 BusDev_Client_Notifiers.bus_create) {
1012 (*BusDev_Client_Notifiers.bus_create) (busNo);
1013 notified = TRUE;
1014 }
1015 break;
1016 case CONTROLVM_BUS_DESTROY:
1017 if (BusDev_Server_Notifiers.bus_destroy) {
1018 (*BusDev_Server_Notifiers.bus_destroy) (busNo);
1019 notified = TRUE;
1020 }
1021 if ((!pBusInfo->flags.server) /*client */ &&
1022 BusDev_Client_Notifiers.bus_destroy) {
1023 (*BusDev_Client_Notifiers.bus_destroy) (busNo);
1024 notified = TRUE;
1025 }
1026 break;
1027 }
1028 }
1029 if (notified)
1030 /* The callback function just called above is responsible
1031 * for calling the appropriate VISORCHIPSET_BUSDEV_RESPONDERS
1032 * function, which will call bus_responder()
1033 */
1034 ;
1035 else
1036 bus_responder(cmd, busNo, response);
1037 UNLOCKSEM(&NotifierLock);
1038}
1039
1040static void
1041device_epilog(U32 busNo, U32 devNo, ULTRA_SEGMENT_STATE state, U32 cmd,
1042 CONTROLVM_MESSAGE_HEADER *msgHdr, int response,
1043 BOOL needResponse, BOOL for_visorbus)
1044{
1045 VISORCHIPSET_BUSDEV_NOTIFIERS *notifiers = NULL;
1046 BOOL notified = FALSE;
1047
1048 VISORCHIPSET_DEVICE_INFO *pDevInfo =
1049 finddevice(&DevInfoList, busNo, devNo);
1050 char *envp[] = {
1051 "SPARSP_DIAGPOOL_PAUSED_STATE = 1",
1052 NULL
1053 };
1054
1055 if (!pDevInfo) {
1056 LOGERR("HUH? bad busNo=%d, devNo=%d", busNo, devNo);
1057 return;
1058 }
1059 if (for_visorbus)
1060 notifiers = &BusDev_Server_Notifiers;
1061 else
1062 notifiers = &BusDev_Client_Notifiers;
1063 if (needResponse) {
1064 memcpy(&pDevInfo->pendingMsgHdr, msgHdr,
1065 sizeof(CONTROLVM_MESSAGE_HEADER));
1066 } else
1067 pDevInfo->pendingMsgHdr.Id = CONTROLVM_INVALID;
1068
1069 LOCKSEM_UNINTERRUPTIBLE(&NotifierLock);
1070 if (response >= 0) {
1071 switch (cmd) {
1072 case CONTROLVM_DEVICE_CREATE:
1073 if (notifiers->device_create) {
1074 (*notifiers->device_create) (busNo, devNo);
1075 notified = TRUE;
1076 }
1077 break;
1078 case CONTROLVM_DEVICE_CHANGESTATE:
1079 /* ServerReady / ServerRunning / SegmentStateRunning */
1080 if (state.Alive == SegmentStateRunning.Alive &&
1081 state.Operating == SegmentStateRunning.Operating) {
1082 if (notifiers->device_resume) {
1083 (*notifiers->device_resume) (busNo,
1084 devNo);
1085 notified = TRUE;
1086 }
1087 }
1088 /* ServerNotReady / ServerLost / SegmentStateStandby */
1089 else if (state.Alive == SegmentStateStandby.Alive &&
1090 state.Operating ==
1091 SegmentStateStandby.Operating) {
1092 /* technically this is standby case
1093 * where server is lost
1094 */
1095 if (notifiers->device_pause) {
1096 (*notifiers->device_pause) (busNo,
1097 devNo);
1098 notified = TRUE;
1099 }
1100 } else if (state.Alive == SegmentStatePaused.Alive &&
1101 state.Operating ==
1102 SegmentStatePaused.Operating) {
1103 /* this is lite pause where channel is
1104 * still valid just 'pause' of it
1105 */
1106 if (busNo == g_diagpoolBusNo
1107 && devNo == g_diagpoolDevNo) {
1108 LOGINF("DEVICE_CHANGESTATE(DiagpoolChannel busNo=%d devNo=%d is pausing...)",
1109 busNo, devNo);
1110 /* this will trigger the
1111 * diag_shutdown.sh script in
1112 * the visorchipset hotplug */
1113 kobject_uevent_env
1114 (&Visorchipset_platform_device.dev.
1115 kobj, KOBJ_ONLINE, envp);
1116 }
1117 }
1118 break;
1119 case CONTROLVM_DEVICE_DESTROY:
1120 if (notifiers->device_destroy) {
1121 (*notifiers->device_destroy) (busNo, devNo);
1122 notified = TRUE;
1123 }
1124 break;
1125 }
1126 }
1127 if (notified)
1128 /* The callback function just called above is responsible
1129 * for calling the appropriate VISORCHIPSET_BUSDEV_RESPONDERS
1130 * function, which will call device_responder()
1131 */
1132 ;
1133 else
1134 device_responder(cmd, busNo, devNo, response);
1135 UNLOCKSEM(&NotifierLock);
1136}
1137
1138static void
1139bus_create(CONTROLVM_MESSAGE *inmsg)
1140{
1141 CONTROLVM_MESSAGE_PACKET *cmd = &inmsg->cmd;
1142 ulong busNo = cmd->createBus.busNo;
1143 int rc = CONTROLVM_RESP_SUCCESS;
1144 VISORCHIPSET_BUS_INFO *pBusInfo = NULL;
1145
1146
1147 pBusInfo = findbus(&BusInfoList, busNo);
1148 if (pBusInfo && (pBusInfo->state.created == 1)) {
1149 LOGERR("CONTROLVM_BUS_CREATE Failed: bus %lu already exists",
1150 busNo);
1151 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, busNo,
1152 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1153 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
1154 goto Away;
12e364b9 1155 }
97a84f12 1156 pBusInfo = kzalloc(sizeof(VISORCHIPSET_BUS_INFO), GFP_KERNEL);
12e364b9 1157 if (pBusInfo == NULL) {
97a84f12 1158 LOGERR("CONTROLVM_BUS_CREATE Failed: bus %lu kzalloc failed",
12e364b9
KC
1159 busNo);
1160 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, busNo,
1161 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1162 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
1163 goto Away;
12e364b9
KC
1164 }
1165
12e364b9
KC
1166 INIT_LIST_HEAD(&pBusInfo->entry);
1167 pBusInfo->busNo = busNo;
1168 pBusInfo->devNo = cmd->createBus.deviceCount;
1169
1170 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, busNo, POSTCODE_SEVERITY_INFO);
1171
1172 if (inmsg->hdr.Flags.testMessage == 1)
1173 pBusInfo->chanInfo.addrType = ADDRTYPE_localTest;
1174 else
1175 pBusInfo->chanInfo.addrType = ADDRTYPE_localPhysical;
1176
1177 pBusInfo->flags.server = inmsg->hdr.Flags.server;
1178 pBusInfo->chanInfo.channelAddr = cmd->createBus.channelAddr;
1179 pBusInfo->chanInfo.nChannelBytes = cmd->createBus.channelBytes;
1180 pBusInfo->chanInfo.channelTypeGuid = cmd->createBus.busDataTypeGuid;
1181 pBusInfo->chanInfo.channelInstGuid = cmd->createBus.busInstGuid;
1182
1183 list_add(&pBusInfo->entry, &BusInfoList);
1184
1185 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, busNo, POSTCODE_SEVERITY_INFO);
1186
1187Away:
1188 bus_epilog(busNo, CONTROLVM_BUS_CREATE, &inmsg->hdr,
1189 rc, inmsg->hdr.Flags.responseExpected == 1);
1190}
1191
1192static void
1193bus_destroy(CONTROLVM_MESSAGE *inmsg)
1194{
1195 CONTROLVM_MESSAGE_PACKET *cmd = &inmsg->cmd;
1196 ulong busNo = cmd->destroyBus.busNo;
1197 VISORCHIPSET_BUS_INFO *pBusInfo;
1198 int rc = CONTROLVM_RESP_SUCCESS;
1199
1200 pBusInfo = findbus(&BusInfoList, busNo);
1201 if (!pBusInfo) {
1202 LOGERR("CONTROLVM_BUS_DESTROY Failed: bus %lu invalid", busNo);
22ad57ba
KC
1203 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
1204 goto Away;
12e364b9
KC
1205 }
1206 if (pBusInfo->state.created == 0) {
1207 LOGERR("CONTROLVM_BUS_DESTROY Failed: bus %lu already destroyed",
1208 busNo);
22ad57ba
KC
1209 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
1210 goto Away;
12e364b9
KC
1211 }
1212
1213Away:
1214 bus_epilog(busNo, CONTROLVM_BUS_DESTROY, &inmsg->hdr,
1215 rc, inmsg->hdr.Flags.responseExpected == 1);
1216}
1217
1218static void
1219bus_configure(CONTROLVM_MESSAGE *inmsg, PARSER_CONTEXT *parser_ctx)
1220{
1221 CONTROLVM_MESSAGE_PACKET *cmd = &inmsg->cmd;
1222 ulong busNo = cmd->configureBus.busNo;
1223 VISORCHIPSET_BUS_INFO *pBusInfo = NULL;
1224 int rc = CONTROLVM_RESP_SUCCESS;
1225 char s[99];
1226
1227 busNo = cmd->configureBus.busNo;
1228 POSTCODE_LINUX_3(BUS_CONFIGURE_ENTRY_PC, busNo, POSTCODE_SEVERITY_INFO);
1229
1230 pBusInfo = findbus(&BusInfoList, busNo);
1231 if (!pBusInfo) {
1232 LOGERR("CONTROLVM_BUS_CONFIGURE Failed: bus %lu invalid",
1233 busNo);
1234 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, busNo,
1235 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1236 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
1237 goto Away;
12e364b9
KC
1238 }
1239 if (pBusInfo->state.created == 0) {
1240 LOGERR("CONTROLVM_BUS_CONFIGURE Failed: Invalid bus %lu - not created yet",
1241 busNo);
1242 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, busNo,
1243 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1244 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
1245 goto Away;
12e364b9
KC
1246 }
1247 /* TBD - add this check to other commands also... */
1248 if (pBusInfo->pendingMsgHdr.Id != CONTROLVM_INVALID) {
1249 LOGERR("CONTROLVM_BUS_CONFIGURE Failed: bus %lu MsgId=%u outstanding",
1250 busNo, (uint) pBusInfo->pendingMsgHdr.Id);
1251 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, busNo,
1252 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1253 rc = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
1254 goto Away;
12e364b9
KC
1255 }
1256
1257 pBusInfo->partitionHandle = cmd->configureBus.guestHandle;
1258 pBusInfo->partitionGuid = parser_id_get(parser_ctx);
1259 parser_param_start(parser_ctx, PARSERSTRING_NAME);
1260 pBusInfo->name = parser_string_get(parser_ctx);
1261
90addb02 1262 visorchannel_uuid_id(&pBusInfo->partitionGuid, s);
12e364b9 1263 pBusInfo->procObject =
927c7927 1264 visor_proc_CreateObject(PartitionType, s, (void *) (pBusInfo));
12e364b9
KC
1265 if (pBusInfo->procObject == NULL) {
1266 LOGERR("CONTROLVM_BUS_CONFIGURE Failed: busNo=%lu failed to create /proc entry",
1267 busNo);
1268 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, busNo,
1269 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1270 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
1271 goto Away;
12e364b9
KC
1272 }
1273 POSTCODE_LINUX_3(BUS_CONFIGURE_EXIT_PC, busNo, POSTCODE_SEVERITY_INFO);
1274Away:
1275 bus_epilog(busNo, CONTROLVM_BUS_CONFIGURE, &inmsg->hdr,
1276 rc, inmsg->hdr.Flags.responseExpected == 1);
1277}
1278
1279static void
1280my_device_create(CONTROLVM_MESSAGE *inmsg)
1281{
1282 CONTROLVM_MESSAGE_PACKET *cmd = &inmsg->cmd;
1283 ulong busNo = cmd->createDevice.busNo;
1284 ulong devNo = cmd->createDevice.devNo;
1285 VISORCHIPSET_DEVICE_INFO *pDevInfo = NULL;
1286 VISORCHIPSET_BUS_INFO *pBusInfo = NULL;
1287 int rc = CONTROLVM_RESP_SUCCESS;
1288
1289 pDevInfo = finddevice(&DevInfoList, busNo, devNo);
1290 if (pDevInfo && (pDevInfo->state.created == 1)) {
1291 LOGERR("CONTROLVM_DEVICE_CREATE Failed: busNo=%lu, devNo=%lu already exists",
1292 busNo, devNo);
1293 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, devNo, busNo,
1294 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1295 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
1296 goto Away;
12e364b9
KC
1297 }
1298 pBusInfo = findbus(&BusInfoList, busNo);
1299 if (!pBusInfo) {
1300 LOGERR("CONTROLVM_DEVICE_CREATE Failed: Invalid bus %lu - out of range",
1301 busNo);
1302 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, devNo, busNo,
1303 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1304 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
1305 goto Away;
12e364b9
KC
1306 }
1307 if (pBusInfo->state.created == 0) {
1308 LOGERR("CONTROLVM_DEVICE_CREATE Failed: Invalid bus %lu - not created yet",
1309 busNo);
1310 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, devNo, busNo,
1311 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1312 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
1313 goto Away;
12e364b9 1314 }
97a84f12 1315 pDevInfo = kzalloc(sizeof(VISORCHIPSET_DEVICE_INFO), GFP_KERNEL);
12e364b9
KC
1316 if (pDevInfo == NULL) {
1317 LOGERR("CONTROLVM_DEVICE_CREATE Failed: busNo=%lu, devNo=%lu kmaloc failed",
1318 busNo, devNo);
1319 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, devNo, busNo,
1320 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1321 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
1322 goto Away;
12e364b9 1323 }
97a84f12 1324
12e364b9
KC
1325 INIT_LIST_HEAD(&pDevInfo->entry);
1326 pDevInfo->busNo = busNo;
1327 pDevInfo->devNo = devNo;
1328 pDevInfo->devInstGuid = cmd->createDevice.devInstGuid;
1329 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, devNo, busNo,
1330 POSTCODE_SEVERITY_INFO);
1331
1332 if (inmsg->hdr.Flags.testMessage == 1)
1333 pDevInfo->chanInfo.addrType = ADDRTYPE_localTest;
1334 else
1335 pDevInfo->chanInfo.addrType = ADDRTYPE_localPhysical;
1336 pDevInfo->chanInfo.channelAddr = cmd->createDevice.channelAddr;
1337 pDevInfo->chanInfo.nChannelBytes = cmd->createDevice.channelBytes;
1338 pDevInfo->chanInfo.channelTypeGuid = cmd->createDevice.dataTypeGuid;
1339 pDevInfo->chanInfo.intr = cmd->createDevice.intr;
1340 list_add(&pDevInfo->entry, &DevInfoList);
1341 POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, devNo, busNo,
1342 POSTCODE_SEVERITY_INFO);
1343Away:
1344 /* get the bus and devNo for DiagPool channel */
1345 if (is_diagpool_channel(pDevInfo->chanInfo.channelTypeGuid)) {
1346 g_diagpoolBusNo = busNo;
1347 g_diagpoolDevNo = devNo;
1348 LOGINF("CONTROLVM_DEVICE_CREATE for DiagPool channel: busNo=%lu, devNo=%lu",
1349 g_diagpoolBusNo, g_diagpoolDevNo);
1350 }
1351 device_epilog(busNo, devNo, SegmentStateRunning,
1352 CONTROLVM_DEVICE_CREATE, &inmsg->hdr, rc,
1353 inmsg->hdr.Flags.responseExpected == 1,
1354 FOR_VISORBUS(pDevInfo->chanInfo.channelTypeGuid));
1355}
1356
1357static void
1358my_device_changestate(CONTROLVM_MESSAGE *inmsg)
1359{
1360 CONTROLVM_MESSAGE_PACKET *cmd = &inmsg->cmd;
1361 ulong busNo = cmd->deviceChangeState.busNo;
1362 ulong devNo = cmd->deviceChangeState.devNo;
1363 ULTRA_SEGMENT_STATE state = cmd->deviceChangeState.state;
1364 VISORCHIPSET_DEVICE_INFO *pDevInfo = NULL;
1365 int rc = CONTROLVM_RESP_SUCCESS;
1366
1367 pDevInfo = finddevice(&DevInfoList, busNo, devNo);
1368 if (!pDevInfo) {
1369 LOGERR("CONTROLVM_DEVICE_CHANGESTATE Failed: busNo=%lu, devNo=%lu invalid (doesn't exist)",
1370 busNo, devNo);
1371 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, devNo, busNo,
1372 POSTCODE_SEVERITY_ERR);
22ad57ba
KC
1373 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
1374 goto Away;
12e364b9
KC
1375 }
1376 if (pDevInfo->state.created == 0) {
1377 LOGERR("CONTROLVM_DEVICE_CHANGESTATE Failed: busNo=%lu, devNo=%lu invalid (not created)",
1378 busNo, devNo);
1379 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, devNo, busNo,
1380 POSTCODE_SEVERITY_ERR);
22ad57ba 1381 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
12e364b9
KC
1382 }
1383Away:
1384 if ((rc >= CONTROLVM_RESP_SUCCESS) && pDevInfo)
1385 device_epilog(busNo, devNo, state, CONTROLVM_DEVICE_CHANGESTATE,
1386 &inmsg->hdr, rc,
1387 inmsg->hdr.Flags.responseExpected == 1,
1388 FOR_VISORBUS(pDevInfo->chanInfo.channelTypeGuid));
1389}
1390
1391static void
1392my_device_destroy(CONTROLVM_MESSAGE *inmsg)
1393{
1394 CONTROLVM_MESSAGE_PACKET *cmd = &inmsg->cmd;
1395 ulong busNo = cmd->destroyDevice.busNo;
1396 ulong devNo = cmd->destroyDevice.devNo;
1397 VISORCHIPSET_DEVICE_INFO *pDevInfo = NULL;
1398 int rc = CONTROLVM_RESP_SUCCESS;
1399
1400 pDevInfo = finddevice(&DevInfoList, busNo, devNo);
1401 if (!pDevInfo) {
1402 LOGERR("CONTROLVM_DEVICE_DESTROY Failed: busNo=%lu, devNo=%lu invalid",
1403 busNo, devNo);
22ad57ba
KC
1404 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
1405 goto Away;
12e364b9
KC
1406 }
1407 if (pDevInfo->state.created == 0) {
1408 LOGERR("CONTROLVM_DEVICE_DESTROY Failed: busNo=%lu, devNo=%lu already destroyed",
1409 busNo, devNo);
22ad57ba 1410 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
12e364b9
KC
1411 }
1412
1413Away:
1414 if ((rc >= CONTROLVM_RESP_SUCCESS) && pDevInfo)
1415 device_epilog(busNo, devNo, SegmentStateRunning,
1416 CONTROLVM_DEVICE_DESTROY, &inmsg->hdr, rc,
1417 inmsg->hdr.Flags.responseExpected == 1,
1418 FOR_VISORBUS(pDevInfo->chanInfo.channelTypeGuid));
1419}
1420
1421/* When provided with the physical address of the controlvm channel
1422 * (phys_addr), the offset to the payload area we need to manage
1423 * (offset), and the size of this payload area (bytes), fills in the
1424 * CONTROLVM_PAYLOAD_INFO struct. Returns TRUE for success or FALSE
1425 * for failure.
1426 */
1427static int
1428initialize_controlvm_payload_info(HOSTADDRESS phys_addr, U64 offset, U32 bytes,
1429 CONTROLVM_PAYLOAD_INFO *info)
1430{
bd5b9b32 1431 U8 __iomem *payload = NULL;
12e364b9
KC
1432 int rc = CONTROLVM_RESP_SUCCESS;
1433
1434 if (info == NULL) {
1435 LOGERR("HUH ? CONTROLVM_PAYLOAD_INIT Failed : Programmer check at %s:%d",
1436 __FILE__, __LINE__);
22ad57ba
KC
1437 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
1438 goto Away;
12e364b9
KC
1439 }
1440 memset(info, 0, sizeof(CONTROLVM_PAYLOAD_INFO));
1441 if ((offset == 0) || (bytes == 0)) {
1442 LOGERR("CONTROLVM_PAYLOAD_INIT Failed: RequestPayloadOffset=%llu RequestPayloadBytes=%llu!",
1443 (u64) offset, (u64) bytes);
22ad57ba
KC
1444 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
1445 goto Away;
12e364b9
KC
1446 }
1447 payload = ioremap_cache(phys_addr + offset, bytes);
1448 if (payload == NULL) {
1449 LOGERR("CONTROLVM_PAYLOAD_INIT Failed: ioremap_cache %llu for %llu bytes failed",
1450 (u64) offset, (u64) bytes);
22ad57ba
KC
1451 rc = -CONTROLVM_RESP_ERROR_IOREMAP_FAILED;
1452 goto Away;
12e364b9
KC
1453 }
1454
1455 info->offset = offset;
1456 info->bytes = bytes;
1457 info->ptr = payload;
1458 LOGINF("offset=%llu, bytes=%lu, ptr=%p",
1459 (u64) (info->offset), (ulong) (info->bytes), info->ptr);
1460
1461Away:
1462 if (rc < 0) {
1463 if (payload != NULL) {
1464 iounmap(payload);
1465 payload = NULL;
1466 }
1467 }
1468 return rc;
1469}
1470
1471static void
1472destroy_controlvm_payload_info(CONTROLVM_PAYLOAD_INFO *info)
1473{
1474 if (info->ptr != NULL) {
1475 iounmap(info->ptr);
1476 info->ptr = NULL;
1477 }
1478 memset(info, 0, sizeof(CONTROLVM_PAYLOAD_INFO));
1479}
1480
1481static void
1482initialize_controlvm_payload(void)
1483{
1484 HOSTADDRESS phys_addr = visorchannel_get_physaddr(ControlVm_channel);
1485 U64 payloadOffset = 0;
1486 U32 payloadBytes = 0;
1487 if (visorchannel_read(ControlVm_channel,
1488 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
1489 RequestPayloadOffset),
1490 &payloadOffset, sizeof(payloadOffset)) < 0) {
1491 LOGERR("CONTROLVM_PAYLOAD_INIT Failed to read controlvm channel!");
1492 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1493 POSTCODE_SEVERITY_ERR);
1494 return;
1495 }
1496 if (visorchannel_read(ControlVm_channel,
1497 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
1498 RequestPayloadBytes),
1499 &payloadBytes, sizeof(payloadBytes)) < 0) {
1500 LOGERR("CONTROLVM_PAYLOAD_INIT Failed to read controlvm channel!");
1501 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1502 POSTCODE_SEVERITY_ERR);
1503 return;
1504 }
1505 initialize_controlvm_payload_info(phys_addr,
1506 payloadOffset, payloadBytes,
1507 &ControlVm_payload_info);
1508}
1509
1510/* Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
1511 * Returns CONTROLVM_RESP_xxx code.
1512 */
1513int
1514visorchipset_chipset_ready(void)
1515{
1516 kobject_uevent(&Visorchipset_platform_device.dev.kobj, KOBJ_ONLINE);
1517 return CONTROLVM_RESP_SUCCESS;
1518}
1519EXPORT_SYMBOL_GPL(visorchipset_chipset_ready);
1520
1521int
1522visorchipset_chipset_selftest(void)
1523{
1524 char env_selftest[20];
1525 char *envp[] = { env_selftest, NULL };
1526 sprintf(env_selftest, "SPARSP_SELFTEST=%d", 1);
1527 kobject_uevent_env(&Visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
1528 envp);
1529 return CONTROLVM_RESP_SUCCESS;
1530}
1531EXPORT_SYMBOL_GPL(visorchipset_chipset_selftest);
1532
1533/* Send ACTION=offline for DEVPATH=/sys/devices/platform/visorchipset.
1534 * Returns CONTROLVM_RESP_xxx code.
1535 */
1536int
1537visorchipset_chipset_notready(void)
1538{
1539 kobject_uevent(&Visorchipset_platform_device.dev.kobj, KOBJ_OFFLINE);
1540 return CONTROLVM_RESP_SUCCESS;
1541}
1542EXPORT_SYMBOL_GPL(visorchipset_chipset_notready);
1543
1544static void
1545chipset_ready(CONTROLVM_MESSAGE_HEADER *msgHdr)
1546{
1547 int rc = visorchipset_chipset_ready();
1548 if (rc != CONTROLVM_RESP_SUCCESS)
1549 rc = -rc;
1550 if (msgHdr->Flags.responseExpected && !visorchipset_holdchipsetready)
1551 controlvm_respond(msgHdr, rc);
1552 if (msgHdr->Flags.responseExpected && visorchipset_holdchipsetready) {
1553 /* Send CHIPSET_READY response when all modules have been loaded
1554 * and disks mounted for the partition
1555 */
1556 g_ChipSetMsgHdr = *msgHdr;
1557 LOGINF("Holding CHIPSET_READY response");
1558 }
1559}
1560
1561static void
1562chipset_selftest(CONTROLVM_MESSAGE_HEADER *msgHdr)
1563{
1564 int rc = visorchipset_chipset_selftest();
1565 if (rc != CONTROLVM_RESP_SUCCESS)
1566 rc = -rc;
1567 if (msgHdr->Flags.responseExpected)
1568 controlvm_respond(msgHdr, rc);
1569}
1570
1571static void
1572chipset_notready(CONTROLVM_MESSAGE_HEADER *msgHdr)
1573{
1574 int rc = visorchipset_chipset_notready();
1575 if (rc != CONTROLVM_RESP_SUCCESS)
1576 rc = -rc;
1577 if (msgHdr->Flags.responseExpected)
1578 controlvm_respond(msgHdr, rc);
1579}
1580
1581/* This is your "one-stop" shop for grabbing the next message from the
1582 * CONTROLVM_QUEUE_EVENT queue in the controlvm channel.
1583 */
1584static BOOL
1585read_controlvm_event(CONTROLVM_MESSAGE *msg)
1586{
1587 if (visorchannel_signalremove(ControlVm_channel,
1588 CONTROLVM_QUEUE_EVENT, msg)) {
1589 /* got a message */
1590 if (msg->hdr.Flags.testMessage == 1) {
1591 LOGERR("ignoring bad CONTROLVM_QUEUE_EVENT msg with controlvm_msg_id=0x%x because Flags.testMessage is nonsensical (=1)", msg->hdr.Id);
1592 return FALSE;
1593 } else
1594 return TRUE;
1595 }
1596 return FALSE;
1597}
1598
1599/*
1600 * The general parahotplug flow works as follows. The visorchipset
1601 * driver receives a DEVICE_CHANGESTATE message from Command
1602 * specifying a physical device to enable or disable. The CONTROLVM
1603 * message handler calls parahotplug_process_message, which then adds
1604 * the message to a global list and kicks off a udev event which
1605 * causes a user level script to enable or disable the specified
1606 * device. The udev script then writes to
1607 * /proc/visorchipset/parahotplug, which causes parahotplug_proc_write
1608 * to get called, at which point the appropriate CONTROLVM message is
1609 * retrieved from the list and responded to.
1610 */
1611
1612#define PARAHOTPLUG_TIMEOUT_MS 2000
1613
1614/*
1615 * Generate unique int to match an outstanding CONTROLVM message with a
1616 * udev script /proc response
1617 */
1618static int
1619parahotplug_next_id(void)
1620{
1621 static atomic_t id = ATOMIC_INIT(0);
1622 return atomic_inc_return(&id);
1623}
1624
1625/*
1626 * Returns the time (in jiffies) when a CONTROLVM message on the list
1627 * should expire -- PARAHOTPLUG_TIMEOUT_MS in the future
1628 */
1629static unsigned long
1630parahotplug_next_expiration(void)
1631{
1632 return jiffies + PARAHOTPLUG_TIMEOUT_MS * HZ / 1000;
1633}
1634
1635/*
1636 * Create a parahotplug_request, which is basically a wrapper for a
1637 * CONTROLVM_MESSAGE that we can stick on a list
1638 */
1639static struct parahotplug_request *
1640parahotplug_request_create(CONTROLVM_MESSAGE *msg)
1641{
1642 struct parahotplug_request *req =
1643 kmalloc(sizeof(struct parahotplug_request),
1644 GFP_KERNEL|__GFP_NORETRY);
1645 if (req == NULL)
1646 return NULL;
1647
1648 req->id = parahotplug_next_id();
1649 req->expiration = parahotplug_next_expiration();
1650 req->msg = *msg;
1651
1652 return req;
1653}
1654
1655/*
1656 * Free a parahotplug_request.
1657 */
1658static void
1659parahotplug_request_destroy(struct parahotplug_request *req)
1660{
1661 kfree(req);
1662}
1663
1664/*
1665 * Cause uevent to run the user level script to do the disable/enable
1666 * specified in (the CONTROLVM message in) the specified
1667 * parahotplug_request
1668 */
1669static void
1670parahotplug_request_kickoff(struct parahotplug_request *req)
1671{
1672 CONTROLVM_MESSAGE_PACKET *cmd = &req->msg.cmd;
1673 char env_cmd[40], env_id[40], env_state[40], env_bus[40], env_dev[40],
1674 env_func[40];
1675 char *envp[] = {
1676 env_cmd, env_id, env_state, env_bus, env_dev, env_func, NULL
1677 };
1678
1679 sprintf(env_cmd, "SPAR_PARAHOTPLUG=1");
1680 sprintf(env_id, "SPAR_PARAHOTPLUG_ID=%d", req->id);
1681 sprintf(env_state, "SPAR_PARAHOTPLUG_STATE=%d",
1682 cmd->deviceChangeState.state.Active);
1683 sprintf(env_bus, "SPAR_PARAHOTPLUG_BUS=%d",
1684 cmd->deviceChangeState.busNo);
1685 sprintf(env_dev, "SPAR_PARAHOTPLUG_DEVICE=%d",
1686 cmd->deviceChangeState.devNo >> 3);
1687 sprintf(env_func, "SPAR_PARAHOTPLUG_FUNCTION=%d",
1688 cmd->deviceChangeState.devNo & 0x7);
1689
1690 LOGINF("parahotplug_request_kickoff: state=%d, bdf=%d/%d/%d, id=%u\n",
1691 cmd->deviceChangeState.state.Active,
1692 cmd->deviceChangeState.busNo, cmd->deviceChangeState.devNo >> 3,
1693 cmd->deviceChangeState.devNo & 7, req->id);
1694
1695 kobject_uevent_env(&Visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
1696 envp);
1697}
1698
1699/*
1700 * Remove any request from the list that's been on there too long and
1701 * respond with an error.
1702 */
1703static void
1704parahotplug_process_list(void)
1705{
1706 struct list_head *pos = NULL;
1707 struct list_head *tmp = NULL;
1708
1709 spin_lock(&Parahotplug_request_list_lock);
1710
1711 list_for_each_safe(pos, tmp, &Parahotplug_request_list) {
1712 struct parahotplug_request *req =
1713 list_entry(pos, struct parahotplug_request, list);
1714 if (time_after_eq(jiffies, req->expiration)) {
1715 list_del(pos);
1716 if (req->msg.hdr.Flags.responseExpected)
1717 controlvm_respond_physdev_changestate(
1718 &req->msg.hdr,
1719 CONTROLVM_RESP_ERROR_DEVICE_UDEV_TIMEOUT,
1720 req->msg.cmd.deviceChangeState.state);
1721 parahotplug_request_destroy(req);
1722 }
1723 }
1724
1725 spin_unlock(&Parahotplug_request_list_lock);
1726}
1727
1728/*
1729 * Called from the /proc handler, which means the user script has
1730 * finished the enable/disable. Find the matching identifier, and
1731 * respond to the CONTROLVM message with success.
1732 */
1733static int
1734parahotplug_request_complete(int id, U16 active)
1735{
1736 struct list_head *pos = NULL;
1737 struct list_head *tmp = NULL;
1738
1739 spin_lock(&Parahotplug_request_list_lock);
1740
1741 /* Look for a request matching "id". */
1742 list_for_each_safe(pos, tmp, &Parahotplug_request_list) {
1743 struct parahotplug_request *req =
1744 list_entry(pos, struct parahotplug_request, list);
1745 if (req->id == id) {
1746 /* Found a match. Remove it from the list and
1747 * respond.
1748 */
1749 list_del(pos);
1750 spin_unlock(&Parahotplug_request_list_lock);
1751 req->msg.cmd.deviceChangeState.state.Active = active;
1752 if (req->msg.hdr.Flags.responseExpected)
1753 controlvm_respond_physdev_changestate(
1754 &req->msg.hdr, CONTROLVM_RESP_SUCCESS,
1755 req->msg.cmd.deviceChangeState.state);
1756 parahotplug_request_destroy(req);
1757 return 0;
1758 }
1759 }
1760
1761 spin_unlock(&Parahotplug_request_list_lock);
1762 return -1;
1763}
1764
1765/*
1766 * Enables or disables a PCI device by kicking off a udev script
1767 */
bd5b9b32 1768static void
12e364b9
KC
1769parahotplug_process_message(CONTROLVM_MESSAGE *inmsg)
1770{
1771 struct parahotplug_request *req;
1772
1773 req = parahotplug_request_create(inmsg);
1774
1775 if (req == NULL) {
1776 LOGERR("parahotplug_process_message: couldn't allocate request");
1777 return;
1778 }
1779
1780 if (inmsg->cmd.deviceChangeState.state.Active) {
1781 /* For enable messages, just respond with success
1782 * right away. This is a bit of a hack, but there are
1783 * issues with the early enable messages we get (with
1784 * either the udev script not detecting that the device
1785 * is up, or not getting called at all). Fortunately
1786 * the messages that get lost don't matter anyway, as
1787 * devices are automatically enabled at
1788 * initialization.
1789 */
1790 parahotplug_request_kickoff(req);
1791 controlvm_respond_physdev_changestate(&inmsg->hdr,
1792 CONTROLVM_RESP_SUCCESS,
1793 inmsg->cmd.
1794 deviceChangeState.state);
1795 parahotplug_request_destroy(req);
1796 } else {
1797 /* For disable messages, add the request to the
1798 * request list before kicking off the udev script. It
1799 * won't get responded to until the script has
1800 * indicated it's done.
1801 */
1802 spin_lock(&Parahotplug_request_list_lock);
1803 list_add_tail(&(req->list), &Parahotplug_request_list);
1804 spin_unlock(&Parahotplug_request_list_lock);
1805
1806 parahotplug_request_kickoff(req);
1807 }
1808}
1809
1810/*
1811 * Gets called when the udev script writes to
1812 * /proc/visorchipset/parahotplug. Expects input in the form of "<id>
1813 * <active>" where <id> is the identifier passed to the script that
1814 * matches a request on the request list, and <active> is 0 or 1
1815 * indicating whether the device is now enabled or not.
1816 */
1817static ssize_t
1818parahotplug_proc_write(struct file *file, const char __user *buffer,
1819 size_t count, loff_t *ppos)
1820{
1821 char buf[64];
1822 uint id;
1823 ushort active;
1824
1825 if (count > sizeof(buf) - 1) {
1826 LOGERR("parahotplug_proc_write: count (%d) exceeds size of buffer (%d)",
1827 (int) count, (int) sizeof(buf));
1828 return -EINVAL;
1829 }
1830 if (copy_from_user(buf, buffer, count)) {
1831 LOGERR("parahotplug_proc_write: copy_from_user failed");
1832 return -EFAULT;
1833 }
1834 buf[count] = '\0';
1835
1836 if (sscanf(buf, "%u %hu", &id, &active) != 2) {
1837 id = 0;
1838 active = 0;
1839 }
1840
1841 if (active != 1 && active != 0) {
1842 LOGERR("parahotplug_proc_write: invalid active field");
1843 return -EINVAL;
1844 }
1845
1846 parahotplug_request_complete((int) id, (U16) active);
1847
1848 return count;
1849}
1850
1851static const struct file_operations parahotplug_proc_fops = {
1852 .owner = THIS_MODULE,
1853 .read = visorchipset_proc_read_writeonly,
1854 .write = parahotplug_proc_write,
1855};
1856
1857/* Process a controlvm message.
1858 * Return result:
1859 * FALSE - this function will return FALSE only in the case where the
1860 * controlvm message was NOT processed, but processing must be
1861 * retried before reading the next controlvm message; a
1862 * scenario where this can occur is when we need to throttle
1863 * the allocation of memory in which to copy out controlvm
1864 * payload data
1865 * TRUE - processing of the controlvm message completed,
1866 * either successfully or with an error.
1867 */
1868static BOOL
1869handle_command(CONTROLVM_MESSAGE inmsg, HOSTADDRESS channel_addr)
1870{
1871 CONTROLVM_MESSAGE_PACKET *cmd = &inmsg.cmd;
1872 U64 parametersAddr = 0;
1873 U32 parametersBytes = 0;
1874 PARSER_CONTEXT *parser_ctx = NULL;
1875 BOOL isLocalAddr = FALSE;
1876 CONTROLVM_MESSAGE ackmsg;
1877
1878 /* create parsing context if necessary */
1879 isLocalAddr = (inmsg.hdr.Flags.testMessage == 1);
1880 if (channel_addr == 0) {
1881 LOGERR("HUH? channel_addr is 0!");
1882 return TRUE;
1883 }
1884 parametersAddr = channel_addr + inmsg.hdr.PayloadVmOffset;
1885 parametersBytes = inmsg.hdr.PayloadBytes;
1886
1887 /* Parameter and channel addresses within test messages actually lie
1888 * within our OS-controlled memory. We need to know that, because it
1889 * makes a difference in how we compute the virtual address.
1890 */
1891 if (parametersAddr != 0 && parametersBytes != 0) {
1892 BOOL retry = FALSE;
1893 parser_ctx =
1894 parser_init_byteStream(parametersAddr, parametersBytes,
1895 isLocalAddr, &retry);
1896 if (!parser_ctx) {
1897 if (retry) {
1898 LOGWRN("throttling to copy payload");
1899 return FALSE;
1900 }
1901 LOGWRN("parsing failed");
1902 LOGWRN("inmsg.hdr.Id=0x%lx", (ulong) inmsg.hdr.Id);
1903 LOGWRN("parametersAddr=0x%llx", (u64) parametersAddr);
1904 LOGWRN("parametersBytes=%lu", (ulong) parametersBytes);
1905 LOGWRN("isLocalAddr=%d", isLocalAddr);
1906 }
1907 }
1908
1909 if (!isLocalAddr) {
1910 controlvm_init_response(&ackmsg, &inmsg.hdr,
1911 CONTROLVM_RESP_SUCCESS);
1912 if ((ControlVm_channel)
1913 &&
1914 (!visorchannel_signalinsert
1915 (ControlVm_channel, CONTROLVM_QUEUE_ACK, &ackmsg)))
1916 LOGWRN("failed to send ACK failed");
1917 }
1918 switch (inmsg.hdr.Id) {
1919 case CONTROLVM_CHIPSET_INIT:
1920 LOGINF("CHIPSET_INIT(#busses=%lu,#switches=%lu)",
1921 (ulong) inmsg.cmd.initChipset.busCount,
1922 (ulong) inmsg.cmd.initChipset.switchCount);
1923 chipset_init(&inmsg);
1924 break;
1925 case CONTROLVM_BUS_CREATE:
1926 LOGINF("BUS_CREATE(%lu,#devs=%lu)",
1927 (ulong) cmd->createBus.busNo,
1928 (ulong) cmd->createBus.deviceCount);
1929 bus_create(&inmsg);
1930 break;
1931 case CONTROLVM_BUS_DESTROY:
1932 LOGINF("BUS_DESTROY(%lu)", (ulong) cmd->destroyBus.busNo);
1933 bus_destroy(&inmsg);
1934 break;
1935 case CONTROLVM_BUS_CONFIGURE:
1936 LOGINF("BUS_CONFIGURE(%lu)", (ulong) cmd->configureBus.busNo);
1937 bus_configure(&inmsg, parser_ctx);
1938 break;
1939 case CONTROLVM_DEVICE_CREATE:
1940 LOGINF("DEVICE_CREATE(%lu,%lu)",
1941 (ulong) cmd->createDevice.busNo,
1942 (ulong) cmd->createDevice.devNo);
1943 my_device_create(&inmsg);
1944 break;
1945 case CONTROLVM_DEVICE_CHANGESTATE:
1946 if (cmd->deviceChangeState.flags.physicalDevice) {
1947 LOGINF("DEVICE_CHANGESTATE for physical device (%lu,%lu, active=%lu)",
1948 (ulong) cmd->deviceChangeState.busNo,
1949 (ulong) cmd->deviceChangeState.devNo,
1950 (ulong) cmd->deviceChangeState.state.Active);
1951 parahotplug_process_message(&inmsg);
1952 } else {
1953 LOGINF("DEVICE_CHANGESTATE for virtual device (%lu,%lu, state.Alive=0x%lx)",
1954 (ulong) cmd->deviceChangeState.busNo,
1955 (ulong) cmd->deviceChangeState.devNo,
1956 (ulong) cmd->deviceChangeState.state.Alive);
1957 /* save the hdr and cmd structures for later use */
1958 /* when sending back the response to Command */
1959 my_device_changestate(&inmsg);
1960 g_DiagMsgHdr = inmsg.hdr;
1961 g_DeviceChangeStatePacket = inmsg.cmd;
1962 break;
1963 }
1964 break;
1965 case CONTROLVM_DEVICE_DESTROY:
1966 LOGINF("DEVICE_DESTROY(%lu,%lu)",
1967 (ulong) cmd->destroyDevice.busNo,
1968 (ulong) cmd->destroyDevice.devNo);
1969 my_device_destroy(&inmsg);
1970 break;
1971 case CONTROLVM_DEVICE_CONFIGURE:
1972 LOGINF("DEVICE_CONFIGURE(%lu,%lu)",
1973 (ulong) cmd->configureDevice.busNo,
1974 (ulong) cmd->configureDevice.devNo);
1975 /* no op for now, just send a respond that we passed */
1976 if (inmsg.hdr.Flags.responseExpected)
1977 controlvm_respond(&inmsg.hdr, CONTROLVM_RESP_SUCCESS);
1978 break;
1979 case CONTROLVM_CHIPSET_READY:
1980 LOGINF("CHIPSET_READY");
1981 chipset_ready(&inmsg.hdr);
1982 break;
1983 case CONTROLVM_CHIPSET_SELFTEST:
1984 LOGINF("CHIPSET_SELFTEST");
1985 chipset_selftest(&inmsg.hdr);
1986 break;
1987 case CONTROLVM_CHIPSET_STOP:
1988 LOGINF("CHIPSET_STOP");
1989 chipset_notready(&inmsg.hdr);
1990 break;
1991 default:
1992 LOGERR("unrecognized controlvm cmd=%d", (int) inmsg.hdr.Id);
1993 if (inmsg.hdr.Flags.responseExpected)
1994 controlvm_respond(&inmsg.hdr,
1995 -CONTROLVM_RESP_ERROR_MESSAGE_ID_UNKNOWN);
1996 break;
1997 }
1998
1999 if (parser_ctx != NULL) {
2000 parser_done(parser_ctx);
2001 parser_ctx = NULL;
2002 }
2003 return TRUE;
2004}
2005
524b0b63
BR
2006HOSTADDRESS controlvm_get_channel_address(void)
2007{
2008 U64 addr = 0;
2009 U32 size = 0;
2010
2011 if (!VMCALL_SUCCESSFUL(Issue_VMCALL_IO_CONTROLVM_ADDR(&addr, &size))) {
2012 ERRDRV("%s - vmcall to determine controlvm channel addr failed",
2013 __func__);
2014 return 0;
2015 }
2016 INFODRV("controlvm addr=%Lx", addr);
2017 return addr;
2018}
2019
12e364b9
KC
2020static void
2021controlvm_periodic_work(struct work_struct *work)
2022{
2023 VISORCHIPSET_CHANNEL_INFO chanInfo;
2024 CONTROLVM_MESSAGE inmsg;
12e364b9
KC
2025 BOOL gotACommand = FALSE;
2026 BOOL handle_command_failed = FALSE;
2027 static U64 Poll_Count;
2028
2029 /* make sure visorbus server is registered for controlvm callbacks */
2030 if (visorchipset_serverregwait && !serverregistered)
097f4c19 2031 goto Away;
12e364b9
KC
2032 /* make sure visorclientbus server is regsitered for controlvm
2033 * callbacks
2034 */
2035 if (visorchipset_clientregwait && !clientregistered)
097f4c19 2036 goto Away;
12e364b9
KC
2037
2038 memset(&chanInfo, 0, sizeof(VISORCHIPSET_CHANNEL_INFO));
12e364b9
KC
2039
2040 Poll_Count++;
8a1182eb 2041 if (Poll_Count >= 250)
12e364b9
KC
2042 ; /* keep going */
2043 else
097f4c19 2044 goto Away;
12e364b9
KC
2045
2046 /* Check events to determine if response to CHIPSET_READY
2047 * should be sent
2048 */
2049 if (visorchipset_holdchipsetready
2050 && (g_ChipSetMsgHdr.Id != CONTROLVM_INVALID)) {
2051 if (check_chipset_events() == 1) {
2052 LOGINF("Sending CHIPSET_READY response");
2053 controlvm_respond(&g_ChipSetMsgHdr, 0);
2054 clear_chipset_events();
2055 memset(&g_ChipSetMsgHdr, 0,
2056 sizeof(CONTROLVM_MESSAGE_HEADER));
2057 }
2058 }
2059
8a1182eb
BR
2060 while (visorchannel_signalremove(ControlVm_channel,
2061 CONTROLVM_QUEUE_RESPONSE,
2062 &inmsg)) {
2063 if (inmsg.hdr.PayloadMaxBytes != 0) {
2064 LOGERR("Payload of size %lu returned @%lu with unexpected message id %d.",
2065 (ulong) inmsg.hdr.PayloadMaxBytes,
2066 (ulong) inmsg.hdr.PayloadVmOffset,
2067 inmsg.hdr.Id);
12e364b9
KC
2068 }
2069 }
8a1182eb
BR
2070 if (!gotACommand) {
2071 if (ControlVm_Pending_Msg_Valid) {
2072 /* we throttled processing of a prior
2073 * msg, so try to process it again
2074 * rather than reading a new one
2075 */
2076 inmsg = ControlVm_Pending_Msg;
2077 ControlVm_Pending_Msg_Valid = FALSE;
2078 gotACommand = TRUE;
2079 } else
2080 gotACommand = read_controlvm_event(&inmsg);
2081 }
12e364b9
KC
2082
2083 handle_command_failed = FALSE;
2084 while (gotACommand && (!handle_command_failed)) {
2085 Most_recent_message_jiffies = jiffies;
8a1182eb
BR
2086 if (handle_command(inmsg,
2087 visorchannel_get_physaddr
2088 (ControlVm_channel)))
2089 gotACommand = read_controlvm_event(&inmsg);
2090 else {
2091 /* this is a scenario where throttling
2092 * is required, but probably NOT an
2093 * error...; we stash the current
2094 * controlvm msg so we will attempt to
2095 * reprocess it on our next loop
2096 */
2097 handle_command_failed = TRUE;
2098 ControlVm_Pending_Msg = inmsg;
2099 ControlVm_Pending_Msg_Valid = TRUE;
12e364b9
KC
2100 }
2101 }
2102
2103 /* parahotplug_worker */
2104 parahotplug_process_list();
2105
12e364b9
KC
2106Away:
2107
2108 if (time_after(jiffies,
2109 Most_recent_message_jiffies + (HZ * MIN_IDLE_SECONDS))) {
2110 /* it's been longer than MIN_IDLE_SECONDS since we
2111 * processed our last controlvm message; slow down the
2112 * polling
2113 */
2114 if (Poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_SLOW) {
2115 LOGINF("switched to slow controlvm polling");
2116 Poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
2117 }
2118 } else {
2119 if (Poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_FAST) {
2120 Poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
2121 LOGINF("switched to fast controlvm polling");
2122 }
2123 }
2124
4b4b535e
DY
2125 queue_delayed_work(Periodic_controlvm_workqueue,
2126 &Periodic_controlvm_work, Poll_jiffies);
12e364b9
KC
2127}
2128
2129static void
2130setup_crash_devices_work_queue(struct work_struct *work)
2131{
2132
2133 CONTROLVM_MESSAGE localCrashCreateBusMsg;
2134 CONTROLVM_MESSAGE localCrashCreateDevMsg;
2135 CONTROLVM_MESSAGE msg;
12e364b9
KC
2136 U32 localSavedCrashMsgOffset;
2137 U16 localSavedCrashMsgCount;
2138
2139 /* make sure visorbus server is registered for controlvm callbacks */
2140 if (visorchipset_serverregwait && !serverregistered)
097f4c19 2141 goto Away;
12e364b9
KC
2142
2143 /* make sure visorclientbus server is regsitered for controlvm
2144 * callbacks
2145 */
2146 if (visorchipset_clientregwait && !clientregistered)
097f4c19 2147 goto Away;
12e364b9
KC
2148
2149 POSTCODE_LINUX_2(CRASH_DEV_ENTRY_PC, POSTCODE_SEVERITY_INFO);
2150
2151 /* send init chipset msg */
2152 msg.hdr.Id = CONTROLVM_CHIPSET_INIT;
2153 msg.cmd.initChipset.busCount = 23;
2154 msg.cmd.initChipset.switchCount = 0;
2155
2156 chipset_init(&msg);
2157
12e364b9
KC
2158 /* get saved message count */
2159 if (visorchannel_read(ControlVm_channel,
2160 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
2161 SavedCrashMsgCount),
2162 &localSavedCrashMsgCount, sizeof(U16)) < 0) {
2163 LOGERR("failed to get Saved Message Count");
2164 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2165 POSTCODE_SEVERITY_ERR);
2166 return;
2167 }
2168
2169 if (localSavedCrashMsgCount != CONTROLVM_CRASHMSG_MAX) {
2170 LOGERR("Saved Message Count incorrect %d",
2171 localSavedCrashMsgCount);
2172 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
2173 localSavedCrashMsgCount,
2174 POSTCODE_SEVERITY_ERR);
2175 return;
2176 }
2177
2178 /* get saved crash message offset */
2179 if (visorchannel_read(ControlVm_channel,
2180 offsetof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL,
2181 SavedCrashMsgOffset),
2182 &localSavedCrashMsgOffset, sizeof(U32)) < 0) {
2183 LOGERR("failed to get Saved Message Offset");
2184 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2185 POSTCODE_SEVERITY_ERR);
2186 return;
2187 }
2188
2189 /* read create device message for storage bus offset */
2190 if (visorchannel_read(ControlVm_channel,
2191 localSavedCrashMsgOffset,
2192 &localCrashCreateBusMsg,
2193 sizeof(CONTROLVM_MESSAGE)) < 0) {
2194 LOGERR("CRASH_DEV_RD_BUS_FAIULRE: Failed to read CrashCreateBusMsg!");
2195 POSTCODE_LINUX_2(CRASH_DEV_RD_BUS_FAIULRE_PC,
2196 POSTCODE_SEVERITY_ERR);
2197 return;
2198 }
2199
2200 /* read create device message for storage device */
2201 if (visorchannel_read(ControlVm_channel,
2202 localSavedCrashMsgOffset +
2203 sizeof(CONTROLVM_MESSAGE),
2204 &localCrashCreateDevMsg,
2205 sizeof(CONTROLVM_MESSAGE)) < 0) {
2206 LOGERR("CRASH_DEV_RD_DEV_FAIULRE: Failed to read CrashCreateDevMsg!");
2207 POSTCODE_LINUX_2(CRASH_DEV_RD_DEV_FAIULRE_PC,
2208 POSTCODE_SEVERITY_ERR);
2209 return;
2210 }
2211
2212 /* reuse IOVM create bus message */
2213 if (localCrashCreateBusMsg.cmd.createBus.channelAddr != 0)
2214 bus_create(&localCrashCreateBusMsg);
2215 else {
2216 LOGERR("CrashCreateBusMsg is null, no dump will be taken");
2217 POSTCODE_LINUX_2(CRASH_DEV_BUS_NULL_FAILURE_PC,
2218 POSTCODE_SEVERITY_ERR);
2219 return;
2220 }
2221
2222 /* reuse create device message for storage device */
2223 if (localCrashCreateDevMsg.cmd.createDevice.channelAddr != 0)
2224 my_device_create(&localCrashCreateDevMsg);
2225 else {
2226 LOGERR("CrashCreateDevMsg is null, no dump will be taken");
2227 POSTCODE_LINUX_2(CRASH_DEV_DEV_NULL_FAILURE_PC,
2228 POSTCODE_SEVERITY_ERR);
2229 return;
2230 }
2231 LOGINF("Bus and device ready for dumping");
2232 POSTCODE_LINUX_2(CRASH_DEV_EXIT_PC, POSTCODE_SEVERITY_INFO);
2233 return;
2234
2235Away:
2236
2237 Poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
2238
4b4b535e
DY
2239 queue_delayed_work(Periodic_controlvm_workqueue,
2240 &Periodic_controlvm_work, Poll_jiffies);
12e364b9
KC
2241}
2242
2243static void
2244bus_create_response(ulong busNo, int response)
2245{
2246 bus_responder(CONTROLVM_BUS_CREATE, busNo, response);
2247}
2248
2249static void
2250bus_destroy_response(ulong busNo, int response)
2251{
2252 bus_responder(CONTROLVM_BUS_DESTROY, busNo, response);
2253}
2254
2255static void
2256device_create_response(ulong busNo, ulong devNo, int response)
2257{
2258 device_responder(CONTROLVM_DEVICE_CREATE, busNo, devNo, response);
2259}
2260
2261static void
2262device_destroy_response(ulong busNo, ulong devNo, int response)
2263{
2264 device_responder(CONTROLVM_DEVICE_DESTROY, busNo, devNo, response);
2265}
2266
2267void
927c7927 2268visorchipset_device_pause_response(ulong busNo, ulong devNo, int response)
12e364b9
KC
2269{
2270
2271 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
2272 busNo, devNo, response,
2273 SegmentStateStandby);
2274}
927c7927 2275EXPORT_SYMBOL_GPL(visorchipset_device_pause_response);
12e364b9
KC
2276
2277static void
2278device_resume_response(ulong busNo, ulong devNo, int response)
2279{
2280 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
2281 busNo, devNo, response,
2282 SegmentStateRunning);
2283}
2284
2285BOOL
2286visorchipset_get_bus_info(ulong busNo, VISORCHIPSET_BUS_INFO *busInfo)
2287{
2288 void *p = findbus(&BusInfoList, busNo);
2289 if (!p) {
2290 LOGERR("(%lu) failed", busNo);
2291 return FALSE;
2292 }
2293 memcpy(busInfo, p, sizeof(VISORCHIPSET_BUS_INFO));
2294 return TRUE;
2295}
2296EXPORT_SYMBOL_GPL(visorchipset_get_bus_info);
2297
2298BOOL
2299visorchipset_set_bus_context(ulong busNo, void *context)
2300{
2301 VISORCHIPSET_BUS_INFO *p = findbus(&BusInfoList, busNo);
2302 if (!p) {
2303 LOGERR("(%lu) failed", busNo);
2304 return FALSE;
2305 }
2306 p->bus_driver_context = context;
2307 return TRUE;
2308}
2309EXPORT_SYMBOL_GPL(visorchipset_set_bus_context);
2310
2311BOOL
2312visorchipset_get_device_info(ulong busNo, ulong devNo,
2313 VISORCHIPSET_DEVICE_INFO *devInfo)
2314{
2315 void *p = finddevice(&DevInfoList, busNo, devNo);
2316 if (!p) {
2317 LOGERR("(%lu,%lu) failed", busNo, devNo);
2318 return FALSE;
2319 }
2320 memcpy(devInfo, p, sizeof(VISORCHIPSET_DEVICE_INFO));
2321 return TRUE;
2322}
2323EXPORT_SYMBOL_GPL(visorchipset_get_device_info);
2324
2325BOOL
2326visorchipset_set_device_context(ulong busNo, ulong devNo, void *context)
2327{
2328 VISORCHIPSET_DEVICE_INFO *p = finddevice(&DevInfoList, busNo, devNo);
2329 if (!p) {
2330 LOGERR("(%lu,%lu) failed", busNo, devNo);
2331 return FALSE;
2332 }
2333 p->bus_driver_context = context;
2334 return TRUE;
2335}
2336EXPORT_SYMBOL_GPL(visorchipset_set_device_context);
2337
2338/* Generic wrapper function for allocating memory from a kmem_cache pool.
2339 */
2340void *
2341visorchipset_cache_alloc(struct kmem_cache *pool, BOOL ok_to_block,
2342 char *fn, int ln)
2343{
2344 gfp_t gfp;
2345 void *p;
2346
2347 if (ok_to_block)
2348 gfp = GFP_KERNEL;
2349 else
2350 gfp = GFP_ATOMIC;
2351 /* __GFP_NORETRY means "ok to fail", meaning
2352 * kmem_cache_alloc() can return NULL, implying the caller CAN
2353 * cope with failure. If you do NOT specify __GFP_NORETRY,
2354 * Linux will go to extreme measures to get memory for you
2355 * (like, invoke oom killer), which will probably cripple the
2356 * system.
2357 */
2358 gfp |= __GFP_NORETRY;
2359 p = kmem_cache_alloc(pool, gfp);
2360 if (!p) {
2361 LOGERR("kmem_cache_alloc failed early @%s:%d\n", fn, ln);
2362 return NULL;
2363 }
2364 atomic_inc(&Visorchipset_cache_buffers_in_use);
2365 return p;
2366}
2367
2368/* Generic wrapper function for freeing memory from a kmem_cache pool.
2369 */
2370void
2371visorchipset_cache_free(struct kmem_cache *pool, void *p, char *fn, int ln)
2372{
2373 if (!p) {
2374 LOGERR("NULL pointer @%s:%d\n", fn, ln);
2375 return;
2376 }
2377 atomic_dec(&Visorchipset_cache_buffers_in_use);
2378 kmem_cache_free(pool, p);
2379}
2380
18b87ed1
BR
2381static ssize_t chipsetready_store(struct device *dev,
2382 struct device_attribute *attr, const char *buf, size_t count)
12e364b9 2383{
18b87ed1 2384 char msgtype[64];
12e364b9 2385
18b87ed1
BR
2386 if (sscanf(buf, "%63s", msgtype) == 1) {
2387 if (strcmp(msgtype, "CALLHOMEDISK_MOUNTED") == 0)
12e364b9 2388 chipset_events[0] = 1;
18b87ed1 2389 else if (strcmp(msgtype, "MODULES_LOADED") == 0)
12e364b9 2390 chipset_events[1] = 1;
18b87ed1
BR
2391 else
2392 return -EINVAL;
12e364b9 2393 } else {
18b87ed1 2394 return -EINVAL;
12e364b9 2395 }
12e364b9
KC
2396 return count;
2397}
2398
2399static ssize_t
2400visorchipset_proc_read_writeonly(struct file *file, char __user *buf,
2401 size_t len, loff_t *offset)
2402{
2403 return 0;
2404}
2405
12e364b9
KC
2406static int __init
2407visorchipset_init(void)
2408{
2409 int rc = 0, x = 0;
8a1182eb 2410 char s[64];
8a1182eb 2411 HOSTADDRESS addr;
12e364b9 2412
fcd0157e
KC
2413 if (!unisys_spar_platform)
2414 return -ENODEV;
2415
12e364b9
KC
2416 LOGINF("chipset driver version %s loaded", VERSION);
2417 /* process module options */
2418 POSTCODE_LINUX_2(DRIVER_ENTRY_PC, POSTCODE_SEVERITY_INFO);
2419
2420 LOGINF("option - testvnic=%d", visorchipset_testvnic);
2421 LOGINF("option - testvnicclient=%d", visorchipset_testvnicclient);
2422 LOGINF("option - testmsg=%d", visorchipset_testmsg);
2423 LOGINF("option - testteardown=%d", visorchipset_testteardown);
2424 LOGINF("option - major=%d", visorchipset_major);
2425 LOGINF("option - serverregwait=%d", visorchipset_serverregwait);
2426 LOGINF("option - clientregwait=%d", visorchipset_clientregwait);
2427 LOGINF("option - holdchipsetready=%d", visorchipset_holdchipsetready);
2428
2429 memset(&BusDev_Server_Notifiers, 0, sizeof(BusDev_Server_Notifiers));
2430 memset(&BusDev_Client_Notifiers, 0, sizeof(BusDev_Client_Notifiers));
2431 memset(&ControlVm_payload_info, 0, sizeof(ControlVm_payload_info));
2432 memset(&LiveDump_info, 0, sizeof(LiveDump_info));
2433 atomic_set(&LiveDump_info.buffers_in_use, 0);
2434
9f8d0e8b
KC
2435 if (visorchipset_testvnic) {
2436 ERRDRV("testvnic option no longer supported: (status = %d)\n",
2437 x);
2438 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, x, DIAG_SEVERITY_ERR);
2439 rc = x;
2440 goto Away;
2441 }
12e364b9 2442
8a1182eb
BR
2443 addr = controlvm_get_channel_address();
2444 if (addr != 0) {
2445 ControlVm_channel =
2446 visorchannel_create_with_lock
2447 (addr,
2448 sizeof(ULTRA_CONTROLVM_CHANNEL_PROTOCOL),
2449 UltraControlvmChannelProtocolGuid);
2450 if (ULTRA_CONTROLVM_CHANNEL_OK_CLIENT
2451 (visorchannel_get_header(ControlVm_channel),
2452 NULL)) {
2453 LOGINF("Channel %s (ControlVm) discovered",
2454 visorchannel_id(ControlVm_channel, s));
2455 initialize_controlvm_payload();
2456 } else {
2457 LOGERR("controlvm channel is invalid");
2458 visorchannel_destroy(ControlVm_channel);
2459 ControlVm_channel = NULL;
2460 return -ENODEV;
2461 }
2462 } else {
2463 LOGERR("no controlvm channel discovered");
2464 return -ENODEV;
2465 }
2466
12e364b9 2467 MajorDev = MKDEV(visorchipset_major, 0);
9f8d0e8b 2468 rc = visorchipset_file_init(MajorDev, &ControlVm_channel);
4cb005a9
KC
2469 if (rc < 0) {
2470 ERRDRV("visorchipset_file_init(MajorDev, &ControlVm_channel): error (status=%d)\n", rc);
2471 POSTCODE_LINUX_2(CHIPSET_INIT_FAILURE_PC, DIAG_SEVERITY_ERR);
2472 goto Away;
2473 }
9f8d0e8b 2474
12e364b9
KC
2475 proc_Init();
2476 memset(PartitionPropertyNames, 0, sizeof(PartitionPropertyNames));
12e364b9 2477 InitPartitionProperties();
12e364b9 2478
927c7927
KC
2479 PartitionType = visor_proc_CreateType(ProcDir, PartitionTypeNames,
2480 (const char **)
2481 PartitionPropertyNames,
2482 &show_partition_property);
12e364b9 2483
12e364b9
KC
2484 memset(&g_DiagMsgHdr, 0, sizeof(CONTROLVM_MESSAGE_HEADER));
2485
12e364b9
KC
2486 memset(&g_ChipSetMsgHdr, 0, sizeof(CONTROLVM_MESSAGE_HEADER));
2487
2488 parahotplug_proc_dir =
2489 proc_create(VISORCHIPSET_PARAHOTPLUG_PROC_ENTRY_FN, 0200,
2490 ProcDir, &parahotplug_proc_fops);
2491 memset(&g_DelDumpMsgHdr, 0, sizeof(CONTROLVM_MESSAGE_HEADER));
2492
12e364b9
KC
2493 Putfile_buffer_list_pool =
2494 kmem_cache_create(Putfile_buffer_list_pool_name,
2495 sizeof(struct putfile_buffer_entry),
2496 0, SLAB_HWCACHE_ALIGN, NULL);
2497 if (!Putfile_buffer_list_pool) {
4cb005a9
KC
2498 ERRDRV("failed to alloc Putfile_buffer_list_pool: (status=-1)\n");
2499 POSTCODE_LINUX_2(CHIPSET_INIT_FAILURE_PC, DIAG_SEVERITY_ERR);
2500 rc = -1;
2501 goto Away;
12e364b9
KC
2502 }
2503 if (visorchipset_disable_controlvm) {
2504 LOGINF("visorchipset_init:controlvm disabled");
2505 } else {
2506 /* if booting in a crash kernel */
2507 if (visorchipset_crash_kernel)
2508 INIT_DELAYED_WORK(&Periodic_controlvm_work,
2509 setup_crash_devices_work_queue);
2510 else
2511 INIT_DELAYED_WORK(&Periodic_controlvm_work,
2512 controlvm_periodic_work);
2513 Periodic_controlvm_workqueue =
2514 create_singlethread_workqueue("visorchipset_controlvm");
2515
4cb005a9
KC
2516 if (Periodic_controlvm_workqueue == NULL) {
2517 ERRDRV("cannot create controlvm workqueue: (status=%d)\n",
2518 -ENOMEM);
2519 POSTCODE_LINUX_2(CREATE_WORKQUEUE_FAILED_PC,
2520 DIAG_SEVERITY_ERR);
2521 rc = -ENOMEM;
2522 goto Away;
2523 }
12e364b9
KC
2524 Most_recent_message_jiffies = jiffies;
2525 Poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
9f8d0e8b
KC
2526 rc = queue_delayed_work(Periodic_controlvm_workqueue,
2527 &Periodic_controlvm_work, Poll_jiffies);
4cb005a9
KC
2528 if (rc < 0) {
2529 ERRDRV("queue_delayed_work(Periodic_controlvm_workqueue, &Periodic_controlvm_work, Poll_jiffies): error (status=%d)\n", rc);
2530 POSTCODE_LINUX_2(QUEUE_DELAYED_WORK_PC,
2531 DIAG_SEVERITY_ERR);
2532 goto Away;
2533 }
9f8d0e8b 2534
12e364b9
KC
2535 }
2536
2537 Visorchipset_platform_device.dev.devt = MajorDev;
4cb005a9
KC
2538 if (platform_device_register(&Visorchipset_platform_device) < 0) {
2539 ERRDRV("platform_device_register(visorchipset) failed: (status=-1)\n");
2540 POSTCODE_LINUX_2(DEVICE_REGISTER_FAILURE_PC, DIAG_SEVERITY_ERR);
2541 rc = -1;
2542 goto Away;
2543 }
12e364b9
KC
2544 LOGINF("visorchipset device created");
2545 POSTCODE_LINUX_2(CHIPSET_INIT_SUCCESS_PC, POSTCODE_SEVERITY_INFO);
22ad57ba 2546 rc = 0;
12e364b9 2547Away:
12e364b9
KC
2548 if (rc) {
2549 LOGERR("visorchipset_init failed");
2550 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
2551 POSTCODE_SEVERITY_ERR);
2552 }
2553 return rc;
2554}
2555
2556static void
2557visorchipset_exit(void)
2558{
2559 char s[99];
2560 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
2561
2562 if (visorchipset_disable_controlvm) {
2563 ;
2564 } else {
2565 cancel_delayed_work(&Periodic_controlvm_work);
2566 flush_workqueue(Periodic_controlvm_workqueue);
2567 destroy_workqueue(Periodic_controlvm_workqueue);
2568 Periodic_controlvm_workqueue = NULL;
2569 destroy_controlvm_payload_info(&ControlVm_payload_info);
2570 }
2571 Test_Vnic_channel = NULL;
2572 if (Putfile_buffer_list_pool) {
2573 kmem_cache_destroy(Putfile_buffer_list_pool);
2574 Putfile_buffer_list_pool = NULL;
2575 }
1783319f 2576
12e364b9
KC
2577 cleanup_controlvm_structures();
2578
12e364b9 2579 if (PartitionType) {
927c7927 2580 visor_proc_DestroyType(PartitionType);
12e364b9
KC
2581 PartitionType = NULL;
2582 }
bec68463 2583
12e364b9
KC
2584 memset(&g_DiagMsgHdr, 0, sizeof(CONTROLVM_MESSAGE_HEADER));
2585
12e364b9
KC
2586 memset(&g_ChipSetMsgHdr, 0, sizeof(CONTROLVM_MESSAGE_HEADER));
2587
2588 if (parahotplug_proc_dir) {
2589 remove_proc_entry(VISORCHIPSET_PARAHOTPLUG_PROC_ENTRY_FN,
2590 ProcDir);
2591 parahotplug_proc_dir = NULL;
2592 }
2593
2594 memset(&g_DelDumpMsgHdr, 0, sizeof(CONTROLVM_MESSAGE_HEADER));
2595
2596 proc_DeInit();
8a1182eb
BR
2597 LOGINF("Channel %s (ControlVm) disconnected",
2598 visorchannel_id(ControlVm_channel, s));
2599 visorchannel_destroy(ControlVm_channel);
2600
12e364b9
KC
2601 visorchipset_file_cleanup();
2602 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
2603 LOGINF("chipset driver unloaded");
2604}
2605
2606module_param_named(testvnic, visorchipset_testvnic, int, S_IRUGO);
2607MODULE_PARM_DESC(visorchipset_testvnic, "1 to test vnic, using dummy VNIC connected via a loopback to a physical ethernet");
2608int visorchipset_testvnic = 0;
2609
2610module_param_named(testvnicclient, visorchipset_testvnicclient, int, S_IRUGO);
2611MODULE_PARM_DESC(visorchipset_testvnicclient, "1 to test vnic, using real VNIC channel attached to a separate IOVM guest");
2612int visorchipset_testvnicclient = 0;
2613
2614module_param_named(testmsg, visorchipset_testmsg, int, S_IRUGO);
2615MODULE_PARM_DESC(visorchipset_testmsg,
2616 "1 to manufacture the chipset, bus, and switch messages");
2617int visorchipset_testmsg = 0;
2618
2619module_param_named(major, visorchipset_major, int, S_IRUGO);
2620MODULE_PARM_DESC(visorchipset_major, "major device number to use for the device node");
2621int visorchipset_major = 0;
2622
2623module_param_named(serverregwait, visorchipset_serverregwait, int, S_IRUGO);
2624MODULE_PARM_DESC(visorchipset_serverreqwait,
2625 "1 to have the module wait for the visor bus to register");
2626int visorchipset_serverregwait = 0; /* default is off */
2627module_param_named(clientregwait, visorchipset_clientregwait, int, S_IRUGO);
2628MODULE_PARM_DESC(visorchipset_clientregwait, "1 to have the module wait for the visorclientbus to register");
2629int visorchipset_clientregwait = 1; /* default is on */
2630module_param_named(testteardown, visorchipset_testteardown, int, S_IRUGO);
2631MODULE_PARM_DESC(visorchipset_testteardown,
2632 "1 to test teardown of the chipset, bus, and switch");
2633int visorchipset_testteardown = 0; /* default is off */
2634module_param_named(disable_controlvm, visorchipset_disable_controlvm, int,
2635 S_IRUGO);
2636MODULE_PARM_DESC(visorchipset_disable_controlvm,
2637 "1 to disable polling of controlVm channel");
2638int visorchipset_disable_controlvm = 0; /* default is off */
2639module_param_named(crash_kernel, visorchipset_crash_kernel, int, S_IRUGO);
2640MODULE_PARM_DESC(visorchipset_crash_kernel,
2641 "1 means we are running in crash kernel");
2642int visorchipset_crash_kernel = 0; /* default is running in non-crash kernel */
2643module_param_named(holdchipsetready, visorchipset_holdchipsetready,
2644 int, S_IRUGO);
2645MODULE_PARM_DESC(visorchipset_holdchipsetready,
2646 "1 to hold response to CHIPSET_READY");
2647int visorchipset_holdchipsetready = 0; /* default is to send CHIPSET_READY
2648 * response immediately */
2649module_init(visorchipset_init);
2650module_exit(visorchipset_exit);
2651
2652MODULE_AUTHOR("Unisys");
2653MODULE_LICENSE("GPL");
2654MODULE_DESCRIPTION("Supervisor chipset driver for service partition: ver "
2655 VERSION);
2656MODULE_VERSION(VERSION);
This page took 0.418051 seconds and 5 git commands to generate.