1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>. 5 * Copyright (c) Intel Corporation. 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 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "scsi_internal.h" 36 37 static struct spdk_scsi_dev g_devs[SPDK_SCSI_MAX_DEVS]; 38 39 struct spdk_scsi_dev * 40 spdk_scsi_dev_get_list(void) 41 { 42 return g_devs; 43 } 44 45 static struct spdk_scsi_dev * 46 allocate_dev(void) 47 { 48 struct spdk_scsi_dev *dev; 49 int i; 50 51 for (i = 0; i < SPDK_SCSI_MAX_DEVS; i++) { 52 dev = &g_devs[i]; 53 if (!dev->is_allocated) { 54 memset(dev, 0, sizeof(*dev)); 55 dev->id = i; 56 dev->is_allocated = 1; 57 return dev; 58 } 59 } 60 61 return NULL; 62 } 63 64 static void 65 free_dev(struct spdk_scsi_dev *dev) 66 { 67 dev->is_allocated = 0; 68 } 69 70 void 71 spdk_scsi_dev_destruct(struct spdk_scsi_dev *dev) 72 { 73 int i; 74 75 if (dev == NULL) { 76 return; 77 } 78 79 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 80 if (dev->lun[i] == NULL) { 81 continue; 82 } 83 84 spdk_scsi_lun_unclaim(dev->lun[i]); 85 spdk_scsi_lun_destruct(dev->lun[i]); 86 dev->lun[i] = NULL; 87 } 88 89 free_dev(dev); 90 } 91 92 static int 93 spdk_scsi_dev_add_lun(struct spdk_scsi_dev *dev, 94 struct spdk_scsi_lun *lun, int id) 95 { 96 int rc; 97 98 rc = spdk_scsi_lun_claim(lun); 99 if (rc < 0) { 100 return rc; 101 } 102 103 lun->id = id; 104 lun->dev = dev; 105 dev->lun[id] = lun; 106 107 return 0; 108 } 109 110 void 111 spdk_scsi_dev_delete_lun(struct spdk_scsi_dev *dev, 112 struct spdk_scsi_lun *lun) 113 { 114 int i; 115 116 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 117 if (dev->lun[i] == lun) 118 dev->lun[i] = NULL; 119 } 120 } 121 122 /* This typedef exists to work around an astyle 2.05 bug. 123 * Remove it when astyle is fixed. 124 */ 125 typedef struct spdk_scsi_dev _spdk_scsi_dev; 126 127 _spdk_scsi_dev * 128 spdk_scsi_dev_construct(const char *name, char *lun_name_list[], int *lun_id_list, int num_luns, 129 uint8_t protocol_id, void (*hotremove_cb)(const struct spdk_scsi_lun *, void *), 130 void *hotremove_ctx) 131 { 132 struct spdk_scsi_dev *dev; 133 struct spdk_bdev *bdev; 134 struct spdk_scsi_lun *lun = NULL; 135 int i, rc; 136 137 if (num_luns == 0) { 138 SPDK_ERRLOG("device %s: no LUNs specified\n", name); 139 return NULL; 140 } 141 142 if (lun_id_list[0] != 0) { 143 SPDK_ERRLOG("device %s: no LUN 0 specified\n", name); 144 return NULL; 145 } 146 147 for (i = 0; i < num_luns; i++) { 148 if (lun_name_list[i] == NULL) { 149 SPDK_ERRLOG("NULL spdk_scsi_lun for LUN %d\n", 150 lun_id_list[i]); 151 return NULL; 152 } 153 } 154 155 dev = allocate_dev(); 156 if (dev == NULL) { 157 return NULL; 158 } 159 160 strncpy(dev->name, name, SPDK_SCSI_DEV_MAX_NAME); 161 162 dev->num_ports = 0; 163 dev->protocol_id = protocol_id; 164 165 for (i = 0; i < num_luns; i++) { 166 bdev = spdk_bdev_get_by_name(lun_name_list[i]); 167 if (bdev == NULL) { 168 goto error; 169 } 170 171 lun = spdk_scsi_lun_construct(spdk_bdev_get_name(bdev), bdev, hotremove_cb, hotremove_ctx); 172 if (lun == NULL) { 173 goto error; 174 } 175 176 rc = spdk_scsi_dev_add_lun(dev, lun, lun_id_list[i]); 177 if (rc < 0) { 178 spdk_scsi_lun_destruct(lun); 179 goto error; 180 } 181 } 182 183 return dev; 184 185 error: 186 spdk_scsi_dev_destruct(dev); 187 188 return NULL; 189 } 190 191 void 192 spdk_scsi_dev_queue_mgmt_task(struct spdk_scsi_dev *dev, 193 struct spdk_scsi_task *task, 194 enum spdk_scsi_task_func func) 195 { 196 assert(task != NULL); 197 198 task->function = func; 199 spdk_scsi_lun_task_mgmt_execute(task, func); 200 } 201 202 void 203 spdk_scsi_dev_queue_task(struct spdk_scsi_dev *dev, 204 struct spdk_scsi_task *task) 205 { 206 assert(task != NULL); 207 208 if (spdk_scsi_lun_append_task(task->lun, task) == 0) { 209 /* ready to execute, disk is valid for LUN access */ 210 spdk_scsi_lun_execute_tasks(task->lun); 211 } 212 } 213 214 int 215 spdk_scsi_dev_add_port(struct spdk_scsi_dev *dev, uint64_t id, const char *name) 216 { 217 struct spdk_scsi_port *port; 218 int rc; 219 220 if (dev->num_ports == SPDK_SCSI_DEV_MAX_PORTS) { 221 SPDK_ERRLOG("device already has %d ports\n", SPDK_SCSI_DEV_MAX_PORTS); 222 return -1; 223 } 224 225 port = &dev->port[dev->num_ports]; 226 227 rc = spdk_scsi_port_construct(port, id, dev->num_ports, name); 228 if (rc != 0) { 229 return rc; 230 } 231 232 dev->num_ports++; 233 return 0; 234 } 235 236 struct spdk_scsi_port * 237 spdk_scsi_dev_find_port_by_id(struct spdk_scsi_dev *dev, uint64_t id) 238 { 239 int i; 240 241 for (i = 0; i < dev->num_ports; i++) { 242 if (dev->port[i].id == id) { 243 return &dev->port[i]; 244 } 245 } 246 247 /* No matching port found. */ 248 return NULL; 249 } 250 251 void 252 spdk_scsi_dev_print(struct spdk_scsi_dev *dev) 253 { 254 struct spdk_scsi_lun *lun; 255 int i; 256 257 printf("device %d HDD UNIT\n", dev->id); 258 259 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 260 lun = dev->lun[i]; 261 if (lun == NULL) 262 continue; 263 printf("device %d: LUN%d %s\n", dev->id, i, lun->name); 264 } 265 } 266 267 void 268 spdk_scsi_dev_free_io_channels(struct spdk_scsi_dev *dev) 269 { 270 int i; 271 272 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 273 if (dev->lun[i] == NULL) { 274 continue; 275 } 276 spdk_scsi_lun_free_io_channel(dev->lun[i]); 277 } 278 } 279 280 int 281 spdk_scsi_dev_allocate_io_channels(struct spdk_scsi_dev *dev) 282 { 283 int i, rc; 284 285 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 286 if (dev->lun[i] == NULL) { 287 continue; 288 } 289 rc = spdk_scsi_lun_allocate_io_channel(dev->lun[i]); 290 if (rc < 0) { 291 spdk_scsi_dev_free_io_channels(dev); 292 return -1; 293 } 294 } 295 296 return 0; 297 } 298 299 const char * 300 spdk_scsi_dev_get_name(const struct spdk_scsi_dev *dev) 301 { 302 return dev->name; 303 } 304 305 int 306 spdk_scsi_dev_get_id(const struct spdk_scsi_dev *dev) 307 { 308 return dev->id; 309 } 310 311 struct spdk_scsi_lun * 312 spdk_scsi_dev_get_lun(struct spdk_scsi_dev *dev, int lun_id) 313 { 314 if (lun_id < 0 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { 315 return NULL; 316 } 317 318 return dev->lun[lun_id]; 319 } 320 321 bool 322 spdk_scsi_dev_has_pending_tasks(const struct spdk_scsi_dev *dev) 323 { 324 int i; 325 326 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; ++i) { 327 if (dev->lun[i] && spdk_scsi_lun_has_pending_tasks(dev->lun[i])) { 328 return true; 329 } 330 } 331 332 return false; 333 } 334