scsi_scan.c 43 KB

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