scsi_scan.c 52 KB

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