scsi_scan.c 45 KB

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