scsi_scan.c 43 KB

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