scsi_scan.c 53 KB

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