1/*-
2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer,
11 *    without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 *    substantially similar to the "NO WARRANTY" disclaimer below
14 *    ("Disclaimer") and any redistribution must be conditioned upon
15 *    including a substantially similar Disclaimer requirement for further
16 *    binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
30 *
31 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.h#2 $
32 * $FreeBSD: stable/10/sys/cam/ctl/ctl_backend.h 313369 2017-02-07 01:56:26Z mav $
33 */
34/*
35 * CTL backend driver definitions
36 *
37 * Author: Ken Merry <ken@FreeBSD.org>
38 */
39
40#ifndef	_CTL_BACKEND_H_
41#define	_CTL_BACKEND_H_
42
43#include <cam/ctl/ctl_ioctl.h>
44
45typedef enum {
46	CTL_LUN_SERSEQ_OFF,
47	CTL_LUN_SERSEQ_READ,
48	CTL_LUN_SERSEQ_ON
49} ctl_lun_serseq;
50
51#ifdef _KERNEL
52
53#define CTL_BACKEND_DECLARE(name, driver) \
54	static int name ## _modevent(module_t mod, int type, void *data) \
55	{ \
56		switch (type) { \
57		case MOD_LOAD: \
58			return (ctl_backend_register( \
59				(struct ctl_backend_driver *)data)); \
60			break; \
61		case MOD_UNLOAD: \
62			return (ctl_backend_deregister( \
63				(struct ctl_backend_driver *)data)); \
64			break; \
65		default: \
66			return EOPNOTSUPP; \
67		} \
68		return 0; \
69	} \
70	static moduledata_t name ## _mod = { \
71		#name, \
72		name ## _modevent, \
73		(void *)&driver \
74	}; \
75	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
76	MODULE_DEPEND(name, ctl, 1, 1, 1); \
77	MODULE_DEPEND(name, cam, 1, 1, 1)
78
79
80typedef enum {
81	CTL_LUN_CONFIG_OK,
82	CTL_LUN_CONFIG_FAILURE
83} ctl_lun_config_status;
84
85typedef void (*be_callback_t)(void *be_lun);
86typedef void (*be_lun_config_t)(void *be_lun,
87				ctl_lun_config_status status);
88
89/*
90 * The lun_type field is the SCSI device type of this particular LUN.  In
91 * general, this should be T_DIRECT, although backends will want to create
92 * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
93 * the various SCSI device types.
94 *
95 * The flags are described above.
96 *
97 * The be_lun field is the backend driver's own context that will get
98 * passsed back so that it can tell which LUN CTL is referencing.
99 *
100 * maxlba is the maximum accessible LBA on the LUN.  Note that this is
101 * different from the capacity of the array.  capacity = maxlba + 1
102 *
103 * blocksize is the size, in bytes, of each LBA on the LUN.  In general
104 * this should be 512.  In theory CTL should be able to handle other block
105 * sizes.  Host application software may not deal with it very well, though.
106 *
107 * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
108 *
109 * pblockoff is the lowest LBA on the LUN aligned to physical sector.
110 *
111 * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block.
112 *
113 * ublockoff is the lowest LBA on the LUN aligned to UNMAP block.
114 *
115 * atomicblock is the number of blocks that can be written atomically.
116 *
117 * opttxferlen is the number of blocks that can be written in one operation.
118 *
119 * req_lun_id is the requested LUN ID.  CTL only pays attention to this
120 * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
121 * not available, the LUN addition will fail.  If a particular LUN ID isn't
122 * requested, the first available LUN ID will be allocated.
123 *
124 * serial_num is the device serial number returned in the SCSI INQUIRY VPD
125 * page 0x80.  This should be a unique, per-shelf value.  The data inside
126 * this field should be ASCII only, left aligned, and any unused space
127 * should be padded out with ASCII spaces.  This field should NOT be NULL
128 * terminated.
129 *
130 * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
131 * page 0x83.  This should be a unique, per-LUN value.  The data inside
132 * this field should be ASCII only, left aligned, and any unused space
133 * should be padded with ASCII spaces.  This field should NOT be NULL
134 * terminated.
135 *
136 * The lun_shutdown() method is the callback for the ctl_invalidate_lun()
137 * call.  It is called when all outstanding I/O for that LUN has been
138 * completed and CTL has deleted the resources for that LUN.  When the CTL
139 * backend gets this call, it can safely free its per-LUN resources.
140 *
141 * The lun_config_status() method is the callback for the ctl_add_lun()
142 * call.  It is called when the LUN is successfully added, or when LUN
143 * addition fails.  If the LUN is successfully added, the backend may call
144 * the ctl_enable_lun() method to enable the LUN.
145 *
146 * The be field is a pointer to the ctl_backend_driver structure, which
147 * contains the backend methods to be called by CTL.
148 *
149 * The ctl_lun field is for CTL internal use only, and should not be used
150 * by the backend.
151 *
152 * The links field is for CTL internal use only, and should not be used by
153 * the backend.
154 */
155struct ctl_be_lun {
156	uint8_t			lun_type;	/* passed to CTL */
157	ctl_backend_lun_flags	flags;		/* passed to CTL */
158	ctl_lun_serseq		serseq;		/* passed to CTL */
159	void			*be_lun;	/* passed to CTL */
160	uint64_t		maxlba;		/* passed to CTL */
161	uint32_t		blocksize;	/* passed to CTL */
162	uint16_t		pblockexp;	/* passed to CTL */
163	uint16_t		pblockoff;	/* passed to CTL */
164	uint16_t		ublockexp;	/* passed to CTL */
165	uint16_t		ublockoff;	/* passed to CTL */
166	uint32_t		atomicblock;	/* passed to CTL */
167	uint32_t		opttxferlen;	/* passed to CTL */
168	uint32_t		req_lun_id;	/* passed to CTL */
169	uint32_t		lun_id;		/* returned from CTL */
170	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
171	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
172	be_callback_t		lun_shutdown;	/* passed to CTL */
173	be_lun_config_t		lun_config_status; /* passed to CTL */
174	struct ctl_backend_driver *be;		/* passed to CTL */
175	void			*ctl_lun;	/* used by CTL */
176	ctl_options_t		options;	/* passed to CTL */
177	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
178};
179
180typedef enum {
181	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
182	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
183} ctl_backend_flags;
184
185typedef int (*be_init_t)(void);
186typedef int (*be_shutdown_t)(void);
187typedef int (*be_func_t)(union ctl_io *io);
188typedef void (*be_vfunc_t)(union ctl_io *io);
189typedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
190			  struct thread *td);
191typedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
192typedef uint64_t (*be_lunattr_t)(void *be_lun, const char *attrname);
193
194struct ctl_backend_driver {
195	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
196	ctl_backend_flags flags;	         /* passed to CTL */
197	be_init_t	  init;			 /* passed to CTL */
198	be_shutdown_t	  shutdown;		 /* passed to CTL */
199	be_func_t	  data_submit;		 /* passed to CTL */
200	be_func_t	  data_move_done;	 /* passed to CTL */
201	be_func_t	  config_read;		 /* passed to CTL */
202	be_func_t	  config_write;		 /* passed to CTL */
203	be_ioctl_t	  ioctl;		 /* passed to CTL */
204	be_luninfo_t	  lun_info;		 /* passed to CTL */
205	be_lunattr_t	  lun_attr;		 /* passed to CTL */
206#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
207	be_func_t	  config_move_done;	 /* passed to backend */
208#endif
209#if 0
210	be_vfunc_t	  config_write_done;	 /* passed to backend */
211#endif
212	u_int		  num_luns;		 /* used by CTL */
213	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
214};
215
216int ctl_backend_register(struct ctl_backend_driver *be);
217int ctl_backend_deregister(struct ctl_backend_driver *be);
218struct ctl_backend_driver *ctl_backend_find(char *backend_name);
219
220/*
221 * To add a LUN, first call ctl_add_lun().  You will get the lun_config_status()
222 * callback when the LUN addition has either succeeded or failed.
223 *
224 * Once you get that callback, you can then call ctl_enable_lun() to enable
225 * the LUN.
226 */
227int ctl_add_lun(struct ctl_be_lun *be_lun);
228int ctl_enable_lun(struct ctl_be_lun *be_lun);
229
230/*
231 * To delete a LUN, first call ctl_disable_lun(), then
232 * ctl_invalidate_lun().  You will get the lun_shutdown() callback when all
233 * I/O to the LUN has completed and the LUN has been deleted.
234 */
235int ctl_disable_lun(struct ctl_be_lun *be_lun);
236int ctl_invalidate_lun(struct ctl_be_lun *be_lun);
237
238/*
239 * To start a LUN (transition from powered off to powered on state) call
240 * ctl_start_lun().  To stop a LUN (transition from powered on to powered
241 * off state) call ctl_stop_lun().
242 */
243int ctl_start_lun(struct ctl_be_lun *be_lun);
244int ctl_stop_lun(struct ctl_be_lun *be_lun);
245
246/*
247 * Methods to notify about media and tray status changes.
248 */
249int ctl_lun_no_media(struct ctl_be_lun *be_lun);
250int ctl_lun_has_media(struct ctl_be_lun *be_lun);
251int ctl_lun_ejected(struct ctl_be_lun *be_lun);
252
253/*
254 * Called on LUN HA role change.
255 */
256int ctl_lun_primary(struct ctl_be_lun *be_lun);
257int ctl_lun_secondary(struct ctl_be_lun *be_lun);
258
259/*
260 * Let the backend notify the initiators about changes.
261 */
262void ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
263
264#endif /* _KERNEL */
265#endif /* _CTL_BACKEND_H_ */
266
267/*
268 * vim: ts=8
269 */
270