scsi_scan.c 43 KB

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