scsi_scan.c 60 KB

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