libata-scsi.c 65 KB

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