scsi_scan.c 42 KB

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