libata-scsi.c 49 KB

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