[go: nahoru, domu]

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