scsi_scan.c 55 KB

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