scsi_scan.c 52 KB

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