scsi_scan.c 54 KB

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