scsi_scan.c 54 KB

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