libata-scsi.c 68 KB

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