scsi_scan.c 46 KB

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