ctl_private.h revision 268675
1/*-
2 * Copyright (c) 2003, 2004, 2005, 2008 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_private.h#7 $
31 * $FreeBSD: stable/10/sys/cam/ctl/ctl_private.h 268675 2014-07-15 16:54:04Z mav $
32 */
33/*
34 * CAM Target Layer driver private data structures/definitions.
35 *
36 * Author: Ken Merry <ken@FreeBSD.org>
37 */
38
39#ifndef	_CTL_PRIVATE_H_
40#define	_CTL_PRIVATE_H_
41
42/*
43 * SCSI vendor and product names.
44 */
45#define	CTL_VENDOR		"FREEBSD "
46#define	CTL_DIRECT_PRODUCT	"CTLDISK         "
47#define	CTL_PROCESSOR_PRODUCT	"CTLPROCESSOR    "
48#define	CTL_UNKNOWN_PRODUCT	"CTLDEVICE       "
49
50struct ctl_fe_ioctl_startstop_info {
51	struct cv			sem;
52	struct ctl_hard_startstop_info	hs_info;
53};
54
55struct ctl_fe_ioctl_bbrread_info {
56	struct cv			sem;
57	struct ctl_bbrread_info		*bbr_info;
58	int				wakeup_done;
59	struct mtx			*lock;
60};
61
62typedef enum {
63	CTL_IOCTL_INPROG,
64	CTL_IOCTL_DATAMOVE,
65	CTL_IOCTL_DONE
66} ctl_fe_ioctl_state;
67
68struct ctl_fe_ioctl_params {
69	struct cv		sem;
70	struct mtx		ioctl_mtx;
71	ctl_fe_ioctl_state	state;
72};
73
74#define	CTL_POOL_ENTRIES_INTERNAL	200
75#define	CTL_POOL_ENTRIES_EMERGENCY	300
76#define CTL_POOL_ENTRIES_OTHER_SC   200
77
78typedef enum {
79	CTL_POOL_INTERNAL,
80	CTL_POOL_FETD,
81	CTL_POOL_EMERGENCY,
82	CTL_POOL_IOCTL,
83	CTL_POOL_4OTHERSC
84} ctl_pool_type;
85
86typedef enum {
87	CTL_POOL_FLAG_NONE	= 0x00,
88	CTL_POOL_FLAG_INVALID	= 0x01
89} ctl_pool_flags;
90
91struct ctl_io_pool {
92	ctl_pool_type			type;
93	ctl_pool_flags			flags;
94	uint32_t			id;
95	struct ctl_softc		*ctl_softc;
96	uint32_t			refcount;
97	uint64_t			total_allocated;
98	uint64_t			total_freed;
99	int32_t				total_ctl_io;
100	int32_t				free_ctl_io;
101	STAILQ_HEAD(, ctl_io_hdr)	free_queue;
102	STAILQ_ENTRY(ctl_io_pool)	links;
103};
104
105typedef enum {
106	CTL_IOCTL_FLAG_NONE	= 0x00,
107	CTL_IOCTL_FLAG_ENABLED	= 0x01
108} ctl_ioctl_flags;
109
110struct ctl_ioctl_info {
111	ctl_ioctl_flags		flags;
112	uint32_t		cur_tag_num;
113	struct ctl_frontend	fe;
114	char			port_name[24];
115};
116
117typedef enum {
118	CTL_SER_BLOCK,
119	CTL_SER_EXTENT,
120	CTL_SER_PASS,
121	CTL_SER_SKIP
122} ctl_serialize_action;
123
124typedef enum {
125	CTL_ACTION_BLOCK,
126	CTL_ACTION_OVERLAP,
127	CTL_ACTION_OVERLAP_TAG,
128	CTL_ACTION_PASS,
129	CTL_ACTION_SKIP,
130	CTL_ACTION_ERROR
131} ctl_action;
132
133/*
134 * WARNING:  Keep the bottom nibble here free, we OR in the data direction
135 * flags for each command.
136 *
137 * Note:  "OK_ON_ALL_LUNS" == we don't have to have a lun configured
138 *        "OK_ON_BOTH"     == we have to have a lun configured
139 *        "SA5"            == command has 5-bit service action at byte 1
140 */
141typedef enum {
142	CTL_CMD_FLAG_NONE		= 0x0000,
143	CTL_CMD_FLAG_NO_SENSE		= 0x0010,
144	CTL_CMD_FLAG_OK_ON_ALL_LUNS	= 0x0020,
145	CTL_CMD_FLAG_ALLOW_ON_RESV	= 0x0040,
146	CTL_CMD_FLAG_OK_ON_PROC		= 0x0100,
147	CTL_CMD_FLAG_OK_ON_SLUN		= 0x0200,
148	CTL_CMD_FLAG_OK_ON_BOTH		= 0x0300,
149	CTL_CMD_FLAG_OK_ON_STOPPED	= 0x0400,
150	CTL_CMD_FLAG_OK_ON_INOPERABLE	= 0x0800,
151	CTL_CMD_FLAG_OK_ON_OFFLINE	= 0x1000,
152	CTL_CMD_FLAG_OK_ON_SECONDARY	= 0x2000,
153	CTL_CMD_FLAG_ALLOW_ON_PR_RESV	= 0x4000,
154	CTL_CMD_FLAG_SA5		= 0x8000
155} ctl_cmd_flags;
156
157typedef enum {
158	CTL_SERIDX_TUR	= 0,
159	CTL_SERIDX_READ,
160	CTL_SERIDX_WRITE,
161	CTL_SERIDX_UNMAP,
162	CTL_SERIDX_MD_SNS,
163	CTL_SERIDX_MD_SEL,
164	CTL_SERIDX_RQ_SNS,
165	CTL_SERIDX_INQ,
166	CTL_SERIDX_RD_CAP,
167	CTL_SERIDX_RESV,
168	CTL_SERIDX_REL,
169	CTL_SERIDX_LOG_SNS,
170	CTL_SERIDX_FORMAT,
171	CTL_SERIDX_START,
172	CTL_SERIDX_PRES_IN,
173	CTL_SERIDX_PRES_OUT,
174	CTL_SERIDX_MAIN_IN,
175	/* TBD: others to be filled in as needed */
176	CTL_SERIDX_COUNT, /* LAST, not a normal code, provides # codes */
177	CTL_SERIDX_INVLD = CTL_SERIDX_COUNT
178} ctl_seridx;
179
180typedef int	ctl_opfunc(struct ctl_scsiio *ctsio);
181
182struct ctl_cmd_entry {
183	ctl_opfunc		*execute;
184	ctl_seridx		seridx;
185	ctl_cmd_flags		flags;
186	ctl_lun_error_pattern	pattern;
187	uint8_t			length;		/* CDB length */
188	uint8_t			usage[15];	/* Mask of allowed CDB bits
189						 * after the opcode byte. */
190};
191
192typedef enum {
193	CTL_LUN_NONE		= 0x000,
194	CTL_LUN_CONTROL		= 0x001,
195	CTL_LUN_RESERVED	= 0x002,
196	CTL_LUN_INVALID		= 0x004,
197	CTL_LUN_DISABLED	= 0x008,
198	CTL_LUN_MALLOCED	= 0x010,
199	CTL_LUN_STOPPED		= 0x020,
200	CTL_LUN_INOPERABLE	= 0x040,
201	CTL_LUN_OFFLINE		= 0x080,
202	CTL_LUN_PR_RESERVED	= 0x100,
203	CTL_LUN_PRIMARY_SC	= 0x200,
204	CTL_LUN_SENSE_DESC	= 0x400
205} ctl_lun_flags;
206
207typedef enum {
208	CTLBLOCK_FLAG_NONE	= 0x00,
209	CTLBLOCK_FLAG_INVALID	= 0x01
210} ctlblock_flags;
211
212union ctl_softcs {
213	struct ctl_softc	*ctl_softc;
214	struct ctlblock_softc	*ctlblock_softc;
215};
216
217/*
218 * Mode page defaults.
219 */
220#if 0
221/*
222 * These values make Solaris trim off some of the capacity.
223 */
224#define	CTL_DEFAULT_SECTORS_PER_TRACK	63
225#define	CTL_DEFAULT_HEADS		255
226/*
227 * These values seem to work okay.
228 */
229#define	CTL_DEFAULT_SECTORS_PER_TRACK	63
230#define	CTL_DEFAULT_HEADS		16
231/*
232 * These values work reasonably well.
233 */
234#define	CTL_DEFAULT_SECTORS_PER_TRACK	512
235#define	CTL_DEFAULT_HEADS		64
236#endif
237
238/*
239 * Solaris is somewhat picky about how many heads and sectors per track you
240 * have defined in mode pages 3 and 4.  These values seem to cause Solaris
241 * to get the capacity more or less right when you run the format tool.
242 * They still have problems when dealing with devices larger than 1TB,
243 * but there isn't anything we can do about that.
244 *
245 * For smaller LUN sizes, this ends up causing the number of cylinders to
246 * work out to 0.  Solaris actually recognizes that and comes up with its
247 * own bogus geometry to fit the actual capacity of the drive.  They really
248 * should just give up on geometry and stick to the read capacity
249 * information alone for modern disk drives.
250 *
251 * One thing worth mentioning about Solaris' mkfs command is that it
252 * doesn't like sectors per track values larger than 256.  512 seems to
253 * work okay for format, but causes problems when you try to make a
254 * filesystem.
255 *
256 * Another caveat about these values:  the product of these two values
257 * really should be a power of 2.  This is because of the simplistic
258 * shift-based calculation that we have to use on the i386 platform to
259 * calculate the number of cylinders here.  (If you use a divide, you end
260 * up calling __udivdi3(), which is a hardware FP call on the PC.  On the
261 * XScale, it is done in software, so you can do that from inside the
262 * kernel.)
263 *
264 * So for the current values (256 S/T, 128 H), we get 32768, which works
265 * very nicely for calculating cylinders.
266 *
267 * If you want to change these values so that their product is no longer a
268 * power of 2, re-visit the calculation in ctl_init_page_index().  You may
269 * need to make it a bit more complicated to get the number of cylinders
270 * right.
271 */
272#define	CTL_DEFAULT_SECTORS_PER_TRACK	256
273#define	CTL_DEFAULT_HEADS		128
274
275#define	CTL_DEFAULT_ROTATION_RATE	10000
276
277struct ctl_page_index;
278
279typedef int	ctl_modesen_handler(struct ctl_scsiio *ctsio,
280				    struct ctl_page_index *page_index,
281				    int pc);
282typedef int	ctl_modesel_handler(struct ctl_scsiio *ctsio,
283				    struct ctl_page_index *page_index,
284				    uint8_t *page_ptr);
285
286typedef enum {
287	CTL_PAGE_FLAG_NONE	 = 0x00,
288	CTL_PAGE_FLAG_DISK_ONLY	 = 0x01
289} ctl_page_flags;
290
291struct ctl_page_index {
292	uint8_t			page_code;
293	uint8_t			subpage;
294	uint16_t		page_len;
295	uint8_t			*page_data;
296	ctl_page_flags		page_flags;
297	ctl_modesen_handler	*sense_handler;
298	ctl_modesel_handler	*select_handler;
299};
300
301#define	CTL_PAGE_CURRENT	0x00
302#define	CTL_PAGE_CHANGEABLE	0x01
303#define	CTL_PAGE_DEFAULT	0x02
304#define	CTL_PAGE_SAVED		0x03
305
306static const struct ctl_page_index page_index_template[] = {
307	{SMS_FORMAT_DEVICE_PAGE, 0, sizeof(struct scsi_format_page), NULL,
308	 CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
309	{SMS_RIGID_DISK_PAGE, 0, sizeof(struct scsi_rigid_disk_page), NULL,
310	 CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
311	{SMS_CACHING_PAGE, 0, sizeof(struct scsi_caching_page), NULL,
312	 CTL_PAGE_FLAG_DISK_ONLY, NULL, NULL},
313	{SMS_CONTROL_MODE_PAGE, 0, sizeof(struct scsi_control_page), NULL,
314	 CTL_PAGE_FLAG_NONE, NULL, ctl_control_page_handler},
315   	{SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, PWR_SUBPAGE_CODE,
316	 sizeof(struct copan_power_subpage), NULL, CTL_PAGE_FLAG_NONE,
317	 ctl_power_sp_sense_handler, ctl_power_sp_handler},
318	{SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, APS_SUBPAGE_CODE,
319	 sizeof(struct copan_aps_subpage), NULL, CTL_PAGE_FLAG_NONE,
320	 NULL, ctl_aps_sp_handler},
321	{SMS_VENDOR_SPECIFIC_PAGE | SMPH_SPF, DBGCNF_SUBPAGE_CODE,
322	 sizeof(struct copan_debugconf_subpage), NULL, CTL_PAGE_FLAG_NONE,
323	 ctl_debugconf_sp_sense_handler, ctl_debugconf_sp_select_handler},
324};
325
326#define	CTL_NUM_MODE_PAGES sizeof(page_index_template)/   \
327			   sizeof(page_index_template[0])
328
329struct ctl_mode_pages {
330	struct scsi_format_page		format_page[4];
331	struct scsi_rigid_disk_page	rigid_disk_page[4];
332	struct scsi_caching_page	caching_page[4];
333	struct scsi_control_page	control_page[4];
334	struct copan_power_subpage	power_subpage[4];
335	struct copan_aps_subpage	aps_subpage[4];
336	struct copan_debugconf_subpage	debugconf_subpage[4];
337	struct ctl_page_index		index[CTL_NUM_MODE_PAGES];
338};
339
340struct ctl_pending_sense {
341	ctl_ua_type		ua_pending;
342	struct scsi_sense_data	sense;
343};
344
345struct ctl_lun_delay_info {
346	ctl_delay_type		datamove_type;
347	uint32_t		datamove_delay;
348	ctl_delay_type		done_type;
349	uint32_t		done_delay;
350};
351
352typedef enum {
353	CTL_ERR_INJ_NONE	= 0x00,
354	CTL_ERR_INJ_ABORTED	= 0x01
355} ctl_err_inject_flags;
356
357typedef enum {
358	CTL_PR_FLAG_NONE	= 0x00,
359	CTL_PR_FLAG_REGISTERED	= 0x01,
360	CTL_PR_FLAG_ACTIVE_RES	= 0x02
361} ctl_per_res_flags;
362
363struct ctl_per_res_info {
364	struct scsi_per_res_key res_key;
365	uint8_t  registered;
366};
367
368#define CTL_PR_ALL_REGISTRANTS  0xFFFF
369#define CTL_PR_NO_RESERVATION   0xFFF0
370
371/*
372 * For report target port groups.
373 */
374#define NUM_TARGET_PORT_GROUPS	2
375#define NUM_PORTS_PER_GRP	2
376
377struct ctl_lun {
378	struct mtx			lun_lock;
379	struct ctl_id			target;
380	uint64_t			lun;
381	ctl_lun_flags			flags;
382	STAILQ_HEAD(,ctl_error_desc)	error_list;
383	uint64_t			error_serial;
384	struct ctl_softc		*ctl_softc;
385	struct ctl_be_lun		*be_lun;
386	struct ctl_backend_driver	*backend;
387	int				io_count;
388	struct ctl_lun_delay_info	delay_info;
389	int				sync_interval;
390	int				sync_count;
391	TAILQ_HEAD(ctl_ooaq, ctl_io_hdr)  ooa_queue;
392	TAILQ_HEAD(ctl_blockq,ctl_io_hdr) blocked_queue;
393	STAILQ_ENTRY(ctl_lun)		links;
394	STAILQ_ENTRY(ctl_lun)		run_links;
395	struct ctl_nexus		rsv_nexus;
396	uint32_t			have_ca[CTL_MAX_INITIATORS >> 5];
397	struct ctl_pending_sense	pending_sense[CTL_MAX_INITIATORS];
398	struct ctl_mode_pages		mode_pages;
399	struct ctl_lun_io_stats		stats;
400	struct ctl_per_res_info		per_res[2*CTL_MAX_INITIATORS];
401	unsigned int			PRGeneration;
402	int				pr_key_count;
403	uint16_t        		pr_res_idx;
404	uint8_t				res_type;
405	uint8_t				write_buffer[524288];
406};
407
408typedef enum {
409	CTL_FLAG_REAL_SYNC	= 0x02,
410	CTL_FLAG_MASTER_SHELF	= 0x04
411} ctl_gen_flags;
412
413struct ctl_wwpn_iid {
414	int in_use;
415	uint64_t wwpn;
416	uint32_t iid;
417	int32_t port;
418};
419
420#define CTL_MAX_THREADS		16
421
422struct ctl_thread {
423	struct mtx_padalign queue_lock;
424	struct ctl_softc	*ctl_softc;
425	struct thread		*thread;
426	STAILQ_HEAD(, ctl_io_hdr) incoming_queue;
427	STAILQ_HEAD(, ctl_io_hdr) rtr_queue;
428	STAILQ_HEAD(, ctl_io_hdr) done_queue;
429	STAILQ_HEAD(, ctl_io_hdr) isc_queue;
430};
431
432struct ctl_softc {
433	struct mtx ctl_lock;
434	struct cdev *dev;
435	int open_count;
436	struct ctl_id target;
437	int num_disks;
438	int num_luns;
439	ctl_gen_flags flags;
440	ctl_ha_mode ha_mode;
441	int inquiry_pq_no_lun;
442	struct sysctl_ctx_list sysctl_ctx;
443	struct sysctl_oid *sysctl_tree;
444	struct ctl_ioctl_info ioctl_info;
445	struct ctl_io_pool *internal_pool;
446	struct ctl_io_pool *emergency_pool;
447	struct ctl_io_pool *othersc_pool;
448	struct proc *ctl_proc;
449	int targ_online;
450	uint32_t ctl_lun_mask[CTL_MAX_LUNS >> 5];
451	struct ctl_lun *ctl_luns[CTL_MAX_LUNS];
452	struct ctl_wwpn_iid wwpn_iid[CTL_MAX_PORTS][CTL_MAX_INIT_PER_PORT];
453	uint32_t ctl_port_mask;
454	uint64_t aps_locked_lun;
455	STAILQ_HEAD(, ctl_lun) lun_list;
456	STAILQ_HEAD(, ctl_be_lun) pending_lun_queue;
457	uint32_t num_frontends;
458	STAILQ_HEAD(, ctl_frontend) fe_list;
459	struct ctl_frontend *ctl_ports[CTL_MAX_PORTS];
460	uint32_t num_backends;
461	STAILQ_HEAD(, ctl_backend_driver) be_list;
462	struct mtx pool_lock;
463	uint32_t num_pools;
464	uint32_t cur_pool_id;
465	STAILQ_HEAD(, ctl_io_pool) io_pools;
466	time_t last_print_jiffies;
467	uint32_t skipped_prints;
468	struct ctl_thread threads[CTL_MAX_THREADS];
469};
470
471#ifdef _KERNEL
472
473extern const struct ctl_cmd_entry ctl_cmd_table[256];
474
475uint32_t ctl_get_initindex(struct ctl_nexus *nexus);
476int ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
477		    uint32_t total_ctl_io, struct ctl_io_pool **npool);
478void ctl_pool_free(struct ctl_io_pool *pool);
479int ctl_scsi_release(struct ctl_scsiio *ctsio);
480int ctl_scsi_reserve(struct ctl_scsiio *ctsio);
481int ctl_start_stop(struct ctl_scsiio *ctsio);
482int ctl_sync_cache(struct ctl_scsiio *ctsio);
483int ctl_format(struct ctl_scsiio *ctsio);
484int ctl_read_buffer(struct ctl_scsiio *ctsio);
485int ctl_write_buffer(struct ctl_scsiio *ctsio);
486int ctl_write_same(struct ctl_scsiio *ctsio);
487int ctl_unmap(struct ctl_scsiio *ctsio);
488int ctl_mode_select(struct ctl_scsiio *ctsio);
489int ctl_mode_sense(struct ctl_scsiio *ctsio);
490int ctl_read_capacity(struct ctl_scsiio *ctsio);
491int ctl_read_capacity_16(struct ctl_scsiio *ctsio);
492int ctl_read_write(struct ctl_scsiio *ctsio);
493int ctl_cnw(struct ctl_scsiio *ctsio);
494int ctl_report_luns(struct ctl_scsiio *ctsio);
495int ctl_request_sense(struct ctl_scsiio *ctsio);
496int ctl_tur(struct ctl_scsiio *ctsio);
497int ctl_verify(struct ctl_scsiio *ctsio);
498int ctl_inquiry(struct ctl_scsiio *ctsio);
499int ctl_persistent_reserve_in(struct ctl_scsiio *ctsio);
500int ctl_persistent_reserve_out(struct ctl_scsiio *ctsio);
501int ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio);
502int ctl_report_supported_opcodes(struct ctl_scsiio *ctsio);
503int ctl_report_supported_tmf(struct ctl_scsiio *ctsio);
504int ctl_report_timestamp(struct ctl_scsiio *ctsio);
505int ctl_isc(struct ctl_scsiio *ctsio);
506
507#endif	/* _KERNEL */
508
509#endif	/* _CTL_PRIVATE_H_ */
510
511/*
512 * vim: ts=8
513 */
514