blob: 107012f3d1aeee227bf7b277fb69b64c631c6f90 [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
29#include <linux/ctype.h>
Erik Arfvidsone3420ed2015-05-05 18:36:13 -040030#include <linux/fs.h>
31#include <linux/mm.h>
Ken Cox12e364b2014-03-04 07:58:07 -060032#include <linux/nls.h>
33#include <linux/netdevice.h>
34#include <linux/platform_device.h>
Benjamin Romer90addb02014-05-06 09:58:23 -040035#include <linux/uuid.h>
Benjamin Romer1ba00982015-04-06 10:27:40 -040036#include <linux/crash_dump.h>
Ken Cox12e364b2014-03-04 07:58:07 -060037
38#define CURRENT_FILE_PC VISOR_CHIPSET_PC_visorchipset_main_c
39#define TEST_VNIC_PHYSITF "eth0" /* physical network itf for
40 * vnic loopback test */
41#define TEST_VNIC_SWITCHNO 1
42#define TEST_VNIC_BUSNO 9
43
44#define MAX_NAME_SIZE 128
45#define MAX_IP_SIZE 50
46#define MAXOUTSTANDINGCHANNELCOMMAND 256
47#define POLLJIFFIES_CONTROLVMCHANNEL_FAST 1
48#define POLLJIFFIES_CONTROLVMCHANNEL_SLOW 100
49
Erik Arfvidson46168812015-05-05 18:36:14 -040050#define MAX_CONTROLVM_PAYLOAD_BYTES (1024*128)
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -040051
52#define VISORCHIPSET_MMAP_CONTROLCHANOFFSET 0x00000000
53
Jes Sorensenb615d622015-05-05 18:35:38 -040054/*
55 * Module parameters
56 */
57static int visorchipset_testvnic;
58static int visorchipset_testvnicclient;
59static int visorchipset_testmsg;
60static int visorchipset_major;
61static int visorchipset_serverregwait;
62static int visorchipset_clientregwait = 1; /* default is on */
63static int visorchipset_testteardown;
64static int visorchipset_disable_controlvm;
65static int visorchipset_holdchipsetready;
Erik Arfvidson46168812015-05-05 18:36:14 -040066static unsigned long controlvm_payload_bytes_buffered;
Jes Sorensenb615d622015-05-05 18:35:38 -040067
Erik Arfvidsone3420ed2015-05-05 18:36:13 -040068static int
69visorchipset_open(struct inode *inode, struct file *file)
70{
71 unsigned minor_number = iminor(inode);
72
73 if (minor_number)
74 return -ENODEV;
75 file->private_data = NULL;
76 return 0;
77}
78
79static int
80visorchipset_release(struct inode *inode, struct file *file)
81{
82 return 0;
83}
84
Ken Cox12e364b2014-03-04 07:58:07 -060085/* When the controlvm channel is idle for at least MIN_IDLE_SECONDS,
86* we switch to slow polling mode. As soon as we get a controlvm
87* message, we switch back to fast polling mode.
88*/
89#define MIN_IDLE_SECONDS 10
Jes Sorensen52063ec2015-04-13 10:28:41 -040090static unsigned long poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
91static unsigned long most_recent_message_jiffies; /* when we got our last
Ken Coxbd5b9b32014-03-13 15:39:22 -050092 * controlvm message */
Ken Cox12e364b2014-03-04 07:58:07 -060093static int serverregistered;
94static int clientregistered;
95
96#define MAX_CHIPSET_EVENTS 2
Benjamin Romerc2422332014-07-29 15:09:40 -040097static u8 chipset_events[MAX_CHIPSET_EVENTS] = { 0, 0 };
Ken Cox12e364b2014-03-04 07:58:07 -060098
Erik Arfvidson46168812015-05-05 18:36:14 -040099struct parser_context {
100 unsigned long allocbytes;
101 unsigned long param_bytes;
102 u8 *curr;
103 unsigned long bytes_remaining;
104 bool byte_stream;
105 char data[0];
106};
107
Benjamin Romer9232d2d2015-03-16 13:57:57 -0400108static struct delayed_work periodic_controlvm_work;
109static struct workqueue_struct *periodic_controlvm_workqueue;
Benjamin Romer8f1947a2015-03-16 13:57:59 -0400110static DEFINE_SEMAPHORE(notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -0600111
Erik Arfvidsone3420ed2015-05-05 18:36:13 -0400112static struct cdev file_cdev;
113static struct visorchannel **file_controlvm_channel;
Benjamin Romerda021f02015-03-16 13:57:58 -0400114static struct controlvm_message_header g_chipset_msg_hdr;
Benjamin Romer59827f02015-03-16 13:58:00 -0400115static const uuid_le spar_diag_pool_channel_protocol_uuid =
Benjamin Romer9eee5d12014-10-23 14:29:47 -0400116 SPAR_DIAG_POOL_CHANNEL_PROTOCOL_UUID;
Ken Cox12e364b2014-03-04 07:58:07 -0600117/* 0xffffff is an invalid Bus/Device number */
Jes Sorensen52063ec2015-04-13 10:28:41 -0400118static u32 g_diagpool_bus_no = 0xffffff;
119static u32 g_diagpool_dev_no = 0xffffff;
Benjamin Romer4f44b722015-03-16 13:58:02 -0400120static struct controlvm_message_packet g_devicechangestate_packet;
Ken Cox12e364b2014-03-04 07:58:07 -0600121
122/* Only VNIC and VHBA channels are sent to visorclientbus (aka
123 * "visorhackbus")
124 */
125#define FOR_VISORHACKBUS(channel_type_guid) \
Benjamin Romer9eee5d12014-10-23 14:29:47 -0400126 (((uuid_le_cmp(channel_type_guid,\
Benjamin Romer0639ba62015-03-16 13:58:04 -0400127 spar_vnic_channel_protocol_uuid) == 0) ||\
128 (uuid_le_cmp(channel_type_guid,\
Benjamin Romer9eee5d12014-10-23 14:29:47 -0400129 spar_vhba_channel_protocol_uuid) == 0)))
Ken Cox12e364b2014-03-04 07:58:07 -0600130#define FOR_VISORBUS(channel_type_guid) (!(FOR_VISORHACKBUS(channel_type_guid)))
131
132#define is_diagpool_channel(channel_type_guid) \
Benjamin Romer59827f02015-03-16 13:58:00 -0400133 (uuid_le_cmp(channel_type_guid,\
134 spar_diag_pool_channel_protocol_uuid) == 0)
Ken Cox12e364b2014-03-04 07:58:07 -0600135
Benjamin Romer1390b882015-03-16 13:58:03 -0400136static LIST_HEAD(bus_info_list);
137static LIST_HEAD(dev_info_list);
Ken Cox12e364b2014-03-04 07:58:07 -0600138
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400139static struct visorchannel *controlvm_channel;
Ken Cox12e364b2014-03-04 07:58:07 -0600140
Benjamin Romer84982fb2015-03-16 13:58:07 -0400141/* Manages the request payload in the controlvm channel */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400142struct visor_controlvm_payload_info {
Benjamin Romerc2422332014-07-29 15:09:40 -0400143 u8 __iomem *ptr; /* pointer to base address of payload pool */
Benjamin Romer5fc02292014-07-31 12:00:51 -0400144 u64 offset; /* offset from beginning of controlvm
Ken Cox12e364b2014-03-04 07:58:07 -0600145 * channel to beginning of payload * pool */
Benjamin Romerb3c55b12014-07-31 12:00:50 -0400146 u32 bytes; /* number of bytes in payload pool */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400147};
148
149static struct visor_controlvm_payload_info controlvm_payload_info;
Ken Cox12e364b2014-03-04 07:58:07 -0600150
Benjamin Romerea33b4ee52015-03-16 13:58:08 -0400151/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
152 * CONTROLVM_DUMP_GETTEXTDUMP / CONTROLVM_DUMP_COMPLETE conversation.
153 */
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400154struct visor_livedump_info {
Benjamin Romerea33b4ee52015-03-16 13:58:08 -0400155 struct controlvm_message_header dumpcapture_header;
156 struct controlvm_message_header gettextdump_header;
157 struct controlvm_message_header dumpcomplete_header;
Jes Sorensenf4c11552015-04-13 10:28:40 -0400158 bool gettextdump_outstanding;
Ken Cox12e364b2014-03-04 07:58:07 -0600159 u32 crc32;
Jes Sorensen52063ec2015-04-13 10:28:41 -0400160 unsigned long length;
Ken Cox12e364b2014-03-04 07:58:07 -0600161 atomic_t buffers_in_use;
Jes Sorensen52063ec2015-04-13 10:28:41 -0400162 unsigned long destination;
Jes Sorensenc1f834e2015-04-13 10:28:39 -0400163};
164
165static struct visor_livedump_info livedump_info;
Ken Cox12e364b2014-03-04 07:58:07 -0600166
167/* The following globals are used to handle the scenario where we are unable to
168 * offload the payload from a controlvm message due to memory requirements. In
169 * this scenario, we simply stash the controlvm message, then attempt to
170 * process it again the next time controlvm_periodic_work() runs.
171 */
Benjamin Romer7166ed12015-03-16 13:58:38 -0400172static struct controlvm_message controlvm_pending_msg;
Prarit Bhargavac79b28f2015-05-05 18:36:15 -0400173static bool controlvm_pending_msg_valid;
Ken Cox12e364b2014-03-04 07:58:07 -0600174
Ken Cox12e364b2014-03-04 07:58:07 -0600175/* This identifies a data buffer that has been received via a controlvm messages
176 * in a remote --> local CONTROLVM_TRANSMIT_FILE conversation.
177 */
178struct putfile_buffer_entry {
179 struct list_head next; /* putfile_buffer_entry list */
Benjamin Romer317d9612015-03-16 13:57:51 -0400180 struct parser_context *parser_ctx; /* points to input data buffer */
Ken Cox12e364b2014-03-04 07:58:07 -0600181};
182
183/* List of struct putfile_request *, via next_putfile_request member.
184 * Each entry in this list identifies an outstanding TRANSMIT_FILE
185 * conversation.
186 */
Benjamin Romer1eee0012015-03-16 13:58:39 -0400187static LIST_HEAD(putfile_request_list);
Ken Cox12e364b2014-03-04 07:58:07 -0600188
189/* This describes a buffer and its current state of transfer (e.g., how many
190 * bytes have already been supplied as putfile data, and how many bytes are
191 * remaining) for a putfile_request.
192 */
193struct putfile_active_buffer {
194 /* a payload from a controlvm message, containing a file data buffer */
Benjamin Romer317d9612015-03-16 13:57:51 -0400195 struct parser_context *parser_ctx;
Ken Cox12e364b2014-03-04 07:58:07 -0600196 /* points within data area of parser_ctx to next byte of data */
197 u8 *pnext;
198 /* # bytes left from <pnext> to the end of this data buffer */
199 size_t bytes_remaining;
200};
201
202#define PUTFILE_REQUEST_SIG 0x0906101302281211
203/* This identifies a single remote --> local CONTROLVM_TRANSMIT_FILE
204 * conversation. Structs of this type are dynamically linked into
205 * <Putfile_request_list>.
206 */
207struct putfile_request {
208 u64 sig; /* PUTFILE_REQUEST_SIG */
209
210 /* header from original TransmitFile request */
Benjamin Romer98d7b592014-10-23 14:30:26 -0400211 struct controlvm_message_header controlvm_header;
Ken Cox12e364b2014-03-04 07:58:07 -0600212 u64 file_request_number; /* from original TransmitFile request */
213
214 /* link to next struct putfile_request */
215 struct list_head next_putfile_request;
216
217 /* most-recent sequence number supplied via a controlvm message */
218 u64 data_sequence_number;
219
220 /* head of putfile_buffer_entry list, which describes the data to be
221 * supplied as putfile data;
222 * - this list is added to when controlvm messages come in that supply
223 * file data
224 * - this list is removed from via the hotplug program that is actually
225 * consuming these buffers to write as file data */
226 struct list_head input_buffer_list;
227 spinlock_t req_list_lock; /* lock for input_buffer_list */
228
229 /* waiters for input_buffer_list to go non-empty */
230 wait_queue_head_t input_buffer_wq;
231
232 /* data not yet read within current putfile_buffer_entry */
233 struct putfile_active_buffer active_buf;
234
235 /* <0 = failed, 0 = in-progress, >0 = successful; */
236 /* note that this must be set with req_list_lock, and if you set <0, */
237 /* it is your responsibility to also free up all of the other objects */
238 /* in this struct (like input_buffer_list, active_buf.parser_ctx) */
239 /* before releasing the lock */
240 int completion_status;
241};
242
Ken Cox12e364b2014-03-04 07:58:07 -0600243struct parahotplug_request {
244 struct list_head list;
245 int id;
246 unsigned long expiration;
Benjamin Romer3ab47702014-10-23 14:30:31 -0400247 struct controlvm_message msg;
Ken Cox12e364b2014-03-04 07:58:07 -0600248};
249
Benjamin Romerddf5de52015-03-16 13:58:41 -0400250static LIST_HEAD(parahotplug_request_list);
251static DEFINE_SPINLOCK(parahotplug_request_list_lock); /* lock for above */
Ken Cox12e364b2014-03-04 07:58:07 -0600252static void parahotplug_process_list(void);
253
254/* Manages the info for a CONTROLVM_DUMP_CAPTURESTATE /
255 * CONTROLVM_REPORTEVENT.
256 */
Benjamin Romer6fe345a2015-03-16 13:58:42 -0400257static struct visorchipset_busdev_notifiers busdev_server_notifiers;
258static struct visorchipset_busdev_notifiers busdev_client_notifiers;
Ken Cox12e364b2014-03-04 07:58:07 -0600259
Jes Sorensen52063ec2015-04-13 10:28:41 -0400260static void bus_create_response(u32 bus_no, int response);
261static void bus_destroy_response(u32 bus_no, int response);
262static void device_create_response(u32 bus_no, u32 dev_no, int response);
263static void device_destroy_response(u32 bus_no, u32 dev_no, int response);
264static void device_resume_response(u32 bus_no, u32 dev_no, int response);
Ken Cox12e364b2014-03-04 07:58:07 -0600265
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400266static void visorchipset_device_pause_response(u32 bus_no, u32 dev_no,
267 int response);
268
Benjamin Romer8e3fedd2015-03-16 13:58:43 -0400269static struct visorchipset_busdev_responders busdev_responders = {
Ken Cox12e364b2014-03-04 07:58:07 -0600270 .bus_create = bus_create_response,
271 .bus_destroy = bus_destroy_response,
272 .device_create = device_create_response,
273 .device_destroy = device_destroy_response,
Ken Cox927c7922014-03-05 14:52:25 -0600274 .device_pause = visorchipset_device_pause_response,
Ken Cox12e364b2014-03-04 07:58:07 -0600275 .device_resume = device_resume_response,
276};
277
278/* info for /dev/visorchipset */
Benjamin Romer5aa8ae52015-03-16 13:58:44 -0400279static dev_t major_dev = -1; /**< indicates major num for device */
Ken Cox12e364b2014-03-04 07:58:07 -0600280
Benjamin Romer19f66342014-07-22 09:56:25 -0400281/* prototypes for attributes */
282static ssize_t toolaction_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400283 struct device_attribute *attr, char *buf);
Benjamin Romer19f66342014-07-22 09:56:25 -0400284static ssize_t toolaction_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400285 struct device_attribute *attr,
286 const char *buf, size_t count);
Benjamin Romer19f66342014-07-22 09:56:25 -0400287static DEVICE_ATTR_RW(toolaction);
288
Benjamin Romer54b31222014-07-22 09:56:26 -0400289static ssize_t boottotool_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400290 struct device_attribute *attr, char *buf);
Benjamin Romer54b31222014-07-22 09:56:26 -0400291static ssize_t boottotool_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400292 struct device_attribute *attr, const char *buf,
293 size_t count);
Benjamin Romer54b31222014-07-22 09:56:26 -0400294static DEVICE_ATTR_RW(boottotool);
295
Benjamin Romer422af172014-07-24 14:08:42 -0400296static ssize_t error_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400297 char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400298static ssize_t error_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400299 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400300static DEVICE_ATTR_RW(error);
301
302static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400303 char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400304static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400305 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400306static DEVICE_ATTR_RW(textid);
307
308static ssize_t remaining_steps_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400309 struct device_attribute *attr, char *buf);
Benjamin Romer422af172014-07-24 14:08:42 -0400310static ssize_t remaining_steps_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400311 struct device_attribute *attr,
312 const char *buf, size_t count);
Benjamin Romer422af172014-07-24 14:08:42 -0400313static DEVICE_ATTR_RW(remaining_steps);
314
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400315static ssize_t chipsetready_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400316 struct device_attribute *attr,
317 const char *buf, size_t count);
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400318static DEVICE_ATTR_WO(chipsetready);
319
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400320static ssize_t devicedisabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400321 struct device_attribute *attr,
322 const char *buf, size_t count);
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400323static DEVICE_ATTR_WO(devicedisabled);
324
325static ssize_t deviceenabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400326 struct device_attribute *attr,
327 const char *buf, size_t count);
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400328static DEVICE_ATTR_WO(deviceenabled);
329
Benjamin Romer19f66342014-07-22 09:56:25 -0400330static struct attribute *visorchipset_install_attrs[] = {
331 &dev_attr_toolaction.attr,
Benjamin Romer54b31222014-07-22 09:56:26 -0400332 &dev_attr_boottotool.attr,
Benjamin Romer422af172014-07-24 14:08:42 -0400333 &dev_attr_error.attr,
334 &dev_attr_textid.attr,
335 &dev_attr_remaining_steps.attr,
Benjamin Romer19f66342014-07-22 09:56:25 -0400336 NULL
337};
338
339static struct attribute_group visorchipset_install_group = {
340 .name = "install",
341 .attrs = visorchipset_install_attrs
342};
343
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400344static struct attribute *visorchipset_guest_attrs[] = {
345 &dev_attr_chipsetready.attr,
346 NULL
347};
348
349static struct attribute_group visorchipset_guest_group = {
350 .name = "guest",
351 .attrs = visorchipset_guest_attrs
352};
353
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400354static struct attribute *visorchipset_parahotplug_attrs[] = {
355 &dev_attr_devicedisabled.attr,
356 &dev_attr_deviceenabled.attr,
357 NULL
358};
359
360static struct attribute_group visorchipset_parahotplug_group = {
361 .name = "parahotplug",
362 .attrs = visorchipset_parahotplug_attrs
363};
364
Benjamin Romer19f66342014-07-22 09:56:25 -0400365static const struct attribute_group *visorchipset_dev_groups[] = {
366 &visorchipset_install_group,
Benjamin Romer18b87ed2014-07-24 14:08:43 -0400367 &visorchipset_guest_group,
Benjamin Romere56fa7c2014-07-29 11:11:21 -0400368 &visorchipset_parahotplug_group,
Benjamin Romer19f66342014-07-22 09:56:25 -0400369 NULL
370};
371
Ken Cox12e364b2014-03-04 07:58:07 -0600372/* /sys/devices/platform/visorchipset */
Benjamin Romereb34e872015-03-16 13:58:45 -0400373static struct platform_device visorchipset_platform_device = {
Ken Cox12e364b2014-03-04 07:58:07 -0600374 .name = "visorchipset",
375 .id = -1,
Benjamin Romer19f66342014-07-22 09:56:25 -0400376 .dev.groups = visorchipset_dev_groups,
Ken Cox12e364b2014-03-04 07:58:07 -0600377};
378
379/* Function prototypes */
Benjamin Romerb3168c72015-03-16 13:58:46 -0400380static void controlvm_respond(struct controlvm_message_header *msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400381 int response);
382static void controlvm_respond_chipset_init(
Benjamin Romerb3168c72015-03-16 13:58:46 -0400383 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400384 enum ultra_chipset_feature features);
385static void controlvm_respond_physdev_changestate(
Benjamin Romerb3168c72015-03-16 13:58:46 -0400386 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -0400387 struct spar_segment_state state);
Ken Cox12e364b2014-03-04 07:58:07 -0600388
Erik Arfvidson46168812015-05-05 18:36:14 -0400389
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400390static void parser_done(struct parser_context *ctx);
391
Erik Arfvidson46168812015-05-05 18:36:14 -0400392static struct parser_context *
393parser_init_guts(u64 addr, u32 bytes, bool local,
394 bool standard_payload_header, bool *retry)
395{
396 int allocbytes = sizeof(struct parser_context) + bytes;
397 struct parser_context *rc = NULL;
398 struct parser_context *ctx = NULL;
Erik Arfvidson46168812015-05-05 18:36:14 -0400399 struct spar_controlvm_parameters_header *phdr = NULL;
400
401 if (retry)
402 *retry = false;
403 if (!standard_payload_header)
404 /* alloc and 0 extra byte to ensure payload is
405 * '\0'-terminated
406 */
407 allocbytes++;
408 if ((controlvm_payload_bytes_buffered + bytes)
409 > MAX_CONTROLVM_PAYLOAD_BYTES) {
410 if (retry)
411 *retry = true;
412 rc = NULL;
413 goto cleanup;
414 }
415 ctx = kzalloc(allocbytes, GFP_KERNEL|__GFP_NORETRY);
416 if (!ctx) {
417 if (retry)
418 *retry = true;
419 rc = NULL;
420 goto cleanup;
421 }
422
423 ctx->allocbytes = allocbytes;
424 ctx->param_bytes = bytes;
425 ctx->curr = NULL;
426 ctx->bytes_remaining = 0;
427 ctx->byte_stream = false;
428 if (local) {
429 void *p;
430
431 if (addr > virt_to_phys(high_memory - 1)) {
432 rc = NULL;
433 goto cleanup;
434 }
435 p = __va((unsigned long) (addr));
436 memcpy(ctx->data, p, bytes);
437 } else {
Jes Sorensendd412752015-05-05 18:36:26 -0400438 void __iomem *mapping;
Jes Sorensen712c03d2015-05-05 18:36:25 -0400439
Jes Sorensendd412752015-05-05 18:36:26 -0400440 if (!request_mem_region(addr, bytes, "visorchipset")) {
Erik Arfvidson46168812015-05-05 18:36:14 -0400441 rc = NULL;
442 goto cleanup;
443 }
Jes Sorensendd412752015-05-05 18:36:26 -0400444
445 mapping = ioremap_cache(addr, bytes);
446 if (!mapping) {
447 release_mem_region(addr, bytes);
448 rc = NULL;
449 goto cleanup;
450 }
451 memcpy_fromio(ctx->data, mapping, bytes);
452 release_mem_region(addr, bytes);
Erik Arfvidson46168812015-05-05 18:36:14 -0400453 }
454 if (!standard_payload_header) {
455 ctx->byte_stream = true;
456 rc = ctx;
457 goto cleanup;
458 }
459 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
460 if (phdr->total_length != bytes) {
461 rc = NULL;
462 goto cleanup;
463 }
464 if (phdr->total_length < phdr->header_length) {
465 rc = NULL;
466 goto cleanup;
467 }
468 if (phdr->header_length <
469 sizeof(struct spar_controlvm_parameters_header)) {
470 rc = NULL;
471 goto cleanup;
472 }
473
474 rc = ctx;
475cleanup:
Erik Arfvidson46168812015-05-05 18:36:14 -0400476 if (rc) {
477 controlvm_payload_bytes_buffered += ctx->param_bytes;
478 } else {
479 if (ctx) {
480 parser_done(ctx);
481 ctx = NULL;
482 }
483 }
484 return rc;
485}
486
487struct parser_context *
488parser_init(u64 addr, u32 bytes, bool local, bool *retry)
489{
490 return parser_init_guts(addr, bytes, local, true, retry);
491}
492
493/* Call this instead of parser_init() if the payload area consists of just
494 * a sequence of bytes, rather than a struct spar_controlvm_parameters_header
495 * structures. Afterwards, you can call parser_simpleString_get() or
496 * parser_byteStream_get() to obtain the data.
497 */
498struct parser_context *
499parser_init_byte_stream(u64 addr, u32 bytes, bool local, bool *retry)
500{
501 return parser_init_guts(addr, bytes, local, false, retry);
502}
503
504/* Obtain '\0'-terminated copy of string in payload area.
505 */
506char *
507parser_simpleString_get(struct parser_context *ctx)
508{
509 if (!ctx->byte_stream)
510 return NULL;
511 return ctx->data; /* note this IS '\0'-terminated, because of
512 * the num of bytes we alloc+clear in
513 * parser_init_byteStream() */
514}
515
516/* Obtain a copy of the buffer in the payload area.
517 */
518void *parser_byte_stream_get(struct parser_context *ctx, unsigned long *nbytes)
519{
520 if (!ctx->byte_stream)
521 return NULL;
522 if (nbytes)
523 *nbytes = ctx->param_bytes;
524 return (void *)ctx->data;
525}
526
527uuid_le
528parser_id_get(struct parser_context *ctx)
529{
530 struct spar_controlvm_parameters_header *phdr = NULL;
531
532 if (ctx == NULL)
533 return NULL_UUID_LE;
534 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
535 return phdr->id;
536}
537
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400538/** Describes the state from the perspective of which controlvm messages have
539 * been received for a bus or device.
540 */
541
542enum PARSER_WHICH_STRING {
543 PARSERSTRING_INITIATOR,
544 PARSERSTRING_TARGET,
545 PARSERSTRING_CONNECTION,
546 PARSERSTRING_NAME, /* TODO: only PARSERSTRING_NAME is used ? */
547};
548
Erik Arfvidson46168812015-05-05 18:36:14 -0400549void
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -0400550parser_param_start(struct parser_context *ctx,
551 enum PARSER_WHICH_STRING which_string)
Erik Arfvidson46168812015-05-05 18:36:14 -0400552{
553 struct spar_controlvm_parameters_header *phdr = NULL;
554
555 if (ctx == NULL)
556 goto Away;
557 phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
558 switch (which_string) {
559 case PARSERSTRING_INITIATOR:
560 ctx->curr = ctx->data + phdr->initiator_offset;
561 ctx->bytes_remaining = phdr->initiator_length;
562 break;
563 case PARSERSTRING_TARGET:
564 ctx->curr = ctx->data + phdr->target_offset;
565 ctx->bytes_remaining = phdr->target_length;
566 break;
567 case PARSERSTRING_CONNECTION:
568 ctx->curr = ctx->data + phdr->connection_offset;
569 ctx->bytes_remaining = phdr->connection_length;
570 break;
571 case PARSERSTRING_NAME:
572 ctx->curr = ctx->data + phdr->name_offset;
573 ctx->bytes_remaining = phdr->name_length;
574 break;
575 default:
576 break;
577 }
578
579Away:
580 return;
581}
582
583void
584parser_done(struct parser_context *ctx)
585{
586 if (!ctx)
587 return;
588 controlvm_payload_bytes_buffered -= ctx->param_bytes;
589 kfree(ctx);
590}
591
592/** Return length of string not counting trailing spaces. */
593static int
594string_length_no_trail(char *s, int len)
595{
596 int i = len - 1;
597
598 while (i >= 0) {
599 if (!isspace(s[i]))
600 return i + 1;
601 i--;
602 }
603 return 0;
604}
605
606/** Grab the next name and value out of the parameter buffer.
607 * The entire parameter buffer looks like this:
608 * <name>=<value>\0
609 * <name>=<value>\0
610 * ...
611 * \0
612 * If successful, the next <name> value is returned within the supplied
613 * <nam> buffer (the value is always upper-cased), and the corresponding
614 * <value> is returned within a kmalloc()ed buffer, whose pointer is
615 * provided as the return value of this function.
616 * (The total number of bytes allocated is strlen(<value>)+1.)
617 *
618 * NULL is returned to indicate failure, which can occur for several reasons:
619 * - all <name>=<value> pairs have already been processed
620 * - bad parameter
621 * - parameter buffer ends prematurely (couldn't find an '=' or '\0' within
622 * the confines of the parameter buffer)
623 * - the <nam> buffer is not large enough to hold the <name> of the next
624 * parameter
625 */
626void *
627parser_param_get(struct parser_context *ctx, char *nam, int namesize)
628{
629 u8 *pscan, *pnam = nam;
630 unsigned long nscan;
631 int value_length = -1, orig_value_length = -1;
632 void *value = NULL;
633 int i;
634 int closing_quote = 0;
635
636 if (!ctx)
637 return NULL;
638 pscan = ctx->curr;
639 nscan = ctx->bytes_remaining;
640 if (nscan == 0)
641 return NULL;
642 if (*pscan == '\0')
643 /* This is the normal return point after you have processed
644 * all of the <name>=<value> pairs in a syntactically-valid
645 * parameter buffer.
646 */
647 return NULL;
648
649 /* skip whitespace */
650 while (isspace(*pscan)) {
651 pscan++;
652 nscan--;
653 if (nscan == 0)
654 return NULL;
655 }
656
657 while (*pscan != ':') {
658 if (namesize <= 0)
659 return NULL;
660 *pnam = toupper(*pscan);
661 pnam++;
662 namesize--;
663 pscan++;
664 nscan--;
665 if (nscan == 0)
666 return NULL;
667 }
668 if (namesize <= 0)
669 return NULL;
670 *pnam = '\0';
671 nam[string_length_no_trail(nam, strlen(nam))] = '\0';
672
673 /* point to char immediately after ":" in "<name>:<value>" */
674 pscan++;
675 nscan--;
676 /* skip whitespace */
677 while (isspace(*pscan)) {
678 pscan++;
679 nscan--;
680 if (nscan == 0)
681 return NULL;
682 }
683 if (nscan == 0)
684 return NULL;
685 if (*pscan == '\'' || *pscan == '"') {
686 closing_quote = *pscan;
687 pscan++;
688 nscan--;
689 if (nscan == 0)
690 return NULL;
691 }
692
693 /* look for a separator character, terminator character, or
694 * end of data
695 */
696 for (i = 0, value_length = -1; i < nscan; i++) {
697 if (closing_quote) {
698 if (pscan[i] == '\0')
699 return NULL;
700 if (pscan[i] == closing_quote) {
701 value_length = i;
702 break;
703 }
704 } else
705 if (pscan[i] == ',' || pscan[i] == ';'
706 || pscan[i] == '\0') {
707 value_length = i;
708 break;
709 }
710 }
711 if (value_length < 0) {
712 if (closing_quote)
713 return NULL;
714 value_length = nscan;
715 }
716 orig_value_length = value_length;
717 if (closing_quote == 0)
718 value_length = string_length_no_trail(pscan, orig_value_length);
719 value = kmalloc(value_length + 1, GFP_KERNEL|__GFP_NORETRY);
720 if (value == NULL)
721 return NULL;
722 memcpy(value, pscan, value_length);
723 ((u8 *) (value))[value_length] = '\0';
724
725 pscan += orig_value_length;
726 nscan -= orig_value_length;
727
728 /* skip past separator or closing quote */
729 if (nscan > 0) {
730 if (*pscan != '\0') {
731 pscan++;
732 nscan--;
733 }
734 }
735
736 if (closing_quote && (nscan > 0)) {
737 /* we still need to skip around the real separator if present */
738 /* first, skip whitespace */
739 while (isspace(*pscan)) {
740 pscan++;
741 nscan--;
742 if (nscan == 0)
743 break;
744 }
745 if (nscan > 0) {
746 if (*pscan == ',' || *pscan == ';') {
747 pscan++;
748 nscan--;
749 } else if (*pscan != '\0') {
750 kfree(value);
751 value = NULL;
752 return NULL;
753 }
754 }
755 }
756 ctx->curr = pscan;
757 ctx->bytes_remaining = nscan;
758 return value;
759}
760
761void *
762parser_string_get(struct parser_context *ctx)
763{
764 u8 *pscan;
765 unsigned long nscan;
766 int value_length = -1;
767 void *value = NULL;
768 int i;
769
770 if (!ctx)
771 return NULL;
772 pscan = ctx->curr;
773 nscan = ctx->bytes_remaining;
774 if (nscan == 0)
775 return NULL;
776 if (!pscan)
777 return NULL;
778 for (i = 0, value_length = -1; i < nscan; i++)
779 if (pscan[i] == '\0') {
780 value_length = i;
781 break;
782 }
783 if (value_length < 0) /* '\0' was not included in the length */
784 value_length = nscan;
785 value = kmalloc(value_length + 1, GFP_KERNEL|__GFP_NORETRY);
786 if (value == NULL)
787 return NULL;
788 if (value_length > 0)
789 memcpy(value, pscan, value_length);
790 ((u8 *) (value))[value_length] = '\0';
791 return value;
792}
793
794
Vincent Bernatd746cb52014-08-01 10:29:30 +0200795static ssize_t toolaction_show(struct device *dev,
796 struct device_attribute *attr,
797 char *buf)
Benjamin Romer19f66342014-07-22 09:56:25 -0400798{
Benjamin Romer01f4d852015-03-16 13:58:48 -0400799 u8 tool_action;
Benjamin Romer19f66342014-07-22 09:56:25 -0400800
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400801 visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400802 offsetof(struct spar_controlvm_channel_protocol,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400803 tool_action), &tool_action, sizeof(u8));
Benjamin Romer01f4d852015-03-16 13:58:48 -0400804 return scnprintf(buf, PAGE_SIZE, "%u\n", tool_action);
Benjamin Romer19f66342014-07-22 09:56:25 -0400805}
806
Vincent Bernatd746cb52014-08-01 10:29:30 +0200807static ssize_t toolaction_store(struct device *dev,
808 struct device_attribute *attr,
809 const char *buf, size_t count)
Benjamin Romer19f66342014-07-22 09:56:25 -0400810{
Benjamin Romer01f4d852015-03-16 13:58:48 -0400811 u8 tool_action;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400812 int ret;
Benjamin Romer19f66342014-07-22 09:56:25 -0400813
Jes Sorensenebec8962015-05-05 18:35:57 -0400814 if (kstrtou8(buf, 10, &tool_action))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400815 return -EINVAL;
816
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400817 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400818 offsetof(struct spar_controlvm_channel_protocol,
819 tool_action),
Benjamin Romer01f4d852015-03-16 13:58:48 -0400820 &tool_action, sizeof(u8));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400821
822 if (ret)
823 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400824 return count;
Benjamin Romer19f66342014-07-22 09:56:25 -0400825}
826
Vincent Bernatd746cb52014-08-01 10:29:30 +0200827static ssize_t boottotool_show(struct device *dev,
828 struct device_attribute *attr,
829 char *buf)
Benjamin Romer54b31222014-07-22 09:56:26 -0400830{
Benjamin Romer365522d2015-03-16 13:58:49 -0400831 struct efi_spar_indication efi_spar_indication;
Benjamin Romer54b31222014-07-22 09:56:26 -0400832
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400833 visorchannel_read(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400834 offsetof(struct spar_controlvm_channel_protocol,
835 efi_spar_ind), &efi_spar_indication,
836 sizeof(struct efi_spar_indication));
Benjamin Romer54b31222014-07-22 09:56:26 -0400837 return scnprintf(buf, PAGE_SIZE, "%u\n",
Benjamin Romer8e76e692015-03-16 13:58:52 -0400838 efi_spar_indication.boot_to_tool);
Benjamin Romer54b31222014-07-22 09:56:26 -0400839}
840
Vincent Bernatd746cb52014-08-01 10:29:30 +0200841static ssize_t boottotool_store(struct device *dev,
842 struct device_attribute *attr,
843 const char *buf, size_t count)
Benjamin Romer54b31222014-07-22 09:56:26 -0400844{
Benjamin Romer66e24b72014-07-25 13:55:10 -0400845 int val, ret;
Benjamin Romer365522d2015-03-16 13:58:49 -0400846 struct efi_spar_indication efi_spar_indication;
Benjamin Romer54b31222014-07-22 09:56:26 -0400847
Jes Sorensenebec8962015-05-05 18:35:57 -0400848 if (kstrtoint(buf, 10, &val))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400849 return -EINVAL;
850
Benjamin Romer365522d2015-03-16 13:58:49 -0400851 efi_spar_indication.boot_to_tool = val;
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400852 ret = visorchannel_write(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -0400853 offsetof(struct spar_controlvm_channel_protocol,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400854 efi_spar_ind), &(efi_spar_indication),
855 sizeof(struct efi_spar_indication));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400856
857 if (ret)
858 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400859 return count;
Benjamin Romer54b31222014-07-22 09:56:26 -0400860}
Benjamin Romer422af172014-07-24 14:08:42 -0400861
862static ssize_t error_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400863 char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400864{
865 u32 error;
866
Benjamin Romer8e76e692015-03-16 13:58:52 -0400867 visorchannel_read(controlvm_channel,
868 offsetof(struct spar_controlvm_channel_protocol,
869 installation_error),
870 &error, sizeof(u32));
Benjamin Romer422af172014-07-24 14:08:42 -0400871 return scnprintf(buf, PAGE_SIZE, "%i\n", error);
872}
873
874static ssize_t error_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400875 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400876{
877 u32 error;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400878 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400879
Jes Sorensenebec8962015-05-05 18:35:57 -0400880 if (kstrtou32(buf, 10, &error))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400881 return -EINVAL;
882
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400883 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400884 offsetof(struct spar_controlvm_channel_protocol,
885 installation_error),
886 &error, sizeof(u32));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400887 if (ret)
888 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400889 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400890}
891
892static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400893 char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400894{
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400895 u32 text_id;
Benjamin Romer422af172014-07-24 14:08:42 -0400896
Benjamin Romer8e76e692015-03-16 13:58:52 -0400897 visorchannel_read(controlvm_channel,
898 offsetof(struct spar_controlvm_channel_protocol,
899 installation_text_id),
900 &text_id, sizeof(u32));
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400901 return scnprintf(buf, PAGE_SIZE, "%i\n", text_id);
Benjamin Romer422af172014-07-24 14:08:42 -0400902}
903
904static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400905 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400906{
Benjamin Romer10dbf0e2015-03-16 13:58:50 -0400907 u32 text_id;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400908 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400909
Jes Sorensenebec8962015-05-05 18:35:57 -0400910 if (kstrtou32(buf, 10, &text_id))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400911 return -EINVAL;
912
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400913 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400914 offsetof(struct spar_controlvm_channel_protocol,
915 installation_text_id),
916 &text_id, sizeof(u32));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400917 if (ret)
918 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400919 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400920}
921
Benjamin Romer422af172014-07-24 14:08:42 -0400922static ssize_t remaining_steps_show(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400923 struct device_attribute *attr, char *buf)
Benjamin Romer422af172014-07-24 14:08:42 -0400924{
Benjamin Romeree8da292015-03-16 13:58:51 -0400925 u16 remaining_steps;
Benjamin Romer422af172014-07-24 14:08:42 -0400926
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400927 visorchannel_read(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400928 offsetof(struct spar_controlvm_channel_protocol,
929 installation_remaining_steps),
930 &remaining_steps, sizeof(u16));
Benjamin Romeree8da292015-03-16 13:58:51 -0400931 return scnprintf(buf, PAGE_SIZE, "%hu\n", remaining_steps);
Benjamin Romer422af172014-07-24 14:08:42 -0400932}
933
934static ssize_t remaining_steps_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400935 struct device_attribute *attr,
936 const char *buf, size_t count)
Benjamin Romer422af172014-07-24 14:08:42 -0400937{
Benjamin Romeree8da292015-03-16 13:58:51 -0400938 u16 remaining_steps;
Benjamin Romer66e24b72014-07-25 13:55:10 -0400939 int ret;
Benjamin Romer422af172014-07-24 14:08:42 -0400940
Jes Sorensenebec8962015-05-05 18:35:57 -0400941 if (kstrtou16(buf, 10, &remaining_steps))
Benjamin Romer66e24b72014-07-25 13:55:10 -0400942 return -EINVAL;
943
Benjamin Romerc3d9a222015-03-16 13:58:05 -0400944 ret = visorchannel_write(controlvm_channel,
Benjamin Romer8e76e692015-03-16 13:58:52 -0400945 offsetof(struct spar_controlvm_channel_protocol,
946 installation_remaining_steps),
947 &remaining_steps, sizeof(u16));
Benjamin Romer66e24b72014-07-25 13:55:10 -0400948 if (ret)
949 return ret;
Benjamin Romere22a4a02014-08-18 09:34:54 -0400950 return count;
Benjamin Romer422af172014-07-24 14:08:42 -0400951}
952
Ken Cox12e364b2014-03-04 07:58:07 -0600953static void
Benjamin Romer9b989a982015-03-16 13:58:11 -0400954bus_info_clear(void *v)
Ken Cox12e364b2014-03-04 07:58:07 -0600955{
Jes Sorensenbbd4be32015-05-05 18:35:43 -0400956 struct visorchipset_bus_info *p = (struct visorchipset_bus_info *) v;
Ken Cox12e364b2014-03-04 07:58:07 -0600957
Ken Cox12e364b2014-03-04 07:58:07 -0600958 kfree(p->name);
Ken Cox12e364b2014-03-04 07:58:07 -0600959 kfree(p->description);
Benjamin Romer33192fa2014-10-31 09:57:27 -0400960 memset(p, 0, sizeof(struct visorchipset_bus_info));
Ken Cox12e364b2014-03-04 07:58:07 -0600961}
962
963static void
Benjamin Romer9b989a982015-03-16 13:58:11 -0400964dev_info_clear(void *v)
Ken Cox12e364b2014-03-04 07:58:07 -0600965{
Benjamin Romer246e0cd2014-10-31 09:57:24 -0400966 struct visorchipset_device_info *p =
Jes Sorensenbbd4be32015-05-05 18:35:43 -0400967 (struct visorchipset_device_info *) v;
Benjamin Romer26eb2c02014-08-18 09:34:53 -0400968
Benjamin Romer246e0cd2014-10-31 09:57:24 -0400969 memset(p, 0, sizeof(struct visorchipset_device_info));
Ken Cox12e364b2014-03-04 07:58:07 -0600970}
971
Jes Sorensen4f665202015-05-05 18:35:52 -0400972static struct visorchipset_bus_info *
973bus_find(struct list_head *list, u32 bus_no)
974{
975 struct visorchipset_bus_info *p;
976
977 list_for_each_entry(p, list, entry) {
978 if (p->bus_no == bus_no)
979 return p;
980 }
981
982 return NULL;
983}
984
Jes Sorensend480f6a2015-05-05 18:35:54 -0400985static struct visorchipset_device_info *
986device_find(struct list_head *list, u32 bus_no, u32 dev_no)
987{
988 struct visorchipset_device_info *p;
989
990 list_for_each_entry(p, list, entry) {
991 if (p->bus_no == bus_no && p->dev_no == dev_no)
992 return p;
993 }
994
995 return NULL;
996}
997
Jes Sorensen28723522015-05-05 18:35:55 -0400998static void busdevices_del(struct list_head *list, u32 bus_no)
999{
1000 struct visorchipset_device_info *p, *tmp;
1001
1002 list_for_each_entry_safe(p, tmp, list, entry) {
1003 if (p->bus_no == bus_no) {
1004 list_del(&p->entry);
1005 kfree(p);
1006 }
1007 }
1008}
1009
Benjamin Romerc2422332014-07-29 15:09:40 -04001010static u8
Ken Cox12e364b2014-03-04 07:58:07 -06001011check_chipset_events(void)
1012{
1013 int i;
Benjamin Romerc2422332014-07-29 15:09:40 -04001014 u8 send_msg = 1;
Ken Cox12e364b2014-03-04 07:58:07 -06001015 /* Check events to determine if response should be sent */
1016 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
1017 send_msg &= chipset_events[i];
1018 return send_msg;
1019}
1020
1021static void
1022clear_chipset_events(void)
1023{
1024 int i;
1025 /* Clear chipset_events */
1026 for (i = 0; i < MAX_CHIPSET_EVENTS; i++)
1027 chipset_events[i] = 0;
1028}
1029
1030void
Benjamin Romerfe90d892014-10-31 09:57:32 -04001031visorchipset_register_busdev_server(
1032 struct visorchipset_busdev_notifiers *notifiers,
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001033 struct visorchipset_busdev_responders *responders,
Benjamin Romer1e7a59c2014-10-31 09:57:35 -04001034 struct ultra_vbus_deviceinfo *driver_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001035{
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001036 down(&notifier_lock);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001037 if (!notifiers) {
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001038 memset(&busdev_server_notifiers, 0,
1039 sizeof(busdev_server_notifiers));
Ken Cox12e364b2014-03-04 07:58:07 -06001040 serverregistered = 0; /* clear flag */
1041 } else {
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001042 busdev_server_notifiers = *notifiers;
Ken Cox12e364b2014-03-04 07:58:07 -06001043 serverregistered = 1; /* set flag */
1044 }
1045 if (responders)
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04001046 *responders = busdev_responders;
Benjamin Romer1e7a59c2014-10-31 09:57:35 -04001047 if (driver_info)
1048 bus_device_info_init(driver_info, "chipset", "visorchipset",
Benjamin Romer8e76e692015-03-16 13:58:52 -04001049 VERSION, NULL);
Ken Cox12e364b2014-03-04 07:58:07 -06001050
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001051 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001052}
1053EXPORT_SYMBOL_GPL(visorchipset_register_busdev_server);
1054
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -04001055/** Register functions (in the bus driver) to get called by visorchipset
1056 * whenever a bus or device appears for which this service partition is
1057 * to be the server for. visorchipset will fill in <responders>, to
1058 * indicate functions the bus driver should call to indicate message
1059 * responses.
1060 */
Ken Cox12e364b2014-03-04 07:58:07 -06001061void
Benjamin Romerfe90d892014-10-31 09:57:32 -04001062visorchipset_register_busdev_client(
1063 struct visorchipset_busdev_notifiers *notifiers,
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001064 struct visorchipset_busdev_responders *responders,
Benjamin Romer43fce012014-10-31 09:57:34 -04001065 struct ultra_vbus_deviceinfo *driver_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001066{
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001067 down(&notifier_lock);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001068 if (!notifiers) {
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001069 memset(&busdev_client_notifiers, 0,
1070 sizeof(busdev_client_notifiers));
Ken Cox12e364b2014-03-04 07:58:07 -06001071 clientregistered = 0; /* clear flag */
1072 } else {
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001073 busdev_client_notifiers = *notifiers;
Ken Cox12e364b2014-03-04 07:58:07 -06001074 clientregistered = 1; /* set flag */
1075 }
1076 if (responders)
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04001077 *responders = busdev_responders;
Benjamin Romer43fce012014-10-31 09:57:34 -04001078 if (driver_info)
1079 bus_device_info_init(driver_info, "chipset(bolts)",
1080 "visorchipset", VERSION, NULL);
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001081 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001082}
1083EXPORT_SYMBOL_GPL(visorchipset_register_busdev_client);
1084
1085static void
1086cleanup_controlvm_structures(void)
1087{
Benjamin Romer33192fa2014-10-31 09:57:27 -04001088 struct visorchipset_bus_info *bi, *tmp_bi;
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001089 struct visorchipset_device_info *di, *tmp_di;
Ken Cox12e364b2014-03-04 07:58:07 -06001090
Benjamin Romer1390b882015-03-16 13:58:03 -04001091 list_for_each_entry_safe(bi, tmp_bi, &bus_info_list, entry) {
Benjamin Romer9b989a982015-03-16 13:58:11 -04001092 bus_info_clear(bi);
Ken Cox12e364b2014-03-04 07:58:07 -06001093 list_del(&bi->entry);
1094 kfree(bi);
1095 }
1096
Benjamin Romer1390b882015-03-16 13:58:03 -04001097 list_for_each_entry_safe(di, tmp_di, &dev_info_list, entry) {
Benjamin Romer9b989a982015-03-16 13:58:11 -04001098 dev_info_clear(di);
Ken Cox12e364b2014-03-04 07:58:07 -06001099 list_del(&di->entry);
1100 kfree(di);
1101 }
1102}
1103
1104static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001105chipset_init(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001106{
1107 static int chipset_inited;
Benjamin Romerb9b141e2014-10-23 14:30:24 -04001108 enum ultra_chipset_feature features = 0;
Ken Cox12e364b2014-03-04 07:58:07 -06001109 int rc = CONTROLVM_RESP_SUCCESS;
1110
1111 POSTCODE_LINUX_2(CHIPSET_INIT_ENTRY_PC, POSTCODE_SEVERITY_INFO);
1112 if (chipset_inited) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001113 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romere3199b22015-03-16 13:58:14 -04001114 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001115 }
1116 chipset_inited = 1;
1117 POSTCODE_LINUX_2(CHIPSET_INIT_EXIT_PC, POSTCODE_SEVERITY_INFO);
1118
1119 /* Set features to indicate we support parahotplug (if Command
1120 * also supports it). */
1121 features =
Benjamin Romer2ea51172014-10-23 14:30:25 -04001122 inmsg->cmd.init_chipset.
Ken Cox12e364b2014-03-04 07:58:07 -06001123 features & ULTRA_CHIPSET_FEATURE_PARA_HOTPLUG;
1124
1125 /* Set the "reply" bit so Command knows this is a
1126 * features-aware driver. */
1127 features |= ULTRA_CHIPSET_FEATURE_REPLY;
1128
Benjamin Romere3199b22015-03-16 13:58:14 -04001129cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001130 if (rc < 0)
1131 cleanup_controlvm_structures();
Benjamin Romer98d7b592014-10-23 14:30:26 -04001132 if (inmsg->hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06001133 controlvm_respond_chipset_init(&inmsg->hdr, rc, features);
1134}
1135
1136static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001137controlvm_init_response(struct controlvm_message *msg,
Benjamin Romerb3168c72015-03-16 13:58:46 -04001138 struct controlvm_message_header *msg_hdr, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001139{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001140 memset(msg, 0, sizeof(struct controlvm_message));
Benjamin Romerb3168c72015-03-16 13:58:46 -04001141 memcpy(&msg->hdr, msg_hdr, sizeof(struct controlvm_message_header));
Benjamin Romer98d7b592014-10-23 14:30:26 -04001142 msg->hdr.payload_bytes = 0;
1143 msg->hdr.payload_vm_offset = 0;
1144 msg->hdr.payload_max_bytes = 0;
Ken Cox12e364b2014-03-04 07:58:07 -06001145 if (response < 0) {
Benjamin Romer98d7b592014-10-23 14:30:26 -04001146 msg->hdr.flags.failed = 1;
1147 msg->hdr.completion_status = (u32) (-response);
Ken Cox12e364b2014-03-04 07:58:07 -06001148 }
1149}
1150
1151static void
Benjamin Romerb3168c72015-03-16 13:58:46 -04001152controlvm_respond(struct controlvm_message_header *msg_hdr, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001153{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001154 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001155
Benjamin Romerb3168c72015-03-16 13:58:46 -04001156 controlvm_init_response(&outmsg, msg_hdr, response);
Ken Cox12e364b2014-03-04 07:58:07 -06001157 /* For DiagPool channel DEVICE_CHANGESTATE, we need to send
1158 * back the deviceChangeState structure in the packet. */
Benjamin Romerb3168c72015-03-16 13:58:46 -04001159 if (msg_hdr->id == CONTROLVM_DEVICE_CHANGESTATE &&
Benjamin Romer0639ba62015-03-16 13:58:04 -04001160 g_devicechangestate_packet.device_change_state.bus_no ==
1161 g_diagpool_bus_no &&
1162 g_devicechangestate_packet.device_change_state.dev_no ==
Benjamin Romer83d48902015-03-16 13:58:01 -04001163 g_diagpool_dev_no)
Benjamin Romer4f44b722015-03-16 13:58:02 -04001164 outmsg.cmd = g_devicechangestate_packet;
Benjamin Romer2098dbd2015-03-04 12:14:22 -05001165 if (outmsg.hdr.flags.test_message == 1)
Ken Cox12e364b2014-03-04 07:58:07 -06001166 return;
Benjamin Romer2098dbd2015-03-04 12:14:22 -05001167
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001168 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001169 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -06001170 return;
1171 }
1172}
1173
1174static void
Benjamin Romerb3168c72015-03-16 13:58:46 -04001175controlvm_respond_chipset_init(struct controlvm_message_header *msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001176 int response,
Benjamin Romerb9b141e2014-10-23 14:30:24 -04001177 enum ultra_chipset_feature features)
Ken Cox12e364b2014-03-04 07:58:07 -06001178{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001179 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001180
Benjamin Romerb3168c72015-03-16 13:58:46 -04001181 controlvm_init_response(&outmsg, msg_hdr, response);
Benjamin Romer2ea51172014-10-23 14:30:25 -04001182 outmsg.cmd.init_chipset.features = features;
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001183 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001184 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -06001185 return;
1186 }
1187}
1188
Benjamin Romer98d7b592014-10-23 14:30:26 -04001189static void controlvm_respond_physdev_changestate(
Benjamin Romerb3168c72015-03-16 13:58:46 -04001190 struct controlvm_message_header *msg_hdr, int response,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001191 struct spar_segment_state state)
Ken Cox12e364b2014-03-04 07:58:07 -06001192{
Benjamin Romer3ab47702014-10-23 14:30:31 -04001193 struct controlvm_message outmsg;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001194
Benjamin Romerb3168c72015-03-16 13:58:46 -04001195 controlvm_init_response(&outmsg, msg_hdr, response);
Benjamin Romer2ea51172014-10-23 14:30:25 -04001196 outmsg.cmd.device_change_state.state = state;
1197 outmsg.cmd.device_change_state.flags.phys_device = 1;
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001198 if (!visorchannel_signalinsert(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001199 CONTROLVM_QUEUE_REQUEST, &outmsg)) {
Ken Cox12e364b2014-03-04 07:58:07 -06001200 return;
1201 }
1202}
1203
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -04001204enum crash_obj_type {
1205 CRASH_DEV,
1206 CRASH_BUS,
1207};
1208
Ken Cox12e364b2014-03-04 07:58:07 -06001209void
Benjamin Romer2c683cd2014-10-31 09:57:22 -04001210visorchipset_save_message(struct controlvm_message *msg,
1211 enum crash_obj_type type)
Ken Cox12e364b2014-03-04 07:58:07 -06001212{
Benjamin Romer45772252015-03-16 13:58:15 -04001213 u32 crash_msg_offset;
1214 u16 crash_msg_count;
Ken Cox12e364b2014-03-04 07:58:07 -06001215
1216 /* get saved message count */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001217 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001218 offsetof(struct spar_controlvm_channel_protocol,
1219 saved_crash_message_count),
Benjamin Romer45772252015-03-16 13:58:15 -04001220 &crash_msg_count, sizeof(u16)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001221 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
1222 POSTCODE_SEVERITY_ERR);
1223 return;
1224 }
1225
Benjamin Romer45772252015-03-16 13:58:15 -04001226 if (crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
Ken Cox12e364b2014-03-04 07:58:07 -06001227 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
Benjamin Romer45772252015-03-16 13:58:15 -04001228 crash_msg_count,
Ken Cox12e364b2014-03-04 07:58:07 -06001229 POSTCODE_SEVERITY_ERR);
1230 return;
1231 }
1232
1233 /* get saved crash message offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001234 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001235 offsetof(struct spar_controlvm_channel_protocol,
1236 saved_crash_message_offset),
Benjamin Romer45772252015-03-16 13:58:15 -04001237 &crash_msg_offset, sizeof(u32)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001238 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
1239 POSTCODE_SEVERITY_ERR);
1240 return;
1241 }
1242
Benjamin Romer2c683cd2014-10-31 09:57:22 -04001243 if (type == CRASH_BUS) {
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001244 if (visorchannel_write(controlvm_channel,
Benjamin Romer45772252015-03-16 13:58:15 -04001245 crash_msg_offset,
Benjamin Romer3ab47702014-10-23 14:30:31 -04001246 msg,
1247 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001248 POSTCODE_LINUX_2(SAVE_MSG_BUS_FAILURE_PC,
1249 POSTCODE_SEVERITY_ERR);
1250 return;
1251 }
Prarit Bhargava2ee0dee2015-05-05 18:36:16 -04001252 } else { /* CRASH_DEV */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001253 if (visorchannel_write(controlvm_channel,
Benjamin Romer45772252015-03-16 13:58:15 -04001254 crash_msg_offset +
Benjamin Romer3ab47702014-10-23 14:30:31 -04001255 sizeof(struct controlvm_message), msg,
1256 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001257 POSTCODE_LINUX_2(SAVE_MSG_DEV_FAILURE_PC,
1258 POSTCODE_SEVERITY_ERR);
1259 return;
1260 }
1261 }
1262}
1263EXPORT_SYMBOL_GPL(visorchipset_save_message);
1264
1265static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04001266bus_responder(enum controlvm_id cmd_id, u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001267{
Jes Sorensene82ba622015-05-05 18:35:45 -04001268 struct visorchipset_bus_info *p;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001269 bool need_clear = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001270
Jes Sorensen4f665202015-05-05 18:35:52 -04001271 p = bus_find(&bus_info_list, bus_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001272 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001273 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001274
Ken Cox12e364b2014-03-04 07:58:07 -06001275 if (response < 0) {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001276 if ((cmd_id == CONTROLVM_BUS_CREATE) &&
Ken Cox12e364b2014-03-04 07:58:07 -06001277 (response != (-CONTROLVM_RESP_ERROR_ALREADY_DONE)))
1278 /* undo the row we just created... */
Jes Sorensen28723522015-05-05 18:35:55 -04001279 busdevices_del(&dev_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001280 } else {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001281 if (cmd_id == CONTROLVM_BUS_CREATE)
Ken Cox12e364b2014-03-04 07:58:07 -06001282 p->state.created = 1;
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001283 if (cmd_id == CONTROLVM_BUS_DESTROY)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001284 need_clear = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001285 }
1286
Benjamin Romer0aca78442015-03-04 12:14:25 -05001287 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001288 return; /* no controlvm response needed */
Benjamin Romer6b59b312015-03-16 13:58:18 -04001289 if (p->pending_msg_hdr.id != (u32)cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001290 return;
Benjamin Romer33192fa2014-10-31 09:57:27 -04001291 controlvm_respond(&p->pending_msg_hdr, response);
1292 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001293 if (need_clear) {
Benjamin Romer9b989a982015-03-16 13:58:11 -04001294 bus_info_clear(p);
Jes Sorensen28723522015-05-05 18:35:55 -04001295 busdevices_del(&dev_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001296 }
1297}
1298
1299static void
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001300device_changestate_responder(enum controlvm_id cmd_id,
Jes Sorensen52063ec2015-04-13 10:28:41 -04001301 u32 bus_no, u32 dev_no, int response,
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001302 struct spar_segment_state response_state)
Ken Cox12e364b2014-03-04 07:58:07 -06001303{
Jes Sorensene82ba622015-05-05 18:35:45 -04001304 struct visorchipset_device_info *p;
Benjamin Romer3ab47702014-10-23 14:30:31 -04001305 struct controlvm_message outmsg;
Ken Cox12e364b2014-03-04 07:58:07 -06001306
Jes Sorensend480f6a2015-05-05 18:35:54 -04001307 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001308 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001309 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001310 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001311 return; /* no controlvm response needed */
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001312 if (p->pending_msg_hdr.id != cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001313 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001314
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001315 controlvm_init_response(&outmsg, &p->pending_msg_hdr, response);
Ken Cox12e364b2014-03-04 07:58:07 -06001316
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001317 outmsg.cmd.device_change_state.bus_no = bus_no;
1318 outmsg.cmd.device_change_state.dev_no = dev_no;
1319 outmsg.cmd.device_change_state.state = response_state;
Ken Cox12e364b2014-03-04 07:58:07 -06001320
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001321 if (!visorchannel_signalinsert(controlvm_channel,
Benjamin Romer0aca78442015-03-04 12:14:25 -05001322 CONTROLVM_QUEUE_REQUEST, &outmsg))
Ken Cox12e364b2014-03-04 07:58:07 -06001323 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001324
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001325 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001326}
1327
1328static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04001329device_responder(enum controlvm_id cmd_id, u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06001330{
Jes Sorensene82ba622015-05-05 18:35:45 -04001331 struct visorchipset_device_info *p;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001332 bool need_clear = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001333
Jes Sorensend480f6a2015-05-05 18:35:54 -04001334 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0aca78442015-03-04 12:14:25 -05001335 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06001336 return;
Ken Cox12e364b2014-03-04 07:58:07 -06001337 if (response >= 0) {
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001338 if (cmd_id == CONTROLVM_DEVICE_CREATE)
Ken Cox12e364b2014-03-04 07:58:07 -06001339 p->state.created = 1;
Benjamin Romerfbb31f42015-03-16 13:58:16 -04001340 if (cmd_id == CONTROLVM_DEVICE_DESTROY)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001341 need_clear = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001342 }
1343
Benjamin Romer0aca78442015-03-04 12:14:25 -05001344 if (p->pending_msg_hdr.id == CONTROLVM_INVALID)
Ken Cox12e364b2014-03-04 07:58:07 -06001345 return; /* no controlvm response needed */
Benjamin Romer0aca78442015-03-04 12:14:25 -05001346
Benjamin Romer6b59b312015-03-16 13:58:18 -04001347 if (p->pending_msg_hdr.id != (u32)cmd_id)
Ken Cox12e364b2014-03-04 07:58:07 -06001348 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001349
Benjamin Romer246e0cd2014-10-31 09:57:24 -04001350 controlvm_respond(&p->pending_msg_hdr, response);
1351 p->pending_msg_hdr.id = CONTROLVM_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001352 if (need_clear)
Benjamin Romer9b989a982015-03-16 13:58:11 -04001353 dev_info_clear(p);
Ken Cox12e364b2014-03-04 07:58:07 -06001354}
1355
1356static void
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001357bus_epilog(u32 bus_no,
1358 u32 cmd, struct controlvm_message_header *msg_hdr,
Jes Sorensenf4c11552015-04-13 10:28:40 -04001359 int response, bool need_response)
Ken Cox12e364b2014-03-04 07:58:07 -06001360{
Jes Sorensen4f665202015-05-05 18:35:52 -04001361 struct visorchipset_bus_info *bus_info;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001362 bool notified = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001363
Jes Sorensen4f665202015-05-05 18:35:52 -04001364 bus_info = bus_find(&bus_info_list, bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001365
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001366 if (!bus_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001367 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001368
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001369 if (need_response) {
1370 memcpy(&bus_info->pending_msg_hdr, msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001371 sizeof(struct controlvm_message_header));
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001372 } else {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001373 bus_info->pending_msg_hdr.id = CONTROLVM_INVALID;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001374 }
Ken Cox12e364b2014-03-04 07:58:07 -06001375
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001376 down(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001377 if (response == CONTROLVM_RESP_SUCCESS) {
1378 switch (cmd) {
1379 case CONTROLVM_BUS_CREATE:
1380 /* We can't tell from the bus_create
1381 * information which of our 2 bus flavors the
1382 * devices on this bus will ultimately end up.
1383 * FORTUNATELY, it turns out it is harmless to
1384 * send the bus_create to both of them. We can
1385 * narrow things down a little bit, though,
1386 * because we know: - BusDev_Server can handle
1387 * either server or client devices
1388 * - BusDev_Client can handle ONLY client
1389 * devices */
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001390 if (busdev_server_notifiers.bus_create) {
1391 (*busdev_server_notifiers.bus_create) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001392 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001393 }
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001394 if ((!bus_info->flags.server) /*client */ &&
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001395 busdev_client_notifiers.bus_create) {
1396 (*busdev_client_notifiers.bus_create) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001397 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001398 }
1399 break;
1400 case CONTROLVM_BUS_DESTROY:
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001401 if (busdev_server_notifiers.bus_destroy) {
1402 (*busdev_server_notifiers.bus_destroy) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001403 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001404 }
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001405 if ((!bus_info->flags.server) /*client */ &&
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001406 busdev_client_notifiers.bus_destroy) {
1407 (*busdev_client_notifiers.bus_destroy) (bus_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001408 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001409 }
1410 break;
1411 }
1412 }
1413 if (notified)
1414 /* The callback function just called above is responsible
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001415 * for calling the appropriate visorchipset_busdev_responders
Ken Cox12e364b2014-03-04 07:58:07 -06001416 * function, which will call bus_responder()
1417 */
1418 ;
1419 else
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001420 bus_responder(cmd, bus_no, response);
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001421 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001422}
1423
1424static void
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001425device_epilog(u32 bus_no, u32 dev_no, struct spar_segment_state state, u32 cmd,
1426 struct controlvm_message_header *msg_hdr, int response,
Jes Sorensenf4c11552015-04-13 10:28:40 -04001427 bool need_response, bool for_visorbus)
Ken Cox12e364b2014-03-04 07:58:07 -06001428{
Jes Sorensene82ba622015-05-05 18:35:45 -04001429 struct visorchipset_busdev_notifiers *notifiers;
Jes Sorensenf4c11552015-04-13 10:28:40 -04001430 bool notified = false;
Ken Cox12e364b2014-03-04 07:58:07 -06001431
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001432 struct visorchipset_device_info *dev_info =
Jes Sorensend480f6a2015-05-05 18:35:54 -04001433 device_find(&dev_info_list, bus_no, dev_no);
Ken Cox12e364b2014-03-04 07:58:07 -06001434 char *envp[] = {
1435 "SPARSP_DIAGPOOL_PAUSED_STATE = 1",
1436 NULL
1437 };
1438
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001439 if (!dev_info)
Ken Cox12e364b2014-03-04 07:58:07 -06001440 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05001441
Ken Cox12e364b2014-03-04 07:58:07 -06001442 if (for_visorbus)
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001443 notifiers = &busdev_server_notifiers;
Ken Cox12e364b2014-03-04 07:58:07 -06001444 else
Benjamin Romer6fe345a2015-03-16 13:58:42 -04001445 notifiers = &busdev_client_notifiers;
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001446 if (need_response) {
1447 memcpy(&dev_info->pending_msg_hdr, msg_hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001448 sizeof(struct controlvm_message_header));
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001449 } else {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001450 dev_info->pending_msg_hdr.id = CONTROLVM_INVALID;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04001451 }
Ken Cox12e364b2014-03-04 07:58:07 -06001452
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001453 down(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001454 if (response >= 0) {
1455 switch (cmd) {
1456 case CONTROLVM_DEVICE_CREATE:
1457 if (notifiers->device_create) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001458 (*notifiers->device_create) (bus_no, dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001459 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001460 }
1461 break;
1462 case CONTROLVM_DEVICE_CHANGESTATE:
1463 /* ServerReady / ServerRunning / SegmentStateRunning */
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001464 if (state.alive == segment_state_running.alive &&
1465 state.operating ==
1466 segment_state_running.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001467 if (notifiers->device_resume) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001468 (*notifiers->device_resume) (bus_no,
1469 dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001470 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001471 }
1472 }
1473 /* ServerNotReady / ServerLost / SegmentStateStandby */
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001474 else if (state.alive == segment_state_standby.alive &&
Benjamin Romer3f833b52014-10-23 14:30:12 -04001475 state.operating ==
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001476 segment_state_standby.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001477 /* technically this is standby case
1478 * where server is lost
1479 */
1480 if (notifiers->device_pause) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001481 (*notifiers->device_pause) (bus_no,
1482 dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001483 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001484 }
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001485 } else if (state.alive == segment_state_paused.alive &&
Benjamin Romer3f833b52014-10-23 14:30:12 -04001486 state.operating ==
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04001487 segment_state_paused.operating) {
Ken Cox12e364b2014-03-04 07:58:07 -06001488 /* this is lite pause where channel is
1489 * still valid just 'pause' of it
1490 */
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001491 if (bus_no == g_diagpool_bus_no &&
1492 dev_no == g_diagpool_dev_no) {
Ken Cox12e364b2014-03-04 07:58:07 -06001493 /* this will trigger the
1494 * diag_shutdown.sh script in
1495 * the visorchipset hotplug */
1496 kobject_uevent_env
Benjamin Romereb34e872015-03-16 13:58:45 -04001497 (&visorchipset_platform_device.dev.
Ken Cox12e364b2014-03-04 07:58:07 -06001498 kobj, KOBJ_ONLINE, envp);
1499 }
1500 }
1501 break;
1502 case CONTROLVM_DEVICE_DESTROY:
1503 if (notifiers->device_destroy) {
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001504 (*notifiers->device_destroy) (bus_no, dev_no);
Jes Sorensenf4c11552015-04-13 10:28:40 -04001505 notified = true;
Ken Cox12e364b2014-03-04 07:58:07 -06001506 }
1507 break;
1508 }
1509 }
1510 if (notified)
1511 /* The callback function just called above is responsible
Benjamin Romer929aa8a2014-10-31 09:57:33 -04001512 * for calling the appropriate visorchipset_busdev_responders
Ken Cox12e364b2014-03-04 07:58:07 -06001513 * function, which will call device_responder()
1514 */
1515 ;
1516 else
Benjamin Romer2836c6a2015-03-16 13:58:17 -04001517 device_responder(cmd, bus_no, dev_no, response);
Benjamin Romer8f1947a2015-03-16 13:57:59 -04001518 up(&notifier_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06001519}
1520
1521static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001522bus_create(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001523{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001524 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001525 u32 bus_no = cmd->create_bus.bus_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001526 int rc = CONTROLVM_RESP_SUCCESS;
Jes Sorensene82ba622015-05-05 18:35:45 -04001527 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001528
Jes Sorensen4f665202015-05-05 18:35:52 -04001529 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001530 if (bus_info && (bus_info->state.created == 1)) {
1531 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001532 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001533 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001534 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001535 }
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001536 bus_info = kzalloc(sizeof(*bus_info), GFP_KERNEL);
1537 if (!bus_info) {
1538 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001539 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001540 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001541 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001542 }
1543
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001544 INIT_LIST_HEAD(&bus_info->entry);
1545 bus_info->bus_no = bus_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001546
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001547 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001548
Benjamin Romer98d7b592014-10-23 14:30:26 -04001549 if (inmsg->hdr.flags.test_message == 1)
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001550 bus_info->chan_info.addr_type = ADDRTYPE_LOCALTEST;
Ken Cox12e364b2014-03-04 07:58:07 -06001551 else
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001552 bus_info->chan_info.addr_type = ADDRTYPE_LOCALPHYSICAL;
Ken Cox12e364b2014-03-04 07:58:07 -06001553
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001554 bus_info->flags.server = inmsg->hdr.flags.server;
1555 bus_info->chan_info.channel_addr = cmd->create_bus.channel_addr;
1556 bus_info->chan_info.n_channel_bytes = cmd->create_bus.channel_bytes;
1557 bus_info->chan_info.channel_type_uuid =
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001558 cmd->create_bus.bus_data_type_uuid;
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001559 bus_info->chan_info.channel_inst_uuid = cmd->create_bus.bus_inst_uuid;
Ken Cox12e364b2014-03-04 07:58:07 -06001560
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001561 list_add(&bus_info->entry, &bus_info_list);
Ken Cox12e364b2014-03-04 07:58:07 -06001562
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001563 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001564
Benjamin Romer6c5fed32015-03-16 13:58:20 -04001565cleanup:
1566 bus_epilog(bus_no, CONTROLVM_BUS_CREATE, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001567 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001568}
1569
1570static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001571bus_destroy(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001572{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001573 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001574 u32 bus_no = cmd->destroy_bus.bus_no;
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001575 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001576 int rc = CONTROLVM_RESP_SUCCESS;
1577
Jes Sorensen4f665202015-05-05 18:35:52 -04001578 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001579 if (!bus_info)
Ken Cox22ad57b2014-03-19 13:06:25 -05001580 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001581 else if (bus_info->state.created == 0)
Ken Cox22ad57b2014-03-19 13:06:25 -05001582 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Ken Cox12e364b2014-03-04 07:58:07 -06001583
Benjamin Romerdff54cd2015-03-16 13:58:21 -04001584 bus_epilog(bus_no, CONTROLVM_BUS_DESTROY, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001585 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001586}
1587
1588static void
Benjamin Romer317d9612015-03-16 13:57:51 -04001589bus_configure(struct controlvm_message *inmsg,
1590 struct parser_context *parser_ctx)
Ken Cox12e364b2014-03-04 07:58:07 -06001591{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001592 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensene82ba622015-05-05 18:35:45 -04001593 u32 bus_no;
1594 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001595 int rc = CONTROLVM_RESP_SUCCESS;
1596 char s[99];
1597
Benjamin Romer654bada2015-03-16 13:58:22 -04001598 bus_no = cmd->configure_bus.bus_no;
1599 POSTCODE_LINUX_3(BUS_CONFIGURE_ENTRY_PC, bus_no,
1600 POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001601
Jes Sorensen4f665202015-05-05 18:35:52 -04001602 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romer654bada2015-03-16 13:58:22 -04001603 if (!bus_info) {
1604 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001605 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001606 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romer654bada2015-03-16 13:58:22 -04001607 } else if (bus_info->state.created == 0) {
1608 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001609 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001610 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romer654bada2015-03-16 13:58:22 -04001611 } else if (bus_info->pending_msg_hdr.id != CONTROLVM_INVALID) {
1612 POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001613 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001614 rc = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
Benjamin Romer654bada2015-03-16 13:58:22 -04001615 } else {
1616 bus_info->partition_handle = cmd->configure_bus.guest_handle;
1617 bus_info->partition_uuid = parser_id_get(parser_ctx);
1618 parser_param_start(parser_ctx, PARSERSTRING_NAME);
1619 bus_info->name = parser_string_get(parser_ctx);
1620
1621 visorchannel_uuid_id(&bus_info->partition_uuid, s);
1622 POSTCODE_LINUX_3(BUS_CONFIGURE_EXIT_PC, bus_no,
1623 POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06001624 }
Benjamin Romer654bada2015-03-16 13:58:22 -04001625 bus_epilog(bus_no, CONTROLVM_BUS_CONFIGURE, &inmsg->hdr,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001626 rc, inmsg->hdr.flags.response_expected == 1);
Ken Cox12e364b2014-03-04 07:58:07 -06001627}
1628
1629static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001630my_device_create(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001631{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001632 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001633 u32 bus_no = cmd->create_device.bus_no;
1634 u32 dev_no = cmd->create_device.dev_no;
Jes Sorensene82ba622015-05-05 18:35:45 -04001635 struct visorchipset_device_info *dev_info;
1636 struct visorchipset_bus_info *bus_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001637 int rc = CONTROLVM_RESP_SUCCESS;
1638
Jes Sorensend480f6a2015-05-05 18:35:54 -04001639 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001640 if (dev_info && (dev_info->state.created == 1)) {
1641 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001642 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001643 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001644 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001645 }
Jes Sorensen4f665202015-05-05 18:35:52 -04001646 bus_info = bus_find(&bus_info_list, bus_no);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001647 if (!bus_info) {
1648 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001649 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001650 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001651 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001652 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001653 if (bus_info->state.created == 0) {
1654 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001655 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001656 rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001657 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001658 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001659 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
1660 if (!dev_info) {
1661 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001662 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001663 rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001664 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001665 }
Andreea-Cristina Bernat97a84f12014-03-14 04:20:06 +02001666
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001667 INIT_LIST_HEAD(&dev_info->entry);
1668 dev_info->bus_no = bus_no;
1669 dev_info->dev_no = dev_no;
1670 dev_info->dev_inst_uuid = cmd->create_device.dev_inst_uuid;
1671 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001672 POSTCODE_SEVERITY_INFO);
1673
Benjamin Romer98d7b592014-10-23 14:30:26 -04001674 if (inmsg->hdr.flags.test_message == 1)
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001675 dev_info->chan_info.addr_type = ADDRTYPE_LOCALTEST;
Ken Cox12e364b2014-03-04 07:58:07 -06001676 else
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001677 dev_info->chan_info.addr_type = ADDRTYPE_LOCALPHYSICAL;
1678 dev_info->chan_info.channel_addr = cmd->create_device.channel_addr;
1679 dev_info->chan_info.n_channel_bytes = cmd->create_device.channel_bytes;
1680 dev_info->chan_info.channel_type_uuid =
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001681 cmd->create_device.data_type_uuid;
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001682 dev_info->chan_info.intr = cmd->create_device.intr;
1683 list_add(&dev_info->entry, &dev_info_list);
1684 POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001685 POSTCODE_SEVERITY_INFO);
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001686cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001687 /* get the bus and devNo for DiagPool channel */
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001688 if (dev_info &&
1689 is_diagpool_channel(dev_info->chan_info.channel_type_uuid)) {
1690 g_diagpool_bus_no = bus_no;
1691 g_diagpool_dev_no = dev_no;
Ken Cox12e364b2014-03-04 07:58:07 -06001692 }
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001693 device_epilog(bus_no, dev_no, segment_state_running,
Ken Cox12e364b2014-03-04 07:58:07 -06001694 CONTROLVM_DEVICE_CREATE, &inmsg->hdr, rc,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001695 inmsg->hdr.flags.response_expected == 1,
Benjamin Romerc60c8e22015-03-16 13:58:23 -04001696 FOR_VISORBUS(dev_info->chan_info.channel_type_uuid));
Ken Cox12e364b2014-03-04 07:58:07 -06001697}
1698
1699static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001700my_device_changestate(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001701{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001702 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001703 u32 bus_no = cmd->device_change_state.bus_no;
1704 u32 dev_no = cmd->device_change_state.dev_no;
Benjamin Romer2ea51172014-10-23 14:30:25 -04001705 struct spar_segment_state state = cmd->device_change_state.state;
Jes Sorensene82ba622015-05-05 18:35:45 -04001706 struct visorchipset_device_info *dev_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001707 int rc = CONTROLVM_RESP_SUCCESS;
1708
Jes Sorensend480f6a2015-05-05 18:35:54 -04001709 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer0278a902015-03-16 13:58:24 -04001710 if (!dev_info) {
1711 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001712 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001713 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Benjamin Romer0278a902015-03-16 13:58:24 -04001714 } else if (dev_info->state.created == 0) {
1715 POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
Ken Cox12e364b2014-03-04 07:58:07 -06001716 POSTCODE_SEVERITY_ERR);
Ken Cox22ad57b2014-03-19 13:06:25 -05001717 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Ken Cox12e364b2014-03-04 07:58:07 -06001718 }
Benjamin Romer0278a902015-03-16 13:58:24 -04001719 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
1720 device_epilog(bus_no, dev_no, state,
1721 CONTROLVM_DEVICE_CHANGESTATE, &inmsg->hdr, rc,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001722 inmsg->hdr.flags.response_expected == 1,
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001723 FOR_VISORBUS(
Benjamin Romer0278a902015-03-16 13:58:24 -04001724 dev_info->chan_info.channel_type_uuid));
Ken Cox12e364b2014-03-04 07:58:07 -06001725}
1726
1727static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04001728my_device_destroy(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06001729{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001730 struct controlvm_message_packet *cmd = &inmsg->cmd;
Jes Sorensen52063ec2015-04-13 10:28:41 -04001731 u32 bus_no = cmd->destroy_device.bus_no;
1732 u32 dev_no = cmd->destroy_device.dev_no;
Jes Sorensene82ba622015-05-05 18:35:45 -04001733 struct visorchipset_device_info *dev_info;
Ken Cox12e364b2014-03-04 07:58:07 -06001734 int rc = CONTROLVM_RESP_SUCCESS;
1735
Jes Sorensend480f6a2015-05-05 18:35:54 -04001736 dev_info = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer61715c82015-03-16 13:58:25 -04001737 if (!dev_info)
Ken Cox22ad57b2014-03-19 13:06:25 -05001738 rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
Benjamin Romer61715c82015-03-16 13:58:25 -04001739 else if (dev_info->state.created == 0)
Ken Cox22ad57b2014-03-19 13:06:25 -05001740 rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
Ken Cox12e364b2014-03-04 07:58:07 -06001741
Benjamin Romer61715c82015-03-16 13:58:25 -04001742 if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
1743 device_epilog(bus_no, dev_no, segment_state_running,
Ken Cox12e364b2014-03-04 07:58:07 -06001744 CONTROLVM_DEVICE_DESTROY, &inmsg->hdr, rc,
Benjamin Romer98d7b592014-10-23 14:30:26 -04001745 inmsg->hdr.flags.response_expected == 1,
Benjamin Romer9b1caee2014-10-31 09:57:23 -04001746 FOR_VISORBUS(
Benjamin Romer61715c82015-03-16 13:58:25 -04001747 dev_info->chan_info.channel_type_uuid));
Ken Cox12e364b2014-03-04 07:58:07 -06001748}
1749
1750/* When provided with the physical address of the controlvm channel
1751 * (phys_addr), the offset to the payload area we need to manage
1752 * (offset), and the size of this payload area (bytes), fills in the
Jes Sorensenf4c11552015-04-13 10:28:40 -04001753 * controlvm_payload_info struct. Returns true for success or false
Ken Cox12e364b2014-03-04 07:58:07 -06001754 * for failure.
1755 */
1756static int
Benjamin Romer5fc02292014-07-31 12:00:51 -04001757initialize_controlvm_payload_info(HOSTADDRESS phys_addr, u64 offset, u32 bytes,
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001758 struct visor_controlvm_payload_info *info)
Ken Cox12e364b2014-03-04 07:58:07 -06001759{
Benjamin Romerc2422332014-07-29 15:09:40 -04001760 u8 __iomem *payload = NULL;
Ken Cox12e364b2014-03-04 07:58:07 -06001761 int rc = CONTROLVM_RESP_SUCCESS;
1762
Benjamin Romer38f736e2015-03-16 13:58:13 -04001763 if (!info) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001764 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
Benjamin Romerf118a392015-03-16 13:58:26 -04001765 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001766 }
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001767 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
Ken Cox12e364b2014-03-04 07:58:07 -06001768 if ((offset == 0) || (bytes == 0)) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001769 rc = -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;
Benjamin Romerf118a392015-03-16 13:58:26 -04001770 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001771 }
1772 payload = ioremap_cache(phys_addr + offset, bytes);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001773 if (!payload) {
Ken Cox22ad57b2014-03-19 13:06:25 -05001774 rc = -CONTROLVM_RESP_ERROR_IOREMAP_FAILED;
Benjamin Romerf118a392015-03-16 13:58:26 -04001775 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06001776 }
1777
1778 info->offset = offset;
1779 info->bytes = bytes;
1780 info->ptr = payload;
Ken Cox12e364b2014-03-04 07:58:07 -06001781
Benjamin Romerf118a392015-03-16 13:58:26 -04001782cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06001783 if (rc < 0) {
Benjamin Romerf118a392015-03-16 13:58:26 -04001784 if (payload) {
Ken Cox12e364b2014-03-04 07:58:07 -06001785 iounmap(payload);
1786 payload = NULL;
1787 }
1788 }
1789 return rc;
1790}
1791
1792static void
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001793destroy_controlvm_payload_info(struct visor_controlvm_payload_info *info)
Ken Cox12e364b2014-03-04 07:58:07 -06001794{
Benjamin Romer597c3382015-03-16 13:58:27 -04001795 if (info->ptr) {
Ken Cox12e364b2014-03-04 07:58:07 -06001796 iounmap(info->ptr);
1797 info->ptr = NULL;
1798 }
Jes Sorensenc1f834e2015-04-13 10:28:39 -04001799 memset(info, 0, sizeof(struct visor_controlvm_payload_info));
Ken Cox12e364b2014-03-04 07:58:07 -06001800}
1801
1802static void
1803initialize_controlvm_payload(void)
1804{
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001805 HOSTADDRESS phys_addr = visorchannel_get_physaddr(controlvm_channel);
Benjamin Romercafefc02015-03-16 13:58:28 -04001806 u64 payload_offset = 0;
1807 u32 payload_bytes = 0;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001808
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001809 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001810 offsetof(struct spar_controlvm_channel_protocol,
1811 request_payload_offset),
Benjamin Romercafefc02015-03-16 13:58:28 -04001812 &payload_offset, sizeof(payload_offset)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001813 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1814 POSTCODE_SEVERITY_ERR);
1815 return;
1816 }
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001817 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04001818 offsetof(struct spar_controlvm_channel_protocol,
1819 request_payload_bytes),
Benjamin Romercafefc02015-03-16 13:58:28 -04001820 &payload_bytes, sizeof(payload_bytes)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06001821 POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
1822 POSTCODE_SEVERITY_ERR);
1823 return;
1824 }
1825 initialize_controlvm_payload_info(phys_addr,
Benjamin Romercafefc02015-03-16 13:58:28 -04001826 payload_offset, payload_bytes,
Benjamin Romer84982fb2015-03-16 13:58:07 -04001827 &controlvm_payload_info);
Ken Cox12e364b2014-03-04 07:58:07 -06001828}
1829
1830/* Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
1831 * Returns CONTROLVM_RESP_xxx code.
1832 */
1833int
1834visorchipset_chipset_ready(void)
1835{
Benjamin Romereb34e872015-03-16 13:58:45 -04001836 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_ONLINE);
Ken Cox12e364b2014-03-04 07:58:07 -06001837 return CONTROLVM_RESP_SUCCESS;
1838}
1839EXPORT_SYMBOL_GPL(visorchipset_chipset_ready);
1840
1841int
1842visorchipset_chipset_selftest(void)
1843{
1844 char env_selftest[20];
1845 char *envp[] = { env_selftest, NULL };
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001846
Ken Cox12e364b2014-03-04 07:58:07 -06001847 sprintf(env_selftest, "SPARSP_SELFTEST=%d", 1);
Benjamin Romereb34e872015-03-16 13:58:45 -04001848 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
Ken Cox12e364b2014-03-04 07:58:07 -06001849 envp);
1850 return CONTROLVM_RESP_SUCCESS;
1851}
1852EXPORT_SYMBOL_GPL(visorchipset_chipset_selftest);
1853
1854/* Send ACTION=offline for DEVPATH=/sys/devices/platform/visorchipset.
1855 * Returns CONTROLVM_RESP_xxx code.
1856 */
1857int
1858visorchipset_chipset_notready(void)
1859{
Benjamin Romereb34e872015-03-16 13:58:45 -04001860 kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_OFFLINE);
Ken Cox12e364b2014-03-04 07:58:07 -06001861 return CONTROLVM_RESP_SUCCESS;
1862}
1863EXPORT_SYMBOL_GPL(visorchipset_chipset_notready);
1864
1865static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001866chipset_ready(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001867{
1868 int rc = visorchipset_chipset_ready();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001869
Ken Cox12e364b2014-03-04 07:58:07 -06001870 if (rc != CONTROLVM_RESP_SUCCESS)
1871 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001872 if (msg_hdr->flags.response_expected && !visorchipset_holdchipsetready)
1873 controlvm_respond(msg_hdr, rc);
1874 if (msg_hdr->flags.response_expected && visorchipset_holdchipsetready) {
Ken Cox12e364b2014-03-04 07:58:07 -06001875 /* Send CHIPSET_READY response when all modules have been loaded
1876 * and disks mounted for the partition
1877 */
Benjamin Romer77a04492015-03-16 13:58:47 -04001878 g_chipset_msg_hdr = *msg_hdr;
Ken Cox12e364b2014-03-04 07:58:07 -06001879 }
1880}
1881
1882static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001883chipset_selftest(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001884{
1885 int rc = visorchipset_chipset_selftest();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001886
Ken Cox12e364b2014-03-04 07:58:07 -06001887 if (rc != CONTROLVM_RESP_SUCCESS)
1888 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001889 if (msg_hdr->flags.response_expected)
1890 controlvm_respond(msg_hdr, rc);
Ken Cox12e364b2014-03-04 07:58:07 -06001891}
1892
1893static void
Benjamin Romer77a04492015-03-16 13:58:47 -04001894chipset_notready(struct controlvm_message_header *msg_hdr)
Ken Cox12e364b2014-03-04 07:58:07 -06001895{
1896 int rc = visorchipset_chipset_notready();
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001897
Ken Cox12e364b2014-03-04 07:58:07 -06001898 if (rc != CONTROLVM_RESP_SUCCESS)
1899 rc = -rc;
Benjamin Romer77a04492015-03-16 13:58:47 -04001900 if (msg_hdr->flags.response_expected)
1901 controlvm_respond(msg_hdr, rc);
Ken Cox12e364b2014-03-04 07:58:07 -06001902}
1903
1904/* This is your "one-stop" shop for grabbing the next message from the
1905 * CONTROLVM_QUEUE_EVENT queue in the controlvm channel.
1906 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04001907static bool
Benjamin Romer3ab47702014-10-23 14:30:31 -04001908read_controlvm_event(struct controlvm_message *msg)
Ken Cox12e364b2014-03-04 07:58:07 -06001909{
Benjamin Romerc3d9a222015-03-16 13:58:05 -04001910 if (visorchannel_signalremove(controlvm_channel,
Ken Cox12e364b2014-03-04 07:58:07 -06001911 CONTROLVM_QUEUE_EVENT, msg)) {
1912 /* got a message */
Benjamin Romer0aca78442015-03-04 12:14:25 -05001913 if (msg->hdr.flags.test_message == 1)
Jes Sorensenf4c11552015-04-13 10:28:40 -04001914 return false;
1915 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06001916 }
Jes Sorensenf4c11552015-04-13 10:28:40 -04001917 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06001918}
1919
1920/*
1921 * The general parahotplug flow works as follows. The visorchipset
1922 * driver receives a DEVICE_CHANGESTATE message from Command
1923 * specifying a physical device to enable or disable. The CONTROLVM
1924 * message handler calls parahotplug_process_message, which then adds
1925 * the message to a global list and kicks off a udev event which
1926 * causes a user level script to enable or disable the specified
1927 * device. The udev script then writes to
1928 * /proc/visorchipset/parahotplug, which causes parahotplug_proc_write
1929 * to get called, at which point the appropriate CONTROLVM message is
1930 * retrieved from the list and responded to.
1931 */
1932
1933#define PARAHOTPLUG_TIMEOUT_MS 2000
1934
1935/*
1936 * Generate unique int to match an outstanding CONTROLVM message with a
1937 * udev script /proc response
1938 */
1939static int
1940parahotplug_next_id(void)
1941{
1942 static atomic_t id = ATOMIC_INIT(0);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04001943
Ken Cox12e364b2014-03-04 07:58:07 -06001944 return atomic_inc_return(&id);
1945}
1946
1947/*
1948 * Returns the time (in jiffies) when a CONTROLVM message on the list
1949 * should expire -- PARAHOTPLUG_TIMEOUT_MS in the future
1950 */
1951static unsigned long
1952parahotplug_next_expiration(void)
1953{
Nicholas Mc Guire2cc1a1b2015-01-31 12:02:08 +01001954 return jiffies + msecs_to_jiffies(PARAHOTPLUG_TIMEOUT_MS);
Ken Cox12e364b2014-03-04 07:58:07 -06001955}
1956
1957/*
1958 * Create a parahotplug_request, which is basically a wrapper for a
1959 * CONTROLVM_MESSAGE that we can stick on a list
1960 */
1961static struct parahotplug_request *
Benjamin Romer3ab47702014-10-23 14:30:31 -04001962parahotplug_request_create(struct controlvm_message *msg)
Ken Cox12e364b2014-03-04 07:58:07 -06001963{
Quentin Lambertea0dcfc2015-02-10 15:12:07 +01001964 struct parahotplug_request *req;
1965
Benjamin Romer6a55e3c2015-03-16 13:58:30 -04001966 req = kmalloc(sizeof(*req), GFP_KERNEL | __GFP_NORETRY);
Benjamin Romer38f736e2015-03-16 13:58:13 -04001967 if (!req)
Ken Cox12e364b2014-03-04 07:58:07 -06001968 return NULL;
1969
1970 req->id = parahotplug_next_id();
1971 req->expiration = parahotplug_next_expiration();
1972 req->msg = *msg;
1973
1974 return req;
1975}
1976
1977/*
1978 * Free a parahotplug_request.
1979 */
1980static void
1981parahotplug_request_destroy(struct parahotplug_request *req)
1982{
1983 kfree(req);
1984}
1985
1986/*
1987 * Cause uevent to run the user level script to do the disable/enable
1988 * specified in (the CONTROLVM message in) the specified
1989 * parahotplug_request
1990 */
1991static void
1992parahotplug_request_kickoff(struct parahotplug_request *req)
1993{
Benjamin Romer2ea51172014-10-23 14:30:25 -04001994 struct controlvm_message_packet *cmd = &req->msg.cmd;
Ken Cox12e364b2014-03-04 07:58:07 -06001995 char env_cmd[40], env_id[40], env_state[40], env_bus[40], env_dev[40],
1996 env_func[40];
1997 char *envp[] = {
1998 env_cmd, env_id, env_state, env_bus, env_dev, env_func, NULL
1999 };
2000
2001 sprintf(env_cmd, "SPAR_PARAHOTPLUG=1");
2002 sprintf(env_id, "SPAR_PARAHOTPLUG_ID=%d", req->id);
2003 sprintf(env_state, "SPAR_PARAHOTPLUG_STATE=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04002004 cmd->device_change_state.state.active);
Ken Cox12e364b2014-03-04 07:58:07 -06002005 sprintf(env_bus, "SPAR_PARAHOTPLUG_BUS=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04002006 cmd->device_change_state.bus_no);
Ken Cox12e364b2014-03-04 07:58:07 -06002007 sprintf(env_dev, "SPAR_PARAHOTPLUG_DEVICE=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04002008 cmd->device_change_state.dev_no >> 3);
Ken Cox12e364b2014-03-04 07:58:07 -06002009 sprintf(env_func, "SPAR_PARAHOTPLUG_FUNCTION=%d",
Benjamin Romer2ea51172014-10-23 14:30:25 -04002010 cmd->device_change_state.dev_no & 0x7);
Ken Cox12e364b2014-03-04 07:58:07 -06002011
Benjamin Romereb34e872015-03-16 13:58:45 -04002012 kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
Ken Cox12e364b2014-03-04 07:58:07 -06002013 envp);
2014}
2015
2016/*
2017 * Remove any request from the list that's been on there too long and
2018 * respond with an error.
2019 */
2020static void
2021parahotplug_process_list(void)
2022{
Jes Sorensene82ba622015-05-05 18:35:45 -04002023 struct list_head *pos;
2024 struct list_head *tmp;
Ken Cox12e364b2014-03-04 07:58:07 -06002025
Benjamin Romerddf5de52015-03-16 13:58:41 -04002026 spin_lock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06002027
Benjamin Romerddf5de52015-03-16 13:58:41 -04002028 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
Ken Cox12e364b2014-03-04 07:58:07 -06002029 struct parahotplug_request *req =
2030 list_entry(pos, struct parahotplug_request, list);
Benjamin Romer55b33412015-03-16 13:58:29 -04002031
2032 if (!time_after_eq(jiffies, req->expiration))
2033 continue;
2034
2035 list_del(pos);
2036 if (req->msg.hdr.flags.response_expected)
2037 controlvm_respond_physdev_changestate(
2038 &req->msg.hdr,
2039 CONTROLVM_RESP_ERROR_DEVICE_UDEV_TIMEOUT,
2040 req->msg.cmd.device_change_state.state);
2041 parahotplug_request_destroy(req);
Ken Cox12e364b2014-03-04 07:58:07 -06002042 }
2043
Benjamin Romerddf5de52015-03-16 13:58:41 -04002044 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06002045}
2046
2047/*
2048 * Called from the /proc handler, which means the user script has
2049 * finished the enable/disable. Find the matching identifier, and
2050 * respond to the CONTROLVM message with success.
2051 */
2052static int
Benjamin Romerb06bdf72014-07-31 12:00:49 -04002053parahotplug_request_complete(int id, u16 active)
Ken Cox12e364b2014-03-04 07:58:07 -06002054{
Jes Sorensene82ba622015-05-05 18:35:45 -04002055 struct list_head *pos;
2056 struct list_head *tmp;
Ken Cox12e364b2014-03-04 07:58:07 -06002057
Benjamin Romerddf5de52015-03-16 13:58:41 -04002058 spin_lock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06002059
2060 /* Look for a request matching "id". */
Benjamin Romerddf5de52015-03-16 13:58:41 -04002061 list_for_each_safe(pos, tmp, &parahotplug_request_list) {
Ken Cox12e364b2014-03-04 07:58:07 -06002062 struct parahotplug_request *req =
2063 list_entry(pos, struct parahotplug_request, list);
2064 if (req->id == id) {
2065 /* Found a match. Remove it from the list and
2066 * respond.
2067 */
2068 list_del(pos);
Benjamin Romerddf5de52015-03-16 13:58:41 -04002069 spin_unlock(&parahotplug_request_list_lock);
Benjamin Romer2ea51172014-10-23 14:30:25 -04002070 req->msg.cmd.device_change_state.state.active = active;
Benjamin Romer98d7b592014-10-23 14:30:26 -04002071 if (req->msg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06002072 controlvm_respond_physdev_changestate(
2073 &req->msg.hdr, CONTROLVM_RESP_SUCCESS,
Benjamin Romer2ea51172014-10-23 14:30:25 -04002074 req->msg.cmd.device_change_state.state);
Ken Cox12e364b2014-03-04 07:58:07 -06002075 parahotplug_request_destroy(req);
2076 return 0;
2077 }
2078 }
2079
Benjamin Romerddf5de52015-03-16 13:58:41 -04002080 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06002081 return -1;
2082}
2083
2084/*
2085 * Enables or disables a PCI device by kicking off a udev script
2086 */
Ken Coxbd5b9b32014-03-13 15:39:22 -05002087static void
Benjamin Romer3ab47702014-10-23 14:30:31 -04002088parahotplug_process_message(struct controlvm_message *inmsg)
Ken Cox12e364b2014-03-04 07:58:07 -06002089{
2090 struct parahotplug_request *req;
2091
2092 req = parahotplug_request_create(inmsg);
2093
Benjamin Romer38f736e2015-03-16 13:58:13 -04002094 if (!req)
Ken Cox12e364b2014-03-04 07:58:07 -06002095 return;
Ken Cox12e364b2014-03-04 07:58:07 -06002096
Benjamin Romer2ea51172014-10-23 14:30:25 -04002097 if (inmsg->cmd.device_change_state.state.active) {
Ken Cox12e364b2014-03-04 07:58:07 -06002098 /* For enable messages, just respond with success
2099 * right away. This is a bit of a hack, but there are
2100 * issues with the early enable messages we get (with
2101 * either the udev script not detecting that the device
2102 * is up, or not getting called at all). Fortunately
2103 * the messages that get lost don't matter anyway, as
2104 * devices are automatically enabled at
2105 * initialization.
2106 */
2107 parahotplug_request_kickoff(req);
2108 controlvm_respond_physdev_changestate(&inmsg->hdr,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002109 CONTROLVM_RESP_SUCCESS,
2110 inmsg->cmd.device_change_state.state);
Ken Cox12e364b2014-03-04 07:58:07 -06002111 parahotplug_request_destroy(req);
2112 } else {
2113 /* For disable messages, add the request to the
2114 * request list before kicking off the udev script. It
2115 * won't get responded to until the script has
2116 * indicated it's done.
2117 */
Benjamin Romerddf5de52015-03-16 13:58:41 -04002118 spin_lock(&parahotplug_request_list_lock);
2119 list_add_tail(&req->list, &parahotplug_request_list);
2120 spin_unlock(&parahotplug_request_list_lock);
Ken Cox12e364b2014-03-04 07:58:07 -06002121
2122 parahotplug_request_kickoff(req);
2123 }
2124}
2125
Ken Cox12e364b2014-03-04 07:58:07 -06002126/* Process a controlvm message.
2127 * Return result:
Jes Sorensenf4c11552015-04-13 10:28:40 -04002128 * false - this function will return FALSE only in the case where the
Ken Cox12e364b2014-03-04 07:58:07 -06002129 * controlvm message was NOT processed, but processing must be
2130 * retried before reading the next controlvm message; a
2131 * scenario where this can occur is when we need to throttle
2132 * the allocation of memory in which to copy out controlvm
2133 * payload data
Jes Sorensenf4c11552015-04-13 10:28:40 -04002134 * true - processing of the controlvm message completed,
Ken Cox12e364b2014-03-04 07:58:07 -06002135 * either successfully or with an error.
2136 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04002137static bool
Benjamin Romer3ab47702014-10-23 14:30:31 -04002138handle_command(struct controlvm_message inmsg, HOSTADDRESS channel_addr)
Ken Cox12e364b2014-03-04 07:58:07 -06002139{
Benjamin Romer2ea51172014-10-23 14:30:25 -04002140 struct controlvm_message_packet *cmd = &inmsg.cmd;
Jes Sorensene82ba622015-05-05 18:35:45 -04002141 u64 parm_addr;
2142 u32 parm_bytes;
Benjamin Romer317d9612015-03-16 13:57:51 -04002143 struct parser_context *parser_ctx = NULL;
Jes Sorensene82ba622015-05-05 18:35:45 -04002144 bool local_addr;
Benjamin Romer3ab47702014-10-23 14:30:31 -04002145 struct controlvm_message ackmsg;
Ken Cox12e364b2014-03-04 07:58:07 -06002146
2147 /* create parsing context if necessary */
Benjamin Romer818352a2015-03-16 13:58:31 -04002148 local_addr = (inmsg.hdr.flags.test_message == 1);
Benjamin Romer0aca78442015-03-04 12:14:25 -05002149 if (channel_addr == 0)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002150 return true;
Benjamin Romer818352a2015-03-16 13:58:31 -04002151 parm_addr = channel_addr + inmsg.hdr.payload_vm_offset;
2152 parm_bytes = inmsg.hdr.payload_bytes;
Ken Cox12e364b2014-03-04 07:58:07 -06002153
2154 /* Parameter and channel addresses within test messages actually lie
2155 * within our OS-controlled memory. We need to know that, because it
2156 * makes a difference in how we compute the virtual address.
2157 */
Jes Sorensenebec8962015-05-05 18:35:57 -04002158 if (parm_addr && parm_bytes) {
Jes Sorensenf4c11552015-04-13 10:28:40 -04002159 bool retry = false;
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002160
Ken Cox12e364b2014-03-04 07:58:07 -06002161 parser_ctx =
Benjamin Romer818352a2015-03-16 13:58:31 -04002162 parser_init_byte_stream(parm_addr, parm_bytes,
2163 local_addr, &retry);
Benjamin Romer1b088722015-03-04 12:14:26 -05002164 if (!parser_ctx && retry)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002165 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002166 }
2167
Benjamin Romer818352a2015-03-16 13:58:31 -04002168 if (!local_addr) {
Ken Cox12e364b2014-03-04 07:58:07 -06002169 controlvm_init_response(&ackmsg, &inmsg.hdr,
2170 CONTROLVM_RESP_SUCCESS);
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002171 if (controlvm_channel)
2172 visorchannel_signalinsert(controlvm_channel,
Benjamin Romer1b088722015-03-04 12:14:26 -05002173 CONTROLVM_QUEUE_ACK,
2174 &ackmsg);
Ken Cox12e364b2014-03-04 07:58:07 -06002175 }
Benjamin Romer98d7b592014-10-23 14:30:26 -04002176 switch (inmsg.hdr.id) {
Ken Cox12e364b2014-03-04 07:58:07 -06002177 case CONTROLVM_CHIPSET_INIT:
Ken Cox12e364b2014-03-04 07:58:07 -06002178 chipset_init(&inmsg);
2179 break;
2180 case CONTROLVM_BUS_CREATE:
Ken Cox12e364b2014-03-04 07:58:07 -06002181 bus_create(&inmsg);
2182 break;
2183 case CONTROLVM_BUS_DESTROY:
Ken Cox12e364b2014-03-04 07:58:07 -06002184 bus_destroy(&inmsg);
2185 break;
2186 case CONTROLVM_BUS_CONFIGURE:
Ken Cox12e364b2014-03-04 07:58:07 -06002187 bus_configure(&inmsg, parser_ctx);
2188 break;
2189 case CONTROLVM_DEVICE_CREATE:
Ken Cox12e364b2014-03-04 07:58:07 -06002190 my_device_create(&inmsg);
2191 break;
2192 case CONTROLVM_DEVICE_CHANGESTATE:
Benjamin Romer2ea51172014-10-23 14:30:25 -04002193 if (cmd->device_change_state.flags.phys_device) {
Ken Cox12e364b2014-03-04 07:58:07 -06002194 parahotplug_process_message(&inmsg);
2195 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002196 /* save the hdr and cmd structures for later use */
2197 /* when sending back the response to Command */
2198 my_device_changestate(&inmsg);
Benjamin Romer4f44b722015-03-16 13:58:02 -04002199 g_devicechangestate_packet = inmsg.cmd;
Ken Cox12e364b2014-03-04 07:58:07 -06002200 break;
2201 }
2202 break;
2203 case CONTROLVM_DEVICE_DESTROY:
Ken Cox12e364b2014-03-04 07:58:07 -06002204 my_device_destroy(&inmsg);
2205 break;
2206 case CONTROLVM_DEVICE_CONFIGURE:
Ken Cox12e364b2014-03-04 07:58:07 -06002207 /* no op for now, just send a respond that we passed */
Benjamin Romer98d7b592014-10-23 14:30:26 -04002208 if (inmsg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06002209 controlvm_respond(&inmsg.hdr, CONTROLVM_RESP_SUCCESS);
2210 break;
2211 case CONTROLVM_CHIPSET_READY:
Ken Cox12e364b2014-03-04 07:58:07 -06002212 chipset_ready(&inmsg.hdr);
2213 break;
2214 case CONTROLVM_CHIPSET_SELFTEST:
Ken Cox12e364b2014-03-04 07:58:07 -06002215 chipset_selftest(&inmsg.hdr);
2216 break;
2217 case CONTROLVM_CHIPSET_STOP:
Ken Cox12e364b2014-03-04 07:58:07 -06002218 chipset_notready(&inmsg.hdr);
2219 break;
2220 default:
Benjamin Romer98d7b592014-10-23 14:30:26 -04002221 if (inmsg.hdr.flags.response_expected)
Ken Cox12e364b2014-03-04 07:58:07 -06002222 controlvm_respond(&inmsg.hdr,
Benjamin Romer818352a2015-03-16 13:58:31 -04002223 -CONTROLVM_RESP_ERROR_MESSAGE_ID_UNKNOWN);
Ken Cox12e364b2014-03-04 07:58:07 -06002224 break;
2225 }
2226
Benjamin Romer38f736e2015-03-16 13:58:13 -04002227 if (parser_ctx) {
Ken Cox12e364b2014-03-04 07:58:07 -06002228 parser_done(parser_ctx);
2229 parser_ctx = NULL;
2230 }
Jes Sorensenf4c11552015-04-13 10:28:40 -04002231 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002232}
2233
Vincent Bernatd746cb52014-08-01 10:29:30 +02002234static HOSTADDRESS controlvm_get_channel_address(void)
Benjamin Romer524b0b62014-07-17 12:39:57 -04002235{
Benjamin Romer5fc02292014-07-31 12:00:51 -04002236 u64 addr = 0;
Benjamin Romerb3c55b12014-07-31 12:00:50 -04002237 u32 size = 0;
Benjamin Romer524b0b62014-07-17 12:39:57 -04002238
Benjamin Romer0aca78442015-03-04 12:14:25 -05002239 if (!VMCALL_SUCCESSFUL(issue_vmcall_io_controlvm_addr(&addr, &size)))
Benjamin Romer524b0b62014-07-17 12:39:57 -04002240 return 0;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002241
Benjamin Romer524b0b62014-07-17 12:39:57 -04002242 return addr;
2243}
2244
Ken Cox12e364b2014-03-04 07:58:07 -06002245static void
2246controlvm_periodic_work(struct work_struct *work)
2247{
Benjamin Romer3ab47702014-10-23 14:30:31 -04002248 struct controlvm_message inmsg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002249 bool got_command = false;
2250 bool handle_command_failed = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002251 static u64 poll_count;
Ken Cox12e364b2014-03-04 07:58:07 -06002252
2253 /* make sure visorbus server is registered for controlvm callbacks */
2254 if (visorchipset_serverregwait && !serverregistered)
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002255 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002256 /* make sure visorclientbus server is regsitered for controlvm
2257 * callbacks
2258 */
2259 if (visorchipset_clientregwait && !clientregistered)
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002260 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002261
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002262 poll_count++;
2263 if (poll_count >= 250)
Ken Cox12e364b2014-03-04 07:58:07 -06002264 ; /* keep going */
2265 else
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002266 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002267
2268 /* Check events to determine if response to CHIPSET_READY
2269 * should be sent
2270 */
Benjamin Romer0639ba62015-03-16 13:58:04 -04002271 if (visorchipset_holdchipsetready &&
2272 (g_chipset_msg_hdr.id != CONTROLVM_INVALID)) {
Ken Cox12e364b2014-03-04 07:58:07 -06002273 if (check_chipset_events() == 1) {
Benjamin Romerda021f02015-03-16 13:57:58 -04002274 controlvm_respond(&g_chipset_msg_hdr, 0);
Ken Cox12e364b2014-03-04 07:58:07 -06002275 clear_chipset_events();
Benjamin Romerda021f02015-03-16 13:57:58 -04002276 memset(&g_chipset_msg_hdr, 0,
Benjamin Romer98d7b592014-10-23 14:30:26 -04002277 sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002278 }
2279 }
2280
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002281 while (visorchannel_signalremove(controlvm_channel,
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002282 CONTROLVM_QUEUE_RESPONSE,
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002283 &inmsg))
2284 ;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002285 if (!got_command) {
Benjamin Romer7166ed12015-03-16 13:58:38 -04002286 if (controlvm_pending_msg_valid) {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002287 /* we throttled processing of a prior
2288 * msg, so try to process it again
2289 * rather than reading a new one
2290 */
Benjamin Romer7166ed12015-03-16 13:58:38 -04002291 inmsg = controlvm_pending_msg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002292 controlvm_pending_msg_valid = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002293 got_command = true;
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002294 } else {
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002295 got_command = read_controlvm_event(&inmsg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002296 }
Ken Cox12e364b2014-03-04 07:58:07 -06002297 }
2298
Jes Sorensenf4c11552015-04-13 10:28:40 -04002299 handle_command_failed = false;
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002300 while (got_command && (!handle_command_failed)) {
Benjamin Romerb53e0e92015-03-16 13:57:56 -04002301 most_recent_message_jiffies = jiffies;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002302 if (handle_command(inmsg,
2303 visorchannel_get_physaddr
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002304 (controlvm_channel)))
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002305 got_command = read_controlvm_event(&inmsg);
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002306 else {
2307 /* this is a scenario where throttling
2308 * is required, but probably NOT an
2309 * error...; we stash the current
2310 * controlvm msg so we will attempt to
2311 * reprocess it on our next loop
2312 */
Jes Sorensenf4c11552015-04-13 10:28:40 -04002313 handle_command_failed = true;
Benjamin Romer7166ed12015-03-16 13:58:38 -04002314 controlvm_pending_msg = inmsg;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002315 controlvm_pending_msg_valid = true;
Ken Cox12e364b2014-03-04 07:58:07 -06002316 }
2317 }
2318
2319 /* parahotplug_worker */
2320 parahotplug_process_list();
2321
Benjamin Romer1c1ed292015-03-16 13:58:32 -04002322cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002323
2324 if (time_after(jiffies,
Benjamin Romerb53e0e92015-03-16 13:57:56 -04002325 most_recent_message_jiffies + (HZ * MIN_IDLE_SECONDS))) {
Ken Cox12e364b2014-03-04 07:58:07 -06002326 /* it's been longer than MIN_IDLE_SECONDS since we
2327 * processed our last controlvm message; slow down the
2328 * polling
2329 */
Benjamin Romer911e2132015-03-16 13:57:47 -04002330 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_SLOW)
2331 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
Ken Cox12e364b2014-03-04 07:58:07 -06002332 } else {
Benjamin Romer911e2132015-03-16 13:57:47 -04002333 if (poll_jiffies != POLLJIFFIES_CONTROLVMCHANNEL_FAST)
2334 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
Ken Cox12e364b2014-03-04 07:58:07 -06002335 }
2336
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002337 queue_delayed_work(periodic_controlvm_workqueue,
2338 &periodic_controlvm_work, poll_jiffies);
Ken Cox12e364b2014-03-04 07:58:07 -06002339}
2340
2341static void
2342setup_crash_devices_work_queue(struct work_struct *work)
2343{
Benjamin Romere6bdb902015-03-16 13:58:33 -04002344 struct controlvm_message local_crash_bus_msg;
2345 struct controlvm_message local_crash_dev_msg;
Benjamin Romer3ab47702014-10-23 14:30:31 -04002346 struct controlvm_message msg;
Benjamin Romere6bdb902015-03-16 13:58:33 -04002347 u32 local_crash_msg_offset;
2348 u16 local_crash_msg_count;
Ken Cox12e364b2014-03-04 07:58:07 -06002349
2350 /* make sure visorbus server is registered for controlvm callbacks */
2351 if (visorchipset_serverregwait && !serverregistered)
Benjamin Romere6bdb902015-03-16 13:58:33 -04002352 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002353
2354 /* make sure visorclientbus server is regsitered for controlvm
2355 * callbacks
2356 */
2357 if (visorchipset_clientregwait && !clientregistered)
Benjamin Romere6bdb902015-03-16 13:58:33 -04002358 goto cleanup;
Ken Cox12e364b2014-03-04 07:58:07 -06002359
2360 POSTCODE_LINUX_2(CRASH_DEV_ENTRY_PC, POSTCODE_SEVERITY_INFO);
2361
2362 /* send init chipset msg */
Benjamin Romer98d7b592014-10-23 14:30:26 -04002363 msg.hdr.id = CONTROLVM_CHIPSET_INIT;
Benjamin Romer2ea51172014-10-23 14:30:25 -04002364 msg.cmd.init_chipset.bus_count = 23;
2365 msg.cmd.init_chipset.switch_count = 0;
Ken Cox12e364b2014-03-04 07:58:07 -06002366
2367 chipset_init(&msg);
2368
Ken Cox12e364b2014-03-04 07:58:07 -06002369 /* get saved message count */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002370 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002371 offsetof(struct spar_controlvm_channel_protocol,
2372 saved_crash_message_count),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002373 &local_crash_msg_count, sizeof(u16)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002374 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2375 POSTCODE_SEVERITY_ERR);
2376 return;
2377 }
2378
Benjamin Romere6bdb902015-03-16 13:58:33 -04002379 if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
Ken Cox12e364b2014-03-04 07:58:07 -06002380 POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002381 local_crash_msg_count,
Ken Cox12e364b2014-03-04 07:58:07 -06002382 POSTCODE_SEVERITY_ERR);
2383 return;
2384 }
2385
2386 /* get saved crash message offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002387 if (visorchannel_read(controlvm_channel,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002388 offsetof(struct spar_controlvm_channel_protocol,
2389 saved_crash_message_offset),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002390 &local_crash_msg_offset, sizeof(u32)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002391 POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
2392 POSTCODE_SEVERITY_ERR);
2393 return;
2394 }
2395
2396 /* read create device message for storage bus offset */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002397 if (visorchannel_read(controlvm_channel,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002398 local_crash_msg_offset,
2399 &local_crash_bus_msg,
Benjamin Romer3ab47702014-10-23 14:30:31 -04002400 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002401 POSTCODE_LINUX_2(CRASH_DEV_RD_BUS_FAIULRE_PC,
2402 POSTCODE_SEVERITY_ERR);
2403 return;
2404 }
2405
2406 /* read create device message for storage device */
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002407 if (visorchannel_read(controlvm_channel,
Benjamin Romere6bdb902015-03-16 13:58:33 -04002408 local_crash_msg_offset +
Benjamin Romer3ab47702014-10-23 14:30:31 -04002409 sizeof(struct controlvm_message),
Benjamin Romere6bdb902015-03-16 13:58:33 -04002410 &local_crash_dev_msg,
Benjamin Romer3ab47702014-10-23 14:30:31 -04002411 sizeof(struct controlvm_message)) < 0) {
Ken Cox12e364b2014-03-04 07:58:07 -06002412 POSTCODE_LINUX_2(CRASH_DEV_RD_DEV_FAIULRE_PC,
2413 POSTCODE_SEVERITY_ERR);
2414 return;
2415 }
2416
2417 /* reuse IOVM create bus message */
Jes Sorensenebec8962015-05-05 18:35:57 -04002418 if (local_crash_bus_msg.cmd.create_bus.channel_addr) {
Benjamin Romere6bdb902015-03-16 13:58:33 -04002419 bus_create(&local_crash_bus_msg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002420 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002421 POSTCODE_LINUX_2(CRASH_DEV_BUS_NULL_FAILURE_PC,
2422 POSTCODE_SEVERITY_ERR);
2423 return;
2424 }
2425
2426 /* reuse create device message for storage device */
Jes Sorensenebec8962015-05-05 18:35:57 -04002427 if (local_crash_dev_msg.cmd.create_device.channel_addr) {
Benjamin Romere6bdb902015-03-16 13:58:33 -04002428 my_device_create(&local_crash_dev_msg);
Benjamin Romer75c1f8b2015-03-16 13:58:19 -04002429 } else {
Ken Cox12e364b2014-03-04 07:58:07 -06002430 POSTCODE_LINUX_2(CRASH_DEV_DEV_NULL_FAILURE_PC,
2431 POSTCODE_SEVERITY_ERR);
2432 return;
2433 }
Ken Cox12e364b2014-03-04 07:58:07 -06002434 POSTCODE_LINUX_2(CRASH_DEV_EXIT_PC, POSTCODE_SEVERITY_INFO);
2435 return;
2436
Benjamin Romere6bdb902015-03-16 13:58:33 -04002437cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002438
Benjamin Romer911e2132015-03-16 13:57:47 -04002439 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_SLOW;
Ken Cox12e364b2014-03-04 07:58:07 -06002440
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002441 queue_delayed_work(periodic_controlvm_workqueue,
2442 &periodic_controlvm_work, poll_jiffies);
Ken Cox12e364b2014-03-04 07:58:07 -06002443}
2444
2445static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002446bus_create_response(u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002447{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002448 bus_responder(CONTROLVM_BUS_CREATE, bus_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002449}
2450
2451static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002452bus_destroy_response(u32 bus_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002453{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002454 bus_responder(CONTROLVM_BUS_DESTROY, bus_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002455}
2456
2457static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002458device_create_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002459{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002460 device_responder(CONTROLVM_DEVICE_CREATE, bus_no, dev_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002461}
2462
2463static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002464device_destroy_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002465{
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002466 device_responder(CONTROLVM_DEVICE_DESTROY, bus_no, dev_no, response);
Ken Cox12e364b2014-03-04 07:58:07 -06002467}
2468
2469void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002470visorchipset_device_pause_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002471{
Ken Cox12e364b2014-03-04 07:58:07 -06002472 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
Benjamin Romer8420f412014-10-31 09:57:36 -04002473 bus_no, dev_no, response,
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04002474 segment_state_standby);
Ken Cox12e364b2014-03-04 07:58:07 -06002475}
Ken Cox927c7922014-03-05 14:52:25 -06002476EXPORT_SYMBOL_GPL(visorchipset_device_pause_response);
Ken Cox12e364b2014-03-04 07:58:07 -06002477
2478static void
Jes Sorensen52063ec2015-04-13 10:28:41 -04002479device_resume_response(u32 bus_no, u32 dev_no, int response)
Ken Cox12e364b2014-03-04 07:58:07 -06002480{
2481 device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
Benjamin Romer8e3fedd2015-03-16 13:58:43 -04002482 bus_no, dev_no, response,
Benjamin Romerbd0d2dc2014-10-23 14:30:13 -04002483 segment_state_running);
Ken Cox12e364b2014-03-04 07:58:07 -06002484}
2485
Jes Sorensenf4c11552015-04-13 10:28:40 -04002486bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002487visorchipset_get_bus_info(u32 bus_no, struct visorchipset_bus_info *bus_info)
Ken Cox12e364b2014-03-04 07:58:07 -06002488{
Jes Sorensen4f665202015-05-05 18:35:52 -04002489 void *p = bus_find(&bus_info_list, bus_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002490
Benjamin Romer0aca78442015-03-04 12:14:25 -05002491 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002492 return false;
Benjamin Romer77db7122014-10-31 09:57:37 -04002493 memcpy(bus_info, p, sizeof(struct visorchipset_bus_info));
Jes Sorensenf4c11552015-04-13 10:28:40 -04002494 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002495}
2496EXPORT_SYMBOL_GPL(visorchipset_get_bus_info);
2497
Jes Sorensenf4c11552015-04-13 10:28:40 -04002498bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002499visorchipset_set_bus_context(u32 bus_no, void *context)
Ken Cox12e364b2014-03-04 07:58:07 -06002500{
Jes Sorensen4f665202015-05-05 18:35:52 -04002501 struct visorchipset_bus_info *p = bus_find(&bus_info_list, bus_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002502
Benjamin Romer0aca78442015-03-04 12:14:25 -05002503 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002504 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002505 p->bus_driver_context = context;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002506 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002507}
2508EXPORT_SYMBOL_GPL(visorchipset_set_bus_context);
2509
Jes Sorensenf4c11552015-04-13 10:28:40 -04002510bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002511visorchipset_get_device_info(u32 bus_no, u32 dev_no,
Benjamin Romerb486df12014-10-31 09:57:38 -04002512 struct visorchipset_device_info *dev_info)
Ken Cox12e364b2014-03-04 07:58:07 -06002513{
Jes Sorensend480f6a2015-05-05 18:35:54 -04002514 void *p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002515
Benjamin Romer0aca78442015-03-04 12:14:25 -05002516 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002517 return false;
Benjamin Romerb486df12014-10-31 09:57:38 -04002518 memcpy(dev_info, p, sizeof(struct visorchipset_device_info));
Jes Sorensenf4c11552015-04-13 10:28:40 -04002519 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002520}
2521EXPORT_SYMBOL_GPL(visorchipset_get_device_info);
2522
Jes Sorensenf4c11552015-04-13 10:28:40 -04002523bool
Jes Sorensen52063ec2015-04-13 10:28:41 -04002524visorchipset_set_device_context(u32 bus_no, u32 dev_no, void *context)
Ken Cox12e364b2014-03-04 07:58:07 -06002525{
Jes Sorensend480f6a2015-05-05 18:35:54 -04002526 struct visorchipset_device_info *p;
2527
2528 p = device_find(&dev_info_list, bus_no, dev_no);
Benjamin Romer26eb2c02014-08-18 09:34:53 -04002529
Benjamin Romer0aca78442015-03-04 12:14:25 -05002530 if (!p)
Jes Sorensenf4c11552015-04-13 10:28:40 -04002531 return false;
Ken Cox12e364b2014-03-04 07:58:07 -06002532 p->bus_driver_context = context;
Jes Sorensenf4c11552015-04-13 10:28:40 -04002533 return true;
Ken Cox12e364b2014-03-04 07:58:07 -06002534}
2535EXPORT_SYMBOL_GPL(visorchipset_set_device_context);
2536
2537/* Generic wrapper function for allocating memory from a kmem_cache pool.
2538 */
2539void *
Jes Sorensenf4c11552015-04-13 10:28:40 -04002540visorchipset_cache_alloc(struct kmem_cache *pool, bool ok_to_block,
Ken Cox12e364b2014-03-04 07:58:07 -06002541 char *fn, int ln)
2542{
2543 gfp_t gfp;
2544 void *p;
2545
2546 if (ok_to_block)
2547 gfp = GFP_KERNEL;
2548 else
2549 gfp = GFP_ATOMIC;
2550 /* __GFP_NORETRY means "ok to fail", meaning
2551 * kmem_cache_alloc() can return NULL, implying the caller CAN
2552 * cope with failure. If you do NOT specify __GFP_NORETRY,
2553 * Linux will go to extreme measures to get memory for you
2554 * (like, invoke oom killer), which will probably cripple the
2555 * system.
2556 */
2557 gfp |= __GFP_NORETRY;
2558 p = kmem_cache_alloc(pool, gfp);
Benjamin Romer0aca78442015-03-04 12:14:25 -05002559 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06002560 return NULL;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002561
Ken Cox12e364b2014-03-04 07:58:07 -06002562 return p;
2563}
2564
2565/* Generic wrapper function for freeing memory from a kmem_cache pool.
2566 */
2567void
2568visorchipset_cache_free(struct kmem_cache *pool, void *p, char *fn, int ln)
2569{
Benjamin Romer0aca78442015-03-04 12:14:25 -05002570 if (!p)
Ken Cox12e364b2014-03-04 07:58:07 -06002571 return;
Benjamin Romer0aca78442015-03-04 12:14:25 -05002572
Ken Cox12e364b2014-03-04 07:58:07 -06002573 kmem_cache_free(pool, p);
2574}
2575
Benjamin Romer18b87ed2014-07-24 14:08:43 -04002576static ssize_t chipsetready_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002577 struct device_attribute *attr,
2578 const char *buf, size_t count)
Ken Cox12e364b2014-03-04 07:58:07 -06002579{
Benjamin Romer18b87ed2014-07-24 14:08:43 -04002580 char msgtype[64];
Ken Cox12e364b2014-03-04 07:58:07 -06002581
Benjamin Romer66e24b72014-07-25 13:55:10 -04002582 if (sscanf(buf, "%63s", msgtype) != 1)
Ken Cox12e364b2014-03-04 07:58:07 -06002583 return -EINVAL;
Benjamin Romer66e24b72014-07-25 13:55:10 -04002584
Jes Sorensenebec8962015-05-05 18:35:57 -04002585 if (!strcmp(msgtype, "CALLHOMEDISK_MOUNTED")) {
Benjamin Romer66e24b72014-07-25 13:55:10 -04002586 chipset_events[0] = 1;
2587 return count;
Jes Sorensenebec8962015-05-05 18:35:57 -04002588 } else if (!strcmp(msgtype, "MODULES_LOADED")) {
Benjamin Romer66e24b72014-07-25 13:55:10 -04002589 chipset_events[1] = 1;
2590 return count;
Benjamin Romere22a4a02014-08-18 09:34:54 -04002591 }
2592 return -EINVAL;
Ken Cox12e364b2014-03-04 07:58:07 -06002593}
2594
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002595/* The parahotplug/devicedisabled interface gets called by our support script
2596 * when an SR-IOV device has been shut down. The ID is passed to the script
2597 * and then passed back when the device has been removed.
2598 */
2599static ssize_t devicedisabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002600 struct device_attribute *attr,
2601 const char *buf, size_t count)
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002602{
Jes Sorensen94217362015-05-05 18:35:46 -04002603 unsigned int id;
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002604
Jes Sorensenebec8962015-05-05 18:35:57 -04002605 if (kstrtouint(buf, 10, &id))
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002606 return -EINVAL;
2607
2608 parahotplug_request_complete(id, 0);
2609 return count;
2610}
2611
2612/* The parahotplug/deviceenabled interface gets called by our support script
2613 * when an SR-IOV device has been recovered. The ID is passed to the script
2614 * and then passed back when the device has been brought back up.
2615 */
2616static ssize_t deviceenabled_store(struct device *dev,
Benjamin Romer8e76e692015-03-16 13:58:52 -04002617 struct device_attribute *attr,
2618 const char *buf, size_t count)
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002619{
Jes Sorensen94217362015-05-05 18:35:46 -04002620 unsigned int id;
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002621
Jes Sorensenebec8962015-05-05 18:35:57 -04002622 if (kstrtouint(buf, 10, &id))
Benjamin Romere56fa7c2014-07-29 11:11:21 -04002623 return -EINVAL;
2624
2625 parahotplug_request_complete(id, 1);
2626 return count;
2627}
2628
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002629static int
2630visorchipset_mmap(struct file *file, struct vm_area_struct *vma)
2631{
2632 unsigned long physaddr = 0;
2633 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
Erik Arfvidson780fcad2015-05-05 18:36:35 -04002634 u64 addr = 0;
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002635
2636 /* sv_enable_dfp(); */
2637 if (offset & (PAGE_SIZE - 1))
2638 return -ENXIO; /* need aligned offsets */
2639
2640 switch (offset) {
2641 case VISORCHIPSET_MMAP_CONTROLCHANOFFSET:
2642 vma->vm_flags |= VM_IO;
2643 if (!*file_controlvm_channel)
2644 return -ENXIO;
2645
2646 visorchannel_read(*file_controlvm_channel,
2647 offsetof(struct spar_controlvm_channel_protocol,
2648 gp_control_channel),
2649 &addr, sizeof(addr));
2650 if (!addr)
2651 return -ENXIO;
2652
2653 physaddr = (unsigned long)addr;
2654 if (remap_pfn_range(vma, vma->vm_start,
2655 physaddr >> PAGE_SHIFT,
2656 vma->vm_end - vma->vm_start,
2657 /*pgprot_noncached */
2658 (vma->vm_page_prot))) {
2659 return -EAGAIN;
2660 }
2661 break;
2662 default:
2663 return -ENXIO;
2664 }
2665 return 0;
2666}
2667
2668static long visorchipset_ioctl(struct file *file, unsigned int cmd,
2669 unsigned long arg)
2670{
2671 s64 adjustment;
2672 s64 vrtc_offset;
2673
2674 switch (cmd) {
2675 case VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET:
2676 /* get the physical rtc offset */
2677 vrtc_offset = issue_vmcall_query_guest_virtual_time_offset();
2678 if (copy_to_user((void __user *)arg, &vrtc_offset,
2679 sizeof(vrtc_offset))) {
2680 return -EFAULT;
2681 }
2682 return SUCCESS;
2683 case VMCALL_UPDATE_PHYSICAL_TIME:
2684 if (copy_from_user(&adjustment, (void __user *)arg,
2685 sizeof(adjustment))) {
2686 return -EFAULT;
2687 }
2688 return issue_vmcall_update_physical_time(adjustment);
2689 default:
2690 return -EFAULT;
2691 }
2692}
2693
2694static const struct file_operations visorchipset_fops = {
2695 .owner = THIS_MODULE,
2696 .open = visorchipset_open,
2697 .read = NULL,
2698 .write = NULL,
2699 .unlocked_ioctl = visorchipset_ioctl,
2700 .release = visorchipset_release,
2701 .mmap = visorchipset_mmap,
2702};
2703
2704int
2705visorchipset_file_init(dev_t major_dev, struct visorchannel **controlvm_channel)
2706{
2707 int rc = 0;
2708
2709 file_controlvm_channel = controlvm_channel;
2710 cdev_init(&file_cdev, &visorchipset_fops);
2711 file_cdev.owner = THIS_MODULE;
2712 if (MAJOR(major_dev) == 0) {
Erik Arfvidson46168812015-05-05 18:36:14 -04002713 rc = alloc_chrdev_region(&major_dev, 0, 1, "visorchipset");
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002714 /* dynamic major device number registration required */
2715 if (rc < 0)
2716 return rc;
2717 } else {
2718 /* static major device number registration required */
Erik Arfvidson46168812015-05-05 18:36:14 -04002719 rc = register_chrdev_region(major_dev, 1, "visorchipset");
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002720 if (rc < 0)
2721 return rc;
2722 }
2723 rc = cdev_add(&file_cdev, MKDEV(MAJOR(major_dev), 0), 1);
2724 if (rc < 0) {
2725 unregister_chrdev_region(major_dev, 1);
2726 return rc;
2727 }
2728 return 0;
2729}
2730
2731
2732
Ken Cox12e364b2014-03-04 07:58:07 -06002733static int __init
2734visorchipset_init(void)
2735{
2736 int rc = 0, x = 0;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002737 HOSTADDRESS addr;
Ken Cox12e364b2014-03-04 07:58:07 -06002738
Ken Coxfcd0157e2014-04-28 12:23:13 -05002739 if (!unisys_spar_platform)
2740 return -ENODEV;
2741
Benjamin Romer6fe345a2015-03-16 13:58:42 -04002742 memset(&busdev_server_notifiers, 0, sizeof(busdev_server_notifiers));
2743 memset(&busdev_client_notifiers, 0, sizeof(busdev_client_notifiers));
Benjamin Romer84982fb2015-03-16 13:58:07 -04002744 memset(&controlvm_payload_info, 0, sizeof(controlvm_payload_info));
Benjamin Romerea33b4ee52015-03-16 13:58:08 -04002745 memset(&livedump_info, 0, sizeof(livedump_info));
2746 atomic_set(&livedump_info.buffers_in_use, 0);
Ken Cox12e364b2014-03-04 07:58:07 -06002747
Ken Cox9f8d0e82014-03-19 13:06:19 -05002748 if (visorchipset_testvnic) {
Ken Cox9f8d0e82014-03-19 13:06:19 -05002749 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, x, DIAG_SEVERITY_ERR);
2750 rc = x;
Benjamin Romera6a39892015-03-16 13:58:34 -04002751 goto cleanup;
Ken Cox9f8d0e82014-03-19 13:06:19 -05002752 }
Ken Cox12e364b2014-03-04 07:58:07 -06002753
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002754 addr = controlvm_get_channel_address();
Jes Sorensenebec8962015-05-05 18:35:57 -04002755 if (addr) {
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002756 controlvm_channel =
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002757 visorchannel_create_with_lock
2758 (addr,
Benjamin Romerd19642f2014-10-23 14:30:34 -04002759 sizeof(struct spar_controlvm_channel_protocol),
Benjamin Romer5fbaa4b2014-10-23 14:30:14 -04002760 spar_controlvm_channel_protocol_uuid);
Benjamin Romer93a84562014-10-23 14:30:05 -04002761 if (SPAR_CONTROLVM_CHANNEL_OK_CLIENT(
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002762 visorchannel_get_header(controlvm_channel))) {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002763 initialize_controlvm_payload();
2764 } else {
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002765 visorchannel_destroy(controlvm_channel);
2766 controlvm_channel = NULL;
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002767 return -ENODEV;
2768 }
2769 } else {
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002770 return -ENODEV;
2771 }
2772
Benjamin Romer5aa8ae52015-03-16 13:58:44 -04002773 major_dev = MKDEV(visorchipset_major, 0);
2774 rc = visorchipset_file_init(major_dev, &controlvm_channel);
Ken Cox4cb005a2014-03-19 13:06:20 -05002775 if (rc < 0) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002776 POSTCODE_LINUX_2(CHIPSET_INIT_FAILURE_PC, DIAG_SEVERITY_ERR);
Benjamin Romera6a39892015-03-16 13:58:34 -04002777 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002778 }
Ken Cox9f8d0e82014-03-19 13:06:19 -05002779
Benjamin Romerda021f02015-03-16 13:57:58 -04002780 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002781
Benjamin Romer2098dbd2015-03-04 12:14:22 -05002782 if (!visorchipset_disable_controlvm) {
Ken Cox12e364b2014-03-04 07:58:07 -06002783 /* if booting in a crash kernel */
Benjamin Romer1ba00982015-04-06 10:27:40 -04002784 if (is_kdump_kernel())
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002785 INIT_DELAYED_WORK(&periodic_controlvm_work,
Ken Cox12e364b2014-03-04 07:58:07 -06002786 setup_crash_devices_work_queue);
2787 else
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002788 INIT_DELAYED_WORK(&periodic_controlvm_work,
Ken Cox12e364b2014-03-04 07:58:07 -06002789 controlvm_periodic_work);
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002790 periodic_controlvm_workqueue =
Ken Cox12e364b2014-03-04 07:58:07 -06002791 create_singlethread_workqueue("visorchipset_controlvm");
2792
Benjamin Romer38f736e2015-03-16 13:58:13 -04002793 if (!periodic_controlvm_workqueue) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002794 POSTCODE_LINUX_2(CREATE_WORKQUEUE_FAILED_PC,
2795 DIAG_SEVERITY_ERR);
2796 rc = -ENOMEM;
Benjamin Romera6a39892015-03-16 13:58:34 -04002797 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002798 }
Benjamin Romerb53e0e92015-03-16 13:57:56 -04002799 most_recent_message_jiffies = jiffies;
Benjamin Romer911e2132015-03-16 13:57:47 -04002800 poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002801 rc = queue_delayed_work(periodic_controlvm_workqueue,
2802 &periodic_controlvm_work, poll_jiffies);
Ken Cox4cb005a2014-03-19 13:06:20 -05002803 if (rc < 0) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002804 POSTCODE_LINUX_2(QUEUE_DELAYED_WORK_PC,
2805 DIAG_SEVERITY_ERR);
Benjamin Romera6a39892015-03-16 13:58:34 -04002806 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002807 }
Ken Cox12e364b2014-03-04 07:58:07 -06002808 }
2809
Benjamin Romereb34e872015-03-16 13:58:45 -04002810 visorchipset_platform_device.dev.devt = major_dev;
2811 if (platform_device_register(&visorchipset_platform_device) < 0) {
Ken Cox4cb005a2014-03-19 13:06:20 -05002812 POSTCODE_LINUX_2(DEVICE_REGISTER_FAILURE_PC, DIAG_SEVERITY_ERR);
2813 rc = -1;
Benjamin Romera6a39892015-03-16 13:58:34 -04002814 goto cleanup;
Ken Cox4cb005a2014-03-19 13:06:20 -05002815 }
Ken Cox12e364b2014-03-04 07:58:07 -06002816 POSTCODE_LINUX_2(CHIPSET_INIT_SUCCESS_PC, POSTCODE_SEVERITY_INFO);
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04002817
2818 rc = visorbus_init();
Benjamin Romera6a39892015-03-16 13:58:34 -04002819cleanup:
Ken Cox12e364b2014-03-04 07:58:07 -06002820 if (rc) {
Ken Cox12e364b2014-03-04 07:58:07 -06002821 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, rc,
2822 POSTCODE_SEVERITY_ERR);
2823 }
2824 return rc;
2825}
2826
Erik Arfvidsone3420ed2015-05-05 18:36:13 -04002827void
2828visorchipset_file_cleanup(dev_t major_dev)
2829{
2830 if (file_cdev.ops)
2831 cdev_del(&file_cdev);
2832 file_cdev.ops = NULL;
2833 unregister_chrdev_region(major_dev, 1);
2834}
2835
Ken Cox12e364b2014-03-04 07:58:07 -06002836static void
2837visorchipset_exit(void)
2838{
Ken Cox12e364b2014-03-04 07:58:07 -06002839 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
2840
Prarit Bhargavac79b28f2015-05-05 18:36:15 -04002841 visorbus_exit();
2842
Ken Cox12e364b2014-03-04 07:58:07 -06002843 if (visorchipset_disable_controlvm) {
2844 ;
2845 } else {
Benjamin Romer9232d2d2015-03-16 13:57:57 -04002846 cancel_delayed_work(&periodic_controlvm_work);
2847 flush_workqueue(periodic_controlvm_workqueue);
2848 destroy_workqueue(periodic_controlvm_workqueue);
2849 periodic_controlvm_workqueue = NULL;
Benjamin Romer84982fb2015-03-16 13:58:07 -04002850 destroy_controlvm_payload_info(&controlvm_payload_info);
Ken Cox12e364b2014-03-04 07:58:07 -06002851 }
Benjamin Romer17833192014-07-15 13:30:41 -04002852
Ken Cox12e364b2014-03-04 07:58:07 -06002853 cleanup_controlvm_structures();
2854
Benjamin Romerda021f02015-03-16 13:57:58 -04002855 memset(&g_chipset_msg_hdr, 0, sizeof(struct controlvm_message_header));
Ken Cox12e364b2014-03-04 07:58:07 -06002856
Benjamin Romerc3d9a222015-03-16 13:58:05 -04002857 visorchannel_destroy(controlvm_channel);
Benjamin Romer8a1182e2014-07-17 12:39:58 -04002858
Sudip Mukherjeeaddceb12015-03-24 20:47:28 +05302859 visorchipset_file_cleanup(visorchipset_platform_device.dev.devt);
Ken Cox12e364b2014-03-04 07:58:07 -06002860 POSTCODE_LINUX_2(DRIVER_EXIT_PC, POSTCODE_SEVERITY_INFO);
Ken Cox12e364b2014-03-04 07:58:07 -06002861}
2862
2863module_param_named(testvnic, visorchipset_testvnic, int, S_IRUGO);
2864MODULE_PARM_DESC(visorchipset_testvnic, "1 to test vnic, using dummy VNIC connected via a loopback to a physical ethernet");
Ken Cox12e364b2014-03-04 07:58:07 -06002865module_param_named(testvnicclient, visorchipset_testvnicclient, int, S_IRUGO);
2866MODULE_PARM_DESC(visorchipset_testvnicclient, "1 to test vnic, using real VNIC channel attached to a separate IOVM guest");
Ken Cox12e364b2014-03-04 07:58:07 -06002867module_param_named(testmsg, visorchipset_testmsg, int, S_IRUGO);
2868MODULE_PARM_DESC(visorchipset_testmsg,
2869 "1 to manufacture the chipset, bus, and switch messages");
Ken Cox12e364b2014-03-04 07:58:07 -06002870module_param_named(major, visorchipset_major, int, S_IRUGO);
Jes Sorensenb615d622015-05-05 18:35:38 -04002871MODULE_PARM_DESC(visorchipset_major,
2872 "major device number to use for the device node");
Ken Cox12e364b2014-03-04 07:58:07 -06002873module_param_named(serverregwait, visorchipset_serverregwait, int, S_IRUGO);
2874MODULE_PARM_DESC(visorchipset_serverreqwait,
2875 "1 to have the module wait for the visor bus to register");
Ken Cox12e364b2014-03-04 07:58:07 -06002876module_param_named(clientregwait, visorchipset_clientregwait, int, S_IRUGO);
2877MODULE_PARM_DESC(visorchipset_clientregwait, "1 to have the module wait for the visorclientbus to register");
Ken Cox12e364b2014-03-04 07:58:07 -06002878module_param_named(testteardown, visorchipset_testteardown, int, S_IRUGO);
2879MODULE_PARM_DESC(visorchipset_testteardown,
2880 "1 to test teardown of the chipset, bus, and switch");
Ken Cox12e364b2014-03-04 07:58:07 -06002881module_param_named(disable_controlvm, visorchipset_disable_controlvm, int,
2882 S_IRUGO);
2883MODULE_PARM_DESC(visorchipset_disable_controlvm,
2884 "1 to disable polling of controlVm channel");
Ken Cox12e364b2014-03-04 07:58:07 -06002885module_param_named(holdchipsetready, visorchipset_holdchipsetready,
2886 int, S_IRUGO);
2887MODULE_PARM_DESC(visorchipset_holdchipsetready,
2888 "1 to hold response to CHIPSET_READY");
Jes Sorensenb615d622015-05-05 18:35:38 -04002889
Ken Cox12e364b2014-03-04 07:58:07 -06002890module_init(visorchipset_init);
2891module_exit(visorchipset_exit);
2892
2893MODULE_AUTHOR("Unisys");
2894MODULE_LICENSE("GPL");
2895MODULE_DESCRIPTION("Supervisor chipset driver for service partition: ver "
2896 VERSION);
2897MODULE_VERSION(VERSION);