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