scsi_scan.c 51 KB

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