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 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 assert(dev->is_allocated == 1); 68 assert(dev->removed == true); 69 70 dev->is_allocated = 0; 71 72 if (dev->remove_cb) { 73 dev->remove_cb(dev->remove_ctx, 0); 74 dev->remove_cb = NULL; 75 } 76 } 77 78 void 79 spdk_scsi_dev_destruct(struct spdk_scsi_dev *dev, 80 spdk_scsi_dev_destruct_cb_t cb_fn, void *cb_arg) 81 { 82 int lun_cnt; 83 int i; 84 85 if (dev == NULL) { 86 if (cb_fn) { 87 cb_fn(cb_arg, -EINVAL); 88 } 89 return; 90 } 91 92 if (dev->removed) { 93 if (cb_fn) { 94 cb_fn(cb_arg, -EINVAL); 95 } 96 return; 97 } 98 99 dev->removed = true; 100 dev->remove_cb = cb_fn; 101 dev->remove_ctx = cb_arg; 102 lun_cnt = 0; 103 104 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 105 if (dev->lun[i] == NULL) { 106 continue; 107 } 108 109 /* 110 * LUN will remove itself from this dev when all outstanding IO 111 * is done. When no more LUNs, dev will be deleted. 112 */ 113 scsi_lun_destruct(dev->lun[i]); 114 lun_cnt++; 115 } 116 117 if (lun_cnt == 0) { 118 free_dev(dev); 119 return; 120 } 121 } 122 123 static int 124 scsi_dev_find_lowest_free_lun_id(struct spdk_scsi_dev *dev) 125 { 126 int i; 127 128 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 129 if (dev->lun[i] == NULL) { 130 return i; 131 } 132 } 133 134 return -1; 135 } 136 137 int 138 spdk_scsi_dev_add_lun(struct spdk_scsi_dev *dev, const char *bdev_name, int lun_id, 139 void (*hotremove_cb)(const struct spdk_scsi_lun *, void *), 140 void *hotremove_ctx) 141 { 142 struct spdk_bdev *bdev; 143 struct spdk_scsi_lun *lun; 144 145 bdev = spdk_bdev_get_by_name(bdev_name); 146 if (bdev == NULL) { 147 SPDK_ERRLOG("device %s: cannot find bdev '%s' (target %d)\n", 148 dev->name, bdev_name, lun_id); 149 return -1; 150 } 151 152 /* Search the lowest free LUN ID if LUN ID is default */ 153 if (lun_id == -1) { 154 lun_id = scsi_dev_find_lowest_free_lun_id(dev); 155 if (lun_id == -1) { 156 SPDK_ERRLOG("Free LUN ID is not found\n"); 157 return -1; 158 } 159 } 160 161 lun = scsi_lun_construct(bdev, hotremove_cb, hotremove_ctx); 162 if (lun == NULL) { 163 return -1; 164 } 165 166 lun->id = lun_id; 167 lun->dev = dev; 168 dev->lun[lun_id] = lun; 169 return 0; 170 } 171 172 void 173 spdk_scsi_dev_delete_lun(struct spdk_scsi_dev *dev, 174 struct spdk_scsi_lun *lun) 175 { 176 int lun_cnt = 0; 177 int i; 178 179 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 180 if (dev->lun[i] == lun) { 181 dev->lun[i] = NULL; 182 } 183 184 if (dev->lun[i]) { 185 lun_cnt++; 186 } 187 } 188 189 if (dev->removed == true && lun_cnt == 0) { 190 free_dev(dev); 191 } 192 } 193 194 struct spdk_scsi_dev *spdk_scsi_dev_construct(const char *name, const char *bdev_name_list[], 195 int *lun_id_list, int num_luns, uint8_t protocol_id, 196 void (*hotremove_cb)(const struct spdk_scsi_lun *, void *), 197 void *hotremove_ctx) 198 { 199 struct spdk_scsi_dev *dev; 200 size_t name_len; 201 bool found_lun_0; 202 int i, rc; 203 204 name_len = strlen(name); 205 if (name_len > sizeof(dev->name) - 1) { 206 SPDK_ERRLOG("device %s: name longer than maximum allowed length %zu\n", 207 name, sizeof(dev->name) - 1); 208 return NULL; 209 } 210 211 if (num_luns == 0) { 212 SPDK_ERRLOG("device %s: no LUNs specified\n", name); 213 return NULL; 214 } 215 216 found_lun_0 = false; 217 for (i = 0; i < num_luns; i++) { 218 if (lun_id_list[i] == 0) { 219 found_lun_0 = true; 220 break; 221 } 222 } 223 224 if (!found_lun_0) { 225 SPDK_ERRLOG("device %s: no LUN 0 specified\n", name); 226 return NULL; 227 } 228 229 for (i = 0; i < num_luns; i++) { 230 if (bdev_name_list[i] == NULL) { 231 SPDK_ERRLOG("NULL spdk_scsi_lun for LUN %d\n", 232 lun_id_list[i]); 233 return NULL; 234 } 235 } 236 237 dev = allocate_dev(); 238 if (dev == NULL) { 239 return NULL; 240 } 241 242 memcpy(dev->name, name, name_len + 1); 243 244 dev->num_ports = 0; 245 dev->protocol_id = protocol_id; 246 247 for (i = 0; i < num_luns; i++) { 248 rc = spdk_scsi_dev_add_lun(dev, bdev_name_list[i], lun_id_list[i], 249 hotremove_cb, hotremove_ctx); 250 if (rc < 0) { 251 spdk_scsi_dev_destruct(dev, NULL, NULL); 252 return NULL; 253 } 254 } 255 256 return dev; 257 } 258 259 void 260 spdk_scsi_dev_queue_mgmt_task(struct spdk_scsi_dev *dev, 261 struct spdk_scsi_task *task) 262 { 263 assert(task != NULL); 264 265 scsi_lun_execute_mgmt_task(task->lun, task); 266 } 267 268 void 269 spdk_scsi_dev_queue_task(struct spdk_scsi_dev *dev, 270 struct spdk_scsi_task *task) 271 { 272 assert(task != NULL); 273 274 scsi_lun_execute_task(task->lun, task); 275 } 276 277 static struct spdk_scsi_port * 278 scsi_dev_find_free_port(struct spdk_scsi_dev *dev) 279 { 280 int i; 281 282 for (i = 0; i < SPDK_SCSI_DEV_MAX_PORTS; i++) { 283 if (!dev->port[i].is_used) { 284 return &dev->port[i]; 285 } 286 } 287 288 return NULL; 289 } 290 291 int 292 spdk_scsi_dev_add_port(struct spdk_scsi_dev *dev, uint64_t id, const char *name) 293 { 294 struct spdk_scsi_port *port; 295 int rc; 296 297 if (dev->num_ports == SPDK_SCSI_DEV_MAX_PORTS) { 298 SPDK_ERRLOG("device already has %d ports\n", SPDK_SCSI_DEV_MAX_PORTS); 299 return -1; 300 } 301 302 port = spdk_scsi_dev_find_port_by_id(dev, id); 303 if (port != NULL) { 304 SPDK_ERRLOG("device already has port(%" PRIu64 ")\n", id); 305 return -1; 306 } 307 308 port = scsi_dev_find_free_port(dev); 309 if (port == NULL) { 310 assert(false); 311 return -1; 312 } 313 314 rc = scsi_port_construct(port, id, dev->num_ports, name); 315 if (rc != 0) { 316 return rc; 317 } 318 319 dev->num_ports++; 320 return 0; 321 } 322 323 int 324 spdk_scsi_dev_delete_port(struct spdk_scsi_dev *dev, uint64_t id) 325 { 326 struct spdk_scsi_port *port; 327 328 port = spdk_scsi_dev_find_port_by_id(dev, id); 329 if (port == NULL) { 330 SPDK_ERRLOG("device does not have specified port(%" PRIu64 ")\n", id); 331 return -1; 332 } 333 334 scsi_port_destruct(port); 335 336 dev->num_ports--; 337 338 return 0; 339 } 340 341 struct spdk_scsi_port * 342 spdk_scsi_dev_find_port_by_id(struct spdk_scsi_dev *dev, uint64_t id) 343 { 344 int i; 345 346 for (i = 0; i < SPDK_SCSI_DEV_MAX_PORTS; i++) { 347 if (!dev->port[i].is_used) { 348 continue; 349 } 350 if (dev->port[i].id == id) { 351 return &dev->port[i]; 352 } 353 } 354 355 /* No matching port found. */ 356 return NULL; 357 } 358 359 void 360 spdk_scsi_dev_free_io_channels(struct spdk_scsi_dev *dev) 361 { 362 int i; 363 364 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 365 if (dev->lun[i] == NULL) { 366 continue; 367 } 368 scsi_lun_free_io_channel(dev->lun[i]); 369 } 370 } 371 372 int 373 spdk_scsi_dev_allocate_io_channels(struct spdk_scsi_dev *dev) 374 { 375 int i, rc; 376 377 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) { 378 if (dev->lun[i] == NULL) { 379 continue; 380 } 381 rc = scsi_lun_allocate_io_channel(dev->lun[i]); 382 if (rc < 0) { 383 spdk_scsi_dev_free_io_channels(dev); 384 return -1; 385 } 386 } 387 388 return 0; 389 } 390 391 const char * 392 spdk_scsi_dev_get_name(const struct spdk_scsi_dev *dev) 393 { 394 return dev->name; 395 } 396 397 int 398 spdk_scsi_dev_get_id(const struct spdk_scsi_dev *dev) 399 { 400 return dev->id; 401 } 402 403 struct spdk_scsi_lun * 404 spdk_scsi_dev_get_lun(struct spdk_scsi_dev *dev, int lun_id) 405 { 406 struct spdk_scsi_lun *lun; 407 408 if (lun_id < 0 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) { 409 return NULL; 410 } 411 412 lun = dev->lun[lun_id]; 413 414 if (lun != NULL && !spdk_scsi_lun_is_removing(lun)) { 415 return lun; 416 } else { 417 return NULL; 418 } 419 } 420 421 bool 422 spdk_scsi_dev_has_pending_tasks(const struct spdk_scsi_dev *dev, 423 const struct spdk_scsi_port *initiator_port) 424 { 425 int i; 426 427 for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; ++i) { 428 if (dev->lun[i] && 429 (scsi_lun_has_pending_tasks(dev->lun[i], initiator_port) || 430 scsi_lun_has_pending_mgmt_tasks(dev->lun[i], initiator_port))) { 431 return true; 432 } 433 } 434 435 return false; 436 } 437