1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3312841Smav * Copyright (c) 2014-2017 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: stable/10/sys/cam/ctl/ctl_backend.h 313369 2017-02-07 01:56:26Z 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
43312841Smav#include <cam/ctl/ctl_ioctl.h>
44229997Sken
45229997Skentypedef enum {
46288727Smav	CTL_LUN_SERSEQ_OFF,
47288727Smav	CTL_LUN_SERSEQ_READ,
48288727Smav	CTL_LUN_SERSEQ_ON
49288727Smav} ctl_lun_serseq;
50288727Smav
51229997Sken#ifdef _KERNEL
52229997Sken
53229997Sken#define CTL_BACKEND_DECLARE(name, driver) \
54229997Sken	static int name ## _modevent(module_t mod, int type, void *data) \
55229997Sken	{ \
56229997Sken		switch (type) { \
57229997Sken		case MOD_LOAD: \
58313369Smav			return (ctl_backend_register( \
59313369Smav				(struct ctl_backend_driver *)data)); \
60229997Sken			break; \
61229997Sken		case MOD_UNLOAD: \
62313369Smav			return (ctl_backend_deregister( \
63313369Smav				(struct ctl_backend_driver *)data)); \
64313369Smav			break; \
65229997Sken		default: \
66229997Sken			return EOPNOTSUPP; \
67229997Sken		} \
68229997Sken		return 0; \
69229997Sken	} \
70229997Sken	static moduledata_t name ## _mod = { \
71229997Sken		#name, \
72229997Sken		name ## _modevent, \
73229997Sken		(void *)&driver \
74229997Sken	}; \
75229997Sken	DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \
76229997Sken	MODULE_DEPEND(name, ctl, 1, 1, 1); \
77229997Sken	MODULE_DEPEND(name, cam, 1, 1, 1)
78229997Sken
79229997Sken
80229997Skentypedef enum {
81229997Sken	CTL_LUN_CONFIG_OK,
82229997Sken	CTL_LUN_CONFIG_FAILURE
83229997Sken} ctl_lun_config_status;
84229997Sken
85229997Skentypedef void (*be_callback_t)(void *be_lun);
86229997Skentypedef void (*be_lun_config_t)(void *be_lun,
87229997Sken				ctl_lun_config_status status);
88229997Sken
89229997Sken/*
90229997Sken * The lun_type field is the SCSI device type of this particular LUN.  In
91229997Sken * general, this should be T_DIRECT, although backends will want to create
92229997Sken * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
93229997Sken * the various SCSI device types.
94229997Sken *
95229997Sken * The flags are described above.
96229997Sken *
97229997Sken * The be_lun field is the backend driver's own context that will get
98229997Sken * passsed back so that it can tell which LUN CTL is referencing.
99229997Sken *
100229997Sken * maxlba is the maximum accessible LBA on the LUN.  Note that this is
101229997Sken * different from the capacity of the array.  capacity = maxlba + 1
102229997Sken *
103229997Sken * blocksize is the size, in bytes, of each LBA on the LUN.  In general
104229997Sken * this should be 512.  In theory CTL should be able to handle other block
105229997Sken * sizes.  Host application software may not deal with it very well, though.
106229997Sken *
107264727Smav * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
108264727Smav *
109276237Smav * pblockoff is the lowest LBA on the LUN aligned to physical sector.
110264727Smav *
111276237Smav * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block.
112276237Smav *
113276237Smav * ublockoff is the lowest LBA on the LUN aligned to UNMAP block.
114276237Smav *
115273311Smav * atomicblock is the number of blocks that can be written atomically.
116273311Smav *
117276237Smav * opttxferlen is the number of blocks that can be written in one operation.
118276237Smav *
119229997Sken * req_lun_id is the requested LUN ID.  CTL only pays attention to this
120229997Sken * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
121229997Sken * not available, the LUN addition will fail.  If a particular LUN ID isn't
122229997Sken * requested, the first available LUN ID will be allocated.
123229997Sken *
124229997Sken * serial_num is the device serial number returned in the SCSI INQUIRY VPD
125229997Sken * page 0x80.  This should be a unique, per-shelf value.  The data inside
126229997Sken * this field should be ASCII only, left aligned, and any unused space
127229997Sken * should be padded out with ASCII spaces.  This field should NOT be NULL
128229997Sken * terminated.
129229997Sken *
130229997Sken * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
131229997Sken * page 0x83.  This should be a unique, per-LUN value.  The data inside
132229997Sken * this field should be ASCII only, left aligned, and any unused space
133229997Sken * should be padded with ASCII spaces.  This field should NOT be NULL
134229997Sken * terminated.
135229997Sken *
136229997Sken * The lun_shutdown() method is the callback for the ctl_invalidate_lun()
137229997Sken * call.  It is called when all outstanding I/O for that LUN has been
138229997Sken * completed and CTL has deleted the resources for that LUN.  When the CTL
139229997Sken * backend gets this call, it can safely free its per-LUN resources.
140229997Sken *
141229997Sken * The lun_config_status() method is the callback for the ctl_add_lun()
142229997Sken * call.  It is called when the LUN is successfully added, or when LUN
143229997Sken * addition fails.  If the LUN is successfully added, the backend may call
144229997Sken * the ctl_enable_lun() method to enable the LUN.
145229997Sken *
146229997Sken * The be field is a pointer to the ctl_backend_driver structure, which
147229997Sken * contains the backend methods to be called by CTL.
148229997Sken *
149229997Sken * The ctl_lun field is for CTL internal use only, and should not be used
150229997Sken * by the backend.
151229997Sken *
152229997Sken * The links field is for CTL internal use only, and should not be used by
153229997Sken * the backend.
154229997Sken */
155229997Skenstruct ctl_be_lun {
156229997Sken	uint8_t			lun_type;	/* passed to CTL */
157229997Sken	ctl_backend_lun_flags	flags;		/* passed to CTL */
158288727Smav	ctl_lun_serseq		serseq;		/* passed to CTL */
159229997Sken	void			*be_lun;	/* passed to CTL */
160229997Sken	uint64_t		maxlba;		/* passed to CTL */
161229997Sken	uint32_t		blocksize;	/* passed to CTL */
162264727Smav	uint16_t		pblockexp;	/* passed to CTL */
163264727Smav	uint16_t		pblockoff;	/* passed to CTL */
164276179Smav	uint16_t		ublockexp;	/* passed to CTL */
165276179Smav	uint16_t		ublockoff;	/* passed to CTL */
166273311Smav	uint32_t		atomicblock;	/* passed to CTL */
167276237Smav	uint32_t		opttxferlen;	/* passed to CTL */
168229997Sken	uint32_t		req_lun_id;	/* passed to CTL */
169229997Sken	uint32_t		lun_id;		/* returned from CTL */
170229997Sken	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
171229997Sken	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
172229997Sken	be_callback_t		lun_shutdown;	/* passed to CTL */
173229997Sken	be_lun_config_t		lun_config_status; /* passed to CTL */
174229997Sken	struct ctl_backend_driver *be;		/* passed to CTL */
175229997Sken	void			*ctl_lun;	/* used by CTL */
176268678Smav	ctl_options_t		options;	/* passed to CTL */
177229997Sken	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
178229997Sken};
179229997Sken
180229997Skentypedef enum {
181229997Sken	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
182229997Sken	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
183229997Sken} ctl_backend_flags;
184229997Sken
185229997Skentypedef int (*be_init_t)(void);
186313369Smavtypedef int (*be_shutdown_t)(void);
187229997Skentypedef int (*be_func_t)(union ctl_io *io);
188229997Skentypedef void (*be_vfunc_t)(union ctl_io *io);
189229997Skentypedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
190229997Sken			  struct thread *td);
191229997Skentypedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
192274732Smavtypedef uint64_t (*be_lunattr_t)(void *be_lun, const char *attrname);
193229997Sken
194229997Skenstruct ctl_backend_driver {
195229997Sken	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
196229997Sken	ctl_backend_flags flags;	         /* passed to CTL */
197229997Sken	be_init_t	  init;			 /* passed to CTL */
198313369Smav	be_shutdown_t	  shutdown;		 /* passed to CTL */
199229997Sken	be_func_t	  data_submit;		 /* passed to CTL */
200229997Sken	be_func_t	  data_move_done;	 /* passed to CTL */
201229997Sken	be_func_t	  config_read;		 /* passed to CTL */
202229997Sken	be_func_t	  config_write;		 /* passed to CTL */
203229997Sken	be_ioctl_t	  ioctl;		 /* passed to CTL */
204229997Sken	be_luninfo_t	  lun_info;		 /* passed to CTL */
205274732Smav	be_lunattr_t	  lun_attr;		 /* passed to CTL */
206229997Sken#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
207229997Sken	be_func_t	  config_move_done;	 /* passed to backend */
208229997Sken#endif
209229997Sken#if 0
210229997Sken	be_vfunc_t	  config_write_done;	 /* passed to backend */
211229997Sken#endif
212229997Sken	u_int		  num_luns;		 /* used by CTL */
213229997Sken	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
214229997Sken};
215229997Sken
216229997Skenint ctl_backend_register(struct ctl_backend_driver *be);
217229997Skenint ctl_backend_deregister(struct ctl_backend_driver *be);
218229997Skenstruct ctl_backend_driver *ctl_backend_find(char *backend_name);
219229997Sken
220229997Sken/*
221229997Sken * To add a LUN, first call ctl_add_lun().  You will get the lun_config_status()
222229997Sken * callback when the LUN addition has either succeeded or failed.
223229997Sken *
224229997Sken * Once you get that callback, you can then call ctl_enable_lun() to enable
225229997Sken * the LUN.
226229997Sken */
227229997Skenint ctl_add_lun(struct ctl_be_lun *be_lun);
228229997Skenint ctl_enable_lun(struct ctl_be_lun *be_lun);
229229997Sken
230229997Sken/*
231229997Sken * To delete a LUN, first call ctl_disable_lun(), then
232229997Sken * ctl_invalidate_lun().  You will get the lun_shutdown() callback when all
233229997Sken * I/O to the LUN has completed and the LUN has been deleted.
234229997Sken */
235229997Skenint ctl_disable_lun(struct ctl_be_lun *be_lun);
236229997Skenint ctl_invalidate_lun(struct ctl_be_lun *be_lun);
237229997Sken
238229997Sken/*
239229997Sken * To start a LUN (transition from powered off to powered on state) call
240229997Sken * ctl_start_lun().  To stop a LUN (transition from powered on to powered
241229997Sken * off state) call ctl_stop_lun().
242229997Sken */
243229997Skenint ctl_start_lun(struct ctl_be_lun *be_lun);
244229997Skenint ctl_stop_lun(struct ctl_be_lun *be_lun);
245229997Sken
246229997Sken/*
247288811Smav * Methods to notify about media and tray status changes.
248229997Sken */
249288811Smavint ctl_lun_no_media(struct ctl_be_lun *be_lun);
250288811Smavint ctl_lun_has_media(struct ctl_be_lun *be_lun);
251288811Smavint ctl_lun_ejected(struct ctl_be_lun *be_lun);
252229997Sken
253229997Sken/*
254288732Smav * Called on LUN HA role change.
255288732Smav */
256288732Smavint ctl_lun_primary(struct ctl_be_lun *be_lun);
257288732Smavint ctl_lun_secondary(struct ctl_be_lun *be_lun);
258288732Smav
259288732Smav/*
260288811Smav * Let the backend notify the initiators about changes.
261232604Strasz */
262232604Straszvoid ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
263232604Strasz
264229997Sken#endif /* _KERNEL */
265229997Sken#endif /* _CTL_BACKEND_H_ */
266229997Sken
267229997Sken/*
268229997Sken * vim: ts=8
269229997Sken */
270