libata-scsi.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  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/scsi_host.h>
  40. #include <scsi/scsi_eh.h>
  41. #include <scsi/scsi_device.h>
  42. #include <scsi/scsi_request.h>
  43. #include <linux/libata.h>
  44. #include <linux/hdreg.h>
  45. #include <asm/uaccess.h>
  46. #include "libata.h"
  47. #define SECTOR_SIZE 512
  48. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
  49. static struct ata_device *
  50. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
  51. #define RW_RECOVERY_MPAGE 0x1
  52. #define RW_RECOVERY_MPAGE_LEN 12
  53. #define CACHE_MPAGE 0x8
  54. #define CACHE_MPAGE_LEN 20
  55. #define CONTROL_MPAGE 0xa
  56. #define CONTROL_MPAGE_LEN 12
  57. #define ALL_MPAGES 0x3f
  58. #define ALL_SUB_MPAGES 0xff
  59. static const u8 def_rw_recovery_mpage[] = {
  60. RW_RECOVERY_MPAGE,
  61. RW_RECOVERY_MPAGE_LEN - 2,
  62. (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
  63. (1 << 6), /* ARRE (auto read reallocation) */
  64. 0, /* read retry count */
  65. 0, 0, 0, 0,
  66. 0, /* write retry count */
  67. 0, 0, 0
  68. };
  69. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  70. CACHE_MPAGE,
  71. CACHE_MPAGE_LEN - 2,
  72. 0, /* contains WCE, needs to be 0 for logic */
  73. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, /* contains DRA, needs to be 0 for logic */
  75. 0, 0, 0, 0, 0, 0, 0
  76. };
  77. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  78. CONTROL_MPAGE,
  79. CONTROL_MPAGE_LEN - 2,
  80. 2, /* DSENSE=0, GLTSD=1 */
  81. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  82. 0, 0, 0, 0, 0xff, 0xff,
  83. 0, 30 /* extended self test time, see 05-359r1 */
  84. };
  85. static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
  86. void (*done)(struct scsi_cmnd *))
  87. {
  88. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  89. /* "Invalid field in cbd" */
  90. done(cmd);
  91. }
  92. /**
  93. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  94. * @sdev: SCSI device for which BIOS geometry is to be determined
  95. * @bdev: block device associated with @sdev
  96. * @capacity: capacity of SCSI device
  97. * @geom: location to which geometry will be output
  98. *
  99. * Generic bios head/sector/cylinder calculator
  100. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  101. * mapping. Some situations may arise where the disk is not
  102. * bootable if this is not used.
  103. *
  104. * LOCKING:
  105. * Defined by the SCSI layer. We don't really care.
  106. *
  107. * RETURNS:
  108. * Zero.
  109. */
  110. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  111. sector_t capacity, int geom[])
  112. {
  113. geom[0] = 255;
  114. geom[1] = 63;
  115. sector_div(capacity, 255*63);
  116. geom[2] = capacity;
  117. return 0;
  118. }
  119. /**
  120. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  121. * @scsidev: Device to which we are issuing command
  122. * @arg: User provided data for issuing command
  123. *
  124. * LOCKING:
  125. * Defined by the SCSI layer. We don't really care.
  126. *
  127. * RETURNS:
  128. * Zero on success, negative errno on error.
  129. */
  130. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  131. {
  132. int rc = 0;
  133. u8 scsi_cmd[MAX_COMMAND_SIZE];
  134. u8 args[4], *argbuf = NULL;
  135. int argsize = 0;
  136. struct scsi_sense_hdr sshdr;
  137. enum dma_data_direction data_dir;
  138. if (NULL == (void *)arg)
  139. return -EINVAL;
  140. if (copy_from_user(args, arg, sizeof(args)))
  141. return -EFAULT;
  142. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  143. if (args[3]) {
  144. argsize = SECTOR_SIZE * args[3];
  145. argbuf = kmalloc(argsize, GFP_KERNEL);
  146. if (argbuf == NULL) {
  147. rc = -ENOMEM;
  148. goto error;
  149. }
  150. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  151. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  152. block count in sector count field */
  153. data_dir = DMA_FROM_DEVICE;
  154. } else {
  155. scsi_cmd[1] = (3 << 1); /* Non-data */
  156. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  157. data_dir = DMA_NONE;
  158. }
  159. scsi_cmd[0] = ATA_16;
  160. scsi_cmd[4] = args[2];
  161. if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
  162. scsi_cmd[6] = args[3];
  163. scsi_cmd[8] = args[1];
  164. scsi_cmd[10] = 0x4f;
  165. scsi_cmd[12] = 0xc2;
  166. } else {
  167. scsi_cmd[6] = args[1];
  168. }
  169. scsi_cmd[14] = args[0];
  170. /* Good values for timeout and retries? Values below
  171. from scsi_ioctl_send_command() for default case... */
  172. if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
  173. &sshdr, (10*HZ), 5)) {
  174. rc = -EIO;
  175. goto error;
  176. }
  177. /* Need code to retrieve data from check condition? */
  178. if ((argbuf)
  179. && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
  180. rc = -EFAULT;
  181. error:
  182. if (argbuf)
  183. kfree(argbuf);
  184. return rc;
  185. }
  186. /**
  187. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  188. * @scsidev: Device to which we are issuing command
  189. * @arg: User provided data for issuing command
  190. *
  191. * LOCKING:
  192. * Defined by the SCSI layer. We don't really care.
  193. *
  194. * RETURNS:
  195. * Zero on success, negative errno on error.
  196. */
  197. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  198. {
  199. int rc = 0;
  200. u8 scsi_cmd[MAX_COMMAND_SIZE];
  201. u8 args[7];
  202. struct scsi_sense_hdr sshdr;
  203. if (NULL == (void *)arg)
  204. return -EINVAL;
  205. if (copy_from_user(args, arg, sizeof(args)))
  206. return -EFAULT;
  207. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  208. scsi_cmd[0] = ATA_16;
  209. scsi_cmd[1] = (3 << 1); /* Non-data */
  210. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  211. scsi_cmd[4] = args[1];
  212. scsi_cmd[6] = args[2];
  213. scsi_cmd[8] = args[3];
  214. scsi_cmd[10] = args[4];
  215. scsi_cmd[12] = args[5];
  216. scsi_cmd[14] = args[0];
  217. /* Good values for timeout and retries? Values below
  218. from scsi_ioctl_send_command() for default case... */
  219. if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
  220. (10*HZ), 5))
  221. rc = -EIO;
  222. /* Need code to retrieve data from check condition? */
  223. return rc;
  224. }
  225. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  226. {
  227. struct ata_port *ap;
  228. struct ata_device *dev;
  229. int val = -EINVAL, rc = -EINVAL;
  230. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  231. if (!ap)
  232. goto out;
  233. dev = ata_scsi_find_dev(ap, scsidev);
  234. if (!dev) {
  235. rc = -ENODEV;
  236. goto out;
  237. }
  238. switch (cmd) {
  239. case ATA_IOC_GET_IO32:
  240. val = 0;
  241. if (copy_to_user(arg, &val, 1))
  242. return -EFAULT;
  243. return 0;
  244. case ATA_IOC_SET_IO32:
  245. val = (unsigned long) arg;
  246. if (val != 0)
  247. return -EINVAL;
  248. return 0;
  249. case HDIO_DRIVE_CMD:
  250. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  251. return -EACCES;
  252. return ata_cmd_ioctl(scsidev, arg);
  253. case HDIO_DRIVE_TASK:
  254. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  255. return -EACCES;
  256. return ata_task_ioctl(scsidev, arg);
  257. default:
  258. rc = -ENOTTY;
  259. break;
  260. }
  261. out:
  262. return rc;
  263. }
  264. /**
  265. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  266. * @ap: ATA port to which the new command is attached
  267. * @dev: ATA device to which the new command is attached
  268. * @cmd: SCSI command that originated this ATA command
  269. * @done: SCSI command completion function
  270. *
  271. * Obtain a reference to an unused ata_queued_cmd structure,
  272. * which is the basic libata structure representing a single
  273. * ATA command sent to the hardware.
  274. *
  275. * If a command was available, fill in the SCSI-specific
  276. * portions of the structure with information on the
  277. * current command.
  278. *
  279. * LOCKING:
  280. * spin_lock_irqsave(host_set lock)
  281. *
  282. * RETURNS:
  283. * Command allocated, or %NULL if none available.
  284. */
  285. struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
  286. struct ata_device *dev,
  287. struct scsi_cmnd *cmd,
  288. void (*done)(struct scsi_cmnd *))
  289. {
  290. struct ata_queued_cmd *qc;
  291. qc = ata_qc_new_init(ap, dev);
  292. if (qc) {
  293. qc->scsicmd = cmd;
  294. qc->scsidone = done;
  295. if (cmd->use_sg) {
  296. qc->__sg = (struct scatterlist *) cmd->request_buffer;
  297. qc->n_elem = cmd->use_sg;
  298. } else {
  299. qc->__sg = &qc->sgent;
  300. qc->n_elem = 1;
  301. }
  302. } else {
  303. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  304. done(cmd);
  305. }
  306. return qc;
  307. }
  308. /**
  309. * ata_dump_status - user friendly display of error info
  310. * @id: id of the port in question
  311. * @tf: ptr to filled out taskfile
  312. *
  313. * Decode and dump the ATA error/status registers for the user so
  314. * that they have some idea what really happened at the non
  315. * make-believe layer.
  316. *
  317. * LOCKING:
  318. * inherited from caller
  319. */
  320. void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  321. {
  322. u8 stat = tf->command, err = tf->feature;
  323. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  324. if (stat & ATA_BUSY) {
  325. printk("Busy }\n"); /* Data is not valid in this case */
  326. } else {
  327. if (stat & 0x40) printk("DriveReady ");
  328. if (stat & 0x20) printk("DeviceFault ");
  329. if (stat & 0x10) printk("SeekComplete ");
  330. if (stat & 0x08) printk("DataRequest ");
  331. if (stat & 0x04) printk("CorrectedError ");
  332. if (stat & 0x02) printk("Index ");
  333. if (stat & 0x01) printk("Error ");
  334. printk("}\n");
  335. if (err) {
  336. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  337. if (err & 0x04) printk("DriveStatusError ");
  338. if (err & 0x80) {
  339. if (err & 0x04) printk("BadCRC ");
  340. else printk("Sector ");
  341. }
  342. if (err & 0x40) printk("UncorrectableError ");
  343. if (err & 0x10) printk("SectorIdNotFound ");
  344. if (err & 0x02) printk("TrackZeroNotFound ");
  345. if (err & 0x01) printk("AddrMarkNotFound ");
  346. printk("}\n");
  347. }
  348. }
  349. }
  350. /**
  351. * ata_to_sense_error - convert ATA error to SCSI error
  352. * @id: ATA device number
  353. * @drv_stat: value contained in ATA status register
  354. * @drv_err: value contained in ATA error register
  355. * @sk: the sense key we'll fill out
  356. * @asc: the additional sense code we'll fill out
  357. * @ascq: the additional sense code qualifier we'll fill out
  358. *
  359. * Converts an ATA error into a SCSI error. Fill out pointers to
  360. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  361. * format sense blocks.
  362. *
  363. * LOCKING:
  364. * spin_lock_irqsave(host_set lock)
  365. */
  366. void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
  367. u8 *ascq)
  368. {
  369. int i;
  370. /* Based on the 3ware driver translation table */
  371. static unsigned char sense_table[][4] = {
  372. /* BBD|ECC|ID|MAR */
  373. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  374. /* BBD|ECC|ID */
  375. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  376. /* ECC|MC|MARK */
  377. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  378. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  379. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  380. /* MC|ID|ABRT|TRK0|MARK */
  381. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  382. /* MCR|MARK */
  383. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  384. /* Bad address mark */
  385. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  386. /* TRK0 */
  387. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  388. /* Abort & !ICRC */
  389. {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
  390. /* Media change request */
  391. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  392. /* SRV */
  393. {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
  394. /* Media change */
  395. {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
  396. /* ECC */
  397. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  398. /* BBD - block marked bad */
  399. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  400. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  401. };
  402. static unsigned char stat_table[][4] = {
  403. /* Must be first because BUSY means no other bits valid */
  404. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  405. {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
  406. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  407. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  408. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  409. };
  410. /*
  411. * Is this an error we can process/parse
  412. */
  413. if (drv_stat & ATA_BUSY) {
  414. drv_err = 0; /* Ignore the err bits, they're invalid */
  415. }
  416. if (drv_err) {
  417. /* Look for drv_err */
  418. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  419. /* Look for best matches first */
  420. if ((sense_table[i][0] & drv_err) ==
  421. sense_table[i][0]) {
  422. *sk = sense_table[i][1];
  423. *asc = sense_table[i][2];
  424. *ascq = sense_table[i][3];
  425. goto translate_done;
  426. }
  427. }
  428. /* No immediate match */
  429. printk(KERN_WARNING "ata%u: no sense translation for "
  430. "error 0x%02x\n", id, drv_err);
  431. }
  432. /* Fall back to interpreting status bits */
  433. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  434. if (stat_table[i][0] & drv_stat) {
  435. *sk = stat_table[i][1];
  436. *asc = stat_table[i][2];
  437. *ascq = stat_table[i][3];
  438. goto translate_done;
  439. }
  440. }
  441. /* No error? Undecoded? */
  442. printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
  443. id, drv_stat);
  444. /* For our last chance pick, use medium read error because
  445. * it's much more common than an ATA drive telling you a write
  446. * has failed.
  447. */
  448. *sk = MEDIUM_ERROR;
  449. *asc = 0x11; /* "unrecovered read error" */
  450. *ascq = 0x04; /* "auto-reallocation failed" */
  451. translate_done:
  452. printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
  453. "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
  454. *sk, *asc, *ascq);
  455. return;
  456. }
  457. /*
  458. * ata_gen_ata_desc_sense - Generate check condition sense block.
  459. * @qc: Command that completed.
  460. *
  461. * This function is specific to the ATA descriptor format sense
  462. * block specified for the ATA pass through commands. Regardless
  463. * of whether the command errored or not, return a sense
  464. * block. Copy all controller registers into the sense
  465. * block. Clear sense key, ASC & ASCQ if there is no error.
  466. *
  467. * LOCKING:
  468. * spin_lock_irqsave(host_set lock)
  469. */
  470. void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
  471. {
  472. struct scsi_cmnd *cmd = qc->scsicmd;
  473. struct ata_taskfile *tf = &qc->tf;
  474. unsigned char *sb = cmd->sense_buffer;
  475. unsigned char *desc = sb + 8;
  476. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  477. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  478. /*
  479. * Read the controller registers.
  480. */
  481. assert(NULL != qc->ap->ops->tf_read);
  482. qc->ap->ops->tf_read(qc->ap, tf);
  483. /*
  484. * Use ata_to_sense_error() to map status register bits
  485. * onto sense key, asc & ascq.
  486. */
  487. if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  488. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  489. &sb[1], &sb[2], &sb[3]);
  490. sb[1] &= 0x0f;
  491. }
  492. /*
  493. * Sense data is current and format is descriptor.
  494. */
  495. sb[0] = 0x72;
  496. desc[0] = 0x09;
  497. /*
  498. * Set length of additional sense data.
  499. * Since we only populate descriptor 0, the total
  500. * length is the same (fixed) length as descriptor 0.
  501. */
  502. desc[1] = sb[7] = 14;
  503. /*
  504. * Copy registers into sense buffer.
  505. */
  506. desc[2] = 0x00;
  507. desc[3] = tf->feature; /* == error reg */
  508. desc[5] = tf->nsect;
  509. desc[7] = tf->lbal;
  510. desc[9] = tf->lbam;
  511. desc[11] = tf->lbah;
  512. desc[12] = tf->device;
  513. desc[13] = tf->command; /* == status reg */
  514. /*
  515. * Fill in Extend bit, and the high order bytes
  516. * if applicable.
  517. */
  518. if (tf->flags & ATA_TFLAG_LBA48) {
  519. desc[2] |= 0x01;
  520. desc[4] = tf->hob_nsect;
  521. desc[6] = tf->hob_lbal;
  522. desc[8] = tf->hob_lbam;
  523. desc[10] = tf->hob_lbah;
  524. }
  525. }
  526. /**
  527. * ata_gen_fixed_sense - generate a SCSI fixed sense block
  528. * @qc: Command that we are erroring out
  529. *
  530. * Leverage ata_to_sense_error() to give us the codes. Fit our
  531. * LBA in here if there's room.
  532. *
  533. * LOCKING:
  534. * inherited from caller
  535. */
  536. void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
  537. {
  538. struct scsi_cmnd *cmd = qc->scsicmd;
  539. struct ata_taskfile *tf = &qc->tf;
  540. unsigned char *sb = cmd->sense_buffer;
  541. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  542. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  543. /*
  544. * Read the controller registers.
  545. */
  546. assert(NULL != qc->ap->ops->tf_read);
  547. qc->ap->ops->tf_read(qc->ap, tf);
  548. /*
  549. * Use ata_to_sense_error() to map status register bits
  550. * onto sense key, asc & ascq.
  551. */
  552. if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  553. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  554. &sb[2], &sb[12], &sb[13]);
  555. sb[2] &= 0x0f;
  556. }
  557. sb[0] = 0x70;
  558. sb[7] = 0x0a;
  559. if (tf->flags & ATA_TFLAG_LBA48) {
  560. /* TODO: find solution for LBA48 descriptors */
  561. }
  562. else if (tf->flags & ATA_TFLAG_LBA) {
  563. /* A small (28b) LBA will fit in the 32b info field */
  564. sb[0] |= 0x80; /* set valid bit */
  565. sb[3] = tf->device & 0x0f;
  566. sb[4] = tf->lbah;
  567. sb[5] = tf->lbam;
  568. sb[6] = tf->lbal;
  569. }
  570. else {
  571. /* TODO: C/H/S */
  572. }
  573. }
  574. /**
  575. * ata_scsi_slave_config - Set SCSI device attributes
  576. * @sdev: SCSI device to examine
  577. *
  578. * This is called before we actually start reading
  579. * and writing to the device, to configure certain
  580. * SCSI mid-layer behaviors.
  581. *
  582. * LOCKING:
  583. * Defined by SCSI layer. We don't really care.
  584. */
  585. int ata_scsi_slave_config(struct scsi_device *sdev)
  586. {
  587. sdev->use_10_for_rw = 1;
  588. sdev->use_10_for_ms = 1;
  589. blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
  590. if (sdev->id < ATA_MAX_DEVICES) {
  591. struct ata_port *ap;
  592. struct ata_device *dev;
  593. ap = (struct ata_port *) &sdev->host->hostdata[0];
  594. dev = &ap->device[sdev->id];
  595. /* TODO: 1024 is an arbitrary number, not the
  596. * hardware maximum. This should be increased to
  597. * 65534 when Jens Axboe's patch for dynamically
  598. * determining max_sectors is merged.
  599. */
  600. if ((dev->flags & ATA_DFLAG_LBA48) &&
  601. ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
  602. /*
  603. * do not overwrite sdev->host->max_sectors, since
  604. * other drives on this host may not support LBA48
  605. */
  606. blk_queue_max_sectors(sdev->request_queue, 2048);
  607. }
  608. /*
  609. * SATA DMA transfers must be multiples of 4 byte, so
  610. * we need to pad ATAPI transfers using an extra sg.
  611. * Decrement max hw segments accordingly.
  612. */
  613. if (dev->class == ATA_DEV_ATAPI) {
  614. request_queue_t *q = sdev->request_queue;
  615. blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
  616. }
  617. }
  618. return 0; /* scsi layer doesn't check return value, sigh */
  619. }
  620. /**
  621. * ata_scsi_error - SCSI layer error handler callback
  622. * @host: SCSI host on which error occurred
  623. *
  624. * Handles SCSI-layer-thrown error events.
  625. *
  626. * LOCKING:
  627. * Inherited from SCSI layer (none, can sleep)
  628. *
  629. * RETURNS:
  630. * Zero.
  631. */
  632. int ata_scsi_error(struct Scsi_Host *host)
  633. {
  634. struct ata_port *ap;
  635. DPRINTK("ENTER\n");
  636. ap = (struct ata_port *) &host->hostdata[0];
  637. ap->ops->eng_timeout(ap);
  638. /* TODO: this is per-command; when queueing is supported
  639. * this code will either change or move to a more
  640. * appropriate place
  641. */
  642. host->host_failed--;
  643. INIT_LIST_HEAD(&host->eh_cmd_q);
  644. DPRINTK("EXIT\n");
  645. return 0;
  646. }
  647. /**
  648. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  649. * @qc: Storage for translated ATA taskfile
  650. * @scsicmd: SCSI command to translate
  651. *
  652. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  653. * (to start). Perhaps these commands should be preceded by
  654. * CHECK POWER MODE to see what power mode the device is already in.
  655. * [See SAT revision 5 at www.t10.org]
  656. *
  657. * LOCKING:
  658. * spin_lock_irqsave(host_set lock)
  659. *
  660. * RETURNS:
  661. * Zero on success, non-zero on error.
  662. */
  663. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
  664. const u8 *scsicmd)
  665. {
  666. struct ata_taskfile *tf = &qc->tf;
  667. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  668. tf->protocol = ATA_PROT_NODATA;
  669. if (scsicmd[1] & 0x1) {
  670. ; /* ignore IMMED bit, violates sat-r05 */
  671. }
  672. if (scsicmd[4] & 0x2)
  673. goto invalid_fld; /* LOEJ bit set not supported */
  674. if (((scsicmd[4] >> 4) & 0xf) != 0)
  675. goto invalid_fld; /* power conditions not supported */
  676. if (scsicmd[4] & 0x1) {
  677. tf->nsect = 1; /* 1 sector, lba=0 */
  678. if (qc->dev->flags & ATA_DFLAG_LBA) {
  679. qc->tf.flags |= ATA_TFLAG_LBA;
  680. tf->lbah = 0x0;
  681. tf->lbam = 0x0;
  682. tf->lbal = 0x0;
  683. tf->device |= ATA_LBA;
  684. } else {
  685. /* CHS */
  686. tf->lbal = 0x1; /* sect */
  687. tf->lbam = 0x0; /* cyl low */
  688. tf->lbah = 0x0; /* cyl high */
  689. }
  690. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  691. } else {
  692. tf->nsect = 0; /* time period value (0 implies now) */
  693. tf->command = ATA_CMD_STANDBY;
  694. /* Consider: ATA STANDBY IMMEDIATE command */
  695. }
  696. /*
  697. * Standby and Idle condition timers could be implemented but that
  698. * would require libata to implement the Power condition mode page
  699. * and allow the user to change it. Changing mode pages requires
  700. * MODE SELECT to be implemented.
  701. */
  702. return 0;
  703. invalid_fld:
  704. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  705. /* "Invalid field in cbd" */
  706. return 1;
  707. }
  708. /**
  709. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  710. * @qc: Storage for translated ATA taskfile
  711. * @scsicmd: SCSI command to translate (ignored)
  712. *
  713. * Sets up an ATA taskfile to issue FLUSH CACHE or
  714. * FLUSH CACHE EXT.
  715. *
  716. * LOCKING:
  717. * spin_lock_irqsave(host_set lock)
  718. *
  719. * RETURNS:
  720. * Zero on success, non-zero on error.
  721. */
  722. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  723. {
  724. struct ata_taskfile *tf = &qc->tf;
  725. tf->flags |= ATA_TFLAG_DEVICE;
  726. tf->protocol = ATA_PROT_NODATA;
  727. if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
  728. (ata_id_has_flush_ext(qc->dev->id)))
  729. tf->command = ATA_CMD_FLUSH_EXT;
  730. else
  731. tf->command = ATA_CMD_FLUSH;
  732. return 0;
  733. }
  734. /**
  735. * scsi_6_lba_len - Get LBA and transfer length
  736. * @scsicmd: SCSI command to translate
  737. *
  738. * Calculate LBA and transfer length for 6-byte commands.
  739. *
  740. * RETURNS:
  741. * @plba: the LBA
  742. * @plen: the transfer length
  743. */
  744. static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  745. {
  746. u64 lba = 0;
  747. u32 len = 0;
  748. VPRINTK("six-byte command\n");
  749. lba |= ((u64)scsicmd[2]) << 8;
  750. lba |= ((u64)scsicmd[3]);
  751. len |= ((u32)scsicmd[4]);
  752. *plba = lba;
  753. *plen = len;
  754. }
  755. /**
  756. * scsi_10_lba_len - Get LBA and transfer length
  757. * @scsicmd: SCSI command to translate
  758. *
  759. * Calculate LBA and transfer length for 10-byte commands.
  760. *
  761. * RETURNS:
  762. * @plba: the LBA
  763. * @plen: the transfer length
  764. */
  765. static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  766. {
  767. u64 lba = 0;
  768. u32 len = 0;
  769. VPRINTK("ten-byte command\n");
  770. lba |= ((u64)scsicmd[2]) << 24;
  771. lba |= ((u64)scsicmd[3]) << 16;
  772. lba |= ((u64)scsicmd[4]) << 8;
  773. lba |= ((u64)scsicmd[5]);
  774. len |= ((u32)scsicmd[7]) << 8;
  775. len |= ((u32)scsicmd[8]);
  776. *plba = lba;
  777. *plen = len;
  778. }
  779. /**
  780. * scsi_16_lba_len - Get LBA and transfer length
  781. * @scsicmd: SCSI command to translate
  782. *
  783. * Calculate LBA and transfer length for 16-byte commands.
  784. *
  785. * RETURNS:
  786. * @plba: the LBA
  787. * @plen: the transfer length
  788. */
  789. static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  790. {
  791. u64 lba = 0;
  792. u32 len = 0;
  793. VPRINTK("sixteen-byte command\n");
  794. lba |= ((u64)scsicmd[2]) << 56;
  795. lba |= ((u64)scsicmd[3]) << 48;
  796. lba |= ((u64)scsicmd[4]) << 40;
  797. lba |= ((u64)scsicmd[5]) << 32;
  798. lba |= ((u64)scsicmd[6]) << 24;
  799. lba |= ((u64)scsicmd[7]) << 16;
  800. lba |= ((u64)scsicmd[8]) << 8;
  801. lba |= ((u64)scsicmd[9]);
  802. len |= ((u32)scsicmd[10]) << 24;
  803. len |= ((u32)scsicmd[11]) << 16;
  804. len |= ((u32)scsicmd[12]) << 8;
  805. len |= ((u32)scsicmd[13]);
  806. *plba = lba;
  807. *plen = len;
  808. }
  809. /**
  810. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  811. * @qc: Storage for translated ATA taskfile
  812. * @scsicmd: SCSI command to translate
  813. *
  814. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  815. *
  816. * LOCKING:
  817. * spin_lock_irqsave(host_set lock)
  818. *
  819. * RETURNS:
  820. * Zero on success, non-zero on error.
  821. */
  822. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  823. {
  824. struct ata_taskfile *tf = &qc->tf;
  825. struct ata_device *dev = qc->dev;
  826. u64 dev_sectors = qc->dev->n_sectors;
  827. u64 block;
  828. u32 n_block;
  829. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  830. tf->protocol = ATA_PROT_NODATA;
  831. if (scsicmd[0] == VERIFY)
  832. scsi_10_lba_len(scsicmd, &block, &n_block);
  833. else if (scsicmd[0] == VERIFY_16)
  834. scsi_16_lba_len(scsicmd, &block, &n_block);
  835. else
  836. goto invalid_fld;
  837. if (!n_block)
  838. goto nothing_to_do;
  839. if (block >= dev_sectors)
  840. goto out_of_range;
  841. if ((block + n_block) > dev_sectors)
  842. goto out_of_range;
  843. if (dev->flags & ATA_DFLAG_LBA) {
  844. tf->flags |= ATA_TFLAG_LBA;
  845. if (dev->flags & ATA_DFLAG_LBA48) {
  846. if (n_block > (64 * 1024))
  847. goto invalid_fld;
  848. /* use LBA48 */
  849. tf->flags |= ATA_TFLAG_LBA48;
  850. tf->command = ATA_CMD_VERIFY_EXT;
  851. tf->hob_nsect = (n_block >> 8) & 0xff;
  852. tf->hob_lbah = (block >> 40) & 0xff;
  853. tf->hob_lbam = (block >> 32) & 0xff;
  854. tf->hob_lbal = (block >> 24) & 0xff;
  855. } else {
  856. if (n_block > 256)
  857. goto invalid_fld;
  858. /* use LBA28 */
  859. tf->command = ATA_CMD_VERIFY;
  860. tf->device |= (block >> 24) & 0xf;
  861. }
  862. tf->nsect = n_block & 0xff;
  863. tf->lbah = (block >> 16) & 0xff;
  864. tf->lbam = (block >> 8) & 0xff;
  865. tf->lbal = block & 0xff;
  866. tf->device |= ATA_LBA;
  867. } else {
  868. /* CHS */
  869. u32 sect, head, cyl, track;
  870. if (n_block > 256)
  871. goto invalid_fld;
  872. /* Convert LBA to CHS */
  873. track = (u32)block / dev->sectors;
  874. cyl = track / dev->heads;
  875. head = track % dev->heads;
  876. sect = (u32)block % dev->sectors + 1;
  877. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  878. (u32)block, track, cyl, head, sect);
  879. /* Check whether the converted CHS can fit.
  880. Cylinder: 0-65535
  881. Head: 0-15
  882. Sector: 1-255*/
  883. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  884. goto out_of_range;
  885. tf->command = ATA_CMD_VERIFY;
  886. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  887. tf->lbal = sect;
  888. tf->lbam = cyl;
  889. tf->lbah = cyl >> 8;
  890. tf->device |= head;
  891. }
  892. return 0;
  893. invalid_fld:
  894. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  895. /* "Invalid field in cbd" */
  896. return 1;
  897. out_of_range:
  898. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  899. /* "Logical Block Address out of range" */
  900. return 1;
  901. nothing_to_do:
  902. qc->scsicmd->result = SAM_STAT_GOOD;
  903. return 1;
  904. }
  905. /**
  906. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  907. * @qc: Storage for translated ATA taskfile
  908. * @scsicmd: SCSI command to translate
  909. *
  910. * Converts any of six SCSI read/write commands into the
  911. * ATA counterpart, including starting sector (LBA),
  912. * sector count, and taking into account the device's LBA48
  913. * support.
  914. *
  915. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  916. * %WRITE_16 are currently supported.
  917. *
  918. * LOCKING:
  919. * spin_lock_irqsave(host_set lock)
  920. *
  921. * RETURNS:
  922. * Zero on success, non-zero on error.
  923. */
  924. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  925. {
  926. struct ata_taskfile *tf = &qc->tf;
  927. struct ata_device *dev = qc->dev;
  928. u64 block;
  929. u32 n_block;
  930. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  931. if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
  932. scsicmd[0] == WRITE_16)
  933. tf->flags |= ATA_TFLAG_WRITE;
  934. /* Calculate the SCSI LBA and transfer length. */
  935. switch (scsicmd[0]) {
  936. case READ_10:
  937. case WRITE_10:
  938. scsi_10_lba_len(scsicmd, &block, &n_block);
  939. break;
  940. case READ_6:
  941. case WRITE_6:
  942. scsi_6_lba_len(scsicmd, &block, &n_block);
  943. /* for 6-byte r/w commands, transfer length 0
  944. * means 256 blocks of data, not 0 block.
  945. */
  946. if (!n_block)
  947. n_block = 256;
  948. break;
  949. case READ_16:
  950. case WRITE_16:
  951. scsi_16_lba_len(scsicmd, &block, &n_block);
  952. break;
  953. default:
  954. DPRINTK("no-byte command\n");
  955. goto invalid_fld;
  956. }
  957. /* Check and compose ATA command */
  958. if (!n_block)
  959. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  960. * length 0 means transfer 0 block of data.
  961. * However, for ATA R/W commands, sector count 0 means
  962. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  963. *
  964. * WARNING: one or two older ATA drives treat 0 as 0...
  965. */
  966. goto nothing_to_do;
  967. if (dev->flags & ATA_DFLAG_LBA) {
  968. tf->flags |= ATA_TFLAG_LBA;
  969. if (dev->flags & ATA_DFLAG_LBA48) {
  970. /* The request -may- be too large for LBA48. */
  971. if ((block >> 48) || (n_block > 65536))
  972. goto out_of_range;
  973. /* use LBA48 */
  974. tf->flags |= ATA_TFLAG_LBA48;
  975. tf->hob_nsect = (n_block >> 8) & 0xff;
  976. tf->hob_lbah = (block >> 40) & 0xff;
  977. tf->hob_lbam = (block >> 32) & 0xff;
  978. tf->hob_lbal = (block >> 24) & 0xff;
  979. } else {
  980. /* use LBA28 */
  981. /* The request -may- be too large for LBA28. */
  982. if ((block >> 28) || (n_block > 256))
  983. goto out_of_range;
  984. tf->device |= (block >> 24) & 0xf;
  985. }
  986. ata_rwcmd_protocol(qc);
  987. qc->nsect = n_block;
  988. tf->nsect = n_block & 0xff;
  989. tf->lbah = (block >> 16) & 0xff;
  990. tf->lbam = (block >> 8) & 0xff;
  991. tf->lbal = block & 0xff;
  992. tf->device |= ATA_LBA;
  993. } else {
  994. /* CHS */
  995. u32 sect, head, cyl, track;
  996. /* The request -may- be too large for CHS addressing. */
  997. if ((block >> 28) || (n_block > 256))
  998. goto out_of_range;
  999. ata_rwcmd_protocol(qc);
  1000. /* Convert LBA to CHS */
  1001. track = (u32)block / dev->sectors;
  1002. cyl = track / dev->heads;
  1003. head = track % dev->heads;
  1004. sect = (u32)block % dev->sectors + 1;
  1005. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1006. (u32)block, track, cyl, head, sect);
  1007. /* Check whether the converted CHS can fit.
  1008. Cylinder: 0-65535
  1009. Head: 0-15
  1010. Sector: 1-255*/
  1011. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1012. goto out_of_range;
  1013. qc->nsect = n_block;
  1014. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1015. tf->lbal = sect;
  1016. tf->lbam = cyl;
  1017. tf->lbah = cyl >> 8;
  1018. tf->device |= head;
  1019. }
  1020. return 0;
  1021. invalid_fld:
  1022. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1023. /* "Invalid field in cbd" */
  1024. return 1;
  1025. out_of_range:
  1026. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1027. /* "Logical Block Address out of range" */
  1028. return 1;
  1029. nothing_to_do:
  1030. qc->scsicmd->result = SAM_STAT_GOOD;
  1031. return 1;
  1032. }
  1033. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc,
  1034. unsigned int err_mask)
  1035. {
  1036. struct scsi_cmnd *cmd = qc->scsicmd;
  1037. u8 *cdb = cmd->cmnd;
  1038. int need_sense = (err_mask != 0);
  1039. /* For ATA pass thru (SAT) commands, generate a sense block if
  1040. * user mandated it or if there's an error. Note that if we
  1041. * generate because the user forced us to, a check condition
  1042. * is generated and the ATA register values are returned
  1043. * whether the command completed successfully or not. If there
  1044. * was no error, SK, ASC and ASCQ will all be zero.
  1045. */
  1046. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1047. ((cdb[2] & 0x20) || need_sense)) {
  1048. ata_gen_ata_desc_sense(qc);
  1049. } else {
  1050. if (!need_sense) {
  1051. cmd->result = SAM_STAT_GOOD;
  1052. } else {
  1053. /* TODO: decide which descriptor format to use
  1054. * for 48b LBA devices and call that here
  1055. * instead of the fixed desc, which is only
  1056. * good for smaller LBA (and maybe CHS?)
  1057. * devices.
  1058. */
  1059. ata_gen_fixed_sense(qc);
  1060. }
  1061. }
  1062. if (need_sense) {
  1063. /* The ata_gen_..._sense routines fill in tf */
  1064. ata_dump_status(qc->ap->id, &qc->tf);
  1065. }
  1066. qc->scsidone(cmd);
  1067. return 0;
  1068. }
  1069. /**
  1070. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1071. * @ap: ATA port to which the command is addressed
  1072. * @dev: ATA device to which the command is addressed
  1073. * @cmd: SCSI command to execute
  1074. * @done: SCSI command completion function
  1075. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1076. *
  1077. * Our ->queuecommand() function has decided that the SCSI
  1078. * command issued can be directly translated into an ATA
  1079. * command, rather than handled internally.
  1080. *
  1081. * This function sets up an ata_queued_cmd structure for the
  1082. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1083. *
  1084. * The xlat_func argument (actor) returns 0 if ready to execute
  1085. * ATA command, else 1 to finish translation. If 1 is returned
  1086. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1087. * to be set reflecting an error condition or clean (early)
  1088. * termination.
  1089. *
  1090. * LOCKING:
  1091. * spin_lock_irqsave(host_set lock)
  1092. */
  1093. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  1094. struct scsi_cmnd *cmd,
  1095. void (*done)(struct scsi_cmnd *),
  1096. ata_xlat_func_t xlat_func)
  1097. {
  1098. struct ata_queued_cmd *qc;
  1099. u8 *scsicmd = cmd->cmnd;
  1100. VPRINTK("ENTER\n");
  1101. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  1102. if (!qc)
  1103. goto err_mem;
  1104. /* data is present; dma-map it */
  1105. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1106. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1107. if (unlikely(cmd->request_bufflen < 1)) {
  1108. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  1109. ap->id, dev->devno);
  1110. goto err_did;
  1111. }
  1112. if (cmd->use_sg)
  1113. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  1114. else
  1115. ata_sg_init_one(qc, cmd->request_buffer,
  1116. cmd->request_bufflen);
  1117. qc->dma_dir = cmd->sc_data_direction;
  1118. }
  1119. qc->complete_fn = ata_scsi_qc_complete;
  1120. if (xlat_func(qc, scsicmd))
  1121. goto early_finish;
  1122. /* select device, send command to hardware */
  1123. if (ata_qc_issue(qc))
  1124. goto err_did;
  1125. VPRINTK("EXIT\n");
  1126. return;
  1127. early_finish:
  1128. ata_qc_free(qc);
  1129. done(cmd);
  1130. DPRINTK("EXIT - early finish (good or error)\n");
  1131. return;
  1132. err_did:
  1133. ata_qc_free(qc);
  1134. err_mem:
  1135. cmd->result = (DID_ERROR << 16);
  1136. done(cmd);
  1137. DPRINTK("EXIT - internal\n");
  1138. return;
  1139. }
  1140. /**
  1141. * ata_scsi_rbuf_get - Map response buffer.
  1142. * @cmd: SCSI command containing buffer to be mapped.
  1143. * @buf_out: Pointer to mapped area.
  1144. *
  1145. * Maps buffer contained within SCSI command @cmd.
  1146. *
  1147. * LOCKING:
  1148. * spin_lock_irqsave(host_set lock)
  1149. *
  1150. * RETURNS:
  1151. * Length of response buffer.
  1152. */
  1153. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  1154. {
  1155. u8 *buf;
  1156. unsigned int buflen;
  1157. if (cmd->use_sg) {
  1158. struct scatterlist *sg;
  1159. sg = (struct scatterlist *) cmd->request_buffer;
  1160. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  1161. buflen = sg->length;
  1162. } else {
  1163. buf = cmd->request_buffer;
  1164. buflen = cmd->request_bufflen;
  1165. }
  1166. *buf_out = buf;
  1167. return buflen;
  1168. }
  1169. /**
  1170. * ata_scsi_rbuf_put - Unmap response buffer.
  1171. * @cmd: SCSI command containing buffer to be unmapped.
  1172. * @buf: buffer to unmap
  1173. *
  1174. * Unmaps response buffer contained within @cmd.
  1175. *
  1176. * LOCKING:
  1177. * spin_lock_irqsave(host_set lock)
  1178. */
  1179. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  1180. {
  1181. if (cmd->use_sg) {
  1182. struct scatterlist *sg;
  1183. sg = (struct scatterlist *) cmd->request_buffer;
  1184. kunmap_atomic(buf - sg->offset, KM_USER0);
  1185. }
  1186. }
  1187. /**
  1188. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1189. * @args: device IDENTIFY data / SCSI command of interest.
  1190. * @actor: Callback hook for desired SCSI command simulator
  1191. *
  1192. * Takes care of the hard work of simulating a SCSI command...
  1193. * Mapping the response buffer, calling the command's handler,
  1194. * and handling the handler's return value. This return value
  1195. * indicates whether the handler wishes the SCSI command to be
  1196. * completed successfully (0), or not (in which case cmd->result
  1197. * and sense buffer are assumed to be set).
  1198. *
  1199. * LOCKING:
  1200. * spin_lock_irqsave(host_set lock)
  1201. */
  1202. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1203. unsigned int (*actor) (struct ata_scsi_args *args,
  1204. u8 *rbuf, unsigned int buflen))
  1205. {
  1206. u8 *rbuf;
  1207. unsigned int buflen, rc;
  1208. struct scsi_cmnd *cmd = args->cmd;
  1209. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  1210. memset(rbuf, 0, buflen);
  1211. rc = actor(args, rbuf, buflen);
  1212. ata_scsi_rbuf_put(cmd, rbuf);
  1213. if (rc == 0)
  1214. cmd->result = SAM_STAT_GOOD;
  1215. args->done(cmd);
  1216. }
  1217. /**
  1218. * ata_scsiop_inq_std - Simulate INQUIRY command
  1219. * @args: device IDENTIFY data / SCSI command of interest.
  1220. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1221. * @buflen: Response buffer length.
  1222. *
  1223. * Returns standard device identification data associated
  1224. * with non-EVPD INQUIRY command output.
  1225. *
  1226. * LOCKING:
  1227. * spin_lock_irqsave(host_set lock)
  1228. */
  1229. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  1230. unsigned int buflen)
  1231. {
  1232. u8 hdr[] = {
  1233. TYPE_DISK,
  1234. 0,
  1235. 0x5, /* claim SPC-3 version compatibility */
  1236. 2,
  1237. 95 - 4
  1238. };
  1239. /* set scsi removeable (RMB) bit per ata bit */
  1240. if (ata_id_removeable(args->id))
  1241. hdr[1] |= (1 << 7);
  1242. VPRINTK("ENTER\n");
  1243. memcpy(rbuf, hdr, sizeof(hdr));
  1244. if (buflen > 35) {
  1245. memcpy(&rbuf[8], "ATA ", 8);
  1246. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  1247. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  1248. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1249. memcpy(&rbuf[32], "n/a ", 4);
  1250. }
  1251. if (buflen > 63) {
  1252. const u8 versions[] = {
  1253. 0x60, /* SAM-3 (no version claimed) */
  1254. 0x03,
  1255. 0x20, /* SBC-2 (no version claimed) */
  1256. 0x02,
  1257. 0x60 /* SPC-3 (no version claimed) */
  1258. };
  1259. memcpy(rbuf + 59, versions, sizeof(versions));
  1260. }
  1261. return 0;
  1262. }
  1263. /**
  1264. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  1265. * @args: device IDENTIFY data / SCSI command of interest.
  1266. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1267. * @buflen: Response buffer length.
  1268. *
  1269. * Returns list of inquiry EVPD pages available.
  1270. *
  1271. * LOCKING:
  1272. * spin_lock_irqsave(host_set lock)
  1273. */
  1274. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  1275. unsigned int buflen)
  1276. {
  1277. const u8 pages[] = {
  1278. 0x00, /* page 0x00, this page */
  1279. 0x80, /* page 0x80, unit serial no page */
  1280. 0x83 /* page 0x83, device ident page */
  1281. };
  1282. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  1283. if (buflen > 6)
  1284. memcpy(rbuf + 4, pages, sizeof(pages));
  1285. return 0;
  1286. }
  1287. /**
  1288. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  1289. * @args: device IDENTIFY data / SCSI command of interest.
  1290. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1291. * @buflen: Response buffer length.
  1292. *
  1293. * Returns ATA device serial number.
  1294. *
  1295. * LOCKING:
  1296. * spin_lock_irqsave(host_set lock)
  1297. */
  1298. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  1299. unsigned int buflen)
  1300. {
  1301. const u8 hdr[] = {
  1302. 0,
  1303. 0x80, /* this page code */
  1304. 0,
  1305. ATA_SERNO_LEN, /* page len */
  1306. };
  1307. memcpy(rbuf, hdr, sizeof(hdr));
  1308. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  1309. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  1310. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1311. return 0;
  1312. }
  1313. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  1314. /**
  1315. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  1316. * @args: device IDENTIFY data / SCSI command of interest.
  1317. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1318. * @buflen: Response buffer length.
  1319. *
  1320. * Returns device identification. Currently hardcoded to
  1321. * return "Linux ATA-SCSI simulator".
  1322. *
  1323. * LOCKING:
  1324. * spin_lock_irqsave(host_set lock)
  1325. */
  1326. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  1327. unsigned int buflen)
  1328. {
  1329. rbuf[1] = 0x83; /* this page code */
  1330. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  1331. /* our one and only identification descriptor (vendor-specific) */
  1332. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  1333. rbuf[4 + 0] = 2; /* code set: ASCII */
  1334. rbuf[4 + 3] = strlen(inq_83_str);
  1335. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  1336. }
  1337. return 0;
  1338. }
  1339. /**
  1340. * ata_scsiop_noop - Command handler that simply returns success.
  1341. * @args: device IDENTIFY data / SCSI command of interest.
  1342. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1343. * @buflen: Response buffer length.
  1344. *
  1345. * No operation. Simply returns success to caller, to indicate
  1346. * that the caller should successfully complete this SCSI command.
  1347. *
  1348. * LOCKING:
  1349. * spin_lock_irqsave(host_set lock)
  1350. */
  1351. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  1352. unsigned int buflen)
  1353. {
  1354. VPRINTK("ENTER\n");
  1355. return 0;
  1356. }
  1357. /**
  1358. * ata_msense_push - Push data onto MODE SENSE data output buffer
  1359. * @ptr_io: (input/output) Location to store more output data
  1360. * @last: End of output data buffer
  1361. * @buf: Pointer to BLOB being added to output buffer
  1362. * @buflen: Length of BLOB
  1363. *
  1364. * Store MODE SENSE data on an output buffer.
  1365. *
  1366. * LOCKING:
  1367. * None.
  1368. */
  1369. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  1370. const u8 *buf, unsigned int buflen)
  1371. {
  1372. u8 *ptr = *ptr_io;
  1373. if ((ptr + buflen - 1) > last)
  1374. return;
  1375. memcpy(ptr, buf, buflen);
  1376. ptr += buflen;
  1377. *ptr_io = ptr;
  1378. }
  1379. /**
  1380. * ata_msense_caching - Simulate MODE SENSE caching info page
  1381. * @id: device IDENTIFY data
  1382. * @ptr_io: (input/output) Location to store more output data
  1383. * @last: End of output data buffer
  1384. *
  1385. * Generate a caching info page, which conditionally indicates
  1386. * write caching to the SCSI layer, depending on device
  1387. * capabilities.
  1388. *
  1389. * LOCKING:
  1390. * None.
  1391. */
  1392. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  1393. const u8 *last)
  1394. {
  1395. u8 page[CACHE_MPAGE_LEN];
  1396. memcpy(page, def_cache_mpage, sizeof(page));
  1397. if (ata_id_wcache_enabled(id))
  1398. page[2] |= (1 << 2); /* write cache enable */
  1399. if (!ata_id_rahead_enabled(id))
  1400. page[12] |= (1 << 5); /* disable read ahead */
  1401. ata_msense_push(ptr_io, last, page, sizeof(page));
  1402. return sizeof(page);
  1403. }
  1404. /**
  1405. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1406. * @dev: Device associated with this MODE SENSE command
  1407. * @ptr_io: (input/output) Location to store more output data
  1408. * @last: End of output data buffer
  1409. *
  1410. * Generate a generic MODE SENSE control mode page.
  1411. *
  1412. * LOCKING:
  1413. * None.
  1414. */
  1415. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  1416. {
  1417. ata_msense_push(ptr_io, last, def_control_mpage,
  1418. sizeof(def_control_mpage));
  1419. return sizeof(def_control_mpage);
  1420. }
  1421. /**
  1422. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1423. * @dev: Device associated with this MODE SENSE command
  1424. * @ptr_io: (input/output) Location to store more output data
  1425. * @last: End of output data buffer
  1426. *
  1427. * Generate a generic MODE SENSE r/w error recovery page.
  1428. *
  1429. * LOCKING:
  1430. * None.
  1431. */
  1432. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  1433. {
  1434. ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
  1435. sizeof(def_rw_recovery_mpage));
  1436. return sizeof(def_rw_recovery_mpage);
  1437. }
  1438. /**
  1439. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1440. * @args: device IDENTIFY data / SCSI command of interest.
  1441. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1442. * @buflen: Response buffer length.
  1443. *
  1444. * Simulate MODE SENSE commands. Assume this is invoked for direct
  1445. * access devices (e.g. disks) only. There should be no block
  1446. * descriptor for other device types.
  1447. *
  1448. * LOCKING:
  1449. * spin_lock_irqsave(host_set lock)
  1450. */
  1451. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1452. unsigned int buflen)
  1453. {
  1454. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1455. const u8 sat_blk_desc[] = {
  1456. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  1457. 0,
  1458. 0, 0x2, 0x0 /* block length: 512 bytes */
  1459. };
  1460. u8 pg, spg;
  1461. unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
  1462. VPRINTK("ENTER\n");
  1463. six_byte = (scsicmd[0] == MODE_SENSE);
  1464. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  1465. /*
  1466. * LLBA bit in msense(10) ignored (compliant)
  1467. */
  1468. page_control = scsicmd[2] >> 6;
  1469. switch (page_control) {
  1470. case 0: /* current */
  1471. break; /* supported */
  1472. case 3: /* saved */
  1473. goto saving_not_supp;
  1474. case 1: /* changeable */
  1475. case 2: /* defaults */
  1476. default:
  1477. goto invalid_fld;
  1478. }
  1479. if (six_byte) {
  1480. output_len = 4 + (ebd ? 8 : 0);
  1481. alloc_len = scsicmd[4];
  1482. } else {
  1483. output_len = 8 + (ebd ? 8 : 0);
  1484. alloc_len = (scsicmd[7] << 8) + scsicmd[8];
  1485. }
  1486. minlen = (alloc_len < buflen) ? alloc_len : buflen;
  1487. p = rbuf + output_len;
  1488. last = rbuf + minlen - 1;
  1489. pg = scsicmd[2] & 0x3f;
  1490. spg = scsicmd[3];
  1491. /*
  1492. * No mode subpages supported (yet) but asking for _all_
  1493. * subpages may be valid
  1494. */
  1495. if (spg && (spg != ALL_SUB_MPAGES))
  1496. goto invalid_fld;
  1497. switch(pg) {
  1498. case RW_RECOVERY_MPAGE:
  1499. output_len += ata_msense_rw_recovery(&p, last);
  1500. break;
  1501. case CACHE_MPAGE:
  1502. output_len += ata_msense_caching(args->id, &p, last);
  1503. break;
  1504. case CONTROL_MPAGE: {
  1505. output_len += ata_msense_ctl_mode(&p, last);
  1506. break;
  1507. }
  1508. case ALL_MPAGES:
  1509. output_len += ata_msense_rw_recovery(&p, last);
  1510. output_len += ata_msense_caching(args->id, &p, last);
  1511. output_len += ata_msense_ctl_mode(&p, last);
  1512. break;
  1513. default: /* invalid page code */
  1514. goto invalid_fld;
  1515. }
  1516. if (minlen < 1)
  1517. return 0;
  1518. if (six_byte) {
  1519. output_len--;
  1520. rbuf[0] = output_len;
  1521. if (ebd) {
  1522. if (minlen > 3)
  1523. rbuf[3] = sizeof(sat_blk_desc);
  1524. if (minlen > 11)
  1525. memcpy(rbuf + 4, sat_blk_desc,
  1526. sizeof(sat_blk_desc));
  1527. }
  1528. } else {
  1529. output_len -= 2;
  1530. rbuf[0] = output_len >> 8;
  1531. if (minlen > 1)
  1532. rbuf[1] = output_len;
  1533. if (ebd) {
  1534. if (minlen > 7)
  1535. rbuf[7] = sizeof(sat_blk_desc);
  1536. if (minlen > 15)
  1537. memcpy(rbuf + 8, sat_blk_desc,
  1538. sizeof(sat_blk_desc));
  1539. }
  1540. }
  1541. return 0;
  1542. invalid_fld:
  1543. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1544. /* "Invalid field in cbd" */
  1545. return 1;
  1546. saving_not_supp:
  1547. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  1548. /* "Saving parameters not supported" */
  1549. return 1;
  1550. }
  1551. /**
  1552. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1553. * @args: device IDENTIFY data / SCSI command of interest.
  1554. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1555. * @buflen: Response buffer length.
  1556. *
  1557. * Simulate READ CAPACITY commands.
  1558. *
  1559. * LOCKING:
  1560. * spin_lock_irqsave(host_set lock)
  1561. */
  1562. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1563. unsigned int buflen)
  1564. {
  1565. u64 n_sectors;
  1566. u32 tmp;
  1567. VPRINTK("ENTER\n");
  1568. if (ata_id_has_lba(args->id)) {
  1569. if (ata_id_has_lba48(args->id))
  1570. n_sectors = ata_id_u64(args->id, 100);
  1571. else
  1572. n_sectors = ata_id_u32(args->id, 60);
  1573. } else {
  1574. /* CHS default translation */
  1575. n_sectors = args->id[1] * args->id[3] * args->id[6];
  1576. if (ata_id_current_chs_valid(args->id))
  1577. /* CHS current translation */
  1578. n_sectors = ata_id_u32(args->id, 57);
  1579. }
  1580. n_sectors--; /* ATA TotalUserSectors - 1 */
  1581. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1582. if( n_sectors >= 0xffffffffULL )
  1583. tmp = 0xffffffff ; /* Return max count on overflow */
  1584. else
  1585. tmp = n_sectors ;
  1586. /* sector count, 32-bit */
  1587. rbuf[0] = tmp >> (8 * 3);
  1588. rbuf[1] = tmp >> (8 * 2);
  1589. rbuf[2] = tmp >> (8 * 1);
  1590. rbuf[3] = tmp;
  1591. /* sector size */
  1592. tmp = ATA_SECT_SIZE;
  1593. rbuf[6] = tmp >> 8;
  1594. rbuf[7] = tmp;
  1595. } else {
  1596. /* sector count, 64-bit */
  1597. tmp = n_sectors >> (8 * 4);
  1598. rbuf[2] = tmp >> (8 * 3);
  1599. rbuf[3] = tmp >> (8 * 2);
  1600. rbuf[4] = tmp >> (8 * 1);
  1601. rbuf[5] = tmp;
  1602. tmp = n_sectors;
  1603. rbuf[6] = tmp >> (8 * 3);
  1604. rbuf[7] = tmp >> (8 * 2);
  1605. rbuf[8] = tmp >> (8 * 1);
  1606. rbuf[9] = tmp;
  1607. /* sector size */
  1608. tmp = ATA_SECT_SIZE;
  1609. rbuf[12] = tmp >> 8;
  1610. rbuf[13] = tmp;
  1611. }
  1612. return 0;
  1613. }
  1614. /**
  1615. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1616. * @args: device IDENTIFY data / SCSI command of interest.
  1617. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1618. * @buflen: Response buffer length.
  1619. *
  1620. * Simulate REPORT LUNS command.
  1621. *
  1622. * LOCKING:
  1623. * spin_lock_irqsave(host_set lock)
  1624. */
  1625. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1626. unsigned int buflen)
  1627. {
  1628. VPRINTK("ENTER\n");
  1629. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1630. return 0;
  1631. }
  1632. /**
  1633. * ata_scsi_set_sense - Set SCSI sense data and status
  1634. * @cmd: SCSI request to be handled
  1635. * @sk: SCSI-defined sense key
  1636. * @asc: SCSI-defined additional sense code
  1637. * @ascq: SCSI-defined additional sense code qualifier
  1638. *
  1639. * Helper function that builds a valid fixed format, current
  1640. * response code and the given sense key (sk), additional sense
  1641. * code (asc) and additional sense code qualifier (ascq) with
  1642. * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
  1643. * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
  1644. *
  1645. * LOCKING:
  1646. * Not required
  1647. */
  1648. void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  1649. {
  1650. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  1651. cmd->sense_buffer[0] = 0x70; /* fixed format, current */
  1652. cmd->sense_buffer[2] = sk;
  1653. cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
  1654. cmd->sense_buffer[12] = asc;
  1655. cmd->sense_buffer[13] = ascq;
  1656. }
  1657. /**
  1658. * ata_scsi_badcmd - End a SCSI request with an error
  1659. * @cmd: SCSI request to be handled
  1660. * @done: SCSI command completion function
  1661. * @asc: SCSI-defined additional sense code
  1662. * @ascq: SCSI-defined additional sense code qualifier
  1663. *
  1664. * Helper function that completes a SCSI command with
  1665. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1666. * and the specified additional sense codes.
  1667. *
  1668. * LOCKING:
  1669. * spin_lock_irqsave(host_set lock)
  1670. */
  1671. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1672. {
  1673. DPRINTK("ENTER\n");
  1674. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
  1675. done(cmd);
  1676. }
  1677. static int atapi_sense_complete(struct ata_queued_cmd *qc,unsigned int err_mask)
  1678. {
  1679. if (err_mask && ((err_mask & AC_ERR_DEV) == 0))
  1680. /* FIXME: not quite right; we don't want the
  1681. * translation of taskfile registers into
  1682. * a sense descriptors, since that's only
  1683. * correct for ATA, not ATAPI
  1684. */
  1685. ata_gen_ata_desc_sense(qc);
  1686. qc->scsidone(qc->scsicmd);
  1687. return 0;
  1688. }
  1689. /* is it pointless to prefer PIO for "safety reasons"? */
  1690. static inline int ata_pio_use_silly(struct ata_port *ap)
  1691. {
  1692. return (ap->flags & ATA_FLAG_PIO_DMA);
  1693. }
  1694. static void atapi_request_sense(struct ata_queued_cmd *qc)
  1695. {
  1696. struct ata_port *ap = qc->ap;
  1697. struct scsi_cmnd *cmd = qc->scsicmd;
  1698. DPRINTK("ATAPI request sense\n");
  1699. /* FIXME: is this needed? */
  1700. memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  1701. ap->ops->tf_read(ap, &qc->tf);
  1702. /* fill these in, for the case where they are -not- overwritten */
  1703. cmd->sense_buffer[0] = 0x70;
  1704. cmd->sense_buffer[2] = qc->tf.feature >> 4;
  1705. ata_qc_reinit(qc);
  1706. ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
  1707. qc->dma_dir = DMA_FROM_DEVICE;
  1708. memset(&qc->cdb, 0, ap->cdb_len);
  1709. qc->cdb[0] = REQUEST_SENSE;
  1710. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1711. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1712. qc->tf.command = ATA_CMD_PACKET;
  1713. if (ata_pio_use_silly(ap)) {
  1714. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1715. qc->tf.feature |= ATAPI_PKT_DMA;
  1716. } else {
  1717. qc->tf.protocol = ATA_PROT_ATAPI;
  1718. qc->tf.lbam = (8 * 1024) & 0xff;
  1719. qc->tf.lbah = (8 * 1024) >> 8;
  1720. }
  1721. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  1722. qc->complete_fn = atapi_sense_complete;
  1723. if (ata_qc_issue(qc))
  1724. ata_qc_complete(qc, AC_ERR_OTHER);
  1725. DPRINTK("EXIT\n");
  1726. }
  1727. static int atapi_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
  1728. {
  1729. struct scsi_cmnd *cmd = qc->scsicmd;
  1730. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  1731. if (unlikely(err_mask & AC_ERR_DEV)) {
  1732. cmd->result = SAM_STAT_CHECK_CONDITION;
  1733. atapi_request_sense(qc);
  1734. return 1;
  1735. }
  1736. else if (unlikely(err_mask))
  1737. /* FIXME: not quite right; we don't want the
  1738. * translation of taskfile registers into
  1739. * a sense descriptors, since that's only
  1740. * correct for ATA, not ATAPI
  1741. */
  1742. ata_gen_ata_desc_sense(qc);
  1743. else {
  1744. u8 *scsicmd = cmd->cmnd;
  1745. if (scsicmd[0] == INQUIRY) {
  1746. u8 *buf = NULL;
  1747. unsigned int buflen;
  1748. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1749. /* ATAPI devices typically report zero for their SCSI version,
  1750. * and sometimes deviate from the spec WRT response data
  1751. * format. If SCSI version is reported as zero like normal,
  1752. * then we make the following fixups: 1) Fake MMC-5 version,
  1753. * to indicate to the Linux scsi midlayer this is a modern
  1754. * device. 2) Ensure response data format / ATAPI information
  1755. * are always correct.
  1756. */
  1757. /* FIXME: do we ever override EVPD pages and the like, with
  1758. * this code?
  1759. */
  1760. if (buf[2] == 0) {
  1761. buf[2] = 0x5;
  1762. buf[3] = 0x32;
  1763. }
  1764. ata_scsi_rbuf_put(cmd, buf);
  1765. }
  1766. cmd->result = SAM_STAT_GOOD;
  1767. }
  1768. qc->scsidone(cmd);
  1769. return 0;
  1770. }
  1771. /**
  1772. * atapi_xlat - Initialize PACKET taskfile
  1773. * @qc: command structure to be initialized
  1774. * @scsicmd: SCSI CDB associated with this PACKET command
  1775. *
  1776. * LOCKING:
  1777. * spin_lock_irqsave(host_set lock)
  1778. *
  1779. * RETURNS:
  1780. * Zero on success, non-zero on failure.
  1781. */
  1782. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  1783. {
  1784. struct scsi_cmnd *cmd = qc->scsicmd;
  1785. struct ata_device *dev = qc->dev;
  1786. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1787. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1788. if (!using_pio)
  1789. /* Check whether ATAPI DMA is safe */
  1790. if (ata_check_atapi_dma(qc))
  1791. using_pio = 1;
  1792. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1793. qc->complete_fn = atapi_qc_complete;
  1794. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1795. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1796. qc->tf.flags |= ATA_TFLAG_WRITE;
  1797. DPRINTK("direction: write\n");
  1798. }
  1799. qc->tf.command = ATA_CMD_PACKET;
  1800. /* no data, or PIO data xfer */
  1801. if (using_pio || nodata) {
  1802. if (nodata)
  1803. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1804. else
  1805. qc->tf.protocol = ATA_PROT_ATAPI;
  1806. qc->tf.lbam = (8 * 1024) & 0xff;
  1807. qc->tf.lbah = (8 * 1024) >> 8;
  1808. }
  1809. /* DMA data xfer */
  1810. else {
  1811. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1812. qc->tf.feature |= ATAPI_PKT_DMA;
  1813. #ifdef ATAPI_ENABLE_DMADIR
  1814. /* some SATA bridges need us to indicate data xfer direction */
  1815. if (cmd->sc_data_direction != DMA_TO_DEVICE)
  1816. qc->tf.feature |= ATAPI_DMADIR;
  1817. #endif
  1818. }
  1819. qc->nbytes = cmd->bufflen;
  1820. return 0;
  1821. }
  1822. /**
  1823. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1824. * @ap: ATA port to which the device is attached
  1825. * @scsidev: SCSI device from which we derive the ATA device
  1826. *
  1827. * Given various information provided in struct scsi_cmnd,
  1828. * map that onto an ATA bus, and using that mapping
  1829. * determine which ata_device is associated with the
  1830. * SCSI command to be sent.
  1831. *
  1832. * LOCKING:
  1833. * spin_lock_irqsave(host_set lock)
  1834. *
  1835. * RETURNS:
  1836. * Associated ATA device, or %NULL if not found.
  1837. */
  1838. static struct ata_device *
  1839. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  1840. {
  1841. struct ata_device *dev;
  1842. /* skip commands not addressed to targets we simulate */
  1843. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1844. dev = &ap->device[scsidev->id];
  1845. else
  1846. return NULL;
  1847. if (unlikely((scsidev->channel != 0) ||
  1848. (scsidev->lun != 0)))
  1849. return NULL;
  1850. if (unlikely(!ata_dev_present(dev)))
  1851. return NULL;
  1852. if (!atapi_enabled) {
  1853. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1854. return NULL;
  1855. }
  1856. return dev;
  1857. }
  1858. /*
  1859. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  1860. * @byte1: Byte 1 from pass-thru CDB.
  1861. *
  1862. * RETURNS:
  1863. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  1864. */
  1865. static u8
  1866. ata_scsi_map_proto(u8 byte1)
  1867. {
  1868. switch((byte1 & 0x1e) >> 1) {
  1869. case 3: /* Non-data */
  1870. return ATA_PROT_NODATA;
  1871. case 6: /* DMA */
  1872. return ATA_PROT_DMA;
  1873. case 4: /* PIO Data-in */
  1874. case 5: /* PIO Data-out */
  1875. if (byte1 & 0xe0) {
  1876. return ATA_PROT_PIO_MULT;
  1877. }
  1878. return ATA_PROT_PIO;
  1879. case 10: /* Device Reset */
  1880. case 0: /* Hard Reset */
  1881. case 1: /* SRST */
  1882. case 2: /* Bus Idle */
  1883. case 7: /* Packet */
  1884. case 8: /* DMA Queued */
  1885. case 9: /* Device Diagnostic */
  1886. case 11: /* UDMA Data-in */
  1887. case 12: /* UDMA Data-Out */
  1888. case 13: /* FPDMA */
  1889. default: /* Reserved */
  1890. break;
  1891. }
  1892. return ATA_PROT_UNKNOWN;
  1893. }
  1894. /**
  1895. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  1896. * @qc: command structure to be initialized
  1897. * @scsicmd: SCSI command to convert
  1898. *
  1899. * Handles either 12 or 16-byte versions of the CDB.
  1900. *
  1901. * RETURNS:
  1902. * Zero on success, non-zero on failure.
  1903. */
  1904. static unsigned int
  1905. ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
  1906. {
  1907. struct ata_taskfile *tf = &(qc->tf);
  1908. struct scsi_cmnd *cmd = qc->scsicmd;
  1909. if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
  1910. goto invalid_fld;
  1911. /*
  1912. * 12 and 16 byte CDBs use different offsets to
  1913. * provide the various register values.
  1914. */
  1915. if (scsicmd[0] == ATA_16) {
  1916. /*
  1917. * 16-byte CDB - may contain extended commands.
  1918. *
  1919. * If that is the case, copy the upper byte register values.
  1920. */
  1921. if (scsicmd[1] & 0x01) {
  1922. tf->hob_feature = scsicmd[3];
  1923. tf->hob_nsect = scsicmd[5];
  1924. tf->hob_lbal = scsicmd[7];
  1925. tf->hob_lbam = scsicmd[9];
  1926. tf->hob_lbah = scsicmd[11];
  1927. tf->flags |= ATA_TFLAG_LBA48;
  1928. } else
  1929. tf->flags &= ~ATA_TFLAG_LBA48;
  1930. /*
  1931. * Always copy low byte, device and command registers.
  1932. */
  1933. tf->feature = scsicmd[4];
  1934. tf->nsect = scsicmd[6];
  1935. tf->lbal = scsicmd[8];
  1936. tf->lbam = scsicmd[10];
  1937. tf->lbah = scsicmd[12];
  1938. tf->device = scsicmd[13];
  1939. tf->command = scsicmd[14];
  1940. } else {
  1941. /*
  1942. * 12-byte CDB - incapable of extended commands.
  1943. */
  1944. tf->flags &= ~ATA_TFLAG_LBA48;
  1945. tf->feature = scsicmd[3];
  1946. tf->nsect = scsicmd[4];
  1947. tf->lbal = scsicmd[5];
  1948. tf->lbam = scsicmd[6];
  1949. tf->lbah = scsicmd[7];
  1950. tf->device = scsicmd[8];
  1951. tf->command = scsicmd[9];
  1952. }
  1953. /*
  1954. * If slave is possible, enforce correct master/slave bit
  1955. */
  1956. if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
  1957. tf->device = qc->dev->devno ?
  1958. tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
  1959. /*
  1960. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  1961. * SET_FEATURES - XFER MODE must be preceded/succeeded
  1962. * by an update to hardware-specific registers for each
  1963. * controller (i.e. the reason for ->set_piomode(),
  1964. * ->set_dmamode(), and ->post_set_mode() hooks).
  1965. */
  1966. if ((tf->command == ATA_CMD_SET_FEATURES)
  1967. && (tf->feature == SETFEATURES_XFER))
  1968. goto invalid_fld;
  1969. /*
  1970. * Set flags so that all registers will be written,
  1971. * and pass on write indication (used for PIO/DMA
  1972. * setup.)
  1973. */
  1974. tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
  1975. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1976. tf->flags |= ATA_TFLAG_WRITE;
  1977. /*
  1978. * Set transfer length.
  1979. *
  1980. * TODO: find out if we need to do more here to
  1981. * cover scatter/gather case.
  1982. */
  1983. qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
  1984. return 0;
  1985. invalid_fld:
  1986. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
  1987. /* "Invalid field in cdb" */
  1988. return 1;
  1989. }
  1990. /**
  1991. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1992. * @dev: ATA device
  1993. * @cmd: SCSI command opcode to consider
  1994. *
  1995. * Look up the SCSI command given, and determine whether the
  1996. * SCSI command is to be translated or simulated.
  1997. *
  1998. * RETURNS:
  1999. * Pointer to translation function if possible, %NULL if not.
  2000. */
  2001. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  2002. {
  2003. switch (cmd) {
  2004. case READ_6:
  2005. case READ_10:
  2006. case READ_16:
  2007. case WRITE_6:
  2008. case WRITE_10:
  2009. case WRITE_16:
  2010. return ata_scsi_rw_xlat;
  2011. case SYNCHRONIZE_CACHE:
  2012. if (ata_try_flush_cache(dev))
  2013. return ata_scsi_flush_xlat;
  2014. break;
  2015. case VERIFY:
  2016. case VERIFY_16:
  2017. return ata_scsi_verify_xlat;
  2018. case ATA_12:
  2019. case ATA_16:
  2020. return ata_scsi_pass_thru;
  2021. case START_STOP:
  2022. return ata_scsi_start_stop_xlat;
  2023. }
  2024. return NULL;
  2025. }
  2026. /**
  2027. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2028. * @ap: ATA port to which the command was being sent
  2029. * @cmd: SCSI command to dump
  2030. *
  2031. * Prints the contents of a SCSI command via printk().
  2032. */
  2033. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2034. struct scsi_cmnd *cmd)
  2035. {
  2036. #ifdef ATA_DEBUG
  2037. struct scsi_device *scsidev = cmd->device;
  2038. u8 *scsicmd = cmd->cmnd;
  2039. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2040. ap->id,
  2041. scsidev->channel, scsidev->id, scsidev->lun,
  2042. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2043. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2044. scsicmd[8]);
  2045. #endif
  2046. }
  2047. /**
  2048. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  2049. * @cmd: SCSI command to be sent
  2050. * @done: Completion function, called when command is complete
  2051. *
  2052. * In some cases, this function translates SCSI commands into
  2053. * ATA taskfiles, and queues the taskfiles to be sent to
  2054. * hardware. In other cases, this function simulates a
  2055. * SCSI device by evaluating and responding to certain
  2056. * SCSI commands. This creates the overall effect of
  2057. * ATA and ATAPI devices appearing as SCSI devices.
  2058. *
  2059. * LOCKING:
  2060. * Releases scsi-layer-held lock, and obtains host_set lock.
  2061. *
  2062. * RETURNS:
  2063. * Zero.
  2064. */
  2065. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  2066. {
  2067. struct ata_port *ap;
  2068. struct ata_device *dev;
  2069. struct scsi_device *scsidev = cmd->device;
  2070. struct Scsi_Host *shost = scsidev->host;
  2071. ap = (struct ata_port *) &shost->hostdata[0];
  2072. spin_unlock(shost->host_lock);
  2073. spin_lock(&ap->host_set->lock);
  2074. ata_scsi_dump_cdb(ap, cmd);
  2075. dev = ata_scsi_find_dev(ap, scsidev);
  2076. if (unlikely(!dev)) {
  2077. cmd->result = (DID_BAD_TARGET << 16);
  2078. done(cmd);
  2079. goto out_unlock;
  2080. }
  2081. if (dev->class == ATA_DEV_ATA) {
  2082. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  2083. cmd->cmnd[0]);
  2084. if (xlat_func)
  2085. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  2086. else
  2087. ata_scsi_simulate(dev->id, cmd, done);
  2088. } else
  2089. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  2090. out_unlock:
  2091. spin_unlock(&ap->host_set->lock);
  2092. spin_lock(shost->host_lock);
  2093. return 0;
  2094. }
  2095. /**
  2096. * ata_scsi_simulate - simulate SCSI command on ATA device
  2097. * @id: current IDENTIFY data for target device.
  2098. * @cmd: SCSI command being sent to device.
  2099. * @done: SCSI command completion function.
  2100. *
  2101. * Interprets and directly executes a select list of SCSI commands
  2102. * that can be handled internally.
  2103. *
  2104. * LOCKING:
  2105. * spin_lock_irqsave(host_set lock)
  2106. */
  2107. void ata_scsi_simulate(u16 *id,
  2108. struct scsi_cmnd *cmd,
  2109. void (*done)(struct scsi_cmnd *))
  2110. {
  2111. struct ata_scsi_args args;
  2112. const u8 *scsicmd = cmd->cmnd;
  2113. args.id = id;
  2114. args.cmd = cmd;
  2115. args.done = done;
  2116. switch(scsicmd[0]) {
  2117. /* no-op's, complete with success */
  2118. case SYNCHRONIZE_CACHE:
  2119. case REZERO_UNIT:
  2120. case SEEK_6:
  2121. case SEEK_10:
  2122. case TEST_UNIT_READY:
  2123. case FORMAT_UNIT: /* FIXME: correct? */
  2124. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  2125. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2126. break;
  2127. case INQUIRY:
  2128. if (scsicmd[1] & 2) /* is CmdDt set? */
  2129. ata_scsi_invalid_field(cmd, done);
  2130. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  2131. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  2132. else if (scsicmd[2] == 0x00)
  2133. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  2134. else if (scsicmd[2] == 0x80)
  2135. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  2136. else if (scsicmd[2] == 0x83)
  2137. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  2138. else
  2139. ata_scsi_invalid_field(cmd, done);
  2140. break;
  2141. case MODE_SENSE:
  2142. case MODE_SENSE_10:
  2143. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  2144. break;
  2145. case MODE_SELECT: /* unconditionally return */
  2146. case MODE_SELECT_10: /* bad-field-in-cdb */
  2147. ata_scsi_invalid_field(cmd, done);
  2148. break;
  2149. case READ_CAPACITY:
  2150. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2151. break;
  2152. case SERVICE_ACTION_IN:
  2153. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  2154. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2155. else
  2156. ata_scsi_invalid_field(cmd, done);
  2157. break;
  2158. case REPORT_LUNS:
  2159. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  2160. break;
  2161. /* mandatory commands we haven't implemented yet */
  2162. case REQUEST_SENSE:
  2163. /* all other commands */
  2164. default:
  2165. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  2166. /* "Invalid command operation code" */
  2167. done(cmd);
  2168. break;
  2169. }
  2170. }
  2171. void ata_scsi_scan_host(struct ata_port *ap)
  2172. {
  2173. struct ata_device *dev;
  2174. unsigned int i;
  2175. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  2176. return;
  2177. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2178. dev = &ap->device[i];
  2179. if (ata_dev_present(dev))
  2180. scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
  2181. }
  2182. }