libata-scsi.c 68 KB

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