scsi_scan.c 43 KB

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