1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3288811Smav * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
4229997Sken * All rights reserved.
5229997Sken *
6229997Sken * Redistribution and use in source and binary forms, with or without
7229997Sken * modification, are permitted provided that the following conditions
8229997Sken * are met:
9229997Sken * 1. Redistributions of source code must retain the above copyright
10229997Sken *    notice, this list of conditions, and the following disclaimer,
11229997Sken *    without modification.
12229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
14229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
15229997Sken *    including a substantially similar Disclaimer requirement for further
16229997Sken *    binary redistribution.
17229997Sken *
18229997Sken * NO WARRANTY
19229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29229997Sken * POSSIBILITY OF SUCH DAMAGES.
30229997Sken *
31229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.h#2 $
32229997Sken * $FreeBSD: releng/10.3/sys/cam/ctl/ctl_backend.h 288811 2015-10-05 11:31:24Z mav $
33229997Sken */
34229997Sken/*
35229997Sken * CTL backend driver definitions
36229997Sken *
37229997Sken * Author: Ken Merry <ken@FreeBSD.org>
38229997Sken */
39229997Sken
40229997Sken#ifndef	_CTL_BACKEND_H_
41229997Sken#define	_CTL_BACKEND_H_
42229997Sken
43229997Sken/*
44229997Sken * XXX KDM move this to another header file?
45229997Sken */
46229997Sken#define	CTL_BE_NAME_LEN		32
47229997Sken
48229997Sken/*
49229997Sken * The ID_REQ flag is used to say that the caller has requested a
50229997Sken * particular LUN ID in the req_lun_id field.  If we cannot allocate that
51229997Sken * LUN ID, the ctl_add_lun() call will fail.
52229997Sken *
53288811Smav * The STOPPED flag tells us that the LUN should default to the powered
54229997Sken * off state.  It will return 0x04,0x02 until it is powered up.  ("Logical
55229997Sken * unit not ready, initializing command required.")
56229997Sken *
57288811Smav * The NO_MEDIA flag tells us that the LUN has no media inserted.
58229997Sken *
59229997Sken * The PRIMARY flag tells us that this LUN is registered as a Primary LUN
60229997Sken * which is accessible via the Master shelf controller in an HA. This flag
61229997Sken * being set indicates a Primary LUN. This flag being reset represents a
62229997Sken * Secondary LUN controlled by the Secondary controller in an HA
63229997Sken * configuration. Flag is applicable at this time to T_DIRECT types.
64229997Sken *
65229997Sken * The SERIAL_NUM flag tells us that the serial_num field is filled in and
66229997Sken * valid for use in SCSI INQUIRY VPD page 0x80.
67229997Sken *
68229997Sken * The DEVID flag tells us that the device_id field is filled in and
69229997Sken * valid for use in SCSI INQUIRY VPD page 0x83.
70229997Sken *
71229997Sken * The DEV_TYPE flag tells us that the device_type field is filled in.
72265634Smav *
73288811Smav * The EJECTED flag tells us that the removable LUN has tray open.
74288811Smav *
75265634Smav * The UNMAP flag tells us that this LUN supports UNMAP.
76273315Smav *
77273315Smav * The OFFLINE flag tells us that this LUN can not access backing store.
78229997Sken */
79229997Skentypedef enum {
80229997Sken	CTL_LUN_FLAG_ID_REQ		= 0x01,
81288811Smav	CTL_LUN_FLAG_STOPPED		= 0x02,
82288811Smav	CTL_LUN_FLAG_NO_MEDIA		= 0x04,
83229997Sken	CTL_LUN_FLAG_PRIMARY		= 0x08,
84229997Sken	CTL_LUN_FLAG_SERIAL_NUM		= 0x10,
85229997Sken	CTL_LUN_FLAG_DEVID		= 0x20,
86265634Smav	CTL_LUN_FLAG_DEV_TYPE		= 0x40,
87273315Smav	CTL_LUN_FLAG_UNMAP		= 0x80,
88288811Smav	CTL_LUN_FLAG_EJECTED		= 0x100,
89288727Smav	CTL_LUN_FLAG_READONLY		= 0x200
90229997Sken} ctl_backend_lun_flags;
91229997Sken
92288727Smavtypedef enum {
93288727Smav	CTL_LUN_SERSEQ_OFF,
94288727Smav	CTL_LUN_SERSEQ_READ,
95288727Smav	CTL_LUN_SERSEQ_ON
96288727Smav} ctl_lun_serseq;
97288727Smav
98229997Sken#ifdef _KERNEL
99229997Sken
100229997Sken#define CTL_BACKEND_DECLARE(name, driver) \
101229997Sken	static int name ## _modevent(module_t mod, int type, void *data) \
102229997Sken	{ \
103229997Sken		switch (type) { \
104229997Sken		case MOD_LOAD: \
105229997Sken			ctl_backend_register( \
106229997Sken				(struct ctl_backend_driver *)data); \
107229997Sken			break; \
108229997Sken		case MOD_UNLOAD: \
109229997Sken			printf(#name " module unload - not possible for this module type\n"); \
110229997Sken			return EINVAL; \
111229997Sken		default: \
112229997Sken			return EOPNOTSUPP; \
113229997Sken		} \
114229997Sken		return 0; \
115229997Sken	} \
116229997Sken	static moduledata_t name ## _mod = { \
117229997Sken		#name, \
118229997Sken		name ## _modevent, \
119229997Sken		(void *)&driver \
120229997Sken	}; \
121229997Sken	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
122229997Sken	MODULE_DEPEND(name, ctl, 1, 1, 1); \
123229997Sken	MODULE_DEPEND(name, cam, 1, 1, 1)
124229997Sken
125229997Sken
126229997Skentypedef enum {
127229997Sken	CTL_LUN_CONFIG_OK,
128229997Sken	CTL_LUN_CONFIG_FAILURE
129229997Sken} ctl_lun_config_status;
130229997Sken
131229997Skentypedef void (*be_callback_t)(void *be_lun);
132229997Skentypedef void (*be_lun_config_t)(void *be_lun,
133229997Sken				ctl_lun_config_status status);
134229997Sken
135229997Sken/*
136229997Sken * The lun_type field is the SCSI device type of this particular LUN.  In
137229997Sken * general, this should be T_DIRECT, although backends will want to create
138229997Sken * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
139229997Sken * the various SCSI device types.
140229997Sken *
141229997Sken * The flags are described above.
142229997Sken *
143229997Sken * The be_lun field is the backend driver's own context that will get
144229997Sken * passsed back so that it can tell which LUN CTL is referencing.
145229997Sken *
146229997Sken * maxlba is the maximum accessible LBA on the LUN.  Note that this is
147229997Sken * different from the capacity of the array.  capacity = maxlba + 1
148229997Sken *
149229997Sken * blocksize is the size, in bytes, of each LBA on the LUN.  In general
150229997Sken * this should be 512.  In theory CTL should be able to handle other block
151229997Sken * sizes.  Host application software may not deal with it very well, though.
152229997Sken *
153264727Smav * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
154264727Smav *
155276237Smav * pblockoff is the lowest LBA on the LUN aligned to physical sector.
156264727Smav *
157276237Smav * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block.
158276237Smav *
159276237Smav * ublockoff is the lowest LBA on the LUN aligned to UNMAP block.
160276237Smav *
161273311Smav * atomicblock is the number of blocks that can be written atomically.
162273311Smav *
163276237Smav * opttxferlen is the number of blocks that can be written in one operation.
164276237Smav *
165229997Sken * req_lun_id is the requested LUN ID.  CTL only pays attention to this
166229997Sken * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
167229997Sken * not available, the LUN addition will fail.  If a particular LUN ID isn't
168229997Sken * requested, the first available LUN ID will be allocated.
169229997Sken *
170229997Sken * serial_num is the device serial number returned in the SCSI INQUIRY VPD
171229997Sken * page 0x80.  This should be a unique, per-shelf value.  The data inside
172229997Sken * this field should be ASCII only, left aligned, and any unused space
173229997Sken * should be padded out with ASCII spaces.  This field should NOT be NULL
174229997Sken * terminated.
175229997Sken *
176229997Sken * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
177229997Sken * page 0x83.  This should be a unique, per-LUN value.  The data inside
178229997Sken * this field should be ASCII only, left aligned, and any unused space
179229997Sken * should be padded with ASCII spaces.  This field should NOT be NULL
180229997Sken * terminated.
181229997Sken *
182229997Sken * The lun_shutdown() method is the callback for the ctl_invalidate_lun()
183229997Sken * call.  It is called when all outstanding I/O for that LUN has been
184229997Sken * completed and CTL has deleted the resources for that LUN.  When the CTL
185229997Sken * backend gets this call, it can safely free its per-LUN resources.
186229997Sken *
187229997Sken * The lun_config_status() method is the callback for the ctl_add_lun()
188229997Sken * call.  It is called when the LUN is successfully added, or when LUN
189229997Sken * addition fails.  If the LUN is successfully added, the backend may call
190229997Sken * the ctl_enable_lun() method to enable the LUN.
191229997Sken *
192229997Sken * The be field is a pointer to the ctl_backend_driver structure, which
193229997Sken * contains the backend methods to be called by CTL.
194229997Sken *
195229997Sken * The ctl_lun field is for CTL internal use only, and should not be used
196229997Sken * by the backend.
197229997Sken *
198229997Sken * The links field is for CTL internal use only, and should not be used by
199229997Sken * the backend.
200229997Sken */
201229997Skenstruct ctl_be_lun {
202229997Sken	uint8_t			lun_type;	/* passed to CTL */
203229997Sken	ctl_backend_lun_flags	flags;		/* passed to CTL */
204288727Smav	ctl_lun_serseq		serseq;		/* passed to CTL */
205229997Sken	void			*be_lun;	/* passed to CTL */
206229997Sken	uint64_t		maxlba;		/* passed to CTL */
207229997Sken	uint32_t		blocksize;	/* passed to CTL */
208264727Smav	uint16_t		pblockexp;	/* passed to CTL */
209264727Smav	uint16_t		pblockoff;	/* passed to CTL */
210276179Smav	uint16_t		ublockexp;	/* passed to CTL */
211276179Smav	uint16_t		ublockoff;	/* passed to CTL */
212273311Smav	uint32_t		atomicblock;	/* passed to CTL */
213276237Smav	uint32_t		opttxferlen;	/* passed to CTL */
214229997Sken	uint32_t		req_lun_id;	/* passed to CTL */
215229997Sken	uint32_t		lun_id;		/* returned from CTL */
216229997Sken	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
217229997Sken	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
218229997Sken	be_callback_t		lun_shutdown;	/* passed to CTL */
219229997Sken	be_lun_config_t		lun_config_status; /* passed to CTL */
220229997Sken	struct ctl_backend_driver *be;		/* passed to CTL */
221229997Sken	void			*ctl_lun;	/* used by CTL */
222268678Smav	ctl_options_t		options;	/* passed to CTL */
223229997Sken	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
224229997Sken};
225229997Sken
226229997Skentypedef enum {
227229997Sken	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
228229997Sken	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
229229997Sken	CTL_BE_FLAG_INTERNAL	= 0x02	/* don't inc mod refcount */
230229997Sken} ctl_backend_flags;
231229997Sken
232229997Skentypedef int (*be_init_t)(void);
233229997Skentypedef int (*be_func_t)(union ctl_io *io);
234229997Skentypedef void (*be_vfunc_t)(union ctl_io *io);
235229997Skentypedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
236229997Sken			  struct thread *td);
237229997Skentypedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
238274732Smavtypedef uint64_t (*be_lunattr_t)(void *be_lun, const char *attrname);
239229997Sken
240229997Skenstruct ctl_backend_driver {
241229997Sken	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
242229997Sken	ctl_backend_flags flags;	         /* passed to CTL */
243229997Sken	be_init_t	  init;			 /* passed to CTL */
244229997Sken	be_func_t	  data_submit;		 /* passed to CTL */
245229997Sken	be_func_t	  data_move_done;	 /* passed to CTL */
246229997Sken	be_func_t	  config_read;		 /* passed to CTL */
247229997Sken	be_func_t	  config_write;		 /* passed to CTL */
248229997Sken	be_ioctl_t	  ioctl;		 /* passed to CTL */
249229997Sken	be_luninfo_t	  lun_info;		 /* passed to CTL */
250274732Smav	be_lunattr_t	  lun_attr;		 /* passed to CTL */
251229997Sken#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
252229997Sken	be_func_t	  config_move_done;	 /* passed to backend */
253229997Sken#endif
254229997Sken#if 0
255229997Sken	be_vfunc_t	  config_write_done;	 /* passed to backend */
256229997Sken#endif
257229997Sken	u_int		  num_luns;		 /* used by CTL */
258229997Sken	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
259229997Sken};
260229997Sken
261229997Skenint ctl_backend_register(struct ctl_backend_driver *be);
262229997Skenint ctl_backend_deregister(struct ctl_backend_driver *be);
263229997Skenstruct ctl_backend_driver *ctl_backend_find(char *backend_name);
264229997Sken
265229997Sken/*
266229997Sken * To add a LUN, first call ctl_add_lun().  You will get the lun_config_status()
267229997Sken * callback when the LUN addition has either succeeded or failed.
268229997Sken *
269229997Sken * Once you get that callback, you can then call ctl_enable_lun() to enable
270229997Sken * the LUN.
271229997Sken */
272229997Skenint ctl_add_lun(struct ctl_be_lun *be_lun);
273229997Skenint ctl_enable_lun(struct ctl_be_lun *be_lun);
274229997Sken
275229997Sken/*
276229997Sken * To delete a LUN, first call ctl_disable_lun(), then
277229997Sken * ctl_invalidate_lun().  You will get the lun_shutdown() callback when all
278229997Sken * I/O to the LUN has completed and the LUN has been deleted.
279229997Sken */
280229997Skenint ctl_disable_lun(struct ctl_be_lun *be_lun);
281229997Skenint ctl_invalidate_lun(struct ctl_be_lun *be_lun);
282229997Sken
283229997Sken/*
284229997Sken * To start a LUN (transition from powered off to powered on state) call
285229997Sken * ctl_start_lun().  To stop a LUN (transition from powered on to powered
286229997Sken * off state) call ctl_stop_lun().
287229997Sken */
288229997Skenint ctl_start_lun(struct ctl_be_lun *be_lun);
289229997Skenint ctl_stop_lun(struct ctl_be_lun *be_lun);
290229997Sken
291229997Sken/*
292288811Smav * Methods to notify about media and tray status changes.
293229997Sken */
294288811Smavint ctl_lun_no_media(struct ctl_be_lun *be_lun);
295288811Smavint ctl_lun_has_media(struct ctl_be_lun *be_lun);
296288811Smavint ctl_lun_ejected(struct ctl_be_lun *be_lun);
297229997Sken
298229997Sken/*
299288732Smav * Called on LUN HA role change.
300288732Smav */
301288732Smavint ctl_lun_primary(struct ctl_be_lun *be_lun);
302288732Smavint ctl_lun_secondary(struct ctl_be_lun *be_lun);
303288732Smav
304288732Smav/*
305288811Smav * Let the backend notify the initiators about changes.
306232604Strasz */
307232604Straszvoid ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
308232604Strasz
309229997Sken#endif /* _KERNEL */
310229997Sken#endif /* _CTL_BACKEND_H_ */
311229997Sken
312229997Sken/*
313229997Sken * vim: ts=8
314229997Sken */
315