scsi_scan.c 43 KB

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