scsi_scan.c 55 KB

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