blob: 2cf59cacc7f42abd4f43ff5a98c5646c38080a11 [file] [log] [blame]
Ken Cox12e364b2014-03-04 07:58:07 -06001/* visorchipset_main.c
2 *
Benjamin Romerf6d0c1e2014-04-23 14:58:34 -04003 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
Ken Cox12e364b2014-03-04 07:58:07 -06004 * 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
Prarit Bhargava55c67dc2015-05-05 18:37:02 -040018#include <linux/acpi.h>
Erik Arfvidsonc0a14642015-05-05 18:37:06 -040019#include <linux/cdev.h>
Erik Arfvidson46168812015-05-05 18:36:14 -040020#include <linux/ctype.h>
Erik Arfvidsone3420ed2015-05-05 18:36:13 -040021#include <linux/fs.h>
22#include <linux/mm.h>
Ken Cox12e364b2014-03-04 07:58:07 -060023#include <linux/nls.h>
24#include <linux/netdevice.h>
25#include <linux/platform_device.h>
Benjamin Romer90addb02014-05-06 09:58:23 -040026#include <linux/uuid.h>
Benjamin Romer1ba00982015-04-06 10:27:40 -040027#include <linux/crash_dump.h>
Ken Cox12e364b2014-03-04 07:58:07 -060028
Prarit Bhargava55c67dc2015-05-05 18:37:02 -040029#include "controlvmchannel.h"
30#include "controlvmcompletionstatus.h"
31#include "guestlinuxdebug.h"
32#include "periodic_work.h"
Prarit Bhargava55c67dc2015-05-05 18:37:02 -040033#include "uisutils.h"
34#include "version.h"
35#include "visorbus.h"
36#include "visorbus_private.h"
37
Ken Cox12e364b2014-03-04 07:58:07 -060038#define CURRENT_FILE_PC VISOR_CHIPSET_PC_visorchipset_main_c
Ken Cox12e364b2014-03-04 07:58:07 -060039
40#define MAX_NAME_SIZE 128
41#define MAX_IP_SIZE 50
42#define MAXOUTSTANDINGCHANNELCOMMAND 256
43#define POLLJIFFIES_CONTROLVMCHANNEL_FAST 1
44#define POLLJIFFIES_CONTROLVMCHANNEL_SLOW 100
45
Erik Arfvidson46168812015-05-05 18:36:14 -040046#define MAX_CONTROLVM_PAYLOAD_BYTES (1024*128)
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -040047
48#define VISORCHIPSET_MMAP_CONTROLCHANOFFSET 0x00000000
49
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -040050
51#define UNISYS_SPAR_LEAF_ID 0x40000000
52
53/* The s-Par leaf ID returns "UnisysSpar64" encoded across ebx, ecx, edx */
54#define UNISYS_SPAR_ID_EBX 0x73696e55
55#define UNISYS_SPAR_ID_ECX 0x70537379
56#define UNISYS_SPAR_ID_EDX 0x34367261
57
Jes Sorensenb615d622015-05-05 18:35:38 -040058/*
59 * Module parameters
60 */
Jes Sorensenb615d622015-05-05 18:35:38 -040061static int visorchipset_major;
David Kershner4da33362015-05-05 18:36:39 -040062static int visorchipset_visorbusregwait = 1; /* default is on */
Jes Sorensenb615d622015-05-05 18:35:38 -040063static int visorchipset_holdchipsetready;
Erik Arfvidson46168812015-05-05 18:36:14 -040064static unsigned long controlvm_payload_bytes_buffered;
Jes Sorensenb615d622015-05-05 18:35:38 -040065
Erik Arfvidsone3420ed2015-05-05 18:36:13 -040066static int
67visorchipset_open(struct inode *inode, struct file *file)
68{
69 unsigned minor_number = iminor(inode);
70
71 if (minor_number)
72 return -ENODEV;
73 file->private_data = NULL;
74 return 0;
75}
76
77static int
78visorchipset_release(struct inode *inode, struct file *file)
79{
80 return 0;
81}
82
Ken Cox12e364b2014-03-04 07:58:07 -060083/* When the controlvm channel is idle for at least MIN_IDLE_SECONDS,
84* we switch to slow polling mode. As soon as we get a controlvm
85* message, we switch back to fast polling mode.
86*/
87#define MIN_IDLE_SECONDS 10
Jes Sorensen52063ec2015-04-13 10:28:41 -040088static unsigned long poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
89static unsigned long most_recent_message_jiffies; /* when we got our last
Ken Coxbd5b9b32014-03-13 15:39:22 -050090 * controlvm message */
David Kershner4da33362015-05-05 18:36:39 -040091static int visorbusregistered;
Ken Cox12e364b2014-03-04 07:58:07 -060092
93#define MAX_CHIPSET_EVENTS 2
Benjamin Romerc2422332014-07-29 15:09:40 -040094static u8 chipset_events[MAX_CHIPSET_EVENTS] = { 0, 0 };
Ken Cox12e364b2014-03-04 07:58:07 -060095
Erik Arfvidson46168812015-05-05 18:36:14 -040096struct parser_context {
97 unsigned long allocbytes;
98 unsigned long param_bytes;
99 u8 *curr;
100 unsigned long bytes_remaining;
101 bool byte_stream;
102 char data[0];
103};
104
Benjamin Romer9232d2d2015-03-16 13:57:57 -0400105static struct delayed_work periodic_controlvm_work;
106static struct workqueue_struct *periodic_controlvm_workqueue;
Benjamin Romer8f1947a2015-03-16 13:57:59 -0400107static DEFINE_SEMAPHORE(notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -0600108
Erik Arfvidsone3420ed2015-05-05 18:36:13 -0400109static struct cdev file_cdev;
110static struct visorchannel **file_controlvm_channel;
Benjamin Romerda021f02015-03-16 13:57:58 -0400111static struct controlvm_message_header g_chipset_msg_hdr;
Benjamin Romer59827f02015-03-16 13:58:00 -0400112static const uuid_le spar_diag_pool_channel_protocol_uuid =
Benjamin Romer9eee5d12014-10-23 14:29:47 -0400113 SPAR_DIAG_POOL_CHANNEL_PROTOCOL_UUID;
Ken Cox12e364b2014-03-04 07:58:07 -0600114/* 0xffffff is an invalid Bus/Device number */
Jes Sorensen52063ec2015-04-13 10:28:41 -0400115static u32 g_diagpool_bus_no = 0xffffff;
116static u32 g_diagpool_dev_no = 0xffffff;
Benjamin Romer4f44b722015-03-16 13:58:02 -0400117static struct controlvm_message_packet g_devicechangestate_packet;
Ken Cox12e364b2014-03-04 07:58:07 -0600118
Ken Cox12e364b2014-03-04 07:58:07 -0600119#define is_diagpool_channel(channel_type_guid) \
Benjamin Romer59827f02015-03-16 13:58:00 -0400120 (uuid_le_cmp(channel_type_guid,\
121 spar_diag_pool_channel_protocol_uuid) == 0)
Ken Cox12e364b2014-03-04 07:58:07 -0600122
Benjamin Romer1390b882015-03-16 13:58:03 -0400123static LIST_HEAD(bus_info_list);
124static LIST_HEAD(dev_info_list);
Ken Cox12e364b2014-03-04 07:58:07 -0600125
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400126static struct visorchannel *controlvm_channel;
Ken Cox12e364b2014-03-04 07:58:07 -0600127
Benjamin Romer84982fb2015-03-16 13:58:07 -0400128/* Manages the request payload in the controlvm channel */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400129struct visor_controlvm_payload_info {
Benjamin Romerc2422332014-07-29 15:09:40 -0400130 u8 __iomem *ptr; /* pointer to base address of payload pool */
Benjamin Romer5fc02292014-07-31 12:00:51 -0400131 u64 offset; /* offset from beginning of controlvm
Ken Cox12e364b2014-03-04 07:58:07 -0600132 * channel to beginning of payload * pool */
Benjamin Romerb3c55b12014-07-31 12:00:50 -0400133 u32 bytes; /* number of bytes in payload pool */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400134};
135
136static struct visor_controlvm_payload_info controlvm_payload_info;
Ken Cox12e364b2014-03-04 07:58:07 -0600137
Benjamin Romerea33b4ee52015-03-16 13:58:08 -0400138/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
139 * CONTROLVM_DUMP_GETTEXTDUMP / CONTROLVM_DUMP_COMPLETE conversation.
140 */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400141struct visor_livedump_info {
Benjamin Romerea33b4ee52015-03-16 13:58:08 -0400142 struct controlvm_message_header dumpcapture_header;
143 struct controlvm_message_header gettextdump_header;
144 struct controlvm_message_header dumpcomplete_header;
Jes Sorensenf4c11552015-04-13 10:28:40 -0400145 bool gettextdump_outstanding;
Ken Cox12e364b2014-03-04 07:58:07 -0600146 u32 crc32;
Jes Sorensen52063ec2015-04-13 10:28:41 -0400147 unsigned long length;
Ken Cox12e364b2014-03-04 07:58:07 -0600148 atomic_t buffers_in_use;
Jes Sorensen52063ec2015-04-13 10:28:41 -0400149 unsigned long destination;
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400150};
151
152static struct visor_livedump_info livedump_info;
Ken Cox12e364b2014-03-04 07:58:07 -0600153
154/* The following globals are used to handle the scenario where we are unable to
155 * offload the payload from a controlvm message due to memory requirements. In
156 * this scenario, we simply stash the controlvm message, then attempt to
157 * process it again the next time controlvm_periodic_work() runs.
158 */
Benjamin Romer7166ed12015-03-16 13:58:38 -0400159static struct controlvm_message controlvm_pending_msg;
Prarit Bhargavac79b28f2015-05-05 18:36:15 -0400160static bool controlvm_pending_msg_valid;
Ken Cox12e364b2014-03-04 07:58:07 -0600161
Ken Cox12e364b2014-03-04 07:58:07 -0600162/* This identifies a data buffer that has been received via a controlvm messages
163 * in a remote --> local CONTROLVM_TRANSMIT_FILE conversation.
164 */
165struct putfile_buffer_entry {
166 struct list_head next; /* putfile_buffer_entry list */
Benjamin Romer317d9612015-03-16 13:57:51 -0400167 struct parser_context *parser_ctx; /* points to input data buffer */
Ken Cox12e364b2014-03-04 07:58:07 -0600168};
169
170/* List of struct putfile_request *, via next_putfile_request member.
171 * Each entry in this list identifies an outstanding TRANSMIT_FILE
172 * conversation.
173 */
Benjamin Romer1eee0012015-03-16 13:58:39 -0400174static LIST_HEAD(putfile_request_list);
Ken Cox12e364b2014-03-04 07:58:07 -0600175
176/* This describes a buffer and its current state of transfer (e.g., how many
177 * bytes have already been supplied as putfile data, and how many bytes are
178 * remaining) for a putfile_request.
179 */
180struct putfile_active_buffer {
181 /* a payload from a controlvm message, containing a file data buffer */
Benjamin Romer317d9612015-03-16 13:57:51 -0400182 struct parser_context *parser_ctx;
Ken Cox12e364b2014-03-04 07:58:07 -0600183 /* points within data area of parser_ctx to next byte of data */
184 u8 *pnext;
185 /* # bytes left from <pnext> to the end of this data buffer */
186 size_t bytes_remaining;
187};
188
189#define PUTFILE_REQUEST_SIG 0x0906101302281211
190/* This identifies a single remote --> local CONTROLVM_TRANSMIT_FILE
191 * conversation. Structs of this type are dynamically linked into
192 * <Putfile_request_list>.
193 */
194struct putfile_request {
195 u64 sig; /* PUTFILE_REQUEST_SIG */
196
197 /* header from original TransmitFile request */
Benjamin Romer98d7b592014-10-23 14:30:26 -0400198 struct controlvm_message_header controlvm_header;
Ken Cox12e364b2014-03-04 07:58:07 -0600199 u64 file_request_number; /* from original TransmitFile request */
200
201 /* link to next struct putfile_request */
202 struct list_head next_putfile_request;
203
204 /* most-recent sequence number supplied via a controlvm message */
205 u64 data_sequence_number;
206
207 /* head of putfile_buffer_entry list, which describes the data to be
208 * supplied as putfile data;
209 * - this list is added to when controlvm messages come in that supply
210 * file data
211 * - this list is removed from via the hotplug program that is actually
212 * consuming these buffers to write as file data */
213 struct list_head input_buffer_list;
214 spinlock_t req_list_lock; /* lock for input_buffer_list */
215
216 /* waiters for input_buffer_list to go non-empty */
217 wait_queue_head_t input_buffer_wq;
218
219 /* data not yet read within current putfile_buffer_entry */
220 struct putfile_active_buffer active_buf;
221
222 /* <0 = failed, 0 = in-progress, >0 = successful; */
223 /* note that this must be set with req_list_lock, and if you set <0, */
224 /* it is your responsibility to also free up all of the other objects */
225 /* in this struct (like input_buffer_list, active_buf.parser_ctx) */
226 /* before releasing the lock */
227 int completion_status;
228};
229
Ken Cox12e364b2014-03-04 07:58:07 -0600230struct parahotplug_request {
231 struct list_head list;
232 int id;
233 unsigned long expiration;
Benjamin Romer3ab47702014-10-23 14:30:31 -0400234 struct controlvm_message msg;
Ken Cox12e364b2014-03-04 07:58:07 -0600235};
236
Benjamin Romerddf5de52015-03-16 13:58:41 -0400237static LIST_HEAD(parahotplug_request_list);
238static DEFINE_SPINLOCK(parahotplug_request_list_lock); /* lock for above */
Ken Cox12e364b2014-03-04 07:58:07 -0600239static void parahotplug_process_list(void);
240
241/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
242 * CONTROLVM_REPORTEVENT.
243 */
David Kershner4da33362015-05-05 18:36:39 -0400244static struct visorchipset_busdev_notifiers busdev_notifiers;
Ken Cox12e364b2014-03-04 07:58:07 -0600245
Jes Sorensen52063ec2015-04-13 10:28:41 -0400246static void bus_create_response(u32 bus_no, int response);
247static void bus_destroy_response(u32 bus_no, int response);
248static void device_create_response(u32 bus_no, u32 dev_no, int response);
249static void device_destroy_response(u32 bus_no, u32 dev_no, int response);
250static void device_resume_response(u32 bus_no, u32 dev_no, int response);
Ken Cox12e364b2014-03-04 07:58:07 -0600251
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400252static void visorchipset_device_pause_response(u32 bus_no, u32 dev_no,
253 int response);
254
Benjamin Romer8e3fedd2015-03-16 13:58:43 -0400255static struct visorchipset_busdev_responders busdev_responders = {
Ken Cox12e364b2014-03-04 07:58:07 -0600256 .bus_create = bus_create_response,
257 .bus_destroy = bus_destroy_response,
258 .device_create = device_create_response,
259 .device_destroy = device_destroy_response,
Ken Cox927c7922014-03-05 14:52:25 -0600260 .device_pause = visorchipset_device_pause_response,
Ken Cox12e364b2014-03-04 07:58:07 -0600261 .device_resume = device_resume_response,
262};
263
264/* info for /dev/visorchipset */
Benjamin Romer5aa8ae52015-03-16 13:58:44 -0400265static dev_t major_dev = -1; /**< indicates major num for device */
Ken Cox12e364b2014-03-04 07:58:07 -0600266
Benjamin Romer19f66342014-07-22 09:56:25 -0400267/* prototypes for attributes */
268static ssize_t toolaction_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400269 struct device_attribute *attr, char *buf);
Benjamin Romer19f66342014-07-22 09:56:25 -0400270static ssize_t toolaction_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400271 struct device_attribute *attr,
272 const char *buf, size_t count);
Benjamin Romer19f66342014-07-22 09:56:25 -0400273static DEVICE_ATTR_RW(toolaction);
274
Benjamin Romer54b31222014-07-22 09:56:26 -0400275static ssize_t boottotool_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400276 struct device_attribute *attr, char *buf);
Benjamin Romer54b31222014-07-22 09:56:26 -0400277static ssize_t boottotool_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400278 struct device_attribute *attr, const char *buf,
279 size_t count);
Benjamin Romer54b31222014-07-22 09:56:26 -0400280static DEVICE_ATTR_RW(boottotool);
281
Benjamin Romer422af172014-07-24 14:08:42 -0400282static ssize_t error_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400283 char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400284static ssize_t error_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400285 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400286static DEVICE_ATTR_RW(error);
287
288static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400289 char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400290static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400291 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400292static DEVICE_ATTR_RW(textid);
293
294static ssize_t remaining_steps_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400295 struct device_attribute *attr, char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400296static ssize_t remaining_steps_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400297 struct device_attribute *attr,
298 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400299static DEVICE_ATTR_RW(remaining_steps);
300
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400301static ssize_t chipsetready_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400302 struct device_attribute *attr,
303 const char *buf, size_t count);
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400304static DEVICE_ATTR_WO(chipsetready);
305
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400306static ssize_t devicedisabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400307 struct device_attribute *attr,
308 const char *buf, size_t count);
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400309static DEVICE_ATTR_WO(devicedisabled);
310
311static ssize_t deviceenabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400312 struct device_attribute *attr,
313 const char *buf, size_t count);
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400314static DEVICE_ATTR_WO(deviceenabled);
315
Benjamin Romer19f66342014-07-22 09:56:25 -0400316static struct attribute *visorchipset_install_attrs[] = {
317 &dev_attr_toolaction.attr,
Benjamin Romer54b31222014-07-22 09:56:26 -0400318 &dev_attr_boottotool.attr,
Benjamin Romer422af172014-07-24 14:08:42 -0400319 &dev_attr_error.attr,
320 &dev_attr_textid.attr,
321 &dev_attr_remaining_steps.attr,
Benjamin Romer19f66342014-07-22 09:56:25 -0400322 NULL
323};
324
325static struct attribute_group visorchipset_install_group = {
326 .name = "install",
327 .attrs = visorchipset_install_attrs
328};
329
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400330static struct attribute *visorchipset_guest_attrs[] = {
331 &dev_attr_chipsetready.attr,
332 NULL
333};
334
335static struct attribute_group visorchipset_guest_group = {
336 .name = "guest",
337 .attrs = visorchipset_guest_attrs
338};
339
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400340static struct attribute *visorchipset_parahotplug_attrs[] = {
341 &dev_attr_devicedisabled.attr,
342 &dev_attr_deviceenabled.attr,
343 NULL
344};
345
346static struct attribute_group visorchipset_parahotplug_group = {
347 .name = "parahotplug",
348 .attrs = visorchipset_parahotplug_attrs
349};
350
Benjamin Romer19f66342014-07-22 09:56:25 -0400351static const struct attribute_group *visorchipset_dev_groups[] = {
352 &visorchipset_install_group,
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400353 &visorchipset_guest_group,
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400354 &visorchipset_parahotplug_group,
Benjamin Romer19f66342014-07-22 09:56:25 -0400355 NULL
356};
357
Ken Cox12e364b2014-03-04 07:58:07 -0600358/* /sys/devices/platform/visorchipset */
Benjamin Romereb34e872015-03-16 13:58:45 -0400359static struct platform_device visorchipset_platform_device = {
Ken Cox12e364b2014-03-04 07:58:07 -0600360 .name = "visorchipset",
361 .id = -1,
Benjamin Romer19f66342014-07-22 09:56:25 -0400362 .dev.groups = visorchipset_dev_groups,
Ken Cox12e364b2014-03-04 07:58:07 -0600363};
364
365/* Function prototypes */
Benjamin Romerb3168c72015-03-16 13:58:46 -0400366static void controlvm_respond(struct controlvm_message_header *msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400367 int response);
368static void controlvm_respond_chipset_init(
Benjamin Romerb3168c72015-03-16 13:58:46 -0400369 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400370 enum ultra_chipset_feature features);
371static void controlvm_respond_physdev_changestate(
Benjamin Romerb3168c72015-03-16 13:58:46 -0400372 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400373 struct spar_segment_state state);
Ken Cox12e364b2014-03-04 07:58:07 -0600374
Erik Arfvidson46168812015-05-05 18:36:14 -0400375
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400376static void parser_done(struct parser_context *ctx);
377
Erik Arfvidson46168812015-05-05 18:36:14 -0400378static struct parser_context *
Jes Sorensencc55b5c2015-05-05 18:37:13 -0400379parser_init_guts(u64 addr, u32 bytes, bool local, bool *retry)
Erik Arfvidson46168812015-05-05 18:36:14 -0400380{
381 int allocbytes = sizeof(struct parser_context) + bytes;
382 struct parser_context *rc = NULL;
383 struct parser_context *ctx = NULL;
Erik Arfvidson46168812015-05-05 18:36:14 -0400384
385 if (retry)
386 *retry = false;
Jes Sorensencc55b5c2015-05-05 18:37:13 -0400387
388 /*
389 * alloc an 0 extra byte to ensure payload is
390 * '\0'-terminated
391 */
392 allocbytes++;
Erik Arfvidson46168812015-05-05 18:36:14 -0400393 if ((controlvm_payload_bytes_buffered + bytes)
394 > MAX_CONTROLVM_PAYLOAD_BYTES) {
395 if (retry)
396 *retry = true;
397 rc = NULL;
398 goto cleanup;
399 }
400 ctx = kzalloc(allocbytes, GFP_KERNEL|__GFP_NORETRY);
401 if (!ctx) {
402 if (retry)
403 *retry = true;
404 rc = NULL;
405 goto cleanup;
406 }
407
408 ctx->allocbytes = allocbytes;
409 ctx->param_bytes = bytes;
410 ctx->curr = NULL;
411 ctx->bytes_remaining = 0;
412 ctx->byte_stream = false;
413 if (local) {
414 void *p;
415
416 if (addr > virt_to_phys(high_memory - 1)) {
417 rc = NULL;
418 goto cleanup;
419 }
420 p = __va((unsigned long) (addr));
421 memcpy(ctx->data, p, bytes);
422 } else {
Jes Sorensendd412752015-05-05 18:36:26 -0400423 void __iomem *mapping;
Jes Sorensen712c03d2015-05-05 18:36:25 -0400424
Jes Sorensendd412752015-05-05 18:36:26 -0400425 if (!request_mem_region(addr, bytes, "visorchipset")) {
Erik Arfvidson46168812015-05-05 18:36:14 -0400426 rc = NULL;
427 goto cleanup;
428 }
Jes Sorensendd412752015-05-05 18:36:26 -0400429
430 mapping = ioremap_cache(addr, bytes);
431 if (!mapping) {
432 release_mem_region(addr, bytes);
433 rc = NULL;
434 goto cleanup;
435 }
436 memcpy_fromio(ctx->data, mapping, bytes);
437 release_mem_region(addr, bytes);
Erik Arfvidson46168812015-05-05 18:36:14 -0400438 }
Erik Arfvidson46168812015-05-05 18:36:14 -0400439
Jes Sorensencc55b5c2015-05-05 18:37:13 -0400440 ctx->byte_stream = true;
Erik Arfvidson46168812015-05-05 18:36:14 -0400441 rc = ctx;
442cleanup:
Erik Arfvidson46168812015-05-05 18:36:14 -0400443 if (rc) {
444 controlvm_payload_bytes_buffered += ctx->param_bytes;
445 } else {
446 if (ctx) {
447 parser_done(ctx);
448 ctx = NULL;
449 }
450 }
451 return rc;
452}
453
Erik Arfvidson46168812015-05-05 18:36:14 -0400454/* Call this instead of parser_init() if the payload area consists of just
455 * a sequence of bytes, rather than a struct spar_controlvm_parameters_header
456 * structures. Afterwards, you can call parser_simpleString_get() or
457 * parser_byteStream_get() to obtain the data.
458 */
Jes Sorensenfcc974d2015-05-05 18:37:11 -0400459static struct parser_context *
Erik Arfvidson46168812015-05-05 18:36:14 -0400460parser_init_byte_stream(u64 addr, u32 bytes, bool local, bool *retry)
461{
Jes Sorensencc55b5c2015-05-05 18:37:13 -0400462 return parser_init_guts(addr, bytes, local, retry);
Erik Arfvidson46168812015-05-05 18:36:14 -0400463}
464
Jes Sorensen464129e2015-05-05 18:37:12 -0400465static uuid_le
Erik Arfvidson46168812015-05-05 18:36:14 -0400466parser_id_get(struct parser_context *ctx)
467{
468 struct spar_controlvm_parameters_header *phdr = NULL;
469
470 if (ctx == NULL)
471 return NULL_UUID_LE;
472 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
473 return phdr->id;
474}
475
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400476/** Describes the state from the perspective of which controlvm messages have
477 * been received for a bus or device.
478 */
479
480enum PARSER_WHICH_STRING {
481 PARSERSTRING_INITIATOR,
482 PARSERSTRING_TARGET,
483 PARSERSTRING_CONNECTION,
484 PARSERSTRING_NAME, /* TODO: only PARSERSTRING_NAME is used ? */
485};
486
Jes Sorensen464129e2015-05-05 18:37:12 -0400487static void
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400488parser_param_start(struct parser_context *ctx,
489 enum PARSER_WHICH_STRING which_string)
Erik Arfvidson46168812015-05-05 18:36:14 -0400490{
491 struct spar_controlvm_parameters_header *phdr = NULL;
492
493 if (ctx == NULL)
494 goto Away;
495 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
496 switch (which_string) {
497 case PARSERSTRING_INITIATOR:
498 ctx->curr = ctx->data + phdr->initiator_offset;
499 ctx->bytes_remaining = phdr->initiator_length;
500 break;
501 case PARSERSTRING_TARGET:
502 ctx->curr = ctx->data + phdr->target_offset;
503 ctx->bytes_remaining = phdr->target_length;
504 break;
505 case PARSERSTRING_CONNECTION:
506 ctx->curr = ctx->data + phdr->connection_offset;
507 ctx->bytes_remaining = phdr->connection_length;
508 break;
509 case PARSERSTRING_NAME:
510 ctx->curr = ctx->data + phdr->name_offset;
511 ctx->bytes_remaining = phdr->name_length;
512 break;
513 default:
514 break;
515 }
516
517Away:
518 return;
519}
520
Jes Sorensen464129e2015-05-05 18:37:12 -0400521static void parser_done(struct parser_context *ctx)
Erik Arfvidson46168812015-05-05 18:36:14 -0400522{
523 if (!ctx)
524 return;
525 controlvm_payload_bytes_buffered -= ctx->param_bytes;
526 kfree(ctx);
527}
528
Jes Sorensen464129e2015-05-05 18:37:12 -0400529static void *
Erik Arfvidson46168812015-05-05 18:36:14 -0400530parser_string_get(struct parser_context *ctx)
531{
532 u8 *pscan;
533 unsigned long nscan;
534 int value_length = -1;
535 void *value = NULL;
536 int i;
537
538 if (!ctx)
539 return NULL;
540 pscan = ctx->curr;
541 nscan = ctx->bytes_remaining;
542 if (nscan == 0)
543 return NULL;
544 if (!pscan)
545 return NULL;
546 for (i = 0, value_length = -1; i < nscan; i++)
547 if (pscan[i] == '\0') {
548 value_length = i;
549 break;
550 }
551 if (value_length < 0) /* '\0' was not included in the length */
552 value_length = nscan;
553 value = kmalloc(value_length + 1, GFP_KERNEL|__GFP_NORETRY);
554 if (value == NULL)
555 return NULL;
556 if (value_length > 0)
557 memcpy(value, pscan, value_length);
558 ((u8 *) (value))[value_length] = '\0';
559 return value;
560}
561
562
Vincent Bernatd746cb52014-08-01 10:29:30 +0200563static ssize_t toolaction_show(struct device *dev,
564 struct device_attribute *attr,
565 char *buf)
Benjamin Romer19f66342014-07-22 09:56:25 -0400566{
Benjamin Romer01f4d852015-03-16 13:58:48 -0400567 u8 tool_action;
Benjamin Romer19f66342014-07-22 09:56:25 -0400568
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400569 visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400570 offsetof(struct spar_controlvm_channel_protocol,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400571 tool_action), &tool_action, sizeof(u8));
Benjamin Romer01f4d852015-03-16 13:58:48 -0400572 return scnprintf(buf, PAGE_SIZE, "%u\n", tool_action);
Benjamin Romer19f66342014-07-22 09:56:25 -0400573}
574
Vincent Bernatd746cb52014-08-01 10:29:30 +0200575static ssize_t toolaction_store(struct device *dev,
576 struct device_attribute *attr,
577 const char *buf, size_t count)
Benjamin Romer19f66342014-07-22 09:56:25 -0400578{
Benjamin Romer01f4d852015-03-16 13:58:48 -0400579 u8 tool_action;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400580 int ret;
Benjamin Romer19f66342014-07-22 09:56:25 -0400581
Jes Sorensenebec8962015-05-05 18:35:57 -0400582 if (kstrtou8(buf, 10, &tool_action))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400583 return -EINVAL;
584
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400585 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400586 offsetof(struct spar_controlvm_channel_protocol,
587 tool_action),
Benjamin Romer01f4d852015-03-16 13:58:48 -0400588 &tool_action, sizeof(u8));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400589
590 if (ret)
591 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400592 return count;
Benjamin Romer19f66342014-07-22 09:56:25 -0400593}
594
Vincent Bernatd746cb52014-08-01 10:29:30 +0200595static ssize_t boottotool_show(struct device *dev,
596 struct device_attribute *attr,
597 char *buf)
Benjamin Romer54b31222014-07-22 09:56:26 -0400598{
Benjamin Romer365522d2015-03-16 13:58:49 -0400599 struct efi_spar_indication efi_spar_indication;
Benjamin Romer54b31222014-07-22 09:56:26 -0400600
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400601 visorchannel_read(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400602 offsetof(struct spar_controlvm_channel_protocol,
603 efi_spar_ind), &efi_spar_indication,
604 sizeof(struct efi_spar_indication));
Benjamin Romer54b31222014-07-22 09:56:26 -0400605 return scnprintf(buf, PAGE_SIZE, "%u\n",
Benjamin Romer8e76e692015-03-16 13:58:52 -0400606 efi_spar_indication.boot_to_tool);
Benjamin Romer54b31222014-07-22 09:56:26 -0400607}
608
Vincent Bernatd746cb52014-08-01 10:29:30 +0200609static ssize_t boottotool_store(struct device *dev,
610 struct device_attribute *attr,
611 const char *buf, size_t count)
Benjamin Romer54b31222014-07-22 09:56:26 -0400612{
Benjamin Romer66e24b72014-07-25 13:55:10 -0400613 int val, ret;
Benjamin Romer365522d2015-03-16 13:58:49 -0400614 struct efi_spar_indication efi_spar_indication;
Benjamin Romer54b31222014-07-22 09:56:26 -0400615
Jes Sorensenebec8962015-05-05 18:35:57 -0400616 if (kstrtoint(buf, 10, &val))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400617 return -EINVAL;
618
Benjamin Romer365522d2015-03-16 13:58:49 -0400619 efi_spar_indication.boot_to_tool = val;
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400620 ret = visorchannel_write(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400621 offsetof(struct spar_controlvm_channel_protocol,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400622 efi_spar_ind), &(efi_spar_indication),
623 sizeof(struct efi_spar_indication));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400624
625 if (ret)
626 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400627 return count;
Benjamin Romer54b31222014-07-22 09:56:26 -0400628}
Benjamin Romer422af172014-07-24 14:08:42 -0400629
630static ssize_t error_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400631 char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400632{
633 u32 error;
634
Benjamin Romer8e76e692015-03-16 13:58:52 -0400635 visorchannel_read(controlvm_channel,
636 offsetof(struct spar_controlvm_channel_protocol,
637 installation_error),
638 &error, sizeof(u32));
Benjamin Romer422af172014-07-24 14:08:42 -0400639 return scnprintf(buf, PAGE_SIZE, "%i\n", error);
640}
641
642static ssize_t error_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400643 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400644{
645 u32 error;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400646 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400647
Jes Sorensenebec8962015-05-05 18:35:57 -0400648 if (kstrtou32(buf, 10, &error))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400649 return -EINVAL;
650
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400651 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400652 offsetof(struct spar_controlvm_channel_protocol,
653 installation_error),
654 &error, sizeof(u32));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400655 if (ret)
656 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400657 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400658}
659
660static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400661 char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400662{
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400663 u32 text_id;
Benjamin Romer422af172014-07-24 14:08:42 -0400664
Benjamin Romer8e76e692015-03-16 13:58:52 -0400665 visorchannel_read(controlvm_channel,
666 offsetof(struct spar_controlvm_channel_protocol,
667 installation_text_id),
668 &text_id, sizeof(u32));
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400669 return scnprintf(buf, PAGE_SIZE, "%i\n", text_id);
Benjamin Romer422af172014-07-24 14:08:42 -0400670}
671
672static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400673 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400674{
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400675 u32 text_id;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400676 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400677
Jes Sorensenebec8962015-05-05 18:35:57 -0400678 if (kstrtou32(buf, 10, &text_id))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400679 return -EINVAL;
680
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400681 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400682 offsetof(struct spar_controlvm_channel_protocol,
683 installation_text_id),
684 &text_id, sizeof(u32));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400685 if (ret)
686 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400687 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400688}
689
Benjamin Romer422af172014-07-24 14:08:42 -0400690static ssize_t remaining_steps_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400691 struct device_attribute *attr, char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400692{
Benjamin Romeree8da292015-03-16 13:58:51 -0400693 u16 remaining_steps;
Benjamin Romer422af172014-07-24 14:08:42 -0400694
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400695 visorchannel_read(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400696 offsetof(struct spar_controlvm_channel_protocol,
697 installation_remaining_steps),
698 &remaining_steps, sizeof(u16));
Benjamin Romeree8da292015-03-16 13:58:51 -0400699 return scnprintf(buf, PAGE_SIZE, "%hu\n", remaining_steps);
Benjamin Romer422af172014-07-24 14:08:42 -0400700}
701
702static ssize_t remaining_steps_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400703 struct device_attribute *attr,
704 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400705{
Benjamin Romeree8da292015-03-16 13:58:51 -0400706 u16 remaining_steps;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400707 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400708
Jes Sorensenebec8962015-05-05 18:35:57 -0400709 if (kstrtou16(buf, 10, &remaining_steps))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400710 return -EINVAL;
711
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400712 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400713 offsetof(struct spar_controlvm_channel_protocol,
714 installation_remaining_steps),
715 &remaining_steps, sizeof(u16));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400716 if (ret)
717 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400718 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400719}
720
Ken Cox12e364b2014-03-04 07:58:07 -0600721static void
Benjamin Romer9b989a982015-03-16 13:58:11 -0400722bus_info_clear(void *v)
Ken Cox12e364b2014-03-04 07:58:07 -0600723{
Jes Sorensenbbd4be32015-05-05 18:35:43 -0400724 struct visorchipset_bus_info *p = (struct visorchipset_bus_info *) v;
Ken Cox12e364b2014-03-04 07:58:07 -0600725
Ken Cox12e364b2014-03-04 07:58:07 -0600726 kfree(p->name);
Ken Cox12e364b2014-03-04 07:58:07 -0600727 kfree(p->description);
Benjamin Romer33192fa2014-10-31 09:57:27 -0400728 memset(p, 0, sizeof(struct visorchipset_bus_info));
Ken Cox12e364b2014-03-04 07:58:07 -0600729}
730
731static void
Benjamin Romer9b989a982015-03-16 13:58:11 -0400732dev_info_clear(void *v)
Ken Cox12e364b2014-03-04 07:58:07 -0600733{
Benjamin Romer246e0cd2014-10-31 09:57:24 -0400734 struct visorchipset_device_info *p =
Jes Sorensenbbd4be32015-05-05 18:35:43 -0400735 (struct visorchipset_device_info *) v;
Benjamin Romer26eb2c02014-08-18 09:34:53 -0400736
Benjamin Romer246e0cd2014-10-31 09:57:24 -0400737 memset(p, 0, sizeof(struct visorchipset_device_info));
Ken Cox12e364b2014-03-04 07:58:07 -0600738}
739
Jes Sorensen4f665202015-05-05 18:35:52 -0400740static struct visorchipset_bus_info *
741bus_find(struct list_head *list, u32 bus_no)
742{
743 struct visorchipset_bus_info *p;
744
745 list_for_each_entry(p, list, entry) {
746 if (p->bus_no == bus_no)
747 return p;
748 }
749
750 return NULL;
751}
752
Jes Sorensend480f6a2015-05-05 18:35:54 -0400753static struct visorchipset_device_info *
754device_find(struct list_head *list, u32 bus_no, u32 dev_no)
755{
756 struct visorchipset_device_info *p;
757
758 list_for_each_entry(p, list, entry) {
759 if (p->bus_no == bus_no && p->dev_no == dev_no)
760 return p;
761 }
762
763 return NULL;
764}
765
Jes Sorensen28723522015-05-05 18:35:55 -0400766static void busdevices_del(struct list_head *list, u32 bus_no)
767{
768 struct visorchipset_device_info *p, *tmp;
769
770 list_for_each_entry_safe(p, tmp, list, entry) {
771 if (p->bus_no == bus_no) {
772 list_del(&p->entry);
773 kfree(p);
774 }
775 }
776}
777
Benjamin Romerc2422332014-07-29 15:09:40 -0400778static u8
Ken Cox12e364b2014-03-04 07:58:07 -0600779check_chipset_events(void)
780{
781 int i;
Benjamin Romerc2422332014-07-29 15:09:40 -0400782 u8 send_msg = 1;
Ken Cox12e364b2014-03-04 07:58:07 -0600783 /* Check events to determine if response should be sent */
784 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
785 send_msg &= chipset_events[i];
786 return send_msg;
787}
788
789static void
790clear_chipset_events(void)
791{
792 int i;
793 /* Clear chipset_events */
794 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
795 chipset_events[i] = 0;
796}
797
798void
David Kershner4da33362015-05-05 18:36:39 -0400799visorchipset_register_busdev(
Benjamin Romerfe90d892014-10-31 09:57:32 -0400800 struct visorchipset_busdev_notifiers *notifiers,
Benjamin Romer929aa8a2014-10-31 09:57:33 -0400801 struct visorchipset_busdev_responders *responders,
Benjamin Romer1e7a59c2014-10-31 09:57:35 -0400802 struct ultra_vbus_deviceinfo *driver_info)
Ken Cox12e364b2014-03-04 07:58:07 -0600803{
Benjamin Romer8f1947a2015-03-16 13:57:59 -0400804 down(&notifier_lock);
Benjamin Romer38f736e2015-03-16 13:58:13 -0400805 if (!notifiers) {
David Kershner4da33362015-05-05 18:36:39 -0400806 memset(&busdev_notifiers, 0,
807 sizeof(busdev_notifiers));
808 visorbusregistered = 0; /* clear flag */
Ken Cox12e364b2014-03-04 07:58:07 -0600809 } else {
David Kershner4da33362015-05-05 18:36:39 -0400810 busdev_notifiers = *notifiers;
811 visorbusregistered = 1; /* set flag */
Ken Cox12e364b2014-03-04 07:58:07 -0600812 }
813 if (responders)
Benjamin Romer8e3fedd2015-03-16 13:58:43 -0400814 *responders = busdev_responders;
Benjamin Romer1e7a59c2014-10-31 09:57:35 -0400815 if (driver_info)
816 bus_device_info_init(driver_info, "chipset", "visorchipset",
Benjamin Romer8e76e692015-03-16 13:58:52 -0400817 VERSION, NULL);
Ken Cox12e364b2014-03-04 07:58:07 -0600818
Benjamin Romer8f1947a2015-03-16 13:57:59 -0400819 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -0600820}
David Kershner4da33362015-05-05 18:36:39 -0400821EXPORT_SYMBOL_GPL(visorchipset_register_busdev);
Ken Cox12e364b2014-03-04 07:58:07 -0600822
823static void
824cleanup_controlvm_structures(void)
825{
Benjamin Romer33192fa2014-10-31 09:57:27 -0400826 struct visorchipset_bus_info *bi, *tmp_bi;
Benjamin Romer246e0cd2014-10-31 09:57:24 -0400827 struct visorchipset_device_info *di, *tmp_di;
Ken Cox12e364b2014-03-04 07:58:07 -0600828
Benjamin Romer1390b882015-03-16 13:58:03 -0400829 list_for_each_entry_safe(bi, tmp_bi, &bus_info_list, entry) {
Benjamin Romer9b989a982015-03-16 13:58:11 -0400830 bus_info_clear(bi);
Ken Cox12e364b2014-03-04 07:58:07 -0600831 list_del(&bi->entry);
832 kfree(bi);
833 }
834
Benjamin Romer1390b882015-03-16 13:58:03 -0400835 list_for_each_entry_safe(di, tmp_di, &dev_info_list, entry) {
Benjamin Romer9b989a982015-03-16 13:58:11 -0400836 dev_info_clear(di);
Ken Cox12e364b2014-03-04 07:58:07 -0600837 list_del(&di->entry);
838 kfree(di);
839 }
840}
841
842static void
Benjamin Romer3ab47702014-10-23 14:30:31 -0400843chipset_init(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -0600844{
845 static int chipset_inited;
Benjamin Romerb9b141e2014-10-23 14:30:24 -0400846 enum ultra_chipset_feature features = 0;
Ken Cox12e364b2014-03-04 07:58:07 -0600847 int rc = CONTROLVM_RESP_SUCCESS;
848
849 POSTCODE_LINUX_2(CHIPSET_INIT_ENTRY_PC, POSTCODE_SEVERITY_INFO);
850 if (chipset_inited) {
Ken Cox22ad57b2014-03-19 13:06:25 -0500851 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romere3199b22015-03-16 13:58:14 -0400852 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -0600853 }
854 chipset_inited = 1;
855 POSTCODE_LINUX_2(CHIPSET_INIT_EXIT_PC, POSTCODE_SEVERITY_INFO);
856
857 /* Set features to indicate we support parahotplug (if Command
858 * also supports it). */
859 features =
Benjamin Romer2ea51172014-10-23 14:30:25 -0400860 inmsg->cmd.init_chipset.
Ken Cox12e364b2014-03-04 07:58:07 -0600861 features & ULTRA_CHIPSET_FEATURE_PARA_HOTPLUG;
862
863 /* Set the "reply" bit so Command knows this is a
864 * features-aware driver. */
865 features |= ULTRA_CHIPSET_FEATURE_REPLY;
866
Benjamin Romere3199b22015-03-16 13:58:14 -0400867cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -0600868 if (rc < 0)
869 cleanup_controlvm_structures();
Benjamin Romer98d7b592014-10-23 14:30:26 -0400870 if (inmsg->hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -0600871 controlvm_respond_chipset_init(&inmsg->hdr, rc, features);
872}
873
874static void
Benjamin Romer3ab47702014-10-23 14:30:31 -0400875controlvm_init_response(struct controlvm_message *msg,
Benjamin Romerb3168c72015-03-16 13:58:46 -0400876 struct controlvm_message_header *msg_hdr, int response)
Ken Cox12e364b2014-03-04 07:58:07 -0600877{
Benjamin Romer3ab47702014-10-23 14:30:31 -0400878 memset(msg, 0, sizeof(struct controlvm_message));
Benjamin Romerb3168c72015-03-16 13:58:46 -0400879 memcpy(&msg->hdr, msg_hdr, sizeof(struct controlvm_message_header));
Benjamin Romer98d7b592014-10-23 14:30:26 -0400880 msg->hdr.payload_bytes = 0;
881 msg->hdr.payload_vm_offset = 0;
882 msg->hdr.payload_max_bytes = 0;
Ken Cox12e364b2014-03-04 07:58:07 -0600883 if (response < 0) {
Benjamin Romer98d7b592014-10-23 14:30:26 -0400884 msg->hdr.flags.failed = 1;
885 msg->hdr.completion_status = (u32) (-response);
Ken Cox12e364b2014-03-04 07:58:07 -0600886 }
887}
888
889static void
Benjamin Romerb3168c72015-03-16 13:58:46 -0400890controlvm_respond(struct controlvm_message_header *msg_hdr, int response)
Ken Cox12e364b2014-03-04 07:58:07 -0600891{
Benjamin Romer3ab47702014-10-23 14:30:31 -0400892 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -0400893
Benjamin Romerb3168c72015-03-16 13:58:46 -0400894 controlvm_init_response(&outmsg, msg_hdr, response);
Ken Cox12e364b2014-03-04 07:58:07 -0600895 /* For DiagPool channel DEVICE_CHANGESTATE, we need to send
896 * back the deviceChangeState structure in the packet. */
Benjamin Romerb3168c72015-03-16 13:58:46 -0400897 if (msg_hdr->id == CONTROLVM_DEVICE_CHANGESTATE &&
Benjamin Romer0639ba62015-03-16 13:58:04 -0400898 g_devicechangestate_packet.device_change_state.bus_no ==
899 g_diagpool_bus_no &&
900 g_devicechangestate_packet.device_change_state.dev_no ==
Benjamin Romer83d48902015-03-16 13:58:01 -0400901 g_diagpool_dev_no)
Benjamin Romer4f44b722015-03-16 13:58:02 -0400902 outmsg.cmd = g_devicechangestate_packet;
Benjamin Romer2098dbd2015-03-04 12:14:22 -0500903 if (outmsg.hdr.flags.test_message == 1)
Ken Cox12e364b2014-03-04 07:58:07 -0600904 return;
Benjamin Romer2098dbd2015-03-04 12:14:22 -0500905
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400906 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -0600907 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -0600908 return;
909 }
910}
911
912static void
Benjamin Romerb3168c72015-03-16 13:58:46 -0400913controlvm_respond_chipset_init(struct controlvm_message_header *msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400914 int response,
Benjamin Romerb9b141e2014-10-23 14:30:24 -0400915 enum ultra_chipset_feature features)
Ken Cox12e364b2014-03-04 07:58:07 -0600916{
Benjamin Romer3ab47702014-10-23 14:30:31 -0400917 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -0400918
Benjamin Romerb3168c72015-03-16 13:58:46 -0400919 controlvm_init_response(&outmsg, msg_hdr, response);
Benjamin Romer2ea51172014-10-23 14:30:25 -0400920 outmsg.cmd.init_chipset.features = features;
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400921 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -0600922 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -0600923 return;
924 }
925}
926
Benjamin Romer98d7b592014-10-23 14:30:26 -0400927static void controlvm_respond_physdev_changestate(
Benjamin Romerb3168c72015-03-16 13:58:46 -0400928 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400929 struct spar_segment_state state)
Ken Cox12e364b2014-03-04 07:58:07 -0600930{
Benjamin Romer3ab47702014-10-23 14:30:31 -0400931 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -0400932
Benjamin Romerb3168c72015-03-16 13:58:46 -0400933 controlvm_init_response(&outmsg, msg_hdr, response);
Benjamin Romer2ea51172014-10-23 14:30:25 -0400934 outmsg.cmd.device_change_state.state = state;
935 outmsg.cmd.device_change_state.flags.phys_device = 1;
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400936 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -0600937 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -0600938 return;
939 }
940}
941
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400942enum crash_obj_type {
943 CRASH_DEV,
944 CRASH_BUS,
945};
946
Ken Cox12e364b2014-03-04 07:58:07 -0600947void
Benjamin Romer2c683cd2014-10-31 09:57:22 -0400948visorchipset_save_message(struct controlvm_message *msg,
949 enum crash_obj_type type)
Ken Cox12e364b2014-03-04 07:58:07 -0600950{
Benjamin Romer45772252015-03-16 13:58:15 -0400951 u32 crash_msg_offset;
952 u16 crash_msg_count;
Ken Cox12e364b2014-03-04 07:58:07 -0600953
954 /* get saved message count */
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400955 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400956 offsetof(struct spar_controlvm_channel_protocol,
957 saved_crash_message_count),
Benjamin Romer45772252015-03-16 13:58:15 -0400958 &crash_msg_count, sizeof(u16)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -0600959 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
960 POSTCODE_SEVERITY_ERR);
961 return;
962 }
963
Benjamin Romer45772252015-03-16 13:58:15 -0400964 if (crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
Ken Cox12e364b2014-03-04 07:58:07 -0600965 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
Benjamin Romer45772252015-03-16 13:58:15 -0400966 crash_msg_count,
Ken Cox12e364b2014-03-04 07:58:07 -0600967 POSTCODE_SEVERITY_ERR);
968 return;
969 }
970
971 /* get saved crash message offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400972 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400973 offsetof(struct spar_controlvm_channel_protocol,
974 saved_crash_message_offset),
Benjamin Romer45772252015-03-16 13:58:15 -0400975 &crash_msg_offset, sizeof(u32)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -0600976 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
977 POSTCODE_SEVERITY_ERR);
978 return;
979 }
980
Benjamin Romer2c683cd2014-10-31 09:57:22 -0400981 if (type == CRASH_BUS) {
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400982 if (visorchannel_write(controlvm_channel,
Benjamin Romer45772252015-03-16 13:58:15 -0400983 crash_msg_offset,
Benjamin Romer3ab47702014-10-23 14:30:31 -0400984 msg,
985 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -0600986 POSTCODE_LINUX_2(SAVE_MSG_BUS_FAILURE_PC,
987 POSTCODE_SEVERITY_ERR);
988 return;
989 }
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400990 } else { /* CRASH_DEV */
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400991 if (visorchannel_write(controlvm_channel,
Benjamin Romer45772252015-03-16 13:58:15 -0400992 crash_msg_offset +
Benjamin Romer3ab47702014-10-23 14:30:31 -0400993 sizeof(struct controlvm_message), msg,
994 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -0600995 POSTCODE_LINUX_2(SAVE_MSG_DEV_FAILURE_PC,
996 POSTCODE_SEVERITY_ERR);
997 return;
998 }
999 }
1000}
1001EXPORT_SYMBOL_GPL(visorchipset_save_message);
1002
1003static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04001004bus_responder(enum controlvm_id cmd_id, u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001005{
Jes Sorensene82ba622015-05-05 18:35:45 -04001006 struct visorchipset_bus_info *p;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001007 bool need_clear = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001008
Jes Sorensen4f665202015-05-05 18:35:52 -04001009 p = bus_find(&bus_info_list, bus_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001010 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001011 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001012
Ken Cox12e364b2014-03-04 07:58:07 -06001013 if (response < 0) {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001014 if ((cmd_id == CONTROLVM_BUS_CREATE) &&
Ken Cox12e364b2014-03-04 07:58:07 -06001015 (response != (-CONTROLVM_RESP_ERROR_ALREADY_DONE)))
1016 /* undo the row we just created... */
Jes Sorensen28723522015-05-05 18:35:55 -04001017 busdevices_del(&dev_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001018 } else {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001019 if (cmd_id == CONTROLVM_BUS_CREATE)
Ken Cox12e364b2014-03-04 07:58:07 -06001020 p->state.created = 1;
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001021 if (cmd_id == CONTROLVM_BUS_DESTROY)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001022 need_clear = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001023 }
1024
Benjamin Romer0aca78442015-03-04 12:14:25 -05001025 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001026 return; /* no controlvm response needed */
Benjamin Romer6b59b312015-03-16 13:58:18 -04001027 if (p->pending_msg_hdr.id != (u32)cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001028 return;
Benjamin Romer33192fa2014-10-31 09:57:27 -04001029 controlvm_respond(&p->pending_msg_hdr, response);
1030 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001031 if (need_clear) {
Benjamin Romer9b989a982015-03-16 13:58:11 -04001032 bus_info_clear(p);
Jes Sorensen28723522015-05-05 18:35:55 -04001033 busdevices_del(&dev_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001034 }
1035}
1036
1037static void
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001038device_changestate_responder(enum controlvm_id cmd_id,
Jes Sorensen52063ec2015-04-13 10:28:41 -04001039 u32 bus_no, u32 dev_no, int response,
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001040 struct spar_segment_state response_state)
Ken Cox12e364b2014-03-04 07:58:07 -06001041{
Jes Sorensene82ba622015-05-05 18:35:45 -04001042 struct visorchipset_device_info *p;
Benjamin Romer3ab47702014-10-23 14:30:31 -04001043 struct controlvm_message outmsg;
Ken Cox12e364b2014-03-04 07:58:07 -06001044
Jes Sorensend480f6a2015-05-05 18:35:54 -04001045 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001046 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001047 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001048 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001049 return; /* no controlvm response needed */
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001050 if (p->pending_msg_hdr.id != cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001051 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001052
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001053 controlvm_init_response(&outmsg, &p->pending_msg_hdr, response);
Ken Cox12e364b2014-03-04 07:58:07 -06001054
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001055 outmsg.cmd.device_change_state.bus_no = bus_no;
1056 outmsg.cmd.device_change_state.dev_no = dev_no;
1057 outmsg.cmd.device_change_state.state = response_state;
Ken Cox12e364b2014-03-04 07:58:07 -06001058
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001059 if (!visorchannel_signalinsert(controlvm_channel,
Benjamin Romer0aca78442015-03-04 12:14:25 -05001060 CONTROLVM_QUEUE_REQUEST, &outmsg))
Ken Cox12e364b2014-03-04 07:58:07 -06001061 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001062
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001063 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001064}
1065
1066static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04001067device_responder(enum controlvm_id cmd_id, u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001068{
Jes Sorensene82ba622015-05-05 18:35:45 -04001069 struct visorchipset_device_info *p;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001070 bool need_clear = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001071
Jes Sorensend480f6a2015-05-05 18:35:54 -04001072 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001073 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001074 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001075 if (response >= 0) {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001076 if (cmd_id == CONTROLVM_DEVICE_CREATE)
Ken Cox12e364b2014-03-04 07:58:07 -06001077 p->state.created = 1;
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001078 if (cmd_id == CONTROLVM_DEVICE_DESTROY)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001079 need_clear = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001080 }
1081
Benjamin Romer0aca78442015-03-04 12:14:25 -05001082 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001083 return; /* no controlvm response needed */
Benjamin Romer0aca78442015-03-04 12:14:25 -05001084
Benjamin Romer6b59b312015-03-16 13:58:18 -04001085 if (p->pending_msg_hdr.id != (u32)cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001086 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001087
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001088 controlvm_respond(&p->pending_msg_hdr, response);
1089 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001090 if (need_clear)
Benjamin Romer9b989a982015-03-16 13:58:11 -04001091 dev_info_clear(p);
Ken Cox12e364b2014-03-04 07:58:07 -06001092}
1093
1094static void
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001095bus_epilog(u32 bus_no,
1096 u32 cmd, struct controlvm_message_header *msg_hdr,
Jes Sorensenf4c11552015-04-13 10:28:40 -04001097 int response, bool need_response)
Ken Cox12e364b2014-03-04 07:58:07 -06001098{
Jes Sorensen4f665202015-05-05 18:35:52 -04001099 struct visorchipset_bus_info *bus_info;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001100 bool notified = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001101
Jes Sorensen4f665202015-05-05 18:35:52 -04001102 bus_info = bus_find(&bus_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001103
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001104 if (!bus_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001105 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001106
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001107 if (need_response) {
1108 memcpy(&bus_info->pending_msg_hdr, msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001109 sizeof(struct controlvm_message_header));
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001110 } else {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001111 bus_info->pending_msg_hdr.id = CONTROLVM_INVALID;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001112 }
Ken Cox12e364b2014-03-04 07:58:07 -06001113
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001114 down(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001115 if (response == CONTROLVM_RESP_SUCCESS) {
1116 switch (cmd) {
1117 case CONTROLVM_BUS_CREATE:
David Kershner4da33362015-05-05 18:36:39 -04001118 if (busdev_notifiers.bus_create) {
1119 (*busdev_notifiers.bus_create) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001120 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001121 }
1122 break;
1123 case CONTROLVM_BUS_DESTROY:
David Kershner4da33362015-05-05 18:36:39 -04001124 if (busdev_notifiers.bus_destroy) {
1125 (*busdev_notifiers.bus_destroy) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001126 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001127 }
1128 break;
1129 }
1130 }
1131 if (notified)
1132 /* The callback function just called above is responsible
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001133 * for calling the appropriate visorchipset_busdev_responders
Ken Cox12e364b2014-03-04 07:58:07 -06001134 * function, which will call bus_responder()
1135 */
1136 ;
1137 else
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001138 bus_responder(cmd, bus_no, response);
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001139 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001140}
1141
1142static void
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001143device_epilog(u32 bus_no, u32 dev_no, struct spar_segment_state state, u32 cmd,
1144 struct controlvm_message_header *msg_hdr, int response,
Jes Sorensenf4c11552015-04-13 10:28:40 -04001145 bool need_response, bool for_visorbus)
Ken Cox12e364b2014-03-04 07:58:07 -06001146{
Jes Sorensene82ba622015-05-05 18:35:45 -04001147 struct visorchipset_busdev_notifiers *notifiers;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001148 bool notified = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001149
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001150 struct visorchipset_device_info *dev_info =
Jes Sorensend480f6a2015-05-05 18:35:54 -04001151 device_find(&dev_info_list, bus_no, dev_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001152 char *envp[] = {
1153 "SPARSP_DIAGPOOL_PAUSED_STATE = 1",
1154 NULL
1155 };
1156
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001157 if (!dev_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001158 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001159
David Kershner4da33362015-05-05 18:36:39 -04001160 notifiers = &busdev_notifiers;
1161
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001162 if (need_response) {
1163 memcpy(&dev_info->pending_msg_hdr, msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001164 sizeof(struct controlvm_message_header));
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001165 } else {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001166 dev_info->pending_msg_hdr.id = CONTROLVM_INVALID;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001167 }
Ken Cox12e364b2014-03-04 07:58:07 -06001168
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001169 down(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001170 if (response >= 0) {
1171 switch (cmd) {
1172 case CONTROLVM_DEVICE_CREATE:
1173 if (notifiers->device_create) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001174 (*notifiers->device_create) (bus_no, dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001175 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001176 }
1177 break;
1178 case CONTROLVM_DEVICE_CHANGESTATE:
1179 /* ServerReady / ServerRunning / SegmentStateRunning */
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001180 if (state.alive == segment_state_running.alive &&
1181 state.operating ==
1182 segment_state_running.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001183 if (notifiers->device_resume) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001184 (*notifiers->device_resume) (bus_no,
1185 dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001186 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001187 }
1188 }
1189 /* ServerNotReady / ServerLost / SegmentStateStandby */
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001190 else if (state.alive == segment_state_standby.alive &&
Benjamin Romer3f833b52014-10-23 14:30:12 -04001191 state.operating ==
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001192 segment_state_standby.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001193 /* technically this is standby case
1194 * where server is lost
1195 */
1196 if (notifiers->device_pause) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001197 (*notifiers->device_pause) (bus_no,
1198 dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001199 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001200 }
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001201 } else if (state.alive == segment_state_paused.alive &&
Benjamin Romer3f833b52014-10-23 14:30:12 -04001202 state.operating ==
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001203 segment_state_paused.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001204 /* this is lite pause where channel is
1205 * still valid just 'pause' of it
1206 */
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001207 if (bus_no == g_diagpool_bus_no &&
1208 dev_no == g_diagpool_dev_no) {
Ken Cox12e364b2014-03-04 07:58:07 -06001209 /* this will trigger the
1210 * diag_shutdown.sh script in
1211 * the visorchipset hotplug */
1212 kobject_uevent_env
Benjamin Romereb34e872015-03-16 13:58:45 -04001213 (&visorchipset_platform_device.dev.
Ken Cox12e364b2014-03-04 07:58:07 -06001214 kobj, KOBJ_ONLINE, envp);
1215 }
1216 }
1217 break;
1218 case CONTROLVM_DEVICE_DESTROY:
1219 if (notifiers->device_destroy) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001220 (*notifiers->device_destroy) (bus_no, dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001221 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001222 }
1223 break;
1224 }
1225 }
1226 if (notified)
1227 /* The callback function just called above is responsible
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001228 * for calling the appropriate visorchipset_busdev_responders
Ken Cox12e364b2014-03-04 07:58:07 -06001229 * function, which will call device_responder()
1230 */
1231 ;
1232 else
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001233 device_responder(cmd, bus_no, dev_no, response);
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001234 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001235}
1236
1237static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001238bus_create(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001239{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001240 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001241 u32 bus_no = cmd->create_bus.bus_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001242 int rc = CONTROLVM_RESP_SUCCESS;
Jes Sorensene82ba622015-05-05 18:35:45 -04001243 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001244
Jes Sorensen4f665202015-05-05 18:35:52 -04001245 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001246 if (bus_info && (bus_info->state.created == 1)) {
1247 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001248 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001249 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001250 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001251 }
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001252 bus_info = kzalloc(sizeof(*bus_info), GFP_KERNEL);
1253 if (!bus_info) {
1254 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001255 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001256 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001257 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001258 }
1259
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001260 INIT_LIST_HEAD(&bus_info->entry);
1261 bus_info->bus_no = bus_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001262
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001263 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001264
Benjamin Romer98d7b592014-10-23 14:30:26 -04001265 if (inmsg->hdr.flags.test_message == 1)
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001266 bus_info->chan_info.addr_type = ADDRTYPE_LOCALTEST;
Ken Cox12e364b2014-03-04 07:58:07 -06001267 else
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001268 bus_info->chan_info.addr_type = ADDRTYPE_LOCALPHYSICAL;
Ken Cox12e364b2014-03-04 07:58:07 -06001269
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001270 bus_info->flags.server = inmsg->hdr.flags.server;
1271 bus_info->chan_info.channel_addr = cmd->create_bus.channel_addr;
1272 bus_info->chan_info.n_channel_bytes = cmd->create_bus.channel_bytes;
1273 bus_info->chan_info.channel_type_uuid =
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001274 cmd->create_bus.bus_data_type_uuid;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001275 bus_info->chan_info.channel_inst_uuid = cmd->create_bus.bus_inst_uuid;
Ken Cox12e364b2014-03-04 07:58:07 -06001276
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001277 list_add(&bus_info->entry, &bus_info_list);
Ken Cox12e364b2014-03-04 07:58:07 -06001278
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001279 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001280
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001281cleanup:
1282 bus_epilog(bus_no, CONTROLVM_BUS_CREATE, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001283 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001284}
1285
1286static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001287bus_destroy(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001288{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001289 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001290 u32 bus_no = cmd->destroy_bus.bus_no;
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001291 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001292 int rc = CONTROLVM_RESP_SUCCESS;
1293
Jes Sorensen4f665202015-05-05 18:35:52 -04001294 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001295 if (!bus_info)
Ken Cox22ad57b2014-03-19 13:06:25 -05001296 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001297 else if (bus_info->state.created == 0)
Ken Cox22ad57b2014-03-19 13:06:25 -05001298 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Ken Cox12e364b2014-03-04 07:58:07 -06001299
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001300 bus_epilog(bus_no, CONTROLVM_BUS_DESTROY, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001301 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001302}
1303
1304static void
Benjamin Romer317d9612015-03-16 13:57:51 -04001305bus_configure(struct controlvm_message *inmsg,
1306 struct parser_context *parser_ctx)
Ken Cox12e364b2014-03-04 07:58:07 -06001307{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001308 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensene82ba622015-05-05 18:35:45 -04001309 u32 bus_no;
1310 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001311 int rc = CONTROLVM_RESP_SUCCESS;
1312 char s[99];
1313
Benjamin Romer654bada2015-03-16 13:58:22 -04001314 bus_no = cmd->configure_bus.bus_no;
1315 POSTCODE_LINUX_3(BUS_CONFIGURE_ENTRY_PC, bus_no,
1316 POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001317
Jes Sorensen4f665202015-05-05 18:35:52 -04001318 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romer654bada2015-03-16 13:58:22 -04001319 if (!bus_info) {
1320 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001321 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001322 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romer654bada2015-03-16 13:58:22 -04001323 } else if (bus_info->state.created == 0) {
1324 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001325 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001326 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romer654bada2015-03-16 13:58:22 -04001327 } else if (bus_info->pending_msg_hdr.id != CONTROLVM_INVALID) {
1328 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001329 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001330 rc = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
Benjamin Romer654bada2015-03-16 13:58:22 -04001331 } else {
1332 bus_info->partition_handle = cmd->configure_bus.guest_handle;
1333 bus_info->partition_uuid = parser_id_get(parser_ctx);
1334 parser_param_start(parser_ctx, PARSERSTRING_NAME);
1335 bus_info->name = parser_string_get(parser_ctx);
1336
1337 visorchannel_uuid_id(&bus_info->partition_uuid, s);
1338 POSTCODE_LINUX_3(BUS_CONFIGURE_EXIT_PC, bus_no,
1339 POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001340 }
Benjamin Romer654bada2015-03-16 13:58:22 -04001341 bus_epilog(bus_no, CONTROLVM_BUS_CONFIGURE, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001342 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001343}
1344
1345static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001346my_device_create(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001347{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001348 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001349 u32 bus_no = cmd->create_device.bus_no;
1350 u32 dev_no = cmd->create_device.dev_no;
Jes Sorensene82ba622015-05-05 18:35:45 -04001351 struct visorchipset_device_info *dev_info;
1352 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001353 int rc = CONTROLVM_RESP_SUCCESS;
1354
Jes Sorensend480f6a2015-05-05 18:35:54 -04001355 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001356 if (dev_info && (dev_info->state.created == 1)) {
1357 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001358 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001359 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001360 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001361 }
Jes Sorensen4f665202015-05-05 18:35:52 -04001362 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001363 if (!bus_info) {
1364 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001365 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001366 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001367 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001368 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001369 if (bus_info->state.created == 0) {
1370 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001371 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001372 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001373 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001374 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001375 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
1376 if (!dev_info) {
1377 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001378 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001379 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001380 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001381 }
Andreea-Cristina Bernat97a84f12014-03-14 04:20:06 +02001382
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001383 INIT_LIST_HEAD(&dev_info->entry);
1384 dev_info->bus_no = bus_no;
1385 dev_info->dev_no = dev_no;
1386 dev_info->dev_inst_uuid = cmd->create_device.dev_inst_uuid;
1387 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001388 POSTCODE_SEVERITY_INFO);
1389
Benjamin Romer98d7b592014-10-23 14:30:26 -04001390 if (inmsg->hdr.flags.test_message == 1)
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001391 dev_info->chan_info.addr_type = ADDRTYPE_LOCALTEST;
Ken Cox12e364b2014-03-04 07:58:07 -06001392 else
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001393 dev_info->chan_info.addr_type = ADDRTYPE_LOCALPHYSICAL;
1394 dev_info->chan_info.channel_addr = cmd->create_device.channel_addr;
1395 dev_info->chan_info.n_channel_bytes = cmd->create_device.channel_bytes;
1396 dev_info->chan_info.channel_type_uuid =
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001397 cmd->create_device.data_type_uuid;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001398 dev_info->chan_info.intr = cmd->create_device.intr;
1399 list_add(&dev_info->entry, &dev_info_list);
1400 POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001401 POSTCODE_SEVERITY_INFO);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001402cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001403 /* get the bus and devNo for DiagPool channel */
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001404 if (dev_info &&
1405 is_diagpool_channel(dev_info->chan_info.channel_type_uuid)) {
1406 g_diagpool_bus_no = bus_no;
1407 g_diagpool_dev_no = dev_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001408 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001409 device_epilog(bus_no, dev_no, segment_state_running,
Ken Cox12e364b2014-03-04 07:58:07 -06001410 CONTROLVM_DEVICE_CREATE, &inmsg->hdr, rc,
David Kershner4da33362015-05-05 18:36:39 -04001411 inmsg->hdr.flags.response_expected == 1, 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001412}
1413
1414static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001415my_device_changestate(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001416{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001417 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001418 u32 bus_no = cmd->device_change_state.bus_no;
1419 u32 dev_no = cmd->device_change_state.dev_no;
Benjamin Romer2ea51172014-10-23 14:30:25 -04001420 struct spar_segment_state state = cmd->device_change_state.state;
Jes Sorensene82ba622015-05-05 18:35:45 -04001421 struct visorchipset_device_info *dev_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001422 int rc = CONTROLVM_RESP_SUCCESS;
1423
Jes Sorensend480f6a2015-05-05 18:35:54 -04001424 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0278a902015-03-16 13:58:24 -04001425 if (!dev_info) {
1426 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001427 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001428 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Benjamin Romer0278a902015-03-16 13:58:24 -04001429 } else if (dev_info->state.created == 0) {
1430 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001431 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001432 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001433 }
Benjamin Romer0278a902015-03-16 13:58:24 -04001434 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
1435 device_epilog(bus_no, dev_no, state,
1436 CONTROLVM_DEVICE_CHANGESTATE, &inmsg->hdr, rc,
David Kershner4da33362015-05-05 18:36:39 -04001437 inmsg->hdr.flags.response_expected == 1, 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001438}
1439
1440static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001441my_device_destroy(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001442{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001443 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001444 u32 bus_no = cmd->destroy_device.bus_no;
1445 u32 dev_no = cmd->destroy_device.dev_no;
Jes Sorensene82ba622015-05-05 18:35:45 -04001446 struct visorchipset_device_info *dev_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001447 int rc = CONTROLVM_RESP_SUCCESS;
1448
Jes Sorensend480f6a2015-05-05 18:35:54 -04001449 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer61715c82015-03-16 13:58:25 -04001450 if (!dev_info)
Ken Cox22ad57b2014-03-19 13:06:25 -05001451 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Benjamin Romer61715c82015-03-16 13:58:25 -04001452 else if (dev_info->state.created == 0)
Ken Cox22ad57b2014-03-19 13:06:25 -05001453 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Ken Cox12e364b2014-03-04 07:58:07 -06001454
Benjamin Romer61715c82015-03-16 13:58:25 -04001455 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
1456 device_epilog(bus_no, dev_no, segment_state_running,
Ken Cox12e364b2014-03-04 07:58:07 -06001457 CONTROLVM_DEVICE_DESTROY, &inmsg->hdr, rc,
David Kershner4da33362015-05-05 18:36:39 -04001458 inmsg->hdr.flags.response_expected == 1, 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001459}
1460
1461/* When provided with the physical address of the controlvm channel
1462 * (phys_addr), the offset to the payload area we need to manage
1463 * (offset), and the size of this payload area (bytes), fills in the
Jes Sorensenf4c11552015-04-13 10:28:40 -04001464 * controlvm_payload_info struct. Returns true for success or false
Ken Cox12e364b2014-03-04 07:58:07 -06001465 * for failure.
1466 */
1467static int
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04001468initialize_controlvm_payload_info(u64 phys_addr, u64 offset, u32 bytes,
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001469 struct visor_controlvm_payload_info *info)
Ken Cox12e364b2014-03-04 07:58:07 -06001470{
Benjamin Romerc2422332014-07-29 15:09:40 -04001471 u8 __iomem *payload = NULL;
Ken Cox12e364b2014-03-04 07:58:07 -06001472 int rc = CONTROLVM_RESP_SUCCESS;
1473
Benjamin Romer38f736e2015-03-16 13:58:13 -04001474 if (!info) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001475 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
Benjamin Romerf118a392015-03-16 13:58:26 -04001476 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001477 }
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001478 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
Ken Cox12e364b2014-03-04 07:58:07 -06001479 if ((offset == 0) || (bytes == 0)) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001480 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
Benjamin Romerf118a392015-03-16 13:58:26 -04001481 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001482 }
1483 payload = ioremap_cache(phys_addr + offset, bytes);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001484 if (!payload) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001485 rc = -CONTROLVM_RESP_ERROR_IOREMAP_FAILED;
Benjamin Romerf118a392015-03-16 13:58:26 -04001486 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001487 }
1488
1489 info->offset = offset;
1490 info->bytes = bytes;
1491 info->ptr = payload;
Ken Cox12e364b2014-03-04 07:58:07 -06001492
Benjamin Romerf118a392015-03-16 13:58:26 -04001493cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001494 if (rc < 0) {
Benjamin Romerf118a392015-03-16 13:58:26 -04001495 if (payload) {
Ken Cox12e364b2014-03-04 07:58:07 -06001496 iounmap(payload);
1497 payload = NULL;
1498 }
1499 }
1500 return rc;
1501}
1502
1503static void
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001504destroy_controlvm_payload_info(struct visor_controlvm_payload_info *info)
Ken Cox12e364b2014-03-04 07:58:07 -06001505{
Benjamin Romer597c3382015-03-16 13:58:27 -04001506 if (info->ptr) {
Ken Cox12e364b2014-03-04 07:58:07 -06001507 iounmap(info->ptr);
1508 info->ptr = NULL;
1509 }
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001510 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
Ken Cox12e364b2014-03-04 07:58:07 -06001511}
1512
1513static void
1514initialize_controlvm_payload(void)
1515{
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04001516 u64 phys_addr = visorchannel_get_physaddr(controlvm_channel);
Benjamin Romercafefc02015-03-16 13:58:28 -04001517 u64 payload_offset = 0;
1518 u32 payload_bytes = 0;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001519
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001520 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001521 offsetof(struct spar_controlvm_channel_protocol,
1522 request_payload_offset),
Benjamin Romercafefc02015-03-16 13:58:28 -04001523 &payload_offset, sizeof(payload_offset)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001524 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1525 POSTCODE_SEVERITY_ERR);
1526 return;
1527 }
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001528 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001529 offsetof(struct spar_controlvm_channel_protocol,
1530 request_payload_bytes),
Benjamin Romercafefc02015-03-16 13:58:28 -04001531 &payload_bytes, sizeof(payload_bytes)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001532 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1533 POSTCODE_SEVERITY_ERR);
1534 return;
1535 }
1536 initialize_controlvm_payload_info(phys_addr,
Benjamin Romercafefc02015-03-16 13:58:28 -04001537 payload_offset, payload_bytes,
Benjamin Romer84982fb2015-03-16 13:58:07 -04001538 &controlvm_payload_info);
Ken Cox12e364b2014-03-04 07:58:07 -06001539}
1540
1541/* Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
1542 * Returns CONTROLVM_RESP_xxx code.
1543 */
1544int
1545visorchipset_chipset_ready(void)
1546{
Benjamin Romereb34e872015-03-16 13:58:45 -04001547 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_ONLINE);
Ken Cox12e364b2014-03-04 07:58:07 -06001548 return CONTROLVM_RESP_SUCCESS;
1549}
1550EXPORT_SYMBOL_GPL(visorchipset_chipset_ready);
1551
1552int
1553visorchipset_chipset_selftest(void)
1554{
1555 char env_selftest[20];
1556 char *envp[] = { env_selftest, NULL };
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001557
Ken Cox12e364b2014-03-04 07:58:07 -06001558 sprintf(env_selftest, "SPARSP_SELFTEST=%d", 1);
Benjamin Romereb34e872015-03-16 13:58:45 -04001559 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
Ken Cox12e364b2014-03-04 07:58:07 -06001560 envp);
1561 return CONTROLVM_RESP_SUCCESS;
1562}
1563EXPORT_SYMBOL_GPL(visorchipset_chipset_selftest);
1564
1565/* Send ACTION=offline for DEVPATH=/sys/devices/platform/visorchipset.
1566 * Returns CONTROLVM_RESP_xxx code.
1567 */
1568int
1569visorchipset_chipset_notready(void)
1570{
Benjamin Romereb34e872015-03-16 13:58:45 -04001571 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_OFFLINE);
Ken Cox12e364b2014-03-04 07:58:07 -06001572 return CONTROLVM_RESP_SUCCESS;
1573}
1574EXPORT_SYMBOL_GPL(visorchipset_chipset_notready);
1575
1576static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001577chipset_ready(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001578{
1579 int rc = visorchipset_chipset_ready();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001580
Ken Cox12e364b2014-03-04 07:58:07 -06001581 if (rc != CONTROLVM_RESP_SUCCESS)
1582 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001583 if (msg_hdr->flags.response_expected && !visorchipset_holdchipsetready)
1584 controlvm_respond(msg_hdr, rc);
1585 if (msg_hdr->flags.response_expected && visorchipset_holdchipsetready) {
Ken Cox12e364b2014-03-04 07:58:07 -06001586 /* Send CHIPSET_READY response when all modules have been loaded
1587 * and disks mounted for the partition
1588 */
Benjamin Romer77a04492015-03-16 13:58:47 -04001589 g_chipset_msg_hdr = *msg_hdr;
Ken Cox12e364b2014-03-04 07:58:07 -06001590 }
1591}
1592
1593static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001594chipset_selftest(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001595{
1596 int rc = visorchipset_chipset_selftest();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001597
Ken Cox12e364b2014-03-04 07:58:07 -06001598 if (rc != CONTROLVM_RESP_SUCCESS)
1599 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001600 if (msg_hdr->flags.response_expected)
1601 controlvm_respond(msg_hdr, rc);
Ken Cox12e364b2014-03-04 07:58:07 -06001602}
1603
1604static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001605chipset_notready(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001606{
1607 int rc = visorchipset_chipset_notready();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001608
Ken Cox12e364b2014-03-04 07:58:07 -06001609 if (rc != CONTROLVM_RESP_SUCCESS)
1610 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001611 if (msg_hdr->flags.response_expected)
1612 controlvm_respond(msg_hdr, rc);
Ken Cox12e364b2014-03-04 07:58:07 -06001613}
1614
1615/* This is your "one-stop" shop for grabbing the next message from the
1616 * CONTROLVM_QUEUE_EVENT queue in the controlvm channel.
1617 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04001618static bool
Benjamin Romer3ab47702014-10-23 14:30:31 -04001619read_controlvm_event(struct controlvm_message *msg)
Ken Cox12e364b2014-03-04 07:58:07 -06001620{
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001621 if (visorchannel_signalremove(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001622 CONTROLVM_QUEUE_EVENT, msg)) {
1623 /* got a message */
Benjamin Romer0aca78442015-03-04 12:14:25 -05001624 if (msg->hdr.flags.test_message == 1)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001625 return false;
1626 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06001627 }
Jes Sorensenf4c11552015-04-13 10:28:40 -04001628 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06001629}
1630
1631/*
1632 * The general parahotplug flow works as follows. The visorchipset
1633 * driver receives a DEVICE_CHANGESTATE message from Command
1634 * specifying a physical device to enable or disable. The CONTROLVM
1635 * message handler calls parahotplug_process_message, which then adds
1636 * the message to a global list and kicks off a udev event which
1637 * causes a user level script to enable or disable the specified
1638 * device. The udev script then writes to
1639 * /proc/visorchipset/parahotplug, which causes parahotplug_proc_write
1640 * to get called, at which point the appropriate CONTROLVM message is
1641 * retrieved from the list and responded to.
1642 */
1643
1644#define PARAHOTPLUG_TIMEOUT_MS 2000
1645
1646/*
1647 * Generate unique int to match an outstanding CONTROLVM message with a
1648 * udev script /proc response
1649 */
1650static int
1651parahotplug_next_id(void)
1652{
1653 static atomic_t id = ATOMIC_INIT(0);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001654
Ken Cox12e364b2014-03-04 07:58:07 -06001655 return atomic_inc_return(&id);
1656}
1657
1658/*
1659 * Returns the time (in jiffies) when a CONTROLVM message on the list
1660 * should expire -- PARAHOTPLUG_TIMEOUT_MS in the future
1661 */
1662static unsigned long
1663parahotplug_next_expiration(void)
1664{
Nicholas Mc Guire2cc1a1b2015-01-31 12:02:08 +01001665 return jiffies + msecs_to_jiffies(PARAHOTPLUG_TIMEOUT_MS);
Ken Cox12e364b2014-03-04 07:58:07 -06001666}
1667
1668/*
1669 * Create a parahotplug_request, which is basically a wrapper for a
1670 * CONTROLVM_MESSAGE that we can stick on a list
1671 */
1672static struct parahotplug_request *
Benjamin Romer3ab47702014-10-23 14:30:31 -04001673parahotplug_request_create(struct controlvm_message *msg)
Ken Cox12e364b2014-03-04 07:58:07 -06001674{
Quentin Lambertea0dcfc2015-02-10 15:12:07 +01001675 struct parahotplug_request *req;
1676
Benjamin Romer6a55e3c2015-03-16 13:58:30 -04001677 req = kmalloc(sizeof(*req), GFP_KERNEL | __GFP_NORETRY);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001678 if (!req)
Ken Cox12e364b2014-03-04 07:58:07 -06001679 return NULL;
1680
1681 req->id = parahotplug_next_id();
1682 req->expiration = parahotplug_next_expiration();
1683 req->msg = *msg;
1684
1685 return req;
1686}
1687
1688/*
1689 * Free a parahotplug_request.
1690 */
1691static void
1692parahotplug_request_destroy(struct parahotplug_request *req)
1693{
1694 kfree(req);
1695}
1696
1697/*
1698 * Cause uevent to run the user level script to do the disable/enable
1699 * specified in (the CONTROLVM message in) the specified
1700 * parahotplug_request
1701 */
1702static void
1703parahotplug_request_kickoff(struct parahotplug_request *req)
1704{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001705 struct controlvm_message_packet *cmd = &req->msg.cmd;
Ken Cox12e364b2014-03-04 07:58:07 -06001706 char env_cmd[40], env_id[40], env_state[40], env_bus[40], env_dev[40],
1707 env_func[40];
1708 char *envp[] = {
1709 env_cmd, env_id, env_state, env_bus, env_dev, env_func, NULL
1710 };
1711
1712 sprintf(env_cmd, "SPAR_PARAHOTPLUG=1");
1713 sprintf(env_id, "SPAR_PARAHOTPLUG_ID=%d", req->id);
1714 sprintf(env_state, "SPAR_PARAHOTPLUG_STATE=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001715 cmd->device_change_state.state.active);
Ken Cox12e364b2014-03-04 07:58:07 -06001716 sprintf(env_bus, "SPAR_PARAHOTPLUG_BUS=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001717 cmd->device_change_state.bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001718 sprintf(env_dev, "SPAR_PARAHOTPLUG_DEVICE=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001719 cmd->device_change_state.dev_no >> 3);
Ken Cox12e364b2014-03-04 07:58:07 -06001720 sprintf(env_func, "SPAR_PARAHOTPLUG_FUNCTION=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001721 cmd->device_change_state.dev_no & 0x7);
Ken Cox12e364b2014-03-04 07:58:07 -06001722
Benjamin Romereb34e872015-03-16 13:58:45 -04001723 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
Ken Cox12e364b2014-03-04 07:58:07 -06001724 envp);
1725}
1726
1727/*
1728 * Remove any request from the list that's been on there too long and
1729 * respond with an error.
1730 */
1731static void
1732parahotplug_process_list(void)
1733{
Jes Sorensene82ba622015-05-05 18:35:45 -04001734 struct list_head *pos;
1735 struct list_head *tmp;
Ken Cox12e364b2014-03-04 07:58:07 -06001736
Benjamin Romerddf5de52015-03-16 13:58:41 -04001737 spin_lock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001738
Benjamin Romerddf5de52015-03-16 13:58:41 -04001739 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
Ken Cox12e364b2014-03-04 07:58:07 -06001740 struct parahotplug_request *req =
1741 list_entry(pos, struct parahotplug_request, list);
Benjamin Romer55b33412015-03-16 13:58:29 -04001742
1743 if (!time_after_eq(jiffies, req->expiration))
1744 continue;
1745
1746 list_del(pos);
1747 if (req->msg.hdr.flags.response_expected)
1748 controlvm_respond_physdev_changestate(
1749 &req->msg.hdr,
1750 CONTROLVM_RESP_ERROR_DEVICE_UDEV_TIMEOUT,
1751 req->msg.cmd.device_change_state.state);
1752 parahotplug_request_destroy(req);
Ken Cox12e364b2014-03-04 07:58:07 -06001753 }
1754
Benjamin Romerddf5de52015-03-16 13:58:41 -04001755 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001756}
1757
1758/*
1759 * Called from the /proc handler, which means the user script has
1760 * finished the enable/disable. Find the matching identifier, and
1761 * respond to the CONTROLVM message with success.
1762 */
1763static int
Benjamin Romerb06bdf72014-07-31 12:00:49 -04001764parahotplug_request_complete(int id, u16 active)
Ken Cox12e364b2014-03-04 07:58:07 -06001765{
Jes Sorensene82ba622015-05-05 18:35:45 -04001766 struct list_head *pos;
1767 struct list_head *tmp;
Ken Cox12e364b2014-03-04 07:58:07 -06001768
Benjamin Romerddf5de52015-03-16 13:58:41 -04001769 spin_lock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001770
1771 /* Look for a request matching "id". */
Benjamin Romerddf5de52015-03-16 13:58:41 -04001772 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
Ken Cox12e364b2014-03-04 07:58:07 -06001773 struct parahotplug_request *req =
1774 list_entry(pos, struct parahotplug_request, list);
1775 if (req->id == id) {
1776 /* Found a match. Remove it from the list and
1777 * respond.
1778 */
1779 list_del(pos);
Benjamin Romerddf5de52015-03-16 13:58:41 -04001780 spin_unlock(&parahotplug_request_list_lock);
Benjamin Romer2ea51172014-10-23 14:30:25 -04001781 req->msg.cmd.device_change_state.state.active = active;
Benjamin Romer98d7b592014-10-23 14:30:26 -04001782 if (req->msg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06001783 controlvm_respond_physdev_changestate(
1784 &req->msg.hdr, CONTROLVM_RESP_SUCCESS,
Benjamin Romer2ea51172014-10-23 14:30:25 -04001785 req->msg.cmd.device_change_state.state);
Ken Cox12e364b2014-03-04 07:58:07 -06001786 parahotplug_request_destroy(req);
1787 return 0;
1788 }
1789 }
1790
Benjamin Romerddf5de52015-03-16 13:58:41 -04001791 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001792 return -1;
1793}
1794
1795/*
1796 * Enables or disables a PCI device by kicking off a udev script
1797 */
Ken Coxbd5b9b32014-03-13 15:39:22 -05001798static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001799parahotplug_process_message(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001800{
1801 struct parahotplug_request *req;
1802
1803 req = parahotplug_request_create(inmsg);
1804
Benjamin Romer38f736e2015-03-16 13:58:13 -04001805 if (!req)
Ken Cox12e364b2014-03-04 07:58:07 -06001806 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001807
Benjamin Romer2ea51172014-10-23 14:30:25 -04001808 if (inmsg->cmd.device_change_state.state.active) {
Ken Cox12e364b2014-03-04 07:58:07 -06001809 /* For enable messages, just respond with success
1810 * right away. This is a bit of a hack, but there are
1811 * issues with the early enable messages we get (with
1812 * either the udev script not detecting that the device
1813 * is up, or not getting called at all). Fortunately
1814 * the messages that get lost don't matter anyway, as
1815 * devices are automatically enabled at
1816 * initialization.
1817 */
1818 parahotplug_request_kickoff(req);
1819 controlvm_respond_physdev_changestate(&inmsg->hdr,
Benjamin Romer8e76e692015-03-16 13:58:52 -04001820 CONTROLVM_RESP_SUCCESS,
1821 inmsg->cmd.device_change_state.state);
Ken Cox12e364b2014-03-04 07:58:07 -06001822 parahotplug_request_destroy(req);
1823 } else {
1824 /* For disable messages, add the request to the
1825 * request list before kicking off the udev script. It
1826 * won't get responded to until the script has
1827 * indicated it's done.
1828 */
Benjamin Romerddf5de52015-03-16 13:58:41 -04001829 spin_lock(&parahotplug_request_list_lock);
1830 list_add_tail(&req->list, &parahotplug_request_list);
1831 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001832
1833 parahotplug_request_kickoff(req);
1834 }
1835}
1836
Ken Cox12e364b2014-03-04 07:58:07 -06001837/* Process a controlvm message.
1838 * Return result:
Prarit Bhargava779d0752015-05-05 18:37:01 -04001839 * false - this function will return false only in the case where the
Ken Cox12e364b2014-03-04 07:58:07 -06001840 * controlvm message was NOT processed, but processing must be
1841 * retried before reading the next controlvm message; a
1842 * scenario where this can occur is when we need to throttle
1843 * the allocation of memory in which to copy out controlvm
1844 * payload data
Jes Sorensenf4c11552015-04-13 10:28:40 -04001845 * true - processing of the controlvm message completed,
Ken Cox12e364b2014-03-04 07:58:07 -06001846 * either successfully or with an error.
1847 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04001848static bool
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04001849handle_command(struct controlvm_message inmsg, u64 channel_addr)
Ken Cox12e364b2014-03-04 07:58:07 -06001850{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001851 struct controlvm_message_packet *cmd = &inmsg.cmd;
Jes Sorensene82ba622015-05-05 18:35:45 -04001852 u64 parm_addr;
1853 u32 parm_bytes;
Benjamin Romer317d9612015-03-16 13:57:51 -04001854 struct parser_context *parser_ctx = NULL;
Jes Sorensene82ba622015-05-05 18:35:45 -04001855 bool local_addr;
Benjamin Romer3ab47702014-10-23 14:30:31 -04001856 struct controlvm_message ackmsg;
Ken Cox12e364b2014-03-04 07:58:07 -06001857
1858 /* create parsing context if necessary */
Benjamin Romer818352a2015-03-16 13:58:31 -04001859 local_addr = (inmsg.hdr.flags.test_message == 1);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001860 if (channel_addr == 0)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001861 return true;
Benjamin Romer818352a2015-03-16 13:58:31 -04001862 parm_addr = channel_addr + inmsg.hdr.payload_vm_offset;
1863 parm_bytes = inmsg.hdr.payload_bytes;
Ken Cox12e364b2014-03-04 07:58:07 -06001864
1865 /* Parameter and channel addresses within test messages actually lie
1866 * within our OS-controlled memory. We need to know that, because it
1867 * makes a difference in how we compute the virtual address.
1868 */
Jes Sorensenebec8962015-05-05 18:35:57 -04001869 if (parm_addr && parm_bytes) {
Jes Sorensenf4c11552015-04-13 10:28:40 -04001870 bool retry = false;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001871
Ken Cox12e364b2014-03-04 07:58:07 -06001872 parser_ctx =
Benjamin Romer818352a2015-03-16 13:58:31 -04001873 parser_init_byte_stream(parm_addr, parm_bytes,
1874 local_addr, &retry);
Benjamin Romer1b088722015-03-04 12:14:26 -05001875 if (!parser_ctx && retry)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001876 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06001877 }
1878
Benjamin Romer818352a2015-03-16 13:58:31 -04001879 if (!local_addr) {
Ken Cox12e364b2014-03-04 07:58:07 -06001880 controlvm_init_response(&ackmsg, &inmsg.hdr,
1881 CONTROLVM_RESP_SUCCESS);
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001882 if (controlvm_channel)
1883 visorchannel_signalinsert(controlvm_channel,
Benjamin Romer1b088722015-03-04 12:14:26 -05001884 CONTROLVM_QUEUE_ACK,
1885 &ackmsg);
Ken Cox12e364b2014-03-04 07:58:07 -06001886 }
Benjamin Romer98d7b592014-10-23 14:30:26 -04001887 switch (inmsg.hdr.id) {
Ken Cox12e364b2014-03-04 07:58:07 -06001888 case CONTROLVM_CHIPSET_INIT:
Ken Cox12e364b2014-03-04 07:58:07 -06001889 chipset_init(&inmsg);
1890 break;
1891 case CONTROLVM_BUS_CREATE:
Ken Cox12e364b2014-03-04 07:58:07 -06001892 bus_create(&inmsg);
1893 break;
1894 case CONTROLVM_BUS_DESTROY:
Ken Cox12e364b2014-03-04 07:58:07 -06001895 bus_destroy(&inmsg);
1896 break;
1897 case CONTROLVM_BUS_CONFIGURE:
Ken Cox12e364b2014-03-04 07:58:07 -06001898 bus_configure(&inmsg, parser_ctx);
1899 break;
1900 case CONTROLVM_DEVICE_CREATE:
Ken Cox12e364b2014-03-04 07:58:07 -06001901 my_device_create(&inmsg);
1902 break;
1903 case CONTROLVM_DEVICE_CHANGESTATE:
Benjamin Romer2ea51172014-10-23 14:30:25 -04001904 if (cmd->device_change_state.flags.phys_device) {
Ken Cox12e364b2014-03-04 07:58:07 -06001905 parahotplug_process_message(&inmsg);
1906 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06001907 /* save the hdr and cmd structures for later use */
1908 /* when sending back the response to Command */
1909 my_device_changestate(&inmsg);
Benjamin Romer4f44b722015-03-16 13:58:02 -04001910 g_devicechangestate_packet = inmsg.cmd;
Ken Cox12e364b2014-03-04 07:58:07 -06001911 break;
1912 }
1913 break;
1914 case CONTROLVM_DEVICE_DESTROY:
Ken Cox12e364b2014-03-04 07:58:07 -06001915 my_device_destroy(&inmsg);
1916 break;
1917 case CONTROLVM_DEVICE_CONFIGURE:
Ken Cox12e364b2014-03-04 07:58:07 -06001918 /* no op for now, just send a respond that we passed */
Benjamin Romer98d7b592014-10-23 14:30:26 -04001919 if (inmsg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06001920 controlvm_respond(&inmsg.hdr, CONTROLVM_RESP_SUCCESS);
1921 break;
1922 case CONTROLVM_CHIPSET_READY:
Ken Cox12e364b2014-03-04 07:58:07 -06001923 chipset_ready(&inmsg.hdr);
1924 break;
1925 case CONTROLVM_CHIPSET_SELFTEST:
Ken Cox12e364b2014-03-04 07:58:07 -06001926 chipset_selftest(&inmsg.hdr);
1927 break;
1928 case CONTROLVM_CHIPSET_STOP:
Ken Cox12e364b2014-03-04 07:58:07 -06001929 chipset_notready(&inmsg.hdr);
1930 break;
1931 default:
Benjamin Romer98d7b592014-10-23 14:30:26 -04001932 if (inmsg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06001933 controlvm_respond(&inmsg.hdr,
Benjamin Romer818352a2015-03-16 13:58:31 -04001934 -CONTROLVM_RESP_ERROR_MESSAGE_ID_UNKNOWN);
Ken Cox12e364b2014-03-04 07:58:07 -06001935 break;
1936 }
1937
Benjamin Romer38f736e2015-03-16 13:58:13 -04001938 if (parser_ctx) {
Ken Cox12e364b2014-03-04 07:58:07 -06001939 parser_done(parser_ctx);
1940 parser_ctx = NULL;
1941 }
Jes Sorensenf4c11552015-04-13 10:28:40 -04001942 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06001943}
1944
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04001945static u64 controlvm_get_channel_address(void)
Benjamin Romer524b0b62014-07-17 12:39:57 -04001946{
Benjamin Romer5fc02292014-07-31 12:00:51 -04001947 u64 addr = 0;
Benjamin Romerb3c55b12014-07-31 12:00:50 -04001948 u32 size = 0;
Benjamin Romer524b0b62014-07-17 12:39:57 -04001949
Benjamin Romer0aca78442015-03-04 12:14:25 -05001950 if (!VMCALL_SUCCESSFUL(issue_vmcall_io_controlvm_addr(&addr, &size)))
Benjamin Romer524b0b62014-07-17 12:39:57 -04001951 return 0;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001952
Benjamin Romer524b0b62014-07-17 12:39:57 -04001953 return addr;
1954}
1955
Ken Cox12e364b2014-03-04 07:58:07 -06001956static void
1957controlvm_periodic_work(struct work_struct *work)
1958{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001959 struct controlvm_message inmsg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001960 bool got_command = false;
1961 bool handle_command_failed = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04001962 static u64 poll_count;
Ken Cox12e364b2014-03-04 07:58:07 -06001963
1964 /* make sure visorbus server is registered for controlvm callbacks */
David Kershner4da33362015-05-05 18:36:39 -04001965 if (visorchipset_visorbusregwait && !visorbusregistered)
Benjamin Romer1c1ed292015-03-16 13:58:32 -04001966 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001967
Benjamin Romer1c1ed292015-03-16 13:58:32 -04001968 poll_count++;
1969 if (poll_count >= 250)
Ken Cox12e364b2014-03-04 07:58:07 -06001970 ; /* keep going */
1971 else
Benjamin Romer1c1ed292015-03-16 13:58:32 -04001972 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001973
1974 /* Check events to determine if response to CHIPSET_READY
1975 * should be sent
1976 */
Benjamin Romer0639ba62015-03-16 13:58:04 -04001977 if (visorchipset_holdchipsetready &&
1978 (g_chipset_msg_hdr.id != CONTROLVM_INVALID)) {
Ken Cox12e364b2014-03-04 07:58:07 -06001979 if (check_chipset_events() == 1) {
Benjamin Romerda021f02015-03-16 13:57:58 -04001980 controlvm_respond(&g_chipset_msg_hdr, 0);
Ken Cox12e364b2014-03-04 07:58:07 -06001981 clear_chipset_events();
Benjamin Romerda021f02015-03-16 13:57:58 -04001982 memset(&g_chipset_msg_hdr, 0,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001983 sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06001984 }
1985 }
1986
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001987 while (visorchannel_signalremove(controlvm_channel,
Benjamin Romer8a1182e2014-07-17 12:39:58 -04001988 CONTROLVM_QUEUE_RESPONSE,
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001989 &inmsg))
1990 ;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04001991 if (!got_command) {
Benjamin Romer7166ed12015-03-16 13:58:38 -04001992 if (controlvm_pending_msg_valid) {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04001993 /* we throttled processing of a prior
1994 * msg, so try to process it again
1995 * rather than reading a new one
1996 */
Benjamin Romer7166ed12015-03-16 13:58:38 -04001997 inmsg = controlvm_pending_msg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001998 controlvm_pending_msg_valid = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04001999 got_command = true;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002000 } else {
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002001 got_command = read_controlvm_event(&inmsg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002002 }
Ken Cox12e364b2014-03-04 07:58:07 -06002003 }
2004
Jes Sorensenf4c11552015-04-13 10:28:40 -04002005 handle_command_failed = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002006 while (got_command && (!handle_command_failed)) {
Benjamin Romerb53e0e92015-03-16 13:57:56 -04002007 most_recent_message_jiffies = jiffies;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002008 if (handle_command(inmsg,
2009 visorchannel_get_physaddr
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002010 (controlvm_channel)))
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002011 got_command = read_controlvm_event(&inmsg);
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002012 else {
2013 /* this is a scenario where throttling
2014 * is required, but probably NOT an
2015 * error...; we stash the current
2016 * controlvm msg so we will attempt to
2017 * reprocess it on our next loop
2018 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04002019 handle_command_failed = true;
Benjamin Romer7166ed12015-03-16 13:58:38 -04002020 controlvm_pending_msg = inmsg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002021 controlvm_pending_msg_valid = true;
Ken Cox12e364b2014-03-04 07:58:07 -06002022 }
2023 }
2024
2025 /* parahotplug_worker */
2026 parahotplug_process_list();
2027
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002028cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002029
2030 if (time_after(jiffies,
Benjamin Romerb53e0e92015-03-16 13:57:56 -04002031 most_recent_message_jiffies + (HZ * MIN_IDLE_SECONDS))) {
Ken Cox12e364b2014-03-04 07:58:07 -06002032 /* it's been longer than MIN_IDLE_SECONDS since we
2033 * processed our last controlvm message; slow down the
2034 * polling
2035 */
Benjamin Romer911e2132015-03-16 13:57:47 -04002036 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_SLOW)
2037 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
Ken Cox12e364b2014-03-04 07:58:07 -06002038 } else {
Benjamin Romer911e2132015-03-16 13:57:47 -04002039 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_FAST)
2040 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
Ken Cox12e364b2014-03-04 07:58:07 -06002041 }
2042
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002043 queue_delayed_work(periodic_controlvm_workqueue,
2044 &periodic_controlvm_work, poll_jiffies);
Ken Cox12e364b2014-03-04 07:58:07 -06002045}
2046
2047static void
2048setup_crash_devices_work_queue(struct work_struct *work)
2049{
Benjamin Romere6bdb902015-03-16 13:58:33 -04002050 struct controlvm_message local_crash_bus_msg;
2051 struct controlvm_message local_crash_dev_msg;
Benjamin Romer3ab47702014-10-23 14:30:31 -04002052 struct controlvm_message msg;
Benjamin Romere6bdb902015-03-16 13:58:33 -04002053 u32 local_crash_msg_offset;
2054 u16 local_crash_msg_count;
Ken Cox12e364b2014-03-04 07:58:07 -06002055
David Kershner4da33362015-05-05 18:36:39 -04002056 /* make sure visorbus is registered for controlvm callbacks */
2057 if (visorchipset_visorbusregwait && !visorbusregistered)
Benjamin Romere6bdb902015-03-16 13:58:33 -04002058 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002059
2060 POSTCODE_LINUX_2(CRASH_DEV_ENTRY_PC, POSTCODE_SEVERITY_INFO);
2061
2062 /* send init chipset msg */
Benjamin Romer98d7b592014-10-23 14:30:26 -04002063 msg.hdr.id = CONTROLVM_CHIPSET_INIT;
Benjamin Romer2ea51172014-10-23 14:30:25 -04002064 msg.cmd.init_chipset.bus_count = 23;
2065 msg.cmd.init_chipset.switch_count = 0;
Ken Cox12e364b2014-03-04 07:58:07 -06002066
2067 chipset_init(&msg);
2068
Ken Cox12e364b2014-03-04 07:58:07 -06002069 /* get saved message count */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002070 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002071 offsetof(struct spar_controlvm_channel_protocol,
2072 saved_crash_message_count),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002073 &local_crash_msg_count, sizeof(u16)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002074 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2075 POSTCODE_SEVERITY_ERR);
2076 return;
2077 }
2078
Benjamin Romere6bdb902015-03-16 13:58:33 -04002079 if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
Ken Cox12e364b2014-03-04 07:58:07 -06002080 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002081 local_crash_msg_count,
Ken Cox12e364b2014-03-04 07:58:07 -06002082 POSTCODE_SEVERITY_ERR);
2083 return;
2084 }
2085
2086 /* get saved crash message offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002087 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002088 offsetof(struct spar_controlvm_channel_protocol,
2089 saved_crash_message_offset),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002090 &local_crash_msg_offset, sizeof(u32)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002091 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2092 POSTCODE_SEVERITY_ERR);
2093 return;
2094 }
2095
2096 /* read create device message for storage bus offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002097 if (visorchannel_read(controlvm_channel,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002098 local_crash_msg_offset,
2099 &local_crash_bus_msg,
Benjamin Romer3ab47702014-10-23 14:30:31 -04002100 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002101 POSTCODE_LINUX_2(CRASH_DEV_RD_BUS_FAIULRE_PC,
2102 POSTCODE_SEVERITY_ERR);
2103 return;
2104 }
2105
2106 /* read create device message for storage device */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002107 if (visorchannel_read(controlvm_channel,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002108 local_crash_msg_offset +
Benjamin Romer3ab47702014-10-23 14:30:31 -04002109 sizeof(struct controlvm_message),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002110 &local_crash_dev_msg,
Benjamin Romer3ab47702014-10-23 14:30:31 -04002111 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002112 POSTCODE_LINUX_2(CRASH_DEV_RD_DEV_FAIULRE_PC,
2113 POSTCODE_SEVERITY_ERR);
2114 return;
2115 }
2116
2117 /* reuse IOVM create bus message */
Jes Sorensenebec8962015-05-05 18:35:57 -04002118 if (local_crash_bus_msg.cmd.create_bus.channel_addr) {
Benjamin Romere6bdb902015-03-16 13:58:33 -04002119 bus_create(&local_crash_bus_msg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002120 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002121 POSTCODE_LINUX_2(CRASH_DEV_BUS_NULL_FAILURE_PC,
2122 POSTCODE_SEVERITY_ERR);
2123 return;
2124 }
2125
2126 /* reuse create device message for storage device */
Jes Sorensenebec8962015-05-05 18:35:57 -04002127 if (local_crash_dev_msg.cmd.create_device.channel_addr) {
Benjamin Romere6bdb902015-03-16 13:58:33 -04002128 my_device_create(&local_crash_dev_msg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002129 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002130 POSTCODE_LINUX_2(CRASH_DEV_DEV_NULL_FAILURE_PC,
2131 POSTCODE_SEVERITY_ERR);
2132 return;
2133 }
Ken Cox12e364b2014-03-04 07:58:07 -06002134 POSTCODE_LINUX_2(CRASH_DEV_EXIT_PC, POSTCODE_SEVERITY_INFO);
2135 return;
2136
Benjamin Romere6bdb902015-03-16 13:58:33 -04002137cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002138
Benjamin Romer911e2132015-03-16 13:57:47 -04002139 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
Ken Cox12e364b2014-03-04 07:58:07 -06002140
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002141 queue_delayed_work(periodic_controlvm_workqueue,
2142 &periodic_controlvm_work, poll_jiffies);
Ken Cox12e364b2014-03-04 07:58:07 -06002143}
2144
2145static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002146bus_create_response(u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002147{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002148 bus_responder(CONTROLVM_BUS_CREATE, bus_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002149}
2150
2151static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002152bus_destroy_response(u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002153{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002154 bus_responder(CONTROLVM_BUS_DESTROY, bus_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002155}
2156
2157static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002158device_create_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002159{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002160 device_responder(CONTROLVM_DEVICE_CREATE, bus_no, dev_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002161}
2162
2163static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002164device_destroy_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002165{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002166 device_responder(CONTROLVM_DEVICE_DESTROY, bus_no, dev_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002167}
2168
2169void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002170visorchipset_device_pause_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002171{
Ken Cox12e364b2014-03-04 07:58:07 -06002172 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
Benjamin Romer8420f412014-10-31 09:57:36 -04002173 bus_no, dev_no, response,
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04002174 segment_state_standby);
Ken Cox12e364b2014-03-04 07:58:07 -06002175}
Ken Cox927c7922014-03-05 14:52:25 -06002176EXPORT_SYMBOL_GPL(visorchipset_device_pause_response);
Ken Cox12e364b2014-03-04 07:58:07 -06002177
2178static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002179device_resume_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002180{
2181 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002182 bus_no, dev_no, response,
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04002183 segment_state_running);
Ken Cox12e364b2014-03-04 07:58:07 -06002184}
2185
Jes Sorensenf4c11552015-04-13 10:28:40 -04002186bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002187visorchipset_get_bus_info(u32 bus_no, struct visorchipset_bus_info *bus_info)
Ken Cox12e364b2014-03-04 07:58:07 -06002188{
Jes Sorensen4f665202015-05-05 18:35:52 -04002189 void *p = bus_find(&bus_info_list, bus_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002190
Benjamin Romer0aca78442015-03-04 12:14:25 -05002191 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002192 return false;
Benjamin Romer77db7122014-10-31 09:57:37 -04002193 memcpy(bus_info, p, sizeof(struct visorchipset_bus_info));
Jes Sorensenf4c11552015-04-13 10:28:40 -04002194 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002195}
2196EXPORT_SYMBOL_GPL(visorchipset_get_bus_info);
2197
Jes Sorensenf4c11552015-04-13 10:28:40 -04002198bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002199visorchipset_set_bus_context(u32 bus_no, void *context)
Ken Cox12e364b2014-03-04 07:58:07 -06002200{
Jes Sorensen4f665202015-05-05 18:35:52 -04002201 struct visorchipset_bus_info *p = bus_find(&bus_info_list, bus_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002202
Benjamin Romer0aca78442015-03-04 12:14:25 -05002203 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002204 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002205 p->bus_driver_context = context;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002206 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002207}
2208EXPORT_SYMBOL_GPL(visorchipset_set_bus_context);
2209
Jes Sorensenf4c11552015-04-13 10:28:40 -04002210bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002211visorchipset_get_device_info(u32 bus_no, u32 dev_no,
Benjamin Romerb486df12014-10-31 09:57:38 -04002212 struct visorchipset_device_info *dev_info)
Ken Cox12e364b2014-03-04 07:58:07 -06002213{
Jes Sorensend480f6a2015-05-05 18:35:54 -04002214 void *p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002215
Benjamin Romer0aca78442015-03-04 12:14:25 -05002216 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002217 return false;
Benjamin Romerb486df12014-10-31 09:57:38 -04002218 memcpy(dev_info, p, sizeof(struct visorchipset_device_info));
Jes Sorensenf4c11552015-04-13 10:28:40 -04002219 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002220}
2221EXPORT_SYMBOL_GPL(visorchipset_get_device_info);
2222
Jes Sorensenf4c11552015-04-13 10:28:40 -04002223bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002224visorchipset_set_device_context(u32 bus_no, u32 dev_no, void *context)
Ken Cox12e364b2014-03-04 07:58:07 -06002225{
Jes Sorensend480f6a2015-05-05 18:35:54 -04002226 struct visorchipset_device_info *p;
2227
2228 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002229
Benjamin Romer0aca78442015-03-04 12:14:25 -05002230 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002231 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002232 p->bus_driver_context = context;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002233 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002234}
2235EXPORT_SYMBOL_GPL(visorchipset_set_device_context);
2236
2237/* Generic wrapper function for allocating memory from a kmem_cache pool.
2238 */
2239void *
Jes Sorensenf4c11552015-04-13 10:28:40 -04002240visorchipset_cache_alloc(struct kmem_cache *pool, bool ok_to_block,
Ken Cox12e364b2014-03-04 07:58:07 -06002241 char *fn, int ln)
2242{
2243 gfp_t gfp;
2244 void *p;
2245
2246 if (ok_to_block)
2247 gfp = GFP_KERNEL;
2248 else
2249 gfp = GFP_ATOMIC;
2250 /* __GFP_NORETRY means "ok to fail", meaning
2251 * kmem_cache_alloc() can return NULL, implying the caller CAN
2252 * cope with failure. If you do NOT specify __GFP_NORETRY,
2253 * Linux will go to extreme measures to get memory for you
2254 * (like, invoke oom killer), which will probably cripple the
2255 * system.
2256 */
2257 gfp |= __GFP_NORETRY;
2258 p = kmem_cache_alloc(pool, gfp);
Benjamin Romer0aca78442015-03-04 12:14:25 -05002259 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06002260 return NULL;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002261
Ken Cox12e364b2014-03-04 07:58:07 -06002262 return p;
2263}
2264
2265/* Generic wrapper function for freeing memory from a kmem_cache pool.
2266 */
2267void
2268visorchipset_cache_free(struct kmem_cache *pool, void *p, char *fn, int ln)
2269{
Benjamin Romer0aca78442015-03-04 12:14:25 -05002270 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06002271 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002272
Ken Cox12e364b2014-03-04 07:58:07 -06002273 kmem_cache_free(pool, p);
2274}
2275
Benjamin Romer18b87ed2014-07-24 14:08:43 -04002276static ssize_t chipsetready_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002277 struct device_attribute *attr,
2278 const char *buf, size_t count)
Ken Cox12e364b2014-03-04 07:58:07 -06002279{
Benjamin Romer18b87ed2014-07-24 14:08:43 -04002280 char msgtype[64];
Ken Cox12e364b2014-03-04 07:58:07 -06002281
Benjamin Romer66e24b72014-07-25 13:55:10 -04002282 if (sscanf(buf, "%63s", msgtype) != 1)
Ken Cox12e364b2014-03-04 07:58:07 -06002283 return -EINVAL;
Benjamin Romer66e24b72014-07-25 13:55:10 -04002284
Jes Sorensenebec8962015-05-05 18:35:57 -04002285 if (!strcmp(msgtype, "CALLHOMEDISK_MOUNTED")) {
Benjamin Romer66e24b72014-07-25 13:55:10 -04002286 chipset_events[0] = 1;
2287 return count;
Jes Sorensenebec8962015-05-05 18:35:57 -04002288 } else if (!strcmp(msgtype, "MODULES_LOADED")) {
Benjamin Romer66e24b72014-07-25 13:55:10 -04002289 chipset_events[1] = 1;
2290 return count;
Benjamin Romere22a4a02014-08-18 09:34:54 -04002291 }
2292 return -EINVAL;
Ken Cox12e364b2014-03-04 07:58:07 -06002293}
2294
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002295/* The parahotplug/devicedisabled interface gets called by our support script
2296 * when an SR-IOV device has been shut down. The ID is passed to the script
2297 * and then passed back when the device has been removed.
2298 */
2299static ssize_t devicedisabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002300 struct device_attribute *attr,
2301 const char *buf, size_t count)
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002302{
Jes Sorensen94217362015-05-05 18:35:46 -04002303 unsigned int id;
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002304
Jes Sorensenebec8962015-05-05 18:35:57 -04002305 if (kstrtouint(buf, 10, &id))
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002306 return -EINVAL;
2307
2308 parahotplug_request_complete(id, 0);
2309 return count;
2310}
2311
2312/* The parahotplug/deviceenabled interface gets called by our support script
2313 * when an SR-IOV device has been recovered. The ID is passed to the script
2314 * and then passed back when the device has been brought back up.
2315 */
2316static ssize_t deviceenabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002317 struct device_attribute *attr,
2318 const char *buf, size_t count)
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002319{
Jes Sorensen94217362015-05-05 18:35:46 -04002320 unsigned int id;
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002321
Jes Sorensenebec8962015-05-05 18:35:57 -04002322 if (kstrtouint(buf, 10, &id))
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002323 return -EINVAL;
2324
2325 parahotplug_request_complete(id, 1);
2326 return count;
2327}
2328
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002329static int
2330visorchipset_mmap(struct file *file, struct vm_area_struct *vma)
2331{
2332 unsigned long physaddr = 0;
2333 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
Erik Arfvidson780fcad2015-05-05 18:36:35 -04002334 u64 addr = 0;
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002335
2336 /* sv_enable_dfp(); */
2337 if (offset & (PAGE_SIZE - 1))
2338 return -ENXIO; /* need aligned offsets */
2339
2340 switch (offset) {
2341 case VISORCHIPSET_MMAP_CONTROLCHANOFFSET:
2342 vma->vm_flags |= VM_IO;
2343 if (!*file_controlvm_channel)
2344 return -ENXIO;
2345
2346 visorchannel_read(*file_controlvm_channel,
2347 offsetof(struct spar_controlvm_channel_protocol,
2348 gp_control_channel),
2349 &addr, sizeof(addr));
2350 if (!addr)
2351 return -ENXIO;
2352
2353 physaddr = (unsigned long)addr;
2354 if (remap_pfn_range(vma, vma->vm_start,
2355 physaddr >> PAGE_SHIFT,
2356 vma->vm_end - vma->vm_start,
2357 /*pgprot_noncached */
2358 (vma->vm_page_prot))) {
2359 return -EAGAIN;
2360 }
2361 break;
2362 default:
2363 return -ENXIO;
2364 }
2365 return 0;
2366}
2367
2368static long visorchipset_ioctl(struct file *file, unsigned int cmd,
2369 unsigned long arg)
2370{
2371 s64 adjustment;
2372 s64 vrtc_offset;
2373
2374 switch (cmd) {
2375 case VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET:
2376 /* get the physical rtc offset */
2377 vrtc_offset = issue_vmcall_query_guest_virtual_time_offset();
2378 if (copy_to_user((void __user *)arg, &vrtc_offset,
2379 sizeof(vrtc_offset))) {
2380 return -EFAULT;
2381 }
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04002382 return 0;
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002383 case VMCALL_UPDATE_PHYSICAL_TIME:
2384 if (copy_from_user(&adjustment, (void __user *)arg,
2385 sizeof(adjustment))) {
2386 return -EFAULT;
2387 }
2388 return issue_vmcall_update_physical_time(adjustment);
2389 default:
2390 return -EFAULT;
2391 }
2392}
2393
2394static const struct file_operations visorchipset_fops = {
2395 .owner = THIS_MODULE,
2396 .open = visorchipset_open,
2397 .read = NULL,
2398 .write = NULL,
2399 .unlocked_ioctl = visorchipset_ioctl,
2400 .release = visorchipset_release,
2401 .mmap = visorchipset_mmap,
2402};
2403
2404int
2405visorchipset_file_init(dev_t major_dev, struct visorchannel **controlvm_channel)
2406{
2407 int rc = 0;
2408
2409 file_controlvm_channel = controlvm_channel;
2410 cdev_init(&file_cdev, &visorchipset_fops);
2411 file_cdev.owner = THIS_MODULE;
2412 if (MAJOR(major_dev) == 0) {
Erik Arfvidson46168812015-05-05 18:36:14 -04002413 rc = alloc_chrdev_region(&major_dev, 0, 1, "visorchipset");
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002414 /* dynamic major device number registration required */
2415 if (rc < 0)
2416 return rc;
2417 } else {
2418 /* static major device number registration required */
Erik Arfvidson46168812015-05-05 18:36:14 -04002419 rc = register_chrdev_region(major_dev, 1, "visorchipset");
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002420 if (rc < 0)
2421 return rc;
2422 }
2423 rc = cdev_add(&file_cdev, MKDEV(MAJOR(major_dev), 0), 1);
2424 if (rc < 0) {
2425 unregister_chrdev_region(major_dev, 1);
2426 return rc;
2427 }
2428 return 0;
2429}
2430
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04002431static int
2432visorchipset_init(struct acpi_device *acpi_device)
Ken Cox12e364b2014-03-04 07:58:07 -06002433{
David Kershner33078252015-05-05 18:36:48 -04002434 int rc = 0;
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04002435 u64 addr;
Ken Cox12e364b2014-03-04 07:58:07 -06002436
David Kershner4da33362015-05-05 18:36:39 -04002437 memset(&busdev_notifiers, 0, sizeof(busdev_notifiers));
Benjamin Romer84982fb2015-03-16 13:58:07 -04002438 memset(&controlvm_payload_info, 0, sizeof(controlvm_payload_info));
Benjamin Romerea33b4ee52015-03-16 13:58:08 -04002439 memset(&livedump_info, 0, sizeof(livedump_info));
2440 atomic_set(&livedump_info.buffers_in_use, 0);
Ken Cox12e364b2014-03-04 07:58:07 -06002441
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002442 addr = controlvm_get_channel_address();
Jes Sorensenebec8962015-05-05 18:35:57 -04002443 if (addr) {
Jes Sorensendf942472015-05-05 18:37:10 -04002444 int tmp_sz = sizeof(struct spar_controlvm_channel_protocol);
2445 uuid_le uuid = SPAR_CONTROLVM_CHANNEL_PROTOCOL_UUID;
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002446 controlvm_channel =
Jes Sorensendf942472015-05-05 18:37:10 -04002447 visorchannel_create_with_lock(addr, tmp_sz,
2448 GFP_KERNEL, uuid);
Benjamin Romer93a84562014-10-23 14:30:05 -04002449 if (SPAR_CONTROLVM_CHANNEL_OK_CLIENT(
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002450 visorchannel_get_header(controlvm_channel))) {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002451 initialize_controlvm_payload();
2452 } else {
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002453 visorchannel_destroy(controlvm_channel);
2454 controlvm_channel = NULL;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002455 return -ENODEV;
2456 }
2457 } else {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002458 return -ENODEV;
2459 }
2460
Benjamin Romer5aa8ae52015-03-16 13:58:44 -04002461 major_dev = MKDEV(visorchipset_major, 0);
2462 rc = visorchipset_file_init(major_dev, &controlvm_channel);
Ken Cox4cb005a2014-03-19 13:06:20 -05002463 if (rc < 0) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002464 POSTCODE_LINUX_2(CHIPSET_INIT_FAILURE_PC, DIAG_SEVERITY_ERR);
Benjamin Romera6a39892015-03-16 13:58:34 -04002465 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002466 }
Ken Cox9f8d0e82014-03-19 13:06:19 -05002467
Benjamin Romerda021f02015-03-16 13:57:58 -04002468 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002469
David Kershner4da33362015-05-05 18:36:39 -04002470 /* if booting in a crash kernel */
2471 if (is_kdump_kernel())
2472 INIT_DELAYED_WORK(&periodic_controlvm_work,
2473 setup_crash_devices_work_queue);
2474 else
2475 INIT_DELAYED_WORK(&periodic_controlvm_work,
2476 controlvm_periodic_work);
2477 periodic_controlvm_workqueue =
2478 create_singlethread_workqueue("visorchipset_controlvm");
Ken Cox12e364b2014-03-04 07:58:07 -06002479
David Kershner4da33362015-05-05 18:36:39 -04002480 if (!periodic_controlvm_workqueue) {
2481 POSTCODE_LINUX_2(CREATE_WORKQUEUE_FAILED_PC,
2482 DIAG_SEVERITY_ERR);
2483 rc = -ENOMEM;
2484 goto cleanup;
2485 }
2486 most_recent_message_jiffies = jiffies;
2487 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
2488 rc = queue_delayed_work(periodic_controlvm_workqueue,
2489 &periodic_controlvm_work, poll_jiffies);
2490 if (rc < 0) {
2491 POSTCODE_LINUX_2(QUEUE_DELAYED_WORK_PC,
2492 DIAG_SEVERITY_ERR);
2493 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002494 }
2495
Benjamin Romereb34e872015-03-16 13:58:45 -04002496 visorchipset_platform_device.dev.devt = major_dev;
2497 if (platform_device_register(&visorchipset_platform_device) < 0) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002498 POSTCODE_LINUX_2(DEVICE_REGISTER_FAILURE_PC, DIAG_SEVERITY_ERR);
2499 rc = -1;
Benjamin Romera6a39892015-03-16 13:58:34 -04002500 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002501 }
Ken Cox12e364b2014-03-04 07:58:07 -06002502 POSTCODE_LINUX_2(CHIPSET_INIT_SUCCESS_PC, POSTCODE_SEVERITY_INFO);
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04002503
2504 rc = visorbus_init();
Benjamin Romera6a39892015-03-16 13:58:34 -04002505cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002506 if (rc) {
Ken Cox12e364b2014-03-04 07:58:07 -06002507 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
2508 POSTCODE_SEVERITY_ERR);
2509 }
2510 return rc;
2511}
2512
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002513void
2514visorchipset_file_cleanup(dev_t major_dev)
2515{
2516 if (file_cdev.ops)
2517 cdev_del(&file_cdev);
2518 file_cdev.ops = NULL;
2519 unregister_chrdev_region(major_dev, 1);
2520}
2521
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04002522static int
2523visorchipset_exit(struct acpi_device *acpi_device)
Ken Cox12e364b2014-03-04 07:58:07 -06002524{
Ken Cox12e364b2014-03-04 07:58:07 -06002525 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
2526
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04002527 visorbus_exit();
2528
David Kershner4da33362015-05-05 18:36:39 -04002529 cancel_delayed_work(&periodic_controlvm_work);
2530 flush_workqueue(periodic_controlvm_workqueue);
2531 destroy_workqueue(periodic_controlvm_workqueue);
2532 periodic_controlvm_workqueue = NULL;
2533 destroy_controlvm_payload_info(&controlvm_payload_info);
Benjamin Romer17833192014-07-15 13:30:41 -04002534
Ken Cox12e364b2014-03-04 07:58:07 -06002535 cleanup_controlvm_structures();
2536
Benjamin Romerda021f02015-03-16 13:57:58 -04002537 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002538
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002539 visorchannel_destroy(controlvm_channel);
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002540
Sudip Mukherjeeaddceb12015-03-24 20:47:28 +05302541 visorchipset_file_cleanup(visorchipset_platform_device.dev.devt);
Ken Cox12e364b2014-03-04 07:58:07 -06002542 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04002543
2544 return 0;
2545}
2546
2547static const struct acpi_device_id unisys_device_ids[] = {
2548 {"PNP0A07", 0},
2549 {"", 0},
2550};
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04002551
2552static struct acpi_driver unisys_acpi_driver = {
2553 .name = "unisys_acpi",
2554 .class = "unisys_acpi_class",
2555 .owner = THIS_MODULE,
2556 .ids = unisys_device_ids,
2557 .ops = {
2558 .add = visorchipset_init,
2559 .remove = visorchipset_exit,
2560 },
2561};
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04002562static __init uint32_t visorutil_spar_detect(void)
2563{
2564 unsigned int eax, ebx, ecx, edx;
2565
2566 if (cpu_has_hypervisor) {
2567 /* check the ID */
2568 cpuid(UNISYS_SPAR_LEAF_ID, &eax, &ebx, &ecx, &edx);
2569 return (ebx == UNISYS_SPAR_ID_EBX) &&
2570 (ecx == UNISYS_SPAR_ID_ECX) &&
2571 (edx == UNISYS_SPAR_ID_EDX);
2572 } else {
2573 return 0;
2574 }
2575}
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04002576
2577static int init_unisys(void)
2578{
2579 int result;
Erik Arfvidsond5b3f1d2015-05-05 18:37:04 -04002580 if (!visorutil_spar_detect())
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04002581 return -ENODEV;
2582
2583 result = acpi_bus_register_driver(&unisys_acpi_driver);
2584 if (result)
2585 return -ENODEV;
2586
2587 pr_info("Unisys Visorchipset Driver Loaded.\n");
2588 return 0;
2589};
2590
2591static void exit_unisys(void)
2592{
2593 acpi_bus_unregister_driver(&unisys_acpi_driver);
Ken Cox12e364b2014-03-04 07:58:07 -06002594}
2595
Ken Cox12e364b2014-03-04 07:58:07 -06002596module_param_named(major, visorchipset_major, int, S_IRUGO);
Jes Sorensenb615d622015-05-05 18:35:38 -04002597MODULE_PARM_DESC(visorchipset_major,
2598 "major device number to use for the device node");
David Kershner4da33362015-05-05 18:36:39 -04002599module_param_named(visorbusregwait, visorchipset_visorbusregwait, int, S_IRUGO);
2600MODULE_PARM_DESC(visorchipset_visorbusreqwait,
Ken Cox12e364b2014-03-04 07:58:07 -06002601 "1 to have the module wait for the visor bus to register");
Ken Cox12e364b2014-03-04 07:58:07 -06002602module_param_named(holdchipsetready, visorchipset_holdchipsetready,
2603 int, S_IRUGO);
2604MODULE_PARM_DESC(visorchipset_holdchipsetready,
2605 "1 to hold response to CHIPSET_READY");
Jes Sorensenb615d622015-05-05 18:35:38 -04002606
Prarit Bhargava55c67dc2015-05-05 18:37:02 -04002607module_init(init_unisys);
2608module_exit(exit_unisys);
Ken Cox12e364b2014-03-04 07:58:07 -06002609
2610MODULE_AUTHOR("Unisys");
2611MODULE_LICENSE("GPL");
2612MODULE_DESCRIPTION("Supervisor chipset driver for service partition: ver "
2613 VERSION);
2614MODULE_VERSION(VERSION);