GNU Linux-libre 4.9.314-gnu1
[releases.git] / drivers / staging / unisys / include / channel.h
1 /* Copyright (C) 2010 - 2013 UNISYS CORPORATION
2  * All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or (at
7  * your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
12  * NON INFRINGEMENT.  See the GNU General Public License for more
13  * details.
14  */
15
16 #ifndef __CHANNEL_H__
17 #define __CHANNEL_H__
18
19 #include <linux/types.h>
20 #include <linux/io.h>
21 #include <linux/uuid.h>
22
23 /*
24 * Whenever this file is changed a corresponding change must be made in
25 * the Console/ServicePart/visordiag_early/supervisor_channel.h file
26 * which is needed for Linux kernel compiles. These two files must be
27 * in sync.
28 */
29
30 /* define the following to prevent include nesting in kernel header
31  * files of similar abbreviated content
32  */
33 #define __SUPERVISOR_CHANNEL_H__
34
35 #define SIGNATURE_16(A, B) ((A) | (B << 8))
36 #define SIGNATURE_32(A, B, C, D) \
37         (SIGNATURE_16(A, B) | (SIGNATURE_16(C, D) << 16))
38 #define SIGNATURE_64(A, B, C, D, E, F, G, H) \
39         (SIGNATURE_32(A, B, C, D) | ((u64)(SIGNATURE_32(E, F, G, H)) << 32))
40
41 #ifndef lengthof
42 #define lengthof(TYPE, MEMBER) (sizeof(((TYPE *)0)->MEMBER))
43 #endif
44 #ifndef COVERQ
45 #define COVERQ(v, d)  (((v) + (d) - 1) / (d))
46 #endif
47 #ifndef COVER
48 #define COVER(v, d)   ((d) * COVERQ(v, d))
49 #endif
50
51 #define ULTRA_CHANNEL_PROTOCOL_SIGNATURE  SIGNATURE_32('E', 'C', 'N', 'L')
52
53 enum channel_serverstate {
54         CHANNELSRV_UNINITIALIZED = 0,   /* channel is in an undefined state */
55         CHANNELSRV_READY = 1    /* channel has been initialized by server */
56 };
57
58 enum channel_clientstate {
59         CHANNELCLI_DETACHED = 0,
60         CHANNELCLI_DISABLED = 1,        /* client can see channel but is NOT
61                                          * allowed to use it unless given TBD
62                                          * explicit request (should actually be
63                                          * < DETACHED)
64                                          */
65         CHANNELCLI_ATTACHING = 2,       /* legacy EFI client request
66                                          * for EFI server to attach
67                                          */
68         CHANNELCLI_ATTACHED = 3,        /* idle, but client may want
69                                          * to use channel any time
70                                          */
71         CHANNELCLI_BUSY = 4,    /* client either wants to use or is
72                                  * using channel
73                                  */
74         CHANNELCLI_OWNED = 5    /* "no worries" state - client can */
75                                 /* access channel anytime */
76 };
77
78 #define SPAR_CHANNEL_SERVER_READY(ch) \
79         (readl(&(ch)->srv_state) == CHANNELSRV_READY)
80
81 #define ULTRA_VALID_CHANNELCLI_TRANSITION(o, n)                         \
82         (((((o) == CHANNELCLI_DETACHED) && ((n) == CHANNELCLI_DISABLED)) || \
83           (((o) == CHANNELCLI_ATTACHING) && ((n) == CHANNELCLI_DISABLED)) || \
84           (((o) == CHANNELCLI_ATTACHED) && ((n) == CHANNELCLI_DISABLED)) || \
85           (((o) == CHANNELCLI_ATTACHING) && ((n) == CHANNELCLI_DETACHED)) || \
86           (((o) == CHANNELCLI_ATTACHED) && ((n) == CHANNELCLI_DETACHED)) || \
87           (((o) == CHANNELCLI_DETACHED) && ((n) == CHANNELCLI_ATTACHING)) || \
88           (((o) == CHANNELCLI_ATTACHING) && ((n) == CHANNELCLI_ATTACHED)) || \
89           (((o) == CHANNELCLI_DETACHED) && ((n) == CHANNELCLI_ATTACHED)) || \
90           (((o) == CHANNELCLI_BUSY) && ((n) == CHANNELCLI_ATTACHED)) || \
91           (((o) == CHANNELCLI_ATTACHED) && ((n) == CHANNELCLI_BUSY)) || \
92           (((o) == CHANNELCLI_DETACHED) && ((n) == CHANNELCLI_OWNED)) || \
93           (((o) == CHANNELCLI_DISABLED) && ((n) == CHANNELCLI_OWNED)) || \
94           (((o) == CHANNELCLI_ATTACHING) && ((n) == CHANNELCLI_OWNED)) || \
95           (((o) == CHANNELCLI_ATTACHED) && ((n) == CHANNELCLI_OWNED)) || \
96           (((o) == CHANNELCLI_BUSY) && ((n) == CHANNELCLI_OWNED)) || (0)) \
97          ? (1) : (0))
98
99 /* Values for ULTRA_CHANNEL_PROTOCOL.CliErrorBoot: */
100 /* throttling invalid boot channel statetransition error due to client
101  * disabled
102  */
103 #define ULTRA_CLIERRORBOOT_THROTTLEMSG_DISABLED    0x01
104
105 /* throttling invalid boot channel statetransition error due to client
106  * not attached
107  */
108 #define ULTRA_CLIERRORBOOT_THROTTLEMSG_NOTATTACHED 0x02
109
110 /* throttling invalid boot channel statetransition error due to busy channel */
111 #define ULTRA_CLIERRORBOOT_THROTTLEMSG_BUSY        0x04
112
113 /* Values for ULTRA_CHANNEL_PROTOCOL.Features: This define exists so
114  * that windows guest can look at the FeatureFlags in the io channel,
115  * and configure the windows driver to use interrupts or not based on
116  * this setting.  This flag is set in uislib after the
117  * ULTRA_VHBA_init_channel is called.  All feature bits for all
118  * channels should be defined here.  The io channel feature bits are
119  * defined right here
120  */
121 #define ULTRA_IO_DRIVER_ENABLES_INTS (0x1ULL << 1)
122 #define ULTRA_IO_CHANNEL_IS_POLLING (0x1ULL << 3)
123 #define ULTRA_IO_IOVM_IS_OK_WITH_DRIVER_DISABLING_INTS (0x1ULL << 4)
124 #define ULTRA_IO_DRIVER_DISABLES_INTS (0x1ULL << 5)
125 #define ULTRA_IO_DRIVER_SUPPORTS_ENHANCED_RCVBUF_CHECKING (0x1ULL << 6)
126
127 /* Common Channel Header */
128 struct channel_header {
129         u64 signature;          /* Signature */
130         u32 legacy_state;       /* DEPRECATED - being replaced by */
131                         /* SrvState, CliStateBoot, and CliStateOS below */
132         u32 header_size;        /* sizeof(struct channel_header) */
133         u64 size;               /* Total size of this channel in bytes */
134         u64 features;           /* Flags to modify behavior */
135         uuid_le chtype;         /* Channel type: data, bus, control, etc. */
136         u64 partition_handle;   /* ID of guest partition */
137         u64 handle;             /* Device number of this channel in client */
138         u64 ch_space_offset;    /* Offset in bytes to channel specific area */
139         u32 version_id;         /* struct channel_header Version ID */
140         u32 partition_index;    /* Index of guest partition */
141         uuid_le zone_uuid;      /* Guid of Channel's zone */
142         u32 cli_str_offset;     /* offset from channel header to
143                                  * nul-terminated ClientString (0 if
144                                  * ClientString not present)
145                                  */
146         u32 cli_state_boot;     /* CHANNEL_CLIENTSTATE of pre-boot
147                                  * EFI client of this channel
148                                  */
149         u32 cmd_state_cli;      /* CHANNEL_COMMANDSTATE (overloaded in
150                                  * Windows drivers, see ServerStateUp,
151                                  * ServerStateDown, etc)
152                                  */
153         u32 cli_state_os;       /* CHANNEL_CLIENTSTATE of Guest OS
154                                  * client of this channel
155                                  */
156         u32 ch_characteristic;  /* CHANNEL_CHARACTERISTIC_<xxx> */
157         u32 cmd_state_srv;      /* CHANNEL_COMMANDSTATE (overloaded in
158                                  * Windows drivers, see ServerStateUp,
159                                  * ServerStateDown, etc)
160                                  */
161         u32 srv_state;          /* CHANNEL_SERVERSTATE */
162         u8 cli_error_boot;      /* bits to indicate err states for
163                                  * boot clients, so err messages can
164                                  * be throttled
165                                  */
166         u8 cli_error_os;        /* bits to indicate err states for OS
167                                  * clients, so err messages can be
168                                  * throttled
169                                  */
170         u8 filler[1];           /* Pad out to 128 byte cacheline */
171         /* Please add all new single-byte values below here */
172         u8 recover_channel;
173 } __packed;
174
175 #define ULTRA_CHANNEL_ENABLE_INTS (0x1ULL << 0)
176
177 /* Subheader for the Signal Type variation of the Common Channel */
178 struct signal_queue_header {
179         /* 1st cache line */
180         u32 version;            /* SIGNAL_QUEUE_HEADER Version ID */
181         u32 chtype;             /* Queue type: storage, network */
182         u64 size;               /* Total size of this queue in bytes */
183         u64 sig_base_offset;    /* Offset to signal queue area */
184         u64 features;           /* Flags to modify behavior */
185         u64 num_sent;           /* Total # of signals placed in this queue */
186         u64 num_overflows;      /* Total # of inserts failed due to
187                                  * full queue
188                                  */
189         u32 signal_size;        /* Total size of a signal for this queue */
190         u32 max_slots;          /* Max # of slots in queue, 1 slot is
191                                  * always empty
192                                  */
193         u32 max_signals;        /* Max # of signals in queue
194                                  * (MaxSignalSlots-1)
195                                  */
196         u32 head;               /* Queue head signal # */
197         /* 2nd cache line */
198         u64 num_received;       /* Total # of signals removed from this queue */
199         u32 tail;               /* Queue tail signal */
200         u32 reserved1;          /* Reserved field */
201         u64 reserved2;          /* Reserved field */
202         u64 client_queue;
203         u64 num_irq_received;   /* Total # of Interrupts received.  This
204                                  * is incremented by the ISR in the
205                                  * guest windows driver
206                                  */
207         u64 num_empty;          /* Number of times that visor_signal_remove
208                                  * is called and returned Empty Status.
209                                  */
210         u32 errorflags;         /* Error bits set during SignalReinit
211                                  * to denote trouble with client's
212                                  * fields
213                                  */
214         u8 filler[12];          /* Pad out to 64 byte cacheline */
215 } __packed;
216
217 #define spar_signal_init(chan, QHDRFLD, QDATAFLD, QDATATYPE, ver, typ)  \
218         do {                                                            \
219                 memset(&chan->QHDRFLD, 0, sizeof(chan->QHDRFLD));       \
220                 chan->QHDRFLD.version = ver;                            \
221                 chan->QHDRFLD.chtype = typ;                             \
222                 chan->QHDRFLD.size = sizeof(chan->QDATAFLD);            \
223                 chan->QHDRFLD.signal_size = sizeof(QDATATYPE);          \
224                 chan->QHDRFLD.sig_base_offset = (u64)(chan->QDATAFLD) - \
225                         (u64)(&chan->QHDRFLD);                          \
226                 chan->QHDRFLD.max_slots =                               \
227                         sizeof(chan->QDATAFLD) / sizeof(QDATATYPE);     \
228                 chan->QHDRFLD.max_signals = chan->QHDRFLD.max_slots - 1;\
229         } while (0)
230
231 /* Generic function useful for validating any type of channel when it is
232  * received by the client that will be accessing the channel.
233  * Note that <logCtx> is only needed for callers in the EFI environment, and
234  * is used to pass the EFI_DIAG_CAPTURE_PROTOCOL needed to log messages.
235  */
236 static inline int
237 spar_check_channel_client(void __iomem *ch,
238                           uuid_le expected_uuid,
239                           char *chname,
240                           u64 expected_min_bytes,
241                           u32 expected_version,
242                           u64 expected_signature)
243 {
244         if (uuid_le_cmp(expected_uuid, NULL_UUID_LE) != 0) {
245                 uuid_le guid;
246
247                 memcpy_fromio(&guid,
248                               &((struct channel_header __iomem *)(ch))->chtype,
249                               sizeof(guid));
250                 /* caller wants us to verify type GUID */
251                 if (uuid_le_cmp(guid, expected_uuid) != 0) {
252                         pr_err("Channel mismatch on channel=%s(%pUL) field=type expected=%pUL actual=%pUL\n",
253                                chname, &expected_uuid,
254                                &expected_uuid, &guid);
255                         return 0;
256                 }
257         }
258         if (expected_min_bytes > 0) {   /* verify channel size */
259                 unsigned long long bytes =
260                                 readq(&((struct channel_header __iomem *)
261                                         (ch))->size);
262                 if (bytes < expected_min_bytes) {
263                         pr_err("Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8Lx actual=0x%-8.8Lx\n",
264                                chname, &expected_uuid,
265                                (unsigned long long)expected_min_bytes, bytes);
266                         return 0;
267                 }
268         }
269         if (expected_version > 0) {     /* verify channel version */
270                 unsigned long ver = readl(&((struct channel_header __iomem *)
271                                     (ch))->version_id);
272                 if (ver != expected_version) {
273                         pr_err("Channel mismatch on channel=%s(%pUL) field=version expected=0x%-8.8lx actual=0x%-8.8lx\n",
274                                chname, &expected_uuid,
275                                (unsigned long)expected_version, ver);
276                         return 0;
277                 }
278         }
279         if (expected_signature > 0) {   /* verify channel signature */
280                 unsigned long long sig =
281                                 readq(&((struct channel_header __iomem *)
282                                         (ch))->signature);
283                 if (sig != expected_signature) {
284                         pr_err("Channel mismatch on channel=%s(%pUL) field=signature expected=0x%-8.8llx actual=0x%-8.8llx\n",
285                                chname, &expected_uuid,
286                                expected_signature, sig);
287                         return 0;
288                 }
289         }
290         return 1;
291 }
292
293 /* Generic function useful for validating any type of channel when it is about
294  * to be initialized by the server of the channel.
295  * Note that <logCtx> is only needed for callers in the EFI environment, and
296  * is used to pass the EFI_DIAG_CAPTURE_PROTOCOL needed to log messages.
297  */
298 static inline int spar_check_channel_server(uuid_le typeuuid, char *name,
299                                             u64 expected_min_bytes,
300                                             u64 actual_bytes)
301 {
302         if (expected_min_bytes > 0)     /* verify channel size */
303                 if (actual_bytes < expected_min_bytes) {
304                         pr_err("Channel mismatch on channel=%s(%pUL) field=size expected=0x%-8.8llx actual=0x%-8.8llx\n",
305                                name, &typeuuid, expected_min_bytes,
306                                actual_bytes);
307                         return 0;
308                 }
309         return 1;
310 }
311
312 /*
313 * Routine Description:
314 * Tries to insert the prebuilt signal pointed to by pSignal into the nth
315 * Queue of the Channel pointed to by pChannel
316 *
317 * Parameters:
318 * pChannel: (IN) points to the IO Channel
319 * Queue: (IN) nth Queue of the IO Channel
320 * pSignal: (IN) pointer to the signal
321 *
322 * Assumptions:
323 * - pChannel, Queue and pSignal are valid.
324 * - If insertion fails due to a full queue, the caller will determine the
325 * retry policy (e.g. wait & try again, report an error, etc.).
326 *
327 * Return value: 1 if the insertion succeeds, 0 if the queue was
328 * full.
329 */
330
331 unsigned char spar_signal_insert(struct channel_header __iomem *ch, u32 queue,
332                                  void *sig);
333
334 /*
335 * Routine Description:
336 * Removes one signal from Channel pChannel's nth Queue at the
337 * time of the call and copies it into the memory pointed to by
338 * pSignal.
339 *
340 * Parameters:
341 * pChannel: (IN) points to the IO Channel
342 * Queue: (IN) nth Queue of the IO Channel
343 * pSignal: (IN) pointer to where the signals are to be copied
344 *
345 * Assumptions:
346 * - pChannel and Queue are valid.
347 * - pSignal points to a memory area large enough to hold queue's SignalSize
348 *
349 * Return value: 1 if the removal succeeds, 0 if the queue was
350 * empty.
351 */
352
353 unsigned char spar_signal_remove(struct channel_header __iomem *ch, u32 queue,
354                                  void *sig);
355
356 /*
357 * Routine Description:
358 * Removes all signals present in Channel pChannel's nth Queue at the
359 * time of the call and copies them into the memory pointed to by
360 * pSignal.  Returns the # of signals copied as the value of the routine.
361 *
362 * Parameters:
363 * pChannel: (IN) points to the IO Channel
364 * Queue: (IN) nth Queue of the IO Channel
365 * pSignal: (IN) pointer to where the signals are to be copied
366 *
367 * Assumptions:
368 * - pChannel and Queue are valid.
369 * - pSignal points to a memory area large enough to hold Queue's MaxSignals
370 * # of signals, each of which is Queue's SignalSize.
371 *
372 * Return value:
373 * # of signals copied.
374 */
375 unsigned int spar_signal_remove_all(struct channel_header *ch, u32 queue,
376                                     void *sig);
377
378 /*
379 * Routine Description:
380 * Determine whether a signal queue is empty.
381 *
382 * Parameters:
383 * pChannel: (IN) points to the IO Channel
384 * Queue: (IN) nth Queue of the IO Channel
385 *
386 * Return value:
387 * 1 if the signal queue is empty, 0 otherwise.
388 */
389 unsigned char spar_signalqueue_empty(struct channel_header __iomem *ch,
390                                      u32 queue);
391
392 /*
393  * CHANNEL Guids
394  */
395
396 /* {414815ed-c58c-11da-95a9-00e08161165f} */
397 #define SPAR_VHBA_CHANNEL_PROTOCOL_UUID \
398                 UUID_LE(0x414815ed, 0xc58c, 0x11da, \
399                                 0x95, 0xa9, 0x0, 0xe0, 0x81, 0x61, 0x16, 0x5f)
400 static const uuid_le spar_vhba_channel_protocol_uuid =
401         SPAR_VHBA_CHANNEL_PROTOCOL_UUID;
402 #define SPAR_VHBA_CHANNEL_PROTOCOL_UUID_STR \
403         "414815ed-c58c-11da-95a9-00e08161165f"
404
405 /* {8cd5994d-c58e-11da-95a9-00e08161165f} */
406 #define SPAR_VNIC_CHANNEL_PROTOCOL_UUID \
407                 UUID_LE(0x8cd5994d, 0xc58e, 0x11da, \
408                                 0x95, 0xa9, 0x0, 0xe0, 0x81, 0x61, 0x16, 0x5f)
409 static const uuid_le spar_vnic_channel_protocol_uuid =
410         SPAR_VNIC_CHANNEL_PROTOCOL_UUID;
411 #define SPAR_VNIC_CHANNEL_PROTOCOL_UUID_STR \
412         "8cd5994d-c58e-11da-95a9-00e08161165f"
413
414 /* {72120008-4AAB-11DC-8530-444553544200} */
415 #define SPAR_SIOVM_UUID \
416                 UUID_LE(0x72120008, 0x4AAB, 0x11DC, \
417                                 0x85, 0x30, 0x44, 0x45, 0x53, 0x54, 0x42, 0x00)
418 static const uuid_le spar_siovm_uuid = SPAR_SIOVM_UUID;
419
420 /* {5b52c5ac-e5f5-4d42-8dff-429eaecd221f} */
421 #define SPAR_CONTROLDIRECTOR_CHANNEL_PROTOCOL_UUID  \
422                 UUID_LE(0x5b52c5ac, 0xe5f5, 0x4d42, \
423                                 0x8d, 0xff, 0x42, 0x9e, 0xae, 0xcd, 0x22, 0x1f)
424
425 static const uuid_le spar_controldirector_channel_protocol_uuid =
426         SPAR_CONTROLDIRECTOR_CHANNEL_PROTOCOL_UUID;
427
428 /* {b4e79625-aede-4eAA-9e11-D3eddcd4504c} */
429 #define SPAR_DIAG_POOL_CHANNEL_PROTOCOL_UUID                            \
430                 UUID_LE(0xb4e79625, 0xaede, 0x4eaa, \
431                                 0x9e, 0x11, 0xd3, 0xed, 0xdc, 0xd4, 0x50, 0x4c)
432
433 #endif