1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3229997Sken * All rights reserved.
4229997Sken *
5229997Sken * Redistribution and use in source and binary forms, with or without
6229997Sken * modification, are permitted provided that the following conditions
7229997Sken * are met:
8229997Sken * 1. Redistributions of source code must retain the above copyright
9229997Sken *    notice, this list of conditions, and the following disclaimer,
10229997Sken *    without modification.
11229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
13229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
14229997Sken *    including a substantially similar Disclaimer requirement for further
15229997Sken *    binary redistribution.
16229997Sken *
17229997Sken * NO WARRANTY
18229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28229997Sken * POSSIBILITY OF SUCH DAMAGES.
29229997Sken *
30229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.h#2 $
31229997Sken * $FreeBSD$
32229997Sken */
33229997Sken/*
34229997Sken * CTL backend driver definitions
35229997Sken *
36229997Sken * Author: Ken Merry <ken@FreeBSD.org>
37229997Sken */
38229997Sken
39229997Sken#ifndef	_CTL_BACKEND_H_
40229997Sken#define	_CTL_BACKEND_H_
41229997Sken
42229997Sken/*
43229997Sken * XXX KDM move this to another header file?
44229997Sken */
45229997Sken#define	CTL_BE_NAME_LEN		32
46229997Sken
47229997Sken/*
48229997Sken * The ID_REQ flag is used to say that the caller has requested a
49229997Sken * particular LUN ID in the req_lun_id field.  If we cannot allocate that
50229997Sken * LUN ID, the ctl_add_lun() call will fail.
51229997Sken *
52229997Sken * The POWERED_OFF flag tells us that the LUN should default to the powered
53229997Sken * off state.  It will return 0x04,0x02 until it is powered up.  ("Logical
54229997Sken * unit not ready, initializing command required.")
55229997Sken *
56229997Sken * The INOPERABLE flag tells us that this LUN is not operable for whatever
57229997Sken * reason.  This means that user data may have been (or has been?) lost.
58229997Sken * We will return 0x31,0x00 ("Medium format corrupted") until the host
59229997Sken * issues a FORMAT UNIT command to clear the error.
60229997Sken *
61229997Sken * The PRIMARY flag tells us that this LUN is registered as a Primary LUN
62229997Sken * which is accessible via the Master shelf controller in an HA. This flag
63229997Sken * being set indicates a Primary LUN. This flag being reset represents a
64229997Sken * Secondary LUN controlled by the Secondary controller in an HA
65229997Sken * configuration. Flag is applicable at this time to T_DIRECT types.
66229997Sken *
67229997Sken * The SERIAL_NUM flag tells us that the serial_num field is filled in and
68229997Sken * valid for use in SCSI INQUIRY VPD page 0x80.
69229997Sken *
70229997Sken * The DEVID flag tells us that the device_id field is filled in and
71229997Sken * valid for use in SCSI INQUIRY VPD page 0x83.
72229997Sken *
73229997Sken * The DEV_TYPE flag tells us that the device_type field is filled in.
74265634Smav *
75265634Smav * The UNMAP flag tells us that this LUN supports UNMAP.
76229997Sken */
77229997Skentypedef enum {
78229997Sken	CTL_LUN_FLAG_ID_REQ		= 0x01,
79229997Sken	CTL_LUN_FLAG_POWERED_OFF	= 0x02,
80229997Sken	CTL_LUN_FLAG_INOPERABLE		= 0x04,
81229997Sken	CTL_LUN_FLAG_PRIMARY		= 0x08,
82229997Sken	CTL_LUN_FLAG_SERIAL_NUM		= 0x10,
83229997Sken	CTL_LUN_FLAG_DEVID		= 0x20,
84265634Smav	CTL_LUN_FLAG_DEV_TYPE		= 0x40,
85265634Smav	CTL_LUN_FLAG_UNMAP		= 0x80
86229997Sken} ctl_backend_lun_flags;
87229997Sken
88229997Sken#ifdef _KERNEL
89229997Sken
90229997Sken#define CTL_BACKEND_DECLARE(name, driver) \
91229997Sken	static int name ## _modevent(module_t mod, int type, void *data) \
92229997Sken	{ \
93229997Sken		switch (type) { \
94229997Sken		case MOD_LOAD: \
95229997Sken			ctl_backend_register( \
96229997Sken				(struct ctl_backend_driver *)data); \
97229997Sken			break; \
98229997Sken		case MOD_UNLOAD: \
99229997Sken			printf(#name " module unload - not possible for this module type\n"); \
100229997Sken			return EINVAL; \
101229997Sken		default: \
102229997Sken			return EOPNOTSUPP; \
103229997Sken		} \
104229997Sken		return 0; \
105229997Sken	} \
106229997Sken	static moduledata_t name ## _mod = { \
107229997Sken		#name, \
108229997Sken		name ## _modevent, \
109229997Sken		(void *)&driver \
110229997Sken	}; \
111229997Sken	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
112229997Sken	MODULE_DEPEND(name, ctl, 1, 1, 1); \
113229997Sken	MODULE_DEPEND(name, cam, 1, 1, 1)
114229997Sken
115229997Sken
116229997Skentypedef enum {
117229997Sken	CTL_LUN_CONFIG_OK,
118229997Sken	CTL_LUN_CONFIG_FAILURE
119229997Sken} ctl_lun_config_status;
120229997Sken
121229997Skentypedef void (*be_callback_t)(void *be_lun);
122229997Skentypedef void (*be_lun_config_t)(void *be_lun,
123229997Sken				ctl_lun_config_status status);
124229997Sken
125229997Sken/*
126229997Sken * The lun_type field is the SCSI device type of this particular LUN.  In
127229997Sken * general, this should be T_DIRECT, although backends will want to create
128229997Sken * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
129229997Sken * the various SCSI device types.
130229997Sken *
131229997Sken * The flags are described above.
132229997Sken *
133229997Sken * The be_lun field is the backend driver's own context that will get
134229997Sken * passsed back so that it can tell which LUN CTL is referencing.
135229997Sken *
136229997Sken * maxlba is the maximum accessible LBA on the LUN.  Note that this is
137229997Sken * different from the capacity of the array.  capacity = maxlba + 1
138229997Sken *
139229997Sken * blocksize is the size, in bytes, of each LBA on the LUN.  In general
140229997Sken * this should be 512.  In theory CTL should be able to handle other block
141229997Sken * sizes.  Host application software may not deal with it very well, though.
142229997Sken *
143264727Smav * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
144264727Smav *
145264727Smav * pblockoff is the lowest LBA on the LUN aligned ot physical sector.
146264727Smav *
147229997Sken * req_lun_id is the requested LUN ID.  CTL only pays attention to this
148229997Sken * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
149229997Sken * not available, the LUN addition will fail.  If a particular LUN ID isn't
150229997Sken * requested, the first available LUN ID will be allocated.
151229997Sken *
152229997Sken * serial_num is the device serial number returned in the SCSI INQUIRY VPD
153229997Sken * page 0x80.  This should be a unique, per-shelf value.  The data inside
154229997Sken * this field should be ASCII only, left aligned, and any unused space
155229997Sken * should be padded out with ASCII spaces.  This field should NOT be NULL
156229997Sken * terminated.
157229997Sken *
158229997Sken * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
159229997Sken * page 0x83.  This should be a unique, per-LUN value.  The data inside
160229997Sken * this field should be ASCII only, left aligned, and any unused space
161229997Sken * should be padded with ASCII spaces.  This field should NOT be NULL
162229997Sken * terminated.
163229997Sken *
164229997Sken * The lun_shutdown() method is the callback for the ctl_invalidate_lun()
165229997Sken * call.  It is called when all outstanding I/O for that LUN has been
166229997Sken * completed and CTL has deleted the resources for that LUN.  When the CTL
167229997Sken * backend gets this call, it can safely free its per-LUN resources.
168229997Sken *
169229997Sken * The lun_config_status() method is the callback for the ctl_add_lun()
170229997Sken * call.  It is called when the LUN is successfully added, or when LUN
171229997Sken * addition fails.  If the LUN is successfully added, the backend may call
172229997Sken * the ctl_enable_lun() method to enable the LUN.
173229997Sken *
174229997Sken * The be field is a pointer to the ctl_backend_driver structure, which
175229997Sken * contains the backend methods to be called by CTL.
176229997Sken *
177229997Sken * The ctl_lun field is for CTL internal use only, and should not be used
178229997Sken * by the backend.
179229997Sken *
180229997Sken * The links field is for CTL internal use only, and should not be used by
181229997Sken * the backend.
182229997Sken */
183229997Skenstruct ctl_be_lun {
184229997Sken	uint8_t			lun_type;	/* passed to CTL */
185229997Sken	ctl_backend_lun_flags	flags;		/* passed to CTL */
186229997Sken	void			*be_lun;	/* passed to CTL */
187229997Sken	uint64_t		maxlba;		/* passed to CTL */
188229997Sken	uint32_t		blocksize;	/* passed to CTL */
189264727Smav	uint16_t		pblockexp;	/* passed to CTL */
190264727Smav	uint16_t		pblockoff;	/* passed to CTL */
191229997Sken	uint32_t		req_lun_id;	/* passed to CTL */
192229997Sken	uint32_t		lun_id;		/* returned from CTL */
193229997Sken	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
194229997Sken	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
195229997Sken	be_callback_t		lun_shutdown;	/* passed to CTL */
196229997Sken	be_lun_config_t		lun_config_status; /* passed to CTL */
197229997Sken	struct ctl_backend_driver *be;		/* passed to CTL */
198229997Sken	void			*ctl_lun;	/* used by CTL */
199268678Smav	ctl_options_t		options;	/* passed to CTL */
200229997Sken	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
201229997Sken};
202229997Sken
203229997Skentypedef enum {
204229997Sken	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
205229997Sken	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
206229997Sken	CTL_BE_FLAG_INTERNAL	= 0x02	/* don't inc mod refcount */
207229997Sken} ctl_backend_flags;
208229997Sken
209229997Skentypedef int (*be_init_t)(void);
210229997Skentypedef int (*be_func_t)(union ctl_io *io);
211229997Skentypedef void (*be_vfunc_t)(union ctl_io *io);
212229997Skentypedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
213229997Sken			  struct thread *td);
214229997Skentypedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
215229997Sken
216229997Skenstruct ctl_backend_driver {
217229997Sken	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
218229997Sken	ctl_backend_flags flags;	         /* passed to CTL */
219229997Sken	be_init_t	  init;			 /* passed to CTL */
220229997Sken	be_func_t	  data_submit;		 /* passed to CTL */
221229997Sken	be_func_t	  data_move_done;	 /* passed to CTL */
222229997Sken	be_func_t	  config_read;		 /* passed to CTL */
223229997Sken	be_func_t	  config_write;		 /* passed to CTL */
224229997Sken	be_ioctl_t	  ioctl;		 /* passed to CTL */
225229997Sken	be_luninfo_t	  lun_info;		 /* passed to CTL */
226229997Sken#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
227229997Sken	be_func_t	  config_move_done;	 /* passed to backend */
228229997Sken#endif
229229997Sken#if 0
230229997Sken	be_vfunc_t	  config_write_done;	 /* passed to backend */
231229997Sken#endif
232229997Sken	u_int		  num_luns;		 /* used by CTL */
233229997Sken	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
234229997Sken};
235229997Sken
236229997Skenint ctl_backend_register(struct ctl_backend_driver *be);
237229997Skenint ctl_backend_deregister(struct ctl_backend_driver *be);
238229997Skenstruct ctl_backend_driver *ctl_backend_find(char *backend_name);
239229997Sken
240229997Sken/*
241229997Sken * To add a LUN, first call ctl_add_lun().  You will get the lun_config_status()
242229997Sken * callback when the LUN addition has either succeeded or failed.
243229997Sken *
244229997Sken * Once you get that callback, you can then call ctl_enable_lun() to enable
245229997Sken * the LUN.
246229997Sken */
247229997Skenint ctl_add_lun(struct ctl_be_lun *be_lun);
248229997Skenint ctl_enable_lun(struct ctl_be_lun *be_lun);
249229997Sken
250229997Sken/*
251229997Sken * To delete a LUN, first call ctl_disable_lun(), then
252229997Sken * ctl_invalidate_lun().  You will get the lun_shutdown() callback when all
253229997Sken * I/O to the LUN has completed and the LUN has been deleted.
254229997Sken */
255229997Skenint ctl_disable_lun(struct ctl_be_lun *be_lun);
256229997Skenint ctl_invalidate_lun(struct ctl_be_lun *be_lun);
257229997Sken
258229997Sken/*
259229997Sken * To start a LUN (transition from powered off to powered on state) call
260229997Sken * ctl_start_lun().  To stop a LUN (transition from powered on to powered
261229997Sken * off state) call ctl_stop_lun().
262229997Sken */
263229997Skenint ctl_start_lun(struct ctl_be_lun *be_lun);
264229997Skenint ctl_stop_lun(struct ctl_be_lun *be_lun);
265229997Sken
266229997Sken/*
267229997Sken * If a LUN is inoperable, call ctl_lun_inoperable().  Generally the LUN
268229997Sken * will become operable once again when the user issues the SCSI FORMAT UNIT
269229997Sken * command.  (CTL will automatically clear the inoperable flag.)  If we
270229997Sken * need to re-enable the LUN, we can call ctl_lun_operable() to enable it
271229997Sken * without a SCSI command.
272229997Sken */
273229997Skenint ctl_lun_inoperable(struct ctl_be_lun *be_lun);
274229997Skenint ctl_lun_operable(struct ctl_be_lun *be_lun);
275229997Sken
276229997Sken/*
277229997Sken * If a LUN is locked on or unlocked from a power/APS standpoint, call
278229997Sken * ctl_lun_power_lock() to update the current status in CTL's APS subpage.
279229997Sken * Set the lock flag to 1 to lock the LUN, set it to 0 to unlock the LUN.
280229997Sken */
281229997Skenint ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
282229997Sken		       int lock);
283229997Sken
284229997Sken/*
285229997Sken * To take a LUN offline, call ctl_lun_offline().  Generally the LUN will
286229997Sken * be online again once the user sends a SCSI START STOP UNIT command with
287229997Sken * the start and on/offline bits set.  The backend can bring the LUN back
288229997Sken * online via the ctl_lun_online() function, if necessary.
289229997Sken */
290229997Skenint ctl_lun_offline(struct ctl_be_lun *be_lun);
291229997Skenint ctl_lun_online(struct ctl_be_lun *be_lun);
292229997Sken
293232604Strasz/*
294232604Strasz * Let the backend notify the initiator about changed capacity.
295232604Strasz */
296232604Straszvoid ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
297232604Strasz
298229997Sken#endif /* _KERNEL */
299229997Sken#endif /* _CTL_BACKEND_H_ */
300229997Sken
301229997Sken/*
302229997Sken * vim: ts=8
303229997Sken */
304