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