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