scsi_scan.c 54 KB

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