scsi_scan.c 44 KB

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