libata-scsi.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  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. * SATA DMA transfers must be multiples of 4 byte, so
  331. * we need to pad ATAPI transfers using an extra sg.
  332. * Decrement max hw segments accordingly.
  333. */
  334. if (dev->class == ATA_DEV_ATAPI) {
  335. request_queue_t *q = sdev->request_queue;
  336. blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
  337. }
  338. }
  339. return 0; /* scsi layer doesn't check return value, sigh */
  340. }
  341. /**
  342. * ata_scsi_error - SCSI layer error handler callback
  343. * @host: SCSI host on which error occurred
  344. *
  345. * Handles SCSI-layer-thrown error events.
  346. *
  347. * LOCKING:
  348. * Inherited from SCSI layer (none, can sleep)
  349. *
  350. * RETURNS:
  351. * Zero.
  352. */
  353. int ata_scsi_error(struct Scsi_Host *host)
  354. {
  355. struct ata_port *ap;
  356. DPRINTK("ENTER\n");
  357. ap = (struct ata_port *) &host->hostdata[0];
  358. ap->ops->eng_timeout(ap);
  359. /* TODO: this is per-command; when queueing is supported
  360. * this code will either change or move to a more
  361. * appropriate place
  362. */
  363. host->host_failed--;
  364. INIT_LIST_HEAD(&host->eh_cmd_q);
  365. DPRINTK("EXIT\n");
  366. return 0;
  367. }
  368. /**
  369. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  370. * @qc: Storage for translated ATA taskfile
  371. * @scsicmd: SCSI command to translate
  372. *
  373. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  374. * (to start). Perhaps these commands should be preceded by
  375. * CHECK POWER MODE to see what power mode the device is already in.
  376. * [See SAT revision 5 at www.t10.org]
  377. *
  378. * LOCKING:
  379. * spin_lock_irqsave(host_set lock)
  380. *
  381. * RETURNS:
  382. * Zero on success, non-zero on error.
  383. */
  384. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
  385. u8 *scsicmd)
  386. {
  387. struct ata_taskfile *tf = &qc->tf;
  388. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  389. tf->protocol = ATA_PROT_NODATA;
  390. if (scsicmd[1] & 0x1) {
  391. ; /* ignore IMMED bit, violates sat-r05 */
  392. }
  393. if (scsicmd[4] & 0x2)
  394. goto invalid_fld; /* LOEJ bit set not supported */
  395. if (((scsicmd[4] >> 4) & 0xf) != 0)
  396. goto invalid_fld; /* power conditions not supported */
  397. if (scsicmd[4] & 0x1) {
  398. tf->nsect = 1; /* 1 sector, lba=0 */
  399. if (qc->dev->flags & ATA_DFLAG_LBA) {
  400. qc->tf.flags |= ATA_TFLAG_LBA;
  401. tf->lbah = 0x0;
  402. tf->lbam = 0x0;
  403. tf->lbal = 0x0;
  404. tf->device |= ATA_LBA;
  405. } else {
  406. /* CHS */
  407. tf->lbal = 0x1; /* sect */
  408. tf->lbam = 0x0; /* cyl low */
  409. tf->lbah = 0x0; /* cyl high */
  410. }
  411. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  412. } else {
  413. tf->nsect = 0; /* time period value (0 implies now) */
  414. tf->command = ATA_CMD_STANDBY;
  415. /* Consider: ATA STANDBY IMMEDIATE command */
  416. }
  417. /*
  418. * Standby and Idle condition timers could be implemented but that
  419. * would require libata to implement the Power condition mode page
  420. * and allow the user to change it. Changing mode pages requires
  421. * MODE SELECT to be implemented.
  422. */
  423. return 0;
  424. invalid_fld:
  425. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  426. /* "Invalid field in cbd" */
  427. return 1;
  428. }
  429. /**
  430. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  431. * @qc: Storage for translated ATA taskfile
  432. * @scsicmd: SCSI command to translate (ignored)
  433. *
  434. * Sets up an ATA taskfile to issue FLUSH CACHE or
  435. * FLUSH CACHE EXT.
  436. *
  437. * LOCKING:
  438. * spin_lock_irqsave(host_set lock)
  439. *
  440. * RETURNS:
  441. * Zero on success, non-zero on error.
  442. */
  443. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  444. {
  445. struct ata_taskfile *tf = &qc->tf;
  446. tf->flags |= ATA_TFLAG_DEVICE;
  447. tf->protocol = ATA_PROT_NODATA;
  448. if ((tf->flags & ATA_TFLAG_LBA48) &&
  449. (ata_id_has_flush_ext(qc->dev->id)))
  450. tf->command = ATA_CMD_FLUSH_EXT;
  451. else
  452. tf->command = ATA_CMD_FLUSH;
  453. return 0;
  454. }
  455. /**
  456. * scsi_6_lba_len - Get LBA and transfer length
  457. * @scsicmd: SCSI command to translate
  458. *
  459. * Calculate LBA and transfer length for 6-byte commands.
  460. *
  461. * RETURNS:
  462. * @plba: the LBA
  463. * @plen: the transfer length
  464. */
  465. static void scsi_6_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
  466. {
  467. u64 lba = 0;
  468. u32 len = 0;
  469. VPRINTK("six-byte command\n");
  470. lba |= ((u64)scsicmd[2]) << 8;
  471. lba |= ((u64)scsicmd[3]);
  472. len |= ((u32)scsicmd[4]);
  473. *plba = lba;
  474. *plen = len;
  475. }
  476. /**
  477. * scsi_10_lba_len - Get LBA and transfer length
  478. * @scsicmd: SCSI command to translate
  479. *
  480. * Calculate LBA and transfer length for 10-byte commands.
  481. *
  482. * RETURNS:
  483. * @plba: the LBA
  484. * @plen: the transfer length
  485. */
  486. static void scsi_10_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
  487. {
  488. u64 lba = 0;
  489. u32 len = 0;
  490. VPRINTK("ten-byte command\n");
  491. lba |= ((u64)scsicmd[2]) << 24;
  492. lba |= ((u64)scsicmd[3]) << 16;
  493. lba |= ((u64)scsicmd[4]) << 8;
  494. lba |= ((u64)scsicmd[5]);
  495. len |= ((u32)scsicmd[7]) << 8;
  496. len |= ((u32)scsicmd[8]);
  497. *plba = lba;
  498. *plen = len;
  499. }
  500. /**
  501. * scsi_16_lba_len - Get LBA and transfer length
  502. * @scsicmd: SCSI command to translate
  503. *
  504. * Calculate LBA and transfer length for 16-byte commands.
  505. *
  506. * RETURNS:
  507. * @plba: the LBA
  508. * @plen: the transfer length
  509. */
  510. static void scsi_16_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
  511. {
  512. u64 lba = 0;
  513. u32 len = 0;
  514. VPRINTK("sixteen-byte command\n");
  515. lba |= ((u64)scsicmd[2]) << 56;
  516. lba |= ((u64)scsicmd[3]) << 48;
  517. lba |= ((u64)scsicmd[4]) << 40;
  518. lba |= ((u64)scsicmd[5]) << 32;
  519. lba |= ((u64)scsicmd[6]) << 24;
  520. lba |= ((u64)scsicmd[7]) << 16;
  521. lba |= ((u64)scsicmd[8]) << 8;
  522. lba |= ((u64)scsicmd[9]);
  523. len |= ((u32)scsicmd[10]) << 24;
  524. len |= ((u32)scsicmd[11]) << 16;
  525. len |= ((u32)scsicmd[12]) << 8;
  526. len |= ((u32)scsicmd[13]);
  527. *plba = lba;
  528. *plen = len;
  529. }
  530. /**
  531. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  532. * @qc: Storage for translated ATA taskfile
  533. * @scsicmd: SCSI command to translate
  534. *
  535. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  536. *
  537. * LOCKING:
  538. * spin_lock_irqsave(host_set lock)
  539. *
  540. * RETURNS:
  541. * Zero on success, non-zero on error.
  542. */
  543. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  544. {
  545. struct ata_taskfile *tf = &qc->tf;
  546. struct ata_device *dev = qc->dev;
  547. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  548. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  549. u64 dev_sectors = qc->dev->n_sectors;
  550. u64 block;
  551. u32 n_block;
  552. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  553. tf->protocol = ATA_PROT_NODATA;
  554. if (scsicmd[0] == VERIFY)
  555. scsi_10_lba_len(scsicmd, &block, &n_block);
  556. else if (scsicmd[0] == VERIFY_16)
  557. scsi_16_lba_len(scsicmd, &block, &n_block);
  558. else
  559. goto invalid_fld;
  560. if (!n_block)
  561. goto nothing_to_do;
  562. if (block >= dev_sectors)
  563. goto out_of_range;
  564. if ((block + n_block) > dev_sectors)
  565. goto out_of_range;
  566. if (lba48) {
  567. if (n_block > (64 * 1024))
  568. goto invalid_fld;
  569. } else {
  570. if (n_block > 256)
  571. goto invalid_fld;
  572. }
  573. if (lba) {
  574. if (lba48) {
  575. tf->command = ATA_CMD_VERIFY_EXT;
  576. tf->hob_nsect = (n_block >> 8) & 0xff;
  577. tf->hob_lbah = (block >> 40) & 0xff;
  578. tf->hob_lbam = (block >> 32) & 0xff;
  579. tf->hob_lbal = (block >> 24) & 0xff;
  580. } else {
  581. tf->command = ATA_CMD_VERIFY;
  582. tf->device |= (block >> 24) & 0xf;
  583. }
  584. tf->nsect = n_block & 0xff;
  585. tf->lbah = (block >> 16) & 0xff;
  586. tf->lbam = (block >> 8) & 0xff;
  587. tf->lbal = block & 0xff;
  588. tf->device |= ATA_LBA;
  589. } else {
  590. /* CHS */
  591. u32 sect, head, cyl, track;
  592. /* Convert LBA to CHS */
  593. track = (u32)block / dev->sectors;
  594. cyl = track / dev->heads;
  595. head = track % dev->heads;
  596. sect = (u32)block % dev->sectors + 1;
  597. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  598. (u32)block, track, cyl, head, sect);
  599. /* Check whether the converted CHS can fit.
  600. Cylinder: 0-65535
  601. Head: 0-15
  602. Sector: 1-255*/
  603. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  604. goto out_of_range;
  605. tf->command = ATA_CMD_VERIFY;
  606. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  607. tf->lbal = sect;
  608. tf->lbam = cyl;
  609. tf->lbah = cyl >> 8;
  610. tf->device |= head;
  611. }
  612. return 0;
  613. invalid_fld:
  614. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  615. /* "Invalid field in cbd" */
  616. return 1;
  617. out_of_range:
  618. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  619. /* "Logical Block Address out of range" */
  620. return 1;
  621. nothing_to_do:
  622. qc->scsicmd->result = SAM_STAT_GOOD;
  623. return 1;
  624. }
  625. /**
  626. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  627. * @qc: Storage for translated ATA taskfile
  628. * @scsicmd: SCSI command to translate
  629. *
  630. * Converts any of six SCSI read/write commands into the
  631. * ATA counterpart, including starting sector (LBA),
  632. * sector count, and taking into account the device's LBA48
  633. * support.
  634. *
  635. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  636. * %WRITE_16 are currently supported.
  637. *
  638. * LOCKING:
  639. * spin_lock_irqsave(host_set lock)
  640. *
  641. * RETURNS:
  642. * Zero on success, non-zero on error.
  643. */
  644. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  645. {
  646. struct ata_taskfile *tf = &qc->tf;
  647. struct ata_device *dev = qc->dev;
  648. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  649. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  650. u64 block;
  651. u32 n_block;
  652. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  653. tf->protocol = qc->dev->xfer_protocol;
  654. if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
  655. scsicmd[0] == READ_16) {
  656. tf->command = qc->dev->read_cmd;
  657. } else {
  658. tf->command = qc->dev->write_cmd;
  659. tf->flags |= ATA_TFLAG_WRITE;
  660. }
  661. /* Calculate the SCSI LBA and transfer length. */
  662. switch (scsicmd[0]) {
  663. case READ_10:
  664. case WRITE_10:
  665. scsi_10_lba_len(scsicmd, &block, &n_block);
  666. break;
  667. case READ_6:
  668. case WRITE_6:
  669. scsi_6_lba_len(scsicmd, &block, &n_block);
  670. /* for 6-byte r/w commands, transfer length 0
  671. * means 256 blocks of data, not 0 block.
  672. */
  673. if (!n_block)
  674. n_block = 256;
  675. break;
  676. case READ_16:
  677. case WRITE_16:
  678. scsi_16_lba_len(scsicmd, &block, &n_block);
  679. break;
  680. default:
  681. DPRINTK("no-byte command\n");
  682. goto invalid_fld;
  683. }
  684. /* Check and compose ATA command */
  685. if (!n_block)
  686. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  687. * length 0 means transfer 0 block of data.
  688. * However, for ATA R/W commands, sector count 0 means
  689. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  690. */
  691. goto nothing_to_do;
  692. if (lba) {
  693. if (lba48) {
  694. /* The request -may- be too large for LBA48. */
  695. if ((block >> 48) || (n_block > 65536))
  696. goto out_of_range;
  697. tf->hob_nsect = (n_block >> 8) & 0xff;
  698. tf->hob_lbah = (block >> 40) & 0xff;
  699. tf->hob_lbam = (block >> 32) & 0xff;
  700. tf->hob_lbal = (block >> 24) & 0xff;
  701. } else {
  702. /* LBA28 */
  703. /* The request -may- be too large for LBA28. */
  704. if ((block >> 28) || (n_block > 256))
  705. goto out_of_range;
  706. tf->device |= (block >> 24) & 0xf;
  707. }
  708. qc->nsect = n_block;
  709. tf->nsect = n_block & 0xff;
  710. tf->lbah = (block >> 16) & 0xff;
  711. tf->lbam = (block >> 8) & 0xff;
  712. tf->lbal = block & 0xff;
  713. tf->device |= ATA_LBA;
  714. } else {
  715. /* CHS */
  716. u32 sect, head, cyl, track;
  717. /* The request -may- be too large for CHS addressing. */
  718. if ((block >> 28) || (n_block > 256))
  719. goto out_of_range;
  720. /* Convert LBA to CHS */
  721. track = (u32)block / dev->sectors;
  722. cyl = track / dev->heads;
  723. head = track % dev->heads;
  724. sect = (u32)block % dev->sectors + 1;
  725. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  726. (u32)block, track, cyl, head, sect);
  727. /* Check whether the converted CHS can fit.
  728. Cylinder: 0-65535
  729. Head: 0-15
  730. Sector: 1-255*/
  731. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  732. goto out_of_range;
  733. qc->nsect = n_block;
  734. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  735. tf->lbal = sect;
  736. tf->lbam = cyl;
  737. tf->lbah = cyl >> 8;
  738. tf->device |= head;
  739. }
  740. return 0;
  741. invalid_fld:
  742. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  743. /* "Invalid field in cbd" */
  744. return 1;
  745. out_of_range:
  746. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  747. /* "Logical Block Address out of range" */
  748. return 1;
  749. nothing_to_do:
  750. qc->scsicmd->result = SAM_STAT_GOOD;
  751. return 1;
  752. }
  753. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  754. {
  755. struct scsi_cmnd *cmd = qc->scsicmd;
  756. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
  757. ata_to_sense_error(qc, drv_stat);
  758. else
  759. cmd->result = SAM_STAT_GOOD;
  760. qc->scsidone(cmd);
  761. return 0;
  762. }
  763. /**
  764. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  765. * @ap: ATA port to which the command is addressed
  766. * @dev: ATA device to which the command is addressed
  767. * @cmd: SCSI command to execute
  768. * @done: SCSI command completion function
  769. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  770. *
  771. * Our ->queuecommand() function has decided that the SCSI
  772. * command issued can be directly translated into an ATA
  773. * command, rather than handled internally.
  774. *
  775. * This function sets up an ata_queued_cmd structure for the
  776. * SCSI command, and sends that ata_queued_cmd to the hardware.
  777. *
  778. * The xlat_func argument (actor) returns 0 if ready to execute
  779. * ATA command, else 1 to finish translation. If 1 is returned
  780. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  781. * to be set reflecting an error condition or clean (early)
  782. * termination.
  783. *
  784. * LOCKING:
  785. * spin_lock_irqsave(host_set lock)
  786. */
  787. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  788. struct scsi_cmnd *cmd,
  789. void (*done)(struct scsi_cmnd *),
  790. ata_xlat_func_t xlat_func)
  791. {
  792. struct ata_queued_cmd *qc;
  793. u8 *scsicmd = cmd->cmnd;
  794. VPRINTK("ENTER\n");
  795. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  796. if (!qc)
  797. goto err_mem;
  798. /* data is present; dma-map it */
  799. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  800. cmd->sc_data_direction == DMA_TO_DEVICE) {
  801. if (unlikely(cmd->request_bufflen < 1)) {
  802. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  803. ap->id, dev->devno);
  804. goto err_did;
  805. }
  806. if (cmd->use_sg)
  807. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  808. else
  809. ata_sg_init_one(qc, cmd->request_buffer,
  810. cmd->request_bufflen);
  811. qc->dma_dir = cmd->sc_data_direction;
  812. }
  813. qc->complete_fn = ata_scsi_qc_complete;
  814. if (xlat_func(qc, scsicmd))
  815. goto early_finish;
  816. /* select device, send command to hardware */
  817. if (ata_qc_issue(qc))
  818. goto err_did;
  819. VPRINTK("EXIT\n");
  820. return;
  821. early_finish:
  822. ata_qc_free(qc);
  823. done(cmd);
  824. DPRINTK("EXIT - early finish (good or error)\n");
  825. return;
  826. err_did:
  827. ata_qc_free(qc);
  828. err_mem:
  829. cmd->result = (DID_ERROR << 16);
  830. done(cmd);
  831. DPRINTK("EXIT - internal\n");
  832. return;
  833. }
  834. /**
  835. * ata_scsi_rbuf_get - Map response buffer.
  836. * @cmd: SCSI command containing buffer to be mapped.
  837. * @buf_out: Pointer to mapped area.
  838. *
  839. * Maps buffer contained within SCSI command @cmd.
  840. *
  841. * LOCKING:
  842. * spin_lock_irqsave(host_set lock)
  843. *
  844. * RETURNS:
  845. * Length of response buffer.
  846. */
  847. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  848. {
  849. u8 *buf;
  850. unsigned int buflen;
  851. if (cmd->use_sg) {
  852. struct scatterlist *sg;
  853. sg = (struct scatterlist *) cmd->request_buffer;
  854. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  855. buflen = sg->length;
  856. } else {
  857. buf = cmd->request_buffer;
  858. buflen = cmd->request_bufflen;
  859. }
  860. *buf_out = buf;
  861. return buflen;
  862. }
  863. /**
  864. * ata_scsi_rbuf_put - Unmap response buffer.
  865. * @cmd: SCSI command containing buffer to be unmapped.
  866. * @buf: buffer to unmap
  867. *
  868. * Unmaps response buffer contained within @cmd.
  869. *
  870. * LOCKING:
  871. * spin_lock_irqsave(host_set lock)
  872. */
  873. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  874. {
  875. if (cmd->use_sg) {
  876. struct scatterlist *sg;
  877. sg = (struct scatterlist *) cmd->request_buffer;
  878. kunmap_atomic(buf - sg->offset, KM_USER0);
  879. }
  880. }
  881. /**
  882. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  883. * @args: device IDENTIFY data / SCSI command of interest.
  884. * @actor: Callback hook for desired SCSI command simulator
  885. *
  886. * Takes care of the hard work of simulating a SCSI command...
  887. * Mapping the response buffer, calling the command's handler,
  888. * and handling the handler's return value. This return value
  889. * indicates whether the handler wishes the SCSI command to be
  890. * completed successfully (0), or not (in which case cmd->result
  891. * and sense buffer are assumed to be set).
  892. *
  893. * LOCKING:
  894. * spin_lock_irqsave(host_set lock)
  895. */
  896. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  897. unsigned int (*actor) (struct ata_scsi_args *args,
  898. u8 *rbuf, unsigned int buflen))
  899. {
  900. u8 *rbuf;
  901. unsigned int buflen, rc;
  902. struct scsi_cmnd *cmd = args->cmd;
  903. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  904. memset(rbuf, 0, buflen);
  905. rc = actor(args, rbuf, buflen);
  906. ata_scsi_rbuf_put(cmd, rbuf);
  907. if (rc == 0)
  908. cmd->result = SAM_STAT_GOOD;
  909. args->done(cmd);
  910. }
  911. /**
  912. * ata_scsiop_inq_std - Simulate INQUIRY command
  913. * @args: device IDENTIFY data / SCSI command of interest.
  914. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  915. * @buflen: Response buffer length.
  916. *
  917. * Returns standard device identification data associated
  918. * with non-EVPD INQUIRY command output.
  919. *
  920. * LOCKING:
  921. * spin_lock_irqsave(host_set lock)
  922. */
  923. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  924. unsigned int buflen)
  925. {
  926. u8 hdr[] = {
  927. TYPE_DISK,
  928. 0,
  929. 0x5, /* claim SPC-3 version compatibility */
  930. 2,
  931. 95 - 4
  932. };
  933. /* set scsi removeable (RMB) bit per ata bit */
  934. if (ata_id_removeable(args->id))
  935. hdr[1] |= (1 << 7);
  936. VPRINTK("ENTER\n");
  937. memcpy(rbuf, hdr, sizeof(hdr));
  938. if (buflen > 35) {
  939. memcpy(&rbuf[8], "ATA ", 8);
  940. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  941. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  942. if (rbuf[32] == 0 || rbuf[32] == ' ')
  943. memcpy(&rbuf[32], "n/a ", 4);
  944. }
  945. if (buflen > 63) {
  946. const u8 versions[] = {
  947. 0x60, /* SAM-3 (no version claimed) */
  948. 0x03,
  949. 0x20, /* SBC-2 (no version claimed) */
  950. 0x02,
  951. 0x60 /* SPC-3 (no version claimed) */
  952. };
  953. memcpy(rbuf + 59, versions, sizeof(versions));
  954. }
  955. return 0;
  956. }
  957. /**
  958. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  959. * @args: device IDENTIFY data / SCSI command of interest.
  960. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  961. * @buflen: Response buffer length.
  962. *
  963. * Returns list of inquiry EVPD pages available.
  964. *
  965. * LOCKING:
  966. * spin_lock_irqsave(host_set lock)
  967. */
  968. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  969. unsigned int buflen)
  970. {
  971. const u8 pages[] = {
  972. 0x00, /* page 0x00, this page */
  973. 0x80, /* page 0x80, unit serial no page */
  974. 0x83 /* page 0x83, device ident page */
  975. };
  976. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  977. if (buflen > 6)
  978. memcpy(rbuf + 4, pages, sizeof(pages));
  979. return 0;
  980. }
  981. /**
  982. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  983. * @args: device IDENTIFY data / SCSI command of interest.
  984. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  985. * @buflen: Response buffer length.
  986. *
  987. * Returns ATA device serial number.
  988. *
  989. * LOCKING:
  990. * spin_lock_irqsave(host_set lock)
  991. */
  992. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  993. unsigned int buflen)
  994. {
  995. const u8 hdr[] = {
  996. 0,
  997. 0x80, /* this page code */
  998. 0,
  999. ATA_SERNO_LEN, /* page len */
  1000. };
  1001. memcpy(rbuf, hdr, sizeof(hdr));
  1002. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  1003. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  1004. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1005. return 0;
  1006. }
  1007. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  1008. /**
  1009. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  1010. * @args: device IDENTIFY data / SCSI command of interest.
  1011. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1012. * @buflen: Response buffer length.
  1013. *
  1014. * Returns device identification. Currently hardcoded to
  1015. * return "Linux ATA-SCSI simulator".
  1016. *
  1017. * LOCKING:
  1018. * spin_lock_irqsave(host_set lock)
  1019. */
  1020. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  1021. unsigned int buflen)
  1022. {
  1023. rbuf[1] = 0x83; /* this page code */
  1024. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  1025. /* our one and only identification descriptor (vendor-specific) */
  1026. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  1027. rbuf[4 + 0] = 2; /* code set: ASCII */
  1028. rbuf[4 + 3] = strlen(inq_83_str);
  1029. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  1030. }
  1031. return 0;
  1032. }
  1033. /**
  1034. * ata_scsiop_noop - Command handler that simply returns success.
  1035. * @args: device IDENTIFY data / SCSI command of interest.
  1036. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1037. * @buflen: Response buffer length.
  1038. *
  1039. * No operation. Simply returns success to caller, to indicate
  1040. * that the caller should successfully complete this SCSI command.
  1041. *
  1042. * LOCKING:
  1043. * spin_lock_irqsave(host_set lock)
  1044. */
  1045. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  1046. unsigned int buflen)
  1047. {
  1048. VPRINTK("ENTER\n");
  1049. return 0;
  1050. }
  1051. /**
  1052. * ata_msense_push - Push data onto MODE SENSE data output buffer
  1053. * @ptr_io: (input/output) Location to store more output data
  1054. * @last: End of output data buffer
  1055. * @buf: Pointer to BLOB being added to output buffer
  1056. * @buflen: Length of BLOB
  1057. *
  1058. * Store MODE SENSE data on an output buffer.
  1059. *
  1060. * LOCKING:
  1061. * None.
  1062. */
  1063. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  1064. const u8 *buf, unsigned int buflen)
  1065. {
  1066. u8 *ptr = *ptr_io;
  1067. if ((ptr + buflen - 1) > last)
  1068. return;
  1069. memcpy(ptr, buf, buflen);
  1070. ptr += buflen;
  1071. *ptr_io = ptr;
  1072. }
  1073. /**
  1074. * ata_msense_caching - Simulate MODE SENSE caching info page
  1075. * @id: device IDENTIFY data
  1076. * @ptr_io: (input/output) Location to store more output data
  1077. * @last: End of output data buffer
  1078. *
  1079. * Generate a caching info page, which conditionally indicates
  1080. * write caching to the SCSI layer, depending on device
  1081. * capabilities.
  1082. *
  1083. * LOCKING:
  1084. * None.
  1085. */
  1086. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  1087. const u8 *last)
  1088. {
  1089. u8 page[] = {
  1090. 0x8, /* page code */
  1091. 0x12, /* page length */
  1092. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
  1093. 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
  1094. };
  1095. if (ata_id_wcache_enabled(id))
  1096. page[2] |= (1 << 2); /* write cache enable */
  1097. if (!ata_id_rahead_enabled(id))
  1098. page[12] |= (1 << 5); /* disable read ahead */
  1099. ata_msense_push(ptr_io, last, page, sizeof(page));
  1100. return sizeof(page);
  1101. }
  1102. /**
  1103. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1104. * @dev: Device associated with this MODE SENSE command
  1105. * @ptr_io: (input/output) Location to store more output data
  1106. * @last: End of output data buffer
  1107. *
  1108. * Generate a generic MODE SENSE control mode page.
  1109. *
  1110. * LOCKING:
  1111. * None.
  1112. */
  1113. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  1114. {
  1115. const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
  1116. /* byte 2: set the descriptor format sense data bit (bit 2)
  1117. * since we need to support returning this format for SAT
  1118. * commands and any SCSI commands against a 48b LBA device.
  1119. */
  1120. ata_msense_push(ptr_io, last, page, sizeof(page));
  1121. return sizeof(page);
  1122. }
  1123. /**
  1124. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1125. * @dev: Device associated with this MODE SENSE command
  1126. * @ptr_io: (input/output) Location to store more output data
  1127. * @last: End of output data buffer
  1128. *
  1129. * Generate a generic MODE SENSE r/w error recovery page.
  1130. *
  1131. * LOCKING:
  1132. * None.
  1133. */
  1134. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  1135. {
  1136. const u8 page[] = {
  1137. 0x1, /* page code */
  1138. 0xa, /* page length */
  1139. (1 << 7) | (1 << 6), /* note auto r/w reallocation */
  1140. 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
  1141. };
  1142. ata_msense_push(ptr_io, last, page, sizeof(page));
  1143. return sizeof(page);
  1144. }
  1145. /**
  1146. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1147. * @args: device IDENTIFY data / SCSI command of interest.
  1148. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1149. * @buflen: Response buffer length.
  1150. *
  1151. * Simulate MODE SENSE commands.
  1152. *
  1153. * LOCKING:
  1154. * spin_lock_irqsave(host_set lock)
  1155. */
  1156. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1157. unsigned int buflen)
  1158. {
  1159. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1160. unsigned int page_control, six_byte, output_len;
  1161. VPRINTK("ENTER\n");
  1162. six_byte = (scsicmd[0] == MODE_SENSE);
  1163. /* we only support saved and current values (which we treat
  1164. * in the same manner)
  1165. */
  1166. page_control = scsicmd[2] >> 6;
  1167. switch (page_control) {
  1168. case 0: /* current */
  1169. break; /* supported */
  1170. case 3: /* saved */
  1171. goto saving_not_supp;
  1172. case 1: /* changeable */
  1173. case 2: /* defaults */
  1174. default:
  1175. goto invalid_fld;
  1176. }
  1177. if (six_byte)
  1178. output_len = 4;
  1179. else
  1180. output_len = 8;
  1181. p = rbuf + output_len;
  1182. last = rbuf + buflen - 1;
  1183. switch(scsicmd[2] & 0x3f) {
  1184. case 0x01: /* r/w error recovery */
  1185. output_len += ata_msense_rw_recovery(&p, last);
  1186. break;
  1187. case 0x08: /* caching */
  1188. output_len += ata_msense_caching(args->id, &p, last);
  1189. break;
  1190. case 0x0a: { /* control mode */
  1191. output_len += ata_msense_ctl_mode(&p, last);
  1192. break;
  1193. }
  1194. case 0x3f: /* all pages */
  1195. output_len += ata_msense_rw_recovery(&p, last);
  1196. output_len += ata_msense_caching(args->id, &p, last);
  1197. output_len += ata_msense_ctl_mode(&p, last);
  1198. break;
  1199. default: /* invalid page code */
  1200. goto invalid_fld;
  1201. }
  1202. if (six_byte) {
  1203. output_len--;
  1204. rbuf[0] = output_len;
  1205. } else {
  1206. output_len -= 2;
  1207. rbuf[0] = output_len >> 8;
  1208. rbuf[1] = output_len;
  1209. }
  1210. return 0;
  1211. invalid_fld:
  1212. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1213. /* "Invalid field in cbd" */
  1214. return 1;
  1215. saving_not_supp:
  1216. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  1217. /* "Saving parameters not supported" */
  1218. return 1;
  1219. }
  1220. /**
  1221. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1222. * @args: device IDENTIFY data / SCSI command of interest.
  1223. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1224. * @buflen: Response buffer length.
  1225. *
  1226. * Simulate READ CAPACITY commands.
  1227. *
  1228. * LOCKING:
  1229. * spin_lock_irqsave(host_set lock)
  1230. */
  1231. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1232. unsigned int buflen)
  1233. {
  1234. u64 n_sectors;
  1235. u32 tmp;
  1236. VPRINTK("ENTER\n");
  1237. if (ata_id_has_lba(args->id)) {
  1238. if (ata_id_has_lba48(args->id))
  1239. n_sectors = ata_id_u64(args->id, 100);
  1240. else
  1241. n_sectors = ata_id_u32(args->id, 60);
  1242. } else {
  1243. /* CHS default translation */
  1244. n_sectors = args->id[1] * args->id[3] * args->id[6];
  1245. if (ata_id_current_chs_valid(args->id))
  1246. /* CHS current translation */
  1247. n_sectors = ata_id_u32(args->id, 57);
  1248. }
  1249. n_sectors--; /* ATA TotalUserSectors - 1 */
  1250. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1251. if( n_sectors >= 0xffffffffULL )
  1252. tmp = 0xffffffff ; /* Return max count on overflow */
  1253. else
  1254. tmp = n_sectors ;
  1255. /* sector count, 32-bit */
  1256. rbuf[0] = tmp >> (8 * 3);
  1257. rbuf[1] = tmp >> (8 * 2);
  1258. rbuf[2] = tmp >> (8 * 1);
  1259. rbuf[3] = tmp;
  1260. /* sector size */
  1261. tmp = ATA_SECT_SIZE;
  1262. rbuf[6] = tmp >> 8;
  1263. rbuf[7] = tmp;
  1264. } else {
  1265. /* sector count, 64-bit */
  1266. tmp = n_sectors >> (8 * 4);
  1267. rbuf[2] = tmp >> (8 * 3);
  1268. rbuf[3] = tmp >> (8 * 2);
  1269. rbuf[4] = tmp >> (8 * 1);
  1270. rbuf[5] = tmp;
  1271. tmp = n_sectors;
  1272. rbuf[6] = tmp >> (8 * 3);
  1273. rbuf[7] = tmp >> (8 * 2);
  1274. rbuf[8] = tmp >> (8 * 1);
  1275. rbuf[9] = tmp;
  1276. /* sector size */
  1277. tmp = ATA_SECT_SIZE;
  1278. rbuf[12] = tmp >> 8;
  1279. rbuf[13] = tmp;
  1280. }
  1281. return 0;
  1282. }
  1283. /**
  1284. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1285. * @args: device IDENTIFY data / SCSI command of interest.
  1286. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1287. * @buflen: Response buffer length.
  1288. *
  1289. * Simulate REPORT LUNS command.
  1290. *
  1291. * LOCKING:
  1292. * spin_lock_irqsave(host_set lock)
  1293. */
  1294. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1295. unsigned int buflen)
  1296. {
  1297. VPRINTK("ENTER\n");
  1298. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1299. return 0;
  1300. }
  1301. /**
  1302. * ata_scsi_set_sense - Set SCSI sense data and status
  1303. * @cmd: SCSI request to be handled
  1304. * @sk: SCSI-defined sense key
  1305. * @asc: SCSI-defined additional sense code
  1306. * @ascq: SCSI-defined additional sense code qualifier
  1307. *
  1308. * Helper function that builds a valid fixed format, current
  1309. * response code and the given sense key (sk), additional sense
  1310. * code (asc) and additional sense code qualifier (ascq) with
  1311. * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
  1312. * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
  1313. *
  1314. * LOCKING:
  1315. * Not required
  1316. */
  1317. void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  1318. {
  1319. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  1320. cmd->sense_buffer[0] = 0x70; /* fixed format, current */
  1321. cmd->sense_buffer[2] = sk;
  1322. cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
  1323. cmd->sense_buffer[12] = asc;
  1324. cmd->sense_buffer[13] = ascq;
  1325. }
  1326. /**
  1327. * ata_scsi_badcmd - End a SCSI request with an error
  1328. * @cmd: SCSI request to be handled
  1329. * @done: SCSI command completion function
  1330. * @asc: SCSI-defined additional sense code
  1331. * @ascq: SCSI-defined additional sense code qualifier
  1332. *
  1333. * Helper function that completes a SCSI command with
  1334. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1335. * and the specified additional sense codes.
  1336. *
  1337. * LOCKING:
  1338. * spin_lock_irqsave(host_set lock)
  1339. */
  1340. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1341. {
  1342. DPRINTK("ENTER\n");
  1343. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
  1344. done(cmd);
  1345. }
  1346. void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
  1347. struct scsi_cmnd *cmd)
  1348. {
  1349. DECLARE_COMPLETION(wait);
  1350. struct ata_queued_cmd *qc;
  1351. unsigned long flags;
  1352. int rc;
  1353. DPRINTK("ATAPI request sense\n");
  1354. qc = ata_qc_new_init(ap, dev);
  1355. BUG_ON(qc == NULL);
  1356. /* FIXME: is this needed? */
  1357. memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  1358. ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
  1359. qc->dma_dir = DMA_FROM_DEVICE;
  1360. memset(&qc->cdb, 0, ap->cdb_len);
  1361. qc->cdb[0] = REQUEST_SENSE;
  1362. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1363. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1364. qc->tf.command = ATA_CMD_PACKET;
  1365. qc->tf.protocol = ATA_PROT_ATAPI;
  1366. qc->tf.lbam = (8 * 1024) & 0xff;
  1367. qc->tf.lbah = (8 * 1024) >> 8;
  1368. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  1369. qc->waiting = &wait;
  1370. qc->complete_fn = ata_qc_complete_noop;
  1371. spin_lock_irqsave(&ap->host_set->lock, flags);
  1372. rc = ata_qc_issue(qc);
  1373. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1374. if (rc)
  1375. ata_port_disable(ap);
  1376. else
  1377. wait_for_completion(&wait);
  1378. DPRINTK("EXIT\n");
  1379. }
  1380. static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  1381. {
  1382. struct scsi_cmnd *cmd = qc->scsicmd;
  1383. VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat);
  1384. if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ)))
  1385. ata_to_sense_error(qc, drv_stat);
  1386. else if (unlikely(drv_stat & ATA_ERR)) {
  1387. DPRINTK("request check condition\n");
  1388. /* FIXME: command completion with check condition
  1389. * but no sense causes the error handler to run,
  1390. * which then issues REQUEST SENSE, fills in the sense
  1391. * buffer, and completes the command (for the second
  1392. * time). We need to issue REQUEST SENSE some other
  1393. * way, to avoid completing the command twice.
  1394. */
  1395. cmd->result = SAM_STAT_CHECK_CONDITION;
  1396. qc->scsidone(cmd);
  1397. return 1;
  1398. }
  1399. else {
  1400. u8 *scsicmd = cmd->cmnd;
  1401. if (scsicmd[0] == INQUIRY) {
  1402. u8 *buf = NULL;
  1403. unsigned int buflen;
  1404. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1405. /* ATAPI devices typically report zero for their SCSI version,
  1406. * and sometimes deviate from the spec WRT response data
  1407. * format. If SCSI version is reported as zero like normal,
  1408. * then we make the following fixups: 1) Fake MMC-5 version,
  1409. * to indicate to the Linux scsi midlayer this is a modern
  1410. * device. 2) Ensure response data format / ATAPI information
  1411. * are always correct.
  1412. */
  1413. /* FIXME: do we ever override EVPD pages and the like, with
  1414. * this code?
  1415. */
  1416. if (buf[2] == 0) {
  1417. buf[2] = 0x5;
  1418. buf[3] = 0x32;
  1419. }
  1420. ata_scsi_rbuf_put(cmd, buf);
  1421. }
  1422. cmd->result = SAM_STAT_GOOD;
  1423. }
  1424. qc->scsidone(cmd);
  1425. return 0;
  1426. }
  1427. /**
  1428. * atapi_xlat - Initialize PACKET taskfile
  1429. * @qc: command structure to be initialized
  1430. * @scsicmd: SCSI CDB associated with this PACKET command
  1431. *
  1432. * LOCKING:
  1433. * spin_lock_irqsave(host_set lock)
  1434. *
  1435. * RETURNS:
  1436. * Zero on success, non-zero on failure.
  1437. */
  1438. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  1439. {
  1440. struct scsi_cmnd *cmd = qc->scsicmd;
  1441. struct ata_device *dev = qc->dev;
  1442. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1443. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1444. if (!using_pio)
  1445. /* Check whether ATAPI DMA is safe */
  1446. if (ata_check_atapi_dma(qc))
  1447. using_pio = 1;
  1448. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1449. qc->complete_fn = atapi_qc_complete;
  1450. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1451. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1452. qc->tf.flags |= ATA_TFLAG_WRITE;
  1453. DPRINTK("direction: write\n");
  1454. }
  1455. qc->tf.command = ATA_CMD_PACKET;
  1456. /* no data, or PIO data xfer */
  1457. if (using_pio || nodata) {
  1458. if (nodata)
  1459. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1460. else
  1461. qc->tf.protocol = ATA_PROT_ATAPI;
  1462. qc->tf.lbam = (8 * 1024) & 0xff;
  1463. qc->tf.lbah = (8 * 1024) >> 8;
  1464. }
  1465. /* DMA data xfer */
  1466. else {
  1467. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1468. qc->tf.feature |= ATAPI_PKT_DMA;
  1469. #ifdef ATAPI_ENABLE_DMADIR
  1470. /* some SATA bridges need us to indicate data xfer direction */
  1471. if (cmd->sc_data_direction != DMA_TO_DEVICE)
  1472. qc->tf.feature |= ATAPI_DMADIR;
  1473. #endif
  1474. }
  1475. qc->nbytes = cmd->bufflen;
  1476. return 0;
  1477. }
  1478. /**
  1479. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1480. * @ap: ATA port to which the device is attached
  1481. * @scsidev: SCSI device from which we derive the ATA device
  1482. *
  1483. * Given various information provided in struct scsi_cmnd,
  1484. * map that onto an ATA bus, and using that mapping
  1485. * determine which ata_device is associated with the
  1486. * SCSI command to be sent.
  1487. *
  1488. * LOCKING:
  1489. * spin_lock_irqsave(host_set lock)
  1490. *
  1491. * RETURNS:
  1492. * Associated ATA device, or %NULL if not found.
  1493. */
  1494. static struct ata_device *
  1495. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
  1496. {
  1497. struct ata_device *dev;
  1498. /* skip commands not addressed to targets we simulate */
  1499. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1500. dev = &ap->device[scsidev->id];
  1501. else
  1502. return NULL;
  1503. if (unlikely((scsidev->channel != 0) ||
  1504. (scsidev->lun != 0)))
  1505. return NULL;
  1506. if (unlikely(!ata_dev_present(dev)))
  1507. return NULL;
  1508. if (!atapi_enabled) {
  1509. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1510. return NULL;
  1511. }
  1512. return dev;
  1513. }
  1514. /**
  1515. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1516. * @dev: ATA device
  1517. * @cmd: SCSI command opcode to consider
  1518. *
  1519. * Look up the SCSI command given, and determine whether the
  1520. * SCSI command is to be translated or simulated.
  1521. *
  1522. * RETURNS:
  1523. * Pointer to translation function if possible, %NULL if not.
  1524. */
  1525. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  1526. {
  1527. switch (cmd) {
  1528. case READ_6:
  1529. case READ_10:
  1530. case READ_16:
  1531. case WRITE_6:
  1532. case WRITE_10:
  1533. case WRITE_16:
  1534. return ata_scsi_rw_xlat;
  1535. case SYNCHRONIZE_CACHE:
  1536. if (ata_try_flush_cache(dev))
  1537. return ata_scsi_flush_xlat;
  1538. break;
  1539. case VERIFY:
  1540. case VERIFY_16:
  1541. return ata_scsi_verify_xlat;
  1542. case START_STOP:
  1543. return ata_scsi_start_stop_xlat;
  1544. }
  1545. return NULL;
  1546. }
  1547. /**
  1548. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  1549. * @ap: ATA port to which the command was being sent
  1550. * @cmd: SCSI command to dump
  1551. *
  1552. * Prints the contents of a SCSI command via printk().
  1553. */
  1554. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  1555. struct scsi_cmnd *cmd)
  1556. {
  1557. #ifdef ATA_DEBUG
  1558. struct scsi_device *scsidev = cmd->device;
  1559. u8 *scsicmd = cmd->cmnd;
  1560. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1561. ap->id,
  1562. scsidev->channel, scsidev->id, scsidev->lun,
  1563. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  1564. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  1565. scsicmd[8]);
  1566. #endif
  1567. }
  1568. /**
  1569. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  1570. * @cmd: SCSI command to be sent
  1571. * @done: Completion function, called when command is complete
  1572. *
  1573. * In some cases, this function translates SCSI commands into
  1574. * ATA taskfiles, and queues the taskfiles to be sent to
  1575. * hardware. In other cases, this function simulates a
  1576. * SCSI device by evaluating and responding to certain
  1577. * SCSI commands. This creates the overall effect of
  1578. * ATA and ATAPI devices appearing as SCSI devices.
  1579. *
  1580. * LOCKING:
  1581. * Releases scsi-layer-held lock, and obtains host_set lock.
  1582. *
  1583. * RETURNS:
  1584. * Zero.
  1585. */
  1586. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  1587. {
  1588. struct ata_port *ap;
  1589. struct ata_device *dev;
  1590. struct scsi_device *scsidev = cmd->device;
  1591. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  1592. ata_scsi_dump_cdb(ap, cmd);
  1593. dev = ata_scsi_find_dev(ap, scsidev);
  1594. if (unlikely(!dev)) {
  1595. cmd->result = (DID_BAD_TARGET << 16);
  1596. done(cmd);
  1597. goto out_unlock;
  1598. }
  1599. if (dev->class == ATA_DEV_ATA) {
  1600. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  1601. cmd->cmnd[0]);
  1602. if (xlat_func)
  1603. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  1604. else
  1605. ata_scsi_simulate(dev->id, cmd, done);
  1606. } else
  1607. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  1608. out_unlock:
  1609. return 0;
  1610. }
  1611. /**
  1612. * ata_scsi_simulate - simulate SCSI command on ATA device
  1613. * @id: current IDENTIFY data for target device.
  1614. * @cmd: SCSI command being sent to device.
  1615. * @done: SCSI command completion function.
  1616. *
  1617. * Interprets and directly executes a select list of SCSI commands
  1618. * that can be handled internally.
  1619. *
  1620. * LOCKING:
  1621. * spin_lock_irqsave(host_set lock)
  1622. */
  1623. void ata_scsi_simulate(u16 *id,
  1624. struct scsi_cmnd *cmd,
  1625. void (*done)(struct scsi_cmnd *))
  1626. {
  1627. struct ata_scsi_args args;
  1628. u8 *scsicmd = cmd->cmnd;
  1629. args.id = id;
  1630. args.cmd = cmd;
  1631. args.done = done;
  1632. switch(scsicmd[0]) {
  1633. /* no-op's, complete with success */
  1634. case SYNCHRONIZE_CACHE:
  1635. case REZERO_UNIT:
  1636. case SEEK_6:
  1637. case SEEK_10:
  1638. case TEST_UNIT_READY:
  1639. case FORMAT_UNIT: /* FIXME: correct? */
  1640. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  1641. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  1642. break;
  1643. case INQUIRY:
  1644. if (scsicmd[1] & 2) /* is CmdDt set? */
  1645. ata_scsi_invalid_field(cmd, done);
  1646. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  1647. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  1648. else if (scsicmd[2] == 0x00)
  1649. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  1650. else if (scsicmd[2] == 0x80)
  1651. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  1652. else if (scsicmd[2] == 0x83)
  1653. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  1654. else
  1655. ata_scsi_invalid_field(cmd, done);
  1656. break;
  1657. case MODE_SENSE:
  1658. case MODE_SENSE_10:
  1659. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  1660. break;
  1661. case MODE_SELECT: /* unconditionally return */
  1662. case MODE_SELECT_10: /* bad-field-in-cdb */
  1663. ata_scsi_invalid_field(cmd, done);
  1664. break;
  1665. case READ_CAPACITY:
  1666. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1667. break;
  1668. case SERVICE_ACTION_IN:
  1669. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  1670. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1671. else
  1672. ata_scsi_invalid_field(cmd, done);
  1673. break;
  1674. case REPORT_LUNS:
  1675. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  1676. break;
  1677. /* mandantory commands we haven't implemented yet */
  1678. case REQUEST_SENSE:
  1679. /* all other commands */
  1680. default:
  1681. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  1682. /* "Invalid command operation code" */
  1683. done(cmd);
  1684. break;
  1685. }
  1686. }
  1687. void ata_scsi_scan_host(struct ata_port *ap)
  1688. {
  1689. struct ata_device *dev;
  1690. unsigned int i;
  1691. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  1692. return;
  1693. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1694. dev = &ap->device[i];
  1695. if (ata_dev_present(dev))
  1696. scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
  1697. }
  1698. }