scsi_scan.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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/config.h>
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/init.h>
  31. #include <linux/blkdev.h>
  32. #include <asm/semaphore.h>
  33. #include <scsi/scsi.h>
  34. #include <scsi/scsi_device.h>
  35. #include <scsi/scsi_driver.h>
  36. #include <scsi/scsi_devinfo.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_request.h>
  39. #include <scsi/scsi_transport.h>
  40. #include <scsi/scsi_eh.h>
  41. #include "scsi_priv.h"
  42. #include "scsi_logging.h"
  43. #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
  44. " SCSI scanning, some SCSI devices might not be configured\n"
  45. /*
  46. * Default timeout
  47. */
  48. #define SCSI_TIMEOUT (2*HZ)
  49. /*
  50. * Prefix values for the SCSI id's (stored in driverfs name field)
  51. */
  52. #define SCSI_UID_SER_NUM 'S'
  53. #define SCSI_UID_UNKNOWN 'Z'
  54. /*
  55. * Return values of some of the scanning functions.
  56. *
  57. * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
  58. * includes allocation or general failures preventing IO from being sent.
  59. *
  60. * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
  61. * on the given LUN.
  62. *
  63. * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
  64. * given LUN.
  65. */
  66. #define SCSI_SCAN_NO_RESPONSE 0
  67. #define SCSI_SCAN_TARGET_PRESENT 1
  68. #define SCSI_SCAN_LUN_PRESENT 2
  69. static char *scsi_null_device_strs = "nullnullnullnull";
  70. #define MAX_SCSI_LUNS 512
  71. #ifdef CONFIG_SCSI_MULTI_LUN
  72. static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
  73. #else
  74. static unsigned int max_scsi_luns = 1;
  75. #endif
  76. module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR);
  77. MODULE_PARM_DESC(max_luns,
  78. "last scsi LUN (should be between 1 and 2^32-1)");
  79. /*
  80. * max_scsi_report_luns: the maximum number of LUNS that will be
  81. * returned from the REPORT LUNS command. 8 times this value must
  82. * be allocated. In theory this could be up to an 8 byte value, but
  83. * in practice, the maximum number of LUNs suppored by any device
  84. * is about 16k.
  85. */
  86. static unsigned int max_scsi_report_luns = 511;
  87. module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
  88. MODULE_PARM_DESC(max_report_luns,
  89. "REPORT LUNS maximum number of LUNS received (should be"
  90. " between 1 and 16384)");
  91. static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
  92. module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
  93. MODULE_PARM_DESC(inq_timeout,
  94. "Timeout (in seconds) waiting for devices to answer INQUIRY."
  95. " Default is 5. Some non-compliant devices need more.");
  96. /**
  97. * scsi_unlock_floptical - unlock device via a special MODE SENSE command
  98. * @sdev: scsi device to send command to
  99. * @result: area to store the result of the MODE SENSE
  100. *
  101. * Description:
  102. * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
  103. * Called for BLIST_KEY devices.
  104. **/
  105. static void scsi_unlock_floptical(struct scsi_device *sdev,
  106. unsigned char *result)
  107. {
  108. unsigned char scsi_cmd[MAX_COMMAND_SIZE];
  109. printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
  110. scsi_cmd[0] = MODE_SENSE;
  111. scsi_cmd[1] = 0;
  112. scsi_cmd[2] = 0x2e;
  113. scsi_cmd[3] = 0;
  114. scsi_cmd[4] = 0x2a; /* size */
  115. scsi_cmd[5] = 0;
  116. scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
  117. SCSI_TIMEOUT, 3);
  118. }
  119. /**
  120. * print_inquiry - printk the inquiry information
  121. * @inq_result: printk this SCSI INQUIRY
  122. *
  123. * Description:
  124. * printk the vendor, model, and other information found in the
  125. * INQUIRY data in @inq_result.
  126. *
  127. * Notes:
  128. * Remove this, and replace with a hotplug event that logs any
  129. * relevant information.
  130. **/
  131. static void print_inquiry(unsigned char *inq_result)
  132. {
  133. int i;
  134. printk(KERN_NOTICE " Vendor: ");
  135. for (i = 8; i < 16; i++)
  136. if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
  137. printk("%c", inq_result[i]);
  138. else
  139. printk(" ");
  140. printk(" Model: ");
  141. for (i = 16; i < 32; i++)
  142. if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
  143. printk("%c", inq_result[i]);
  144. else
  145. printk(" ");
  146. printk(" Rev: ");
  147. for (i = 32; i < 36; i++)
  148. if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
  149. printk("%c", inq_result[i]);
  150. else
  151. printk(" ");
  152. printk("\n");
  153. i = inq_result[0] & 0x1f;
  154. printk(KERN_NOTICE " Type: %s ",
  155. i <
  156. MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
  157. "Unknown ");
  158. printk(" ANSI SCSI revision: %02x",
  159. inq_result[2] & 0x07);
  160. if ((inq_result[2] & 0x07) == 1 && (inq_result[3] & 0x0f) == 1)
  161. printk(" CCS\n");
  162. else
  163. printk("\n");
  164. }
  165. /**
  166. * scsi_alloc_sdev - allocate and setup a scsi_Device
  167. *
  168. * Description:
  169. * Allocate, initialize for io, and return a pointer to a scsi_Device.
  170. * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
  171. * adds scsi_Device to the appropriate list.
  172. *
  173. * Return value:
  174. * scsi_Device pointer, or NULL on failure.
  175. **/
  176. static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
  177. unsigned int lun, void *hostdata)
  178. {
  179. struct scsi_device *sdev;
  180. int display_failure_msg = 1, ret;
  181. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  182. sdev = kmalloc(sizeof(*sdev) + shost->transportt->device_size,
  183. GFP_ATOMIC);
  184. if (!sdev)
  185. goto out;
  186. memset(sdev, 0, sizeof(*sdev));
  187. sdev->vendor = scsi_null_device_strs;
  188. sdev->model = scsi_null_device_strs;
  189. sdev->rev = scsi_null_device_strs;
  190. sdev->host = shost;
  191. sdev->id = starget->id;
  192. sdev->lun = lun;
  193. sdev->channel = starget->channel;
  194. sdev->sdev_state = SDEV_CREATED;
  195. INIT_LIST_HEAD(&sdev->siblings);
  196. INIT_LIST_HEAD(&sdev->same_target_siblings);
  197. INIT_LIST_HEAD(&sdev->cmd_list);
  198. INIT_LIST_HEAD(&sdev->starved_entry);
  199. spin_lock_init(&sdev->list_lock);
  200. sdev->sdev_gendev.parent = get_device(&starget->dev);
  201. sdev->sdev_target = starget;
  202. /* usually NULL and set by ->slave_alloc instead */
  203. sdev->hostdata = hostdata;
  204. /* if the device needs this changing, it may do so in the
  205. * slave_configure function */
  206. sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
  207. /*
  208. * Some low level driver could use device->type
  209. */
  210. sdev->type = -1;
  211. /*
  212. * Assume that the device will have handshaking problems,
  213. * and then fix this field later if it turns out it
  214. * doesn't
  215. */
  216. sdev->borken = 1;
  217. sdev->request_queue = scsi_alloc_queue(sdev);
  218. if (!sdev->request_queue) {
  219. /* release fn is set up in scsi_sysfs_device_initialise, so
  220. * have to free and put manually here */
  221. put_device(&starget->dev);
  222. goto out;
  223. }
  224. sdev->request_queue->queuedata = sdev;
  225. scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
  226. scsi_sysfs_device_initialize(sdev);
  227. if (shost->hostt->slave_alloc) {
  228. ret = shost->hostt->slave_alloc(sdev);
  229. if (ret) {
  230. /*
  231. * if LLDD reports slave not present, don't clutter
  232. * console with alloc failure messages
  233. */
  234. if (ret == -ENXIO)
  235. display_failure_msg = 0;
  236. goto out_device_destroy;
  237. }
  238. }
  239. return sdev;
  240. out_device_destroy:
  241. transport_destroy_device(&sdev->sdev_gendev);
  242. scsi_free_queue(sdev->request_queue);
  243. put_device(&sdev->sdev_gendev);
  244. out:
  245. if (display_failure_msg)
  246. printk(ALLOC_FAILURE_MSG, __FUNCTION__);
  247. return NULL;
  248. }
  249. static void scsi_target_dev_release(struct device *dev)
  250. {
  251. struct device *parent = dev->parent;
  252. struct scsi_target *starget = to_scsi_target(dev);
  253. struct Scsi_Host *shost = dev_to_shost(parent);
  254. if (shost->hostt->target_destroy)
  255. shost->hostt->target_destroy(starget);
  256. kfree(starget);
  257. put_device(parent);
  258. }
  259. int scsi_is_target_device(const struct device *dev)
  260. {
  261. return dev->release == scsi_target_dev_release;
  262. }
  263. EXPORT_SYMBOL(scsi_is_target_device);
  264. static struct scsi_target *__scsi_find_target(struct device *parent,
  265. int channel, uint id)
  266. {
  267. struct scsi_target *starget, *found_starget = NULL;
  268. struct Scsi_Host *shost = dev_to_shost(parent);
  269. /*
  270. * Search for an existing target for this sdev.
  271. */
  272. list_for_each_entry(starget, &shost->__targets, siblings) {
  273. if (starget->id == id &&
  274. starget->channel == channel) {
  275. found_starget = starget;
  276. break;
  277. }
  278. }
  279. if (found_starget)
  280. get_device(&found_starget->dev);
  281. return found_starget;
  282. }
  283. static struct scsi_target *scsi_alloc_target(struct device *parent,
  284. int channel, uint id)
  285. {
  286. struct Scsi_Host *shost = dev_to_shost(parent);
  287. struct device *dev = NULL;
  288. unsigned long flags;
  289. const int size = sizeof(struct scsi_target)
  290. + shost->transportt->target_size;
  291. struct scsi_target *starget;
  292. struct scsi_target *found_target;
  293. /*
  294. * Obtain the real parent from the transport. The transport
  295. * is allowed to fail (no error) if there is nothing at that
  296. * target id.
  297. */
  298. if (shost->transportt->target_parent) {
  299. spin_lock_irqsave(shost->host_lock, flags);
  300. parent = shost->transportt->target_parent(shost, channel, id);
  301. spin_unlock_irqrestore(shost->host_lock, flags);
  302. if (!parent)
  303. return NULL;
  304. }
  305. starget = kmalloc(size, GFP_KERNEL);
  306. if (!starget) {
  307. printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
  308. return NULL;
  309. }
  310. memset(starget, 0, size);
  311. dev = &starget->dev;
  312. device_initialize(dev);
  313. starget->reap_ref = 1;
  314. dev->parent = get_device(parent);
  315. dev->release = scsi_target_dev_release;
  316. sprintf(dev->bus_id, "target%d:%d:%d",
  317. shost->host_no, channel, id);
  318. starget->id = id;
  319. starget->channel = channel;
  320. INIT_LIST_HEAD(&starget->siblings);
  321. INIT_LIST_HEAD(&starget->devices);
  322. spin_lock_irqsave(shost->host_lock, flags);
  323. found_target = __scsi_find_target(parent, channel, id);
  324. if (found_target)
  325. goto found;
  326. list_add_tail(&starget->siblings, &shost->__targets);
  327. spin_unlock_irqrestore(shost->host_lock, flags);
  328. /* allocate and add */
  329. transport_setup_device(dev);
  330. device_add(dev);
  331. transport_add_device(dev);
  332. if (shost->hostt->target_alloc) {
  333. int error = shost->hostt->target_alloc(starget);
  334. if(error) {
  335. dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
  336. /* don't want scsi_target_reap to do the final
  337. * put because it will be under the host lock */
  338. get_device(dev);
  339. scsi_target_reap(starget);
  340. put_device(dev);
  341. return NULL;
  342. }
  343. }
  344. return starget;
  345. found:
  346. found_target->reap_ref++;
  347. spin_unlock_irqrestore(shost->host_lock, flags);
  348. put_device(parent);
  349. kfree(starget);
  350. return found_target;
  351. }
  352. /**
  353. * scsi_target_reap - check to see if target is in use and destroy if not
  354. *
  355. * @starget: target to be checked
  356. *
  357. * This is used after removing a LUN or doing a last put of the target
  358. * it checks atomically that nothing is using the target and removes
  359. * it if so.
  360. */
  361. void scsi_target_reap(struct scsi_target *starget)
  362. {
  363. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  364. unsigned long flags;
  365. spin_lock_irqsave(shost->host_lock, flags);
  366. if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
  367. list_del_init(&starget->siblings);
  368. spin_unlock_irqrestore(shost->host_lock, flags);
  369. device_del(&starget->dev);
  370. transport_unregister_device(&starget->dev);
  371. put_device(&starget->dev);
  372. return;
  373. }
  374. spin_unlock_irqrestore(shost->host_lock, flags);
  375. }
  376. /**
  377. * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
  378. * @sdev: scsi_device to probe
  379. * @inq_result: area to store the INQUIRY result
  380. * @result_len: len of inq_result
  381. * @bflags: store any bflags found here
  382. *
  383. * Description:
  384. * Probe the lun associated with @req using a standard SCSI INQUIRY;
  385. *
  386. * If the INQUIRY is successful, zero is returned and the
  387. * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
  388. * are copied to the Scsi_Device any flags value is stored in *@bflags.
  389. **/
  390. static int scsi_probe_lun(struct scsi_device *sdev, char *inq_result,
  391. int result_len, int *bflags)
  392. {
  393. unsigned char scsi_cmd[MAX_COMMAND_SIZE];
  394. int first_inquiry_len, try_inquiry_len, next_inquiry_len;
  395. int response_len = 0;
  396. int pass, count, result;
  397. struct scsi_sense_hdr sshdr;
  398. *bflags = 0;
  399. /* Perform up to 3 passes. The first pass uses a conservative
  400. * transfer length of 36 unless sdev->inquiry_len specifies a
  401. * different value. */
  402. first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
  403. try_inquiry_len = first_inquiry_len;
  404. pass = 1;
  405. next_pass:
  406. SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY pass %d "
  407. "to host %d channel %d id %d lun %d, length %d\n",
  408. pass, sdev->host->host_no, sdev->channel,
  409. sdev->id, sdev->lun, try_inquiry_len));
  410. /* Each pass gets up to three chances to ignore Unit Attention */
  411. for (count = 0; count < 3; ++count) {
  412. memset(scsi_cmd, 0, 6);
  413. scsi_cmd[0] = INQUIRY;
  414. scsi_cmd[4] = (unsigned char) try_inquiry_len;
  415. memset(inq_result, 0, try_inquiry_len);
  416. result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
  417. inq_result, try_inquiry_len, &sshdr,
  418. HZ / 2 + HZ * scsi_inq_timeout, 3);
  419. SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
  420. "with code 0x%x\n",
  421. result ? "failed" : "successful", result));
  422. if (result) {
  423. /*
  424. * not-ready to ready transition [asc/ascq=0x28/0x0]
  425. * or power-on, reset [asc/ascq=0x29/0x0], continue.
  426. * INQUIRY should not yield UNIT_ATTENTION
  427. * but many buggy devices do so anyway.
  428. */
  429. if ((driver_byte(result) & DRIVER_SENSE) &&
  430. scsi_sense_valid(&sshdr)) {
  431. if ((sshdr.sense_key == UNIT_ATTENTION) &&
  432. ((sshdr.asc == 0x28) ||
  433. (sshdr.asc == 0x29)) &&
  434. (sshdr.ascq == 0))
  435. continue;
  436. }
  437. }
  438. break;
  439. }
  440. if (result == 0) {
  441. response_len = (unsigned char) inq_result[4] + 5;
  442. if (response_len > 255)
  443. response_len = first_inquiry_len; /* sanity */
  444. /*
  445. * Get any flags for this device.
  446. *
  447. * XXX add a bflags to Scsi_Device, and replace the
  448. * corresponding bit fields in Scsi_Device, so bflags
  449. * need not be passed as an argument.
  450. */
  451. *bflags = scsi_get_device_flags(sdev, &inq_result[8],
  452. &inq_result[16]);
  453. /* When the first pass succeeds we gain information about
  454. * what larger transfer lengths might work. */
  455. if (pass == 1) {
  456. if (BLIST_INQUIRY_36 & *bflags)
  457. next_inquiry_len = 36;
  458. else if (BLIST_INQUIRY_58 & *bflags)
  459. next_inquiry_len = 58;
  460. else if (sdev->inquiry_len)
  461. next_inquiry_len = sdev->inquiry_len;
  462. else
  463. next_inquiry_len = response_len;
  464. /* If more data is available perform the second pass */
  465. if (next_inquiry_len > try_inquiry_len) {
  466. try_inquiry_len = next_inquiry_len;
  467. pass = 2;
  468. goto next_pass;
  469. }
  470. }
  471. } else if (pass == 2) {
  472. printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
  473. "Consider BLIST_INQUIRY_36 for this device\n",
  474. try_inquiry_len);
  475. /* If this pass failed, the third pass goes back and transfers
  476. * the same amount as we successfully got in the first pass. */
  477. try_inquiry_len = first_inquiry_len;
  478. pass = 3;
  479. goto next_pass;
  480. }
  481. /* If the last transfer attempt got an error, assume the
  482. * peripheral doesn't exist or is dead. */
  483. if (result)
  484. return -EIO;
  485. /* Don't report any more data than the device says is valid */
  486. sdev->inquiry_len = min(try_inquiry_len, response_len);
  487. /*
  488. * XXX Abort if the response length is less than 36? If less than
  489. * 32, the lookup of the device flags (above) could be invalid,
  490. * and it would be possible to take an incorrect action - we do
  491. * not want to hang because of a short INQUIRY. On the flip side,
  492. * if the device is spun down or becoming ready (and so it gives a
  493. * short INQUIRY), an abort here prevents any further use of the
  494. * device, including spin up.
  495. *
  496. * Related to the above issue:
  497. *
  498. * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
  499. * and if not ready, sent a START_STOP to start (maybe spin up) and
  500. * then send the INQUIRY again, since the INQUIRY can change after
  501. * a device is initialized.
  502. *
  503. * Ideally, start a device if explicitly asked to do so. This
  504. * assumes that a device is spun up on power on, spun down on
  505. * request, and then spun up on request.
  506. */
  507. /*
  508. * The scanning code needs to know the scsi_level, even if no
  509. * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
  510. * non-zero LUNs can be scanned.
  511. */
  512. sdev->scsi_level = inq_result[2] & 0x07;
  513. if (sdev->scsi_level >= 2 ||
  514. (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
  515. sdev->scsi_level++;
  516. sdev->sdev_target->scsi_level = sdev->scsi_level;
  517. return 0;
  518. }
  519. /**
  520. * scsi_add_lun - allocate and fully initialze a Scsi_Device
  521. * @sdevscan: holds information to be stored in the new Scsi_Device
  522. * @sdevnew: store the address of the newly allocated Scsi_Device
  523. * @inq_result: holds the result of a previous INQUIRY to the LUN
  524. * @bflags: black/white list flag
  525. *
  526. * Description:
  527. * Allocate and initialize a Scsi_Device matching sdevscan. Optionally
  528. * set fields based on values in *@bflags. If @sdevnew is not
  529. * NULL, store the address of the new Scsi_Device in *@sdevnew (needed
  530. * when scanning a particular LUN).
  531. *
  532. * Return:
  533. * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
  534. * SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
  535. **/
  536. static int scsi_add_lun(struct scsi_device *sdev, char *inq_result, int *bflags)
  537. {
  538. /*
  539. * XXX do not save the inquiry, since it can change underneath us,
  540. * save just vendor/model/rev.
  541. *
  542. * Rather than save it and have an ioctl that retrieves the saved
  543. * value, have an ioctl that executes the same INQUIRY code used
  544. * in scsi_probe_lun, let user level programs doing INQUIRY
  545. * scanning run at their own risk, or supply a user level program
  546. * that can correctly scan.
  547. */
  548. sdev->inquiry = kmalloc(sdev->inquiry_len, GFP_ATOMIC);
  549. if (sdev->inquiry == NULL) {
  550. return SCSI_SCAN_NO_RESPONSE;
  551. }
  552. memcpy(sdev->inquiry, inq_result, sdev->inquiry_len);
  553. sdev->vendor = (char *) (sdev->inquiry + 8);
  554. sdev->model = (char *) (sdev->inquiry + 16);
  555. sdev->rev = (char *) (sdev->inquiry + 32);
  556. if (*bflags & BLIST_ISROM) {
  557. /*
  558. * It would be better to modify sdev->type, and set
  559. * sdev->removable, but then the print_inquiry() output
  560. * would not show TYPE_ROM; if print_inquiry() is removed
  561. * the issue goes away.
  562. */
  563. inq_result[0] = TYPE_ROM;
  564. inq_result[1] |= 0x80; /* removable */
  565. } else if (*bflags & BLIST_NO_ULD_ATTACH)
  566. sdev->no_uld_attach = 1;
  567. switch (sdev->type = (inq_result[0] & 0x1f)) {
  568. case TYPE_TAPE:
  569. case TYPE_DISK:
  570. case TYPE_PRINTER:
  571. case TYPE_MOD:
  572. case TYPE_PROCESSOR:
  573. case TYPE_SCANNER:
  574. case TYPE_MEDIUM_CHANGER:
  575. case TYPE_ENCLOSURE:
  576. case TYPE_COMM:
  577. case TYPE_RBC:
  578. sdev->writeable = 1;
  579. break;
  580. case TYPE_WORM:
  581. case TYPE_ROM:
  582. sdev->writeable = 0;
  583. break;
  584. default:
  585. printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
  586. }
  587. print_inquiry(inq_result);
  588. /*
  589. * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
  590. * spec says: The device server is capable of supporting the
  591. * specified peripheral device type on this logical unit. However,
  592. * the physical device is not currently connected to this logical
  593. * unit.
  594. *
  595. * The above is vague, as it implies that we could treat 001 and
  596. * 011 the same. Stay compatible with previous code, and create a
  597. * Scsi_Device for a PQ of 1
  598. *
  599. * Don't set the device offline here; rather let the upper
  600. * level drivers eval the PQ to decide whether they should
  601. * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
  602. */
  603. sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
  604. sdev->removable = (0x80 & inq_result[1]) >> 7;
  605. sdev->lockable = sdev->removable;
  606. sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
  607. if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
  608. inq_result[56] & 0x04))
  609. sdev->ppr = 1;
  610. if (inq_result[7] & 0x60)
  611. sdev->wdtr = 1;
  612. if (inq_result[7] & 0x10)
  613. sdev->sdtr = 1;
  614. sprintf(sdev->devfs_name, "scsi/host%d/bus%d/target%d/lun%d",
  615. sdev->host->host_no, sdev->channel,
  616. sdev->id, sdev->lun);
  617. /*
  618. * End driverfs/devfs code.
  619. */
  620. if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
  621. !(*bflags & BLIST_NOTQ))
  622. sdev->tagged_supported = 1;
  623. /*
  624. * Some devices (Texel CD ROM drives) have handshaking problems
  625. * when used with the Seagate controllers. borken is initialized
  626. * to 1, and then set it to 0 here.
  627. */
  628. if ((*bflags & BLIST_BORKEN) == 0)
  629. sdev->borken = 0;
  630. /*
  631. * Apparently some really broken devices (contrary to the SCSI
  632. * standards) need to be selected without asserting ATN
  633. */
  634. if (*bflags & BLIST_SELECT_NO_ATN)
  635. sdev->select_no_atn = 1;
  636. /*
  637. * Some devices may not want to have a start command automatically
  638. * issued when a device is added.
  639. */
  640. if (*bflags & BLIST_NOSTARTONADD)
  641. sdev->no_start_on_add = 1;
  642. if (*bflags & BLIST_SINGLELUN)
  643. sdev->single_lun = 1;
  644. sdev->use_10_for_rw = 1;
  645. if (*bflags & BLIST_MS_SKIP_PAGE_08)
  646. sdev->skip_ms_page_8 = 1;
  647. if (*bflags & BLIST_MS_SKIP_PAGE_3F)
  648. sdev->skip_ms_page_3f = 1;
  649. if (*bflags & BLIST_USE_10_BYTE_MS)
  650. sdev->use_10_for_ms = 1;
  651. /* set the device running here so that slave configure
  652. * may do I/O */
  653. scsi_device_set_state(sdev, SDEV_RUNNING);
  654. if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
  655. sdev->use_192_bytes_for_3f = 1;
  656. if (*bflags & BLIST_NOT_LOCKABLE)
  657. sdev->lockable = 0;
  658. if (*bflags & BLIST_RETRY_HWERROR)
  659. sdev->retry_hwerror = 1;
  660. transport_configure_device(&sdev->sdev_gendev);
  661. if (sdev->host->hostt->slave_configure)
  662. sdev->host->hostt->slave_configure(sdev);
  663. /*
  664. * Ok, the device is now all set up, we can
  665. * register it and tell the rest of the kernel
  666. * about it.
  667. */
  668. if (scsi_sysfs_add_sdev(sdev) != 0)
  669. return SCSI_SCAN_NO_RESPONSE;
  670. return SCSI_SCAN_LUN_PRESENT;
  671. }
  672. static inline void scsi_destroy_sdev(struct scsi_device *sdev)
  673. {
  674. if (sdev->host->hostt->slave_destroy)
  675. sdev->host->hostt->slave_destroy(sdev);
  676. transport_destroy_device(&sdev->sdev_gendev);
  677. put_device(&sdev->sdev_gendev);
  678. }
  679. /**
  680. * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
  681. * @starget: pointer to target device structure
  682. * @lun: LUN of target device
  683. * @sdevscan: probe the LUN corresponding to this Scsi_Device
  684. * @sdevnew: store the value of any new Scsi_Device allocated
  685. * @bflagsp: store bflags here if not NULL
  686. *
  687. * Description:
  688. * Call scsi_probe_lun, if a LUN with an attached device is found,
  689. * allocate and set it up by calling scsi_add_lun.
  690. *
  691. * Return:
  692. * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
  693. * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
  694. * attached at the LUN
  695. * SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
  696. **/
  697. static int scsi_probe_and_add_lun(struct scsi_target *starget,
  698. uint lun, int *bflagsp,
  699. struct scsi_device **sdevp, int rescan,
  700. void *hostdata)
  701. {
  702. struct scsi_device *sdev;
  703. unsigned char *result;
  704. int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
  705. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  706. /*
  707. * The rescan flag is used as an optimization, the first scan of a
  708. * host adapter calls into here with rescan == 0.
  709. */
  710. sdev = scsi_device_lookup_by_target(starget, lun);
  711. if (sdev) {
  712. if (rescan || sdev->sdev_state != SDEV_CREATED) {
  713. SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
  714. "scsi scan: device exists on %s\n",
  715. sdev->sdev_gendev.bus_id));
  716. if (sdevp)
  717. *sdevp = sdev;
  718. else
  719. scsi_device_put(sdev);
  720. if (bflagsp)
  721. *bflagsp = scsi_get_device_flags(sdev,
  722. sdev->vendor,
  723. sdev->model);
  724. return SCSI_SCAN_LUN_PRESENT;
  725. }
  726. scsi_device_put(sdev);
  727. } else
  728. sdev = scsi_alloc_sdev(starget, lun, hostdata);
  729. if (!sdev)
  730. goto out;
  731. result = kmalloc(result_len, GFP_ATOMIC |
  732. ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
  733. if (!result)
  734. goto out_free_sdev;
  735. if (scsi_probe_lun(sdev, result, result_len, &bflags))
  736. goto out_free_result;
  737. /*
  738. * result contains valid SCSI INQUIRY data.
  739. */
  740. if ((result[0] >> 5) == 3) {
  741. /*
  742. * For a Peripheral qualifier 3 (011b), the SCSI
  743. * spec says: The device server is not capable of
  744. * supporting a physical device on this logical
  745. * unit.
  746. *
  747. * For disks, this implies that there is no
  748. * logical disk configured at sdev->lun, but there
  749. * is a target id responding.
  750. */
  751. SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
  752. "scsi scan: peripheral qualifier of 3,"
  753. " no device added\n"));
  754. res = SCSI_SCAN_TARGET_PRESENT;
  755. goto out_free_result;
  756. }
  757. res = scsi_add_lun(sdev, result, &bflags);
  758. if (res == SCSI_SCAN_LUN_PRESENT) {
  759. if (bflags & BLIST_KEY) {
  760. sdev->lockable = 0;
  761. scsi_unlock_floptical(sdev, result);
  762. }
  763. if (bflagsp)
  764. *bflagsp = bflags;
  765. }
  766. out_free_result:
  767. kfree(result);
  768. out_free_sdev:
  769. if (res == SCSI_SCAN_LUN_PRESENT) {
  770. if (sdevp) {
  771. if (scsi_device_get(sdev) == 0) {
  772. *sdevp = sdev;
  773. } else {
  774. __scsi_remove_device(sdev);
  775. res = SCSI_SCAN_NO_RESPONSE;
  776. }
  777. }
  778. } else
  779. scsi_destroy_sdev(sdev);
  780. out:
  781. return res;
  782. }
  783. /**
  784. * scsi_sequential_lun_scan - sequentially scan a SCSI target
  785. * @starget: pointer to target structure to scan
  786. * @bflags: black/white list flag for LUN 0
  787. * @lun0_res: result of scanning LUN 0
  788. *
  789. * Description:
  790. * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
  791. * scanned) to some maximum lun until a LUN is found with no device
  792. * attached. Use the bflags to figure out any oddities.
  793. *
  794. * Modifies sdevscan->lun.
  795. **/
  796. static void scsi_sequential_lun_scan(struct scsi_target *starget,
  797. int bflags, int lun0_res, int scsi_level,
  798. int rescan)
  799. {
  800. unsigned int sparse_lun, lun, max_dev_lun;
  801. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  802. SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
  803. "%s\n", starget->dev.bus_id));
  804. max_dev_lun = min(max_scsi_luns, shost->max_lun);
  805. /*
  806. * If this device is known to support sparse multiple units,
  807. * override the other settings, and scan all of them. Normally,
  808. * SCSI-3 devices should be scanned via the REPORT LUNS.
  809. */
  810. if (bflags & BLIST_SPARSELUN) {
  811. max_dev_lun = shost->max_lun;
  812. sparse_lun = 1;
  813. } else
  814. sparse_lun = 0;
  815. /*
  816. * If not sparse lun and no device attached at LUN 0 do not scan
  817. * any further.
  818. */
  819. if (!sparse_lun && (lun0_res != SCSI_SCAN_LUN_PRESENT))
  820. return;
  821. /*
  822. * If less than SCSI_1_CSS, and no special lun scaning, stop
  823. * scanning; this matches 2.4 behaviour, but could just be a bug
  824. * (to continue scanning a SCSI_1_CSS device).
  825. *
  826. * This test is broken. We might not have any device on lun0 for
  827. * a sparselun device, and if that's the case then how would we
  828. * know the real scsi_level, eh? It might make sense to just not
  829. * scan any SCSI_1 device for non-0 luns, but that check would best
  830. * go into scsi_alloc_sdev() and just have it return null when asked
  831. * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
  832. *
  833. if ((sdevscan->scsi_level < SCSI_1_CCS) &&
  834. ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
  835. == 0))
  836. return;
  837. */
  838. /*
  839. * If this device is known to support multiple units, override
  840. * the other settings, and scan all of them.
  841. */
  842. if (bflags & BLIST_FORCELUN)
  843. max_dev_lun = shost->max_lun;
  844. /*
  845. * REGAL CDC-4X: avoid hang after LUN 4
  846. */
  847. if (bflags & BLIST_MAX5LUN)
  848. max_dev_lun = min(5U, max_dev_lun);
  849. /*
  850. * Do not scan SCSI-2 or lower device past LUN 7, unless
  851. * BLIST_LARGELUN.
  852. */
  853. if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
  854. max_dev_lun = min(8U, max_dev_lun);
  855. /*
  856. * We have already scanned LUN 0, so start at LUN 1. Keep scanning
  857. * until we reach the max, or no LUN is found and we are not
  858. * sparse_lun.
  859. */
  860. for (lun = 1; lun < max_dev_lun; ++lun)
  861. if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
  862. NULL) != SCSI_SCAN_LUN_PRESENT) &&
  863. !sparse_lun)
  864. return;
  865. }
  866. /**
  867. * scsilun_to_int: convert a scsi_lun to an int
  868. * @scsilun: struct scsi_lun to be converted.
  869. *
  870. * Description:
  871. * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
  872. * integer, and return the result. The caller must check for
  873. * truncation before using this function.
  874. *
  875. * Notes:
  876. * The struct scsi_lun is assumed to be four levels, with each level
  877. * effectively containing a SCSI byte-ordered (big endian) short; the
  878. * addressing bits of each level are ignored (the highest two bits).
  879. * For a description of the LUN format, post SCSI-3 see the SCSI
  880. * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
  881. *
  882. * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
  883. * the integer: 0x0b030a04
  884. **/
  885. static int scsilun_to_int(struct scsi_lun *scsilun)
  886. {
  887. int i;
  888. unsigned int lun;
  889. lun = 0;
  890. for (i = 0; i < sizeof(lun); i += 2)
  891. lun = lun | (((scsilun->scsi_lun[i] << 8) |
  892. scsilun->scsi_lun[i + 1]) << (i * 8));
  893. return lun;
  894. }
  895. /**
  896. * int_to_scsilun: reverts an int into a scsi_lun
  897. * @int: integer to be reverted
  898. * @scsilun: struct scsi_lun to be set.
  899. *
  900. * Description:
  901. * Reverts the functionality of the scsilun_to_int, which packed
  902. * an 8-byte lun value into an int. This routine unpacks the int
  903. * back into the lun value.
  904. * Note: the scsilun_to_int() routine does not truly handle all
  905. * 8bytes of the lun value. This functions restores only as much
  906. * as was set by the routine.
  907. *
  908. * Notes:
  909. * Given an integer : 0x0b030a04, this function returns a
  910. * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
  911. *
  912. **/
  913. void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
  914. {
  915. int i;
  916. memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
  917. for (i = 0; i < sizeof(lun); i += 2) {
  918. scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
  919. scsilun->scsi_lun[i+1] = lun & 0xFF;
  920. lun = lun >> 16;
  921. }
  922. }
  923. EXPORT_SYMBOL(int_to_scsilun);
  924. /**
  925. * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
  926. * @sdevscan: scan the host, channel, and id of this Scsi_Device
  927. *
  928. * Description:
  929. * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
  930. * command, and scan the resulting list of LUNs by calling
  931. * scsi_probe_and_add_lun.
  932. *
  933. * Modifies sdevscan->lun.
  934. *
  935. * Return:
  936. * 0: scan completed (or no memory, so further scanning is futile)
  937. * 1: no report lun scan, or not configured
  938. **/
  939. static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
  940. int rescan)
  941. {
  942. char devname[64];
  943. unsigned char scsi_cmd[MAX_COMMAND_SIZE];
  944. unsigned int length;
  945. unsigned int lun;
  946. unsigned int num_luns;
  947. unsigned int retries;
  948. int result;
  949. struct scsi_lun *lunp, *lun_data;
  950. u8 *data;
  951. struct scsi_sense_hdr sshdr;
  952. struct scsi_device *sdev;
  953. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  954. /*
  955. * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
  956. * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
  957. * support more than 8 LUNs.
  958. */
  959. if ((bflags & BLIST_NOREPORTLUN) ||
  960. starget->scsi_level < SCSI_2 ||
  961. (starget->scsi_level < SCSI_3 &&
  962. (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) )
  963. return 1;
  964. if (bflags & BLIST_NOLUN)
  965. return 0;
  966. if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
  967. sdev = scsi_alloc_sdev(starget, 0, NULL);
  968. if (!sdev)
  969. return 0;
  970. if (scsi_device_get(sdev))
  971. return 0;
  972. }
  973. sprintf(devname, "host %d channel %d id %d",
  974. shost->host_no, sdev->channel, sdev->id);
  975. /*
  976. * Allocate enough to hold the header (the same size as one scsi_lun)
  977. * plus the max number of luns we are requesting.
  978. *
  979. * Reallocating and trying again (with the exact amount we need)
  980. * would be nice, but then we need to somehow limit the size
  981. * allocated based on the available memory and the limits of
  982. * kmalloc - we don't want a kmalloc() failure of a huge value to
  983. * prevent us from finding any LUNs on this target.
  984. */
  985. length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
  986. lun_data = kmalloc(length, GFP_ATOMIC |
  987. (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
  988. if (!lun_data) {
  989. printk(ALLOC_FAILURE_MSG, __FUNCTION__);
  990. goto out;
  991. }
  992. scsi_cmd[0] = REPORT_LUNS;
  993. /*
  994. * bytes 1 - 5: reserved, set to zero.
  995. */
  996. memset(&scsi_cmd[1], 0, 5);
  997. /*
  998. * bytes 6 - 9: length of the command.
  999. */
  1000. scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
  1001. scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
  1002. scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
  1003. scsi_cmd[9] = (unsigned char) length & 0xff;
  1004. scsi_cmd[10] = 0; /* reserved */
  1005. scsi_cmd[11] = 0; /* control */
  1006. /*
  1007. * We can get a UNIT ATTENTION, for example a power on/reset, so
  1008. * retry a few times (like sd.c does for TEST UNIT READY).
  1009. * Experience shows some combinations of adapter/devices get at
  1010. * least two power on/resets.
  1011. *
  1012. * Illegal requests (for devices that do not support REPORT LUNS)
  1013. * should come through as a check condition, and will not generate
  1014. * a retry.
  1015. */
  1016. for (retries = 0; retries < 3; retries++) {
  1017. SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
  1018. " REPORT LUNS to %s (try %d)\n", devname,
  1019. retries));
  1020. result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
  1021. lun_data, length, &sshdr,
  1022. SCSI_TIMEOUT + 4 * HZ, 3);
  1023. SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
  1024. " %s (try %d) result 0x%x\n", result
  1025. ? "failed" : "successful", retries, result));
  1026. if (result == 0)
  1027. break;
  1028. else if (scsi_sense_valid(&sshdr)) {
  1029. if (sshdr.sense_key != UNIT_ATTENTION)
  1030. break;
  1031. }
  1032. }
  1033. if (result) {
  1034. /*
  1035. * The device probably does not support a REPORT LUN command
  1036. */
  1037. kfree(lun_data);
  1038. return 1;
  1039. }
  1040. /*
  1041. * Get the length from the first four bytes of lun_data.
  1042. */
  1043. data = (u8 *) lun_data->scsi_lun;
  1044. length = ((data[0] << 24) | (data[1] << 16) |
  1045. (data[2] << 8) | (data[3] << 0));
  1046. num_luns = (length / sizeof(struct scsi_lun));
  1047. if (num_luns > max_scsi_report_luns) {
  1048. printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
  1049. " of %d luns reported, try increasing"
  1050. " max_scsi_report_luns.\n", devname,
  1051. max_scsi_report_luns, num_luns);
  1052. num_luns = max_scsi_report_luns;
  1053. }
  1054. SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUN scan of"
  1055. " host %d channel %d id %d\n", sdev->host->host_no,
  1056. sdev->channel, sdev->id));
  1057. /*
  1058. * Scan the luns in lun_data. The entry at offset 0 is really
  1059. * the header, so start at 1 and go up to and including num_luns.
  1060. */
  1061. for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
  1062. lun = scsilun_to_int(lunp);
  1063. /*
  1064. * Check if the unused part of lunp is non-zero, and so
  1065. * does not fit in lun.
  1066. */
  1067. if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
  1068. int i;
  1069. /*
  1070. * Output an error displaying the LUN in byte order,
  1071. * this differs from what linux would print for the
  1072. * integer LUN value.
  1073. */
  1074. printk(KERN_WARNING "scsi: %s lun 0x", devname);
  1075. data = (char *)lunp->scsi_lun;
  1076. for (i = 0; i < sizeof(struct scsi_lun); i++)
  1077. printk("%02x", data[i]);
  1078. printk(" has a LUN larger than currently supported.\n");
  1079. } else if (lun > sdev->host->max_lun) {
  1080. printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
  1081. " than allowed by the host adapter\n",
  1082. devname, lun);
  1083. } else {
  1084. int res;
  1085. res = scsi_probe_and_add_lun(starget,
  1086. lun, NULL, NULL, rescan, NULL);
  1087. if (res == SCSI_SCAN_NO_RESPONSE) {
  1088. /*
  1089. * Got some results, but now none, abort.
  1090. */
  1091. printk(KERN_ERR "scsi: Unexpected response"
  1092. " from %s lun %d while scanning, scan"
  1093. " aborted\n", devname, lun);
  1094. break;
  1095. }
  1096. }
  1097. }
  1098. kfree(lun_data);
  1099. out:
  1100. scsi_device_put(sdev);
  1101. if (sdev->sdev_state == SDEV_CREATED)
  1102. /*
  1103. * the sdev we used didn't appear in the report luns scan
  1104. */
  1105. scsi_destroy_sdev(sdev);
  1106. return 0;
  1107. }
  1108. struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
  1109. uint id, uint lun, void *hostdata)
  1110. {
  1111. struct scsi_device *sdev;
  1112. struct device *parent = &shost->shost_gendev;
  1113. int res;
  1114. struct scsi_target *starget = scsi_alloc_target(parent, channel, id);
  1115. if (!starget)
  1116. return ERR_PTR(-ENOMEM);
  1117. get_device(&starget->dev);
  1118. down(&shost->scan_mutex);
  1119. if (scsi_host_scan_allowed(shost)) {
  1120. res = scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1,
  1121. hostdata);
  1122. if (res != SCSI_SCAN_LUN_PRESENT)
  1123. sdev = ERR_PTR(-ENODEV);
  1124. }
  1125. up(&shost->scan_mutex);
  1126. scsi_target_reap(starget);
  1127. put_device(&starget->dev);
  1128. return sdev;
  1129. }
  1130. EXPORT_SYMBOL(__scsi_add_device);
  1131. int scsi_add_device(struct Scsi_Host *host, uint channel,
  1132. uint target, uint lun)
  1133. {
  1134. struct scsi_device *sdev =
  1135. __scsi_add_device(host, channel, target, lun, NULL);
  1136. if (IS_ERR(sdev))
  1137. return PTR_ERR(sdev);
  1138. scsi_device_put(sdev);
  1139. return 0;
  1140. }
  1141. EXPORT_SYMBOL(scsi_add_device);
  1142. void scsi_rescan_device(struct device *dev)
  1143. {
  1144. struct scsi_driver *drv;
  1145. if (!dev->driver)
  1146. return;
  1147. drv = to_scsi_driver(dev->driver);
  1148. if (try_module_get(drv->owner)) {
  1149. if (drv->rescan)
  1150. drv->rescan(dev);
  1151. module_put(drv->owner);
  1152. }
  1153. }
  1154. EXPORT_SYMBOL(scsi_rescan_device);
  1155. static void __scsi_scan_target(struct device *parent, unsigned int channel,
  1156. unsigned int id, unsigned int lun, int rescan)
  1157. {
  1158. struct Scsi_Host *shost = dev_to_shost(parent);
  1159. int bflags = 0;
  1160. int res;
  1161. struct scsi_target *starget;
  1162. if (shost->this_id == id)
  1163. /*
  1164. * Don't scan the host adapter
  1165. */
  1166. return;
  1167. starget = scsi_alloc_target(parent, channel, id);
  1168. if (!starget)
  1169. return;
  1170. get_device(&starget->dev);
  1171. if (lun != SCAN_WILD_CARD) {
  1172. /*
  1173. * Scan for a specific host/chan/id/lun.
  1174. */
  1175. scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
  1176. goto out_reap;
  1177. }
  1178. /*
  1179. * Scan LUN 0, if there is some response, scan further. Ideally, we
  1180. * would not configure LUN 0 until all LUNs are scanned.
  1181. */
  1182. res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
  1183. if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
  1184. if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
  1185. /*
  1186. * The REPORT LUN did not scan the target,
  1187. * do a sequential scan.
  1188. */
  1189. scsi_sequential_lun_scan(starget, bflags,
  1190. res, starget->scsi_level, rescan);
  1191. }
  1192. out_reap:
  1193. /* now determine if the target has any children at all
  1194. * and if not, nuke it */
  1195. scsi_target_reap(starget);
  1196. put_device(&starget->dev);
  1197. }
  1198. /**
  1199. * scsi_scan_target - scan a target id, possibly including all LUNs on the
  1200. * target.
  1201. * @parent: host to scan
  1202. * @channel: channel to scan
  1203. * @id: target id to scan
  1204. * @lun: Specific LUN to scan or SCAN_WILD_CARD
  1205. * @rescan: passed to LUN scanning routines
  1206. *
  1207. * Description:
  1208. * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
  1209. * and possibly all LUNs on the target id.
  1210. *
  1211. * First try a REPORT LUN scan, if that does not scan the target, do a
  1212. * sequential scan of LUNs on the target id.
  1213. **/
  1214. void scsi_scan_target(struct device *parent, unsigned int channel,
  1215. unsigned int id, unsigned int lun, int rescan)
  1216. {
  1217. struct Scsi_Host *shost = dev_to_shost(parent);
  1218. down(&shost->scan_mutex);
  1219. if (scsi_host_scan_allowed(shost))
  1220. __scsi_scan_target(parent, channel, id, lun, rescan);
  1221. up(&shost->scan_mutex);
  1222. }
  1223. EXPORT_SYMBOL(scsi_scan_target);
  1224. static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
  1225. unsigned int id, unsigned int lun, int rescan)
  1226. {
  1227. uint order_id;
  1228. if (id == SCAN_WILD_CARD)
  1229. for (id = 0; id < shost->max_id; ++id) {
  1230. /*
  1231. * XXX adapter drivers when possible (FCP, iSCSI)
  1232. * could modify max_id to match the current max,
  1233. * not the absolute max.
  1234. *
  1235. * XXX add a shost id iterator, so for example,
  1236. * the FC ID can be the same as a target id
  1237. * without a huge overhead of sparse id's.
  1238. */
  1239. if (shost->reverse_ordering)
  1240. /*
  1241. * Scan from high to low id.
  1242. */
  1243. order_id = shost->max_id - id - 1;
  1244. else
  1245. order_id = id;
  1246. __scsi_scan_target(&shost->shost_gendev, channel,
  1247. order_id, lun, rescan);
  1248. }
  1249. else
  1250. __scsi_scan_target(&shost->shost_gendev, channel,
  1251. id, lun, rescan);
  1252. }
  1253. int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
  1254. unsigned int id, unsigned int lun, int rescan)
  1255. {
  1256. SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "%s: <%u:%u:%u:%u>\n",
  1257. __FUNCTION__, shost->host_no, channel, id, lun));
  1258. if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
  1259. ((id != SCAN_WILD_CARD) && (id > shost->max_id)) ||
  1260. ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
  1261. return -EINVAL;
  1262. down(&shost->scan_mutex);
  1263. if (scsi_host_scan_allowed(shost)) {
  1264. if (channel == SCAN_WILD_CARD)
  1265. for (channel = 0; channel <= shost->max_channel;
  1266. channel++)
  1267. scsi_scan_channel(shost, channel, id, lun,
  1268. rescan);
  1269. else
  1270. scsi_scan_channel(shost, channel, id, lun, rescan);
  1271. }
  1272. up(&shost->scan_mutex);
  1273. return 0;
  1274. }
  1275. /**
  1276. * scsi_scan_host - scan the given adapter
  1277. * @shost: adapter to scan
  1278. **/
  1279. void scsi_scan_host(struct Scsi_Host *shost)
  1280. {
  1281. scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
  1282. SCAN_WILD_CARD, 0);
  1283. }
  1284. EXPORT_SYMBOL(scsi_scan_host);
  1285. /**
  1286. * scsi_scan_single_target - scan the given SCSI target
  1287. * @shost: adapter to scan
  1288. * @chan: channel to scan
  1289. * @id: target id to scan
  1290. **/
  1291. void scsi_scan_single_target(struct Scsi_Host *shost,
  1292. unsigned int chan, unsigned int id)
  1293. {
  1294. scsi_scan_host_selected(shost, chan, id, SCAN_WILD_CARD, 1);
  1295. }
  1296. EXPORT_SYMBOL(scsi_scan_single_target);
  1297. void scsi_forget_host(struct Scsi_Host *shost)
  1298. {
  1299. struct scsi_device *sdev;
  1300. unsigned long flags;
  1301. restart:
  1302. spin_lock_irqsave(shost->host_lock, flags);
  1303. list_for_each_entry(sdev, &shost->__devices, siblings) {
  1304. if (sdev->sdev_state == SDEV_DEL)
  1305. continue;
  1306. spin_unlock_irqrestore(shost->host_lock, flags);
  1307. __scsi_remove_device(sdev);
  1308. goto restart;
  1309. }
  1310. spin_unlock_irqrestore(shost->host_lock, flags);
  1311. }
  1312. /*
  1313. * Function: scsi_get_host_dev()
  1314. *
  1315. * Purpose: Create a Scsi_Device that points to the host adapter itself.
  1316. *
  1317. * Arguments: SHpnt - Host that needs a Scsi_Device
  1318. *
  1319. * Lock status: None assumed.
  1320. *
  1321. * Returns: The Scsi_Device or NULL
  1322. *
  1323. * Notes:
  1324. * Attach a single Scsi_Device to the Scsi_Host - this should
  1325. * be made to look like a "pseudo-device" that points to the
  1326. * HA itself.
  1327. *
  1328. * Note - this device is not accessible from any high-level
  1329. * drivers (including generics), which is probably not
  1330. * optimal. We can add hooks later to attach
  1331. */
  1332. struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
  1333. {
  1334. struct scsi_device *sdev = NULL;
  1335. struct scsi_target *starget;
  1336. down(&shost->scan_mutex);
  1337. if (!scsi_host_scan_allowed(shost))
  1338. goto out;
  1339. starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
  1340. if (!starget)
  1341. goto out;
  1342. sdev = scsi_alloc_sdev(starget, 0, NULL);
  1343. if (sdev) {
  1344. sdev->sdev_gendev.parent = get_device(&starget->dev);
  1345. sdev->borken = 0;
  1346. }
  1347. put_device(&starget->dev);
  1348. out:
  1349. up(&shost->scan_mutex);
  1350. return sdev;
  1351. }
  1352. EXPORT_SYMBOL(scsi_get_host_dev);
  1353. /*
  1354. * Function: scsi_free_host_dev()
  1355. *
  1356. * Purpose: Free a scsi_device that points to the host adapter itself.
  1357. *
  1358. * Arguments: SHpnt - Host that needs a Scsi_Device
  1359. *
  1360. * Lock status: None assumed.
  1361. *
  1362. * Returns: Nothing
  1363. *
  1364. * Notes:
  1365. */
  1366. void scsi_free_host_dev(struct scsi_device *sdev)
  1367. {
  1368. BUG_ON(sdev->id != sdev->host->this_id);
  1369. scsi_destroy_sdev(sdev);
  1370. }
  1371. EXPORT_SYMBOL(scsi_free_host_dev);