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