scsi_scan.c 55 KB

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