scsi_scan.c 54 KB

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