1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3312840Smav * 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/11/sys/cam/ctl/ctl_backend.h 361256 2020-05-19 14:42:09Z 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
43312840Smav#include <cam/ctl/ctl_ioctl.h>
44229997Sken
45229997Skentypedef enum {
46287499Smav	CTL_LUN_SERSEQ_OFF,
47287499Smav	CTL_LUN_SERSEQ_READ,
48287499Smav	CTL_LUN_SERSEQ_ON
49287499Smav} ctl_lun_serseq;
50287499Smav
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: \
58313368Smav			return (ctl_backend_register( \
59313368Smav				(struct ctl_backend_driver *)data)); \
60229997Sken			break; \
61229997Sken		case MOD_UNLOAD: \
62313368Smav			return (ctl_backend_deregister( \
63313368Smav				(struct ctl_backend_driver *)data)); \
64313368Smav			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 void (*be_callback_t)(void *be_lun);
81229997Sken
82229997Sken/*
83229997Sken * The lun_type field is the SCSI device type of this particular LUN.  In
84229997Sken * general, this should be T_DIRECT, although backends will want to create
85229997Sken * a processor LUN, typically at LUN 0.  See scsi_all.h for the defines for
86229997Sken * the various SCSI device types.
87229997Sken *
88229997Sken * The flags are described above.
89229997Sken *
90229997Sken * The be_lun field is the backend driver's own context that will get
91229997Sken * passsed back so that it can tell which LUN CTL is referencing.
92229997Sken *
93229997Sken * maxlba is the maximum accessible LBA on the LUN.  Note that this is
94229997Sken * different from the capacity of the array.  capacity = maxlba + 1
95229997Sken *
96229997Sken * blocksize is the size, in bytes, of each LBA on the LUN.  In general
97229997Sken * this should be 512.  In theory CTL should be able to handle other block
98229997Sken * sizes.  Host application software may not deal with it very well, though.
99229997Sken *
100264191Smav * pblockexp is the log2() of number of LBAs on the LUN per physical sector.
101264191Smav *
102275920Smav * pblockoff is the lowest LBA on the LUN aligned to physical sector.
103264191Smav *
104275920Smav * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block.
105275920Smav *
106275920Smav * ublockoff is the lowest LBA on the LUN aligned to UNMAP block.
107275920Smav *
108272734Smav * atomicblock is the number of blocks that can be written atomically.
109272734Smav *
110275920Smav * opttxferlen is the number of blocks that can be written in one operation.
111275920Smav *
112229997Sken * req_lun_id is the requested LUN ID.  CTL only pays attention to this
113229997Sken * field if the CTL_LUN_FLAG_ID_REQ flag is set.  If the requested LUN ID is
114229997Sken * not available, the LUN addition will fail.  If a particular LUN ID isn't
115229997Sken * requested, the first available LUN ID will be allocated.
116229997Sken *
117229997Sken * serial_num is the device serial number returned in the SCSI INQUIRY VPD
118229997Sken * page 0x80.  This should be a unique, per-shelf value.  The data inside
119229997Sken * this field should be ASCII only, left aligned, and any unused space
120229997Sken * should be padded out with ASCII spaces.  This field should NOT be NULL
121229997Sken * terminated.
122229997Sken *
123229997Sken * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD
124229997Sken * page 0x83.  This should be a unique, per-LUN value.  The data inside
125229997Sken * this field should be ASCII only, left aligned, and any unused space
126229997Sken * should be padded with ASCII spaces.  This field should NOT be NULL
127229997Sken * terminated.
128229997Sken *
129361256Smav * The lun_shutdown() method is the callback for the ctl_remove_lun()
130229997Sken * call.  It is called when all outstanding I/O for that LUN has been
131229997Sken * completed and CTL has deleted the resources for that LUN.  When the CTL
132229997Sken * backend gets this call, it can safely free its per-LUN resources.
133229997Sken *
134229997Sken * The be field is a pointer to the ctl_backend_driver structure, which
135229997Sken * contains the backend methods to be called by CTL.
136229997Sken *
137229997Sken * The ctl_lun field is for CTL internal use only, and should not be used
138229997Sken * by the backend.
139229997Sken *
140229997Sken * The links field is for CTL internal use only, and should not be used by
141229997Sken * the backend.
142229997Sken */
143229997Skenstruct ctl_be_lun {
144229997Sken	uint8_t			lun_type;	/* passed to CTL */
145229997Sken	ctl_backend_lun_flags	flags;		/* passed to CTL */
146287499Smav	ctl_lun_serseq		serseq;		/* passed to CTL */
147229997Sken	void			*be_lun;	/* passed to CTL */
148229997Sken	uint64_t		maxlba;		/* passed to CTL */
149229997Sken	uint32_t		blocksize;	/* passed to CTL */
150264191Smav	uint16_t		pblockexp;	/* passed to CTL */
151264191Smav	uint16_t		pblockoff;	/* passed to CTL */
152275865Smav	uint16_t		ublockexp;	/* passed to CTL */
153275865Smav	uint16_t		ublockoff;	/* passed to CTL */
154272734Smav	uint32_t		atomicblock;	/* passed to CTL */
155275920Smav	uint32_t		opttxferlen;	/* passed to CTL */
156229997Sken	uint32_t		req_lun_id;	/* passed to CTL */
157229997Sken	uint32_t		lun_id;		/* returned from CTL */
158229997Sken	uint8_t			serial_num[CTL_SN_LEN];	 /* passed to CTL */
159229997Sken	uint8_t			device_id[CTL_DEVID_LEN];/* passed to CTL */
160229997Sken	be_callback_t		lun_shutdown;	/* passed to CTL */
161229997Sken	struct ctl_backend_driver *be;		/* passed to CTL */
162229997Sken	void			*ctl_lun;	/* used by CTL */
163268280Smav	ctl_options_t		options;	/* passed to CTL */
164229997Sken	STAILQ_ENTRY(ctl_be_lun) links;		/* used by CTL */
165229997Sken};
166229997Sken
167229997Skentypedef enum {
168229997Sken	CTL_BE_FLAG_NONE	= 0x00,	/* no flags */
169229997Sken	CTL_BE_FLAG_HAS_CONFIG	= 0x01,	/* can do config reads, writes */
170229997Sken} ctl_backend_flags;
171229997Sken
172229997Skentypedef int (*be_init_t)(void);
173313368Smavtypedef int (*be_shutdown_t)(void);
174229997Skentypedef int (*be_func_t)(union ctl_io *io);
175229997Skentypedef void (*be_vfunc_t)(union ctl_io *io);
176229997Skentypedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
177229997Sken			  struct thread *td);
178229997Skentypedef int (*be_luninfo_t)(void *be_lun, struct sbuf *sb);
179274154Smavtypedef uint64_t (*be_lunattr_t)(void *be_lun, const char *attrname);
180229997Sken
181229997Skenstruct ctl_backend_driver {
182229997Sken	char		  name[CTL_BE_NAME_LEN]; /* passed to CTL */
183229997Sken	ctl_backend_flags flags;	         /* passed to CTL */
184229997Sken	be_init_t	  init;			 /* passed to CTL */
185313368Smav	be_shutdown_t	  shutdown;		 /* passed to CTL */
186229997Sken	be_func_t	  data_submit;		 /* passed to CTL */
187229997Sken	be_func_t	  data_move_done;	 /* passed to CTL */
188229997Sken	be_func_t	  config_read;		 /* passed to CTL */
189229997Sken	be_func_t	  config_write;		 /* passed to CTL */
190229997Sken	be_ioctl_t	  ioctl;		 /* passed to CTL */
191229997Sken	be_luninfo_t	  lun_info;		 /* passed to CTL */
192274154Smav	be_lunattr_t	  lun_attr;		 /* passed to CTL */
193229997Sken#ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED
194229997Sken	be_func_t	  config_move_done;	 /* passed to backend */
195229997Sken#endif
196229997Sken#if 0
197229997Sken	be_vfunc_t	  config_write_done;	 /* passed to backend */
198229997Sken#endif
199229997Sken	STAILQ_ENTRY(ctl_backend_driver) links;	 /* used by CTL */
200229997Sken};
201229997Sken
202229997Skenint ctl_backend_register(struct ctl_backend_driver *be);
203229997Skenint ctl_backend_deregister(struct ctl_backend_driver *be);
204229997Skenstruct ctl_backend_driver *ctl_backend_find(char *backend_name);
205229997Sken
206229997Sken/*
207361256Smav * To add a LUN, call ctl_add_lun().
208229997Sken */
209229997Skenint ctl_add_lun(struct ctl_be_lun *be_lun);
210229997Sken
211229997Sken/*
212361256Smav * To remove a LUN, first call ctl_remove_lun().
213361256Smav * You will get the lun_shutdown() callback when all
214229997Sken * I/O to the LUN has completed and the LUN has been deleted.
215229997Sken */
216361256Smavint ctl_remove_lun(struct ctl_be_lun *be_lun);
217229997Sken
218229997Sken/*
219229997Sken * To start a LUN (transition from powered off to powered on state) call
220229997Sken * ctl_start_lun().  To stop a LUN (transition from powered on to powered
221229997Sken * off state) call ctl_stop_lun().
222229997Sken */
223229997Skenint ctl_start_lun(struct ctl_be_lun *be_lun);
224229997Skenint ctl_stop_lun(struct ctl_be_lun *be_lun);
225229997Sken
226229997Sken/*
227288348Smav * Methods to notify about media and tray status changes.
228229997Sken */
229288348Smavint ctl_lun_no_media(struct ctl_be_lun *be_lun);
230288348Smavint ctl_lun_has_media(struct ctl_be_lun *be_lun);
231288348Smavint ctl_lun_ejected(struct ctl_be_lun *be_lun);
232229997Sken
233229997Sken/*
234287621Smav * Called on LUN HA role change.
235287621Smav */
236287621Smavint ctl_lun_primary(struct ctl_be_lun *be_lun);
237287621Smavint ctl_lun_secondary(struct ctl_be_lun *be_lun);
238287621Smav
239287621Smav/*
240288348Smav * Let the backend notify the initiators about changes.
241232604Strasz */
242232604Straszvoid ctl_lun_capacity_changed(struct ctl_be_lun *be_lun);
243232604Strasz
244229997Sken#endif /* _KERNEL */
245229997Sken#endif /* _CTL_BACKEND_H_ */
246229997Sken
247229997Sken/*
248229997Sken * vim: ts=8
249229997Sken */
250