scsi_scan.c 46 KB

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