libata-scsi.c 69 KB

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