scsi_scan.c 43 KB

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