libata-scsi.c 66 KB

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