scsi_scan.c 58 KB

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