scsi_scan.c 54 KB

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