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/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. #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);