1130f4520SKenneth D. Merry /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3bec9534dSPedro F. Giffuni * 4130f4520SKenneth D. Merry * Copyright (c) 2003 Silicon Graphics International Corp. 5bb8f9017SAlexander Motin * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org> 6130f4520SKenneth D. Merry * All rights reserved. 7130f4520SKenneth D. Merry * 8130f4520SKenneth D. Merry * Redistribution and use in source and binary forms, with or without 9130f4520SKenneth D. Merry * modification, are permitted provided that the following conditions 10130f4520SKenneth D. Merry * are met: 11130f4520SKenneth D. Merry * 1. Redistributions of source code must retain the above copyright 12130f4520SKenneth D. Merry * notice, this list of conditions, and the following disclaimer, 13130f4520SKenneth D. Merry * without modification. 14130f4520SKenneth D. Merry * 2. Redistributions in binary form must reproduce at minimum a disclaimer 15130f4520SKenneth D. Merry * substantially similar to the "NO WARRANTY" disclaimer below 16130f4520SKenneth D. Merry * ("Disclaimer") and any redistribution must be conditioned upon 17130f4520SKenneth D. Merry * including a substantially similar Disclaimer requirement for further 18130f4520SKenneth D. Merry * binary redistribution. 19130f4520SKenneth D. Merry * 20130f4520SKenneth D. Merry * NO WARRANTY 21130f4520SKenneth D. Merry * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22130f4520SKenneth D. Merry * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23130f4520SKenneth D. Merry * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 24130f4520SKenneth D. Merry * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25130f4520SKenneth D. Merry * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26130f4520SKenneth D. Merry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27130f4520SKenneth D. Merry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28130f4520SKenneth D. Merry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29130f4520SKenneth D. Merry * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30130f4520SKenneth D. Merry * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31130f4520SKenneth D. Merry * POSSIBILITY OF SUCH DAMAGES. 32130f4520SKenneth D. Merry * 33130f4520SKenneth D. Merry * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend.h#2 $ 34130f4520SKenneth D. Merry */ 35130f4520SKenneth D. Merry /* 36130f4520SKenneth D. Merry * CTL backend driver definitions 37130f4520SKenneth D. Merry * 38130f4520SKenneth D. Merry * Author: Ken Merry <ken@FreeBSD.org> 39130f4520SKenneth D. Merry */ 40130f4520SKenneth D. Merry 41130f4520SKenneth D. Merry #ifndef _CTL_BACKEND_H_ 42130f4520SKenneth D. Merry #define _CTL_BACKEND_H_ 43130f4520SKenneth D. Merry 44bb8f9017SAlexander Motin #include <cam/ctl/ctl_ioctl.h> 458951f055SMarcelo Araujo #include <sys/nv.h> 46130f4520SKenneth D. Merry 470bcd4ab6SAlexander Motin typedef enum { 480bcd4ab6SAlexander Motin CTL_LUN_SERSEQ_OFF, 49ac503c19SAlexander Motin CTL_LUN_SERSEQ_SOFT, 500bcd4ab6SAlexander Motin CTL_LUN_SERSEQ_READ, 510bcd4ab6SAlexander Motin CTL_LUN_SERSEQ_ON 520bcd4ab6SAlexander Motin } ctl_lun_serseq; 530bcd4ab6SAlexander Motin 54130f4520SKenneth D. Merry #ifdef _KERNEL 55130f4520SKenneth D. Merry 56130f4520SKenneth D. Merry #define CTL_BACKEND_DECLARE(name, driver) \ 57130f4520SKenneth D. Merry static int name ## _modevent(module_t mod, int type, void *data) \ 58130f4520SKenneth D. Merry { \ 59130f4520SKenneth D. Merry switch (type) { \ 60130f4520SKenneth D. Merry case MOD_LOAD: \ 610c629e28SAlexander Motin return (ctl_backend_register( \ 620c629e28SAlexander Motin (struct ctl_backend_driver *)data)); \ 63130f4520SKenneth D. Merry break; \ 64130f4520SKenneth D. Merry case MOD_UNLOAD: \ 650c629e28SAlexander Motin return (ctl_backend_deregister( \ 660c629e28SAlexander Motin (struct ctl_backend_driver *)data)); \ 670c629e28SAlexander Motin break; \ 68130f4520SKenneth D. Merry default: \ 69130f4520SKenneth D. Merry return EOPNOTSUPP; \ 70130f4520SKenneth D. Merry } \ 71130f4520SKenneth D. Merry return 0; \ 72130f4520SKenneth D. Merry } \ 73130f4520SKenneth D. Merry static moduledata_t name ## _mod = { \ 74130f4520SKenneth D. Merry #name, \ 75130f4520SKenneth D. Merry name ## _modevent, \ 76130f4520SKenneth D. Merry (void *)&driver \ 77130f4520SKenneth D. Merry }; \ 78130f4520SKenneth D. Merry DECLARE_MODULE(name, name ## _mod, SI_SUB_CONFIGURE, SI_ORDER_FOURTH); \ 79130f4520SKenneth D. Merry MODULE_DEPEND(name, ctl, 1, 1, 1); \ 80130f4520SKenneth D. Merry MODULE_DEPEND(name, cam, 1, 1, 1) 81130f4520SKenneth D. Merry 82767300e8SAlexander Motin struct ctl_be_lun; 83767300e8SAlexander Motin typedef void (*be_callback_t)(struct ctl_be_lun *be_lun); 84130f4520SKenneth D. Merry 85130f4520SKenneth D. Merry /* 86130f4520SKenneth D. Merry * The lun_type field is the SCSI device type of this particular LUN. In 87130f4520SKenneth D. Merry * general, this should be T_DIRECT, although backends will want to create 88130f4520SKenneth D. Merry * a processor LUN, typically at LUN 0. See scsi_all.h for the defines for 89130f4520SKenneth D. Merry * the various SCSI device types. 90130f4520SKenneth D. Merry * 91130f4520SKenneth D. Merry * The flags are described above. 92130f4520SKenneth D. Merry * 93130f4520SKenneth D. Merry * The be_lun field is the backend driver's own context that will get 94130f4520SKenneth D. Merry * passsed back so that it can tell which LUN CTL is referencing. 95130f4520SKenneth D. Merry * 96130f4520SKenneth D. Merry * maxlba is the maximum accessible LBA on the LUN. Note that this is 97130f4520SKenneth D. Merry * different from the capacity of the array. capacity = maxlba + 1 98130f4520SKenneth D. Merry * 99130f4520SKenneth D. Merry * blocksize is the size, in bytes, of each LBA on the LUN. In general 100130f4520SKenneth D. Merry * this should be 512. In theory CTL should be able to handle other block 101130f4520SKenneth D. Merry * sizes. Host application software may not deal with it very well, though. 102130f4520SKenneth D. Merry * 103f6012722SAlexander Motin * pblockexp is the log2() of number of LBAs on the LUN per physical sector. 104f6012722SAlexander Motin * 105cb8727e2SAlexander Motin * pblockoff is the lowest LBA on the LUN aligned to physical sector. 106cb8727e2SAlexander Motin * 107cb8727e2SAlexander Motin * ublockexp is the log2() of number of LBAs on the LUN per UNMAP block. 108cb8727e2SAlexander Motin * 109cb8727e2SAlexander Motin * ublockoff is the lowest LBA on the LUN aligned to UNMAP block. 110f6012722SAlexander Motin * 1118a416753SAlexander Motin * atomicblock is the number of blocks that can be written atomically. 1128a416753SAlexander Motin * 113cb8727e2SAlexander Motin * opttxferlen is the number of blocks that can be written in one operation. 114cb8727e2SAlexander Motin * 115130f4520SKenneth D. Merry * req_lun_id is the requested LUN ID. CTL only pays attention to this 116130f4520SKenneth D. Merry * field if the CTL_LUN_FLAG_ID_REQ flag is set. If the requested LUN ID is 117130f4520SKenneth D. Merry * not available, the LUN addition will fail. If a particular LUN ID isn't 118130f4520SKenneth D. Merry * requested, the first available LUN ID will be allocated. 119130f4520SKenneth D. Merry * 120130f4520SKenneth D. Merry * serial_num is the device serial number returned in the SCSI INQUIRY VPD 121130f4520SKenneth D. Merry * page 0x80. This should be a unique, per-shelf value. The data inside 122130f4520SKenneth D. Merry * this field should be ASCII only, left aligned, and any unused space 123130f4520SKenneth D. Merry * should be padded out with ASCII spaces. This field should NOT be NULL 124130f4520SKenneth D. Merry * terminated. 125130f4520SKenneth D. Merry * 126130f4520SKenneth D. Merry * device_id is the T10 device identifier returned in the SCSI INQUIRY VPD 127130f4520SKenneth D. Merry * page 0x83. This should be a unique, per-LUN value. The data inside 128130f4520SKenneth D. Merry * this field should be ASCII only, left aligned, and any unused space 129130f4520SKenneth D. Merry * should be padded with ASCII spaces. This field should NOT be NULL 130130f4520SKenneth D. Merry * terminated. 131130f4520SKenneth D. Merry * 13234144c2cSAlexander Motin * The lun_shutdown() method is the callback for the ctl_remove_lun() 133130f4520SKenneth D. Merry * call. It is called when all outstanding I/O for that LUN has been 134130f4520SKenneth D. Merry * completed and CTL has deleted the resources for that LUN. When the CTL 135130f4520SKenneth D. Merry * backend gets this call, it can safely free its per-LUN resources. 136130f4520SKenneth D. Merry * 137130f4520SKenneth D. Merry * The be field is a pointer to the ctl_backend_driver structure, which 138130f4520SKenneth D. Merry * contains the backend methods to be called by CTL. 139130f4520SKenneth D. Merry * 140130f4520SKenneth D. Merry * The ctl_lun field is for CTL internal use only, and should not be used 141130f4520SKenneth D. Merry * by the backend. 142130f4520SKenneth D. Merry * 143130f4520SKenneth D. Merry * The links field is for CTL internal use only, and should not be used by 144130f4520SKenneth D. Merry * the backend. 145130f4520SKenneth D. Merry */ 146130f4520SKenneth D. Merry struct ctl_be_lun { 147130f4520SKenneth D. Merry uint8_t lun_type; /* passed to CTL */ 148130f4520SKenneth D. Merry ctl_backend_lun_flags flags; /* passed to CTL */ 1490bcd4ab6SAlexander Motin ctl_lun_serseq serseq; /* passed to CTL */ 150130f4520SKenneth D. Merry uint64_t maxlba; /* passed to CTL */ 151130f4520SKenneth D. Merry uint32_t blocksize; /* passed to CTL */ 152f6012722SAlexander Motin uint16_t pblockexp; /* passed to CTL */ 153f6012722SAlexander Motin uint16_t pblockoff; /* passed to CTL */ 15434961f40SAlexander Motin uint16_t ublockexp; /* passed to CTL */ 15534961f40SAlexander Motin uint16_t ublockoff; /* passed to CTL */ 1568a416753SAlexander Motin uint32_t atomicblock; /* passed to CTL */ 157cb8727e2SAlexander Motin uint32_t opttxferlen; /* passed to CTL */ 158130f4520SKenneth D. Merry uint32_t req_lun_id; /* passed to CTL */ 159130f4520SKenneth D. Merry uint32_t lun_id; /* returned from CTL */ 160130f4520SKenneth D. Merry uint8_t serial_num[CTL_SN_LEN]; /* passed to CTL */ 161130f4520SKenneth D. Merry uint8_t device_id[CTL_DEVID_LEN];/* passed to CTL */ 162130f4520SKenneth D. Merry be_callback_t lun_shutdown; /* passed to CTL */ 163130f4520SKenneth D. Merry struct ctl_backend_driver *be; /* passed to CTL */ 164130f4520SKenneth D. Merry void *ctl_lun; /* used by CTL */ 1658951f055SMarcelo Araujo nvlist_t *options; /* passed to CTL */ 166130f4520SKenneth D. Merry STAILQ_ENTRY(ctl_be_lun) links; /* used by CTL */ 167130f4520SKenneth D. Merry }; 168130f4520SKenneth D. Merry 169130f4520SKenneth D. Merry typedef enum { 170130f4520SKenneth D. Merry CTL_BE_FLAG_NONE = 0x00, /* no flags */ 171130f4520SKenneth D. Merry CTL_BE_FLAG_HAS_CONFIG = 0x01, /* can do config reads, writes */ 172130f4520SKenneth D. Merry } ctl_backend_flags; 173130f4520SKenneth D. Merry 174130f4520SKenneth D. Merry typedef int (*be_init_t)(void); 1750c629e28SAlexander Motin typedef int (*be_shutdown_t)(void); 176130f4520SKenneth D. Merry typedef int (*be_func_t)(union ctl_io *io); 177130f4520SKenneth D. Merry typedef void (*be_vfunc_t)(union ctl_io *io); 178130f4520SKenneth D. Merry typedef int (*be_ioctl_t)(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 179130f4520SKenneth D. Merry struct thread *td); 180767300e8SAlexander Motin typedef int (*be_luninfo_t)(struct ctl_be_lun *be_lun, struct sbuf *sb); 181767300e8SAlexander Motin typedef uint64_t (*be_lunattr_t)(struct ctl_be_lun *be_lun, const char *attrname); 182130f4520SKenneth D. Merry 183130f4520SKenneth D. Merry struct ctl_backend_driver { 184130f4520SKenneth D. Merry char name[CTL_BE_NAME_LEN]; /* passed to CTL */ 185130f4520SKenneth D. Merry ctl_backend_flags flags; /* passed to CTL */ 186130f4520SKenneth D. Merry be_init_t init; /* passed to CTL */ 1870c629e28SAlexander Motin be_shutdown_t shutdown; /* passed to CTL */ 188130f4520SKenneth D. Merry be_func_t data_submit; /* passed to CTL */ 189130f4520SKenneth D. Merry be_func_t config_read; /* passed to CTL */ 190130f4520SKenneth D. Merry be_func_t config_write; /* passed to CTL */ 191130f4520SKenneth D. Merry be_ioctl_t ioctl; /* passed to CTL */ 192130f4520SKenneth D. Merry be_luninfo_t lun_info; /* passed to CTL */ 193c3e7ba3eSAlexander Motin be_lunattr_t lun_attr; /* passed to CTL */ 194130f4520SKenneth D. Merry #ifdef CS_BE_CONFIG_MOVE_DONE_IS_NOT_USED 195130f4520SKenneth D. Merry be_func_t config_move_done; /* passed to backend */ 196130f4520SKenneth D. Merry #endif 197130f4520SKenneth D. Merry #if 0 198130f4520SKenneth D. Merry be_vfunc_t config_write_done; /* passed to backend */ 199130f4520SKenneth D. Merry #endif 200130f4520SKenneth D. Merry STAILQ_ENTRY(ctl_backend_driver) links; /* used by CTL */ 201130f4520SKenneth D. Merry }; 202130f4520SKenneth D. Merry 203130f4520SKenneth D. Merry int ctl_backend_register(struct ctl_backend_driver *be); 204130f4520SKenneth D. Merry int ctl_backend_deregister(struct ctl_backend_driver *be); 205130f4520SKenneth D. Merry struct ctl_backend_driver *ctl_backend_find(char *backend_name); 206130f4520SKenneth D. Merry 207130f4520SKenneth D. Merry /* 20834144c2cSAlexander Motin * To add a LUN, call ctl_add_lun(). 209130f4520SKenneth D. Merry */ 210130f4520SKenneth D. Merry int ctl_add_lun(struct ctl_be_lun *be_lun); 211130f4520SKenneth D. Merry 212130f4520SKenneth D. Merry /* 21334144c2cSAlexander Motin * To remove a LUN, first call ctl_remove_lun(). 21434144c2cSAlexander Motin * You will get the lun_shutdown() callback when all 215130f4520SKenneth D. Merry * I/O to the LUN has completed and the LUN has been deleted. 216130f4520SKenneth D. Merry */ 21734144c2cSAlexander Motin int ctl_remove_lun(struct ctl_be_lun *be_lun); 218130f4520SKenneth D. Merry 219130f4520SKenneth D. Merry /* 220130f4520SKenneth D. Merry * To start a LUN (transition from powered off to powered on state) call 221130f4520SKenneth D. Merry * ctl_start_lun(). To stop a LUN (transition from powered on to powered 222130f4520SKenneth D. Merry * off state) call ctl_stop_lun(). 223130f4520SKenneth D. Merry */ 224130f4520SKenneth D. Merry int ctl_start_lun(struct ctl_be_lun *be_lun); 225130f4520SKenneth D. Merry int ctl_stop_lun(struct ctl_be_lun *be_lun); 226130f4520SKenneth D. Merry 227130f4520SKenneth D. Merry /* 228648dfc1aSAlexander Motin * Methods to notify about media and tray status changes. 229130f4520SKenneth D. Merry */ 230648dfc1aSAlexander Motin int ctl_lun_no_media(struct ctl_be_lun *be_lun); 231648dfc1aSAlexander Motin int ctl_lun_has_media(struct ctl_be_lun *be_lun); 232648dfc1aSAlexander Motin int ctl_lun_ejected(struct ctl_be_lun *be_lun); 233130f4520SKenneth D. Merry 23481177295SEdward Tomasz Napierala /* 2357ac58230SAlexander Motin * Called on LUN HA role change. 2367ac58230SAlexander Motin */ 2377ac58230SAlexander Motin int ctl_lun_primary(struct ctl_be_lun *be_lun); 2387ac58230SAlexander Motin int ctl_lun_secondary(struct ctl_be_lun *be_lun); 2397ac58230SAlexander Motin 2407ac58230SAlexander Motin /* 241648dfc1aSAlexander Motin * Let the backend notify the initiators about changes. 24281177295SEdward Tomasz Napierala */ 24381177295SEdward Tomasz Napierala void ctl_lun_capacity_changed(struct ctl_be_lun *be_lun); 24481177295SEdward Tomasz Napierala 245*bef5da8eSJohn Baldwin /* 246*bef5da8eSJohn Baldwin * Populate unique ID fields in NVMe namespace data for a LUN. 247*bef5da8eSJohn Baldwin */ 248*bef5da8eSJohn Baldwin void ctl_lun_nsdata_ids(struct ctl_be_lun *be_lun, 249*bef5da8eSJohn Baldwin struct nvme_namespace_data *nsdata); 250*bef5da8eSJohn Baldwin 251*bef5da8eSJohn Baldwin /* 252*bef5da8eSJohn Baldwin * Populate the NVMe namespace identification descriptor list for a LUN. 253*bef5da8eSJohn Baldwin */ 254*bef5da8eSJohn Baldwin void ctl_lun_nvme_ids(struct ctl_be_lun *be_lun, void *data); 255*bef5da8eSJohn Baldwin 256130f4520SKenneth D. Merry #endif /* _KERNEL */ 257130f4520SKenneth D. Merry #endif /* _CTL_BACKEND_H_ */ 258130f4520SKenneth D. Merry 259130f4520SKenneth D. Merry /* 260130f4520SKenneth D. Merry * vim: ts=8 261130f4520SKenneth D. Merry */ 262