scsi_scan.c 56 KB

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