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