ctl_io.h revision 265634
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 265634 2014-05-08 07:00:45Z 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 these next two lines 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#define	CTL_TIMER_BYTES		sizeof(struct callout)
68
69typedef enum {
70	CTL_STATUS_NONE,	/* No status */
71	CTL_SUCCESS,		/* Transaction completed successfully */
72	CTL_CMD_TIMEOUT,	/* Command timed out, shouldn't happen here */
73	CTL_SEL_TIMEOUT,	/* Selection timeout, shouldn't happen here */
74	CTL_ERROR,		/* General CTL error XXX expand on this? */
75	CTL_SCSI_ERROR,		/* SCSI error, look at status byte/sense data */
76	CTL_CMD_ABORTED,	/* Command aborted, don't return status */
77	CTL_STATUS_MASK = 0xfff,/* Mask off any status flags */
78	CTL_AUTOSENSE = 0x1000	/* Autosense performed */
79} ctl_io_status;
80
81/*
82 * WARNING:  Keep the data in/out/none flags where they are.  They're used
83 * in conjuction with ctl_cmd_flags.  See comment above ctl_cmd_flags
84 * definition in ctl_private.h.
85 */
86typedef enum {
87	CTL_FLAG_NONE		= 0x00000000,	/* no flags */
88	CTL_FLAG_DATA_IN	= 0x00000001,	/* DATA IN */
89	CTL_FLAG_DATA_OUT	= 0x00000002,	/* DATA OUT */
90	CTL_FLAG_DATA_NONE	= 0x00000003,	/* no data */
91	CTL_FLAG_DATA_MASK	= 0x00000003,
92	CTL_FLAG_KDPTR_SGLIST	= 0x00000008, 	/* kern_data_ptr is S/G list*/
93	CTL_FLAG_EDPTR_SGLIST	= 0x00000010,	/* ext_data_ptr is S/G list */
94	CTL_FLAG_DO_AUTOSENSE	= 0x00000020,	/* grab sense info */
95	CTL_FLAG_USER_REQ	= 0x00000040,	/* request came from userland */
96	CTL_FLAG_CONTROL_DEV	= 0x00000080,	/* processor device */
97	CTL_FLAG_ALLOCATED	= 0x00000100,	/* data space allocated */
98	CTL_FLAG_BLOCKED	= 0x00000200,	/* on the blocked queue */
99	CTL_FLAG_ABORT		= 0x00000800,	/* this I/O should be aborted */
100	CTL_FLAG_DMA_INPROG	= 0x00001000,	/* DMA in progress */
101	CTL_FLAG_NO_DATASYNC	= 0x00002000,	/* don't cache flush data */
102	CTL_FLAG_DELAY_DONE	= 0x00004000,	/* delay injection done */
103	CTL_FLAG_INT_COPY	= 0x00008000,	/* internal copy, no done call*/
104	CTL_FLAG_SENT_2OTHER_SC	= 0x00010000,
105	CTL_FLAG_FROM_OTHER_SC	= 0x00020000,
106	CTL_FLAG_IS_WAS_ON_RTR  = 0x00040000,	/* Don't rerun cmd on failover*/
107	CTL_FLAG_BUS_ADDR	= 0x00080000,	/* ctl_sglist contains BUS
108						   addresses, not virtual ones*/
109	CTL_FLAG_IO_CONT	= 0x00100000,	/* Continue I/O instead of
110						   completing */
111	CTL_FLAG_AUTO_MIRROR	= 0x00200000,	/* Automatically use memory
112						   from the RC cache mirrored
113						   address area. */
114#if 0
115	CTL_FLAG_ALREADY_DONE	= 0x00200000	/* I/O already completed */
116#endif
117	CTL_FLAG_NO_DATAMOVE	= 0x00400000,
118	CTL_FLAG_DMA_QUEUED	= 0x00800000,	/* DMA queued but not started*/
119	CTL_FLAG_STATUS_QUEUED	= 0x01000000,	/* Status queued but not sent*/
120
121	CTL_FLAG_REDIR_DONE	= 0x02000000,	/* Redirection has already
122						   been done. */
123	CTL_FLAG_FAILOVER	= 0x04000000,	/* Killed by a failover */
124	CTL_FLAG_IO_ACTIVE	= 0x08000000,	/* I/O active on this SC */
125	CTL_FLAG_RDMA_MASK	= CTL_FLAG_NO_DATASYNC | CTL_FLAG_BUS_ADDR |
126				  CTL_FLAG_AUTO_MIRROR | CTL_FLAG_REDIR_DONE
127						/* Flags we care about for
128						   remote DMA */
129} ctl_io_flags;
130
131
132struct ctl_lba_len {
133	uint64_t lba;
134	uint32_t len;
135};
136
137struct ctl_lba_len_flags {
138	uint64_t lba;
139	uint32_t len;
140	uint32_t flags;
141};
142
143struct ctl_ptr_len_flags {
144	uint8_t *ptr;
145	uint32_t len;
146	uint32_t flags;
147};
148
149union ctl_priv {
150	uint8_t		bytes[sizeof(uint64_t) * 2];
151	uint64_t	integer;
152	void		*ptr;
153};
154
155/*
156 * Number of CTL private areas.
157 */
158#define	CTL_NUM_PRIV	6
159
160/*
161 * Which private area are we using for a particular piece of data?
162 */
163#define	CTL_PRIV_LUN		0	/* CTL LUN pointer goes here */
164#define	CTL_PRIV_LBA_LEN	1	/* Decoded LBA/len for read/write*/
165#define	CTL_PRIV_MODEPAGE	1	/* Modepage info for config write */
166#define	CTL_PRIV_BACKEND	2	/* Reserved for block, RAIDCore */
167#define	CTL_PRIV_BACKEND_LUN	3	/* Backend LUN pointer */
168#define	CTL_PRIV_FRONTEND	4	/* LSI driver, ioctl front end */
169#define	CTL_PRIV_USER		5	/* Userland use */
170
171#define CTL_INVALID_PORTNAME 0xFF
172#define CTL_UNMAPPED_IID     0xFF
173/*
174 * XXX KDM this size is for the port_priv variable in struct ctl_io_hdr
175 * below.  This should be defined in terms of the size of struct
176 * ctlfe_lun_cmd_info at the moment:
177 * struct ctlfe_lun_cmd_info {
178 *	int cur_transfer_index;
179 * 	ctlfe_cmd_flags flags;
180 * 	bus_dma_segment_t cam_sglist[32];
181 * };
182 *
183 * This isn't really the way I'd prefer to do it, but it does make some
184 * sense, AS LONG AS we can guarantee that there will always only be one
185 * outstanding DMA request per ctl_io.  If that assumption isn't valid,
186 * then we've got problems.
187 *
188 * At some point it may be nice switch CTL over to using CCBs for
189 * everything.  At that point we can probably use the ATIO/CTIO model, so
190 * that multiple simultaneous DMAs per command will just work.
191 *
192 * Also note that the current size, 600, is appropriate for 64-bit
193 * architectures, but is overkill for 32-bit architectures.  Need a way to
194 * figure out the size at compile time, or just get rid of this altogether.
195 */
196#define	CTL_PORT_PRIV_SIZE	600
197
198struct ctl_sg_entry {
199	void	*addr;
200	size_t	len;
201};
202
203struct ctl_id {
204	uint32_t		id;
205	uint64_t		wwid[2];
206};
207
208typedef enum {
209	CTL_IO_NONE,
210	CTL_IO_SCSI,
211	CTL_IO_TASK,
212} ctl_io_type;
213
214struct ctl_nexus {
215	struct ctl_id initid;		/* Initiator ID */
216	uint32_t targ_port;		/* Target port, filled in by PORT */
217	struct ctl_id targ_target;	/* Destination target */
218	uint32_t targ_lun;		/* Destination lun */
219	uint32_t (*lun_map_fn)(void *arg, uint32_t lun);
220	void *lun_map_arg;
221};
222
223typedef enum {
224	CTL_MSG_SERIALIZE,
225	CTL_MSG_R2R,
226	CTL_MSG_FINISH_IO,
227	CTL_MSG_BAD_JUJU,
228	CTL_MSG_MANAGE_TASKS,
229	CTL_MSG_PERS_ACTION,
230	CTL_MSG_SYNC_FE,
231	CTL_MSG_APS_LOCK,
232	CTL_MSG_DATAMOVE,
233	CTL_MSG_DATAMOVE_DONE
234} ctl_msg_type;
235
236struct ctl_scsiio;
237
238#define	CTL_NUM_SG_ENTRIES	9
239
240struct ctl_io_hdr {
241	uint32_t	  version;	/* interface version XXX */
242	ctl_io_type	  io_type;	/* task I/O, SCSI I/O, etc. */
243	ctl_msg_type	  msg_type;
244	struct ctl_nexus  nexus;	/* Initiator, port, target, lun */
245	uint32_t	  iid_indx;	/* the index into the iid mapping */
246	uint32_t	  flags;	/* transaction flags */
247	uint32_t	  status;	/* transaction status */
248	uint32_t	  port_status;	/* trans status, set by PORT, 0 = good*/
249	uint32_t	  timeout;	/* timeout in ms */
250	uint32_t	  retries;	/* retry count */
251#ifdef CTL_IO_DELAY
252	uint8_t		  timer_bytes[CTL_TIMER_BYTES]; /* timer kludge */
253#endif /* CTL_IO_DELAY */
254#ifdef CTL_TIME_IO
255	time_t		  start_time;	/* I/O start time */
256	struct bintime	  start_bt;	/* Timer start ticks */
257	struct bintime	  dma_start_bt;	/* DMA start ticks */
258	struct bintime	  dma_bt;	/* DMA total ticks */
259	uint32_t	  num_dmas;	/* Number of DMAs */
260#endif /* CTL_TIME_IO */
261	union ctl_io	  *original_sc;
262	union ctl_io	  *serializing_sc;
263	void		  *pool;	/* I/O pool */
264	union ctl_priv	  ctl_private[CTL_NUM_PRIV];/* CTL private area */
265	uint8_t		  port_priv[CTL_PORT_PRIV_SIZE];/* PORT private area*/
266	struct ctl_sg_entry remote_sglist[CTL_NUM_SG_ENTRIES];
267	struct ctl_sg_entry remote_dma_sglist[CTL_NUM_SG_ENTRIES];
268	struct ctl_sg_entry local_sglist[CTL_NUM_SG_ENTRIES];
269	struct ctl_sg_entry local_dma_sglist[CTL_NUM_SG_ENTRIES];
270	STAILQ_ENTRY(ctl_io_hdr) links;	/* linked list pointer */
271	TAILQ_ENTRY(ctl_io_hdr) ooa_links;
272	TAILQ_ENTRY(ctl_io_hdr) blocked_links;
273};
274
275typedef enum {
276	CTL_TAG_UNTAGGED,
277	CTL_TAG_SIMPLE,
278	CTL_TAG_ORDERED,
279	CTL_TAG_HEAD_OF_QUEUE,
280	CTL_TAG_ACA
281} ctl_tag_type;
282
283union ctl_io;
284
285/*
286 * SCSI passthrough I/O structure for the CAM Target Layer.  Note
287 * that some of these fields are here for completeness, but they aren't
288 * used in the CTL implementation.  e.g., timeout and retries won't be
289 * used.
290 *
291 * Note:  Make sure the io_hdr is *always* the first element in this
292 * structure.
293 */
294struct ctl_scsiio {
295	struct ctl_io_hdr io_hdr;	/* common to all I/O types */
296	uint32_t   ext_sg_entries;	/* 0 = no S/G list, > 0 = num entries */
297	uint8_t	   *ext_data_ptr;	/* data buffer or S/G list */
298	uint32_t   ext_data_len;	/* Data transfer length */
299	uint32_t   ext_data_filled;	/* Amount of data filled so far */
300	uint32_t   kern_sg_entries;	/* 0 = no S/G list, > 0 = num entries */
301	uint32_t   rem_sg_entries;	/* 0 = no S/G list, > 0 = num entries */
302	uint8_t    *kern_data_ptr;	/* data buffer or S/G list */
303	uint32_t   kern_data_len;	/* Length of this S/G list/buffer */
304	uint32_t   kern_total_len;	/* Total length of this transaction */
305	uint32_t   kern_data_resid;	/* Length left to transfer after this*/
306	uint32_t   kern_rel_offset;	/* Byte Offset of this transfer */
307	struct     scsi_sense_data sense_data;	/* sense data */
308	uint8_t	   sense_len;		/* Returned sense length */
309	uint8_t	   scsi_status;		/* SCSI status byte */
310	uint8_t	   sense_residual;	/* sense residual length */
311	uint32_t   residual;		/* data residual length */
312	uint32_t   tag_num;		/* tag number */
313	ctl_tag_type tag_type;		/* simple, ordered, head of queue,etc.*/
314	uint8_t    cdb_len;		/* CDB length */
315	uint8_t	   cdb[CTL_MAX_CDBLEN];	/* CDB */
316	int	   (*be_move_done)(union ctl_io *io); /* called by fe */
317	int        (*io_cont)(union ctl_io *io); /* to continue processing */
318};
319
320typedef enum {
321	CTL_TASK_ABORT_TASK,
322	CTL_TASK_ABORT_TASK_SET,
323	CTL_TASK_CLEAR_ACA,
324	CTL_TASK_CLEAR_TASK_SET,
325	CTL_TASK_LUN_RESET,
326	CTL_TASK_TARGET_RESET,
327	CTL_TASK_BUS_RESET,
328	CTL_TASK_PORT_LOGIN,
329	CTL_TASK_PORT_LOGOUT
330} ctl_task_type;
331
332/*
333 * Task management I/O structure.  Aborts, bus resets, etc., are sent using
334 * this structure.
335 *
336 * Note:  Make sure the io_hdr is *always* the first element in this
337 * structure.
338 */
339struct ctl_taskio {
340	struct ctl_io_hdr	io_hdr;      /* common to all I/O types */
341	ctl_task_type		task_action; /* Target Reset, Abort, etc.  */
342	uint32_t		tag_num;     /* tag number */
343	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
344};
345
346typedef enum {
347	CTL_PR_REG_KEY,
348	CTL_PR_UNREG_KEY,
349	CTL_PR_PREEMPT,
350	CTL_PR_CLEAR,
351	CTL_PR_RESERVE,
352	CTL_PR_RELEASE
353} ctl_pr_action;
354
355/*
356 * The PR info is specifically for sending Persistent Reserve actions
357 * to the other SC which it must also act on.
358 *
359 * Note:  Make sure the io_hdr is *always* the first element in this
360 * structure.
361 */
362struct ctl_pr_info {
363	ctl_pr_action        action;
364	uint8_t              sa_res_key[8];
365	uint8_t              res_type;
366	uint16_t             residx;
367};
368
369struct ctl_ha_msg_hdr {
370	ctl_msg_type		msg_type;
371	union ctl_io		*original_sc;
372	union ctl_io		*serializing_sc;
373	struct ctl_nexus	nexus;	     /* Initiator, port, target, lun */
374	uint32_t		status;	     /* transaction status */
375	TAILQ_ENTRY(ctl_ha_msg_hdr) links;
376};
377
378#define	CTL_HA_MAX_SG_ENTRIES	16
379
380/*
381 * Used for CTL_MSG_APS_LOCK.
382 */
383struct ctl_ha_msg_aps {
384	struct ctl_ha_msg_hdr	hdr;
385	uint8_t			lock_flag;
386};
387
388/*
389 * Used for CTL_MSG_PERS_ACTION.
390 */
391struct ctl_ha_msg_pr {
392	struct ctl_ha_msg_hdr	hdr;
393	struct ctl_pr_info	pr_info;
394};
395
396/*
397 * The S/G handling here is a little different than the standard ctl_scsiio
398 * structure, because we can't pass data by reference in between controllers.
399 * The S/G list in the ctl_scsiio struct is normally passed in the
400 * kern_data_ptr field.  So kern_sg_entries here will always be non-zero,
401 * even if there is only one entry.
402 *
403 * Used for CTL_MSG_DATAMOVE.
404 */
405struct ctl_ha_msg_dt {
406	struct ctl_ha_msg_hdr	hdr;
407	ctl_io_flags		flags;  /* Only I/O flags are used here */
408	uint32_t		sg_sequence;     /* S/G portion number  */
409	uint8_t			sg_last;         /* last S/G batch = 1 */
410	uint32_t		sent_sg_entries; /* previous S/G count */
411	uint32_t		cur_sg_entries;  /* current S/G entries */
412	uint32_t		kern_sg_entries; /* total S/G entries */
413	uint32_t		kern_data_len;   /* Length of this S/G list */
414	uint32_t		kern_total_len;  /* Total length of this
415						    transaction */
416	uint32_t		kern_data_resid; /* Length left to transfer
417						    after this*/
418	uint32_t		kern_rel_offset; /* Byte Offset of this
419						    transfer */
420	struct ctl_sg_entry	sg_list[CTL_HA_MAX_SG_ENTRIES];
421};
422
423/*
424 * Used for CTL_MSG_SERIALIZE, CTL_MSG_FINISH_IO, CTL_MSG_BAD_JUJU.
425 */
426struct ctl_ha_msg_scsi {
427	struct ctl_ha_msg_hdr	hdr;
428	uint8_t			cdb[CTL_MAX_CDBLEN];	/* CDB */
429	uint32_t		tag_num;     /* tag number */
430	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
431	uint8_t			scsi_status; /* SCSI status byte */
432	struct scsi_sense_data	sense_data;  /* sense data */
433	uint8_t			sense_len;   /* Returned sense length */
434	uint8_t			sense_residual;	/* sense residual length */
435	uint32_t		residual;    /* data residual length */
436	uint32_t		fetd_status; /* trans status, set by FETD,
437						0 = good*/
438	struct ctl_lba_len	lbalen;      /* used for stats */
439};
440
441/*
442 * Used for CTL_MSG_MANAGE_TASKS.
443 */
444struct ctl_ha_msg_task {
445	struct ctl_ha_msg_hdr	hdr;
446	ctl_task_type		task_action; /* Target Reset, Abort, etc.  */
447	uint32_t		tag_num;     /* tag number */
448	ctl_tag_type		tag_type;    /* simple, ordered, etc. */
449};
450
451union ctl_ha_msg {
452	struct ctl_ha_msg_hdr	hdr;
453	struct ctl_ha_msg_task	task;
454	struct ctl_ha_msg_scsi	scsi;
455	struct ctl_ha_msg_dt	dt;
456	struct ctl_ha_msg_pr	pr;
457	struct ctl_ha_msg_aps	aps;
458};
459
460
461struct ctl_prio {
462	struct ctl_io_hdr  io_hdr;
463	struct ctl_ha_msg_pr pr_msg;
464};
465
466
467
468union ctl_io {
469	struct ctl_io_hdr io_hdr;	/* common to all I/O types */
470	struct ctl_scsiio scsiio;	/* Normal SCSI commands */
471	struct ctl_taskio taskio;	/* SCSI task management/reset */
472	struct ctl_prio   presio;	/* update per. res info on other SC */
473};
474
475#ifdef _KERNEL
476
477union ctl_io *ctl_alloc_io(void *pool_ref);
478void ctl_free_io(union ctl_io *io);
479void ctl_zero_io(union ctl_io *io);
480void ctl_copy_io(union ctl_io *src, union ctl_io *dest);
481
482#endif /* _KERNEL */
483
484#endif	/* _CTL_IO_H_ */
485
486/*
487 * vim: ts=8
488 */
489