libata-scsi.c 68 KB

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