scsi_scan.c 44 KB

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