scsi_scan.c 44 KB

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