• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2010 - 2015 UNISYS CORPORATION
2  * All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  * NON INFRINGEMENT.  See the GNU General Public License for more
12  * details.
13  */
14 
15 #ifndef __VBUSCHANNEL_H__
16 #define __VBUSCHANNEL_H__
17 
18 /*  The vbus channel is the channel area provided via the BUS_CREATE controlvm
19  *  message for each virtual bus.  This channel area is provided to both server
20  *  and client ends of the bus.  The channel header area is initialized by
21  *  the server, and the remaining information is filled in by the client.
22  *  We currently use this for the client to provide various information about
23  *  the client devices and client drivers for the server end to see.
24  */
25 #include <linux/uuid.h>
26 #include "channel.h"
27 
28 /* {193b331b-c58f-11da-95a9-00e08161165f} */
29 #define SPAR_VBUS_CHANNEL_PROTOCOL_UUID \
30 		UUID_LE(0x193b331b, 0xc58f, 0x11da, \
31 				0x95, 0xa9, 0x0, 0xe0, 0x81, 0x61, 0x16, 0x5f)
32 static const uuid_le spar_vbus_channel_protocol_uuid =
33 	SPAR_VBUS_CHANNEL_PROTOCOL_UUID;
34 
35 #define SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE ULTRA_CHANNEL_PROTOCOL_SIGNATURE
36 
37 /* Must increment this whenever you insert or delete fields within this channel
38  * struct.  Also increment whenever you change the meaning of fields within this
39  * channel struct so as to break pre-existing software.  Note that you can
40  * usually add fields to the END of the channel struct withOUT needing to
41  * increment this.
42  */
43 #define SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID 1
44 
45 #define SPAR_VBUS_CHANNEL_OK_CLIENT(ch)       \
46 	spar_check_channel_client(ch,				\
47 				   spar_vbus_channel_protocol_uuid,	\
48 				   "vbus",				\
49 				   sizeof(struct spar_vbus_channel_protocol),\
50 				   SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID, \
51 				   SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE)
52 
53 #define SPAR_VBUS_CHANNEL_OK_SERVER(actual_bytes)    \
54 	(spar_check_channel_server(spar_vbus_channel_protocol_uuid,	\
55 				   "vbus",				\
56 				   sizeof(struct spar_vbus_channel_protocol),\
57 				   actual_bytes))
58 
59 #pragma pack(push, 1)		/* both GCC and VC now allow this pragma */
60 
61 /*
62  * An array of this struct is present in the channel area for each vbus.
63  * (See vbuschannel.h.)
64  * It is filled in by the client side to provide info about the device
65  * and driver from the client's perspective.
66  */
67 struct ultra_vbus_deviceinfo {
68 	u8 devtype[16];		/* short string identifying the device type */
69 	u8 drvname[16];		/* driver .sys file name */
70 	u8 infostrs[96];	/* kernel version */
71 	u8 reserved[128];	/* pad size to 256 bytes */
72 };
73 
74 /**
75  * vbuschannel_sanitize_buffer() - remove non-printable chars from buffer
76  * @p: destination buffer where chars are written to
77  * @remain: number of bytes that can be written starting at #p
78  * @src: pointer to source buffer
79  * @srcmax: number of valid characters at #src
80  *
81  * Reads chars from the buffer at @src for @srcmax bytes, and writes to
82  * the buffer at @p, which is @remain bytes long, ensuring never to
83  * overflow the buffer at @p, using the following rules:
84  * - printable characters are simply copied from the buffer at @src to the
85  *   buffer at @p
86  * - intervening streaks of non-printable characters in the buffer at @src
87  *   are replaced with a single space in the buffer at @p
88  * Note that we pay no attention to '\0'-termination.
89  *
90  * Pass @p == NULL and @remain == 0 for this special behavior -- In this
91  * case, we simply return the number of bytes that WOULD HAVE been written
92  * to a buffer at @p, had it been infinitely big.
93  *
94  * Return: the number of bytes written to @p (or WOULD HAVE been written to
95  *         @p, as described in the previous paragraph)
96  */
97 static inline int
vbuschannel_sanitize_buffer(char * p,int remain,char * src,int srcmax)98 vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
99 {
100 	int chars = 0;
101 	int nonprintable_streak = 0;
102 
103 	while (srcmax > 0) {
104 		if ((*src >= ' ') && (*src < 0x7f)) {
105 			if (nonprintable_streak) {
106 				if (remain > 0) {
107 					*p = ' ';
108 					p++;
109 					remain--;
110 					chars++;
111 				} else if (!p) {
112 					chars++;
113 				}
114 				nonprintable_streak = 0;
115 			}
116 			if (remain > 0) {
117 				*p = *src;
118 				p++;
119 				remain--;
120 				chars++;
121 			} else if (!p) {
122 				chars++;
123 			}
124 		} else {
125 			nonprintable_streak = 1;
126 		}
127 		src++;
128 		srcmax--;
129 	}
130 	return chars;
131 }
132 
133 #define VBUSCHANNEL_ADDACHAR(ch, p, remain, chars) \
134 	do {					   \
135 		if (remain <= 0)		   \
136 			break;			   \
137 		*p = ch;			   \
138 		p++;  chars++;  remain--;	   \
139 	} while (0)
140 
141 /**
142  * vbuschannel_itoa() - convert non-negative int to string
143  * @p: destination string
144  * @remain: max number of bytes that can be written to @p
145  * @num: input int to convert
146  *
147  * Converts the non-negative value at @num to an ascii decimal string
148  * at @p, writing at most @remain bytes.  Note there is NO '\0' termination
149  * written to @p.
150  *
151  * Return: number of bytes written to @p
152  *
153  */
154 static inline int
vbuschannel_itoa(char * p,int remain,int num)155 vbuschannel_itoa(char *p, int remain, int num)
156 {
157 	int digits = 0;
158 	char s[32];
159 	int i;
160 
161 	if (num == 0) {
162 		/* '0' is a special case */
163 		if (remain <= 0)
164 			return 0;
165 		*p = '0';
166 		return 1;
167 	}
168 	/* form a backwards decimal ascii string in <s> */
169 	while (num > 0) {
170 		if (digits >= (int)sizeof(s))
171 			return 0;
172 		s[digits++] = (num % 10) + '0';
173 		num = num / 10;
174 	}
175 	if (remain < digits) {
176 		/* not enough room left at <p> to hold number, so fill with
177 		 * '?'
178 		 */
179 		for (i = 0; i < remain; i++, p++)
180 			*p = '?';
181 		return remain;
182 	}
183 	/* plug in the decimal ascii string representing the number, by */
184 	/* reversing the string we just built in <s> */
185 	i = digits;
186 	while (i > 0) {
187 		i--;
188 		*p = s[i];
189 		p++;
190 	}
191 	return digits;
192 }
193 
194 /**
195  * vbuschannel_devinfo_to_string() - format a struct ultra_vbus_deviceinfo
196  *                                   to a printable string
197  * @devinfo: the struct ultra_vbus_deviceinfo to format
198  * @p: destination string area
199  * @remain: size of destination string area in bytes
200  * @devix: the device index to be included in the output data, or -1 if no
201  *         device index is to be included
202  *
203  * Reads @devInfo, and converts its contents to a printable string at @p,
204  * writing at most @remain bytes. Note there is NO '\0' termination
205  * written to @p.
206  *
207  * Return: number of bytes written to @p
208  */
209 static inline int
vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo * devinfo,char * p,int remain,int devix)210 vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
211 			      char *p, int remain, int devix)
212 {
213 	char *psrc;
214 	int nsrc, x, i, pad;
215 	int chars = 0;
216 
217 	psrc = &devinfo->devtype[0];
218 	nsrc = sizeof(devinfo->devtype);
219 	if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
220 		return 0;
221 
222 	/* emit device index */
223 	if (devix >= 0) {
224 		VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
225 		x = vbuschannel_itoa(p, remain, devix);
226 		p += x;
227 		remain -= x;
228 		chars += x;
229 		VBUSCHANNEL_ADDACHAR(']', p, remain, chars);
230 	} else {
231 		VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
232 		VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
233 		VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
234 	}
235 
236 	/* emit device type */
237 	x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
238 	p += x;
239 	remain -= x;
240 	chars += x;
241 	pad = 15 - x;		/* pad device type to be exactly 15 chars */
242 	for (i = 0; i < pad; i++)
243 		VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
244 	VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
245 
246 	/* emit driver name */
247 	psrc = &devinfo->drvname[0];
248 	nsrc = sizeof(devinfo->drvname);
249 	x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
250 	p += x;
251 	remain -= x;
252 	chars += x;
253 	pad = 15 - x;		/* pad driver name to be exactly 15 chars */
254 	for (i = 0; i < pad; i++)
255 		VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
256 	VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
257 
258 	/* emit strings */
259 	psrc = &devinfo->infostrs[0];
260 	nsrc = sizeof(devinfo->infostrs);
261 	x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
262 	p += x;
263 	remain -= x;
264 	chars += x;
265 	VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);
266 
267 	return chars;
268 }
269 
270 struct spar_vbus_headerinfo {
271 	u32 struct_bytes;	/* size of this struct in bytes */
272 	u32 device_info_struct_bytes;	/* sizeof(ULTRA_VBUS_DEVICEINFO) */
273 	u32 dev_info_count;	/* num of items in DevInfo member */
274 	/* (this is the allocated size) */
275 	u32 chp_info_offset;	/* byte offset from beginning of this struct */
276 	/* to the ChpInfo struct (below) */
277 	u32 bus_info_offset;	/* byte offset from beginning of this struct */
278 	/* to the BusInfo struct (below) */
279 	u32 dev_info_offset;	/* byte offset from beginning of this struct */
280 	/* to the DevInfo array (below) */
281 	u8 reserved[104];
282 };
283 
284 struct spar_vbus_channel_protocol {
285 	struct channel_header channel_header;	/* initialized by server */
286 	struct spar_vbus_headerinfo hdr_info;	/* initialized by server */
287 	/* the remainder of this channel is filled in by the client */
288 	struct ultra_vbus_deviceinfo chp_info;
289 	/* describes client chipset device and driver */
290 	struct ultra_vbus_deviceinfo bus_info;
291 	/* describes client bus device and driver */
292 	struct ultra_vbus_deviceinfo dev_info[0];
293 	/* describes client device and driver for each device on the bus */
294 };
295 
296 #define VBUS_CH_SIZE_EXACT(MAXDEVICES) \
297 	(sizeof(ULTRA_VBUS_CHANNEL_PROTOCOL) + ((MAXDEVICES) * \
298 						sizeof(ULTRA_VBUS_DEVICEINFO)))
299 #define VBUS_CH_SIZE(MAXDEVICES) COVER(VBUS_CH_SIZE_EXACT(MAXDEVICES), 4096)
300 
301 #pragma pack(pop)
302 
303 #endif
304