GNU Linux-libre 4.14.257-gnu1
[releases.git] / drivers / staging / unisys / include / iochannel.h
1 /*
2  * Copyright (C) 2010 - 2016 UNISYS CORPORATION
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13  * NON INFRINGEMENT.  See the GNU General Public License for more
14  * details.
15  */
16
17 #ifndef __IOCHANNEL_H__
18 #define __IOCHANNEL_H__
19
20 /*
21  * Everything needed for IOPart-GuestPart communication is define in
22  * this file. Note: Everything is OS-independent because this file is
23  * used by Windows, Linux and possible EFI drivers.
24  *
25  * Communication flow between the IOPart and GuestPart uses the channel headers
26  * channel state. The following states are currently being used:
27  *       UNINIT(All Zeroes), CHANNEL_ATTACHING, CHANNEL_ATTACHED, CHANNEL_OPENED
28  *
29  * Additional states will be used later. No locking is needed to switch between
30  * states due to the following rules:
31  *
32  *      1.  IOPart is only the only partition allowed to change from UNIT
33  *      2.  IOPart is only the only partition allowed to change from
34  *              CHANNEL_ATTACHING
35  *      3.  GuestPart is only the only partition allowed to change from
36  *              CHANNEL_ATTACHED
37  *
38  * The state changes are the following: IOPart sees the channel is in UNINIT,
39  *        UNINIT -> CHANNEL_ATTACHING (performed only by IOPart)
40  *        CHANNEL_ATTACHING -> CHANNEL_ATTACHED (performed only by IOPart)
41  *        CHANNEL_ATTACHED -> CHANNEL_OPENED (performed only by GuestPart)
42  */
43
44 #include <linux/uuid.h>
45 #include <linux/skbuff.h>
46
47 #include "channel.h"
48
49 /*
50  * Must increment these whenever you insert or delete fields within this channel
51  * struct. Also increment whenever you change the meaning of fields within this
52  * channel struct so as to break pre-existing software. Note that you can
53  * usually add fields to the END of the channel struct without needing to
54  * increment this.
55  */
56 #define VISOR_VHBA_CHANNEL_VERSIONID 2
57 #define VISOR_VNIC_CHANNEL_VERSIONID 2
58
59 /*
60  * Everything necessary to handle SCSI & NIC traffic between Guest Partition and
61  * IO Partition is defined below.
62  */
63
64 /*
65  * Define the two queues per data channel between iopart and ioguestparts.
66  *      IOCHAN_TO_IOPART -- used by guest to 'insert' signals to iopart.
67  *      IOCHAN_FROM_IOPART -- used by guest to 'remove' signals from IO part.
68  */
69 #define IOCHAN_TO_IOPART 0
70 #define IOCHAN_FROM_IOPART 1
71
72 /* Size of cdb - i.e., SCSI cmnd */
73 #define MAX_CMND_SIZE 16
74
75 /* Unisys-specific DMA direction values */
76 enum uis_dma_data_direction {
77         UIS_DMA_BIDIRECTIONAL = 0,
78         UIS_DMA_TO_DEVICE = 1,
79         UIS_DMA_FROM_DEVICE = 2,
80         UIS_DMA_NONE = 3
81 };
82
83 #define MAX_SENSE_SIZE 64
84 #define MAX_PHYS_INFO 64
85
86 /*
87  * enum net_types - Various types of network packets that can be sent in cmdrsp.
88  * @NET_RCV_POST:       Submit buffer to hold receiving incoming packet.
89  * @NET_RCV:            visornic -> uisnic. Incoming packet received.
90  * @NET_XMIT:           uisnic -> visornic. For outgoing packet.
91  * @NET_XMIT_DONE:      visornic -> uisnic. Outgoing packet xmitted.
92  * @NET_RCV_ENBDIS:     uisnic -> visornic. Enable/Disable packet reception.
93  * @NET_RCV_ENBDIS_ACK: visornic -> uisnic. Acknowledge enable/disable packet.
94  * @NET_RCV_PROMISC:    uisnic -> visornic. Enable/Disable promiscuous mode.
95  * @NET_CONNECT_STATUS: visornic -> uisnic. Indicate the loss or restoration of
96  *                      a network connection.
97  * @NET_MACADDR:        uisnic -> visornic. Indicates the client has requested
98  *                      to update it's MAC address.
99  * @NET_MACADDR_ACK:    MAC address acknowledge.
100  */
101 enum net_types {
102         NET_RCV_POST = 0,
103         NET_RCV,
104         NET_XMIT,
105         NET_XMIT_DONE,
106         NET_RCV_ENBDIS,
107         NET_RCV_ENBDIS_ACK,
108         /* Reception */
109         NET_RCV_PROMISC,
110         NET_CONNECT_STATUS,
111         NET_MACADDR,
112         NET_MACADDR_ACK,
113 };
114
115 /* Minimum eth data size */
116 #define ETH_MIN_DATA_SIZE 46
117 #define ETH_MIN_PACKET_SIZE (ETH_HLEN + ETH_MIN_DATA_SIZE)
118
119 /* Maximum data size */
120 #define VISOR_ETH_MAX_MTU 16384
121
122 #ifndef MAX_MACADDR_LEN
123 /* Number of bytes in MAC address */
124 #define MAX_MACADDR_LEN 6
125 #endif
126
127 /* Various types of scsi task mgmt commands. */
128 enum task_mgmt_types {
129         TASK_MGMT_ABORT_TASK = 1,
130         TASK_MGMT_BUS_RESET,
131         TASK_MGMT_LUN_RESET,
132         TASK_MGMT_TARGET_RESET,
133 };
134
135 /* Various types of vdisk mgmt commands. */
136 enum vdisk_mgmt_types {
137         VDISK_MGMT_ACQUIRE = 1,
138         VDISK_MGMT_RELEASE,
139 };
140
141 struct phys_info {
142         u64 pi_pfn;
143         u16 pi_off;
144         u16 pi_len;
145 } __packed;
146
147 #define MIN_NUMSIGNALS 64
148
149 /* Structs with pragma pack. */
150
151 struct guest_phys_info {
152         u64 address;
153         u64 length;
154 } __packed;
155
156 /*
157  * struct uisscsi_dest
158  * @channel: Bus number.
159  * @id:      Target number.
160  * @lun:     Logical unit number.
161  */
162 struct uisscsi_dest {
163         u32 channel;
164         u32 id;
165         u32 lun;
166 } __packed;
167
168 struct vhba_wwnn {
169         u32 wwnn1;
170         u32 wwnn2;
171 } __packed;
172
173 /*
174  * struct vhba_config_max
175  * @max_channel: Maximum channel for devices attached to this bus.
176  * @max_id:      Maximum SCSI ID for devices attached to bus.
177  * @max_lun:     Maximum SCSI LUN for devices attached to bus.
178  * @cmd_per_lun: Maximum number of outstanding commands per LUN.
179  * @max_io_size: Maximum io size for devices attached to this bus. Max io size
180  *               is often determined by the resource of the hba.
181  *               e.g Max scatter gather list length * page size / sector size.
182  *
183  * WARNING: Values stored in this structure must contain maximum counts (not
184  * maximum values).
185  *
186  * 20 bytes
187  */
188 struct vhba_config_max {
189         u32 max_channel;
190         u32 max_id;
191         u32 max_lun;
192         u32 cmd_per_lun;
193         u32 max_io_size;
194 } __packed;
195
196 /*
197  * struct uiscmdrsp_scsi
198  *
199  * @handle:             The handle to the cmd that was received. Send it back as
200  *                      is in the rsp packet.
201  * @cmnd:               The cdb for the command.
202  * @bufflen:            Length of data to be transferred out or in.
203  * @guest_phys_entries: Number of entries in scatter-gather list.
204  * @struct gpi_list:    Physical address information for each fragment.
205  * @data_dir:           Direction of the data, if any.
206  * @struct vdest:       Identifies the virtual hba, id, channel, lun to which
207  *                      cmd was sent.
208  * @linuxstat:          Original Linux status used by Linux vdisk.
209  * @scsistat:           The scsi status.
210  * @addlstat:           Non-scsi status.
211  * @sensebuf:           Sense info in case cmd failed. sensebuf holds the
212  *                      sense_data struct. See sense_data struct for more
213  *                      details.
214  * @*vdisk:             Pointer to the vdisk to clean up when IO completes.
215  * @no_disk_result:     Used to return no disk inquiry result when
216  *                      no_disk_result is set to 1
217  *                      scsi.scsistat is SAM_STAT_GOOD
218  *                      scsi.addlstat is 0
219  *                      scsi.linuxstat is SAM_STAT_GOOD
220  *                      That is, there is NO error.
221  */
222 struct uiscmdrsp_scsi {
223         u64 handle;
224         u8 cmnd[MAX_CMND_SIZE];
225         u32 bufflen;
226         u16 guest_phys_entries;
227         struct guest_phys_info gpi_list[MAX_PHYS_INFO];
228         u32 data_dir;
229         struct uisscsi_dest vdest;
230         /* Needed to queue the rsp back to cmd originator. */
231         int linuxstat;
232         u8 scsistat;
233         u8 addlstat;
234 #define ADDL_SEL_TIMEOUT 4
235         /* The following fields are need to determine the result of command. */
236         u8 sensebuf[MAX_SENSE_SIZE];
237         void *vdisk;
238         int no_disk_result;
239 } __packed;
240
241 /*
242  * Defines to support sending correct inquiry result when no disk is
243  * configured.
244  *
245  * From SCSI SPC2 -
246  *
247  * If the target is not capable of supporting a device on this logical unit, the
248  * device server shall set this field to 7Fh (PERIPHERAL QUALIFIER set to 011b
249  * and PERIPHERAL DEVICE TYPE set to 1Fh).
250  *
251  * The device server is capable of supporting the specified peripheral device
252  * type on this logical unit. However, the physical device is not currently
253  * connected to this logical unit.
254  */
255
256 /*
257  * Peripheral qualifier of 0x3
258  * Peripheral type of 0x1f
259  * Specifies no device but target present
260  */
261 #define DEV_NOT_CAPABLE 0x7f
262 /*
263  * Peripheral qualifier of 0x1
264  * Peripheral type of 0 - disk
265  * Specifies device capable, but not present
266  */
267 #define DEV_DISK_CAPABLE_NOT_PRESENT 0x20
268 /* HiSup = 1; shows support for report luns must be returned for lun 0. */
269 #define DEV_HISUPPORT 0x10
270
271 /*
272  * Peripheral qualifier of 0x3
273  * Peripheral type of 0x1f
274  * Specifies no device but target present
275  */
276 #define DEV_NOT_CAPABLE 0x7f
277 /*
278  * Peripheral qualifier of 0x1
279  * Peripheral type of 0 - disk
280  * Specifies device capable, but not present
281  */
282 #define DEV_DISK_CAPABLE_NOT_PRESENT 0x20
283 /* HiSup = 1; shows support for report luns must be returned for lun 0. */
284 #define DEV_HISUPPORT 0x10
285
286 /*
287  * NOTE: Linux code assumes inquiry contains 36 bytes. Without checking length
288  * in buf[4] some Linux code accesses bytes beyond 5 to retrieve vendor, product
289  * and revision. Yikes! So let us always send back 36 bytes, the minimum for
290  * inquiry result.
291  */
292 #define NO_DISK_INQUIRY_RESULT_LEN 36
293 /* 5 bytes minimum for inquiry result */
294 #define MIN_INQUIRY_RESULT_LEN 5
295
296 /* SCSI device version for no disk inquiry result */
297 /* indicates SCSI SPC2 (SPC3 is 5) */
298 #define SCSI_SPC2_VER 4
299
300 /* Struct and Defines to support sense information. */
301
302 /*
303  * The following struct is returned in sensebuf field in uiscmdrsp_scsi. It is
304  * initialized in exactly the manner that is recommended in Windows (hence the
305  * odd values).
306  * When set, these fields will have the following values:
307  * ErrorCode = 0x70             indicates current error
308  * Valid = 1                    indicates sense info is valid
309  * SenseKey                     contains sense key as defined by SCSI specs.
310  * AdditionalSenseCode          contains sense key as defined by SCSI specs.
311  * AdditionalSenseCodeQualifier contains qualifier to sense code as defined by
312  *                              scsi docs.
313  * AdditionalSenseLength        contains will be sizeof(sense_data)-8=10.
314  */
315 struct sense_data {
316         u8 errorcode:7;
317         u8 valid:1;
318         u8 segment_number;
319         u8 sense_key:4;
320         u8 reserved:1;
321         u8 incorrect_length:1;
322         u8 end_of_media:1;
323         u8 file_mark:1;
324         u8 information[4];
325         u8 additional_sense_length;
326         u8 command_specific_information[4];
327         u8 additional_sense_code;
328         u8 additional_sense_code_qualifier;
329         u8 fru_code;
330         u8 sense_key_specific[3];
331 } __packed;
332
333 /*
334  * struct net_pkt_xmt
335  * @len:                    Full length of data in the packet.
336  * @num_frags:              Number of fragments in frags containing data.
337  * @struct phys_info frags: Physical page information.
338  * @ethhdr:                 The ethernet header.
339  * @struct lincsum:         These are needed for csum at uisnic end.
340  *      @valid:     1 = struct is valid - else ignore.
341  *      @hrawoffv:  1 = hwrafoff is valid.
342  *      @nhrawoffv: 1 = nhwrafoff is valid.
343  *      @protocol:  Specifies packet protocol.
344  *      @csum:      Value used to set skb->csum at IOPart.
345  *      @hrawoff:   Value used to set skb->h.raw at IOPart. hrawoff points to
346  *                  the start of the TRANSPORT LAYER HEADER.
347  *      @nhrawoff:  Value used to set skb->nh.raw at IOPart. nhrawoff points to
348  *                  the start of the NETWORK LAYER HEADER.
349  *
350  * NOTE:
351  * The full packet is described in frags but the ethernet header is
352  * separately kept in ethhdr so that uisnic doesn't have "MAP" the
353  * guest memory to get to the header. uisnic needs ethhdr to
354  * determine how to route the packet.
355  */
356 struct net_pkt_xmt {
357         int len;
358         int num_frags;
359         struct phys_info frags[MAX_PHYS_INFO];
360         char ethhdr[ETH_HLEN];
361         struct {
362                 u8 valid;
363                 u8 hrawoffv;
364                 u8 nhrawoffv;
365                 __be16 protocol;
366                 __wsum csum;
367                 u32 hrawoff;
368                 u32 nhrawoff;
369         } lincsum;
370 } __packed;
371
372 struct net_pkt_xmtdone {
373         /* Result of NET_XMIT */
374         u32 xmt_done_result;
375 } __packed;
376
377 /*
378  * RCVPOST_BUF_SIZE must be at most page_size(4096) - cache_line_size (64) The
379  * reason is because dev_skb_alloc which is used to generate RCV_POST skbs in
380  * visornic requires that there is "overhead" in the buffer, and pads 16 bytes.
381  * Use 1 full cache line size for "overhead" so that transfers are optimized.
382  * IOVM requires that a buffer be represented by 1 phys_info structure
383  * which can only cover page_size.
384  */
385 #define RCVPOST_BUF_SIZE 4032
386 #define MAX_NET_RCV_CHAIN \
387         ((VISOR_ETH_MAX_MTU + ETH_HLEN + RCVPOST_BUF_SIZE - 1) \
388          / RCVPOST_BUF_SIZE)
389
390 /* rcv buf size must be large enough to include ethernet data len + ethernet
391  * header len - we are choosing 2K because it is guaranteed to be describable.
392  */
393 struct net_pkt_rcvpost {
394         /* Physical page information for the single fragment 2K rcv buf */
395         struct phys_info frag;
396         /*
397          * Ensures that receive posts are returned to the adapter which we sent
398          * them from originally.
399          */
400         u64 unique_num;
401
402 } __packed;
403
404 /*
405  * struct net_pkt_rcv
406  * @rcv_done_len:       Length of the received data.
407  * @numrcvbufs:         Contains the incoming data. Guest side MUST chain these
408  *                      together.
409  * @*rcvbuf:            List of chained rcvbufa. Each entry is a receive buffer
410  *                      provided by NET_RCV_POST. NOTE: First rcvbuf in the
411  *                      chain will also be provided in net.buf.
412  * @unique_num:
413  * @rcvs_dropped_delta:
414  *
415  * The number of rcvbuf that can be chained is based on max mtu and size of each
416  * rcvbuf.
417  */
418 struct net_pkt_rcv {
419         u32 rcv_done_len;
420         u8 numrcvbufs;
421         void *rcvbuf[MAX_NET_RCV_CHAIN];
422         u64 unique_num;
423         u32 rcvs_dropped_delta;
424 } __packed;
425
426 struct net_pkt_enbdis {
427         void *context;
428         /* 1 = enable, 0 = disable */
429         u16 enable;
430 } __packed;
431
432 struct net_pkt_macaddr {
433         void *context;
434         /* 6 bytes */
435         u8 macaddr[MAX_MACADDR_LEN];
436 } __packed;
437
438 /*
439  * struct uiscmdrsp_net - cmd rsp packet used for VNIC network traffic.
440  * @enum type:
441  * @*buf:
442  * @union:
443  *      @struct xmt:     Used for NET_XMIT.
444  *      @struct xmtdone: Used for NET_XMIT_DONE.
445  *      @struct rcvpost: Used for NET_RCV_POST.
446  *      @struct rcv:     Used for NET_RCV.
447  *      @struct enbdis:  Used for NET_RCV_ENBDIS, NET_RCV_ENBDIS_ACK,
448  *                       NET_RCV_PROMSIC, and NET_CONNECT_STATUS.
449  *      @struct macaddr:
450  */
451 struct uiscmdrsp_net {
452         enum net_types type;
453         void *buf;
454         union {
455                 struct net_pkt_xmt xmt;
456                 struct net_pkt_xmtdone xmtdone;
457                 struct net_pkt_rcvpost rcvpost;
458                 struct net_pkt_rcv rcv;
459                 struct net_pkt_enbdis enbdis;
460                 struct net_pkt_macaddr macaddr;
461         };
462 } __packed;
463
464 /*
465  * struct uiscmdrsp_scsitaskmgmt
466  * @enum tasktype:       The type of task.
467  * @struct vdest:        The vdisk for which this task mgmt is generated.
468  * @handle:              This is a handle that the guest has saved off for its
469  *                       own use. The handle value is preserved by iopart and
470  *                       returned as in task mgmt rsp.
471  * @notify_handle:       For Linux guests, this is a pointer to wait_queue_head
472  *                       that a thread is waiting on to see if the taskmgmt
473  *                       command has completed. When the rsp is received by
474  *                       guest, the thread receiving the response uses this to
475  *                       notify the thread waiting for taskmgmt command
476  *                       completion. It's value is preserved by iopart and
477  *                       returned as in the task mgmt rsp.
478  * @notifyresult_handle: This is a handle to the location in the guest where
479  *                       the result of the taskmgmt command (result field) is
480  *                       saved to when the response is handled. It's value is
481  *                       preserved by iopart and returned as is in the task mgmt
482  *                       rsp.
483  * @result:              Result of taskmgmt command - set by IOPart.
484  */
485 struct uiscmdrsp_scsitaskmgmt {
486         enum task_mgmt_types tasktype;
487         struct uisscsi_dest vdest;
488         u64 handle;
489         u64 notify_handle;
490         u64 notifyresult_handle;
491         char result;
492
493 #define TASK_MGMT_FAILED 0
494 } __packed;
495
496 /*
497  * struct uiscmdrsp_disknotify - Used by uissd to send disk add/remove
498  *                               notifications to Guest.
499  * @add:     0-remove, 1-add.
500  * @*v_hba:  Channel info to route msg.
501  * @channel: SCSI Path of Disk to added or removed.
502  * @id:      SCSI Path of Disk to added or removed.
503  * @lun:     SCSI Path of Disk to added or removed.
504  *
505  * Note that the vHba pointer is not used by the Client/Guest side.
506  */
507 struct uiscmdrsp_disknotify {
508         u8 add;
509         void *v_hba;
510         u32 channel, id, lun;
511 } __packed;
512
513 /* Keeping cmd and rsp info in one structure for now cmd rsp packet for SCSI */
514 struct uiscmdrsp {
515         char cmdtype;
516         /* Describes what type of information is in the struct */
517 #define CMD_SCSI_TYPE         1
518 #define CMD_NET_TYPE          2
519 #define CMD_SCSITASKMGMT_TYPE 3
520 #define CMD_NOTIFYGUEST_TYPE  4
521         union {
522                 struct uiscmdrsp_scsi scsi;
523                 struct uiscmdrsp_net net;
524                 struct uiscmdrsp_scsitaskmgmt scsitaskmgmt;
525                 struct uiscmdrsp_disknotify disknotify;
526         };
527         /* Send the response when the cmd is done (scsi and scsittaskmgmt). */
528         void *private_data;
529         /* General Purpose Queue Link */
530         struct uiscmdrsp *next;
531         /* Pointer to the nextactive commands */
532         struct uiscmdrsp *activeQ_next;
533         /* Pointer to the prevactive commands */
534         struct uiscmdrsp *activeQ_prev;
535 } __packed;
536
537 /* total = 28 bytes */
538 struct iochannel_vhba {
539         /* 8 bytes */
540         struct vhba_wwnn wwnn;
541         /* 20 bytes */
542         struct vhba_config_max max;
543 } __packed;
544
545 struct iochannel_vnic {
546         /* 6 bytes */
547         u8 macaddr[6];
548         /* 4 bytes */
549         u32 num_rcv_bufs;
550         /* 4 bytes */
551         u32 mtu;
552         /* 16 bytes */
553         guid_t zone_guid;
554 } __packed;
555
556 /*
557  * This is just the header of the IO channel. It is assumed that directly after
558  * this header there is a large region of memory which contains the command and
559  * response queues as specified in cmd_q and rsp_q SIGNAL_QUEUE_HEADERS.
560  */
561 struct visor_io_channel {
562         struct channel_header channel_header;
563         struct signal_queue_header cmd_q;
564         struct signal_queue_header rsp_q;
565         union {
566                 struct iochannel_vhba vhba;
567                 struct iochannel_vnic vnic;
568         } __packed;
569
570 #define MAX_CLIENTSTRING_LEN 1024
571         /* client_string is NULL termimated so holds max-1 bytes */
572          u8 client_string[MAX_CLIENTSTRING_LEN];
573 } __packed;
574
575 /* INLINE functions for initializing and accessing I/O data channels. */
576 #define SIZEOF_CMDRSP (64 * DIV_ROUND_UP(sizeof(struct uiscmdrsp), 64))
577
578 /* Use 4K page sizes when passing page info between Guest and IOPartition. */
579 #define PI_PAGE_SIZE 0x1000
580 #define PI_PAGE_MASK 0x0FFF
581
582 /* __IOCHANNEL_H__ */
583 #endif