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/11/sys/cam/ctl/ctl_backend.h 361256 2020-05-19 14:42:09Z 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 void (*be_callback_t)(void *be_lun);
81
82/*
83 * The lun_type field is the SCSI device type of this particular LUN.  In
84 * general, this should be T_DIRECT, although backends will want to create
85 * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
86 * the various SCSI device types.
87 *
88 * The flags are described above.
89 *
90 * The be_lun field is the backend driver's own context that will get
91 * passsed back so that it can tell which LUN CTL is referencing.
92 *
93 * maxlba is the maximum accessible LBA on the LUN.  Note that this is
94 * different from the capacity of the array.  capacity = maxlba + 1
95 *
96 * blocksize is the size, in bytes, of each LBA on the LUN.  In general
97 * this should be 512.  In theory CTL should be able to handle other block
98 * sizes.  Host application software may not deal with it very well, though.
99 *
100 * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
101 *
102 * pblockoff is the lowest LBA on the LUN aligned to physical sector.
103 *
104 * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block.
105 *
106 * ublockoff is the lowest LBA on the LUN aligned to UNMAP block.
107 *
108 * atomicblock is the number of blocks that can be written atomically.
109 *
110 * opttxferlen is the number of blocks that can be written in one operation.
111 *
112 * req_lun_id is the requested LUN ID.  CTL only pays attention to this
113 * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
114 * not available, the LUN addition will fail.  If a particular LUN ID isn't
115 * requested, the first available LUN ID will be allocated.
116 *
117 * serial_num is the device serial number returned in the SCSI INQUIRY VPD
118 * page 0x80.  This should be a unique, per-shelf value.  The data inside
119 * this field should be ASCII only, left aligned, and any unused space
120 * should be padded out with ASCII spaces.  This field should NOT be NULL
121 * terminated.
122 *
123 * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
124 * page 0x83.  This should be a unique, per-LUN value.  The data inside
125 * this field should be ASCII only, left aligned, and any unused space
126 * should be padded with ASCII spaces.  This field should NOT be NULL
127 * terminated.
128 *
129 * The lun_shutdown() method is the callback for the ctl_remove_lun()
130 * call.  It is called when all outstanding I/O for that LUN has been
131 * completed and CTL has deleted the resources for that LUN.  When the CTL
132 * backend gets this call, it can safely free its per-LUN resources.
133 *
134 * The be field is a pointer to the ctl_backend_driver structure, which
135 * contains the backend methods to be called by CTL.
136 *
137 * The ctl_lun field is for CTL internal use only, and should not be used
138 * by the backend.
139 *
140 * The links field is for CTL internal use only, and should not be used by
141 * the backend.
142 */
143struct ctl_be_lun {
144	uint8_t			lun_type;	/* passed to CTL */
145	ctl_backend_lun_flags	flags;		/* passed to CTL */
146	ctl_lun_serseq		serseq;		/* passed to CTL */
147	void			*be_lun;	/* passed to CTL */
148	uint64_t		maxlba;		/* passed to CTL */
149	uint32_t		blocksize;	/* passed to CTL */
150	uint16_t		pblockexp;	/* passed to CTL */
151	uint16_t		pblockoff;	/* passed to CTL */
152	uint16_t		ublockexp;	/* passed to CTL */
153	uint16_t		ublockoff;	/* passed to CTL */
154	uint32_t		atomicblock;	/* passed to CTL */
155	uint32_t		opttxferlen;	/* passed to CTL */
156	uint32_t		req_lun_id;	/* passed to CTL */
157	uint32_t		lun_id;		/* returned from CTL */
158	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
159	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
160	be_callback_t		lun_shutdown;	/* passed to CTL */
161	struct ctl_backend_driver *be;		/* passed to CTL */
162	void			*ctl_lun;	/* used by CTL */
163	ctl_options_t		options;	/* passed to CTL */
164	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
165};
166
167typedef enum {
168	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
169	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
170} ctl_backend_flags;
171
172typedef int (*be_init_t)(void);
173typedef int (*be_shutdown_t)(void);
174typedef int (*be_func_t)(union ctl_io *io);
175typedef void (*be_vfunc_t)(union ctl_io *io);
176typedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
177			  struct thread *td);
178typedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
179typedef uint64_t (*be_lunattr_t)(void *be_lun, const char *attrname);
180
181struct ctl_backend_driver {
182	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
183	ctl_backend_flags flags;	         /* passed to CTL */
184	be_init_t	  init;			 /* passed to CTL */
185	be_shutdown_t	  shutdown;		 /* passed to CTL */
186	be_func_t	  data_submit;		 /* passed to CTL */
187	be_func_t	  data_move_done;	 /* passed to CTL */
188	be_func_t	  config_read;		 /* passed to CTL */
189	be_func_t	  config_write;		 /* passed to CTL */
190	be_ioctl_t	  ioctl;		 /* passed to CTL */
191	be_luninfo_t	  lun_info;		 /* passed to CTL */
192	be_lunattr_t	  lun_attr;		 /* passed to CTL */
193#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
194	be_func_t	  config_move_done;	 /* passed to backend */
195#endif
196#if 0
197	be_vfunc_t	  config_write_done;	 /* passed to backend */
198#endif
199	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
200};
201
202int ctl_backend_register(struct ctl_backend_driver *be);
203int ctl_backend_deregister(struct ctl_backend_driver *be);
204struct ctl_backend_driver *ctl_backend_find(char *backend_name);
205
206/*
207 * To add a LUN, call ctl_add_lun().
208 */
209int ctl_add_lun(struct ctl_be_lun *be_lun);
210
211/*
212 * To remove a LUN, first call ctl_remove_lun().
213 * You will get the lun_shutdown() callback when all
214 * I/O to the LUN has completed and the LUN has been deleted.
215 */
216int ctl_remove_lun(struct ctl_be_lun *be_lun);
217
218/*
219 * To start a LUN (transition from powered off to powered on state) call
220 * ctl_start_lun().  To stop a LUN (transition from powered on to powered
221 * off state) call ctl_stop_lun().
222 */
223int ctl_start_lun(struct ctl_be_lun *be_lun);
224int ctl_stop_lun(struct ctl_be_lun *be_lun);
225
226/*
227 * Methods to notify about media and tray status changes.
228 */
229int ctl_lun_no_media(struct ctl_be_lun *be_lun);
230int ctl_lun_has_media(struct ctl_be_lun *be_lun);
231int ctl_lun_ejected(struct ctl_be_lun *be_lun);
232
233/*
234 * Called on LUN HA role change.
235 */
236int ctl_lun_primary(struct ctl_be_lun *be_lun);
237int ctl_lun_secondary(struct ctl_be_lun *be_lun);
238
239/*
240 * Let the backend notify the initiators about changes.
241 */
242void ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
243
244#endif /* _KERNEL */
245#endif /* _CTL_BACKEND_H_ */
246
247/*
248 * vim: ts=8
249 */
250