libata-scsi.c 68 KB

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