scsi_scan.c 44 KB

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