scsi_scan.c 46 KB

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