scsi_scan.c 56 KB

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