libata-scsi.c 64 KB

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