scsi_scan.c 43 KB

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