scsi_scan.c 56 KB

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