xref: /spdk/lib/scsi/dev.c (revision 712a3f69d32632bf6c862f00200f7f437d3f7529)
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 	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 		spdk_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 = spdk_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 /* This typedef exists to work around an astyle 2.05 bug.
195  * Remove it when astyle is fixed.
196  */
197 typedef struct spdk_scsi_dev _spdk_scsi_dev;
198 
199 _spdk_scsi_dev *
200 spdk_scsi_dev_construct(const char *name, const char *bdev_name_list[],
201 			int *lun_id_list, int num_luns, uint8_t protocol_id,
202 			void (*hotremove_cb)(const struct spdk_scsi_lun *, void *),
203 			void *hotremove_ctx)
204 {
205 	struct spdk_scsi_dev *dev;
206 	size_t name_len;
207 	bool found_lun_0;
208 	int i, rc;
209 
210 	name_len = strlen(name);
211 	if (name_len > sizeof(dev->name) - 1) {
212 		SPDK_ERRLOG("device %s: name longer than maximum allowed length %zu\n",
213 			    name, sizeof(dev->name) - 1);
214 		return NULL;
215 	}
216 
217 	if (num_luns == 0) {
218 		SPDK_ERRLOG("device %s: no LUNs specified\n", name);
219 		return NULL;
220 	}
221 
222 	found_lun_0 = false;
223 	for (i = 0; i < num_luns; i++) {
224 		if (lun_id_list[i] == 0) {
225 			found_lun_0 = true;
226 			break;
227 		}
228 	}
229 
230 	if (!found_lun_0) {
231 		SPDK_ERRLOG("device %s: no LUN 0 specified\n", name);
232 		return NULL;
233 	}
234 
235 	for (i = 0; i < num_luns; i++) {
236 		if (bdev_name_list[i] == NULL) {
237 			SPDK_ERRLOG("NULL spdk_scsi_lun for LUN %d\n",
238 				    lun_id_list[i]);
239 			return NULL;
240 		}
241 	}
242 
243 	dev = allocate_dev();
244 	if (dev == NULL) {
245 		return NULL;
246 	}
247 
248 	memcpy(dev->name, name, name_len + 1);
249 
250 	dev->num_ports = 0;
251 	dev->protocol_id = protocol_id;
252 
253 	for (i = 0; i < num_luns; i++) {
254 		rc = spdk_scsi_dev_add_lun(dev, bdev_name_list[i], lun_id_list[i],
255 					   hotremove_cb, hotremove_ctx);
256 		if (rc < 0) {
257 			spdk_scsi_dev_destruct(dev, NULL, NULL);
258 			return NULL;
259 		}
260 	}
261 
262 	return dev;
263 }
264 
265 void
266 spdk_scsi_dev_queue_mgmt_task(struct spdk_scsi_dev *dev,
267 			      struct spdk_scsi_task *task)
268 {
269 	assert(task != NULL);
270 
271 	spdk_scsi_lun_append_mgmt_task(task->lun, task);
272 	spdk_scsi_lun_execute_mgmt_task(task->lun);
273 }
274 
275 void
276 spdk_scsi_dev_queue_task(struct spdk_scsi_dev *dev,
277 			 struct spdk_scsi_task *task)
278 {
279 	assert(task != NULL);
280 
281 	spdk_scsi_lun_append_task(task->lun, task);
282 	spdk_scsi_lun_execute_tasks(task->lun);
283 }
284 
285 static struct spdk_scsi_port *
286 scsi_dev_find_free_port(struct spdk_scsi_dev *dev)
287 {
288 	int i;
289 
290 	for (i = 0; i < SPDK_SCSI_DEV_MAX_PORTS; i++) {
291 		if (!dev->port[i].is_used) {
292 			return &dev->port[i];
293 		}
294 	}
295 
296 	return NULL;
297 }
298 
299 int
300 spdk_scsi_dev_add_port(struct spdk_scsi_dev *dev, uint64_t id, const char *name)
301 {
302 	struct spdk_scsi_port *port;
303 	int rc;
304 
305 	if (dev->num_ports == SPDK_SCSI_DEV_MAX_PORTS) {
306 		SPDK_ERRLOG("device already has %d ports\n", SPDK_SCSI_DEV_MAX_PORTS);
307 		return -1;
308 	}
309 
310 	port = spdk_scsi_dev_find_port_by_id(dev, id);
311 	if (port != NULL) {
312 		SPDK_ERRLOG("device already has port(%" PRIu64 ")\n", id);
313 		return -1;
314 	}
315 
316 	port = scsi_dev_find_free_port(dev);
317 	if (port == NULL) {
318 		assert(false);
319 		return -1;
320 	}
321 
322 	rc = spdk_scsi_port_construct(port, id, dev->num_ports, name);
323 	if (rc != 0) {
324 		return rc;
325 	}
326 
327 	dev->num_ports++;
328 	return 0;
329 }
330 
331 int
332 spdk_scsi_dev_delete_port(struct spdk_scsi_dev *dev, uint64_t id)
333 {
334 	struct spdk_scsi_port *port;
335 
336 	port = spdk_scsi_dev_find_port_by_id(dev, id);
337 	if (port == NULL) {
338 		SPDK_ERRLOG("device does not have specified port(%" PRIu64 ")\n", id);
339 		return -1;
340 	}
341 
342 	spdk_scsi_port_destruct(port);
343 
344 	dev->num_ports--;
345 
346 	return 0;
347 }
348 
349 struct spdk_scsi_port *
350 spdk_scsi_dev_find_port_by_id(struct spdk_scsi_dev *dev, uint64_t id)
351 {
352 	int i;
353 
354 	for (i = 0; i < SPDK_SCSI_DEV_MAX_PORTS; i++) {
355 		if (!dev->port[i].is_used) {
356 			continue;
357 		}
358 		if (dev->port[i].id == id) {
359 			return &dev->port[i];
360 		}
361 	}
362 
363 	/* No matching port found. */
364 	return NULL;
365 }
366 
367 void
368 spdk_scsi_dev_free_io_channels(struct spdk_scsi_dev *dev)
369 {
370 	int i;
371 
372 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
373 		if (dev->lun[i] == NULL) {
374 			continue;
375 		}
376 		_spdk_scsi_lun_free_io_channel(dev->lun[i]);
377 	}
378 }
379 
380 int
381 spdk_scsi_dev_allocate_io_channels(struct spdk_scsi_dev *dev)
382 {
383 	int i, rc;
384 
385 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; i++) {
386 		if (dev->lun[i] == NULL) {
387 			continue;
388 		}
389 		rc = _spdk_scsi_lun_allocate_io_channel(dev->lun[i]);
390 		if (rc < 0) {
391 			spdk_scsi_dev_free_io_channels(dev);
392 			return -1;
393 		}
394 	}
395 
396 	return 0;
397 }
398 
399 const char *
400 spdk_scsi_dev_get_name(const struct spdk_scsi_dev *dev)
401 {
402 	return dev->name;
403 }
404 
405 int
406 spdk_scsi_dev_get_id(const struct spdk_scsi_dev *dev)
407 {
408 	return dev->id;
409 }
410 
411 struct spdk_scsi_lun *
412 spdk_scsi_dev_get_lun(struct spdk_scsi_dev *dev, int lun_id)
413 {
414 	struct spdk_scsi_lun *lun;
415 
416 	if (lun_id < 0 || lun_id >= SPDK_SCSI_DEV_MAX_LUN) {
417 		return NULL;
418 	}
419 
420 	lun = dev->lun[lun_id];
421 
422 	if (lun != NULL && !spdk_scsi_lun_is_removing(lun)) {
423 		return lun;
424 	} else {
425 		return NULL;
426 	}
427 }
428 
429 bool
430 spdk_scsi_dev_has_pending_tasks(const struct spdk_scsi_dev *dev,
431 				const struct spdk_scsi_port *initiator_port)
432 {
433 	int i;
434 
435 	for (i = 0; i < SPDK_SCSI_DEV_MAX_LUN; ++i) {
436 		if (dev->lun[i] &&
437 		    (spdk_scsi_lun_has_pending_tasks(dev->lun[i], initiator_port) ||
438 		     spdk_scsi_lun_has_pending_mgmt_tasks(dev->lun[i], initiator_port))) {
439 			return true;
440 		}
441 	}
442 
443 	return false;
444 }
445