scsi_scan.c 53 KB

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