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