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