scsi_scan.c 54 KB

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