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