scsi_scan.c 55 KB

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