scsi_scan.c 43 KB

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