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