PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/scsi_scan.c

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