libata-scsi.c 62 KB

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