ctl_io.h revision 288770
1/*-
2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions, and the following disclaimer,
10 *    without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 *    substantially similar to the "NO WARRANTY" disclaimer below
13 *    ("Disclaimer") and any redistribution must be conditioned upon
14 *    including a substantially similar Disclaimer requirement for further
15 *    binary redistribution.
16 *
17 * NO WARRANTY
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_io.h#5 $
31 * $FreeBSD: stable/10/sys/cam/ctl/ctl_io.h 288770 2015-10-05 10:37:20Z mav $
32 */
33/*
34 * CAM Target Layer data movement structures/interface.
35 *
36 * Author: Ken Merry <ken@FreeBSD.org>
37 */
38
39#ifndef	_CTL_IO_H_
40#define	_CTL_IO_H_
41
42#ifdef _CTL_C
43#define EXTERN(__var,__val) __var = __val
44#else
45#define EXTERN(__var,__val) extern __var
46#endif
47
48#define	CTL_MAX_CDBLEN	32
49/*
50 * Uncomment this next line to enable printing out times for I/Os
51 * that take longer than CTL_TIME_IO_SECS seconds to get to the datamove
52 * and/or done stage.
53 */
54#define	CTL_TIME_IO
55#ifdef  CTL_TIME_IO
56#define	CTL_TIME_IO_DEFAULT_SECS	90
57EXTERN(int ctl_time_io_secs, CTL_TIME_IO_DEFAULT_SECS);
58#endif
59
60/*
61 * Uncomment this next line to enable the CTL I/O delay feature.  You
62 * can delay I/O at two different points -- datamove and done.  This is
63 * useful for diagnosing abort conditions (for hosts that send an abort on a
64 * timeout), and for determining how long a host's timeout is.
65 */
66//#define	CTL_IO_DELAY
67
68typedef enum {
69	CTL_STATUS_NONE,	/* No status */
70	CTL_SUCCESS,		/* Transaction completed successfully */
71	CTL_CMD_TIMEOUT,	/* Command timed out, shouldn't happen here */
72	CTL_SEL_TIMEOUT,	/* Selection timeout, shouldn't happen here */
73	CTL_ERROR,		/* General CTL error XXX expand on this? */
74	CTL_SCSI_ERROR,		/* SCSI error, look at status byte/sense data */
75	CTL_CMD_ABORTED,	/* Command aborted, don't return status */
76	CTL_STATUS_MASK = 0xfff,/* Mask off any status flags */
77	CTL_AUTOSENSE = 0x1000	/* Autosense performed */
78} ctl_io_status;
79
80/*
81 * WARNING:  Keep the data in/out/none flags where they are.  They're used
82 * in conjuction with ctl_cmd_flags.  See comment above ctl_cmd_flags
83 * definition in ctl_private.h.
84 */
85typedef enum {
86	CTL_FLAG_NONE		= 0x00000000,	/* no flags */
87	CTL_FLAG_DATA_IN	= 0x00000001,	/* DATA IN */
88	CTL_FLAG_DATA_OUT	= 0x00000002,	/* DATA OUT */
89	CTL_FLAG_DATA_NONE	= 0x00000003,	/* no data */
90	CTL_FLAG_DATA_MASK	= 0x00000003,
91	CTL_FLAG_KDPTR_SGLIST	= 0x00000008, 	/* kern_data_ptr is S/G list*/
92	CTL_FLAG_EDPTR_SGLIST	= 0x00000010,	/* ext_data_ptr is S/G list */
93	CTL_FLAG_DO_AUTOSENSE	= 0x00000020,	/* grab sense info */
94	CTL_FLAG_USER_REQ	= 0x00000040,	/* request came from userland */
95	CTL_FLAG_ALLOCATED	= 0x00000100,	/* data space allocated */
96	CTL_FLAG_BLOCKED	= 0x00000200,	/* on the blocked queue */
97	CTL_FLAG_ABORT_STATUS	= 0x00000400,	/* return TASK ABORTED status */
98	CTL_FLAG_ABORT		= 0x00000800,	/* this I/O should be aborted */
99	CTL_FLAG_DMA_INPROG	= 0x00001000,	/* DMA in progress */
100	CTL_FLAG_DELAY_DONE	= 0x00004000,	/* delay injection done */
101	CTL_FLAG_INT_COPY	= 0x00008000,	/* internal copy, no done call*/
102	CTL_FLAG_SENT_2OTHER_SC	= 0x00010000,
103	CTL_FLAG_FROM_OTHER_SC	= 0x00020000,
104	CTL_FLAG_IS_WAS_ON_RTR  = 0x00040000,	/* Don't rerun cmd on failover*/
105	CTL_FLAG_BUS_ADDR	= 0x00080000,	/* ctl_sglist contains BUS
106						   addresses, not virtual ones*/
107	CTL_FLAG_IO_CONT	= 0x00100000,	/* Continue I/O instead of
108						   completing */
109#if 0
110	CTL_FLAG_ALREADY_DONE	= 0x00200000	/* I/O already completed */
111#endif
112	CTL_FLAG_NO_DATAMOVE	= 0x00400000,
113	CTL_FLAG_DMA_QUEUED	= 0x00800000,	/* DMA queued but not started*/
114	CTL_FLAG_STATUS_QUEUED	= 0x01000000,	/* Status queued but not sent*/
115
116	CTL_FLAG_FAILOVER	= 0x04000000,	/* Killed by a failover */
117	CTL_FLAG_IO_ACTIVE	= 0x08000000,	/* I/O active on this SC */
118	CTL_FLAG_STATUS_SENT	= 0x10000000	/* Status sent by datamove */
119} ctl_io_flags;
120
121
122struct ctl_lba_len {
123	uint64_t lba;
124	uint32_t len;
125};
126
127struct ctl_lba_len_flags {
128	uint64_t lba;
129	uint32_t len;
130	uint32_t flags;
131#define CTL_LLF_FUA	0x04000000
132#define CTL_LLF_DPO	0x08000000
133#define CTL_LLF_READ	0x10000000
134#define CTL_LLF_WRITE	0x20000000
135#define CTL_LLF_VERIFY	0x40000000
136#define CTL_LLF_COMPARE	0x80000000
137};
138
139struct ctl_ptr_len_flags {
140	uint8_t *ptr;
141	uint32_t len;
142	uint32_t flags;
143};
144
145union ctl_priv {
146	uint8_t		bytes[sizeof(uint64_t) * 2];
147	uint64_t	integer;
148	void		*ptr;
149};
150
151/*
152 * Number of CTL private areas.
153 */
154#define	CTL_NUM_PRIV	6
155
156/*
157 * Which private area are we using for a particular piece of data?
158 */
159#define	CTL_PRIV_LUN		0	/* CTL LUN pointer goes here */
160#define	CTL_PRIV_LBA_LEN	1	/* Decoded LBA/len for read/write*/
161#define	CTL_PRIV_MODEPAGE	1	/* Modepage info for config write */
162#define	CTL_PRIV_BACKEND	2	/* Reserved for block, RAIDCore */
163#define	CTL_PRIV_BACKEND_LUN	3	/* Backend LUN pointer */
164#define	CTL_PRIV_FRONTEND	4	/* Frontend storage */
165#define	CTL_PRIV_FRONTEND2	5	/* Another frontend storage */
166
167#define CTL_INVALID_PORTNAME 0xFF
168#define CTL_UNMAPPED_IID     0xFF
169
170struct ctl_sg_entry {
171	void	*addr;
172	size_t	len;
173};
174
175typedef enum {
176	CTL_IO_NONE,
177	CTL_IO_SCSI,
178	CTL_IO_TASK,
179} ctl_io_type;
180
181struct ctl_nexus {
182	uint32_t initid;		/* Initiator ID */
183	uint32_t targ_port;		/* Target port, filled in by PORT */
184	uint32_t targ_lun;		/* Destination lun */
185	uint32_t targ_mapped_lun;	/* Destination lun CTL-wide */
186};
187
188typedef enum {
189	CTL_MSG_SERIALIZE,
190	CTL_MSG_R2R,
191	CTL_MSG_FINISH_IO,
192	CTL_MSG_BAD_JUJU,
193	CTL_MSG_MANAGE_TASKS,
194	CTL_MSG_PERS_ACTION,
195	CTL_MSG_DATAMOVE,
196	CTL_MSG_DATAMOVE_DONE,
197	CTL_MSG_UA,			/* Set/clear UA on secondary. */
198	CTL_MSG_PORT_SYNC,		/* Information about port. */
199	CTL_MSG_LUN_SYNC,		/* Information about LUN. */
200	CTL_MSG_IID_SYNC,		/* Information about initiator. */
201	CTL_MSG_FAILOVER		/* Fake, never sent though the wire */
202} ctl_msg_type;
203
204struct ctl_scsiio;
205
206struct ctl_io_hdr {
207	uint32_t	  version;	/* interface version XXX */
208	ctl_io_type	  io_type;	/* task I/O, SCSI I/O, etc. */
209	ctl_msg_type	  msg_type;
210	struct ctl_nexus  nexus;	/* Initiator, port, target, lun */
211	uint32_t	  iid_indx;	/* the index into the iid mapping */
212	uint32_t	  flags;	/* transaction flags */
213	uint32_t	  status;	/* transaction status */
214	uint32_t	  port_status;	/* trans status, set by PORT, 0 = good*/
215	uint32_t	  timeout;	/* timeout in ms */
216	uint32_t	  retries;	/* retry count */
217#ifdef CTL_IO_DELAY
218	struct callout	  delay_callout;
219#endif /* CTL_IO_DELAY */
220#ifdef CTL_TIME_IO
221	time_t		  start_time;	/* I/O start time */
222	struct bintime	  start_bt;	/* Timer start ticks */
223	struct bintime	  dma_start_bt;	/* DMA start ticks */
224	struct bintime	  dma_bt;	/* DMA total ticks */
225	uint32_t	  num_dmas;	/* Number of DMAs */
226#endif /* CTL_TIME_IO */
227	union ctl_io	  *original_sc;
228	union ctl_io	  *serializing_sc;
229	void		  *pool;	/* I/O pool */
230	union ctl_priv	  ctl_private[CTL_NUM_PRIV];/* CTL private area */
231	struct ctl_sg_entry *remote_sglist;
232	struct ctl_sg_entry *local_sglist;
233	STAILQ_ENTRY(ctl_io_hdr) links;	/* linked list pointer */
234	TAILQ_ENTRY(ctl_io_hdr) ooa_links;
235	TAILQ_ENTRY(ctl_io_hdr) blocked_links;
236};
237
238typedef enum {
239	CTL_TAG_UNTAGGED,
240	CTL_TAG_SIMPLE,
241	CTL_TAG_ORDERED,
242	CTL_TAG_HEAD_OF_QUEUE,
243	CTL_TAG_ACA
244} ctl_tag_type;
245
246union ctl_io;
247
248/*
249 * SCSI passthrough I/O structure for the CAM Target Layer.  Note
250 * that some of these fields are here for completeness, but they aren't
251 * used in the CTL implementation.  e.g., timeout and retries won't be
252 * used.
253 *
254 * Note:  Make sure the io_hdr is *always* the first element in this
255 * structure.
256 */
257struct ctl_scsiio {
258	struct ctl_io_hdr io_hdr;	/* common to all I/O types */
259
260	/*
261	 * The ext_* fields are generally intended for frontend use; CTL itself
262	 * doesn't modify or use them.
263	 */
264	uint32_t   ext_sg_entries;	/* 0 = no S/G list, > 0 = num entries */
265	uint8_t	   *ext_data_ptr;	/* data buffer or S/G list */
266	uint32_t   ext_data_len;	/* Data transfer length */
267	uint32_t   ext_data_filled;	/* Amount of data filled so far */
268
269	/*
270	 * The number of scatter/gather entries in the list pointed to
271	 * by kern_data_ptr.  0 means there is no list, just a data pointer.
272	 */
273	uint32_t   kern_sg_entries;
274
275	uint32_t   rem_sg_entries;	/* Unused. */
276
277	/*
278	 * The data pointer or a pointer to the scatter/gather list.
279	 */
280	uint8_t    *kern_data_ptr;
281
282	/*
283	 * Length of the data buffer or scatter/gather list.  It's also
284	 * the length of this particular piece of the data transfer,
285	 * ie. number of bytes expected to be transferred by the current
286	 * invocation of frontend's datamove() callback.  It's always
287	 * less than or equal to kern_total_len.
288	 */
289	uint32_t   kern_data_len;
290
291	/*
292	 * Total length of data to be transferred during this particular
293	 * SCSI command, as decoded from SCSI CDB.
294	 */
295	uint32_t   kern_total_len;
296
297	/*
298	 * Amount of data left after the current data transfer.
299	 */
300	uint32_t   kern_data_resid;
301
302	/*
303	 * Byte offset of this transfer, equal to the amount of data
304	 * already transferred for this SCSI command during previous
305	 * datamove() invocations.
306	 */
307	uint32_t   kern_rel_offset;
308
309	struct     scsi_sense_data sense_data;	/* sense data */
310	uint8_t	   sense_len;		/* Returned sense length */
311	uint8_t	   scsi_status;		/* SCSI status byte */
312	uint8_t	   sense_residual;	/* Unused. */
313	uint32_t   residual;		/* data residual length */
314	uint32_t   tag_num;		/* tag number */
315	ctl_tag_type tag_type;		/* simple, ordered, head of queue,etc.*/
316	uint8_t    cdb_len;		/* CDB length */
317	uint8_t	   cdb[CTL_MAX_CDBLEN];	/* CDB */
318	int	   (*be_move_done)(union ctl_io *io); /* called by fe */
319	int        (*io_cont)(union ctl_io *io); /* to continue processing */
320};
321
322typedef enum {
323	CTL_TASK_ABORT_TASK,
324	CTL_TASK_ABORT_TASK_SET,
325	CTL_TASK_CLEAR_ACA,
326	CTL_TASK_CLEAR_TASK_SET,
327	CTL_TASK_I_T_NEXUS_RESET,
328	CTL_TASK_LUN_RESET,
329	CTL_TASK_TARGET_RESET,
330	CTL_TASK_BUS_RESET,
331	CTL_TASK_PORT_LOGIN,
332	CTL_TASK_PORT_LOGOUT,
333	CTL_TASK_QUERY_TASK,
334	CTL_TASK_QUERY_TASK_SET,
335	CTL_TASK_QUERY_ASYNC_EVENT
336} ctl_task_type;
337
338typedef enum {
339	CTL_TASK_FUNCTION_COMPLETE,
340	CTL_TASK_FUNCTION_SUCCEEDED,
341	CTL_TASK_FUNCTION_REJECTED,
342	CTL_TASK_LUN_DOES_NOT_EXIST,
343	CTL_TASK_FUNCTION_NOT_SUPPORTED
344} ctl_task_status;
345
346/*
347 * Task management I/O structure.  Aborts, bus resets, etc., are sent using
348 * this structure.
349 *
350 * Note:  Make sure the io_hdr is *always* the first element in this
351 * structure.
352 */
353struct ctl_taskio {
354	struct ctl_io_hdr	io_hdr;      /* common to all I/O types */
355	ctl_task_type		task_action; /* Target Reset, Abort, etc.  */
356	uint32_t		tag_num;     /* tag number */
357	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
358	uint8_t			task_status; /* Complete, Succeeded, etc. */
359	uint8_t			task_resp[3];/* Response information */
360};
361
362typedef enum {
363	CTL_PR_REG_KEY,
364	CTL_PR_UNREG_KEY,
365	CTL_PR_PREEMPT,
366	CTL_PR_CLEAR,
367	CTL_PR_RESERVE,
368	CTL_PR_RELEASE
369} ctl_pr_action;
370
371/*
372 * The PR info is specifically for sending Persistent Reserve actions
373 * to the other SC which it must also act on.
374 *
375 * Note:  Make sure the io_hdr is *always* the first element in this
376 * structure.
377 */
378struct ctl_pr_info {
379	ctl_pr_action        action;
380	uint8_t              sa_res_key[8];
381	uint8_t              res_type;
382	uint32_t             residx;
383};
384
385struct ctl_ha_msg_hdr {
386	ctl_msg_type		msg_type;
387	union ctl_io		*original_sc;
388	union ctl_io		*serializing_sc;
389	struct ctl_nexus	nexus;	     /* Initiator, port, target, lun */
390	uint32_t		status;	     /* transaction status */
391};
392
393#define	CTL_HA_MAX_SG_ENTRIES	16
394#define	CTL_HA_DATAMOVE_SEGMENT	131072
395
396/*
397 * Used for CTL_MSG_PERS_ACTION.
398 */
399struct ctl_ha_msg_pr {
400	struct ctl_ha_msg_hdr	hdr;
401	struct ctl_pr_info	pr_info;
402};
403
404/*
405 * Used for CTL_MSG_UA.
406 */
407struct ctl_ha_msg_ua {
408	struct ctl_ha_msg_hdr	hdr;
409	int			ua_all;
410	int			ua_set;
411	int			ua_type;
412	uint8_t			ua_info[8];
413};
414
415/*
416 * The S/G handling here is a little different than the standard ctl_scsiio
417 * structure, because we can't pass data by reference in between controllers.
418 * The S/G list in the ctl_scsiio struct is normally passed in the
419 * kern_data_ptr field.  So kern_sg_entries here will always be non-zero,
420 * even if there is only one entry.
421 *
422 * Used for CTL_MSG_DATAMOVE.
423 */
424struct ctl_ha_msg_dt {
425	struct ctl_ha_msg_hdr	hdr;
426	ctl_io_flags		flags;  /* Only I/O flags are used here */
427	uint32_t		sg_sequence;     /* S/G portion number  */
428	uint8_t			sg_last;         /* last S/G batch = 1 */
429	uint32_t		sent_sg_entries; /* previous S/G count */
430	uint32_t		cur_sg_entries;  /* current S/G entries */
431	uint32_t		kern_sg_entries; /* total S/G entries */
432	uint32_t		kern_data_len;   /* Length of this S/G list */
433	uint32_t		kern_total_len;  /* Total length of this
434						    transaction */
435	uint32_t		kern_data_resid; /* Length left to transfer
436						    after this*/
437	uint32_t		kern_rel_offset; /* Byte Offset of this
438						    transfer */
439	struct ctl_sg_entry	sg_list[CTL_HA_MAX_SG_ENTRIES];
440};
441
442/*
443 * Used for CTL_MSG_SERIALIZE, CTL_MSG_FINISH_IO, CTL_MSG_BAD_JUJU.
444 */
445struct ctl_ha_msg_scsi {
446	struct ctl_ha_msg_hdr	hdr;
447	uint32_t		tag_num;     /* tag number */
448	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
449	uint8_t			cdb[CTL_MAX_CDBLEN];	/* CDB */
450	uint8_t			cdb_len;	/* CDB length */
451	uint8_t			scsi_status; /* SCSI status byte */
452	uint8_t			sense_len;   /* Returned sense length */
453	uint8_t			sense_residual;	/* sense residual length */
454	uint32_t		residual;    /* data residual length */
455	uint32_t		fetd_status; /* trans status, set by FETD,
456						0 = good*/
457	struct scsi_sense_data	sense_data;  /* sense data */
458};
459
460/*
461 * Used for CTL_MSG_MANAGE_TASKS.
462 */
463struct ctl_ha_msg_task {
464	struct ctl_ha_msg_hdr	hdr;
465	ctl_task_type		task_action; /* Target Reset, Abort, etc.  */
466	uint32_t		tag_num;     /* tag number */
467	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
468};
469
470/*
471 * Used for CTL_MSG_PORT_SYNC.
472 */
473struct ctl_ha_msg_port {
474	struct ctl_ha_msg_hdr	hdr;
475	int			port_type;
476	int			physical_port;
477	int			virtual_port;
478	int			status;
479	int			name_len;
480	int			lun_map_len;
481	int			port_devid_len;
482	int			target_devid_len;
483	int			init_devid_len;
484	uint8_t			data[];
485};
486
487/*
488 * Used for CTL_MSG_LUN_SYNC.
489 */
490struct ctl_ha_msg_lun {
491	struct ctl_ha_msg_hdr	hdr;
492	int			flags;
493	unsigned int		pr_generation;
494	uint32_t		pr_res_idx;
495	uint8_t			pr_res_type;
496	int			lun_devid_len;
497	int			pr_key_count;
498	uint8_t			data[];
499};
500
501struct ctl_ha_msg_lun_pr_key {
502	uint32_t		pr_iid;
503	uint64_t		pr_key;
504};
505
506/*
507 * Used for CTL_MSG_IID_SYNC.
508 */
509struct ctl_ha_msg_iid {
510	struct ctl_ha_msg_hdr	hdr;
511	int			in_use;
512	int			name_len;
513	uint64_t		wwpn;
514	uint8_t			data[];
515};
516
517union ctl_ha_msg {
518	struct ctl_ha_msg_hdr	hdr;
519	struct ctl_ha_msg_task	task;
520	struct ctl_ha_msg_scsi	scsi;
521	struct ctl_ha_msg_dt	dt;
522	struct ctl_ha_msg_pr	pr;
523	struct ctl_ha_msg_ua	ua;
524	struct ctl_ha_msg_port	port;
525	struct ctl_ha_msg_lun	lun;
526	struct ctl_ha_msg_iid	iid;
527};
528
529
530struct ctl_prio {
531	struct ctl_io_hdr  io_hdr;
532	struct ctl_ha_msg_pr pr_msg;
533};
534
535
536
537union ctl_io {
538	struct ctl_io_hdr io_hdr;	/* common to all I/O types */
539	struct ctl_scsiio scsiio;	/* Normal SCSI commands */
540	struct ctl_taskio taskio;	/* SCSI task management/reset */
541	struct ctl_prio   presio;	/* update per. res info on other SC */
542};
543
544#ifdef _KERNEL
545
546union ctl_io *ctl_alloc_io(void *pool_ref);
547union ctl_io *ctl_alloc_io_nowait(void *pool_ref);
548void ctl_free_io(union ctl_io *io);
549void ctl_zero_io(union ctl_io *io);
550void ctl_copy_io(union ctl_io *src, union ctl_io *dest);
551
552#endif /* _KERNEL */
553
554#endif	/* _CTL_IO_H_ */
555
556/*
557 * vim: ts=8
558 */
559