scsi_scan.c 53 KB

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