blob: 3a1ee2d21a9f6dc59d228b3e58225990eaf2efd2 [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
Erik Arfvidson46168812015-05-05 18:36:14 -040018#include "controlvmchannel.h"
Jes Sorensen70236382015-05-05 18:35:40 -040019#include "version.h"
Ken Cox12e364b2014-03-04 07:58:07 -060020#include "procobjecttree.h"
Don Zickusf6439212015-05-05 18:36:02 -040021#include "visorbus.h"
Ken Cox12e364b2014-03-04 07:58:07 -060022#include "periodic_work.h"
Ken Cox12e364b2014-03-04 07:58:07 -060023#include "uisutils.h"
Ken Cox12e364b2014-03-04 07:58:07 -060024#include "controlvmcompletionstatus.h"
25#include "guestlinuxdebug.h"
Prarit Bhargavac79b28f2015-05-05 18:36:15 -040026#include "visorbus_private.h"
Ken Cox12e364b2014-03-04 07:58:07 -060027
Erik Arfvidson46168812015-05-05 18:36:14 -040028#include <linux/ctype.h>
Erik Arfvidsone3420ed2015-05-05 18:36:13 -040029#include <linux/fs.h>
30#include <linux/mm.h>
Ken Cox12e364b2014-03-04 07:58:07 -060031#include <linux/nls.h>
32#include <linux/netdevice.h>
33#include <linux/platform_device.h>
Benjamin Romer90addb02014-05-06 09:58:23 -040034#include <linux/uuid.h>
Benjamin Romer1ba00982015-04-06 10:27:40 -040035#include <linux/crash_dump.h>
Ken Cox12e364b2014-03-04 07:58:07 -060036
37#define CURRENT_FILE_PC VISOR_CHIPSET_PC_visorchipset_main_c
Ken Cox12e364b2014-03-04 07:58:07 -060038
39#define MAX_NAME_SIZE 128
40#define MAX_IP_SIZE 50
41#define MAXOUTSTANDINGCHANNELCOMMAND 256
42#define POLLJIFFIES_CONTROLVMCHANNEL_FAST 1
43#define POLLJIFFIES_CONTROLVMCHANNEL_SLOW 100
44
Erik Arfvidson46168812015-05-05 18:36:14 -040045#define MAX_CONTROLVM_PAYLOAD_BYTES (1024*128)
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -040046
47#define VISORCHIPSET_MMAP_CONTROLCHANOFFSET 0x00000000
48
Jes Sorensenb615d622015-05-05 18:35:38 -040049/*
50 * Module parameters
51 */
Jes Sorensenb615d622015-05-05 18:35:38 -040052static int visorchipset_major;
David Kershner4da33362015-05-05 18:36:39 -040053static int visorchipset_visorbusregwait = 1; /* default is on */
Jes Sorensenb615d622015-05-05 18:35:38 -040054static int visorchipset_holdchipsetready;
Erik Arfvidson46168812015-05-05 18:36:14 -040055static unsigned long controlvm_payload_bytes_buffered;
Jes Sorensenb615d622015-05-05 18:35:38 -040056
Erik Arfvidsone3420ed2015-05-05 18:36:13 -040057static int
58visorchipset_open(struct inode *inode, struct file *file)
59{
60 unsigned minor_number = iminor(inode);
61
62 if (minor_number)
63 return -ENODEV;
64 file->private_data = NULL;
65 return 0;
66}
67
68static int
69visorchipset_release(struct inode *inode, struct file *file)
70{
71 return 0;
72}
73
Ken Cox12e364b2014-03-04 07:58:07 -060074/* When the controlvm channel is idle for at least MIN_IDLE_SECONDS,
75* we switch to slow polling mode. As soon as we get a controlvm
76* message, we switch back to fast polling mode.
77*/
78#define MIN_IDLE_SECONDS 10
Jes Sorensen52063ec2015-04-13 10:28:41 -040079static unsigned long poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
80static unsigned long most_recent_message_jiffies; /* when we got our last
Ken Coxbd5b9b32014-03-13 15:39:22 -050081 * controlvm message */
David Kershner4da33362015-05-05 18:36:39 -040082static int visorbusregistered;
Ken Cox12e364b2014-03-04 07:58:07 -060083
84#define MAX_CHIPSET_EVENTS 2
Benjamin Romerc2422332014-07-29 15:09:40 -040085static u8 chipset_events[MAX_CHIPSET_EVENTS] = { 0, 0 };
Ken Cox12e364b2014-03-04 07:58:07 -060086
Erik Arfvidson46168812015-05-05 18:36:14 -040087struct parser_context {
88 unsigned long allocbytes;
89 unsigned long param_bytes;
90 u8 *curr;
91 unsigned long bytes_remaining;
92 bool byte_stream;
93 char data[0];
94};
95
Benjamin Romer9232d2d2015-03-16 13:57:57 -040096static struct delayed_work periodic_controlvm_work;
97static struct workqueue_struct *periodic_controlvm_workqueue;
Benjamin Romer8f1947a2015-03-16 13:57:59 -040098static DEFINE_SEMAPHORE(notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -060099
Erik Arfvidsone3420ed2015-05-05 18:36:13 -0400100static struct cdev file_cdev;
101static struct visorchannel **file_controlvm_channel;
Benjamin Romerda021f02015-03-16 13:57:58 -0400102static struct controlvm_message_header g_chipset_msg_hdr;
Benjamin Romer59827f02015-03-16 13:58:00 -0400103static const uuid_le spar_diag_pool_channel_protocol_uuid =
Benjamin Romer9eee5d12014-10-23 14:29:47 -0400104 SPAR_DIAG_POOL_CHANNEL_PROTOCOL_UUID;
Ken Cox12e364b2014-03-04 07:58:07 -0600105/* 0xffffff is an invalid Bus/Device number */
Jes Sorensen52063ec2015-04-13 10:28:41 -0400106static u32 g_diagpool_bus_no = 0xffffff;
107static u32 g_diagpool_dev_no = 0xffffff;
Benjamin Romer4f44b722015-03-16 13:58:02 -0400108static struct controlvm_message_packet g_devicechangestate_packet;
Ken Cox12e364b2014-03-04 07:58:07 -0600109
Ken Cox12e364b2014-03-04 07:58:07 -0600110#define is_diagpool_channel(channel_type_guid) \
Benjamin Romer59827f02015-03-16 13:58:00 -0400111 (uuid_le_cmp(channel_type_guid,\
112 spar_diag_pool_channel_protocol_uuid) == 0)
Ken Cox12e364b2014-03-04 07:58:07 -0600113
Benjamin Romer1390b882015-03-16 13:58:03 -0400114static LIST_HEAD(bus_info_list);
115static LIST_HEAD(dev_info_list);
Ken Cox12e364b2014-03-04 07:58:07 -0600116
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400117static struct visorchannel *controlvm_channel;
Ken Cox12e364b2014-03-04 07:58:07 -0600118
Benjamin Romer84982fb2015-03-16 13:58:07 -0400119/* Manages the request payload in the controlvm channel */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400120struct visor_controlvm_payload_info {
Benjamin Romerc2422332014-07-29 15:09:40 -0400121 u8 __iomem *ptr; /* pointer to base address of payload pool */
Benjamin Romer5fc02292014-07-31 12:00:51 -0400122 u64 offset; /* offset from beginning of controlvm
Ken Cox12e364b2014-03-04 07:58:07 -0600123 * channel to beginning of payload * pool */
Benjamin Romerb3c55b12014-07-31 12:00:50 -0400124 u32 bytes; /* number of bytes in payload pool */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400125};
126
127static struct visor_controlvm_payload_info controlvm_payload_info;
Ken Cox12e364b2014-03-04 07:58:07 -0600128
Benjamin Romerea33b4ee52015-03-16 13:58:08 -0400129/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
130 * CONTROLVM_DUMP_GETTEXTDUMP / CONTROLVM_DUMP_COMPLETE conversation.
131 */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400132struct visor_livedump_info {
Benjamin Romerea33b4ee52015-03-16 13:58:08 -0400133 struct controlvm_message_header dumpcapture_header;
134 struct controlvm_message_header gettextdump_header;
135 struct controlvm_message_header dumpcomplete_header;
Jes Sorensenf4c11552015-04-13 10:28:40 -0400136 bool gettextdump_outstanding;
Ken Cox12e364b2014-03-04 07:58:07 -0600137 u32 crc32;
Jes Sorensen52063ec2015-04-13 10:28:41 -0400138 unsigned long length;
Ken Cox12e364b2014-03-04 07:58:07 -0600139 atomic_t buffers_in_use;
Jes Sorensen52063ec2015-04-13 10:28:41 -0400140 unsigned long destination;
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400141};
142
143static struct visor_livedump_info livedump_info;
Ken Cox12e364b2014-03-04 07:58:07 -0600144
145/* The following globals are used to handle the scenario where we are unable to
146 * offload the payload from a controlvm message due to memory requirements. In
147 * this scenario, we simply stash the controlvm message, then attempt to
148 * process it again the next time controlvm_periodic_work() runs.
149 */
Benjamin Romer7166ed12015-03-16 13:58:38 -0400150static struct controlvm_message controlvm_pending_msg;
Prarit Bhargavac79b28f2015-05-05 18:36:15 -0400151static bool controlvm_pending_msg_valid;
Ken Cox12e364b2014-03-04 07:58:07 -0600152
Ken Cox12e364b2014-03-04 07:58:07 -0600153/* This identifies a data buffer that has been received via a controlvm messages
154 * in a remote --> local CONTROLVM_TRANSMIT_FILE conversation.
155 */
156struct putfile_buffer_entry {
157 struct list_head next; /* putfile_buffer_entry list */
Benjamin Romer317d9612015-03-16 13:57:51 -0400158 struct parser_context *parser_ctx; /* points to input data buffer */
Ken Cox12e364b2014-03-04 07:58:07 -0600159};
160
161/* List of struct putfile_request *, via next_putfile_request member.
162 * Each entry in this list identifies an outstanding TRANSMIT_FILE
163 * conversation.
164 */
Benjamin Romer1eee0012015-03-16 13:58:39 -0400165static LIST_HEAD(putfile_request_list);
Ken Cox12e364b2014-03-04 07:58:07 -0600166
167/* This describes a buffer and its current state of transfer (e.g., how many
168 * bytes have already been supplied as putfile data, and how many bytes are
169 * remaining) for a putfile_request.
170 */
171struct putfile_active_buffer {
172 /* a payload from a controlvm message, containing a file data buffer */
Benjamin Romer317d9612015-03-16 13:57:51 -0400173 struct parser_context *parser_ctx;
Ken Cox12e364b2014-03-04 07:58:07 -0600174 /* points within data area of parser_ctx to next byte of data */
175 u8 *pnext;
176 /* # bytes left from <pnext> to the end of this data buffer */
177 size_t bytes_remaining;
178};
179
180#define PUTFILE_REQUEST_SIG 0x0906101302281211
181/* This identifies a single remote --> local CONTROLVM_TRANSMIT_FILE
182 * conversation. Structs of this type are dynamically linked into
183 * <Putfile_request_list>.
184 */
185struct putfile_request {
186 u64 sig; /* PUTFILE_REQUEST_SIG */
187
188 /* header from original TransmitFile request */
Benjamin Romer98d7b592014-10-23 14:30:26 -0400189 struct controlvm_message_header controlvm_header;
Ken Cox12e364b2014-03-04 07:58:07 -0600190 u64 file_request_number; /* from original TransmitFile request */
191
192 /* link to next struct putfile_request */
193 struct list_head next_putfile_request;
194
195 /* most-recent sequence number supplied via a controlvm message */
196 u64 data_sequence_number;
197
198 /* head of putfile_buffer_entry list, which describes the data to be
199 * supplied as putfile data;
200 * - this list is added to when controlvm messages come in that supply
201 * file data
202 * - this list is removed from via the hotplug program that is actually
203 * consuming these buffers to write as file data */
204 struct list_head input_buffer_list;
205 spinlock_t req_list_lock; /* lock for input_buffer_list */
206
207 /* waiters for input_buffer_list to go non-empty */
208 wait_queue_head_t input_buffer_wq;
209
210 /* data not yet read within current putfile_buffer_entry */
211 struct putfile_active_buffer active_buf;
212
213 /* <0 = failed, 0 = in-progress, >0 = successful; */
214 /* note that this must be set with req_list_lock, and if you set <0, */
215 /* it is your responsibility to also free up all of the other objects */
216 /* in this struct (like input_buffer_list, active_buf.parser_ctx) */
217 /* before releasing the lock */
218 int completion_status;
219};
220
Ken Cox12e364b2014-03-04 07:58:07 -0600221struct parahotplug_request {
222 struct list_head list;
223 int id;
224 unsigned long expiration;
Benjamin Romer3ab47702014-10-23 14:30:31 -0400225 struct controlvm_message msg;
Ken Cox12e364b2014-03-04 07:58:07 -0600226};
227
Benjamin Romerddf5de52015-03-16 13:58:41 -0400228static LIST_HEAD(parahotplug_request_list);
229static DEFINE_SPINLOCK(parahotplug_request_list_lock); /* lock for above */
Ken Cox12e364b2014-03-04 07:58:07 -0600230static void parahotplug_process_list(void);
231
232/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
233 * CONTROLVM_REPORTEVENT.
234 */
David Kershner4da33362015-05-05 18:36:39 -0400235static struct visorchipset_busdev_notifiers busdev_notifiers;
Ken Cox12e364b2014-03-04 07:58:07 -0600236
Jes Sorensen52063ec2015-04-13 10:28:41 -0400237static void bus_create_response(u32 bus_no, int response);
238static void bus_destroy_response(u32 bus_no, int response);
239static void device_create_response(u32 bus_no, u32 dev_no, int response);
240static void device_destroy_response(u32 bus_no, u32 dev_no, int response);
241static void device_resume_response(u32 bus_no, u32 dev_no, int response);
Ken Cox12e364b2014-03-04 07:58:07 -0600242
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400243static void visorchipset_device_pause_response(u32 bus_no, u32 dev_no,
244 int response);
245
Benjamin Romer8e3fedd2015-03-16 13:58:43 -0400246static struct visorchipset_busdev_responders busdev_responders = {
Ken Cox12e364b2014-03-04 07:58:07 -0600247 .bus_create = bus_create_response,
248 .bus_destroy = bus_destroy_response,
249 .device_create = device_create_response,
250 .device_destroy = device_destroy_response,
Ken Cox927c7922014-03-05 14:52:25 -0600251 .device_pause = visorchipset_device_pause_response,
Ken Cox12e364b2014-03-04 07:58:07 -0600252 .device_resume = device_resume_response,
253};
254
255/* info for /dev/visorchipset */
Benjamin Romer5aa8ae52015-03-16 13:58:44 -0400256static dev_t major_dev = -1; /**< indicates major num for device */
Ken Cox12e364b2014-03-04 07:58:07 -0600257
Benjamin Romer19f66342014-07-22 09:56:25 -0400258/* prototypes for attributes */
259static ssize_t toolaction_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400260 struct device_attribute *attr, char *buf);
Benjamin Romer19f66342014-07-22 09:56:25 -0400261static ssize_t toolaction_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400262 struct device_attribute *attr,
263 const char *buf, size_t count);
Benjamin Romer19f66342014-07-22 09:56:25 -0400264static DEVICE_ATTR_RW(toolaction);
265
Benjamin Romer54b31222014-07-22 09:56:26 -0400266static ssize_t boottotool_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400267 struct device_attribute *attr, char *buf);
Benjamin Romer54b31222014-07-22 09:56:26 -0400268static ssize_t boottotool_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400269 struct device_attribute *attr, const char *buf,
270 size_t count);
Benjamin Romer54b31222014-07-22 09:56:26 -0400271static DEVICE_ATTR_RW(boottotool);
272
Benjamin Romer422af172014-07-24 14:08:42 -0400273static ssize_t error_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400274 char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400275static ssize_t error_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400276 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400277static DEVICE_ATTR_RW(error);
278
279static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400280 char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400281static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400282 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400283static DEVICE_ATTR_RW(textid);
284
285static ssize_t remaining_steps_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400286 struct device_attribute *attr, char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400287static ssize_t remaining_steps_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400288 struct device_attribute *attr,
289 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400290static DEVICE_ATTR_RW(remaining_steps);
291
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400292static ssize_t chipsetready_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400293 struct device_attribute *attr,
294 const char *buf, size_t count);
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400295static DEVICE_ATTR_WO(chipsetready);
296
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400297static ssize_t devicedisabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400298 struct device_attribute *attr,
299 const char *buf, size_t count);
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400300static DEVICE_ATTR_WO(devicedisabled);
301
302static ssize_t deviceenabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400303 struct device_attribute *attr,
304 const char *buf, size_t count);
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400305static DEVICE_ATTR_WO(deviceenabled);
306
Benjamin Romer19f66342014-07-22 09:56:25 -0400307static struct attribute *visorchipset_install_attrs[] = {
308 &dev_attr_toolaction.attr,
Benjamin Romer54b31222014-07-22 09:56:26 -0400309 &dev_attr_boottotool.attr,
Benjamin Romer422af172014-07-24 14:08:42 -0400310 &dev_attr_error.attr,
311 &dev_attr_textid.attr,
312 &dev_attr_remaining_steps.attr,
Benjamin Romer19f66342014-07-22 09:56:25 -0400313 NULL
314};
315
316static struct attribute_group visorchipset_install_group = {
317 .name = "install",
318 .attrs = visorchipset_install_attrs
319};
320
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400321static struct attribute *visorchipset_guest_attrs[] = {
322 &dev_attr_chipsetready.attr,
323 NULL
324};
325
326static struct attribute_group visorchipset_guest_group = {
327 .name = "guest",
328 .attrs = visorchipset_guest_attrs
329};
330
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400331static struct attribute *visorchipset_parahotplug_attrs[] = {
332 &dev_attr_devicedisabled.attr,
333 &dev_attr_deviceenabled.attr,
334 NULL
335};
336
337static struct attribute_group visorchipset_parahotplug_group = {
338 .name = "parahotplug",
339 .attrs = visorchipset_parahotplug_attrs
340};
341
Benjamin Romer19f66342014-07-22 09:56:25 -0400342static const struct attribute_group *visorchipset_dev_groups[] = {
343 &visorchipset_install_group,
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400344 &visorchipset_guest_group,
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400345 &visorchipset_parahotplug_group,
Benjamin Romer19f66342014-07-22 09:56:25 -0400346 NULL
347};
348
Ken Cox12e364b2014-03-04 07:58:07 -0600349/* /sys/devices/platform/visorchipset */
Benjamin Romereb34e872015-03-16 13:58:45 -0400350static struct platform_device visorchipset_platform_device = {
Ken Cox12e364b2014-03-04 07:58:07 -0600351 .name = "visorchipset",
352 .id = -1,
Benjamin Romer19f66342014-07-22 09:56:25 -0400353 .dev.groups = visorchipset_dev_groups,
Ken Cox12e364b2014-03-04 07:58:07 -0600354};
355
356/* Function prototypes */
Benjamin Romerb3168c72015-03-16 13:58:46 -0400357static void controlvm_respond(struct controlvm_message_header *msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400358 int response);
359static void controlvm_respond_chipset_init(
Benjamin Romerb3168c72015-03-16 13:58:46 -0400360 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400361 enum ultra_chipset_feature features);
362static void controlvm_respond_physdev_changestate(
Benjamin Romerb3168c72015-03-16 13:58:46 -0400363 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400364 struct spar_segment_state state);
Ken Cox12e364b2014-03-04 07:58:07 -0600365
Erik Arfvidson46168812015-05-05 18:36:14 -0400366
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400367static void parser_done(struct parser_context *ctx);
368
Erik Arfvidson46168812015-05-05 18:36:14 -0400369static struct parser_context *
370parser_init_guts(u64 addr, u32 bytes, bool local,
371 bool standard_payload_header, bool *retry)
372{
373 int allocbytes = sizeof(struct parser_context) + bytes;
374 struct parser_context *rc = NULL;
375 struct parser_context *ctx = NULL;
Erik Arfvidson46168812015-05-05 18:36:14 -0400376 struct spar_controlvm_parameters_header *phdr = NULL;
377
378 if (retry)
379 *retry = false;
380 if (!standard_payload_header)
381 /* alloc and 0 extra byte to ensure payload is
382 * '\0'-terminated
383 */
384 allocbytes++;
385 if ((controlvm_payload_bytes_buffered + bytes)
386 > MAX_CONTROLVM_PAYLOAD_BYTES) {
387 if (retry)
388 *retry = true;
389 rc = NULL;
390 goto cleanup;
391 }
392 ctx = kzalloc(allocbytes, GFP_KERNEL|__GFP_NORETRY);
393 if (!ctx) {
394 if (retry)
395 *retry = true;
396 rc = NULL;
397 goto cleanup;
398 }
399
400 ctx->allocbytes = allocbytes;
401 ctx->param_bytes = bytes;
402 ctx->curr = NULL;
403 ctx->bytes_remaining = 0;
404 ctx->byte_stream = false;
405 if (local) {
406 void *p;
407
408 if (addr > virt_to_phys(high_memory - 1)) {
409 rc = NULL;
410 goto cleanup;
411 }
412 p = __va((unsigned long) (addr));
413 memcpy(ctx->data, p, bytes);
414 } else {
Jes Sorensendd412752015-05-05 18:36:26 -0400415 void __iomem *mapping;
Jes Sorensen712c03d2015-05-05 18:36:25 -0400416
Jes Sorensendd412752015-05-05 18:36:26 -0400417 if (!request_mem_region(addr, bytes, "visorchipset")) {
Erik Arfvidson46168812015-05-05 18:36:14 -0400418 rc = NULL;
419 goto cleanup;
420 }
Jes Sorensendd412752015-05-05 18:36:26 -0400421
422 mapping = ioremap_cache(addr, bytes);
423 if (!mapping) {
424 release_mem_region(addr, bytes);
425 rc = NULL;
426 goto cleanup;
427 }
428 memcpy_fromio(ctx->data, mapping, bytes);
429 release_mem_region(addr, bytes);
Erik Arfvidson46168812015-05-05 18:36:14 -0400430 }
431 if (!standard_payload_header) {
432 ctx->byte_stream = true;
433 rc = ctx;
434 goto cleanup;
435 }
436 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
437 if (phdr->total_length != bytes) {
438 rc = NULL;
439 goto cleanup;
440 }
441 if (phdr->total_length < phdr->header_length) {
442 rc = NULL;
443 goto cleanup;
444 }
445 if (phdr->header_length <
446 sizeof(struct spar_controlvm_parameters_header)) {
447 rc = NULL;
448 goto cleanup;
449 }
450
451 rc = ctx;
452cleanup:
Erik Arfvidson46168812015-05-05 18:36:14 -0400453 if (rc) {
454 controlvm_payload_bytes_buffered += ctx->param_bytes;
455 } else {
456 if (ctx) {
457 parser_done(ctx);
458 ctx = NULL;
459 }
460 }
461 return rc;
462}
463
464struct parser_context *
465parser_init(u64 addr, u32 bytes, bool local, bool *retry)
466{
467 return parser_init_guts(addr, bytes, local, true, retry);
468}
469
470/* Call this instead of parser_init() if the payload area consists of just
471 * a sequence of bytes, rather than a struct spar_controlvm_parameters_header
472 * structures. Afterwards, you can call parser_simpleString_get() or
473 * parser_byteStream_get() to obtain the data.
474 */
475struct parser_context *
476parser_init_byte_stream(u64 addr, u32 bytes, bool local, bool *retry)
477{
478 return parser_init_guts(addr, bytes, local, false, retry);
479}
480
481/* Obtain '\0'-terminated copy of string in payload area.
482 */
483char *
484parser_simpleString_get(struct parser_context *ctx)
485{
486 if (!ctx->byte_stream)
487 return NULL;
488 return ctx->data; /* note this IS '\0'-terminated, because of
489 * the num of bytes we alloc+clear in
490 * parser_init_byteStream() */
491}
492
493/* Obtain a copy of the buffer in the payload area.
494 */
495void *parser_byte_stream_get(struct parser_context *ctx, unsigned long *nbytes)
496{
497 if (!ctx->byte_stream)
498 return NULL;
499 if (nbytes)
500 *nbytes = ctx->param_bytes;
501 return (void *)ctx->data;
502}
503
504uuid_le
505parser_id_get(struct parser_context *ctx)
506{
507 struct spar_controlvm_parameters_header *phdr = NULL;
508
509 if (ctx == NULL)
510 return NULL_UUID_LE;
511 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
512 return phdr->id;
513}
514
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400515/** Describes the state from the perspective of which controlvm messages have
516 * been received for a bus or device.
517 */
518
519enum PARSER_WHICH_STRING {
520 PARSERSTRING_INITIATOR,
521 PARSERSTRING_TARGET,
522 PARSERSTRING_CONNECTION,
523 PARSERSTRING_NAME, /* TODO: only PARSERSTRING_NAME is used ? */
524};
525
Erik Arfvidson46168812015-05-05 18:36:14 -0400526void
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400527parser_param_start(struct parser_context *ctx,
528 enum PARSER_WHICH_STRING which_string)
Erik Arfvidson46168812015-05-05 18:36:14 -0400529{
530 struct spar_controlvm_parameters_header *phdr = NULL;
531
532 if (ctx == NULL)
533 goto Away;
534 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
535 switch (which_string) {
536 case PARSERSTRING_INITIATOR:
537 ctx->curr = ctx->data + phdr->initiator_offset;
538 ctx->bytes_remaining = phdr->initiator_length;
539 break;
540 case PARSERSTRING_TARGET:
541 ctx->curr = ctx->data + phdr->target_offset;
542 ctx->bytes_remaining = phdr->target_length;
543 break;
544 case PARSERSTRING_CONNECTION:
545 ctx->curr = ctx->data + phdr->connection_offset;
546 ctx->bytes_remaining = phdr->connection_length;
547 break;
548 case PARSERSTRING_NAME:
549 ctx->curr = ctx->data + phdr->name_offset;
550 ctx->bytes_remaining = phdr->name_length;
551 break;
552 default:
553 break;
554 }
555
556Away:
557 return;
558}
559
560void
561parser_done(struct parser_context *ctx)
562{
563 if (!ctx)
564 return;
565 controlvm_payload_bytes_buffered -= ctx->param_bytes;
566 kfree(ctx);
567}
568
569/** Return length of string not counting trailing spaces. */
570static int
571string_length_no_trail(char *s, int len)
572{
573 int i = len - 1;
574
575 while (i >= 0) {
576 if (!isspace(s[i]))
577 return i + 1;
578 i--;
579 }
580 return 0;
581}
582
583/** Grab the next name and value out of the parameter buffer.
584 * The entire parameter buffer looks like this:
585 * <name>=<value>\0
586 * <name>=<value>\0
587 * ...
588 * \0
589 * If successful, the next <name> value is returned within the supplied
590 * <nam> buffer (the value is always upper-cased), and the corresponding
591 * <value> is returned within a kmalloc()ed buffer, whose pointer is
592 * provided as the return value of this function.
593 * (The total number of bytes allocated is strlen(<value>)+1.)
594 *
595 * NULL is returned to indicate failure, which can occur for several reasons:
596 * - all <name>=<value> pairs have already been processed
597 * - bad parameter
598 * - parameter buffer ends prematurely (couldn't find an '=' or '\0' within
599 * the confines of the parameter buffer)
600 * - the <nam> buffer is not large enough to hold the <name> of the next
601 * parameter
602 */
603void *
604parser_param_get(struct parser_context *ctx, char *nam, int namesize)
605{
606 u8 *pscan, *pnam = nam;
607 unsigned long nscan;
608 int value_length = -1, orig_value_length = -1;
609 void *value = NULL;
610 int i;
611 int closing_quote = 0;
612
613 if (!ctx)
614 return NULL;
615 pscan = ctx->curr;
616 nscan = ctx->bytes_remaining;
617 if (nscan == 0)
618 return NULL;
619 if (*pscan == '\0')
620 /* This is the normal return point after you have processed
621 * all of the <name>=<value> pairs in a syntactically-valid
622 * parameter buffer.
623 */
624 return NULL;
625
626 /* skip whitespace */
627 while (isspace(*pscan)) {
628 pscan++;
629 nscan--;
630 if (nscan == 0)
631 return NULL;
632 }
633
634 while (*pscan != ':') {
635 if (namesize <= 0)
636 return NULL;
637 *pnam = toupper(*pscan);
638 pnam++;
639 namesize--;
640 pscan++;
641 nscan--;
642 if (nscan == 0)
643 return NULL;
644 }
645 if (namesize <= 0)
646 return NULL;
647 *pnam = '\0';
648 nam[string_length_no_trail(nam, strlen(nam))] = '\0';
649
650 /* point to char immediately after ":" in "<name>:<value>" */
651 pscan++;
652 nscan--;
653 /* skip whitespace */
654 while (isspace(*pscan)) {
655 pscan++;
656 nscan--;
657 if (nscan == 0)
658 return NULL;
659 }
660 if (nscan == 0)
661 return NULL;
662 if (*pscan == '\'' || *pscan == '"') {
663 closing_quote = *pscan;
664 pscan++;
665 nscan--;
666 if (nscan == 0)
667 return NULL;
668 }
669
670 /* look for a separator character, terminator character, or
671 * end of data
672 */
673 for (i = 0, value_length = -1; i < nscan; i++) {
674 if (closing_quote) {
675 if (pscan[i] == '\0')
676 return NULL;
677 if (pscan[i] == closing_quote) {
678 value_length = i;
679 break;
680 }
681 } else
682 if (pscan[i] == ',' || pscan[i] == ';'
683 || pscan[i] == '\0') {
684 value_length = i;
685 break;
686 }
687 }
688 if (value_length < 0) {
689 if (closing_quote)
690 return NULL;
691 value_length = nscan;
692 }
693 orig_value_length = value_length;
694 if (closing_quote == 0)
695 value_length = string_length_no_trail(pscan, orig_value_length);
696 value = kmalloc(value_length + 1, GFP_KERNEL|__GFP_NORETRY);
697 if (value == NULL)
698 return NULL;
699 memcpy(value, pscan, value_length);
700 ((u8 *) (value))[value_length] = '\0';
701
702 pscan += orig_value_length;
703 nscan -= orig_value_length;
704
705 /* skip past separator or closing quote */
706 if (nscan > 0) {
707 if (*pscan != '\0') {
708 pscan++;
709 nscan--;
710 }
711 }
712
713 if (closing_quote && (nscan > 0)) {
714 /* we still need to skip around the real separator if present */
715 /* first, skip whitespace */
716 while (isspace(*pscan)) {
717 pscan++;
718 nscan--;
719 if (nscan == 0)
720 break;
721 }
722 if (nscan > 0) {
723 if (*pscan == ',' || *pscan == ';') {
724 pscan++;
725 nscan--;
726 } else if (*pscan != '\0') {
727 kfree(value);
728 value = NULL;
729 return NULL;
730 }
731 }
732 }
733 ctx->curr = pscan;
734 ctx->bytes_remaining = nscan;
735 return value;
736}
737
738void *
739parser_string_get(struct parser_context *ctx)
740{
741 u8 *pscan;
742 unsigned long nscan;
743 int value_length = -1;
744 void *value = NULL;
745 int i;
746
747 if (!ctx)
748 return NULL;
749 pscan = ctx->curr;
750 nscan = ctx->bytes_remaining;
751 if (nscan == 0)
752 return NULL;
753 if (!pscan)
754 return NULL;
755 for (i = 0, value_length = -1; i < nscan; i++)
756 if (pscan[i] == '\0') {
757 value_length = i;
758 break;
759 }
760 if (value_length < 0) /* '\0' was not included in the length */
761 value_length = nscan;
762 value = kmalloc(value_length + 1, GFP_KERNEL|__GFP_NORETRY);
763 if (value == NULL)
764 return NULL;
765 if (value_length > 0)
766 memcpy(value, pscan, value_length);
767 ((u8 *) (value))[value_length] = '\0';
768 return value;
769}
770
771
Vincent Bernatd746cb52014-08-01 10:29:30 +0200772static ssize_t toolaction_show(struct device *dev,
773 struct device_attribute *attr,
774 char *buf)
Benjamin Romer19f66342014-07-22 09:56:25 -0400775{
Benjamin Romer01f4d852015-03-16 13:58:48 -0400776 u8 tool_action;
Benjamin Romer19f66342014-07-22 09:56:25 -0400777
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400778 visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400779 offsetof(struct spar_controlvm_channel_protocol,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400780 tool_action), &tool_action, sizeof(u8));
Benjamin Romer01f4d852015-03-16 13:58:48 -0400781 return scnprintf(buf, PAGE_SIZE, "%u\n", tool_action);
Benjamin Romer19f66342014-07-22 09:56:25 -0400782}
783
Vincent Bernatd746cb52014-08-01 10:29:30 +0200784static ssize_t toolaction_store(struct device *dev,
785 struct device_attribute *attr,
786 const char *buf, size_t count)
Benjamin Romer19f66342014-07-22 09:56:25 -0400787{
Benjamin Romer01f4d852015-03-16 13:58:48 -0400788 u8 tool_action;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400789 int ret;
Benjamin Romer19f66342014-07-22 09:56:25 -0400790
Jes Sorensenebec8962015-05-05 18:35:57 -0400791 if (kstrtou8(buf, 10, &tool_action))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400792 return -EINVAL;
793
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400794 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400795 offsetof(struct spar_controlvm_channel_protocol,
796 tool_action),
Benjamin Romer01f4d852015-03-16 13:58:48 -0400797 &tool_action, sizeof(u8));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400798
799 if (ret)
800 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400801 return count;
Benjamin Romer19f66342014-07-22 09:56:25 -0400802}
803
Vincent Bernatd746cb52014-08-01 10:29:30 +0200804static ssize_t boottotool_show(struct device *dev,
805 struct device_attribute *attr,
806 char *buf)
Benjamin Romer54b31222014-07-22 09:56:26 -0400807{
Benjamin Romer365522d2015-03-16 13:58:49 -0400808 struct efi_spar_indication efi_spar_indication;
Benjamin Romer54b31222014-07-22 09:56:26 -0400809
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400810 visorchannel_read(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400811 offsetof(struct spar_controlvm_channel_protocol,
812 efi_spar_ind), &efi_spar_indication,
813 sizeof(struct efi_spar_indication));
Benjamin Romer54b31222014-07-22 09:56:26 -0400814 return scnprintf(buf, PAGE_SIZE, "%u\n",
Benjamin Romer8e76e692015-03-16 13:58:52 -0400815 efi_spar_indication.boot_to_tool);
Benjamin Romer54b31222014-07-22 09:56:26 -0400816}
817
Vincent Bernatd746cb52014-08-01 10:29:30 +0200818static ssize_t boottotool_store(struct device *dev,
819 struct device_attribute *attr,
820 const char *buf, size_t count)
Benjamin Romer54b31222014-07-22 09:56:26 -0400821{
Benjamin Romer66e24b72014-07-25 13:55:10 -0400822 int val, ret;
Benjamin Romer365522d2015-03-16 13:58:49 -0400823 struct efi_spar_indication efi_spar_indication;
Benjamin Romer54b31222014-07-22 09:56:26 -0400824
Jes Sorensenebec8962015-05-05 18:35:57 -0400825 if (kstrtoint(buf, 10, &val))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400826 return -EINVAL;
827
Benjamin Romer365522d2015-03-16 13:58:49 -0400828 efi_spar_indication.boot_to_tool = val;
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400829 ret = visorchannel_write(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400830 offsetof(struct spar_controlvm_channel_protocol,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400831 efi_spar_ind), &(efi_spar_indication),
832 sizeof(struct efi_spar_indication));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400833
834 if (ret)
835 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400836 return count;
Benjamin Romer54b31222014-07-22 09:56:26 -0400837}
Benjamin Romer422af172014-07-24 14:08:42 -0400838
839static ssize_t error_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400840 char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400841{
842 u32 error;
843
Benjamin Romer8e76e692015-03-16 13:58:52 -0400844 visorchannel_read(controlvm_channel,
845 offsetof(struct spar_controlvm_channel_protocol,
846 installation_error),
847 &error, sizeof(u32));
Benjamin Romer422af172014-07-24 14:08:42 -0400848 return scnprintf(buf, PAGE_SIZE, "%i\n", error);
849}
850
851static ssize_t error_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400852 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400853{
854 u32 error;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400855 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400856
Jes Sorensenebec8962015-05-05 18:35:57 -0400857 if (kstrtou32(buf, 10, &error))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400858 return -EINVAL;
859
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400860 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400861 offsetof(struct spar_controlvm_channel_protocol,
862 installation_error),
863 &error, sizeof(u32));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400864 if (ret)
865 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400866 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400867}
868
869static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400870 char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400871{
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400872 u32 text_id;
Benjamin Romer422af172014-07-24 14:08:42 -0400873
Benjamin Romer8e76e692015-03-16 13:58:52 -0400874 visorchannel_read(controlvm_channel,
875 offsetof(struct spar_controlvm_channel_protocol,
876 installation_text_id),
877 &text_id, sizeof(u32));
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400878 return scnprintf(buf, PAGE_SIZE, "%i\n", text_id);
Benjamin Romer422af172014-07-24 14:08:42 -0400879}
880
881static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400882 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400883{
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400884 u32 text_id;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400885 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400886
Jes Sorensenebec8962015-05-05 18:35:57 -0400887 if (kstrtou32(buf, 10, &text_id))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400888 return -EINVAL;
889
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400890 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400891 offsetof(struct spar_controlvm_channel_protocol,
892 installation_text_id),
893 &text_id, sizeof(u32));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400894 if (ret)
895 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400896 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400897}
898
Benjamin Romer422af172014-07-24 14:08:42 -0400899static ssize_t remaining_steps_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400900 struct device_attribute *attr, char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400901{
Benjamin Romeree8da292015-03-16 13:58:51 -0400902 u16 remaining_steps;
Benjamin Romer422af172014-07-24 14:08:42 -0400903
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400904 visorchannel_read(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400905 offsetof(struct spar_controlvm_channel_protocol,
906 installation_remaining_steps),
907 &remaining_steps, sizeof(u16));
Benjamin Romeree8da292015-03-16 13:58:51 -0400908 return scnprintf(buf, PAGE_SIZE, "%hu\n", remaining_steps);
Benjamin Romer422af172014-07-24 14:08:42 -0400909}
910
911static ssize_t remaining_steps_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400912 struct device_attribute *attr,
913 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400914{
Benjamin Romeree8da292015-03-16 13:58:51 -0400915 u16 remaining_steps;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400916 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400917
Jes Sorensenebec8962015-05-05 18:35:57 -0400918 if (kstrtou16(buf, 10, &remaining_steps))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400919 return -EINVAL;
920
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400921 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400922 offsetof(struct spar_controlvm_channel_protocol,
923 installation_remaining_steps),
924 &remaining_steps, sizeof(u16));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400925 if (ret)
926 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400927 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400928}
929
Ken Cox12e364b2014-03-04 07:58:07 -0600930static void
Benjamin Romer9b989a982015-03-16 13:58:11 -0400931bus_info_clear(void *v)
Ken Cox12e364b2014-03-04 07:58:07 -0600932{
Jes Sorensenbbd4be32015-05-05 18:35:43 -0400933 struct visorchipset_bus_info *p = (struct visorchipset_bus_info *) v;
Ken Cox12e364b2014-03-04 07:58:07 -0600934
Ken Cox12e364b2014-03-04 07:58:07 -0600935 kfree(p->name);
Ken Cox12e364b2014-03-04 07:58:07 -0600936 kfree(p->description);
Benjamin Romer33192fa2014-10-31 09:57:27 -0400937 memset(p, 0, sizeof(struct visorchipset_bus_info));
Ken Cox12e364b2014-03-04 07:58:07 -0600938}
939
940static void
Benjamin Romer9b989a982015-03-16 13:58:11 -0400941dev_info_clear(void *v)
Ken Cox12e364b2014-03-04 07:58:07 -0600942{
Benjamin Romer246e0cd2014-10-31 09:57:24 -0400943 struct visorchipset_device_info *p =
Jes Sorensenbbd4be32015-05-05 18:35:43 -0400944 (struct visorchipset_device_info *) v;
Benjamin Romer26eb2c02014-08-18 09:34:53 -0400945
Benjamin Romer246e0cd2014-10-31 09:57:24 -0400946 memset(p, 0, sizeof(struct visorchipset_device_info));
Ken Cox12e364b2014-03-04 07:58:07 -0600947}
948
Jes Sorensen4f665202015-05-05 18:35:52 -0400949static struct visorchipset_bus_info *
950bus_find(struct list_head *list, u32 bus_no)
951{
952 struct visorchipset_bus_info *p;
953
954 list_for_each_entry(p, list, entry) {
955 if (p->bus_no == bus_no)
956 return p;
957 }
958
959 return NULL;
960}
961
Jes Sorensend480f6a2015-05-05 18:35:54 -0400962static struct visorchipset_device_info *
963device_find(struct list_head *list, u32 bus_no, u32 dev_no)
964{
965 struct visorchipset_device_info *p;
966
967 list_for_each_entry(p, list, entry) {
968 if (p->bus_no == bus_no && p->dev_no == dev_no)
969 return p;
970 }
971
972 return NULL;
973}
974
Jes Sorensen28723522015-05-05 18:35:55 -0400975static void busdevices_del(struct list_head *list, u32 bus_no)
976{
977 struct visorchipset_device_info *p, *tmp;
978
979 list_for_each_entry_safe(p, tmp, list, entry) {
980 if (p->bus_no == bus_no) {
981 list_del(&p->entry);
982 kfree(p);
983 }
984 }
985}
986
Benjamin Romerc2422332014-07-29 15:09:40 -0400987static u8
Ken Cox12e364b2014-03-04 07:58:07 -0600988check_chipset_events(void)
989{
990 int i;
Benjamin Romerc2422332014-07-29 15:09:40 -0400991 u8 send_msg = 1;
Ken Cox12e364b2014-03-04 07:58:07 -0600992 /* Check events to determine if response should be sent */
993 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
994 send_msg &= chipset_events[i];
995 return send_msg;
996}
997
998static void
999clear_chipset_events(void)
1000{
1001 int i;
1002 /* Clear chipset_events */
1003 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
1004 chipset_events[i] = 0;
1005}
1006
1007void
David Kershner4da33362015-05-05 18:36:39 -04001008visorchipset_register_busdev(
Benjamin Romerfe90d892014-10-31 09:57:32 -04001009 struct visorchipset_busdev_notifiers *notifiers,
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001010 struct visorchipset_busdev_responders *responders,
Benjamin Romer1e7a59c2014-10-31 09:57:35 -04001011 struct ultra_vbus_deviceinfo *driver_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001012{
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001013 down(&notifier_lock);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001014 if (!notifiers) {
David Kershner4da33362015-05-05 18:36:39 -04001015 memset(&busdev_notifiers, 0,
1016 sizeof(busdev_notifiers));
1017 visorbusregistered = 0; /* clear flag */
Ken Cox12e364b2014-03-04 07:58:07 -06001018 } else {
David Kershner4da33362015-05-05 18:36:39 -04001019 busdev_notifiers = *notifiers;
1020 visorbusregistered = 1; /* set flag */
Ken Cox12e364b2014-03-04 07:58:07 -06001021 }
1022 if (responders)
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04001023 *responders = busdev_responders;
Benjamin Romer1e7a59c2014-10-31 09:57:35 -04001024 if (driver_info)
1025 bus_device_info_init(driver_info, "chipset", "visorchipset",
Benjamin Romer8e76e692015-03-16 13:58:52 -04001026 VERSION, NULL);
Ken Cox12e364b2014-03-04 07:58:07 -06001027
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001028 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001029}
David Kershner4da33362015-05-05 18:36:39 -04001030EXPORT_SYMBOL_GPL(visorchipset_register_busdev);
Ken Cox12e364b2014-03-04 07:58:07 -06001031
1032static void
1033cleanup_controlvm_structures(void)
1034{
Benjamin Romer33192fa2014-10-31 09:57:27 -04001035 struct visorchipset_bus_info *bi, *tmp_bi;
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001036 struct visorchipset_device_info *di, *tmp_di;
Ken Cox12e364b2014-03-04 07:58:07 -06001037
Benjamin Romer1390b882015-03-16 13:58:03 -04001038 list_for_each_entry_safe(bi, tmp_bi, &bus_info_list, entry) {
Benjamin Romer9b989a982015-03-16 13:58:11 -04001039 bus_info_clear(bi);
Ken Cox12e364b2014-03-04 07:58:07 -06001040 list_del(&bi->entry);
1041 kfree(bi);
1042 }
1043
Benjamin Romer1390b882015-03-16 13:58:03 -04001044 list_for_each_entry_safe(di, tmp_di, &dev_info_list, entry) {
Benjamin Romer9b989a982015-03-16 13:58:11 -04001045 dev_info_clear(di);
Ken Cox12e364b2014-03-04 07:58:07 -06001046 list_del(&di->entry);
1047 kfree(di);
1048 }
1049}
1050
1051static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001052chipset_init(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001053{
1054 static int chipset_inited;
Benjamin Romerb9b141e2014-10-23 14:30:24 -04001055 enum ultra_chipset_feature features = 0;
Ken Cox12e364b2014-03-04 07:58:07 -06001056 int rc = CONTROLVM_RESP_SUCCESS;
1057
1058 POSTCODE_LINUX_2(CHIPSET_INIT_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1059 if (chipset_inited) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001060 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romere3199b22015-03-16 13:58:14 -04001061 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001062 }
1063 chipset_inited = 1;
1064 POSTCODE_LINUX_2(CHIPSET_INIT_EXIT_PC, POSTCODE_SEVERITY_INFO);
1065
1066 /* Set features to indicate we support parahotplug (if Command
1067 * also supports it). */
1068 features =
Benjamin Romer2ea51172014-10-23 14:30:25 -04001069 inmsg->cmd.init_chipset.
Ken Cox12e364b2014-03-04 07:58:07 -06001070 features & ULTRA_CHIPSET_FEATURE_PARA_HOTPLUG;
1071
1072 /* Set the "reply" bit so Command knows this is a
1073 * features-aware driver. */
1074 features |= ULTRA_CHIPSET_FEATURE_REPLY;
1075
Benjamin Romere3199b22015-03-16 13:58:14 -04001076cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001077 if (rc < 0)
1078 cleanup_controlvm_structures();
Benjamin Romer98d7b592014-10-23 14:30:26 -04001079 if (inmsg->hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06001080 controlvm_respond_chipset_init(&inmsg->hdr, rc, features);
1081}
1082
1083static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001084controlvm_init_response(struct controlvm_message *msg,
Benjamin Romerb3168c72015-03-16 13:58:46 -04001085 struct controlvm_message_header *msg_hdr, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001086{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001087 memset(msg, 0, sizeof(struct controlvm_message));
Benjamin Romerb3168c72015-03-16 13:58:46 -04001088 memcpy(&msg->hdr, msg_hdr, sizeof(struct controlvm_message_header));
Benjamin Romer98d7b592014-10-23 14:30:26 -04001089 msg->hdr.payload_bytes = 0;
1090 msg->hdr.payload_vm_offset = 0;
1091 msg->hdr.payload_max_bytes = 0;
Ken Cox12e364b2014-03-04 07:58:07 -06001092 if (response < 0) {
Benjamin Romer98d7b592014-10-23 14:30:26 -04001093 msg->hdr.flags.failed = 1;
1094 msg->hdr.completion_status = (u32) (-response);
Ken Cox12e364b2014-03-04 07:58:07 -06001095 }
1096}
1097
1098static void
Benjamin Romerb3168c72015-03-16 13:58:46 -04001099controlvm_respond(struct controlvm_message_header *msg_hdr, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001100{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001101 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001102
Benjamin Romerb3168c72015-03-16 13:58:46 -04001103 controlvm_init_response(&outmsg, msg_hdr, response);
Ken Cox12e364b2014-03-04 07:58:07 -06001104 /* For DiagPool channel DEVICE_CHANGESTATE, we need to send
1105 * back the deviceChangeState structure in the packet. */
Benjamin Romerb3168c72015-03-16 13:58:46 -04001106 if (msg_hdr->id == CONTROLVM_DEVICE_CHANGESTATE &&
Benjamin Romer0639ba62015-03-16 13:58:04 -04001107 g_devicechangestate_packet.device_change_state.bus_no ==
1108 g_diagpool_bus_no &&
1109 g_devicechangestate_packet.device_change_state.dev_no ==
Benjamin Romer83d48902015-03-16 13:58:01 -04001110 g_diagpool_dev_no)
Benjamin Romer4f44b722015-03-16 13:58:02 -04001111 outmsg.cmd = g_devicechangestate_packet;
Benjamin Romer2098dbd2015-03-04 12:14:22 -05001112 if (outmsg.hdr.flags.test_message == 1)
Ken Cox12e364b2014-03-04 07:58:07 -06001113 return;
Benjamin Romer2098dbd2015-03-04 12:14:22 -05001114
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001115 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001116 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -06001117 return;
1118 }
1119}
1120
1121static void
Benjamin Romerb3168c72015-03-16 13:58:46 -04001122controlvm_respond_chipset_init(struct controlvm_message_header *msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001123 int response,
Benjamin Romerb9b141e2014-10-23 14:30:24 -04001124 enum ultra_chipset_feature features)
Ken Cox12e364b2014-03-04 07:58:07 -06001125{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001126 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001127
Benjamin Romerb3168c72015-03-16 13:58:46 -04001128 controlvm_init_response(&outmsg, msg_hdr, response);
Benjamin Romer2ea51172014-10-23 14:30:25 -04001129 outmsg.cmd.init_chipset.features = features;
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001130 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001131 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -06001132 return;
1133 }
1134}
1135
Benjamin Romer98d7b592014-10-23 14:30:26 -04001136static void controlvm_respond_physdev_changestate(
Benjamin Romerb3168c72015-03-16 13:58:46 -04001137 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001138 struct spar_segment_state state)
Ken Cox12e364b2014-03-04 07:58:07 -06001139{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001140 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001141
Benjamin Romerb3168c72015-03-16 13:58:46 -04001142 controlvm_init_response(&outmsg, msg_hdr, response);
Benjamin Romer2ea51172014-10-23 14:30:25 -04001143 outmsg.cmd.device_change_state.state = state;
1144 outmsg.cmd.device_change_state.flags.phys_device = 1;
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001145 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001146 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -06001147 return;
1148 }
1149}
1150
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -04001151enum crash_obj_type {
1152 CRASH_DEV,
1153 CRASH_BUS,
1154};
1155
Ken Cox12e364b2014-03-04 07:58:07 -06001156void
Benjamin Romer2c683cd2014-10-31 09:57:22 -04001157visorchipset_save_message(struct controlvm_message *msg,
1158 enum crash_obj_type type)
Ken Cox12e364b2014-03-04 07:58:07 -06001159{
Benjamin Romer45772252015-03-16 13:58:15 -04001160 u32 crash_msg_offset;
1161 u16 crash_msg_count;
Ken Cox12e364b2014-03-04 07:58:07 -06001162
1163 /* get saved message count */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001164 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001165 offsetof(struct spar_controlvm_channel_protocol,
1166 saved_crash_message_count),
Benjamin Romer45772252015-03-16 13:58:15 -04001167 &crash_msg_count, sizeof(u16)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001168 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
1169 POSTCODE_SEVERITY_ERR);
1170 return;
1171 }
1172
Benjamin Romer45772252015-03-16 13:58:15 -04001173 if (crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
Ken Cox12e364b2014-03-04 07:58:07 -06001174 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
Benjamin Romer45772252015-03-16 13:58:15 -04001175 crash_msg_count,
Ken Cox12e364b2014-03-04 07:58:07 -06001176 POSTCODE_SEVERITY_ERR);
1177 return;
1178 }
1179
1180 /* get saved crash message offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001181 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001182 offsetof(struct spar_controlvm_channel_protocol,
1183 saved_crash_message_offset),
Benjamin Romer45772252015-03-16 13:58:15 -04001184 &crash_msg_offset, sizeof(u32)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001185 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
1186 POSTCODE_SEVERITY_ERR);
1187 return;
1188 }
1189
Benjamin Romer2c683cd2014-10-31 09:57:22 -04001190 if (type == CRASH_BUS) {
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001191 if (visorchannel_write(controlvm_channel,
Benjamin Romer45772252015-03-16 13:58:15 -04001192 crash_msg_offset,
Benjamin Romer3ab47702014-10-23 14:30:31 -04001193 msg,
1194 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001195 POSTCODE_LINUX_2(SAVE_MSG_BUS_FAILURE_PC,
1196 POSTCODE_SEVERITY_ERR);
1197 return;
1198 }
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -04001199 } else { /* CRASH_DEV */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001200 if (visorchannel_write(controlvm_channel,
Benjamin Romer45772252015-03-16 13:58:15 -04001201 crash_msg_offset +
Benjamin Romer3ab47702014-10-23 14:30:31 -04001202 sizeof(struct controlvm_message), msg,
1203 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001204 POSTCODE_LINUX_2(SAVE_MSG_DEV_FAILURE_PC,
1205 POSTCODE_SEVERITY_ERR);
1206 return;
1207 }
1208 }
1209}
1210EXPORT_SYMBOL_GPL(visorchipset_save_message);
1211
1212static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04001213bus_responder(enum controlvm_id cmd_id, u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001214{
Jes Sorensene82ba622015-05-05 18:35:45 -04001215 struct visorchipset_bus_info *p;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001216 bool need_clear = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001217
Jes Sorensen4f665202015-05-05 18:35:52 -04001218 p = bus_find(&bus_info_list, bus_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001219 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001220 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001221
Ken Cox12e364b2014-03-04 07:58:07 -06001222 if (response < 0) {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001223 if ((cmd_id == CONTROLVM_BUS_CREATE) &&
Ken Cox12e364b2014-03-04 07:58:07 -06001224 (response != (-CONTROLVM_RESP_ERROR_ALREADY_DONE)))
1225 /* undo the row we just created... */
Jes Sorensen28723522015-05-05 18:35:55 -04001226 busdevices_del(&dev_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001227 } else {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001228 if (cmd_id == CONTROLVM_BUS_CREATE)
Ken Cox12e364b2014-03-04 07:58:07 -06001229 p->state.created = 1;
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001230 if (cmd_id == CONTROLVM_BUS_DESTROY)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001231 need_clear = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001232 }
1233
Benjamin Romer0aca78442015-03-04 12:14:25 -05001234 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001235 return; /* no controlvm response needed */
Benjamin Romer6b59b312015-03-16 13:58:18 -04001236 if (p->pending_msg_hdr.id != (u32)cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001237 return;
Benjamin Romer33192fa2014-10-31 09:57:27 -04001238 controlvm_respond(&p->pending_msg_hdr, response);
1239 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001240 if (need_clear) {
Benjamin Romer9b989a982015-03-16 13:58:11 -04001241 bus_info_clear(p);
Jes Sorensen28723522015-05-05 18:35:55 -04001242 busdevices_del(&dev_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001243 }
1244}
1245
1246static void
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001247device_changestate_responder(enum controlvm_id cmd_id,
Jes Sorensen52063ec2015-04-13 10:28:41 -04001248 u32 bus_no, u32 dev_no, int response,
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001249 struct spar_segment_state response_state)
Ken Cox12e364b2014-03-04 07:58:07 -06001250{
Jes Sorensene82ba622015-05-05 18:35:45 -04001251 struct visorchipset_device_info *p;
Benjamin Romer3ab47702014-10-23 14:30:31 -04001252 struct controlvm_message outmsg;
Ken Cox12e364b2014-03-04 07:58:07 -06001253
Jes Sorensend480f6a2015-05-05 18:35:54 -04001254 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001255 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001256 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001257 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001258 return; /* no controlvm response needed */
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001259 if (p->pending_msg_hdr.id != cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001260 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001261
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001262 controlvm_init_response(&outmsg, &p->pending_msg_hdr, response);
Ken Cox12e364b2014-03-04 07:58:07 -06001263
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001264 outmsg.cmd.device_change_state.bus_no = bus_no;
1265 outmsg.cmd.device_change_state.dev_no = dev_no;
1266 outmsg.cmd.device_change_state.state = response_state;
Ken Cox12e364b2014-03-04 07:58:07 -06001267
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001268 if (!visorchannel_signalinsert(controlvm_channel,
Benjamin Romer0aca78442015-03-04 12:14:25 -05001269 CONTROLVM_QUEUE_REQUEST, &outmsg))
Ken Cox12e364b2014-03-04 07:58:07 -06001270 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001271
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001272 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001273}
1274
1275static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04001276device_responder(enum controlvm_id cmd_id, u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001277{
Jes Sorensene82ba622015-05-05 18:35:45 -04001278 struct visorchipset_device_info *p;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001279 bool need_clear = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001280
Jes Sorensend480f6a2015-05-05 18:35:54 -04001281 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001282 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001283 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001284 if (response >= 0) {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001285 if (cmd_id == CONTROLVM_DEVICE_CREATE)
Ken Cox12e364b2014-03-04 07:58:07 -06001286 p->state.created = 1;
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001287 if (cmd_id == CONTROLVM_DEVICE_DESTROY)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001288 need_clear = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001289 }
1290
Benjamin Romer0aca78442015-03-04 12:14:25 -05001291 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001292 return; /* no controlvm response needed */
Benjamin Romer0aca78442015-03-04 12:14:25 -05001293
Benjamin Romer6b59b312015-03-16 13:58:18 -04001294 if (p->pending_msg_hdr.id != (u32)cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001295 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001296
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001297 controlvm_respond(&p->pending_msg_hdr, response);
1298 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001299 if (need_clear)
Benjamin Romer9b989a982015-03-16 13:58:11 -04001300 dev_info_clear(p);
Ken Cox12e364b2014-03-04 07:58:07 -06001301}
1302
1303static void
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001304bus_epilog(u32 bus_no,
1305 u32 cmd, struct controlvm_message_header *msg_hdr,
Jes Sorensenf4c11552015-04-13 10:28:40 -04001306 int response, bool need_response)
Ken Cox12e364b2014-03-04 07:58:07 -06001307{
Jes Sorensen4f665202015-05-05 18:35:52 -04001308 struct visorchipset_bus_info *bus_info;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001309 bool notified = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001310
Jes Sorensen4f665202015-05-05 18:35:52 -04001311 bus_info = bus_find(&bus_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001312
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001313 if (!bus_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001314 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001315
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001316 if (need_response) {
1317 memcpy(&bus_info->pending_msg_hdr, msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001318 sizeof(struct controlvm_message_header));
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001319 } else {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001320 bus_info->pending_msg_hdr.id = CONTROLVM_INVALID;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001321 }
Ken Cox12e364b2014-03-04 07:58:07 -06001322
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001323 down(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001324 if (response == CONTROLVM_RESP_SUCCESS) {
1325 switch (cmd) {
1326 case CONTROLVM_BUS_CREATE:
David Kershner4da33362015-05-05 18:36:39 -04001327 if (busdev_notifiers.bus_create) {
1328 (*busdev_notifiers.bus_create) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001329 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001330 }
1331 break;
1332 case CONTROLVM_BUS_DESTROY:
David Kershner4da33362015-05-05 18:36:39 -04001333 if (busdev_notifiers.bus_destroy) {
1334 (*busdev_notifiers.bus_destroy) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001335 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001336 }
1337 break;
1338 }
1339 }
1340 if (notified)
1341 /* The callback function just called above is responsible
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001342 * for calling the appropriate visorchipset_busdev_responders
Ken Cox12e364b2014-03-04 07:58:07 -06001343 * function, which will call bus_responder()
1344 */
1345 ;
1346 else
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001347 bus_responder(cmd, bus_no, response);
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001348 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001349}
1350
1351static void
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001352device_epilog(u32 bus_no, u32 dev_no, struct spar_segment_state state, u32 cmd,
1353 struct controlvm_message_header *msg_hdr, int response,
Jes Sorensenf4c11552015-04-13 10:28:40 -04001354 bool need_response, bool for_visorbus)
Ken Cox12e364b2014-03-04 07:58:07 -06001355{
Jes Sorensene82ba622015-05-05 18:35:45 -04001356 struct visorchipset_busdev_notifiers *notifiers;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001357 bool notified = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001358
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001359 struct visorchipset_device_info *dev_info =
Jes Sorensend480f6a2015-05-05 18:35:54 -04001360 device_find(&dev_info_list, bus_no, dev_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001361 char *envp[] = {
1362 "SPARSP_DIAGPOOL_PAUSED_STATE = 1",
1363 NULL
1364 };
1365
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001366 if (!dev_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001367 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001368
David Kershner4da33362015-05-05 18:36:39 -04001369 notifiers = &busdev_notifiers;
1370
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001371 if (need_response) {
1372 memcpy(&dev_info->pending_msg_hdr, msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001373 sizeof(struct controlvm_message_header));
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001374 } else {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001375 dev_info->pending_msg_hdr.id = CONTROLVM_INVALID;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001376 }
Ken Cox12e364b2014-03-04 07:58:07 -06001377
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001378 down(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001379 if (response >= 0) {
1380 switch (cmd) {
1381 case CONTROLVM_DEVICE_CREATE:
1382 if (notifiers->device_create) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001383 (*notifiers->device_create) (bus_no, dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001384 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001385 }
1386 break;
1387 case CONTROLVM_DEVICE_CHANGESTATE:
1388 /* ServerReady / ServerRunning / SegmentStateRunning */
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001389 if (state.alive == segment_state_running.alive &&
1390 state.operating ==
1391 segment_state_running.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001392 if (notifiers->device_resume) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001393 (*notifiers->device_resume) (bus_no,
1394 dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001395 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001396 }
1397 }
1398 /* ServerNotReady / ServerLost / SegmentStateStandby */
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001399 else if (state.alive == segment_state_standby.alive &&
Benjamin Romer3f833b52014-10-23 14:30:12 -04001400 state.operating ==
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001401 segment_state_standby.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001402 /* technically this is standby case
1403 * where server is lost
1404 */
1405 if (notifiers->device_pause) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001406 (*notifiers->device_pause) (bus_no,
1407 dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001408 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001409 }
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001410 } else if (state.alive == segment_state_paused.alive &&
Benjamin Romer3f833b52014-10-23 14:30:12 -04001411 state.operating ==
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001412 segment_state_paused.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001413 /* this is lite pause where channel is
1414 * still valid just 'pause' of it
1415 */
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001416 if (bus_no == g_diagpool_bus_no &&
1417 dev_no == g_diagpool_dev_no) {
Ken Cox12e364b2014-03-04 07:58:07 -06001418 /* this will trigger the
1419 * diag_shutdown.sh script in
1420 * the visorchipset hotplug */
1421 kobject_uevent_env
Benjamin Romereb34e872015-03-16 13:58:45 -04001422 (&visorchipset_platform_device.dev.
Ken Cox12e364b2014-03-04 07:58:07 -06001423 kobj, KOBJ_ONLINE, envp);
1424 }
1425 }
1426 break;
1427 case CONTROLVM_DEVICE_DESTROY:
1428 if (notifiers->device_destroy) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001429 (*notifiers->device_destroy) (bus_no, dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001430 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001431 }
1432 break;
1433 }
1434 }
1435 if (notified)
1436 /* The callback function just called above is responsible
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001437 * for calling the appropriate visorchipset_busdev_responders
Ken Cox12e364b2014-03-04 07:58:07 -06001438 * function, which will call device_responder()
1439 */
1440 ;
1441 else
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001442 device_responder(cmd, bus_no, dev_no, response);
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001443 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001444}
1445
1446static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001447bus_create(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001448{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001449 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001450 u32 bus_no = cmd->create_bus.bus_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001451 int rc = CONTROLVM_RESP_SUCCESS;
Jes Sorensene82ba622015-05-05 18:35:45 -04001452 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001453
Jes Sorensen4f665202015-05-05 18:35:52 -04001454 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001455 if (bus_info && (bus_info->state.created == 1)) {
1456 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001457 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001458 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001459 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001460 }
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001461 bus_info = kzalloc(sizeof(*bus_info), GFP_KERNEL);
1462 if (!bus_info) {
1463 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001464 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001465 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001466 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001467 }
1468
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001469 INIT_LIST_HEAD(&bus_info->entry);
1470 bus_info->bus_no = bus_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001471
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001472 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001473
Benjamin Romer98d7b592014-10-23 14:30:26 -04001474 if (inmsg->hdr.flags.test_message == 1)
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001475 bus_info->chan_info.addr_type = ADDRTYPE_LOCALTEST;
Ken Cox12e364b2014-03-04 07:58:07 -06001476 else
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001477 bus_info->chan_info.addr_type = ADDRTYPE_LOCALPHYSICAL;
Ken Cox12e364b2014-03-04 07:58:07 -06001478
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001479 bus_info->flags.server = inmsg->hdr.flags.server;
1480 bus_info->chan_info.channel_addr = cmd->create_bus.channel_addr;
1481 bus_info->chan_info.n_channel_bytes = cmd->create_bus.channel_bytes;
1482 bus_info->chan_info.channel_type_uuid =
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001483 cmd->create_bus.bus_data_type_uuid;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001484 bus_info->chan_info.channel_inst_uuid = cmd->create_bus.bus_inst_uuid;
Ken Cox12e364b2014-03-04 07:58:07 -06001485
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001486 list_add(&bus_info->entry, &bus_info_list);
Ken Cox12e364b2014-03-04 07:58:07 -06001487
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001488 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001489
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001490cleanup:
1491 bus_epilog(bus_no, CONTROLVM_BUS_CREATE, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001492 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001493}
1494
1495static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001496bus_destroy(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001497{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001498 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001499 u32 bus_no = cmd->destroy_bus.bus_no;
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001500 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001501 int rc = CONTROLVM_RESP_SUCCESS;
1502
Jes Sorensen4f665202015-05-05 18:35:52 -04001503 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001504 if (!bus_info)
Ken Cox22ad57b2014-03-19 13:06:25 -05001505 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001506 else if (bus_info->state.created == 0)
Ken Cox22ad57b2014-03-19 13:06:25 -05001507 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Ken Cox12e364b2014-03-04 07:58:07 -06001508
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001509 bus_epilog(bus_no, CONTROLVM_BUS_DESTROY, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001510 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001511}
1512
1513static void
Benjamin Romer317d9612015-03-16 13:57:51 -04001514bus_configure(struct controlvm_message *inmsg,
1515 struct parser_context *parser_ctx)
Ken Cox12e364b2014-03-04 07:58:07 -06001516{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001517 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensene82ba622015-05-05 18:35:45 -04001518 u32 bus_no;
1519 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001520 int rc = CONTROLVM_RESP_SUCCESS;
1521 char s[99];
1522
Benjamin Romer654bada2015-03-16 13:58:22 -04001523 bus_no = cmd->configure_bus.bus_no;
1524 POSTCODE_LINUX_3(BUS_CONFIGURE_ENTRY_PC, bus_no,
1525 POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001526
Jes Sorensen4f665202015-05-05 18:35:52 -04001527 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romer654bada2015-03-16 13:58:22 -04001528 if (!bus_info) {
1529 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001530 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001531 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romer654bada2015-03-16 13:58:22 -04001532 } else if (bus_info->state.created == 0) {
1533 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001534 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001535 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romer654bada2015-03-16 13:58:22 -04001536 } else if (bus_info->pending_msg_hdr.id != CONTROLVM_INVALID) {
1537 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001538 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001539 rc = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
Benjamin Romer654bada2015-03-16 13:58:22 -04001540 } else {
1541 bus_info->partition_handle = cmd->configure_bus.guest_handle;
1542 bus_info->partition_uuid = parser_id_get(parser_ctx);
1543 parser_param_start(parser_ctx, PARSERSTRING_NAME);
1544 bus_info->name = parser_string_get(parser_ctx);
1545
1546 visorchannel_uuid_id(&bus_info->partition_uuid, s);
1547 POSTCODE_LINUX_3(BUS_CONFIGURE_EXIT_PC, bus_no,
1548 POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001549 }
Benjamin Romer654bada2015-03-16 13:58:22 -04001550 bus_epilog(bus_no, CONTROLVM_BUS_CONFIGURE, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001551 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001552}
1553
1554static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001555my_device_create(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001556{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001557 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001558 u32 bus_no = cmd->create_device.bus_no;
1559 u32 dev_no = cmd->create_device.dev_no;
Jes Sorensene82ba622015-05-05 18:35:45 -04001560 struct visorchipset_device_info *dev_info;
1561 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001562 int rc = CONTROLVM_RESP_SUCCESS;
1563
Jes Sorensend480f6a2015-05-05 18:35:54 -04001564 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001565 if (dev_info && (dev_info->state.created == 1)) {
1566 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001567 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001568 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001569 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001570 }
Jes Sorensen4f665202015-05-05 18:35:52 -04001571 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001572 if (!bus_info) {
1573 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001574 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001575 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001576 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001577 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001578 if (bus_info->state.created == 0) {
1579 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001580 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001581 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001582 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001583 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001584 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
1585 if (!dev_info) {
1586 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001587 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001588 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001589 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001590 }
Andreea-Cristina Bernat97a84f12014-03-14 04:20:06 +02001591
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001592 INIT_LIST_HEAD(&dev_info->entry);
1593 dev_info->bus_no = bus_no;
1594 dev_info->dev_no = dev_no;
1595 dev_info->dev_inst_uuid = cmd->create_device.dev_inst_uuid;
1596 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001597 POSTCODE_SEVERITY_INFO);
1598
Benjamin Romer98d7b592014-10-23 14:30:26 -04001599 if (inmsg->hdr.flags.test_message == 1)
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001600 dev_info->chan_info.addr_type = ADDRTYPE_LOCALTEST;
Ken Cox12e364b2014-03-04 07:58:07 -06001601 else
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001602 dev_info->chan_info.addr_type = ADDRTYPE_LOCALPHYSICAL;
1603 dev_info->chan_info.channel_addr = cmd->create_device.channel_addr;
1604 dev_info->chan_info.n_channel_bytes = cmd->create_device.channel_bytes;
1605 dev_info->chan_info.channel_type_uuid =
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001606 cmd->create_device.data_type_uuid;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001607 dev_info->chan_info.intr = cmd->create_device.intr;
1608 list_add(&dev_info->entry, &dev_info_list);
1609 POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001610 POSTCODE_SEVERITY_INFO);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001611cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001612 /* get the bus and devNo for DiagPool channel */
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001613 if (dev_info &&
1614 is_diagpool_channel(dev_info->chan_info.channel_type_uuid)) {
1615 g_diagpool_bus_no = bus_no;
1616 g_diagpool_dev_no = dev_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001617 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001618 device_epilog(bus_no, dev_no, segment_state_running,
Ken Cox12e364b2014-03-04 07:58:07 -06001619 CONTROLVM_DEVICE_CREATE, &inmsg->hdr, rc,
David Kershner4da33362015-05-05 18:36:39 -04001620 inmsg->hdr.flags.response_expected == 1, 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001621}
1622
1623static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001624my_device_changestate(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001625{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001626 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001627 u32 bus_no = cmd->device_change_state.bus_no;
1628 u32 dev_no = cmd->device_change_state.dev_no;
Benjamin Romer2ea51172014-10-23 14:30:25 -04001629 struct spar_segment_state state = cmd->device_change_state.state;
Jes Sorensene82ba622015-05-05 18:35:45 -04001630 struct visorchipset_device_info *dev_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001631 int rc = CONTROLVM_RESP_SUCCESS;
1632
Jes Sorensend480f6a2015-05-05 18:35:54 -04001633 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0278a902015-03-16 13:58:24 -04001634 if (!dev_info) {
1635 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001636 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001637 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Benjamin Romer0278a902015-03-16 13:58:24 -04001638 } else if (dev_info->state.created == 0) {
1639 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001640 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001641 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001642 }
Benjamin Romer0278a902015-03-16 13:58:24 -04001643 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
1644 device_epilog(bus_no, dev_no, state,
1645 CONTROLVM_DEVICE_CHANGESTATE, &inmsg->hdr, rc,
David Kershner4da33362015-05-05 18:36:39 -04001646 inmsg->hdr.flags.response_expected == 1, 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001647}
1648
1649static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001650my_device_destroy(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001651{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001652 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001653 u32 bus_no = cmd->destroy_device.bus_no;
1654 u32 dev_no = cmd->destroy_device.dev_no;
Jes Sorensene82ba622015-05-05 18:35:45 -04001655 struct visorchipset_device_info *dev_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001656 int rc = CONTROLVM_RESP_SUCCESS;
1657
Jes Sorensend480f6a2015-05-05 18:35:54 -04001658 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer61715c82015-03-16 13:58:25 -04001659 if (!dev_info)
Ken Cox22ad57b2014-03-19 13:06:25 -05001660 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Benjamin Romer61715c82015-03-16 13:58:25 -04001661 else if (dev_info->state.created == 0)
Ken Cox22ad57b2014-03-19 13:06:25 -05001662 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Ken Cox12e364b2014-03-04 07:58:07 -06001663
Benjamin Romer61715c82015-03-16 13:58:25 -04001664 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
1665 device_epilog(bus_no, dev_no, segment_state_running,
Ken Cox12e364b2014-03-04 07:58:07 -06001666 CONTROLVM_DEVICE_DESTROY, &inmsg->hdr, rc,
David Kershner4da33362015-05-05 18:36:39 -04001667 inmsg->hdr.flags.response_expected == 1, 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001668}
1669
1670/* When provided with the physical address of the controlvm channel
1671 * (phys_addr), the offset to the payload area we need to manage
1672 * (offset), and the size of this payload area (bytes), fills in the
Jes Sorensenf4c11552015-04-13 10:28:40 -04001673 * controlvm_payload_info struct. Returns true for success or false
Ken Cox12e364b2014-03-04 07:58:07 -06001674 * for failure.
1675 */
1676static int
Benjamin Romer5fc02292014-07-31 12:00:51 -04001677initialize_controlvm_payload_info(HOSTADDRESS phys_addr, u64 offset, u32 bytes,
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001678 struct visor_controlvm_payload_info *info)
Ken Cox12e364b2014-03-04 07:58:07 -06001679{
Benjamin Romerc2422332014-07-29 15:09:40 -04001680 u8 __iomem *payload = NULL;
Ken Cox12e364b2014-03-04 07:58:07 -06001681 int rc = CONTROLVM_RESP_SUCCESS;
1682
Benjamin Romer38f736e2015-03-16 13:58:13 -04001683 if (!info) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001684 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
Benjamin Romerf118a392015-03-16 13:58:26 -04001685 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001686 }
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001687 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
Ken Cox12e364b2014-03-04 07:58:07 -06001688 if ((offset == 0) || (bytes == 0)) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001689 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
Benjamin Romerf118a392015-03-16 13:58:26 -04001690 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001691 }
1692 payload = ioremap_cache(phys_addr + offset, bytes);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001693 if (!payload) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001694 rc = -CONTROLVM_RESP_ERROR_IOREMAP_FAILED;
Benjamin Romerf118a392015-03-16 13:58:26 -04001695 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001696 }
1697
1698 info->offset = offset;
1699 info->bytes = bytes;
1700 info->ptr = payload;
Ken Cox12e364b2014-03-04 07:58:07 -06001701
Benjamin Romerf118a392015-03-16 13:58:26 -04001702cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001703 if (rc < 0) {
Benjamin Romerf118a392015-03-16 13:58:26 -04001704 if (payload) {
Ken Cox12e364b2014-03-04 07:58:07 -06001705 iounmap(payload);
1706 payload = NULL;
1707 }
1708 }
1709 return rc;
1710}
1711
1712static void
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001713destroy_controlvm_payload_info(struct visor_controlvm_payload_info *info)
Ken Cox12e364b2014-03-04 07:58:07 -06001714{
Benjamin Romer597c3382015-03-16 13:58:27 -04001715 if (info->ptr) {
Ken Cox12e364b2014-03-04 07:58:07 -06001716 iounmap(info->ptr);
1717 info->ptr = NULL;
1718 }
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001719 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
Ken Cox12e364b2014-03-04 07:58:07 -06001720}
1721
1722static void
1723initialize_controlvm_payload(void)
1724{
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001725 HOSTADDRESS phys_addr = visorchannel_get_physaddr(controlvm_channel);
Benjamin Romercafefc02015-03-16 13:58:28 -04001726 u64 payload_offset = 0;
1727 u32 payload_bytes = 0;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001728
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001729 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001730 offsetof(struct spar_controlvm_channel_protocol,
1731 request_payload_offset),
Benjamin Romercafefc02015-03-16 13:58:28 -04001732 &payload_offset, sizeof(payload_offset)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001733 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1734 POSTCODE_SEVERITY_ERR);
1735 return;
1736 }
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001737 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001738 offsetof(struct spar_controlvm_channel_protocol,
1739 request_payload_bytes),
Benjamin Romercafefc02015-03-16 13:58:28 -04001740 &payload_bytes, sizeof(payload_bytes)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001741 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1742 POSTCODE_SEVERITY_ERR);
1743 return;
1744 }
1745 initialize_controlvm_payload_info(phys_addr,
Benjamin Romercafefc02015-03-16 13:58:28 -04001746 payload_offset, payload_bytes,
Benjamin Romer84982fb2015-03-16 13:58:07 -04001747 &controlvm_payload_info);
Ken Cox12e364b2014-03-04 07:58:07 -06001748}
1749
1750/* Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
1751 * Returns CONTROLVM_RESP_xxx code.
1752 */
1753int
1754visorchipset_chipset_ready(void)
1755{
Benjamin Romereb34e872015-03-16 13:58:45 -04001756 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_ONLINE);
Ken Cox12e364b2014-03-04 07:58:07 -06001757 return CONTROLVM_RESP_SUCCESS;
1758}
1759EXPORT_SYMBOL_GPL(visorchipset_chipset_ready);
1760
1761int
1762visorchipset_chipset_selftest(void)
1763{
1764 char env_selftest[20];
1765 char *envp[] = { env_selftest, NULL };
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001766
Ken Cox12e364b2014-03-04 07:58:07 -06001767 sprintf(env_selftest, "SPARSP_SELFTEST=%d", 1);
Benjamin Romereb34e872015-03-16 13:58:45 -04001768 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
Ken Cox12e364b2014-03-04 07:58:07 -06001769 envp);
1770 return CONTROLVM_RESP_SUCCESS;
1771}
1772EXPORT_SYMBOL_GPL(visorchipset_chipset_selftest);
1773
1774/* Send ACTION=offline for DEVPATH=/sys/devices/platform/visorchipset.
1775 * Returns CONTROLVM_RESP_xxx code.
1776 */
1777int
1778visorchipset_chipset_notready(void)
1779{
Benjamin Romereb34e872015-03-16 13:58:45 -04001780 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_OFFLINE);
Ken Cox12e364b2014-03-04 07:58:07 -06001781 return CONTROLVM_RESP_SUCCESS;
1782}
1783EXPORT_SYMBOL_GPL(visorchipset_chipset_notready);
1784
1785static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001786chipset_ready(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001787{
1788 int rc = visorchipset_chipset_ready();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001789
Ken Cox12e364b2014-03-04 07:58:07 -06001790 if (rc != CONTROLVM_RESP_SUCCESS)
1791 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001792 if (msg_hdr->flags.response_expected && !visorchipset_holdchipsetready)
1793 controlvm_respond(msg_hdr, rc);
1794 if (msg_hdr->flags.response_expected && visorchipset_holdchipsetready) {
Ken Cox12e364b2014-03-04 07:58:07 -06001795 /* Send CHIPSET_READY response when all modules have been loaded
1796 * and disks mounted for the partition
1797 */
Benjamin Romer77a04492015-03-16 13:58:47 -04001798 g_chipset_msg_hdr = *msg_hdr;
Ken Cox12e364b2014-03-04 07:58:07 -06001799 }
1800}
1801
1802static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001803chipset_selftest(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001804{
1805 int rc = visorchipset_chipset_selftest();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001806
Ken Cox12e364b2014-03-04 07:58:07 -06001807 if (rc != CONTROLVM_RESP_SUCCESS)
1808 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001809 if (msg_hdr->flags.response_expected)
1810 controlvm_respond(msg_hdr, rc);
Ken Cox12e364b2014-03-04 07:58:07 -06001811}
1812
1813static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001814chipset_notready(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001815{
1816 int rc = visorchipset_chipset_notready();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001817
Ken Cox12e364b2014-03-04 07:58:07 -06001818 if (rc != CONTROLVM_RESP_SUCCESS)
1819 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001820 if (msg_hdr->flags.response_expected)
1821 controlvm_respond(msg_hdr, rc);
Ken Cox12e364b2014-03-04 07:58:07 -06001822}
1823
1824/* This is your "one-stop" shop for grabbing the next message from the
1825 * CONTROLVM_QUEUE_EVENT queue in the controlvm channel.
1826 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04001827static bool
Benjamin Romer3ab47702014-10-23 14:30:31 -04001828read_controlvm_event(struct controlvm_message *msg)
Ken Cox12e364b2014-03-04 07:58:07 -06001829{
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001830 if (visorchannel_signalremove(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001831 CONTROLVM_QUEUE_EVENT, msg)) {
1832 /* got a message */
Benjamin Romer0aca78442015-03-04 12:14:25 -05001833 if (msg->hdr.flags.test_message == 1)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001834 return false;
1835 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06001836 }
Jes Sorensenf4c11552015-04-13 10:28:40 -04001837 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06001838}
1839
1840/*
1841 * The general parahotplug flow works as follows. The visorchipset
1842 * driver receives a DEVICE_CHANGESTATE message from Command
1843 * specifying a physical device to enable or disable. The CONTROLVM
1844 * message handler calls parahotplug_process_message, which then adds
1845 * the message to a global list and kicks off a udev event which
1846 * causes a user level script to enable or disable the specified
1847 * device. The udev script then writes to
1848 * /proc/visorchipset/parahotplug, which causes parahotplug_proc_write
1849 * to get called, at which point the appropriate CONTROLVM message is
1850 * retrieved from the list and responded to.
1851 */
1852
1853#define PARAHOTPLUG_TIMEOUT_MS 2000
1854
1855/*
1856 * Generate unique int to match an outstanding CONTROLVM message with a
1857 * udev script /proc response
1858 */
1859static int
1860parahotplug_next_id(void)
1861{
1862 static atomic_t id = ATOMIC_INIT(0);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001863
Ken Cox12e364b2014-03-04 07:58:07 -06001864 return atomic_inc_return(&id);
1865}
1866
1867/*
1868 * Returns the time (in jiffies) when a CONTROLVM message on the list
1869 * should expire -- PARAHOTPLUG_TIMEOUT_MS in the future
1870 */
1871static unsigned long
1872parahotplug_next_expiration(void)
1873{
Nicholas Mc Guire2cc1a1b2015-01-31 12:02:08 +01001874 return jiffies + msecs_to_jiffies(PARAHOTPLUG_TIMEOUT_MS);
Ken Cox12e364b2014-03-04 07:58:07 -06001875}
1876
1877/*
1878 * Create a parahotplug_request, which is basically a wrapper for a
1879 * CONTROLVM_MESSAGE that we can stick on a list
1880 */
1881static struct parahotplug_request *
Benjamin Romer3ab47702014-10-23 14:30:31 -04001882parahotplug_request_create(struct controlvm_message *msg)
Ken Cox12e364b2014-03-04 07:58:07 -06001883{
Quentin Lambertea0dcfc2015-02-10 15:12:07 +01001884 struct parahotplug_request *req;
1885
Benjamin Romer6a55e3c2015-03-16 13:58:30 -04001886 req = kmalloc(sizeof(*req), GFP_KERNEL | __GFP_NORETRY);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001887 if (!req)
Ken Cox12e364b2014-03-04 07:58:07 -06001888 return NULL;
1889
1890 req->id = parahotplug_next_id();
1891 req->expiration = parahotplug_next_expiration();
1892 req->msg = *msg;
1893
1894 return req;
1895}
1896
1897/*
1898 * Free a parahotplug_request.
1899 */
1900static void
1901parahotplug_request_destroy(struct parahotplug_request *req)
1902{
1903 kfree(req);
1904}
1905
1906/*
1907 * Cause uevent to run the user level script to do the disable/enable
1908 * specified in (the CONTROLVM message in) the specified
1909 * parahotplug_request
1910 */
1911static void
1912parahotplug_request_kickoff(struct parahotplug_request *req)
1913{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001914 struct controlvm_message_packet *cmd = &req->msg.cmd;
Ken Cox12e364b2014-03-04 07:58:07 -06001915 char env_cmd[40], env_id[40], env_state[40], env_bus[40], env_dev[40],
1916 env_func[40];
1917 char *envp[] = {
1918 env_cmd, env_id, env_state, env_bus, env_dev, env_func, NULL
1919 };
1920
1921 sprintf(env_cmd, "SPAR_PARAHOTPLUG=1");
1922 sprintf(env_id, "SPAR_PARAHOTPLUG_ID=%d", req->id);
1923 sprintf(env_state, "SPAR_PARAHOTPLUG_STATE=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001924 cmd->device_change_state.state.active);
Ken Cox12e364b2014-03-04 07:58:07 -06001925 sprintf(env_bus, "SPAR_PARAHOTPLUG_BUS=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001926 cmd->device_change_state.bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001927 sprintf(env_dev, "SPAR_PARAHOTPLUG_DEVICE=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001928 cmd->device_change_state.dev_no >> 3);
Ken Cox12e364b2014-03-04 07:58:07 -06001929 sprintf(env_func, "SPAR_PARAHOTPLUG_FUNCTION=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04001930 cmd->device_change_state.dev_no & 0x7);
Ken Cox12e364b2014-03-04 07:58:07 -06001931
Benjamin Romereb34e872015-03-16 13:58:45 -04001932 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
Ken Cox12e364b2014-03-04 07:58:07 -06001933 envp);
1934}
1935
1936/*
1937 * Remove any request from the list that's been on there too long and
1938 * respond with an error.
1939 */
1940static void
1941parahotplug_process_list(void)
1942{
Jes Sorensene82ba622015-05-05 18:35:45 -04001943 struct list_head *pos;
1944 struct list_head *tmp;
Ken Cox12e364b2014-03-04 07:58:07 -06001945
Benjamin Romerddf5de52015-03-16 13:58:41 -04001946 spin_lock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001947
Benjamin Romerddf5de52015-03-16 13:58:41 -04001948 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
Ken Cox12e364b2014-03-04 07:58:07 -06001949 struct parahotplug_request *req =
1950 list_entry(pos, struct parahotplug_request, list);
Benjamin Romer55b33412015-03-16 13:58:29 -04001951
1952 if (!time_after_eq(jiffies, req->expiration))
1953 continue;
1954
1955 list_del(pos);
1956 if (req->msg.hdr.flags.response_expected)
1957 controlvm_respond_physdev_changestate(
1958 &req->msg.hdr,
1959 CONTROLVM_RESP_ERROR_DEVICE_UDEV_TIMEOUT,
1960 req->msg.cmd.device_change_state.state);
1961 parahotplug_request_destroy(req);
Ken Cox12e364b2014-03-04 07:58:07 -06001962 }
1963
Benjamin Romerddf5de52015-03-16 13:58:41 -04001964 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001965}
1966
1967/*
1968 * Called from the /proc handler, which means the user script has
1969 * finished the enable/disable. Find the matching identifier, and
1970 * respond to the CONTROLVM message with success.
1971 */
1972static int
Benjamin Romerb06bdf72014-07-31 12:00:49 -04001973parahotplug_request_complete(int id, u16 active)
Ken Cox12e364b2014-03-04 07:58:07 -06001974{
Jes Sorensene82ba622015-05-05 18:35:45 -04001975 struct list_head *pos;
1976 struct list_head *tmp;
Ken Cox12e364b2014-03-04 07:58:07 -06001977
Benjamin Romerddf5de52015-03-16 13:58:41 -04001978 spin_lock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001979
1980 /* Look for a request matching "id". */
Benjamin Romerddf5de52015-03-16 13:58:41 -04001981 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
Ken Cox12e364b2014-03-04 07:58:07 -06001982 struct parahotplug_request *req =
1983 list_entry(pos, struct parahotplug_request, list);
1984 if (req->id == id) {
1985 /* Found a match. Remove it from the list and
1986 * respond.
1987 */
1988 list_del(pos);
Benjamin Romerddf5de52015-03-16 13:58:41 -04001989 spin_unlock(&parahotplug_request_list_lock);
Benjamin Romer2ea51172014-10-23 14:30:25 -04001990 req->msg.cmd.device_change_state.state.active = active;
Benjamin Romer98d7b592014-10-23 14:30:26 -04001991 if (req->msg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06001992 controlvm_respond_physdev_changestate(
1993 &req->msg.hdr, CONTROLVM_RESP_SUCCESS,
Benjamin Romer2ea51172014-10-23 14:30:25 -04001994 req->msg.cmd.device_change_state.state);
Ken Cox12e364b2014-03-04 07:58:07 -06001995 parahotplug_request_destroy(req);
1996 return 0;
1997 }
1998 }
1999
Benjamin Romerddf5de52015-03-16 13:58:41 -04002000 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06002001 return -1;
2002}
2003
2004/*
2005 * Enables or disables a PCI device by kicking off a udev script
2006 */
Ken Coxbd5b9b32014-03-13 15:39:22 -05002007static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04002008parahotplug_process_message(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06002009{
2010 struct parahotplug_request *req;
2011
2012 req = parahotplug_request_create(inmsg);
2013
Benjamin Romer38f736e2015-03-16 13:58:13 -04002014 if (!req)
Ken Cox12e364b2014-03-04 07:58:07 -06002015 return;
Ken Cox12e364b2014-03-04 07:58:07 -06002016
Benjamin Romer2ea51172014-10-23 14:30:25 -04002017 if (inmsg->cmd.device_change_state.state.active) {
Ken Cox12e364b2014-03-04 07:58:07 -06002018 /* For enable messages, just respond with success
2019 * right away. This is a bit of a hack, but there are
2020 * issues with the early enable messages we get (with
2021 * either the udev script not detecting that the device
2022 * is up, or not getting called at all). Fortunately
2023 * the messages that get lost don't matter anyway, as
2024 * devices are automatically enabled at
2025 * initialization.
2026 */
2027 parahotplug_request_kickoff(req);
2028 controlvm_respond_physdev_changestate(&inmsg->hdr,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002029 CONTROLVM_RESP_SUCCESS,
2030 inmsg->cmd.device_change_state.state);
Ken Cox12e364b2014-03-04 07:58:07 -06002031 parahotplug_request_destroy(req);
2032 } else {
2033 /* For disable messages, add the request to the
2034 * request list before kicking off the udev script. It
2035 * won't get responded to until the script has
2036 * indicated it's done.
2037 */
Benjamin Romerddf5de52015-03-16 13:58:41 -04002038 spin_lock(&parahotplug_request_list_lock);
2039 list_add_tail(&req->list, &parahotplug_request_list);
2040 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06002041
2042 parahotplug_request_kickoff(req);
2043 }
2044}
2045
Ken Cox12e364b2014-03-04 07:58:07 -06002046/* Process a controlvm message.
2047 * Return result:
Jes Sorensenf4c11552015-04-13 10:28:40 -04002048 * false - this function will return FALSE only in the case where the
Ken Cox12e364b2014-03-04 07:58:07 -06002049 * controlvm message was NOT processed, but processing must be
2050 * retried before reading the next controlvm message; a
2051 * scenario where this can occur is when we need to throttle
2052 * the allocation of memory in which to copy out controlvm
2053 * payload data
Jes Sorensenf4c11552015-04-13 10:28:40 -04002054 * true - processing of the controlvm message completed,
Ken Cox12e364b2014-03-04 07:58:07 -06002055 * either successfully or with an error.
2056 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04002057static bool
Benjamin Romer3ab47702014-10-23 14:30:31 -04002058handle_command(struct controlvm_message inmsg, HOSTADDRESS channel_addr)
Ken Cox12e364b2014-03-04 07:58:07 -06002059{
Benjamin Romer2ea51172014-10-23 14:30:25 -04002060 struct controlvm_message_packet *cmd = &inmsg.cmd;
Jes Sorensene82ba622015-05-05 18:35:45 -04002061 u64 parm_addr;
2062 u32 parm_bytes;
Benjamin Romer317d9612015-03-16 13:57:51 -04002063 struct parser_context *parser_ctx = NULL;
Jes Sorensene82ba622015-05-05 18:35:45 -04002064 bool local_addr;
Benjamin Romer3ab47702014-10-23 14:30:31 -04002065 struct controlvm_message ackmsg;
Ken Cox12e364b2014-03-04 07:58:07 -06002066
2067 /* create parsing context if necessary */
Benjamin Romer818352a2015-03-16 13:58:31 -04002068 local_addr = (inmsg.hdr.flags.test_message == 1);
Benjamin Romer0aca78442015-03-04 12:14:25 -05002069 if (channel_addr == 0)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002070 return true;
Benjamin Romer818352a2015-03-16 13:58:31 -04002071 parm_addr = channel_addr + inmsg.hdr.payload_vm_offset;
2072 parm_bytes = inmsg.hdr.payload_bytes;
Ken Cox12e364b2014-03-04 07:58:07 -06002073
2074 /* Parameter and channel addresses within test messages actually lie
2075 * within our OS-controlled memory. We need to know that, because it
2076 * makes a difference in how we compute the virtual address.
2077 */
Jes Sorensenebec8962015-05-05 18:35:57 -04002078 if (parm_addr && parm_bytes) {
Jes Sorensenf4c11552015-04-13 10:28:40 -04002079 bool retry = false;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002080
Ken Cox12e364b2014-03-04 07:58:07 -06002081 parser_ctx =
Benjamin Romer818352a2015-03-16 13:58:31 -04002082 parser_init_byte_stream(parm_addr, parm_bytes,
2083 local_addr, &retry);
Benjamin Romer1b088722015-03-04 12:14:26 -05002084 if (!parser_ctx && retry)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002085 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002086 }
2087
Benjamin Romer818352a2015-03-16 13:58:31 -04002088 if (!local_addr) {
Ken Cox12e364b2014-03-04 07:58:07 -06002089 controlvm_init_response(&ackmsg, &inmsg.hdr,
2090 CONTROLVM_RESP_SUCCESS);
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002091 if (controlvm_channel)
2092 visorchannel_signalinsert(controlvm_channel,
Benjamin Romer1b088722015-03-04 12:14:26 -05002093 CONTROLVM_QUEUE_ACK,
2094 &ackmsg);
Ken Cox12e364b2014-03-04 07:58:07 -06002095 }
Benjamin Romer98d7b592014-10-23 14:30:26 -04002096 switch (inmsg.hdr.id) {
Ken Cox12e364b2014-03-04 07:58:07 -06002097 case CONTROLVM_CHIPSET_INIT:
Ken Cox12e364b2014-03-04 07:58:07 -06002098 chipset_init(&inmsg);
2099 break;
2100 case CONTROLVM_BUS_CREATE:
Ken Cox12e364b2014-03-04 07:58:07 -06002101 bus_create(&inmsg);
2102 break;
2103 case CONTROLVM_BUS_DESTROY:
Ken Cox12e364b2014-03-04 07:58:07 -06002104 bus_destroy(&inmsg);
2105 break;
2106 case CONTROLVM_BUS_CONFIGURE:
Ken Cox12e364b2014-03-04 07:58:07 -06002107 bus_configure(&inmsg, parser_ctx);
2108 break;
2109 case CONTROLVM_DEVICE_CREATE:
Ken Cox12e364b2014-03-04 07:58:07 -06002110 my_device_create(&inmsg);
2111 break;
2112 case CONTROLVM_DEVICE_CHANGESTATE:
Benjamin Romer2ea51172014-10-23 14:30:25 -04002113 if (cmd->device_change_state.flags.phys_device) {
Ken Cox12e364b2014-03-04 07:58:07 -06002114 parahotplug_process_message(&inmsg);
2115 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002116 /* save the hdr and cmd structures for later use */
2117 /* when sending back the response to Command */
2118 my_device_changestate(&inmsg);
Benjamin Romer4f44b722015-03-16 13:58:02 -04002119 g_devicechangestate_packet = inmsg.cmd;
Ken Cox12e364b2014-03-04 07:58:07 -06002120 break;
2121 }
2122 break;
2123 case CONTROLVM_DEVICE_DESTROY:
Ken Cox12e364b2014-03-04 07:58:07 -06002124 my_device_destroy(&inmsg);
2125 break;
2126 case CONTROLVM_DEVICE_CONFIGURE:
Ken Cox12e364b2014-03-04 07:58:07 -06002127 /* no op for now, just send a respond that we passed */
Benjamin Romer98d7b592014-10-23 14:30:26 -04002128 if (inmsg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06002129 controlvm_respond(&inmsg.hdr, CONTROLVM_RESP_SUCCESS);
2130 break;
2131 case CONTROLVM_CHIPSET_READY:
Ken Cox12e364b2014-03-04 07:58:07 -06002132 chipset_ready(&inmsg.hdr);
2133 break;
2134 case CONTROLVM_CHIPSET_SELFTEST:
Ken Cox12e364b2014-03-04 07:58:07 -06002135 chipset_selftest(&inmsg.hdr);
2136 break;
2137 case CONTROLVM_CHIPSET_STOP:
Ken Cox12e364b2014-03-04 07:58:07 -06002138 chipset_notready(&inmsg.hdr);
2139 break;
2140 default:
Benjamin Romer98d7b592014-10-23 14:30:26 -04002141 if (inmsg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06002142 controlvm_respond(&inmsg.hdr,
Benjamin Romer818352a2015-03-16 13:58:31 -04002143 -CONTROLVM_RESP_ERROR_MESSAGE_ID_UNKNOWN);
Ken Cox12e364b2014-03-04 07:58:07 -06002144 break;
2145 }
2146
Benjamin Romer38f736e2015-03-16 13:58:13 -04002147 if (parser_ctx) {
Ken Cox12e364b2014-03-04 07:58:07 -06002148 parser_done(parser_ctx);
2149 parser_ctx = NULL;
2150 }
Jes Sorensenf4c11552015-04-13 10:28:40 -04002151 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002152}
2153
Vincent Bernatd746cb52014-08-01 10:29:30 +02002154static HOSTADDRESS controlvm_get_channel_address(void)
Benjamin Romer524b0b62014-07-17 12:39:57 -04002155{
Benjamin Romer5fc02292014-07-31 12:00:51 -04002156 u64 addr = 0;
Benjamin Romerb3c55b12014-07-31 12:00:50 -04002157 u32 size = 0;
Benjamin Romer524b0b62014-07-17 12:39:57 -04002158
Benjamin Romer0aca78442015-03-04 12:14:25 -05002159 if (!VMCALL_SUCCESSFUL(issue_vmcall_io_controlvm_addr(&addr, &size)))
Benjamin Romer524b0b62014-07-17 12:39:57 -04002160 return 0;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002161
Benjamin Romer524b0b62014-07-17 12:39:57 -04002162 return addr;
2163}
2164
Ken Cox12e364b2014-03-04 07:58:07 -06002165static void
2166controlvm_periodic_work(struct work_struct *work)
2167{
Benjamin Romer3ab47702014-10-23 14:30:31 -04002168 struct controlvm_message inmsg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002169 bool got_command = false;
2170 bool handle_command_failed = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002171 static u64 poll_count;
Ken Cox12e364b2014-03-04 07:58:07 -06002172
2173 /* make sure visorbus server is registered for controlvm callbacks */
David Kershner4da33362015-05-05 18:36:39 -04002174 if (visorchipset_visorbusregwait && !visorbusregistered)
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002175 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002176
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002177 poll_count++;
2178 if (poll_count >= 250)
Ken Cox12e364b2014-03-04 07:58:07 -06002179 ; /* keep going */
2180 else
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002181 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002182
2183 /* Check events to determine if response to CHIPSET_READY
2184 * should be sent
2185 */
Benjamin Romer0639ba62015-03-16 13:58:04 -04002186 if (visorchipset_holdchipsetready &&
2187 (g_chipset_msg_hdr.id != CONTROLVM_INVALID)) {
Ken Cox12e364b2014-03-04 07:58:07 -06002188 if (check_chipset_events() == 1) {
Benjamin Romerda021f02015-03-16 13:57:58 -04002189 controlvm_respond(&g_chipset_msg_hdr, 0);
Ken Cox12e364b2014-03-04 07:58:07 -06002190 clear_chipset_events();
Benjamin Romerda021f02015-03-16 13:57:58 -04002191 memset(&g_chipset_msg_hdr, 0,
Benjamin Romer98d7b592014-10-23 14:30:26 -04002192 sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002193 }
2194 }
2195
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002196 while (visorchannel_signalremove(controlvm_channel,
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002197 CONTROLVM_QUEUE_RESPONSE,
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002198 &inmsg))
2199 ;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002200 if (!got_command) {
Benjamin Romer7166ed12015-03-16 13:58:38 -04002201 if (controlvm_pending_msg_valid) {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002202 /* we throttled processing of a prior
2203 * msg, so try to process it again
2204 * rather than reading a new one
2205 */
Benjamin Romer7166ed12015-03-16 13:58:38 -04002206 inmsg = controlvm_pending_msg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002207 controlvm_pending_msg_valid = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002208 got_command = true;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002209 } else {
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002210 got_command = read_controlvm_event(&inmsg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002211 }
Ken Cox12e364b2014-03-04 07:58:07 -06002212 }
2213
Jes Sorensenf4c11552015-04-13 10:28:40 -04002214 handle_command_failed = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002215 while (got_command && (!handle_command_failed)) {
Benjamin Romerb53e0e92015-03-16 13:57:56 -04002216 most_recent_message_jiffies = jiffies;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002217 if (handle_command(inmsg,
2218 visorchannel_get_physaddr
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002219 (controlvm_channel)))
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002220 got_command = read_controlvm_event(&inmsg);
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002221 else {
2222 /* this is a scenario where throttling
2223 * is required, but probably NOT an
2224 * error...; we stash the current
2225 * controlvm msg so we will attempt to
2226 * reprocess it on our next loop
2227 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04002228 handle_command_failed = true;
Benjamin Romer7166ed12015-03-16 13:58:38 -04002229 controlvm_pending_msg = inmsg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002230 controlvm_pending_msg_valid = true;
Ken Cox12e364b2014-03-04 07:58:07 -06002231 }
2232 }
2233
2234 /* parahotplug_worker */
2235 parahotplug_process_list();
2236
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002237cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002238
2239 if (time_after(jiffies,
Benjamin Romerb53e0e92015-03-16 13:57:56 -04002240 most_recent_message_jiffies + (HZ * MIN_IDLE_SECONDS))) {
Ken Cox12e364b2014-03-04 07:58:07 -06002241 /* it's been longer than MIN_IDLE_SECONDS since we
2242 * processed our last controlvm message; slow down the
2243 * polling
2244 */
Benjamin Romer911e2132015-03-16 13:57:47 -04002245 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_SLOW)
2246 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
Ken Cox12e364b2014-03-04 07:58:07 -06002247 } else {
Benjamin Romer911e2132015-03-16 13:57:47 -04002248 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_FAST)
2249 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
Ken Cox12e364b2014-03-04 07:58:07 -06002250 }
2251
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002252 queue_delayed_work(periodic_controlvm_workqueue,
2253 &periodic_controlvm_work, poll_jiffies);
Ken Cox12e364b2014-03-04 07:58:07 -06002254}
2255
2256static void
2257setup_crash_devices_work_queue(struct work_struct *work)
2258{
Benjamin Romere6bdb902015-03-16 13:58:33 -04002259 struct controlvm_message local_crash_bus_msg;
2260 struct controlvm_message local_crash_dev_msg;
Benjamin Romer3ab47702014-10-23 14:30:31 -04002261 struct controlvm_message msg;
Benjamin Romere6bdb902015-03-16 13:58:33 -04002262 u32 local_crash_msg_offset;
2263 u16 local_crash_msg_count;
Ken Cox12e364b2014-03-04 07:58:07 -06002264
David Kershner4da33362015-05-05 18:36:39 -04002265 /* make sure visorbus is registered for controlvm callbacks */
2266 if (visorchipset_visorbusregwait && !visorbusregistered)
Benjamin Romere6bdb902015-03-16 13:58:33 -04002267 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002268
2269 POSTCODE_LINUX_2(CRASH_DEV_ENTRY_PC, POSTCODE_SEVERITY_INFO);
2270
2271 /* send init chipset msg */
Benjamin Romer98d7b592014-10-23 14:30:26 -04002272 msg.hdr.id = CONTROLVM_CHIPSET_INIT;
Benjamin Romer2ea51172014-10-23 14:30:25 -04002273 msg.cmd.init_chipset.bus_count = 23;
2274 msg.cmd.init_chipset.switch_count = 0;
Ken Cox12e364b2014-03-04 07:58:07 -06002275
2276 chipset_init(&msg);
2277
Ken Cox12e364b2014-03-04 07:58:07 -06002278 /* get saved message count */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002279 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002280 offsetof(struct spar_controlvm_channel_protocol,
2281 saved_crash_message_count),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002282 &local_crash_msg_count, sizeof(u16)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002283 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2284 POSTCODE_SEVERITY_ERR);
2285 return;
2286 }
2287
Benjamin Romere6bdb902015-03-16 13:58:33 -04002288 if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
Ken Cox12e364b2014-03-04 07:58:07 -06002289 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002290 local_crash_msg_count,
Ken Cox12e364b2014-03-04 07:58:07 -06002291 POSTCODE_SEVERITY_ERR);
2292 return;
2293 }
2294
2295 /* get saved crash message offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002296 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002297 offsetof(struct spar_controlvm_channel_protocol,
2298 saved_crash_message_offset),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002299 &local_crash_msg_offset, sizeof(u32)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002300 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2301 POSTCODE_SEVERITY_ERR);
2302 return;
2303 }
2304
2305 /* read create device message for storage bus offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002306 if (visorchannel_read(controlvm_channel,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002307 local_crash_msg_offset,
2308 &local_crash_bus_msg,
Benjamin Romer3ab47702014-10-23 14:30:31 -04002309 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002310 POSTCODE_LINUX_2(CRASH_DEV_RD_BUS_FAIULRE_PC,
2311 POSTCODE_SEVERITY_ERR);
2312 return;
2313 }
2314
2315 /* read create device message for storage device */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002316 if (visorchannel_read(controlvm_channel,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002317 local_crash_msg_offset +
Benjamin Romer3ab47702014-10-23 14:30:31 -04002318 sizeof(struct controlvm_message),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002319 &local_crash_dev_msg,
Benjamin Romer3ab47702014-10-23 14:30:31 -04002320 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002321 POSTCODE_LINUX_2(CRASH_DEV_RD_DEV_FAIULRE_PC,
2322 POSTCODE_SEVERITY_ERR);
2323 return;
2324 }
2325
2326 /* reuse IOVM create bus message */
Jes Sorensenebec8962015-05-05 18:35:57 -04002327 if (local_crash_bus_msg.cmd.create_bus.channel_addr) {
Benjamin Romere6bdb902015-03-16 13:58:33 -04002328 bus_create(&local_crash_bus_msg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002329 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002330 POSTCODE_LINUX_2(CRASH_DEV_BUS_NULL_FAILURE_PC,
2331 POSTCODE_SEVERITY_ERR);
2332 return;
2333 }
2334
2335 /* reuse create device message for storage device */
Jes Sorensenebec8962015-05-05 18:35:57 -04002336 if (local_crash_dev_msg.cmd.create_device.channel_addr) {
Benjamin Romere6bdb902015-03-16 13:58:33 -04002337 my_device_create(&local_crash_dev_msg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002338 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002339 POSTCODE_LINUX_2(CRASH_DEV_DEV_NULL_FAILURE_PC,
2340 POSTCODE_SEVERITY_ERR);
2341 return;
2342 }
Ken Cox12e364b2014-03-04 07:58:07 -06002343 POSTCODE_LINUX_2(CRASH_DEV_EXIT_PC, POSTCODE_SEVERITY_INFO);
2344 return;
2345
Benjamin Romere6bdb902015-03-16 13:58:33 -04002346cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002347
Benjamin Romer911e2132015-03-16 13:57:47 -04002348 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
Ken Cox12e364b2014-03-04 07:58:07 -06002349
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002350 queue_delayed_work(periodic_controlvm_workqueue,
2351 &periodic_controlvm_work, poll_jiffies);
Ken Cox12e364b2014-03-04 07:58:07 -06002352}
2353
2354static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002355bus_create_response(u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002356{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002357 bus_responder(CONTROLVM_BUS_CREATE, bus_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002358}
2359
2360static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002361bus_destroy_response(u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002362{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002363 bus_responder(CONTROLVM_BUS_DESTROY, bus_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002364}
2365
2366static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002367device_create_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002368{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002369 device_responder(CONTROLVM_DEVICE_CREATE, bus_no, dev_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002370}
2371
2372static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002373device_destroy_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002374{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002375 device_responder(CONTROLVM_DEVICE_DESTROY, bus_no, dev_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002376}
2377
2378void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002379visorchipset_device_pause_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002380{
Ken Cox12e364b2014-03-04 07:58:07 -06002381 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
Benjamin Romer8420f412014-10-31 09:57:36 -04002382 bus_no, dev_no, response,
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04002383 segment_state_standby);
Ken Cox12e364b2014-03-04 07:58:07 -06002384}
Ken Cox927c7922014-03-05 14:52:25 -06002385EXPORT_SYMBOL_GPL(visorchipset_device_pause_response);
Ken Cox12e364b2014-03-04 07:58:07 -06002386
2387static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002388device_resume_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002389{
2390 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002391 bus_no, dev_no, response,
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04002392 segment_state_running);
Ken Cox12e364b2014-03-04 07:58:07 -06002393}
2394
Jes Sorensenf4c11552015-04-13 10:28:40 -04002395bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002396visorchipset_get_bus_info(u32 bus_no, struct visorchipset_bus_info *bus_info)
Ken Cox12e364b2014-03-04 07:58:07 -06002397{
Jes Sorensen4f665202015-05-05 18:35:52 -04002398 void *p = bus_find(&bus_info_list, bus_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002399
Benjamin Romer0aca78442015-03-04 12:14:25 -05002400 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002401 return false;
Benjamin Romer77db7122014-10-31 09:57:37 -04002402 memcpy(bus_info, p, sizeof(struct visorchipset_bus_info));
Jes Sorensenf4c11552015-04-13 10:28:40 -04002403 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002404}
2405EXPORT_SYMBOL_GPL(visorchipset_get_bus_info);
2406
Jes Sorensenf4c11552015-04-13 10:28:40 -04002407bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002408visorchipset_set_bus_context(u32 bus_no, void *context)
Ken Cox12e364b2014-03-04 07:58:07 -06002409{
Jes Sorensen4f665202015-05-05 18:35:52 -04002410 struct visorchipset_bus_info *p = bus_find(&bus_info_list, bus_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002411
Benjamin Romer0aca78442015-03-04 12:14:25 -05002412 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002413 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002414 p->bus_driver_context = context;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002415 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002416}
2417EXPORT_SYMBOL_GPL(visorchipset_set_bus_context);
2418
Jes Sorensenf4c11552015-04-13 10:28:40 -04002419bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002420visorchipset_get_device_info(u32 bus_no, u32 dev_no,
Benjamin Romerb486df12014-10-31 09:57:38 -04002421 struct visorchipset_device_info *dev_info)
Ken Cox12e364b2014-03-04 07:58:07 -06002422{
Jes Sorensend480f6a2015-05-05 18:35:54 -04002423 void *p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002424
Benjamin Romer0aca78442015-03-04 12:14:25 -05002425 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002426 return false;
Benjamin Romerb486df12014-10-31 09:57:38 -04002427 memcpy(dev_info, p, sizeof(struct visorchipset_device_info));
Jes Sorensenf4c11552015-04-13 10:28:40 -04002428 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002429}
2430EXPORT_SYMBOL_GPL(visorchipset_get_device_info);
2431
Jes Sorensenf4c11552015-04-13 10:28:40 -04002432bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002433visorchipset_set_device_context(u32 bus_no, u32 dev_no, void *context)
Ken Cox12e364b2014-03-04 07:58:07 -06002434{
Jes Sorensend480f6a2015-05-05 18:35:54 -04002435 struct visorchipset_device_info *p;
2436
2437 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002438
Benjamin Romer0aca78442015-03-04 12:14:25 -05002439 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002440 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002441 p->bus_driver_context = context;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002442 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002443}
2444EXPORT_SYMBOL_GPL(visorchipset_set_device_context);
2445
2446/* Generic wrapper function for allocating memory from a kmem_cache pool.
2447 */
2448void *
Jes Sorensenf4c11552015-04-13 10:28:40 -04002449visorchipset_cache_alloc(struct kmem_cache *pool, bool ok_to_block,
Ken Cox12e364b2014-03-04 07:58:07 -06002450 char *fn, int ln)
2451{
2452 gfp_t gfp;
2453 void *p;
2454
2455 if (ok_to_block)
2456 gfp = GFP_KERNEL;
2457 else
2458 gfp = GFP_ATOMIC;
2459 /* __GFP_NORETRY means "ok to fail", meaning
2460 * kmem_cache_alloc() can return NULL, implying the caller CAN
2461 * cope with failure. If you do NOT specify __GFP_NORETRY,
2462 * Linux will go to extreme measures to get memory for you
2463 * (like, invoke oom killer), which will probably cripple the
2464 * system.
2465 */
2466 gfp |= __GFP_NORETRY;
2467 p = kmem_cache_alloc(pool, gfp);
Benjamin Romer0aca78442015-03-04 12:14:25 -05002468 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06002469 return NULL;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002470
Ken Cox12e364b2014-03-04 07:58:07 -06002471 return p;
2472}
2473
2474/* Generic wrapper function for freeing memory from a kmem_cache pool.
2475 */
2476void
2477visorchipset_cache_free(struct kmem_cache *pool, void *p, char *fn, int ln)
2478{
Benjamin Romer0aca78442015-03-04 12:14:25 -05002479 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06002480 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002481
Ken Cox12e364b2014-03-04 07:58:07 -06002482 kmem_cache_free(pool, p);
2483}
2484
Benjamin Romer18b87ed2014-07-24 14:08:43 -04002485static ssize_t chipsetready_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002486 struct device_attribute *attr,
2487 const char *buf, size_t count)
Ken Cox12e364b2014-03-04 07:58:07 -06002488{
Benjamin Romer18b87ed2014-07-24 14:08:43 -04002489 char msgtype[64];
Ken Cox12e364b2014-03-04 07:58:07 -06002490
Benjamin Romer66e24b72014-07-25 13:55:10 -04002491 if (sscanf(buf, "%63s", msgtype) != 1)
Ken Cox12e364b2014-03-04 07:58:07 -06002492 return -EINVAL;
Benjamin Romer66e24b72014-07-25 13:55:10 -04002493
Jes Sorensenebec8962015-05-05 18:35:57 -04002494 if (!strcmp(msgtype, "CALLHOMEDISK_MOUNTED")) {
Benjamin Romer66e24b72014-07-25 13:55:10 -04002495 chipset_events[0] = 1;
2496 return count;
Jes Sorensenebec8962015-05-05 18:35:57 -04002497 } else if (!strcmp(msgtype, "MODULES_LOADED")) {
Benjamin Romer66e24b72014-07-25 13:55:10 -04002498 chipset_events[1] = 1;
2499 return count;
Benjamin Romere22a4a02014-08-18 09:34:54 -04002500 }
2501 return -EINVAL;
Ken Cox12e364b2014-03-04 07:58:07 -06002502}
2503
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002504/* The parahotplug/devicedisabled interface gets called by our support script
2505 * when an SR-IOV device has been shut down. The ID is passed to the script
2506 * and then passed back when the device has been removed.
2507 */
2508static ssize_t devicedisabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002509 struct device_attribute *attr,
2510 const char *buf, size_t count)
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002511{
Jes Sorensen94217362015-05-05 18:35:46 -04002512 unsigned int id;
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002513
Jes Sorensenebec8962015-05-05 18:35:57 -04002514 if (kstrtouint(buf, 10, &id))
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002515 return -EINVAL;
2516
2517 parahotplug_request_complete(id, 0);
2518 return count;
2519}
2520
2521/* The parahotplug/deviceenabled interface gets called by our support script
2522 * when an SR-IOV device has been recovered. The ID is passed to the script
2523 * and then passed back when the device has been brought back up.
2524 */
2525static ssize_t deviceenabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002526 struct device_attribute *attr,
2527 const char *buf, size_t count)
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002528{
Jes Sorensen94217362015-05-05 18:35:46 -04002529 unsigned int id;
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002530
Jes Sorensenebec8962015-05-05 18:35:57 -04002531 if (kstrtouint(buf, 10, &id))
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002532 return -EINVAL;
2533
2534 parahotplug_request_complete(id, 1);
2535 return count;
2536}
2537
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002538static int
2539visorchipset_mmap(struct file *file, struct vm_area_struct *vma)
2540{
2541 unsigned long physaddr = 0;
2542 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
Erik Arfvidson780fcad2015-05-05 18:36:35 -04002543 u64 addr = 0;
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002544
2545 /* sv_enable_dfp(); */
2546 if (offset & (PAGE_SIZE - 1))
2547 return -ENXIO; /* need aligned offsets */
2548
2549 switch (offset) {
2550 case VISORCHIPSET_MMAP_CONTROLCHANOFFSET:
2551 vma->vm_flags |= VM_IO;
2552 if (!*file_controlvm_channel)
2553 return -ENXIO;
2554
2555 visorchannel_read(*file_controlvm_channel,
2556 offsetof(struct spar_controlvm_channel_protocol,
2557 gp_control_channel),
2558 &addr, sizeof(addr));
2559 if (!addr)
2560 return -ENXIO;
2561
2562 physaddr = (unsigned long)addr;
2563 if (remap_pfn_range(vma, vma->vm_start,
2564 physaddr >> PAGE_SHIFT,
2565 vma->vm_end - vma->vm_start,
2566 /*pgprot_noncached */
2567 (vma->vm_page_prot))) {
2568 return -EAGAIN;
2569 }
2570 break;
2571 default:
2572 return -ENXIO;
2573 }
2574 return 0;
2575}
2576
2577static long visorchipset_ioctl(struct file *file, unsigned int cmd,
2578 unsigned long arg)
2579{
2580 s64 adjustment;
2581 s64 vrtc_offset;
2582
2583 switch (cmd) {
2584 case VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET:
2585 /* get the physical rtc offset */
2586 vrtc_offset = issue_vmcall_query_guest_virtual_time_offset();
2587 if (copy_to_user((void __user *)arg, &vrtc_offset,
2588 sizeof(vrtc_offset))) {
2589 return -EFAULT;
2590 }
2591 return SUCCESS;
2592 case VMCALL_UPDATE_PHYSICAL_TIME:
2593 if (copy_from_user(&adjustment, (void __user *)arg,
2594 sizeof(adjustment))) {
2595 return -EFAULT;
2596 }
2597 return issue_vmcall_update_physical_time(adjustment);
2598 default:
2599 return -EFAULT;
2600 }
2601}
2602
2603static const struct file_operations visorchipset_fops = {
2604 .owner = THIS_MODULE,
2605 .open = visorchipset_open,
2606 .read = NULL,
2607 .write = NULL,
2608 .unlocked_ioctl = visorchipset_ioctl,
2609 .release = visorchipset_release,
2610 .mmap = visorchipset_mmap,
2611};
2612
2613int
2614visorchipset_file_init(dev_t major_dev, struct visorchannel **controlvm_channel)
2615{
2616 int rc = 0;
2617
2618 file_controlvm_channel = controlvm_channel;
2619 cdev_init(&file_cdev, &visorchipset_fops);
2620 file_cdev.owner = THIS_MODULE;
2621 if (MAJOR(major_dev) == 0) {
Erik Arfvidson46168812015-05-05 18:36:14 -04002622 rc = alloc_chrdev_region(&major_dev, 0, 1, "visorchipset");
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002623 /* dynamic major device number registration required */
2624 if (rc < 0)
2625 return rc;
2626 } else {
2627 /* static major device number registration required */
Erik Arfvidson46168812015-05-05 18:36:14 -04002628 rc = register_chrdev_region(major_dev, 1, "visorchipset");
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002629 if (rc < 0)
2630 return rc;
2631 }
2632 rc = cdev_add(&file_cdev, MKDEV(MAJOR(major_dev), 0), 1);
2633 if (rc < 0) {
2634 unregister_chrdev_region(major_dev, 1);
2635 return rc;
2636 }
2637 return 0;
2638}
2639
2640
2641
Ken Cox12e364b2014-03-04 07:58:07 -06002642static int __init
2643visorchipset_init(void)
2644{
2645 int rc = 0, x = 0;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002646 HOSTADDRESS addr;
Ken Cox12e364b2014-03-04 07:58:07 -06002647
Ken Coxfcd0157e2014-04-28 12:23:13 -05002648 if (!unisys_spar_platform)
2649 return -ENODEV;
2650
David Kershner4da33362015-05-05 18:36:39 -04002651 memset(&busdev_notifiers, 0, sizeof(busdev_notifiers));
Benjamin Romer84982fb2015-03-16 13:58:07 -04002652 memset(&controlvm_payload_info, 0, sizeof(controlvm_payload_info));
Benjamin Romerea33b4ee52015-03-16 13:58:08 -04002653 memset(&livedump_info, 0, sizeof(livedump_info));
2654 atomic_set(&livedump_info.buffers_in_use, 0);
Ken Cox12e364b2014-03-04 07:58:07 -06002655
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002656 addr = controlvm_get_channel_address();
Jes Sorensenebec8962015-05-05 18:35:57 -04002657 if (addr) {
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002658 controlvm_channel =
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002659 visorchannel_create_with_lock
2660 (addr,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002661 sizeof(struct spar_controlvm_channel_protocol),
Benjamin Romer5fbaa4b2014-10-23 14:30:14 -04002662 spar_controlvm_channel_protocol_uuid);
Benjamin Romer93a84562014-10-23 14:30:05 -04002663 if (SPAR_CONTROLVM_CHANNEL_OK_CLIENT(
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002664 visorchannel_get_header(controlvm_channel))) {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002665 initialize_controlvm_payload();
2666 } else {
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002667 visorchannel_destroy(controlvm_channel);
2668 controlvm_channel = NULL;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002669 return -ENODEV;
2670 }
2671 } else {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002672 return -ENODEV;
2673 }
2674
Benjamin Romer5aa8ae52015-03-16 13:58:44 -04002675 major_dev = MKDEV(visorchipset_major, 0);
2676 rc = visorchipset_file_init(major_dev, &controlvm_channel);
Ken Cox4cb005a2014-03-19 13:06:20 -05002677 if (rc < 0) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002678 POSTCODE_LINUX_2(CHIPSET_INIT_FAILURE_PC, DIAG_SEVERITY_ERR);
Benjamin Romera6a39892015-03-16 13:58:34 -04002679 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002680 }
Ken Cox9f8d0e82014-03-19 13:06:19 -05002681
Benjamin Romerda021f02015-03-16 13:57:58 -04002682 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002683
David Kershner4da33362015-05-05 18:36:39 -04002684 /* if booting in a crash kernel */
2685 if (is_kdump_kernel())
2686 INIT_DELAYED_WORK(&periodic_controlvm_work,
2687 setup_crash_devices_work_queue);
2688 else
2689 INIT_DELAYED_WORK(&periodic_controlvm_work,
2690 controlvm_periodic_work);
2691 periodic_controlvm_workqueue =
2692 create_singlethread_workqueue("visorchipset_controlvm");
Ken Cox12e364b2014-03-04 07:58:07 -06002693
David Kershner4da33362015-05-05 18:36:39 -04002694 if (!periodic_controlvm_workqueue) {
2695 POSTCODE_LINUX_2(CREATE_WORKQUEUE_FAILED_PC,
2696 DIAG_SEVERITY_ERR);
2697 rc = -ENOMEM;
2698 goto cleanup;
2699 }
2700 most_recent_message_jiffies = jiffies;
2701 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
2702 rc = queue_delayed_work(periodic_controlvm_workqueue,
2703 &periodic_controlvm_work, poll_jiffies);
2704 if (rc < 0) {
2705 POSTCODE_LINUX_2(QUEUE_DELAYED_WORK_PC,
2706 DIAG_SEVERITY_ERR);
2707 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002708 }
2709
Benjamin Romereb34e872015-03-16 13:58:45 -04002710 visorchipset_platform_device.dev.devt = major_dev;
2711 if (platform_device_register(&visorchipset_platform_device) < 0) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002712 POSTCODE_LINUX_2(DEVICE_REGISTER_FAILURE_PC, DIAG_SEVERITY_ERR);
2713 rc = -1;
Benjamin Romera6a39892015-03-16 13:58:34 -04002714 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002715 }
Ken Cox12e364b2014-03-04 07:58:07 -06002716 POSTCODE_LINUX_2(CHIPSET_INIT_SUCCESS_PC, POSTCODE_SEVERITY_INFO);
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04002717
2718 rc = visorbus_init();
Benjamin Romera6a39892015-03-16 13:58:34 -04002719cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002720 if (rc) {
Ken Cox12e364b2014-03-04 07:58:07 -06002721 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
2722 POSTCODE_SEVERITY_ERR);
2723 }
2724 return rc;
2725}
2726
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002727void
2728visorchipset_file_cleanup(dev_t major_dev)
2729{
2730 if (file_cdev.ops)
2731 cdev_del(&file_cdev);
2732 file_cdev.ops = NULL;
2733 unregister_chrdev_region(major_dev, 1);
2734}
2735
Ken Cox12e364b2014-03-04 07:58:07 -06002736static void
2737visorchipset_exit(void)
2738{
Ken Cox12e364b2014-03-04 07:58:07 -06002739 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
2740
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04002741 visorbus_exit();
2742
David Kershner4da33362015-05-05 18:36:39 -04002743 cancel_delayed_work(&periodic_controlvm_work);
2744 flush_workqueue(periodic_controlvm_workqueue);
2745 destroy_workqueue(periodic_controlvm_workqueue);
2746 periodic_controlvm_workqueue = NULL;
2747 destroy_controlvm_payload_info(&controlvm_payload_info);
Benjamin Romer17833192014-07-15 13:30:41 -04002748
Ken Cox12e364b2014-03-04 07:58:07 -06002749 cleanup_controlvm_structures();
2750
Benjamin Romerda021f02015-03-16 13:57:58 -04002751 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002752
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002753 visorchannel_destroy(controlvm_channel);
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002754
Sudip Mukherjeeaddceb12015-03-24 20:47:28 +05302755 visorchipset_file_cleanup(visorchipset_platform_device.dev.devt);
Ken Cox12e364b2014-03-04 07:58:07 -06002756 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06002757}
2758
Ken Cox12e364b2014-03-04 07:58:07 -06002759module_param_named(major, visorchipset_major, int, S_IRUGO);
Jes Sorensenb615d622015-05-05 18:35:38 -04002760MODULE_PARM_DESC(visorchipset_major,
2761 "major device number to use for the device node");
David Kershner4da33362015-05-05 18:36:39 -04002762module_param_named(visorbusregwait, visorchipset_visorbusregwait, int, S_IRUGO);
2763MODULE_PARM_DESC(visorchipset_visorbusreqwait,
Ken Cox12e364b2014-03-04 07:58:07 -06002764 "1 to have the module wait for the visor bus to register");
Ken Cox12e364b2014-03-04 07:58:07 -06002765module_param_named(holdchipsetready, visorchipset_holdchipsetready,
2766 int, S_IRUGO);
2767MODULE_PARM_DESC(visorchipset_holdchipsetready,
2768 "1 to hold response to CHIPSET_READY");
Jes Sorensenb615d622015-05-05 18:35:38 -04002769
Ken Cox12e364b2014-03-04 07:58:07 -06002770module_init(visorchipset_init);
2771module_exit(visorchipset_exit);
2772
2773MODULE_AUTHOR("Unisys");
2774MODULE_LICENSE("GPL");
2775MODULE_DESCRIPTION("Supervisor chipset driver for service partition: ver "
2776 VERSION);
2777MODULE_VERSION(VERSION);