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