Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * scsi_scan.c | |
3 | * | |
4 | * Copyright (C) 2000 Eric Youngdale, | |
5 | * Copyright (C) 2002 Patrick Mansfield | |
6 | * | |
7 | * The general scanning/probing algorithm is as follows, exceptions are | |
8 | * made to it depending on device specific flags, compilation options, and | |
9 | * global variable (boot or module load time) settings. | |
10 | * | |
11 | * A specific LUN is scanned via an INQUIRY command; if the LUN has a | |
f64a181d | 12 | * device attached, a scsi_device is allocated and setup for it. |
1da177e4 LT |
13 | * |
14 | * For every id of every channel on the given host: | |
15 | * | |
16 | * Scan LUN 0; if the target responds to LUN 0 (even if there is no | |
17 | * device or storage attached to LUN 0): | |
18 | * | |
19 | * If LUN 0 has a device attached, allocate and setup a | |
f64a181d | 20 | * scsi_device for it. |
1da177e4 LT |
21 | * |
22 | * If target is SCSI-3 or up, issue a REPORT LUN, and scan | |
23 | * all of the LUNs returned by the REPORT LUN; else, | |
24 | * sequentially scan LUNs up until some maximum is reached, | |
25 | * or a LUN is seen that cannot have a device attached to it. | |
26 | */ | |
27 | ||
1da177e4 LT |
28 | #include <linux/module.h> |
29 | #include <linux/moduleparam.h> | |
30 | #include <linux/init.h> | |
31 | #include <linux/blkdev.h> | |
3e082a91 MW |
32 | #include <linux/delay.h> |
33 | #include <linux/kthread.h> | |
34 | #include <linux/spinlock.h> | |
1da177e4 LT |
35 | |
36 | #include <scsi/scsi.h> | |
beb40487 | 37 | #include <scsi/scsi_cmnd.h> |
1da177e4 LT |
38 | #include <scsi/scsi_device.h> |
39 | #include <scsi/scsi_driver.h> | |
40 | #include <scsi/scsi_devinfo.h> | |
41 | #include <scsi/scsi_host.h> | |
1da177e4 LT |
42 | #include <scsi/scsi_transport.h> |
43 | #include <scsi/scsi_eh.h> | |
44 | ||
45 | #include "scsi_priv.h" | |
46 | #include "scsi_logging.h" | |
47 | ||
48 | #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \ | |
49 | " SCSI scanning, some SCSI devices might not be configured\n" | |
50 | ||
51 | /* | |
52 | * Default timeout | |
53 | */ | |
54 | #define SCSI_TIMEOUT (2*HZ) | |
55 | ||
56 | /* | |
57 | * Prefix values for the SCSI id's (stored in driverfs name field) | |
58 | */ | |
59 | #define SCSI_UID_SER_NUM 'S' | |
60 | #define SCSI_UID_UNKNOWN 'Z' | |
61 | ||
62 | /* | |
63 | * Return values of some of the scanning functions. | |
64 | * | |
65 | * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this | |
66 | * includes allocation or general failures preventing IO from being sent. | |
67 | * | |
68 | * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available | |
69 | * on the given LUN. | |
70 | * | |
71 | * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a | |
72 | * given LUN. | |
73 | */ | |
74 | #define SCSI_SCAN_NO_RESPONSE 0 | |
75 | #define SCSI_SCAN_TARGET_PRESENT 1 | |
76 | #define SCSI_SCAN_LUN_PRESENT 2 | |
77 | ||
0ad78200 | 78 | static const char *scsi_null_device_strs = "nullnullnullnull"; |
1da177e4 LT |
79 | |
80 | #define MAX_SCSI_LUNS 512 | |
81 | ||
82 | #ifdef CONFIG_SCSI_MULTI_LUN | |
83 | static unsigned int max_scsi_luns = MAX_SCSI_LUNS; | |
84 | #else | |
85 | static unsigned int max_scsi_luns = 1; | |
86 | #endif | |
87 | ||
88 | module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR); | |
89 | MODULE_PARM_DESC(max_luns, | |
90 | "last scsi LUN (should be between 1 and 2^32-1)"); | |
91 | ||
21db1882 MW |
92 | #ifdef CONFIG_SCSI_SCAN_ASYNC |
93 | #define SCSI_SCAN_TYPE_DEFAULT "async" | |
94 | #else | |
95 | #define SCSI_SCAN_TYPE_DEFAULT "sync" | |
96 | #endif | |
97 | ||
98 | static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT; | |
3e082a91 MW |
99 | |
100 | module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO); | |
101 | MODULE_PARM_DESC(scan, "sync, async or none"); | |
102 | ||
1da177e4 LT |
103 | /* |
104 | * max_scsi_report_luns: the maximum number of LUNS that will be | |
105 | * returned from the REPORT LUNS command. 8 times this value must | |
106 | * be allocated. In theory this could be up to an 8 byte value, but | |
107 | * in practice, the maximum number of LUNs suppored by any device | |
108 | * is about 16k. | |
109 | */ | |
110 | static unsigned int max_scsi_report_luns = 511; | |
111 | ||
112 | module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR); | |
113 | MODULE_PARM_DESC(max_report_luns, | |
114 | "REPORT LUNS maximum number of LUNS received (should be" | |
115 | " between 1 and 16384)"); | |
116 | ||
117 | static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3; | |
118 | ||
119 | module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR); | |
120 | MODULE_PARM_DESC(inq_timeout, | |
121 | "Timeout (in seconds) waiting for devices to answer INQUIRY." | |
122 | " Default is 5. Some non-compliant devices need more."); | |
123 | ||
3e082a91 MW |
124 | static DEFINE_SPINLOCK(async_scan_lock); |
125 | static LIST_HEAD(scanning_hosts); | |
126 | ||
127 | struct async_scan_data { | |
128 | struct list_head list; | |
129 | struct Scsi_Host *shost; | |
130 | struct completion prev_finished; | |
131 | }; | |
132 | ||
133 | /** | |
134 | * scsi_complete_async_scans - Wait for asynchronous scans to complete | |
135 | * | |
136 | * Asynchronous scans add themselves to the scanning_hosts list. Once | |
137 | * that list is empty, we know that the scans are complete. Rather than | |
138 | * waking up periodically to check the state of the list, we pretend to be | |
139 | * a scanning task by adding ourselves at the end of the list and going to | |
140 | * sleep. When the task before us wakes us up, we take ourselves off the | |
141 | * list and return. | |
142 | */ | |
143 | int scsi_complete_async_scans(void) | |
144 | { | |
145 | struct async_scan_data *data; | |
146 | ||
147 | do { | |
148 | if (list_empty(&scanning_hosts)) | |
149 | return 0; | |
150 | /* If we can't get memory immediately, that's OK. Just | |
151 | * sleep a little. Even if we never get memory, the async | |
152 | * scans will finish eventually. | |
153 | */ | |
154 | data = kmalloc(sizeof(*data), GFP_KERNEL); | |
155 | if (!data) | |
156 | msleep(1); | |
157 | } while (!data); | |
158 | ||
159 | data->shost = NULL; | |
160 | init_completion(&data->prev_finished); | |
161 | ||
162 | spin_lock(&async_scan_lock); | |
163 | /* Check that there's still somebody else on the list */ | |
164 | if (list_empty(&scanning_hosts)) | |
165 | goto done; | |
166 | list_add_tail(&data->list, &scanning_hosts); | |
167 | spin_unlock(&async_scan_lock); | |
168 | ||
169 | printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n"); | |
170 | wait_for_completion(&data->prev_finished); | |
171 | ||
172 | spin_lock(&async_scan_lock); | |
173 | list_del(&data->list); | |
174 | done: | |
175 | spin_unlock(&async_scan_lock); | |
176 | ||
177 | kfree(data); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | #ifdef MODULE | |
182 | /* Only exported for the benefit of scsi_wait_scan */ | |
183 | EXPORT_SYMBOL_GPL(scsi_complete_async_scans); | |
184 | #endif | |
185 | ||
1da177e4 LT |
186 | /** |
187 | * scsi_unlock_floptical - unlock device via a special MODE SENSE command | |
39216033 | 188 | * @sdev: scsi device to send command to |
1da177e4 LT |
189 | * @result: area to store the result of the MODE SENSE |
190 | * | |
191 | * Description: | |
39216033 | 192 | * Send a vendor specific MODE SENSE (not a MODE SELECT) command. |
1da177e4 LT |
193 | * Called for BLIST_KEY devices. |
194 | **/ | |
39216033 | 195 | static void scsi_unlock_floptical(struct scsi_device *sdev, |
1da177e4 LT |
196 | unsigned char *result) |
197 | { | |
198 | unsigned char scsi_cmd[MAX_COMMAND_SIZE]; | |
199 | ||
200 | printk(KERN_NOTICE "scsi: unlocking floptical drive\n"); | |
201 | scsi_cmd[0] = MODE_SENSE; | |
202 | scsi_cmd[1] = 0; | |
203 | scsi_cmd[2] = 0x2e; | |
204 | scsi_cmd[3] = 0; | |
39216033 | 205 | scsi_cmd[4] = 0x2a; /* size */ |
1da177e4 | 206 | scsi_cmd[5] = 0; |
39216033 JB |
207 | scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL, |
208 | SCSI_TIMEOUT, 3); | |
1da177e4 LT |
209 | } |
210 | ||
1da177e4 LT |
211 | /** |
212 | * scsi_alloc_sdev - allocate and setup a scsi_Device | |
213 | * | |
214 | * Description: | |
215 | * Allocate, initialize for io, and return a pointer to a scsi_Device. | |
216 | * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and | |
217 | * adds scsi_Device to the appropriate list. | |
218 | * | |
219 | * Return value: | |
220 | * scsi_Device pointer, or NULL on failure. | |
221 | **/ | |
222 | static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, | |
223 | unsigned int lun, void *hostdata) | |
224 | { | |
225 | struct scsi_device *sdev; | |
226 | int display_failure_msg = 1, ret; | |
227 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); | |
228 | ||
24669f75 | 229 | sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size, |
1da177e4 LT |
230 | GFP_ATOMIC); |
231 | if (!sdev) | |
232 | goto out; | |
233 | ||
1da177e4 LT |
234 | sdev->vendor = scsi_null_device_strs; |
235 | sdev->model = scsi_null_device_strs; | |
236 | sdev->rev = scsi_null_device_strs; | |
237 | sdev->host = shost; | |
238 | sdev->id = starget->id; | |
239 | sdev->lun = lun; | |
240 | sdev->channel = starget->channel; | |
241 | sdev->sdev_state = SDEV_CREATED; | |
242 | INIT_LIST_HEAD(&sdev->siblings); | |
243 | INIT_LIST_HEAD(&sdev->same_target_siblings); | |
244 | INIT_LIST_HEAD(&sdev->cmd_list); | |
245 | INIT_LIST_HEAD(&sdev->starved_entry); | |
246 | spin_lock_init(&sdev->list_lock); | |
247 | ||
248 | sdev->sdev_gendev.parent = get_device(&starget->dev); | |
249 | sdev->sdev_target = starget; | |
250 | ||
251 | /* usually NULL and set by ->slave_alloc instead */ | |
252 | sdev->hostdata = hostdata; | |
253 | ||
254 | /* if the device needs this changing, it may do so in the | |
255 | * slave_configure function */ | |
256 | sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED; | |
257 | ||
258 | /* | |
259 | * Some low level driver could use device->type | |
260 | */ | |
261 | sdev->type = -1; | |
262 | ||
263 | /* | |
264 | * Assume that the device will have handshaking problems, | |
265 | * and then fix this field later if it turns out it | |
266 | * doesn't | |
267 | */ | |
268 | sdev->borken = 1; | |
269 | ||
1da177e4 LT |
270 | sdev->request_queue = scsi_alloc_queue(sdev); |
271 | if (!sdev->request_queue) { | |
272 | /* release fn is set up in scsi_sysfs_device_initialise, so | |
273 | * have to free and put manually here */ | |
274 | put_device(&starget->dev); | |
93f56089 | 275 | kfree(sdev); |
1da177e4 LT |
276 | goto out; |
277 | } | |
278 | ||
279 | sdev->request_queue->queuedata = sdev; | |
280 | scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun); | |
281 | ||
282 | scsi_sysfs_device_initialize(sdev); | |
283 | ||
284 | if (shost->hostt->slave_alloc) { | |
285 | ret = shost->hostt->slave_alloc(sdev); | |
286 | if (ret) { | |
287 | /* | |
288 | * if LLDD reports slave not present, don't clutter | |
289 | * console with alloc failure messages | |
1da177e4 LT |
290 | */ |
291 | if (ret == -ENXIO) | |
292 | display_failure_msg = 0; | |
293 | goto out_device_destroy; | |
294 | } | |
295 | } | |
296 | ||
297 | return sdev; | |
298 | ||
299 | out_device_destroy: | |
300 | transport_destroy_device(&sdev->sdev_gendev); | |
1da177e4 LT |
301 | put_device(&sdev->sdev_gendev); |
302 | out: | |
303 | if (display_failure_msg) | |
304 | printk(ALLOC_FAILURE_MSG, __FUNCTION__); | |
305 | return NULL; | |
306 | } | |
307 | ||
308 | static void scsi_target_dev_release(struct device *dev) | |
309 | { | |
310 | struct device *parent = dev->parent; | |
311 | struct scsi_target *starget = to_scsi_target(dev); | |
a283bd37 | 312 | |
1da177e4 LT |
313 | kfree(starget); |
314 | put_device(parent); | |
315 | } | |
316 | ||
317 | int scsi_is_target_device(const struct device *dev) | |
318 | { | |
319 | return dev->release == scsi_target_dev_release; | |
320 | } | |
321 | EXPORT_SYMBOL(scsi_is_target_device); | |
322 | ||
323 | static struct scsi_target *__scsi_find_target(struct device *parent, | |
324 | int channel, uint id) | |
325 | { | |
326 | struct scsi_target *starget, *found_starget = NULL; | |
327 | struct Scsi_Host *shost = dev_to_shost(parent); | |
328 | /* | |
329 | * Search for an existing target for this sdev. | |
330 | */ | |
331 | list_for_each_entry(starget, &shost->__targets, siblings) { | |
332 | if (starget->id == id && | |
333 | starget->channel == channel) { | |
334 | found_starget = starget; | |
335 | break; | |
336 | } | |
337 | } | |
338 | if (found_starget) | |
339 | get_device(&found_starget->dev); | |
340 | ||
341 | return found_starget; | |
342 | } | |
343 | ||
884d25cc JB |
344 | /** |
345 | * scsi_alloc_target - allocate a new or find an existing target | |
346 | * @parent: parent of the target (need not be a scsi host) | |
347 | * @channel: target channel number (zero if no channels) | |
348 | * @id: target id number | |
349 | * | |
350 | * Return an existing target if one exists, provided it hasn't already | |
351 | * gone into STARGET_DEL state, otherwise allocate a new target. | |
352 | * | |
353 | * The target is returned with an incremented reference, so the caller | |
354 | * is responsible for both reaping and doing a last put | |
355 | */ | |
1da177e4 LT |
356 | static struct scsi_target *scsi_alloc_target(struct device *parent, |
357 | int channel, uint id) | |
358 | { | |
359 | struct Scsi_Host *shost = dev_to_shost(parent); | |
360 | struct device *dev = NULL; | |
361 | unsigned long flags; | |
362 | const int size = sizeof(struct scsi_target) | |
363 | + shost->transportt->target_size; | |
5c44cd2a | 364 | struct scsi_target *starget; |
1da177e4 | 365 | struct scsi_target *found_target; |
32f95792 | 366 | int error; |
1da177e4 | 367 | |
24669f75 | 368 | starget = kzalloc(size, GFP_KERNEL); |
1da177e4 LT |
369 | if (!starget) { |
370 | printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__); | |
371 | return NULL; | |
372 | } | |
1da177e4 LT |
373 | dev = &starget->dev; |
374 | device_initialize(dev); | |
375 | starget->reap_ref = 1; | |
376 | dev->parent = get_device(parent); | |
377 | dev->release = scsi_target_dev_release; | |
378 | sprintf(dev->bus_id, "target%d:%d:%d", | |
379 | shost->host_no, channel, id); | |
380 | starget->id = id; | |
381 | starget->channel = channel; | |
382 | INIT_LIST_HEAD(&starget->siblings); | |
383 | INIT_LIST_HEAD(&starget->devices); | |
ffedb452 JB |
384 | starget->state = STARGET_RUNNING; |
385 | retry: | |
1da177e4 LT |
386 | spin_lock_irqsave(shost->host_lock, flags); |
387 | ||
388 | found_target = __scsi_find_target(parent, channel, id); | |
389 | if (found_target) | |
390 | goto found; | |
391 | ||
392 | list_add_tail(&starget->siblings, &shost->__targets); | |
393 | spin_unlock_irqrestore(shost->host_lock, flags); | |
394 | /* allocate and add */ | |
a283bd37 | 395 | transport_setup_device(dev); |
32f95792 BK |
396 | error = device_add(dev); |
397 | if (error) { | |
398 | dev_err(dev, "target device_add failed, error %d\n", error); | |
399 | spin_lock_irqsave(shost->host_lock, flags); | |
400 | list_del_init(&starget->siblings); | |
401 | spin_unlock_irqrestore(shost->host_lock, flags); | |
402 | transport_destroy_device(dev); | |
403 | put_device(parent); | |
404 | kfree(starget); | |
405 | return NULL; | |
406 | } | |
a283bd37 JB |
407 | transport_add_device(dev); |
408 | if (shost->hostt->target_alloc) { | |
32f95792 | 409 | error = shost->hostt->target_alloc(starget); |
a283bd37 JB |
410 | |
411 | if(error) { | |
412 | dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error); | |
413 | /* don't want scsi_target_reap to do the final | |
414 | * put because it will be under the host lock */ | |
415 | get_device(dev); | |
416 | scsi_target_reap(starget); | |
417 | put_device(dev); | |
418 | return NULL; | |
419 | } | |
420 | } | |
884d25cc | 421 | get_device(dev); |
a283bd37 | 422 | |
1da177e4 LT |
423 | return starget; |
424 | ||
425 | found: | |
426 | found_target->reap_ref++; | |
427 | spin_unlock_irqrestore(shost->host_lock, flags); | |
ffedb452 | 428 | if (found_target->state != STARGET_DEL) { |
884d25cc | 429 | put_device(parent); |
ffedb452 JB |
430 | kfree(starget); |
431 | return found_target; | |
432 | } | |
433 | /* Unfortunately, we found a dying target; need to | |
434 | * wait until it's dead before we can get a new one */ | |
435 | put_device(&found_target->dev); | |
436 | flush_scheduled_work(); | |
437 | goto retry; | |
1da177e4 LT |
438 | } |
439 | ||
65f27f38 | 440 | static void scsi_target_reap_usercontext(struct work_struct *work) |
65110b21 | 441 | { |
65f27f38 DH |
442 | struct scsi_target *starget = |
443 | container_of(work, struct scsi_target, ew.work); | |
863a930a JB |
444 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
445 | unsigned long flags; | |
446 | ||
ffedb452 JB |
447 | transport_remove_device(&starget->dev); |
448 | device_del(&starget->dev); | |
449 | transport_destroy_device(&starget->dev); | |
863a930a | 450 | spin_lock_irqsave(shost->host_lock, flags); |
a50a5e37 MA |
451 | if (shost->hostt->target_destroy) |
452 | shost->hostt->target_destroy(starget); | |
ffedb452 | 453 | list_del_init(&starget->siblings); |
863a930a | 454 | spin_unlock_irqrestore(shost->host_lock, flags); |
ffedb452 | 455 | put_device(&starget->dev); |
863a930a JB |
456 | } |
457 | ||
1da177e4 LT |
458 | /** |
459 | * scsi_target_reap - check to see if target is in use and destroy if not | |
460 | * | |
461 | * @starget: target to be checked | |
462 | * | |
463 | * This is used after removing a LUN or doing a last put of the target | |
464 | * it checks atomically that nothing is using the target and removes | |
465 | * it if so. | |
466 | */ | |
467 | void scsi_target_reap(struct scsi_target *starget) | |
468 | { | |
ffedb452 JB |
469 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
470 | unsigned long flags; | |
471 | ||
472 | spin_lock_irqsave(shost->host_lock, flags); | |
473 | ||
474 | if (--starget->reap_ref == 0 && list_empty(&starget->devices)) { | |
475 | BUG_ON(starget->state == STARGET_DEL); | |
476 | starget->state = STARGET_DEL; | |
477 | spin_unlock_irqrestore(shost->host_lock, flags); | |
478 | execute_in_process_context(scsi_target_reap_usercontext, | |
65f27f38 | 479 | &starget->ew); |
ffedb452 JB |
480 | return; |
481 | ||
482 | } | |
483 | spin_unlock_irqrestore(shost->host_lock, flags); | |
484 | ||
485 | return; | |
1da177e4 LT |
486 | } |
487 | ||
e5b3cd42 AS |
488 | /** |
489 | * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string | |
490 | * @s: INQUIRY result string to sanitize | |
491 | * @len: length of the string | |
492 | * | |
493 | * Description: | |
494 | * The SCSI spec says that INQUIRY vendor, product, and revision | |
495 | * strings must consist entirely of graphic ASCII characters, | |
496 | * padded on the right with spaces. Since not all devices obey | |
497 | * this rule, we will replace non-graphic or non-ASCII characters | |
498 | * with spaces. Exception: a NUL character is interpreted as a | |
499 | * string terminator, so all the following characters are set to | |
500 | * spaces. | |
501 | **/ | |
502 | static void sanitize_inquiry_string(unsigned char *s, int len) | |
503 | { | |
504 | int terminated = 0; | |
505 | ||
506 | for (; len > 0; (--len, ++s)) { | |
507 | if (*s == 0) | |
508 | terminated = 1; | |
509 | if (terminated || *s < 0x20 || *s > 0x7e) | |
510 | *s = ' '; | |
511 | } | |
512 | } | |
513 | ||
1da177e4 LT |
514 | /** |
515 | * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY | |
39216033 | 516 | * @sdev: scsi_device to probe |
1da177e4 | 517 | * @inq_result: area to store the INQUIRY result |
39216033 | 518 | * @result_len: len of inq_result |
1da177e4 LT |
519 | * @bflags: store any bflags found here |
520 | * | |
521 | * Description: | |
39216033 | 522 | * Probe the lun associated with @req using a standard SCSI INQUIRY; |
1da177e4 | 523 | * |
39216033 | 524 | * If the INQUIRY is successful, zero is returned and the |
1da177e4 | 525 | * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length |
f64a181d | 526 | * are copied to the scsi_device any flags value is stored in *@bflags. |
1da177e4 | 527 | **/ |
e5b3cd42 | 528 | static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result, |
39216033 | 529 | int result_len, int *bflags) |
1da177e4 | 530 | { |
1da177e4 LT |
531 | unsigned char scsi_cmd[MAX_COMMAND_SIZE]; |
532 | int first_inquiry_len, try_inquiry_len, next_inquiry_len; | |
533 | int response_len = 0; | |
39216033 | 534 | int pass, count, result; |
1da177e4 LT |
535 | struct scsi_sense_hdr sshdr; |
536 | ||
537 | *bflags = 0; | |
538 | ||
539 | /* Perform up to 3 passes. The first pass uses a conservative | |
540 | * transfer length of 36 unless sdev->inquiry_len specifies a | |
541 | * different value. */ | |
542 | first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36; | |
543 | try_inquiry_len = first_inquiry_len; | |
544 | pass = 1; | |
545 | ||
546 | next_pass: | |
9ccfc756 JB |
547 | SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, |
548 | "scsi scan: INQUIRY pass %d length %d\n", | |
549 | pass, try_inquiry_len)); | |
1da177e4 LT |
550 | |
551 | /* Each pass gets up to three chances to ignore Unit Attention */ | |
552 | for (count = 0; count < 3; ++count) { | |
553 | memset(scsi_cmd, 0, 6); | |
554 | scsi_cmd[0] = INQUIRY; | |
555 | scsi_cmd[4] = (unsigned char) try_inquiry_len; | |
1da177e4 LT |
556 | |
557 | memset(inq_result, 0, try_inquiry_len); | |
39216033 JB |
558 | |
559 | result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, | |
ea73a9f2 | 560 | inq_result, try_inquiry_len, &sshdr, |
39216033 | 561 | HZ / 2 + HZ * scsi_inq_timeout, 3); |
1da177e4 LT |
562 | |
563 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s " | |
564 | "with code 0x%x\n", | |
39216033 | 565 | result ? "failed" : "successful", result)); |
1da177e4 | 566 | |
39216033 | 567 | if (result) { |
1da177e4 LT |
568 | /* |
569 | * not-ready to ready transition [asc/ascq=0x28/0x0] | |
570 | * or power-on, reset [asc/ascq=0x29/0x0], continue. | |
571 | * INQUIRY should not yield UNIT_ATTENTION | |
572 | * but many buggy devices do so anyway. | |
573 | */ | |
39216033 | 574 | if ((driver_byte(result) & DRIVER_SENSE) && |
ea73a9f2 | 575 | scsi_sense_valid(&sshdr)) { |
1da177e4 LT |
576 | if ((sshdr.sense_key == UNIT_ATTENTION) && |
577 | ((sshdr.asc == 0x28) || | |
578 | (sshdr.asc == 0x29)) && | |
579 | (sshdr.ascq == 0)) | |
580 | continue; | |
581 | } | |
582 | } | |
583 | break; | |
584 | } | |
585 | ||
39216033 | 586 | if (result == 0) { |
e5b3cd42 AS |
587 | sanitize_inquiry_string(&inq_result[8], 8); |
588 | sanitize_inquiry_string(&inq_result[16], 16); | |
589 | sanitize_inquiry_string(&inq_result[32], 4); | |
590 | ||
591 | response_len = inq_result[4] + 5; | |
1da177e4 LT |
592 | if (response_len > 255) |
593 | response_len = first_inquiry_len; /* sanity */ | |
594 | ||
595 | /* | |
596 | * Get any flags for this device. | |
597 | * | |
f64a181d CH |
598 | * XXX add a bflags to scsi_device, and replace the |
599 | * corresponding bit fields in scsi_device, so bflags | |
1da177e4 LT |
600 | * need not be passed as an argument. |
601 | */ | |
602 | *bflags = scsi_get_device_flags(sdev, &inq_result[8], | |
603 | &inq_result[16]); | |
604 | ||
605 | /* When the first pass succeeds we gain information about | |
606 | * what larger transfer lengths might work. */ | |
607 | if (pass == 1) { | |
608 | if (BLIST_INQUIRY_36 & *bflags) | |
609 | next_inquiry_len = 36; | |
610 | else if (BLIST_INQUIRY_58 & *bflags) | |
611 | next_inquiry_len = 58; | |
612 | else if (sdev->inquiry_len) | |
613 | next_inquiry_len = sdev->inquiry_len; | |
614 | else | |
615 | next_inquiry_len = response_len; | |
616 | ||
617 | /* If more data is available perform the second pass */ | |
618 | if (next_inquiry_len > try_inquiry_len) { | |
619 | try_inquiry_len = next_inquiry_len; | |
620 | pass = 2; | |
621 | goto next_pass; | |
622 | } | |
623 | } | |
624 | ||
625 | } else if (pass == 2) { | |
626 | printk(KERN_INFO "scsi scan: %d byte inquiry failed. " | |
627 | "Consider BLIST_INQUIRY_36 for this device\n", | |
628 | try_inquiry_len); | |
629 | ||
630 | /* If this pass failed, the third pass goes back and transfers | |
631 | * the same amount as we successfully got in the first pass. */ | |
632 | try_inquiry_len = first_inquiry_len; | |
633 | pass = 3; | |
634 | goto next_pass; | |
635 | } | |
636 | ||
637 | /* If the last transfer attempt got an error, assume the | |
638 | * peripheral doesn't exist or is dead. */ | |
39216033 JB |
639 | if (result) |
640 | return -EIO; | |
1da177e4 LT |
641 | |
642 | /* Don't report any more data than the device says is valid */ | |
643 | sdev->inquiry_len = min(try_inquiry_len, response_len); | |
644 | ||
645 | /* | |
646 | * XXX Abort if the response length is less than 36? If less than | |
647 | * 32, the lookup of the device flags (above) could be invalid, | |
648 | * and it would be possible to take an incorrect action - we do | |
649 | * not want to hang because of a short INQUIRY. On the flip side, | |
650 | * if the device is spun down or becoming ready (and so it gives a | |
651 | * short INQUIRY), an abort here prevents any further use of the | |
652 | * device, including spin up. | |
653 | * | |
654 | * Related to the above issue: | |
655 | * | |
656 | * XXX Devices (disk or all?) should be sent a TEST UNIT READY, | |
657 | * and if not ready, sent a START_STOP to start (maybe spin up) and | |
658 | * then send the INQUIRY again, since the INQUIRY can change after | |
659 | * a device is initialized. | |
660 | * | |
661 | * Ideally, start a device if explicitly asked to do so. This | |
662 | * assumes that a device is spun up on power on, spun down on | |
663 | * request, and then spun up on request. | |
664 | */ | |
665 | ||
666 | /* | |
667 | * The scanning code needs to know the scsi_level, even if no | |
668 | * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so | |
669 | * non-zero LUNs can be scanned. | |
670 | */ | |
671 | sdev->scsi_level = inq_result[2] & 0x07; | |
672 | if (sdev->scsi_level >= 2 || | |
673 | (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1)) | |
674 | sdev->scsi_level++; | |
6f3a2024 | 675 | sdev->sdev_target->scsi_level = sdev->scsi_level; |
1da177e4 | 676 | |
39216033 | 677 | return 0; |
1da177e4 LT |
678 | } |
679 | ||
680 | /** | |
f64a181d CH |
681 | * scsi_add_lun - allocate and fully initialze a scsi_device |
682 | * @sdevscan: holds information to be stored in the new scsi_device | |
683 | * @sdevnew: store the address of the newly allocated scsi_device | |
1da177e4 LT |
684 | * @inq_result: holds the result of a previous INQUIRY to the LUN |
685 | * @bflags: black/white list flag | |
686 | * | |
687 | * Description: | |
f64a181d | 688 | * Allocate and initialize a scsi_device matching sdevscan. Optionally |
1da177e4 | 689 | * set fields based on values in *@bflags. If @sdevnew is not |
f64a181d | 690 | * NULL, store the address of the new scsi_device in *@sdevnew (needed |
1da177e4 LT |
691 | * when scanning a particular LUN). |
692 | * | |
693 | * Return: | |
f64a181d CH |
694 | * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device |
695 | * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized | |
1da177e4 | 696 | **/ |
e5b3cd42 | 697 | static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, |
3e082a91 | 698 | int *bflags, int async) |
1da177e4 LT |
699 | { |
700 | /* | |
701 | * XXX do not save the inquiry, since it can change underneath us, | |
702 | * save just vendor/model/rev. | |
703 | * | |
704 | * Rather than save it and have an ioctl that retrieves the saved | |
705 | * value, have an ioctl that executes the same INQUIRY code used | |
706 | * in scsi_probe_lun, let user level programs doing INQUIRY | |
707 | * scanning run at their own risk, or supply a user level program | |
708 | * that can correctly scan. | |
709 | */ | |
09123d23 AS |
710 | |
711 | /* | |
712 | * Copy at least 36 bytes of INQUIRY data, so that we don't | |
713 | * dereference unallocated memory when accessing the Vendor, | |
714 | * Product, and Revision strings. Badly behaved devices may set | |
715 | * the INQUIRY Additional Length byte to a small value, indicating | |
716 | * these strings are invalid, but often they contain plausible data | |
717 | * nonetheless. It doesn't matter if the device sent < 36 bytes | |
718 | * total, since scsi_probe_lun() initializes inq_result with 0s. | |
719 | */ | |
720 | sdev->inquiry = kmemdup(inq_result, | |
721 | max_t(size_t, sdev->inquiry_len, 36), | |
722 | GFP_ATOMIC); | |
723 | if (sdev->inquiry == NULL) | |
1da177e4 | 724 | return SCSI_SCAN_NO_RESPONSE; |
1da177e4 | 725 | |
1da177e4 LT |
726 | sdev->vendor = (char *) (sdev->inquiry + 8); |
727 | sdev->model = (char *) (sdev->inquiry + 16); | |
728 | sdev->rev = (char *) (sdev->inquiry + 32); | |
729 | ||
730 | if (*bflags & BLIST_ISROM) { | |
731 | /* | |
732 | * It would be better to modify sdev->type, and set | |
4ff36718 MW |
733 | * sdev->removable; this can now be done since |
734 | * print_inquiry has gone away. | |
1da177e4 LT |
735 | */ |
736 | inq_result[0] = TYPE_ROM; | |
737 | inq_result[1] |= 0x80; /* removable */ | |
738 | } else if (*bflags & BLIST_NO_ULD_ATTACH) | |
739 | sdev->no_uld_attach = 1; | |
740 | ||
741 | switch (sdev->type = (inq_result[0] & 0x1f)) { | |
742 | case TYPE_TAPE: | |
743 | case TYPE_DISK: | |
744 | case TYPE_PRINTER: | |
745 | case TYPE_MOD: | |
746 | case TYPE_PROCESSOR: | |
747 | case TYPE_SCANNER: | |
748 | case TYPE_MEDIUM_CHANGER: | |
749 | case TYPE_ENCLOSURE: | |
750 | case TYPE_COMM: | |
4d7db04a | 751 | case TYPE_RAID: |
631e8a13 | 752 | case TYPE_RBC: |
1da177e4 LT |
753 | sdev->writeable = 1; |
754 | break; | |
755 | case TYPE_WORM: | |
756 | case TYPE_ROM: | |
757 | sdev->writeable = 0; | |
758 | break; | |
759 | default: | |
760 | printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type); | |
761 | } | |
762 | ||
1da177e4 LT |
763 | /* |
764 | * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI | |
765 | * spec says: The device server is capable of supporting the | |
766 | * specified peripheral device type on this logical unit. However, | |
767 | * the physical device is not currently connected to this logical | |
768 | * unit. | |
769 | * | |
770 | * The above is vague, as it implies that we could treat 001 and | |
771 | * 011 the same. Stay compatible with previous code, and create a | |
f64a181d | 772 | * scsi_device for a PQ of 1 |
1da177e4 LT |
773 | * |
774 | * Don't set the device offline here; rather let the upper | |
775 | * level drivers eval the PQ to decide whether they should | |
776 | * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. | |
777 | */ | |
778 | ||
779 | sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; | |
780 | sdev->removable = (0x80 & inq_result[1]) >> 7; | |
781 | sdev->lockable = sdev->removable; | |
782 | sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); | |
783 | ||
784 | if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 && | |
785 | inq_result[56] & 0x04)) | |
786 | sdev->ppr = 1; | |
787 | if (inq_result[7] & 0x60) | |
788 | sdev->wdtr = 1; | |
789 | if (inq_result[7] & 0x10) | |
790 | sdev->sdtr = 1; | |
791 | ||
19ac0db3 | 792 | sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d " |
4ff36718 MW |
793 | "ANSI: %d%s\n", scsi_device_type(sdev->type), |
794 | sdev->vendor, sdev->model, sdev->rev, | |
795 | sdev->inq_periph_qual, inq_result[2] & 0x07, | |
796 | (inq_result[3] & 0x0f) == 1 ? " CCS" : ""); | |
797 | ||
1da177e4 | 798 | /* |
5e3c34c1 | 799 | * End sysfs code. |
1da177e4 LT |
800 | */ |
801 | ||
802 | if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && | |
803 | !(*bflags & BLIST_NOTQ)) | |
804 | sdev->tagged_supported = 1; | |
805 | /* | |
806 | * Some devices (Texel CD ROM drives) have handshaking problems | |
807 | * when used with the Seagate controllers. borken is initialized | |
808 | * to 1, and then set it to 0 here. | |
809 | */ | |
810 | if ((*bflags & BLIST_BORKEN) == 0) | |
811 | sdev->borken = 0; | |
812 | ||
813 | /* | |
814 | * Apparently some really broken devices (contrary to the SCSI | |
815 | * standards) need to be selected without asserting ATN | |
816 | */ | |
817 | if (*bflags & BLIST_SELECT_NO_ATN) | |
818 | sdev->select_no_atn = 1; | |
819 | ||
4d7db04a JB |
820 | /* |
821 | * Maximum 512 sector transfer length | |
822 | * broken RA4x00 Compaq Disk Array | |
823 | */ | |
824 | if (*bflags & BLIST_MAX_512) | |
825 | blk_queue_max_sectors(sdev->request_queue, 512); | |
826 | ||
1da177e4 LT |
827 | /* |
828 | * Some devices may not want to have a start command automatically | |
829 | * issued when a device is added. | |
830 | */ | |
831 | if (*bflags & BLIST_NOSTARTONADD) | |
832 | sdev->no_start_on_add = 1; | |
833 | ||
834 | if (*bflags & BLIST_SINGLELUN) | |
835 | sdev->single_lun = 1; | |
836 | ||
837 | ||
838 | sdev->use_10_for_rw = 1; | |
839 | ||
840 | if (*bflags & BLIST_MS_SKIP_PAGE_08) | |
841 | sdev->skip_ms_page_8 = 1; | |
842 | ||
843 | if (*bflags & BLIST_MS_SKIP_PAGE_3F) | |
844 | sdev->skip_ms_page_3f = 1; | |
845 | ||
846 | if (*bflags & BLIST_USE_10_BYTE_MS) | |
847 | sdev->use_10_for_ms = 1; | |
848 | ||
849 | /* set the device running here so that slave configure | |
850 | * may do I/O */ | |
851 | scsi_device_set_state(sdev, SDEV_RUNNING); | |
852 | ||
853 | if (*bflags & BLIST_MS_192_BYTES_FOR_3F) | |
854 | sdev->use_192_bytes_for_3f = 1; | |
855 | ||
856 | if (*bflags & BLIST_NOT_LOCKABLE) | |
857 | sdev->lockable = 0; | |
858 | ||
859 | if (*bflags & BLIST_RETRY_HWERROR) | |
860 | sdev->retry_hwerror = 1; | |
861 | ||
862 | transport_configure_device(&sdev->sdev_gendev); | |
863 | ||
93805091 CH |
864 | if (sdev->host->hostt->slave_configure) { |
865 | int ret = sdev->host->hostt->slave_configure(sdev); | |
866 | if (ret) { | |
867 | /* | |
868 | * if LLDD reports slave not present, don't clutter | |
869 | * console with alloc failure messages | |
870 | */ | |
871 | if (ret != -ENXIO) { | |
872 | sdev_printk(KERN_ERR, sdev, | |
873 | "failed to configure device\n"); | |
874 | } | |
875 | return SCSI_SCAN_NO_RESPONSE; | |
876 | } | |
877 | } | |
1da177e4 LT |
878 | |
879 | /* | |
880 | * Ok, the device is now all set up, we can | |
881 | * register it and tell the rest of the kernel | |
882 | * about it. | |
883 | */ | |
3e082a91 | 884 | if (!async && scsi_sysfs_add_sdev(sdev) != 0) |
b24b1033 | 885 | return SCSI_SCAN_NO_RESPONSE; |
1da177e4 LT |
886 | |
887 | return SCSI_SCAN_LUN_PRESENT; | |
888 | } | |
889 | ||
6f3a2024 JB |
890 | static inline void scsi_destroy_sdev(struct scsi_device *sdev) |
891 | { | |
309bd271 | 892 | scsi_device_set_state(sdev, SDEV_DEL); |
6f3a2024 JB |
893 | if (sdev->host->hostt->slave_destroy) |
894 | sdev->host->hostt->slave_destroy(sdev); | |
895 | transport_destroy_device(&sdev->sdev_gendev); | |
896 | put_device(&sdev->sdev_gendev); | |
897 | } | |
898 | ||
c5f2e640 | 899 | #ifdef CONFIG_SCSI_LOGGING |
6c7154c9 KG |
900 | /** |
901 | * scsi_inq_str - print INQUIRY data from min to max index, | |
902 | * strip trailing whitespace | |
903 | * @buf: Output buffer with at least end-first+1 bytes of space | |
904 | * @inq: Inquiry buffer (input) | |
905 | * @first: Offset of string into inq | |
906 | * @end: Index after last character in inq | |
907 | */ | |
c5f2e640 | 908 | static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq, |
6c7154c9 KG |
909 | unsigned first, unsigned end) |
910 | { | |
911 | unsigned term = 0, idx; | |
c5f2e640 | 912 | |
913 | for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) { | |
914 | if (inq[idx+first] > ' ') { | |
6c7154c9 KG |
915 | buf[idx] = inq[idx+first]; |
916 | term = idx+1; | |
917 | } else { | |
918 | buf[idx] = ' '; | |
919 | } | |
920 | } | |
921 | buf[term] = 0; | |
922 | return buf; | |
923 | } | |
c5f2e640 | 924 | #endif |
6f3a2024 | 925 | |
1da177e4 LT |
926 | /** |
927 | * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it | |
928 | * @starget: pointer to target device structure | |
929 | * @lun: LUN of target device | |
f64a181d CH |
930 | * @sdevscan: probe the LUN corresponding to this scsi_device |
931 | * @sdevnew: store the value of any new scsi_device allocated | |
1da177e4 LT |
932 | * @bflagsp: store bflags here if not NULL |
933 | * | |
934 | * Description: | |
935 | * Call scsi_probe_lun, if a LUN with an attached device is found, | |
936 | * allocate and set it up by calling scsi_add_lun. | |
937 | * | |
938 | * Return: | |
f64a181d | 939 | * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device |
1da177e4 LT |
940 | * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is |
941 | * attached at the LUN | |
f64a181d | 942 | * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized |
1da177e4 LT |
943 | **/ |
944 | static int scsi_probe_and_add_lun(struct scsi_target *starget, | |
945 | uint lun, int *bflagsp, | |
946 | struct scsi_device **sdevp, int rescan, | |
947 | void *hostdata) | |
948 | { | |
949 | struct scsi_device *sdev; | |
1da177e4 | 950 | unsigned char *result; |
39216033 | 951 | int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256; |
1da177e4 LT |
952 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
953 | ||
954 | /* | |
955 | * The rescan flag is used as an optimization, the first scan of a | |
956 | * host adapter calls into here with rescan == 0. | |
957 | */ | |
6f3a2024 JB |
958 | sdev = scsi_device_lookup_by_target(starget, lun); |
959 | if (sdev) { | |
960 | if (rescan || sdev->sdev_state != SDEV_CREATED) { | |
1da177e4 LT |
961 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO |
962 | "scsi scan: device exists on %s\n", | |
963 | sdev->sdev_gendev.bus_id)); | |
964 | if (sdevp) | |
965 | *sdevp = sdev; | |
966 | else | |
967 | scsi_device_put(sdev); | |
968 | ||
969 | if (bflagsp) | |
970 | *bflagsp = scsi_get_device_flags(sdev, | |
971 | sdev->vendor, | |
972 | sdev->model); | |
973 | return SCSI_SCAN_LUN_PRESENT; | |
974 | } | |
6f3a2024 JB |
975 | scsi_device_put(sdev); |
976 | } else | |
977 | sdev = scsi_alloc_sdev(starget, lun, hostdata); | |
1da177e4 LT |
978 | if (!sdev) |
979 | goto out; | |
39216033 JB |
980 | |
981 | result = kmalloc(result_len, GFP_ATOMIC | | |
bc86120a | 982 | ((shost->unchecked_isa_dma) ? __GFP_DMA : 0)); |
1da177e4 | 983 | if (!result) |
39216033 | 984 | goto out_free_sdev; |
1da177e4 | 985 | |
39216033 | 986 | if (scsi_probe_lun(sdev, result, result_len, &bflags)) |
1da177e4 LT |
987 | goto out_free_result; |
988 | ||
4186ab19 KG |
989 | if (bflagsp) |
990 | *bflagsp = bflags; | |
1da177e4 LT |
991 | /* |
992 | * result contains valid SCSI INQUIRY data. | |
993 | */ | |
13f7e5ac | 994 | if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) { |
1da177e4 LT |
995 | /* |
996 | * For a Peripheral qualifier 3 (011b), the SCSI | |
997 | * spec says: The device server is not capable of | |
998 | * supporting a physical device on this logical | |
999 | * unit. | |
1000 | * | |
1001 | * For disks, this implies that there is no | |
1002 | * logical disk configured at sdev->lun, but there | |
1003 | * is a target id responding. | |
1004 | */ | |
6c7154c9 KG |
1005 | SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:" |
1006 | " peripheral qualifier of 3, device not" | |
1007 | " added\n")) | |
1008 | if (lun == 0) { | |
c5f2e640 | 1009 | SCSI_LOG_SCAN_BUS(1, { |
1010 | unsigned char vend[9]; | |
1011 | unsigned char mod[17]; | |
1012 | ||
1013 | sdev_printk(KERN_INFO, sdev, | |
6c7154c9 KG |
1014 | "scsi scan: consider passing scsi_mod." |
1015 | "dev_flags=%s:%s:0x240 or 0x800240\n", | |
1016 | scsi_inq_str(vend, result, 8, 16), | |
c5f2e640 | 1017 | scsi_inq_str(mod, result, 16, 32)); |
1018 | }); | |
6c7154c9 KG |
1019 | } |
1020 | ||
1da177e4 LT |
1021 | res = SCSI_SCAN_TARGET_PRESENT; |
1022 | goto out_free_result; | |
1023 | } | |
1024 | ||
1bfc5d9d | 1025 | /* |
84961f28 | 1026 | * Some targets may set slight variations of PQ and PDT to signal |
1027 | * that no LUN is present, so don't add sdev in these cases. | |
1028 | * Two specific examples are: | |
1029 | * 1) NetApp targets: return PQ=1, PDT=0x1f | |
1030 | * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" | |
1031 | * in the UFI 1.0 spec (we cannot rely on reserved bits). | |
1032 | * | |
1033 | * References: | |
1034 | * 1) SCSI SPC-3, pp. 145-146 | |
1035 | * PQ=1: "A peripheral device having the specified peripheral | |
1036 | * device type is not connected to this logical unit. However, the | |
1037 | * device server is capable of supporting the specified peripheral | |
1038 | * device type on this logical unit." | |
1039 | * PDT=0x1f: "Unknown or no device type" | |
1040 | * 2) USB UFI 1.0, p. 20 | |
1041 | * PDT=00h Direct-access device (floppy) | |
1042 | * PDT=1Fh none (no FDD connected to the requested logical unit) | |
1bfc5d9d | 1043 | */ |
84961f28 | 1044 | if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) && |
1045 | (result[0] & 0x1f) == 0x1f) { | |
1bfc5d9d AS |
1046 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO |
1047 | "scsi scan: peripheral device type" | |
1048 | " of 31, no device added\n")); | |
1049 | res = SCSI_SCAN_TARGET_PRESENT; | |
1050 | goto out_free_result; | |
1051 | } | |
1052 | ||
3e082a91 | 1053 | res = scsi_add_lun(sdev, result, &bflags, shost->async_scan); |
1da177e4 LT |
1054 | if (res == SCSI_SCAN_LUN_PRESENT) { |
1055 | if (bflags & BLIST_KEY) { | |
1056 | sdev->lockable = 0; | |
39216033 | 1057 | scsi_unlock_floptical(sdev, result); |
1da177e4 | 1058 | } |
1da177e4 LT |
1059 | } |
1060 | ||
1061 | out_free_result: | |
1062 | kfree(result); | |
1da177e4 LT |
1063 | out_free_sdev: |
1064 | if (res == SCSI_SCAN_LUN_PRESENT) { | |
1065 | if (sdevp) { | |
b70d37bf AS |
1066 | if (scsi_device_get(sdev) == 0) { |
1067 | *sdevp = sdev; | |
1068 | } else { | |
1069 | __scsi_remove_device(sdev); | |
1070 | res = SCSI_SCAN_NO_RESPONSE; | |
1071 | } | |
1da177e4 | 1072 | } |
6f3a2024 JB |
1073 | } else |
1074 | scsi_destroy_sdev(sdev); | |
1da177e4 LT |
1075 | out: |
1076 | return res; | |
1077 | } | |
1078 | ||
1079 | /** | |
1080 | * scsi_sequential_lun_scan - sequentially scan a SCSI target | |
1081 | * @starget: pointer to target structure to scan | |
1082 | * @bflags: black/white list flag for LUN 0 | |
1da177e4 LT |
1083 | * |
1084 | * Description: | |
1085 | * Generally, scan from LUN 1 (LUN 0 is assumed to already have been | |
1086 | * scanned) to some maximum lun until a LUN is found with no device | |
1087 | * attached. Use the bflags to figure out any oddities. | |
1088 | * | |
1089 | * Modifies sdevscan->lun. | |
1090 | **/ | |
1091 | static void scsi_sequential_lun_scan(struct scsi_target *starget, | |
4186ab19 | 1092 | int bflags, int scsi_level, int rescan) |
1da177e4 LT |
1093 | { |
1094 | unsigned int sparse_lun, lun, max_dev_lun; | |
1095 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); | |
1096 | ||
1097 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of" | |
1098 | "%s\n", starget->dev.bus_id)); | |
1099 | ||
1100 | max_dev_lun = min(max_scsi_luns, shost->max_lun); | |
1101 | /* | |
1102 | * If this device is known to support sparse multiple units, | |
1103 | * override the other settings, and scan all of them. Normally, | |
1104 | * SCSI-3 devices should be scanned via the REPORT LUNS. | |
1105 | */ | |
1106 | if (bflags & BLIST_SPARSELUN) { | |
1107 | max_dev_lun = shost->max_lun; | |
1108 | sparse_lun = 1; | |
1109 | } else | |
1110 | sparse_lun = 0; | |
1111 | ||
1da177e4 LT |
1112 | /* |
1113 | * If less than SCSI_1_CSS, and no special lun scaning, stop | |
1114 | * scanning; this matches 2.4 behaviour, but could just be a bug | |
1115 | * (to continue scanning a SCSI_1_CSS device). | |
1116 | * | |
1117 | * This test is broken. We might not have any device on lun0 for | |
1118 | * a sparselun device, and if that's the case then how would we | |
1119 | * know the real scsi_level, eh? It might make sense to just not | |
1120 | * scan any SCSI_1 device for non-0 luns, but that check would best | |
1121 | * go into scsi_alloc_sdev() and just have it return null when asked | |
1122 | * to alloc an sdev for lun > 0 on an already found SCSI_1 device. | |
1123 | * | |
1124 | if ((sdevscan->scsi_level < SCSI_1_CCS) && | |
1125 | ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) | |
1126 | == 0)) | |
1127 | return; | |
1128 | */ | |
1129 | /* | |
1130 | * If this device is known to support multiple units, override | |
1131 | * the other settings, and scan all of them. | |
1132 | */ | |
1133 | if (bflags & BLIST_FORCELUN) | |
1134 | max_dev_lun = shost->max_lun; | |
1135 | /* | |
1136 | * REGAL CDC-4X: avoid hang after LUN 4 | |
1137 | */ | |
1138 | if (bflags & BLIST_MAX5LUN) | |
1139 | max_dev_lun = min(5U, max_dev_lun); | |
1140 | /* | |
1141 | * Do not scan SCSI-2 or lower device past LUN 7, unless | |
1142 | * BLIST_LARGELUN. | |
1143 | */ | |
1144 | if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) | |
1145 | max_dev_lun = min(8U, max_dev_lun); | |
1146 | ||
1147 | /* | |
1148 | * We have already scanned LUN 0, so start at LUN 1. Keep scanning | |
1149 | * until we reach the max, or no LUN is found and we are not | |
1150 | * sparse_lun. | |
1151 | */ | |
1152 | for (lun = 1; lun < max_dev_lun; ++lun) | |
1153 | if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, | |
1154 | NULL) != SCSI_SCAN_LUN_PRESENT) && | |
1155 | !sparse_lun) | |
1156 | return; | |
1157 | } | |
1158 | ||
1159 | /** | |
1160 | * scsilun_to_int: convert a scsi_lun to an int | |
1161 | * @scsilun: struct scsi_lun to be converted. | |
1162 | * | |
1163 | * Description: | |
1164 | * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered | |
1165 | * integer, and return the result. The caller must check for | |
1166 | * truncation before using this function. | |
1167 | * | |
1168 | * Notes: | |
1169 | * The struct scsi_lun is assumed to be four levels, with each level | |
1170 | * effectively containing a SCSI byte-ordered (big endian) short; the | |
1171 | * addressing bits of each level are ignored (the highest two bits). | |
1172 | * For a description of the LUN format, post SCSI-3 see the SCSI | |
1173 | * Architecture Model, for SCSI-3 see the SCSI Controller Commands. | |
1174 | * | |
1175 | * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns | |
1176 | * the integer: 0x0b030a04 | |
1177 | **/ | |
1178 | static int scsilun_to_int(struct scsi_lun *scsilun) | |
1179 | { | |
1180 | int i; | |
1181 | unsigned int lun; | |
1182 | ||
1183 | lun = 0; | |
1184 | for (i = 0; i < sizeof(lun); i += 2) | |
1185 | lun = lun | (((scsilun->scsi_lun[i] << 8) | | |
1186 | scsilun->scsi_lun[i + 1]) << (i * 8)); | |
1187 | return lun; | |
1188 | } | |
1189 | ||
2f4701d8 JSEC |
1190 | /** |
1191 | * int_to_scsilun: reverts an int into a scsi_lun | |
1192 | * @int: integer to be reverted | |
1193 | * @scsilun: struct scsi_lun to be set. | |
1194 | * | |
1195 | * Description: | |
1196 | * Reverts the functionality of the scsilun_to_int, which packed | |
1197 | * an 8-byte lun value into an int. This routine unpacks the int | |
1198 | * back into the lun value. | |
1199 | * Note: the scsilun_to_int() routine does not truly handle all | |
1200 | * 8bytes of the lun value. This functions restores only as much | |
1201 | * as was set by the routine. | |
1202 | * | |
1203 | * Notes: | |
1204 | * Given an integer : 0x0b030a04, this function returns a | |
1205 | * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00 | |
1206 | * | |
1207 | **/ | |
1208 | void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun) | |
1209 | { | |
1210 | int i; | |
1211 | ||
1212 | memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun)); | |
1213 | ||
1214 | for (i = 0; i < sizeof(lun); i += 2) { | |
1215 | scsilun->scsi_lun[i] = (lun >> 8) & 0xFF; | |
1216 | scsilun->scsi_lun[i+1] = lun & 0xFF; | |
1217 | lun = lun >> 16; | |
1218 | } | |
1219 | } | |
1220 | EXPORT_SYMBOL(int_to_scsilun); | |
1221 | ||
1da177e4 LT |
1222 | /** |
1223 | * scsi_report_lun_scan - Scan using SCSI REPORT LUN results | |
f64a181d | 1224 | * @sdevscan: scan the host, channel, and id of this scsi_device |
1da177e4 LT |
1225 | * |
1226 | * Description: | |
1227 | * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN | |
1228 | * command, and scan the resulting list of LUNs by calling | |
1229 | * scsi_probe_and_add_lun. | |
1230 | * | |
1231 | * Modifies sdevscan->lun. | |
1232 | * | |
1233 | * Return: | |
1234 | * 0: scan completed (or no memory, so further scanning is futile) | |
1235 | * 1: no report lun scan, or not configured | |
1236 | **/ | |
6f3a2024 | 1237 | static int scsi_report_lun_scan(struct scsi_target *starget, int bflags, |
1da177e4 LT |
1238 | int rescan) |
1239 | { | |
1240 | char devname[64]; | |
1241 | unsigned char scsi_cmd[MAX_COMMAND_SIZE]; | |
1242 | unsigned int length; | |
1243 | unsigned int lun; | |
1244 | unsigned int num_luns; | |
1245 | unsigned int retries; | |
39216033 | 1246 | int result; |
1da177e4 | 1247 | struct scsi_lun *lunp, *lun_data; |
1da177e4 LT |
1248 | u8 *data; |
1249 | struct scsi_sense_hdr sshdr; | |
6f3a2024 JB |
1250 | struct scsi_device *sdev; |
1251 | struct Scsi_Host *shost = dev_to_shost(&starget->dev); | |
2ef89198 | 1252 | int ret = 0; |
1da177e4 LT |
1253 | |
1254 | /* | |
1255 | * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set. | |
1256 | * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does | |
1257 | * support more than 8 LUNs. | |
1258 | */ | |
4d7db04a JB |
1259 | if (bflags & BLIST_NOREPORTLUN) |
1260 | return 1; | |
1261 | if (starget->scsi_level < SCSI_2 && | |
1262 | starget->scsi_level != SCSI_UNKNOWN) | |
1263 | return 1; | |
1264 | if (starget->scsi_level < SCSI_3 && | |
1265 | (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) | |
1da177e4 LT |
1266 | return 1; |
1267 | if (bflags & BLIST_NOLUN) | |
1268 | return 0; | |
1269 | ||
6f3a2024 JB |
1270 | if (!(sdev = scsi_device_lookup_by_target(starget, 0))) { |
1271 | sdev = scsi_alloc_sdev(starget, 0, NULL); | |
1272 | if (!sdev) | |
1273 | return 0; | |
1274 | if (scsi_device_get(sdev)) | |
1275 | return 0; | |
1276 | } | |
1277 | ||
1da177e4 | 1278 | sprintf(devname, "host %d channel %d id %d", |
6f3a2024 | 1279 | shost->host_no, sdev->channel, sdev->id); |
1da177e4 LT |
1280 | |
1281 | /* | |
1282 | * Allocate enough to hold the header (the same size as one scsi_lun) | |
1283 | * plus the max number of luns we are requesting. | |
1284 | * | |
1285 | * Reallocating and trying again (with the exact amount we need) | |
1286 | * would be nice, but then we need to somehow limit the size | |
1287 | * allocated based on the available memory and the limits of | |
1288 | * kmalloc - we don't want a kmalloc() failure of a huge value to | |
1289 | * prevent us from finding any LUNs on this target. | |
1290 | */ | |
1291 | length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun); | |
1292 | lun_data = kmalloc(length, GFP_ATOMIC | | |
1293 | (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0)); | |
6f3a2024 JB |
1294 | if (!lun_data) { |
1295 | printk(ALLOC_FAILURE_MSG, __FUNCTION__); | |
39216033 | 1296 | goto out; |
6f3a2024 | 1297 | } |
1da177e4 LT |
1298 | |
1299 | scsi_cmd[0] = REPORT_LUNS; | |
1300 | ||
1301 | /* | |
1302 | * bytes 1 - 5: reserved, set to zero. | |
1303 | */ | |
1304 | memset(&scsi_cmd[1], 0, 5); | |
1305 | ||
1306 | /* | |
1307 | * bytes 6 - 9: length of the command. | |
1308 | */ | |
1309 | scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff; | |
1310 | scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff; | |
1311 | scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff; | |
1312 | scsi_cmd[9] = (unsigned char) length & 0xff; | |
1313 | ||
1314 | scsi_cmd[10] = 0; /* reserved */ | |
1315 | scsi_cmd[11] = 0; /* control */ | |
1da177e4 LT |
1316 | |
1317 | /* | |
1318 | * We can get a UNIT ATTENTION, for example a power on/reset, so | |
1319 | * retry a few times (like sd.c does for TEST UNIT READY). | |
1320 | * Experience shows some combinations of adapter/devices get at | |
1321 | * least two power on/resets. | |
1322 | * | |
1323 | * Illegal requests (for devices that do not support REPORT LUNS) | |
1324 | * should come through as a check condition, and will not generate | |
1325 | * a retry. | |
1326 | */ | |
1327 | for (retries = 0; retries < 3; retries++) { | |
1328 | SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending" | |
1329 | " REPORT LUNS to %s (try %d)\n", devname, | |
1330 | retries)); | |
39216033 | 1331 | |
39216033 | 1332 | result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, |
ea73a9f2 | 1333 | lun_data, length, &sshdr, |
39216033 JB |
1334 | SCSI_TIMEOUT + 4 * HZ, 3); |
1335 | ||
1da177e4 | 1336 | SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS" |
39216033 JB |
1337 | " %s (try %d) result 0x%x\n", result |
1338 | ? "failed" : "successful", retries, result)); | |
1339 | if (result == 0) | |
1da177e4 | 1340 | break; |
ea73a9f2 | 1341 | else if (scsi_sense_valid(&sshdr)) { |
1da177e4 LT |
1342 | if (sshdr.sense_key != UNIT_ATTENTION) |
1343 | break; | |
1344 | } | |
1345 | } | |
1346 | ||
39216033 | 1347 | if (result) { |
1da177e4 LT |
1348 | /* |
1349 | * The device probably does not support a REPORT LUN command | |
1350 | */ | |
2ef89198 AS |
1351 | ret = 1; |
1352 | goto out_err; | |
1da177e4 | 1353 | } |
1da177e4 LT |
1354 | |
1355 | /* | |
1356 | * Get the length from the first four bytes of lun_data. | |
1357 | */ | |
1358 | data = (u8 *) lun_data->scsi_lun; | |
1359 | length = ((data[0] << 24) | (data[1] << 16) | | |
1360 | (data[2] << 8) | (data[3] << 0)); | |
1361 | ||
1362 | num_luns = (length / sizeof(struct scsi_lun)); | |
1363 | if (num_luns > max_scsi_report_luns) { | |
1364 | printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)" | |
1365 | " of %d luns reported, try increasing" | |
1366 | " max_scsi_report_luns.\n", devname, | |
1367 | max_scsi_report_luns, num_luns); | |
1368 | num_luns = max_scsi_report_luns; | |
1369 | } | |
1370 | ||
3bf743e7 JG |
1371 | SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, |
1372 | "scsi scan: REPORT LUN scan\n")); | |
1da177e4 LT |
1373 | |
1374 | /* | |
1375 | * Scan the luns in lun_data. The entry at offset 0 is really | |
1376 | * the header, so start at 1 and go up to and including num_luns. | |
1377 | */ | |
1378 | for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) { | |
1379 | lun = scsilun_to_int(lunp); | |
1380 | ||
1381 | /* | |
1382 | * Check if the unused part of lunp is non-zero, and so | |
1383 | * does not fit in lun. | |
1384 | */ | |
1385 | if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) { | |
1386 | int i; | |
1387 | ||
1388 | /* | |
1389 | * Output an error displaying the LUN in byte order, | |
1390 | * this differs from what linux would print for the | |
1391 | * integer LUN value. | |
1392 | */ | |
1393 | printk(KERN_WARNING "scsi: %s lun 0x", devname); | |
1394 | data = (char *)lunp->scsi_lun; | |
1395 | for (i = 0; i < sizeof(struct scsi_lun); i++) | |
1396 | printk("%02x", data[i]); | |
1397 | printk(" has a LUN larger than currently supported.\n"); | |
1da177e4 LT |
1398 | } else if (lun > sdev->host->max_lun) { |
1399 | printk(KERN_WARNING "scsi: %s lun%d has a LUN larger" | |
1400 | " than allowed by the host adapter\n", | |
1401 | devname, lun); | |
1402 | } else { | |
1403 | int res; | |
1404 | ||
1405 | res = scsi_probe_and_add_lun(starget, | |
1406 | lun, NULL, NULL, rescan, NULL); | |
1407 | if (res == SCSI_SCAN_NO_RESPONSE) { | |
1408 | /* | |
1409 | * Got some results, but now none, abort. | |
1410 | */ | |
3bf743e7 JG |
1411 | sdev_printk(KERN_ERR, sdev, |
1412 | "Unexpected response" | |
1413 | " from lun %d while scanning, scan" | |
1414 | " aborted\n", lun); | |
1da177e4 LT |
1415 | break; |
1416 | } | |
1417 | } | |
1418 | } | |
1419 | ||
2ef89198 | 1420 | out_err: |
1da177e4 | 1421 | kfree(lun_data); |
1da177e4 | 1422 | out: |
6f3a2024 JB |
1423 | scsi_device_put(sdev); |
1424 | if (sdev->sdev_state == SDEV_CREATED) | |
1425 | /* | |
1426 | * the sdev we used didn't appear in the report luns scan | |
1427 | */ | |
1428 | scsi_destroy_sdev(sdev); | |
2ef89198 | 1429 | return ret; |
1da177e4 LT |
1430 | } |
1431 | ||
1432 | struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel, | |
1433 | uint id, uint lun, void *hostdata) | |
1434 | { | |
a97a83a0 | 1435 | struct scsi_device *sdev = ERR_PTR(-ENODEV); |
1da177e4 | 1436 | struct device *parent = &shost->shost_gendev; |
e02f3f59 | 1437 | struct scsi_target *starget; |
1da177e4 | 1438 | |
e02f3f59 | 1439 | starget = scsi_alloc_target(parent, channel, id); |
1da177e4 LT |
1440 | if (!starget) |
1441 | return ERR_PTR(-ENOMEM); | |
1442 | ||
0b950672 | 1443 | mutex_lock(&shost->scan_mutex); |
a97a83a0 MW |
1444 | if (scsi_host_scan_allowed(shost)) |
1445 | scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata); | |
0b950672 | 1446 | mutex_unlock(&shost->scan_mutex); |
1da177e4 LT |
1447 | scsi_target_reap(starget); |
1448 | put_device(&starget->dev); | |
1449 | ||
1450 | return sdev; | |
1451 | } | |
1452 | EXPORT_SYMBOL(__scsi_add_device); | |
1453 | ||
146f7262 JB |
1454 | int scsi_add_device(struct Scsi_Host *host, uint channel, |
1455 | uint target, uint lun) | |
1456 | { | |
1457 | struct scsi_device *sdev = | |
1458 | __scsi_add_device(host, channel, target, lun, NULL); | |
1459 | if (IS_ERR(sdev)) | |
1460 | return PTR_ERR(sdev); | |
1461 | ||
1462 | scsi_device_put(sdev); | |
1463 | return 0; | |
1464 | } | |
1465 | EXPORT_SYMBOL(scsi_add_device); | |
1466 | ||
1da177e4 LT |
1467 | void scsi_rescan_device(struct device *dev) |
1468 | { | |
1469 | struct scsi_driver *drv; | |
1470 | ||
1471 | if (!dev->driver) | |
1472 | return; | |
1473 | ||
1474 | drv = to_scsi_driver(dev->driver); | |
1475 | if (try_module_get(drv->owner)) { | |
1476 | if (drv->rescan) | |
1477 | drv->rescan(dev); | |
1478 | module_put(drv->owner); | |
1479 | } | |
1480 | } | |
1481 | EXPORT_SYMBOL(scsi_rescan_device); | |
1482 | ||
e517d313 AS |
1483 | static void __scsi_scan_target(struct device *parent, unsigned int channel, |
1484 | unsigned int id, unsigned int lun, int rescan) | |
1da177e4 LT |
1485 | { |
1486 | struct Scsi_Host *shost = dev_to_shost(parent); | |
1487 | int bflags = 0; | |
1488 | int res; | |
1da177e4 LT |
1489 | struct scsi_target *starget; |
1490 | ||
1491 | if (shost->this_id == id) | |
1492 | /* | |
1493 | * Don't scan the host adapter | |
1494 | */ | |
1495 | return; | |
1496 | ||
1da177e4 | 1497 | starget = scsi_alloc_target(parent, channel, id); |
1da177e4 LT |
1498 | if (!starget) |
1499 | return; | |
1500 | ||
1da177e4 LT |
1501 | if (lun != SCAN_WILD_CARD) { |
1502 | /* | |
1503 | * Scan for a specific host/chan/id/lun. | |
1504 | */ | |
1505 | scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL); | |
1506 | goto out_reap; | |
1507 | } | |
1508 | ||
1509 | /* | |
1510 | * Scan LUN 0, if there is some response, scan further. Ideally, we | |
1511 | * would not configure LUN 0 until all LUNs are scanned. | |
1512 | */ | |
6f3a2024 JB |
1513 | res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL); |
1514 | if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) { | |
1515 | if (scsi_report_lun_scan(starget, bflags, rescan) != 0) | |
1da177e4 LT |
1516 | /* |
1517 | * The REPORT LUN did not scan the target, | |
1518 | * do a sequential scan. | |
1519 | */ | |
1520 | scsi_sequential_lun_scan(starget, bflags, | |
4186ab19 | 1521 | starget->scsi_level, rescan); |
1da177e4 | 1522 | } |
1da177e4 LT |
1523 | |
1524 | out_reap: | |
1525 | /* now determine if the target has any children at all | |
1526 | * and if not, nuke it */ | |
1527 | scsi_target_reap(starget); | |
1528 | ||
1529 | put_device(&starget->dev); | |
1530 | } | |
e517d313 AS |
1531 | |
1532 | /** | |
1533 | * scsi_scan_target - scan a target id, possibly including all LUNs on the | |
1534 | * target. | |
1535 | * @parent: host to scan | |
1536 | * @channel: channel to scan | |
1537 | * @id: target id to scan | |
1538 | * @lun: Specific LUN to scan or SCAN_WILD_CARD | |
1539 | * @rescan: passed to LUN scanning routines | |
1540 | * | |
1541 | * Description: | |
1542 | * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0, | |
1543 | * and possibly all LUNs on the target id. | |
1544 | * | |
1545 | * First try a REPORT LUN scan, if that does not scan the target, do a | |
1546 | * sequential scan of LUNs on the target id. | |
1547 | **/ | |
1548 | void scsi_scan_target(struct device *parent, unsigned int channel, | |
1549 | unsigned int id, unsigned int lun, int rescan) | |
1550 | { | |
1551 | struct Scsi_Host *shost = dev_to_shost(parent); | |
1552 | ||
93b45af5 MW |
1553 | if (strncmp(scsi_scan_type, "none", 4) == 0) |
1554 | return; | |
1555 | ||
3e082a91 MW |
1556 | if (!shost->async_scan) |
1557 | scsi_complete_async_scans(); | |
1558 | ||
0b950672 | 1559 | mutex_lock(&shost->scan_mutex); |
e517d313 AS |
1560 | if (scsi_host_scan_allowed(shost)) |
1561 | __scsi_scan_target(parent, channel, id, lun, rescan); | |
0b950672 | 1562 | mutex_unlock(&shost->scan_mutex); |
e517d313 | 1563 | } |
1da177e4 LT |
1564 | EXPORT_SYMBOL(scsi_scan_target); |
1565 | ||
1566 | static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel, | |
1567 | unsigned int id, unsigned int lun, int rescan) | |
1568 | { | |
1569 | uint order_id; | |
1570 | ||
1571 | if (id == SCAN_WILD_CARD) | |
1572 | for (id = 0; id < shost->max_id; ++id) { | |
1573 | /* | |
1574 | * XXX adapter drivers when possible (FCP, iSCSI) | |
1575 | * could modify max_id to match the current max, | |
1576 | * not the absolute max. | |
1577 | * | |
1578 | * XXX add a shost id iterator, so for example, | |
1579 | * the FC ID can be the same as a target id | |
1580 | * without a huge overhead of sparse id's. | |
1581 | */ | |
1582 | if (shost->reverse_ordering) | |
1583 | /* | |
1584 | * Scan from high to low id. | |
1585 | */ | |
1586 | order_id = shost->max_id - id - 1; | |
1587 | else | |
1588 | order_id = id; | |
e517d313 AS |
1589 | __scsi_scan_target(&shost->shost_gendev, channel, |
1590 | order_id, lun, rescan); | |
1da177e4 LT |
1591 | } |
1592 | else | |
e517d313 AS |
1593 | __scsi_scan_target(&shost->shost_gendev, channel, |
1594 | id, lun, rescan); | |
1da177e4 LT |
1595 | } |
1596 | ||
1597 | int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel, | |
1598 | unsigned int id, unsigned int lun, int rescan) | |
1599 | { | |
3bf743e7 JG |
1600 | SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost, |
1601 | "%s: <%u:%u:%u>\n", | |
1602 | __FUNCTION__, channel, id, lun)); | |
1da177e4 | 1603 | |
3e082a91 MW |
1604 | if (!shost->async_scan) |
1605 | scsi_complete_async_scans(); | |
1606 | ||
1da177e4 | 1607 | if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || |
091686d3 | 1608 | ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || |
1da177e4 LT |
1609 | ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun))) |
1610 | return -EINVAL; | |
1611 | ||
0b950672 | 1612 | mutex_lock(&shost->scan_mutex); |
82f29467 MA |
1613 | if (scsi_host_scan_allowed(shost)) { |
1614 | if (channel == SCAN_WILD_CARD) | |
1615 | for (channel = 0; channel <= shost->max_channel; | |
1616 | channel++) | |
1617 | scsi_scan_channel(shost, channel, id, lun, | |
1618 | rescan); | |
1619 | else | |
1da177e4 | 1620 | scsi_scan_channel(shost, channel, id, lun, rescan); |
82f29467 | 1621 | } |
0b950672 | 1622 | mutex_unlock(&shost->scan_mutex); |
1da177e4 LT |
1623 | |
1624 | return 0; | |
1625 | } | |
1626 | ||
3e082a91 MW |
1627 | static void scsi_sysfs_add_devices(struct Scsi_Host *shost) |
1628 | { | |
1629 | struct scsi_device *sdev; | |
1630 | shost_for_each_device(sdev, shost) { | |
1631 | if (scsi_sysfs_add_sdev(sdev) != 0) | |
1632 | scsi_destroy_sdev(sdev); | |
1633 | } | |
1634 | } | |
1635 | ||
1636 | /** | |
1637 | * scsi_prep_async_scan - prepare for an async scan | |
1638 | * @shost: the host which will be scanned | |
1639 | * Returns: a cookie to be passed to scsi_finish_async_scan() | |
1640 | * | |
1641 | * Tells the midlayer this host is going to do an asynchronous scan. | |
1642 | * It reserves the host's position in the scanning list and ensures | |
1643 | * that other asynchronous scans started after this one won't affect the | |
1644 | * ordering of the discovered devices. | |
1645 | */ | |
1aa8fab2 | 1646 | static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) |
3e082a91 MW |
1647 | { |
1648 | struct async_scan_data *data; | |
1649 | ||
1650 | if (strncmp(scsi_scan_type, "sync", 4) == 0) | |
1651 | return NULL; | |
1652 | ||
1653 | if (shost->async_scan) { | |
1654 | printk("%s called twice for host %d", __FUNCTION__, | |
1655 | shost->host_no); | |
1656 | dump_stack(); | |
1657 | return NULL; | |
1658 | } | |
1659 | ||
1660 | data = kmalloc(sizeof(*data), GFP_KERNEL); | |
1661 | if (!data) | |
1662 | goto err; | |
1663 | data->shost = scsi_host_get(shost); | |
1664 | if (!data->shost) | |
1665 | goto err; | |
1666 | init_completion(&data->prev_finished); | |
1667 | ||
1668 | spin_lock(&async_scan_lock); | |
1669 | shost->async_scan = 1; | |
1670 | if (list_empty(&scanning_hosts)) | |
1671 | complete(&data->prev_finished); | |
1672 | list_add_tail(&data->list, &scanning_hosts); | |
1673 | spin_unlock(&async_scan_lock); | |
1674 | ||
1675 | return data; | |
1676 | ||
1677 | err: | |
1678 | kfree(data); | |
1679 | return NULL; | |
1680 | } | |
1681 | ||
1682 | /** | |
1683 | * scsi_finish_async_scan - asynchronous scan has finished | |
1684 | * @data: cookie returned from earlier call to scsi_prep_async_scan() | |
1685 | * | |
1686 | * All the devices currently attached to this host have been found. | |
1687 | * This function announces all the devices it has found to the rest | |
1688 | * of the system. | |
1689 | */ | |
1aa8fab2 | 1690 | static void scsi_finish_async_scan(struct async_scan_data *data) |
3e082a91 MW |
1691 | { |
1692 | struct Scsi_Host *shost; | |
1693 | ||
1694 | if (!data) | |
1695 | return; | |
1696 | ||
1697 | shost = data->shost; | |
1698 | if (!shost->async_scan) { | |
1699 | printk("%s called twice for host %d", __FUNCTION__, | |
1700 | shost->host_no); | |
1701 | dump_stack(); | |
1702 | return; | |
1703 | } | |
1704 | ||
1705 | wait_for_completion(&data->prev_finished); | |
1706 | ||
1707 | scsi_sysfs_add_devices(shost); | |
1708 | ||
1709 | spin_lock(&async_scan_lock); | |
1710 | shost->async_scan = 0; | |
1711 | list_del(&data->list); | |
1712 | if (!list_empty(&scanning_hosts)) { | |
1713 | struct async_scan_data *next = list_entry(scanning_hosts.next, | |
1714 | struct async_scan_data, list); | |
1715 | complete(&next->prev_finished); | |
1716 | } | |
1717 | spin_unlock(&async_scan_lock); | |
1718 | ||
1719 | scsi_host_put(shost); | |
1720 | kfree(data); | |
1721 | } | |
1722 | ||
1aa8fab2 | 1723 | static void do_scsi_scan_host(struct Scsi_Host *shost) |
3e082a91 | 1724 | { |
1aa8fab2 MW |
1725 | if (shost->hostt->scan_finished) { |
1726 | unsigned long start = jiffies; | |
1727 | if (shost->hostt->scan_start) | |
1728 | shost->hostt->scan_start(shost); | |
1729 | ||
1730 | while (!shost->hostt->scan_finished(shost, jiffies - start)) | |
1731 | msleep(10); | |
1732 | } else { | |
1733 | scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD, | |
3e082a91 | 1734 | SCAN_WILD_CARD, 0); |
1aa8fab2 MW |
1735 | } |
1736 | } | |
3e082a91 | 1737 | |
1aa8fab2 MW |
1738 | static int do_scan_async(void *_data) |
1739 | { | |
1740 | struct async_scan_data *data = _data; | |
1741 | do_scsi_scan_host(data->shost); | |
3e082a91 MW |
1742 | scsi_finish_async_scan(data); |
1743 | return 0; | |
1744 | } | |
1745 | ||
1da177e4 LT |
1746 | /** |
1747 | * scsi_scan_host - scan the given adapter | |
1748 | * @shost: adapter to scan | |
1749 | **/ | |
1750 | void scsi_scan_host(struct Scsi_Host *shost) | |
1751 | { | |
3e082a91 MW |
1752 | struct async_scan_data *data; |
1753 | ||
1754 | if (strncmp(scsi_scan_type, "none", 4) == 0) | |
1755 | return; | |
1756 | ||
1757 | data = scsi_prep_async_scan(shost); | |
1758 | if (!data) { | |
1aa8fab2 | 1759 | do_scsi_scan_host(shost); |
3e082a91 MW |
1760 | return; |
1761 | } | |
1aa8fab2 | 1762 | |
3e082a91 | 1763 | kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no); |
1da177e4 LT |
1764 | } |
1765 | EXPORT_SYMBOL(scsi_scan_host); | |
1766 | ||
1da177e4 LT |
1767 | void scsi_forget_host(struct Scsi_Host *shost) |
1768 | { | |
a64358db | 1769 | struct scsi_device *sdev; |
1da177e4 LT |
1770 | unsigned long flags; |
1771 | ||
a64358db | 1772 | restart: |
1da177e4 | 1773 | spin_lock_irqsave(shost->host_lock, flags); |
a64358db AS |
1774 | list_for_each_entry(sdev, &shost->__devices, siblings) { |
1775 | if (sdev->sdev_state == SDEV_DEL) | |
1776 | continue; | |
1da177e4 | 1777 | spin_unlock_irqrestore(shost->host_lock, flags); |
a64358db AS |
1778 | __scsi_remove_device(sdev); |
1779 | goto restart; | |
1da177e4 LT |
1780 | } |
1781 | spin_unlock_irqrestore(shost->host_lock, flags); | |
1782 | } | |
1783 | ||
1784 | /* | |
1785 | * Function: scsi_get_host_dev() | |
1786 | * | |
f64a181d | 1787 | * Purpose: Create a scsi_device that points to the host adapter itself. |
1da177e4 | 1788 | * |
f64a181d | 1789 | * Arguments: SHpnt - Host that needs a scsi_device |
1da177e4 LT |
1790 | * |
1791 | * Lock status: None assumed. | |
1792 | * | |
f64a181d | 1793 | * Returns: The scsi_device or NULL |
1da177e4 LT |
1794 | * |
1795 | * Notes: | |
f64a181d | 1796 | * Attach a single scsi_device to the Scsi_Host - this should |
1da177e4 LT |
1797 | * be made to look like a "pseudo-device" that points to the |
1798 | * HA itself. | |
1799 | * | |
1800 | * Note - this device is not accessible from any high-level | |
1801 | * drivers (including generics), which is probably not | |
1802 | * optimal. We can add hooks later to attach | |
1803 | */ | |
1804 | struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost) | |
1805 | { | |
e517d313 | 1806 | struct scsi_device *sdev = NULL; |
1da177e4 LT |
1807 | struct scsi_target *starget; |
1808 | ||
0b950672 | 1809 | mutex_lock(&shost->scan_mutex); |
e517d313 AS |
1810 | if (!scsi_host_scan_allowed(shost)) |
1811 | goto out; | |
1da177e4 LT |
1812 | starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id); |
1813 | if (!starget) | |
e517d313 | 1814 | goto out; |
1da177e4 LT |
1815 | |
1816 | sdev = scsi_alloc_sdev(starget, 0, NULL); | |
1817 | if (sdev) { | |
1818 | sdev->sdev_gendev.parent = get_device(&starget->dev); | |
1819 | sdev->borken = 0; | |
884d25cc JB |
1820 | } else |
1821 | scsi_target_reap(starget); | |
1da177e4 | 1822 | put_device(&starget->dev); |
e517d313 | 1823 | out: |
0b950672 | 1824 | mutex_unlock(&shost->scan_mutex); |
1da177e4 LT |
1825 | return sdev; |
1826 | } | |
1827 | EXPORT_SYMBOL(scsi_get_host_dev); | |
1828 | ||
1829 | /* | |
1830 | * Function: scsi_free_host_dev() | |
1831 | * | |
1832 | * Purpose: Free a scsi_device that points to the host adapter itself. | |
1833 | * | |
f64a181d | 1834 | * Arguments: SHpnt - Host that needs a scsi_device |
1da177e4 LT |
1835 | * |
1836 | * Lock status: None assumed. | |
1837 | * | |
1838 | * Returns: Nothing | |
1839 | * | |
1840 | * Notes: | |
1841 | */ | |
1842 | void scsi_free_host_dev(struct scsi_device *sdev) | |
1843 | { | |
1844 | BUG_ON(sdev->id != sdev->host->this_id); | |
1845 | ||
6f3a2024 | 1846 | scsi_destroy_sdev(sdev); |
1da177e4 LT |
1847 | } |
1848 | EXPORT_SYMBOL(scsi_free_host_dev); | |
1849 |