scsi_scan.c 55 KB

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