scsi_scan.c 54 KB

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