libata-scsi.c 62 KB

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