scsi_scan.c 42 KB

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