libata-scsi.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. /*
  2. libata-scsi.c - helper library for ATA
  3. Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  4. Copyright 2003-2004 Jeff Garzik
  5. The contents of this file are subject to the Open
  6. Software License version 1.1 that can be found at
  7. http://www.opensource.org/licenses/osl-1.1.txt and is included herein
  8. by reference.
  9. Alternatively, the contents of this file may be used under the terms
  10. of the GNU General Public License version 2 (the "GPL") as distributed
  11. in the kernel source COPYING file, in which case the provisions of
  12. the GPL are applicable instead of the above. If you wish to allow
  13. the use of your version of this file only under the terms of the
  14. GPL and not to allow others to use your version of this file under
  15. the OSL, indicate your decision by deleting the provisions above and
  16. replace them with the notice and other provisions required by the GPL.
  17. If you do not delete the provisions above, a recipient may use your
  18. version of this file under either the OSL or the GPL.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/spinlock.h>
  23. #include <scsi/scsi.h>
  24. #include "scsi.h"
  25. #include <scsi/scsi_host.h>
  26. #include <linux/libata.h>
  27. #include <asm/uaccess.h>
  28. #include "libata.h"
  29. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
  30. static struct ata_device *
  31. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
  32. /**
  33. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  34. * @sdev: SCSI device for which BIOS geometry is to be determined
  35. * @bdev: block device associated with @sdev
  36. * @capacity: capacity of SCSI device
  37. * @geom: location to which geometry will be output
  38. *
  39. * Generic bios head/sector/cylinder calculator
  40. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  41. * mapping. Some situations may arise where the disk is not
  42. * bootable if this is not used.
  43. *
  44. * LOCKING:
  45. * Defined by the SCSI layer. We don't really care.
  46. *
  47. * RETURNS:
  48. * Zero.
  49. */
  50. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  51. sector_t capacity, int geom[])
  52. {
  53. geom[0] = 255;
  54. geom[1] = 63;
  55. sector_div(capacity, 255*63);
  56. geom[2] = capacity;
  57. return 0;
  58. }
  59. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  60. {
  61. struct ata_port *ap;
  62. struct ata_device *dev;
  63. int val = -EINVAL, rc = -EINVAL;
  64. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  65. if (!ap)
  66. goto out;
  67. dev = ata_scsi_find_dev(ap, scsidev);
  68. if (!dev) {
  69. rc = -ENODEV;
  70. goto out;
  71. }
  72. switch (cmd) {
  73. case ATA_IOC_GET_IO32:
  74. val = 0;
  75. if (copy_to_user(arg, &val, 1))
  76. return -EFAULT;
  77. return 0;
  78. case ATA_IOC_SET_IO32:
  79. val = (unsigned long) arg;
  80. if (val != 0)
  81. return -EINVAL;
  82. return 0;
  83. default:
  84. rc = -ENOTTY;
  85. break;
  86. }
  87. out:
  88. return rc;
  89. }
  90. /**
  91. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  92. * @ap: ATA port to which the new command is attached
  93. * @dev: ATA device to which the new command is attached
  94. * @cmd: SCSI command that originated this ATA command
  95. * @done: SCSI command completion function
  96. *
  97. * Obtain a reference to an unused ata_queued_cmd structure,
  98. * which is the basic libata structure representing a single
  99. * ATA command sent to the hardware.
  100. *
  101. * If a command was available, fill in the SCSI-specific
  102. * portions of the structure with information on the
  103. * current command.
  104. *
  105. * LOCKING:
  106. * spin_lock_irqsave(host_set lock)
  107. *
  108. * RETURNS:
  109. * Command allocated, or %NULL if none available.
  110. */
  111. struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
  112. struct ata_device *dev,
  113. struct scsi_cmnd *cmd,
  114. void (*done)(struct scsi_cmnd *))
  115. {
  116. struct ata_queued_cmd *qc;
  117. qc = ata_qc_new_init(ap, dev);
  118. if (qc) {
  119. qc->scsicmd = cmd;
  120. qc->scsidone = done;
  121. if (cmd->use_sg) {
  122. qc->sg = (struct scatterlist *) cmd->request_buffer;
  123. qc->n_elem = cmd->use_sg;
  124. } else {
  125. qc->sg = &qc->sgent;
  126. qc->n_elem = 1;
  127. }
  128. } else {
  129. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  130. done(cmd);
  131. }
  132. return qc;
  133. }
  134. /**
  135. * ata_to_sense_error - convert ATA error to SCSI error
  136. * @qc: Command that we are erroring out
  137. * @drv_stat: value contained in ATA status register
  138. *
  139. * Converts an ATA error into a SCSI error. While we are at it
  140. * we decode and dump the ATA error for the user so that they
  141. * have some idea what really happened at the non make-believe
  142. * layer.
  143. *
  144. * LOCKING:
  145. * spin_lock_irqsave(host_set lock)
  146. */
  147. void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
  148. {
  149. struct scsi_cmnd *cmd = qc->scsicmd;
  150. u8 err = 0;
  151. unsigned char *sb = cmd->sense_buffer;
  152. /* Based on the 3ware driver translation table */
  153. static unsigned char sense_table[][4] = {
  154. /* BBD|ECC|ID|MAR */
  155. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  156. /* BBD|ECC|ID */
  157. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  158. /* ECC|MC|MARK */
  159. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  160. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  161. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  162. /* MC|ID|ABRT|TRK0|MARK */
  163. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  164. /* MCR|MARK */
  165. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  166. /* Bad address mark */
  167. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  168. /* TRK0 */
  169. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  170. /* Abort & !ICRC */
  171. {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
  172. /* Media change request */
  173. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  174. /* SRV */
  175. {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
  176. /* Media change */
  177. {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
  178. /* ECC */
  179. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  180. /* BBD - block marked bad */
  181. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  182. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  183. };
  184. static unsigned char stat_table[][4] = {
  185. /* Must be first because BUSY means no other bits valid */
  186. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  187. {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
  188. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  189. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  190. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  191. };
  192. int i = 0;
  193. cmd->result = SAM_STAT_CHECK_CONDITION;
  194. /*
  195. * Is this an error we can process/parse
  196. */
  197. if(drv_stat & ATA_ERR)
  198. /* Read the err bits */
  199. err = ata_chk_err(qc->ap);
  200. /* Display the ATA level error info */
  201. printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat);
  202. if(drv_stat & 0x80)
  203. {
  204. printk("Busy ");
  205. err = 0; /* Data is not valid in this case */
  206. }
  207. else {
  208. if(drv_stat & 0x40) printk("DriveReady ");
  209. if(drv_stat & 0x20) printk("DeviceFault ");
  210. if(drv_stat & 0x10) printk("SeekComplete ");
  211. if(drv_stat & 0x08) printk("DataRequest ");
  212. if(drv_stat & 0x04) printk("CorrectedError ");
  213. if(drv_stat & 0x02) printk("Index ");
  214. if(drv_stat & 0x01) printk("Error ");
  215. }
  216. printk("}\n");
  217. if(err)
  218. {
  219. printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err);
  220. if(err & 0x04) printk("DriveStatusError ");
  221. if(err & 0x80)
  222. {
  223. if(err & 0x04)
  224. printk("BadCRC ");
  225. else
  226. printk("Sector ");
  227. }
  228. if(err & 0x40) printk("UncorrectableError ");
  229. if(err & 0x10) printk("SectorIdNotFound ");
  230. if(err & 0x02) printk("TrackZeroNotFound ");
  231. if(err & 0x01) printk("AddrMarkNotFound ");
  232. printk("}\n");
  233. /* Should we dump sector info here too ?? */
  234. }
  235. /* Look for err */
  236. while(sense_table[i][0] != 0xFF)
  237. {
  238. /* Look for best matches first */
  239. if((sense_table[i][0] & err) == sense_table[i][0])
  240. {
  241. sb[0] = 0x70;
  242. sb[2] = sense_table[i][1];
  243. sb[7] = 0x0a;
  244. sb[12] = sense_table[i][2];
  245. sb[13] = sense_table[i][3];
  246. return;
  247. }
  248. i++;
  249. }
  250. /* No immediate match */
  251. if(err)
  252. printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err);
  253. i = 0;
  254. /* Fall back to interpreting status bits */
  255. while(stat_table[i][0] != 0xFF)
  256. {
  257. if(stat_table[i][0] & drv_stat)
  258. {
  259. sb[0] = 0x70;
  260. sb[2] = stat_table[i][1];
  261. sb[7] = 0x0a;
  262. sb[12] = stat_table[i][2];
  263. sb[13] = stat_table[i][3];
  264. return;
  265. }
  266. i++;
  267. }
  268. /* No error ?? */
  269. printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
  270. /* additional-sense-code[-qualifier] */
  271. sb[0] = 0x70;
  272. sb[2] = MEDIUM_ERROR;
  273. sb[7] = 0x0A;
  274. if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  275. sb[12] = 0x11; /* "unrecovered read error" */
  276. sb[13] = 0x04;
  277. } else {
  278. sb[12] = 0x0C; /* "write error - */
  279. sb[13] = 0x02; /* auto-reallocation failed" */
  280. }
  281. }
  282. /**
  283. * ata_scsi_slave_config - Set SCSI device attributes
  284. * @sdev: SCSI device to examine
  285. *
  286. * This is called before we actually start reading
  287. * and writing to the device, to configure certain
  288. * SCSI mid-layer behaviors.
  289. *
  290. * LOCKING:
  291. * Defined by SCSI layer. We don't really care.
  292. */
  293. int ata_scsi_slave_config(struct scsi_device *sdev)
  294. {
  295. sdev->use_10_for_rw = 1;
  296. sdev->use_10_for_ms = 1;
  297. blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
  298. if (sdev->id < ATA_MAX_DEVICES) {
  299. struct ata_port *ap;
  300. struct ata_device *dev;
  301. ap = (struct ata_port *) &sdev->host->hostdata[0];
  302. dev = &ap->device[sdev->id];
  303. /* TODO: 1024 is an arbitrary number, not the
  304. * hardware maximum. This should be increased to
  305. * 65534 when Jens Axboe's patch for dynamically
  306. * determining max_sectors is merged.
  307. */
  308. if ((dev->flags & ATA_DFLAG_LBA48) &&
  309. ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
  310. sdev->host->max_sectors = 2048;
  311. blk_queue_max_sectors(sdev->request_queue, 2048);
  312. }
  313. }
  314. return 0; /* scsi layer doesn't check return value, sigh */
  315. }
  316. /**
  317. * ata_scsi_error - SCSI layer error handler callback
  318. * @host: SCSI host on which error occurred
  319. *
  320. * Handles SCSI-layer-thrown error events.
  321. *
  322. * LOCKING:
  323. * Inherited from SCSI layer (none, can sleep)
  324. *
  325. * RETURNS:
  326. * Zero.
  327. */
  328. int ata_scsi_error(struct Scsi_Host *host)
  329. {
  330. struct ata_port *ap;
  331. DPRINTK("ENTER\n");
  332. ap = (struct ata_port *) &host->hostdata[0];
  333. ap->ops->eng_timeout(ap);
  334. /* TODO: this is per-command; when queueing is supported
  335. * this code will either change or move to a more
  336. * appropriate place
  337. */
  338. host->host_failed--;
  339. DPRINTK("EXIT\n");
  340. return 0;
  341. }
  342. /**
  343. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  344. * @qc: Storage for translated ATA taskfile
  345. * @scsicmd: SCSI command to translate (ignored)
  346. *
  347. * Sets up an ATA taskfile to issue FLUSH CACHE or
  348. * FLUSH CACHE EXT.
  349. *
  350. * LOCKING:
  351. * spin_lock_irqsave(host_set lock)
  352. *
  353. * RETURNS:
  354. * Zero on success, non-zero on error.
  355. */
  356. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  357. {
  358. struct ata_taskfile *tf = &qc->tf;
  359. tf->flags |= ATA_TFLAG_DEVICE;
  360. tf->protocol = ATA_PROT_NODATA;
  361. if ((tf->flags & ATA_TFLAG_LBA48) &&
  362. (ata_id_has_flush_ext(qc->dev->id)))
  363. tf->command = ATA_CMD_FLUSH_EXT;
  364. else
  365. tf->command = ATA_CMD_FLUSH;
  366. return 0;
  367. }
  368. /**
  369. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  370. * @qc: Storage for translated ATA taskfile
  371. * @scsicmd: SCSI command to translate
  372. *
  373. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  374. *
  375. * LOCKING:
  376. * spin_lock_irqsave(host_set lock)
  377. *
  378. * RETURNS:
  379. * Zero on success, non-zero on error.
  380. */
  381. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  382. {
  383. struct ata_taskfile *tf = &qc->tf;
  384. struct ata_device *dev = qc->dev;
  385. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  386. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  387. u64 dev_sectors = qc->dev->n_sectors;
  388. u64 block = 0;
  389. u32 n_block = 0;
  390. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  391. tf->protocol = ATA_PROT_NODATA;
  392. if (scsicmd[0] == VERIFY) {
  393. block |= ((u64)scsicmd[2]) << 24;
  394. block |= ((u64)scsicmd[3]) << 16;
  395. block |= ((u64)scsicmd[4]) << 8;
  396. block |= ((u64)scsicmd[5]);
  397. n_block |= ((u32)scsicmd[7]) << 8;
  398. n_block |= ((u32)scsicmd[8]);
  399. }
  400. else if (scsicmd[0] == VERIFY_16) {
  401. block |= ((u64)scsicmd[2]) << 56;
  402. block |= ((u64)scsicmd[3]) << 48;
  403. block |= ((u64)scsicmd[4]) << 40;
  404. block |= ((u64)scsicmd[5]) << 32;
  405. block |= ((u64)scsicmd[6]) << 24;
  406. block |= ((u64)scsicmd[7]) << 16;
  407. block |= ((u64)scsicmd[8]) << 8;
  408. block |= ((u64)scsicmd[9]);
  409. n_block |= ((u32)scsicmd[10]) << 24;
  410. n_block |= ((u32)scsicmd[11]) << 16;
  411. n_block |= ((u32)scsicmd[12]) << 8;
  412. n_block |= ((u32)scsicmd[13]);
  413. }
  414. else
  415. return 1;
  416. if (!n_block)
  417. return 1;
  418. if (block >= dev_sectors)
  419. return 1;
  420. if ((block + n_block) > dev_sectors)
  421. return 1;
  422. if (lba48) {
  423. if (n_block > (64 * 1024))
  424. return 1;
  425. } else {
  426. if (n_block > 256)
  427. return 1;
  428. }
  429. if (lba) {
  430. if (lba48) {
  431. tf->command = ATA_CMD_VERIFY_EXT;
  432. tf->hob_nsect = (n_block >> 8) & 0xff;
  433. tf->hob_lbah = (block >> 40) & 0xff;
  434. tf->hob_lbam = (block >> 32) & 0xff;
  435. tf->hob_lbal = (block >> 24) & 0xff;
  436. } else {
  437. tf->command = ATA_CMD_VERIFY;
  438. tf->device |= (block >> 24) & 0xf;
  439. }
  440. tf->nsect = n_block & 0xff;
  441. tf->lbah = (block >> 16) & 0xff;
  442. tf->lbam = (block >> 8) & 0xff;
  443. tf->lbal = block & 0xff;
  444. tf->device |= ATA_LBA;
  445. } else {
  446. /* CHS */
  447. u32 sect, head, cyl, track;
  448. /* Convert LBA to CHS */
  449. track = (u32)block / dev->sectors;
  450. cyl = track / dev->heads;
  451. head = track % dev->heads;
  452. sect = (u32)block % dev->sectors + 1;
  453. DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n", (u32)block, track, cyl, head, sect);
  454. /* Check whether the converted CHS can fit.
  455. Cylinder: 0-65535
  456. Head: 0-15
  457. Sector: 1-255*/
  458. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  459. return 1;
  460. tf->command = ATA_CMD_VERIFY;
  461. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  462. tf->lbal = sect;
  463. tf->lbam = cyl;
  464. tf->lbah = cyl >> 8;
  465. tf->device |= head;
  466. }
  467. return 0;
  468. }
  469. /**
  470. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  471. * @qc: Storage for translated ATA taskfile
  472. * @scsicmd: SCSI command to translate
  473. *
  474. * Converts any of six SCSI read/write commands into the
  475. * ATA counterpart, including starting sector (LBA),
  476. * sector count, and taking into account the device's LBA48
  477. * support.
  478. *
  479. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  480. * %WRITE_16 are currently supported.
  481. *
  482. * LOCKING:
  483. * spin_lock_irqsave(host_set lock)
  484. *
  485. * RETURNS:
  486. * Zero on success, non-zero on error.
  487. */
  488. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  489. {
  490. struct ata_taskfile *tf = &qc->tf;
  491. struct ata_device *dev = qc->dev;
  492. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  493. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  494. u64 block = 0;
  495. u32 n_block = 0;
  496. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  497. tf->protocol = qc->dev->xfer_protocol;
  498. if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
  499. scsicmd[0] == READ_16) {
  500. tf->command = qc->dev->read_cmd;
  501. } else {
  502. tf->command = qc->dev->write_cmd;
  503. tf->flags |= ATA_TFLAG_WRITE;
  504. }
  505. /* Calculate the SCSI LBA and transfer length. */
  506. if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
  507. block |= ((u64)scsicmd[2]) << 24;
  508. block |= ((u64)scsicmd[3]) << 16;
  509. block |= ((u64)scsicmd[4]) << 8;
  510. block |= ((u64)scsicmd[5]);
  511. n_block |= ((u32)scsicmd[7]) << 8;
  512. n_block |= ((u32)scsicmd[8]);
  513. VPRINTK("ten-byte command\n");
  514. } else if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
  515. block |= ((u64)scsicmd[2]) << 8;
  516. block |= ((u64)scsicmd[3]);
  517. n_block |= ((u32)scsicmd[4]);
  518. VPRINTK("six-byte command\n");
  519. } else if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
  520. block |= ((u64)scsicmd[2]) << 56;
  521. block |= ((u64)scsicmd[3]) << 48;
  522. block |= ((u64)scsicmd[4]) << 40;
  523. block |= ((u64)scsicmd[5]) << 32;
  524. block |= ((u64)scsicmd[6]) << 24;
  525. block |= ((u64)scsicmd[7]) << 16;
  526. block |= ((u64)scsicmd[8]) << 8;
  527. block |= ((u64)scsicmd[9]);
  528. n_block |= ((u32)scsicmd[10]) << 24;
  529. n_block |= ((u32)scsicmd[11]) << 16;
  530. n_block |= ((u32)scsicmd[12]) << 8;
  531. n_block |= ((u32)scsicmd[13]);
  532. VPRINTK("sixteen-byte command\n");
  533. } else {
  534. DPRINTK("no-byte command\n");
  535. return 1;
  536. }
  537. /* Check and compose ATA command */
  538. if (!n_block)
  539. /* In ATA, sector count 0 means 256 or 65536 sectors, not 0 sectors. */
  540. return 1;
  541. if (lba) {
  542. if (lba48) {
  543. /* The request -may- be too large for LBA48. */
  544. if ((block >> 48) || (n_block > 65536))
  545. return 1;
  546. tf->hob_nsect = (n_block >> 8) & 0xff;
  547. tf->hob_lbah = (block >> 40) & 0xff;
  548. tf->hob_lbam = (block >> 32) & 0xff;
  549. tf->hob_lbal = (block >> 24) & 0xff;
  550. } else {
  551. /* LBA28 */
  552. /* The request -may- be too large for LBA28. */
  553. if ((block >> 28) || (n_block > 256))
  554. return 1;
  555. tf->device |= (block >> 24) & 0xf;
  556. }
  557. qc->nsect = n_block;
  558. tf->nsect = n_block & 0xff;
  559. tf->lbah = (block >> 16) & 0xff;
  560. tf->lbam = (block >> 8) & 0xff;
  561. tf->lbal = block & 0xff;
  562. tf->device |= ATA_LBA;
  563. } else {
  564. /* CHS */
  565. u32 sect, head, cyl, track;
  566. /* The request -may- be too large for CHS addressing. */
  567. if ((block >> 28) || (n_block > 256))
  568. return 1;
  569. /* Convert LBA to CHS */
  570. track = (u32)block / dev->sectors;
  571. cyl = track / dev->heads;
  572. head = track % dev->heads;
  573. sect = (u32)block % dev->sectors + 1;
  574. DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n",
  575. (u32)block, track, cyl, head, sect);
  576. /* Check whether the converted CHS can fit.
  577. Cylinder: 0-65535
  578. Head: 0-15
  579. Sector: 1-255*/
  580. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  581. return 1;
  582. qc->nsect = n_block;
  583. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  584. tf->lbal = sect;
  585. tf->lbam = cyl;
  586. tf->lbah = cyl >> 8;
  587. tf->device |= head;
  588. }
  589. return 0;
  590. }
  591. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  592. {
  593. struct scsi_cmnd *cmd = qc->scsicmd;
  594. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
  595. ata_to_sense_error(qc, drv_stat);
  596. else
  597. cmd->result = SAM_STAT_GOOD;
  598. qc->scsidone(cmd);
  599. return 0;
  600. }
  601. /**
  602. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  603. * @ap: ATA port to which the command is addressed
  604. * @dev: ATA device to which the command is addressed
  605. * @cmd: SCSI command to execute
  606. * @done: SCSI command completion function
  607. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  608. *
  609. * Our ->queuecommand() function has decided that the SCSI
  610. * command issued can be directly translated into an ATA
  611. * command, rather than handled internally.
  612. *
  613. * This function sets up an ata_queued_cmd structure for the
  614. * SCSI command, and sends that ata_queued_cmd to the hardware.
  615. *
  616. * LOCKING:
  617. * spin_lock_irqsave(host_set lock)
  618. */
  619. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  620. struct scsi_cmnd *cmd,
  621. void (*done)(struct scsi_cmnd *),
  622. ata_xlat_func_t xlat_func)
  623. {
  624. struct ata_queued_cmd *qc;
  625. u8 *scsicmd = cmd->cmnd;
  626. VPRINTK("ENTER\n");
  627. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  628. if (!qc)
  629. return;
  630. /* data is present; dma-map it */
  631. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  632. cmd->sc_data_direction == DMA_TO_DEVICE) {
  633. if (unlikely(cmd->request_bufflen < 1)) {
  634. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  635. ap->id, dev->devno);
  636. goto err_out;
  637. }
  638. if (cmd->use_sg)
  639. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  640. else
  641. ata_sg_init_one(qc, cmd->request_buffer,
  642. cmd->request_bufflen);
  643. qc->dma_dir = cmd->sc_data_direction;
  644. }
  645. qc->complete_fn = ata_scsi_qc_complete;
  646. if (xlat_func(qc, scsicmd))
  647. goto err_out;
  648. /* select device, send command to hardware */
  649. if (ata_qc_issue(qc))
  650. goto err_out;
  651. VPRINTK("EXIT\n");
  652. return;
  653. err_out:
  654. ata_qc_free(qc);
  655. ata_bad_cdb(cmd, done);
  656. DPRINTK("EXIT - badcmd\n");
  657. }
  658. /**
  659. * ata_scsi_rbuf_get - Map response buffer.
  660. * @cmd: SCSI command containing buffer to be mapped.
  661. * @buf_out: Pointer to mapped area.
  662. *
  663. * Maps buffer contained within SCSI command @cmd.
  664. *
  665. * LOCKING:
  666. * spin_lock_irqsave(host_set lock)
  667. *
  668. * RETURNS:
  669. * Length of response buffer.
  670. */
  671. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  672. {
  673. u8 *buf;
  674. unsigned int buflen;
  675. if (cmd->use_sg) {
  676. struct scatterlist *sg;
  677. sg = (struct scatterlist *) cmd->request_buffer;
  678. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  679. buflen = sg->length;
  680. } else {
  681. buf = cmd->request_buffer;
  682. buflen = cmd->request_bufflen;
  683. }
  684. *buf_out = buf;
  685. return buflen;
  686. }
  687. /**
  688. * ata_scsi_rbuf_put - Unmap response buffer.
  689. * @cmd: SCSI command containing buffer to be unmapped.
  690. * @buf: buffer to unmap
  691. *
  692. * Unmaps response buffer contained within @cmd.
  693. *
  694. * LOCKING:
  695. * spin_lock_irqsave(host_set lock)
  696. */
  697. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  698. {
  699. if (cmd->use_sg) {
  700. struct scatterlist *sg;
  701. sg = (struct scatterlist *) cmd->request_buffer;
  702. kunmap_atomic(buf - sg->offset, KM_USER0);
  703. }
  704. }
  705. /**
  706. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  707. * @args: device IDENTIFY data / SCSI command of interest.
  708. * @actor: Callback hook for desired SCSI command simulator
  709. *
  710. * Takes care of the hard work of simulating a SCSI command...
  711. * Mapping the response buffer, calling the command's handler,
  712. * and handling the handler's return value. This return value
  713. * indicates whether the handler wishes the SCSI command to be
  714. * completed successfully, or not.
  715. *
  716. * LOCKING:
  717. * spin_lock_irqsave(host_set lock)
  718. */
  719. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  720. unsigned int (*actor) (struct ata_scsi_args *args,
  721. u8 *rbuf, unsigned int buflen))
  722. {
  723. u8 *rbuf;
  724. unsigned int buflen, rc;
  725. struct scsi_cmnd *cmd = args->cmd;
  726. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  727. memset(rbuf, 0, buflen);
  728. rc = actor(args, rbuf, buflen);
  729. ata_scsi_rbuf_put(cmd, rbuf);
  730. if (rc)
  731. ata_bad_cdb(cmd, args->done);
  732. else {
  733. cmd->result = SAM_STAT_GOOD;
  734. args->done(cmd);
  735. }
  736. }
  737. /**
  738. * ata_scsiop_inq_std - Simulate INQUIRY command
  739. * @args: device IDENTIFY data / SCSI command of interest.
  740. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  741. * @buflen: Response buffer length.
  742. *
  743. * Returns standard device identification data associated
  744. * with non-EVPD INQUIRY command output.
  745. *
  746. * LOCKING:
  747. * spin_lock_irqsave(host_set lock)
  748. */
  749. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  750. unsigned int buflen)
  751. {
  752. u8 hdr[] = {
  753. TYPE_DISK,
  754. 0,
  755. 0x5, /* claim SPC-3 version compatibility */
  756. 2,
  757. 95 - 4
  758. };
  759. /* set scsi removeable (RMB) bit per ata bit */
  760. if (ata_id_removeable(args->id))
  761. hdr[1] |= (1 << 7);
  762. VPRINTK("ENTER\n");
  763. memcpy(rbuf, hdr, sizeof(hdr));
  764. if (buflen > 35) {
  765. memcpy(&rbuf[8], "ATA ", 8);
  766. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  767. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  768. if (rbuf[32] == 0 || rbuf[32] == ' ')
  769. memcpy(&rbuf[32], "n/a ", 4);
  770. }
  771. if (buflen > 63) {
  772. const u8 versions[] = {
  773. 0x60, /* SAM-3 (no version claimed) */
  774. 0x03,
  775. 0x20, /* SBC-2 (no version claimed) */
  776. 0x02,
  777. 0x60 /* SPC-3 (no version claimed) */
  778. };
  779. memcpy(rbuf + 59, versions, sizeof(versions));
  780. }
  781. return 0;
  782. }
  783. /**
  784. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  785. * @args: device IDENTIFY data / SCSI command of interest.
  786. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  787. * @buflen: Response buffer length.
  788. *
  789. * Returns list of inquiry EVPD pages available.
  790. *
  791. * LOCKING:
  792. * spin_lock_irqsave(host_set lock)
  793. */
  794. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  795. unsigned int buflen)
  796. {
  797. const u8 pages[] = {
  798. 0x00, /* page 0x00, this page */
  799. 0x80, /* page 0x80, unit serial no page */
  800. 0x83 /* page 0x83, device ident page */
  801. };
  802. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  803. if (buflen > 6)
  804. memcpy(rbuf + 4, pages, sizeof(pages));
  805. return 0;
  806. }
  807. /**
  808. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  809. * @args: device IDENTIFY data / SCSI command of interest.
  810. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  811. * @buflen: Response buffer length.
  812. *
  813. * Returns ATA device serial number.
  814. *
  815. * LOCKING:
  816. * spin_lock_irqsave(host_set lock)
  817. */
  818. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  819. unsigned int buflen)
  820. {
  821. const u8 hdr[] = {
  822. 0,
  823. 0x80, /* this page code */
  824. 0,
  825. ATA_SERNO_LEN, /* page len */
  826. };
  827. memcpy(rbuf, hdr, sizeof(hdr));
  828. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  829. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  830. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  831. return 0;
  832. }
  833. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  834. /**
  835. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  836. * @args: device IDENTIFY data / SCSI command of interest.
  837. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  838. * @buflen: Response buffer length.
  839. *
  840. * Returns device identification. Currently hardcoded to
  841. * return "Linux ATA-SCSI simulator".
  842. *
  843. * LOCKING:
  844. * spin_lock_irqsave(host_set lock)
  845. */
  846. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  847. unsigned int buflen)
  848. {
  849. rbuf[1] = 0x83; /* this page code */
  850. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  851. /* our one and only identification descriptor (vendor-specific) */
  852. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  853. rbuf[4 + 0] = 2; /* code set: ASCII */
  854. rbuf[4 + 3] = strlen(inq_83_str);
  855. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  856. }
  857. return 0;
  858. }
  859. /**
  860. * ata_scsiop_noop -
  861. * @args: device IDENTIFY data / SCSI command of interest.
  862. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  863. * @buflen: Response buffer length.
  864. *
  865. * No operation. Simply returns success to caller, to indicate
  866. * that the caller should successfully complete this SCSI command.
  867. *
  868. * LOCKING:
  869. * spin_lock_irqsave(host_set lock)
  870. */
  871. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  872. unsigned int buflen)
  873. {
  874. VPRINTK("ENTER\n");
  875. return 0;
  876. }
  877. /**
  878. * ata_msense_push - Push data onto MODE SENSE data output buffer
  879. * @ptr_io: (input/output) Location to store more output data
  880. * @last: End of output data buffer
  881. * @buf: Pointer to BLOB being added to output buffer
  882. * @buflen: Length of BLOB
  883. *
  884. * Store MODE SENSE data on an output buffer.
  885. *
  886. * LOCKING:
  887. * None.
  888. */
  889. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  890. const u8 *buf, unsigned int buflen)
  891. {
  892. u8 *ptr = *ptr_io;
  893. if ((ptr + buflen - 1) > last)
  894. return;
  895. memcpy(ptr, buf, buflen);
  896. ptr += buflen;
  897. *ptr_io = ptr;
  898. }
  899. /**
  900. * ata_msense_caching - Simulate MODE SENSE caching info page
  901. * @id: device IDENTIFY data
  902. * @ptr_io: (input/output) Location to store more output data
  903. * @last: End of output data buffer
  904. *
  905. * Generate a caching info page, which conditionally indicates
  906. * write caching to the SCSI layer, depending on device
  907. * capabilities.
  908. *
  909. * LOCKING:
  910. * None.
  911. */
  912. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  913. const u8 *last)
  914. {
  915. u8 page[] = {
  916. 0x8, /* page code */
  917. 0x12, /* page length */
  918. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
  919. 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
  920. };
  921. if (ata_id_wcache_enabled(id))
  922. page[2] |= (1 << 2); /* write cache enable */
  923. if (!ata_id_rahead_enabled(id))
  924. page[12] |= (1 << 5); /* disable read ahead */
  925. ata_msense_push(ptr_io, last, page, sizeof(page));
  926. return sizeof(page);
  927. }
  928. /**
  929. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  930. * @dev: Device associated with this MODE SENSE command
  931. * @ptr_io: (input/output) Location to store more output data
  932. * @last: End of output data buffer
  933. *
  934. * Generate a generic MODE SENSE control mode page.
  935. *
  936. * LOCKING:
  937. * None.
  938. */
  939. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  940. {
  941. const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
  942. /* byte 2: set the descriptor format sense data bit (bit 2)
  943. * since we need to support returning this format for SAT
  944. * commands and any SCSI commands against a 48b LBA device.
  945. */
  946. ata_msense_push(ptr_io, last, page, sizeof(page));
  947. return sizeof(page);
  948. }
  949. /**
  950. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  951. * @dev: Device associated with this MODE SENSE command
  952. * @ptr_io: (input/output) Location to store more output data
  953. * @last: End of output data buffer
  954. *
  955. * Generate a generic MODE SENSE r/w error recovery page.
  956. *
  957. * LOCKING:
  958. * None.
  959. */
  960. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  961. {
  962. const u8 page[] = {
  963. 0x1, /* page code */
  964. 0xa, /* page length */
  965. (1 << 7) | (1 << 6), /* note auto r/w reallocation */
  966. 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
  967. };
  968. ata_msense_push(ptr_io, last, page, sizeof(page));
  969. return sizeof(page);
  970. }
  971. /**
  972. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  973. * @args: device IDENTIFY data / SCSI command of interest.
  974. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  975. * @buflen: Response buffer length.
  976. *
  977. * Simulate MODE SENSE commands.
  978. *
  979. * LOCKING:
  980. * spin_lock_irqsave(host_set lock)
  981. */
  982. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  983. unsigned int buflen)
  984. {
  985. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  986. unsigned int page_control, six_byte, output_len;
  987. VPRINTK("ENTER\n");
  988. six_byte = (scsicmd[0] == MODE_SENSE);
  989. /* we only support saved and current values (which we treat
  990. * in the same manner)
  991. */
  992. page_control = scsicmd[2] >> 6;
  993. if ((page_control != 0) && (page_control != 3))
  994. return 1;
  995. if (six_byte)
  996. output_len = 4;
  997. else
  998. output_len = 8;
  999. p = rbuf + output_len;
  1000. last = rbuf + buflen - 1;
  1001. switch(scsicmd[2] & 0x3f) {
  1002. case 0x01: /* r/w error recovery */
  1003. output_len += ata_msense_rw_recovery(&p, last);
  1004. break;
  1005. case 0x08: /* caching */
  1006. output_len += ata_msense_caching(args->id, &p, last);
  1007. break;
  1008. case 0x0a: { /* control mode */
  1009. output_len += ata_msense_ctl_mode(&p, last);
  1010. break;
  1011. }
  1012. case 0x3f: /* all pages */
  1013. output_len += ata_msense_rw_recovery(&p, last);
  1014. output_len += ata_msense_caching(args->id, &p, last);
  1015. output_len += ata_msense_ctl_mode(&p, last);
  1016. break;
  1017. default: /* invalid page code */
  1018. return 1;
  1019. }
  1020. if (six_byte) {
  1021. output_len--;
  1022. rbuf[0] = output_len;
  1023. } else {
  1024. output_len -= 2;
  1025. rbuf[0] = output_len >> 8;
  1026. rbuf[1] = output_len;
  1027. }
  1028. return 0;
  1029. }
  1030. /**
  1031. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1032. * @args: device IDENTIFY data / SCSI command of interest.
  1033. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1034. * @buflen: Response buffer length.
  1035. *
  1036. * Simulate READ CAPACITY commands.
  1037. *
  1038. * LOCKING:
  1039. * spin_lock_irqsave(host_set lock)
  1040. */
  1041. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1042. unsigned int buflen)
  1043. {
  1044. u64 n_sectors;
  1045. u32 tmp;
  1046. VPRINTK("ENTER\n");
  1047. if (ata_id_has_lba(args->id)) {
  1048. if (ata_id_has_lba48(args->id))
  1049. n_sectors = ata_id_u64(args->id, 100);
  1050. else
  1051. n_sectors = ata_id_u32(args->id, 60);
  1052. } else {
  1053. /* CHS default translation */
  1054. n_sectors = args->id[1] * args->id[3] * args->id[6];
  1055. if (ata_id_current_chs_valid(args->id))
  1056. /* CHS current translation */
  1057. n_sectors = ata_id_u32(args->id, 57);
  1058. }
  1059. n_sectors--; /* ATA TotalUserSectors - 1 */
  1060. tmp = n_sectors; /* note: truncates, if lba48 */
  1061. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1062. /* sector count, 32-bit */
  1063. rbuf[0] = tmp >> (8 * 3);
  1064. rbuf[1] = tmp >> (8 * 2);
  1065. rbuf[2] = tmp >> (8 * 1);
  1066. rbuf[3] = tmp;
  1067. /* sector size */
  1068. tmp = ATA_SECT_SIZE;
  1069. rbuf[6] = tmp >> 8;
  1070. rbuf[7] = tmp;
  1071. } else {
  1072. /* sector count, 64-bit */
  1073. rbuf[2] = n_sectors >> (8 * 7);
  1074. rbuf[3] = n_sectors >> (8 * 6);
  1075. rbuf[4] = n_sectors >> (8 * 5);
  1076. rbuf[5] = n_sectors >> (8 * 4);
  1077. rbuf[6] = tmp >> (8 * 3);
  1078. rbuf[7] = tmp >> (8 * 2);
  1079. rbuf[8] = tmp >> (8 * 1);
  1080. rbuf[9] = tmp;
  1081. /* sector size */
  1082. tmp = ATA_SECT_SIZE;
  1083. rbuf[12] = tmp >> 8;
  1084. rbuf[13] = tmp;
  1085. }
  1086. return 0;
  1087. }
  1088. /**
  1089. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1090. * @args: device IDENTIFY data / SCSI command of interest.
  1091. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1092. * @buflen: Response buffer length.
  1093. *
  1094. * Simulate REPORT LUNS command.
  1095. *
  1096. * LOCKING:
  1097. * spin_lock_irqsave(host_set lock)
  1098. */
  1099. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1100. unsigned int buflen)
  1101. {
  1102. VPRINTK("ENTER\n");
  1103. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1104. return 0;
  1105. }
  1106. /**
  1107. * ata_scsi_badcmd - End a SCSI request with an error
  1108. * @cmd: SCSI request to be handled
  1109. * @done: SCSI command completion function
  1110. * @asc: SCSI-defined additional sense code
  1111. * @ascq: SCSI-defined additional sense code qualifier
  1112. *
  1113. * Helper function that completes a SCSI command with
  1114. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1115. * and the specified additional sense codes.
  1116. *
  1117. * LOCKING:
  1118. * spin_lock_irqsave(host_set lock)
  1119. */
  1120. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1121. {
  1122. DPRINTK("ENTER\n");
  1123. cmd->result = SAM_STAT_CHECK_CONDITION;
  1124. cmd->sense_buffer[0] = 0x70;
  1125. cmd->sense_buffer[2] = ILLEGAL_REQUEST;
  1126. cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
  1127. cmd->sense_buffer[12] = asc;
  1128. cmd->sense_buffer[13] = ascq;
  1129. done(cmd);
  1130. }
  1131. static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  1132. {
  1133. struct scsi_cmnd *cmd = qc->scsicmd;
  1134. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
  1135. DPRINTK("request check condition\n");
  1136. cmd->result = SAM_STAT_CHECK_CONDITION;
  1137. qc->scsidone(cmd);
  1138. return 1;
  1139. } else {
  1140. u8 *scsicmd = cmd->cmnd;
  1141. if (scsicmd[0] == INQUIRY) {
  1142. u8 *buf = NULL;
  1143. unsigned int buflen;
  1144. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1145. buf[2] = 0x5;
  1146. buf[3] = (buf[3] & 0xf0) | 2;
  1147. ata_scsi_rbuf_put(cmd, buf);
  1148. }
  1149. cmd->result = SAM_STAT_GOOD;
  1150. }
  1151. qc->scsidone(cmd);
  1152. return 0;
  1153. }
  1154. /**
  1155. * atapi_xlat - Initialize PACKET taskfile
  1156. * @qc: command structure to be initialized
  1157. * @scsicmd: SCSI CDB associated with this PACKET command
  1158. *
  1159. * LOCKING:
  1160. * spin_lock_irqsave(host_set lock)
  1161. *
  1162. * RETURNS:
  1163. * Zero on success, non-zero on failure.
  1164. */
  1165. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  1166. {
  1167. struct scsi_cmnd *cmd = qc->scsicmd;
  1168. struct ata_device *dev = qc->dev;
  1169. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1170. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1171. if (!using_pio)
  1172. /* Check whether ATAPI DMA is safe */
  1173. if (ata_check_atapi_dma(qc))
  1174. using_pio = 1;
  1175. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1176. qc->complete_fn = atapi_qc_complete;
  1177. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1178. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1179. qc->tf.flags |= ATA_TFLAG_WRITE;
  1180. DPRINTK("direction: write\n");
  1181. }
  1182. qc->tf.command = ATA_CMD_PACKET;
  1183. /* no data, or PIO data xfer */
  1184. if (using_pio || nodata) {
  1185. if (nodata)
  1186. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1187. else
  1188. qc->tf.protocol = ATA_PROT_ATAPI;
  1189. qc->tf.lbam = (8 * 1024) & 0xff;
  1190. qc->tf.lbah = (8 * 1024) >> 8;
  1191. }
  1192. /* DMA data xfer */
  1193. else {
  1194. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1195. qc->tf.feature |= ATAPI_PKT_DMA;
  1196. #ifdef ATAPI_ENABLE_DMADIR
  1197. /* some SATA bridges need us to indicate data xfer direction */
  1198. if (cmd->sc_data_direction != DMA_TO_DEVICE)
  1199. qc->tf.feature |= ATAPI_DMADIR;
  1200. #endif
  1201. }
  1202. qc->nbytes = cmd->bufflen;
  1203. return 0;
  1204. }
  1205. /**
  1206. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1207. * @ap: ATA port to which the device is attached
  1208. * @scsidev: SCSI device from which we derive the ATA device
  1209. *
  1210. * Given various information provided in struct scsi_cmnd,
  1211. * map that onto an ATA bus, and using that mapping
  1212. * determine which ata_device is associated with the
  1213. * SCSI command to be sent.
  1214. *
  1215. * LOCKING:
  1216. * spin_lock_irqsave(host_set lock)
  1217. *
  1218. * RETURNS:
  1219. * Associated ATA device, or %NULL if not found.
  1220. */
  1221. static struct ata_device *
  1222. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
  1223. {
  1224. struct ata_device *dev;
  1225. /* skip commands not addressed to targets we simulate */
  1226. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1227. dev = &ap->device[scsidev->id];
  1228. else
  1229. return NULL;
  1230. if (unlikely((scsidev->channel != 0) ||
  1231. (scsidev->lun != 0)))
  1232. return NULL;
  1233. if (unlikely(!ata_dev_present(dev)))
  1234. return NULL;
  1235. #ifndef ATA_ENABLE_ATAPI
  1236. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1237. return NULL;
  1238. #endif
  1239. return dev;
  1240. }
  1241. /**
  1242. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1243. * @dev: ATA device
  1244. * @cmd: SCSI command opcode to consider
  1245. *
  1246. * Look up the SCSI command given, and determine whether the
  1247. * SCSI command is to be translated or simulated.
  1248. *
  1249. * RETURNS:
  1250. * Pointer to translation function if possible, %NULL if not.
  1251. */
  1252. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  1253. {
  1254. switch (cmd) {
  1255. case READ_6:
  1256. case READ_10:
  1257. case READ_16:
  1258. case WRITE_6:
  1259. case WRITE_10:
  1260. case WRITE_16:
  1261. return ata_scsi_rw_xlat;
  1262. case SYNCHRONIZE_CACHE:
  1263. if (ata_try_flush_cache(dev))
  1264. return ata_scsi_flush_xlat;
  1265. break;
  1266. case VERIFY:
  1267. case VERIFY_16:
  1268. return ata_scsi_verify_xlat;
  1269. }
  1270. return NULL;
  1271. }
  1272. /**
  1273. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  1274. * @ap: ATA port to which the command was being sent
  1275. * @cmd: SCSI command to dump
  1276. *
  1277. * Prints the contents of a SCSI command via printk().
  1278. */
  1279. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  1280. struct scsi_cmnd *cmd)
  1281. {
  1282. #ifdef ATA_DEBUG
  1283. struct scsi_device *scsidev = cmd->device;
  1284. u8 *scsicmd = cmd->cmnd;
  1285. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1286. ap->id,
  1287. scsidev->channel, scsidev->id, scsidev->lun,
  1288. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  1289. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  1290. scsicmd[8]);
  1291. #endif
  1292. }
  1293. /**
  1294. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  1295. * @cmd: SCSI command to be sent
  1296. * @done: Completion function, called when command is complete
  1297. *
  1298. * In some cases, this function translates SCSI commands into
  1299. * ATA taskfiles, and queues the taskfiles to be sent to
  1300. * hardware. In other cases, this function simulates a
  1301. * SCSI device by evaluating and responding to certain
  1302. * SCSI commands. This creates the overall effect of
  1303. * ATA and ATAPI devices appearing as SCSI devices.
  1304. *
  1305. * LOCKING:
  1306. * Releases scsi-layer-held lock, and obtains host_set lock.
  1307. *
  1308. * RETURNS:
  1309. * Zero.
  1310. */
  1311. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  1312. {
  1313. struct ata_port *ap;
  1314. struct ata_device *dev;
  1315. struct scsi_device *scsidev = cmd->device;
  1316. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  1317. ata_scsi_dump_cdb(ap, cmd);
  1318. dev = ata_scsi_find_dev(ap, scsidev);
  1319. if (unlikely(!dev)) {
  1320. cmd->result = (DID_BAD_TARGET << 16);
  1321. done(cmd);
  1322. goto out_unlock;
  1323. }
  1324. if (dev->class == ATA_DEV_ATA) {
  1325. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  1326. cmd->cmnd[0]);
  1327. if (xlat_func)
  1328. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  1329. else
  1330. ata_scsi_simulate(dev->id, cmd, done);
  1331. } else
  1332. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  1333. out_unlock:
  1334. return 0;
  1335. }
  1336. /**
  1337. * ata_scsi_simulate - simulate SCSI command on ATA device
  1338. * @id: current IDENTIFY data for target device.
  1339. * @cmd: SCSI command being sent to device.
  1340. * @done: SCSI command completion function.
  1341. *
  1342. * Interprets and directly executes a select list of SCSI commands
  1343. * that can be handled internally.
  1344. *
  1345. * LOCKING:
  1346. * spin_lock_irqsave(host_set lock)
  1347. */
  1348. void ata_scsi_simulate(u16 *id,
  1349. struct scsi_cmnd *cmd,
  1350. void (*done)(struct scsi_cmnd *))
  1351. {
  1352. struct ata_scsi_args args;
  1353. u8 *scsicmd = cmd->cmnd;
  1354. args.id = id;
  1355. args.cmd = cmd;
  1356. args.done = done;
  1357. switch(scsicmd[0]) {
  1358. /* no-op's, complete with success */
  1359. case SYNCHRONIZE_CACHE:
  1360. case REZERO_UNIT:
  1361. case SEEK_6:
  1362. case SEEK_10:
  1363. case TEST_UNIT_READY:
  1364. case FORMAT_UNIT: /* FIXME: correct? */
  1365. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  1366. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  1367. break;
  1368. case INQUIRY:
  1369. if (scsicmd[1] & 2) /* is CmdDt set? */
  1370. ata_bad_cdb(cmd, done);
  1371. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  1372. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  1373. else if (scsicmd[2] == 0x00)
  1374. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  1375. else if (scsicmd[2] == 0x80)
  1376. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  1377. else if (scsicmd[2] == 0x83)
  1378. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  1379. else
  1380. ata_bad_cdb(cmd, done);
  1381. break;
  1382. case MODE_SENSE:
  1383. case MODE_SENSE_10:
  1384. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  1385. break;
  1386. case MODE_SELECT: /* unconditionally return */
  1387. case MODE_SELECT_10: /* bad-field-in-cdb */
  1388. ata_bad_cdb(cmd, done);
  1389. break;
  1390. case READ_CAPACITY:
  1391. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1392. break;
  1393. case SERVICE_ACTION_IN:
  1394. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  1395. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1396. else
  1397. ata_bad_cdb(cmd, done);
  1398. break;
  1399. case REPORT_LUNS:
  1400. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  1401. break;
  1402. /* mandantory commands we haven't implemented yet */
  1403. case REQUEST_SENSE:
  1404. /* all other commands */
  1405. default:
  1406. ata_bad_scsiop(cmd, done);
  1407. break;
  1408. }
  1409. }