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