libata-scsi.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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 <asm/uaccess.h>
  43. #include "libata.h"
  44. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
  45. static struct ata_device *
  46. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
  47. /**
  48. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  49. * @sdev: SCSI device for which BIOS geometry is to be determined
  50. * @bdev: block device associated with @sdev
  51. * @capacity: capacity of SCSI device
  52. * @geom: location to which geometry will be output
  53. *
  54. * Generic bios head/sector/cylinder calculator
  55. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  56. * mapping. Some situations may arise where the disk is not
  57. * bootable if this is not used.
  58. *
  59. * LOCKING:
  60. * Defined by the SCSI layer. We don't really care.
  61. *
  62. * RETURNS:
  63. * Zero.
  64. */
  65. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  66. sector_t capacity, int geom[])
  67. {
  68. geom[0] = 255;
  69. geom[1] = 63;
  70. sector_div(capacity, 255*63);
  71. geom[2] = capacity;
  72. return 0;
  73. }
  74. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  75. {
  76. struct ata_port *ap;
  77. struct ata_device *dev;
  78. int val = -EINVAL, rc = -EINVAL;
  79. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  80. if (!ap)
  81. goto out;
  82. dev = ata_scsi_find_dev(ap, scsidev);
  83. if (!dev) {
  84. rc = -ENODEV;
  85. goto out;
  86. }
  87. switch (cmd) {
  88. case ATA_IOC_GET_IO32:
  89. val = 0;
  90. if (copy_to_user(arg, &val, 1))
  91. return -EFAULT;
  92. return 0;
  93. case ATA_IOC_SET_IO32:
  94. val = (unsigned long) arg;
  95. if (val != 0)
  96. return -EINVAL;
  97. return 0;
  98. default:
  99. rc = -ENOTTY;
  100. break;
  101. }
  102. out:
  103. return rc;
  104. }
  105. /**
  106. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  107. * @ap: ATA port to which the new command is attached
  108. * @dev: ATA device to which the new command is attached
  109. * @cmd: SCSI command that originated this ATA command
  110. * @done: SCSI command completion function
  111. *
  112. * Obtain a reference to an unused ata_queued_cmd structure,
  113. * which is the basic libata structure representing a single
  114. * ATA command sent to the hardware.
  115. *
  116. * If a command was available, fill in the SCSI-specific
  117. * portions of the structure with information on the
  118. * current command.
  119. *
  120. * LOCKING:
  121. * spin_lock_irqsave(host_set lock)
  122. *
  123. * RETURNS:
  124. * Command allocated, or %NULL if none available.
  125. */
  126. struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
  127. struct ata_device *dev,
  128. struct scsi_cmnd *cmd,
  129. void (*done)(struct scsi_cmnd *))
  130. {
  131. struct ata_queued_cmd *qc;
  132. qc = ata_qc_new_init(ap, dev);
  133. if (qc) {
  134. qc->scsicmd = cmd;
  135. qc->scsidone = done;
  136. if (cmd->use_sg) {
  137. qc->sg = (struct scatterlist *) cmd->request_buffer;
  138. qc->n_elem = cmd->use_sg;
  139. } else {
  140. qc->sg = &qc->sgent;
  141. qc->n_elem = 1;
  142. }
  143. } else {
  144. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  145. done(cmd);
  146. }
  147. return qc;
  148. }
  149. /**
  150. * ata_to_sense_error - convert ATA error to SCSI error
  151. * @qc: Command that we are erroring out
  152. * @drv_stat: value contained in ATA status register
  153. *
  154. * Converts an ATA error into a SCSI error. While we are at it
  155. * we decode and dump the ATA error for the user so that they
  156. * have some idea what really happened at the non make-believe
  157. * layer.
  158. *
  159. * LOCKING:
  160. * spin_lock_irqsave(host_set lock)
  161. */
  162. void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
  163. {
  164. struct scsi_cmnd *cmd = qc->scsicmd;
  165. u8 err = 0;
  166. unsigned char *sb = cmd->sense_buffer;
  167. /* Based on the 3ware driver translation table */
  168. static unsigned char sense_table[][4] = {
  169. /* BBD|ECC|ID|MAR */
  170. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  171. /* BBD|ECC|ID */
  172. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  173. /* ECC|MC|MARK */
  174. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  175. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  176. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  177. /* MC|ID|ABRT|TRK0|MARK */
  178. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  179. /* MCR|MARK */
  180. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  181. /* Bad address mark */
  182. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  183. /* TRK0 */
  184. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  185. /* Abort & !ICRC */
  186. {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
  187. /* Media change request */
  188. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  189. /* SRV */
  190. {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
  191. /* Media change */
  192. {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
  193. /* ECC */
  194. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  195. /* BBD - block marked bad */
  196. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  197. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  198. };
  199. static unsigned char stat_table[][4] = {
  200. /* Must be first because BUSY means no other bits valid */
  201. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  202. {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
  203. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  204. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  205. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  206. };
  207. int i = 0;
  208. cmd->result = SAM_STAT_CHECK_CONDITION;
  209. /*
  210. * Is this an error we can process/parse
  211. */
  212. if(drv_stat & ATA_ERR)
  213. /* Read the err bits */
  214. err = ata_chk_err(qc->ap);
  215. /* Display the ATA level error info */
  216. printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat);
  217. if(drv_stat & 0x80)
  218. {
  219. printk("Busy ");
  220. err = 0; /* Data is not valid in this case */
  221. }
  222. else {
  223. if(drv_stat & 0x40) printk("DriveReady ");
  224. if(drv_stat & 0x20) printk("DeviceFault ");
  225. if(drv_stat & 0x10) printk("SeekComplete ");
  226. if(drv_stat & 0x08) printk("DataRequest ");
  227. if(drv_stat & 0x04) printk("CorrectedError ");
  228. if(drv_stat & 0x02) printk("Index ");
  229. if(drv_stat & 0x01) printk("Error ");
  230. }
  231. printk("}\n");
  232. if(err)
  233. {
  234. printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err);
  235. if(err & 0x04) printk("DriveStatusError ");
  236. if(err & 0x80)
  237. {
  238. if(err & 0x04)
  239. printk("BadCRC ");
  240. else
  241. printk("Sector ");
  242. }
  243. if(err & 0x40) printk("UncorrectableError ");
  244. if(err & 0x10) printk("SectorIdNotFound ");
  245. if(err & 0x02) printk("TrackZeroNotFound ");
  246. if(err & 0x01) printk("AddrMarkNotFound ");
  247. printk("}\n");
  248. /* Should we dump sector info here too ?? */
  249. }
  250. /* Look for err */
  251. while(sense_table[i][0] != 0xFF)
  252. {
  253. /* Look for best matches first */
  254. if((sense_table[i][0] & err) == sense_table[i][0])
  255. {
  256. sb[0] = 0x70;
  257. sb[2] = sense_table[i][1];
  258. sb[7] = 0x0a;
  259. sb[12] = sense_table[i][2];
  260. sb[13] = sense_table[i][3];
  261. return;
  262. }
  263. i++;
  264. }
  265. /* No immediate match */
  266. if(err)
  267. printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err);
  268. i = 0;
  269. /* Fall back to interpreting status bits */
  270. while(stat_table[i][0] != 0xFF)
  271. {
  272. if(stat_table[i][0] & drv_stat)
  273. {
  274. sb[0] = 0x70;
  275. sb[2] = stat_table[i][1];
  276. sb[7] = 0x0a;
  277. sb[12] = stat_table[i][2];
  278. sb[13] = stat_table[i][3];
  279. return;
  280. }
  281. i++;
  282. }
  283. /* No error ?? */
  284. printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
  285. /* additional-sense-code[-qualifier] */
  286. sb[0] = 0x70;
  287. sb[2] = MEDIUM_ERROR;
  288. sb[7] = 0x0A;
  289. if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  290. sb[12] = 0x11; /* "unrecovered read error" */
  291. sb[13] = 0x04;
  292. } else {
  293. sb[12] = 0x0C; /* "write error - */
  294. sb[13] = 0x02; /* auto-reallocation failed" */
  295. }
  296. }
  297. /**
  298. * ata_scsi_slave_config - Set SCSI device attributes
  299. * @sdev: SCSI device to examine
  300. *
  301. * This is called before we actually start reading
  302. * and writing to the device, to configure certain
  303. * SCSI mid-layer behaviors.
  304. *
  305. * LOCKING:
  306. * Defined by SCSI layer. We don't really care.
  307. */
  308. int ata_scsi_slave_config(struct scsi_device *sdev)
  309. {
  310. sdev->use_10_for_rw = 1;
  311. sdev->use_10_for_ms = 1;
  312. blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
  313. if (sdev->id < ATA_MAX_DEVICES) {
  314. struct ata_port *ap;
  315. struct ata_device *dev;
  316. ap = (struct ata_port *) &sdev->host->hostdata[0];
  317. dev = &ap->device[sdev->id];
  318. /* TODO: 1024 is an arbitrary number, not the
  319. * hardware maximum. This should be increased to
  320. * 65534 when Jens Axboe's patch for dynamically
  321. * determining max_sectors is merged.
  322. */
  323. if ((dev->flags & ATA_DFLAG_LBA48) &&
  324. ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
  325. /*
  326. * do not overwrite sdev->host->max_sectors, since
  327. * other drives on this host may not support LBA48
  328. */
  329. blk_queue_max_sectors(sdev->request_queue, 2048);
  330. }
  331. }
  332. return 0; /* scsi layer doesn't check return value, sigh */
  333. }
  334. /**
  335. * ata_scsi_error - SCSI layer error handler callback
  336. * @host: SCSI host on which error occurred
  337. *
  338. * Handles SCSI-layer-thrown error events.
  339. *
  340. * LOCKING:
  341. * Inherited from SCSI layer (none, can sleep)
  342. *
  343. * RETURNS:
  344. * Zero.
  345. */
  346. int ata_scsi_error(struct Scsi_Host *host)
  347. {
  348. struct ata_port *ap;
  349. DPRINTK("ENTER\n");
  350. ap = (struct ata_port *) &host->hostdata[0];
  351. ap->ops->eng_timeout(ap);
  352. /* TODO: this is per-command; when queueing is supported
  353. * this code will either change or move to a more
  354. * appropriate place
  355. */
  356. host->host_failed--;
  357. INIT_LIST_HEAD(&host->eh_cmd_q);
  358. DPRINTK("EXIT\n");
  359. return 0;
  360. }
  361. /**
  362. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  363. * @qc: Storage for translated ATA taskfile
  364. * @scsicmd: SCSI command to translate
  365. *
  366. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  367. * (to start). Perhaps these commands should be preceded by
  368. * CHECK POWER MODE to see what power mode the device is already in.
  369. * [See SAT revision 5 at www.t10.org]
  370. *
  371. * LOCKING:
  372. * spin_lock_irqsave(host_set lock)
  373. *
  374. * RETURNS:
  375. * Zero on success, non-zero on error.
  376. */
  377. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
  378. u8 *scsicmd)
  379. {
  380. struct ata_taskfile *tf = &qc->tf;
  381. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  382. tf->protocol = ATA_PROT_NODATA;
  383. if (scsicmd[1] & 0x1) {
  384. ; /* ignore IMMED bit, violates sat-r05 */
  385. }
  386. if (scsicmd[4] & 0x2)
  387. return 1; /* LOEJ bit set not supported */
  388. if (((scsicmd[4] >> 4) & 0xf) != 0)
  389. return 1; /* power conditions not supported */
  390. if (scsicmd[4] & 0x1) {
  391. tf->nsect = 1; /* 1 sector, lba=0 */
  392. tf->lbah = 0x0;
  393. tf->lbam = 0x0;
  394. tf->lbal = 0x0;
  395. tf->device |= ATA_LBA;
  396. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  397. } else {
  398. tf->nsect = 0; /* time period value (0 implies now) */
  399. tf->command = ATA_CMD_STANDBY;
  400. /* Consider: ATA STANDBY IMMEDIATE command */
  401. }
  402. /*
  403. * Standby and Idle condition timers could be implemented but that
  404. * would require libata to implement the Power condition mode page
  405. * and allow the user to change it. Changing mode pages requires
  406. * MODE SELECT to be implemented.
  407. */
  408. return 0;
  409. }
  410. /**
  411. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  412. * @qc: Storage for translated ATA taskfile
  413. * @scsicmd: SCSI command to translate (ignored)
  414. *
  415. * Sets up an ATA taskfile to issue FLUSH CACHE or
  416. * FLUSH CACHE EXT.
  417. *
  418. * LOCKING:
  419. * spin_lock_irqsave(host_set lock)
  420. *
  421. * RETURNS:
  422. * Zero on success, non-zero on error.
  423. */
  424. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  425. {
  426. struct ata_taskfile *tf = &qc->tf;
  427. tf->flags |= ATA_TFLAG_DEVICE;
  428. tf->protocol = ATA_PROT_NODATA;
  429. if ((tf->flags & ATA_TFLAG_LBA48) &&
  430. (ata_id_has_flush_ext(qc->dev->id)))
  431. tf->command = ATA_CMD_FLUSH_EXT;
  432. else
  433. tf->command = ATA_CMD_FLUSH;
  434. return 0;
  435. }
  436. /**
  437. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  438. * @qc: Storage for translated ATA taskfile
  439. * @scsicmd: SCSI command to translate
  440. *
  441. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  442. *
  443. * LOCKING:
  444. * spin_lock_irqsave(host_set lock)
  445. *
  446. * RETURNS:
  447. * Zero on success, non-zero on error.
  448. */
  449. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  450. {
  451. struct ata_taskfile *tf = &qc->tf;
  452. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  453. u64 dev_sectors = qc->dev->n_sectors;
  454. u64 sect = 0;
  455. u32 n_sect = 0;
  456. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  457. tf->protocol = ATA_PROT_NODATA;
  458. tf->device |= ATA_LBA;
  459. if (scsicmd[0] == VERIFY) {
  460. sect |= ((u64)scsicmd[2]) << 24;
  461. sect |= ((u64)scsicmd[3]) << 16;
  462. sect |= ((u64)scsicmd[4]) << 8;
  463. sect |= ((u64)scsicmd[5]);
  464. n_sect |= ((u32)scsicmd[7]) << 8;
  465. n_sect |= ((u32)scsicmd[8]);
  466. }
  467. else if (scsicmd[0] == VERIFY_16) {
  468. sect |= ((u64)scsicmd[2]) << 56;
  469. sect |= ((u64)scsicmd[3]) << 48;
  470. sect |= ((u64)scsicmd[4]) << 40;
  471. sect |= ((u64)scsicmd[5]) << 32;
  472. sect |= ((u64)scsicmd[6]) << 24;
  473. sect |= ((u64)scsicmd[7]) << 16;
  474. sect |= ((u64)scsicmd[8]) << 8;
  475. sect |= ((u64)scsicmd[9]);
  476. n_sect |= ((u32)scsicmd[10]) << 24;
  477. n_sect |= ((u32)scsicmd[11]) << 16;
  478. n_sect |= ((u32)scsicmd[12]) << 8;
  479. n_sect |= ((u32)scsicmd[13]);
  480. }
  481. else
  482. return 1;
  483. if (!n_sect)
  484. return 1;
  485. if (sect >= dev_sectors)
  486. return 1;
  487. if ((sect + n_sect) > dev_sectors)
  488. return 1;
  489. if (lba48) {
  490. if (n_sect > (64 * 1024))
  491. return 1;
  492. } else {
  493. if (n_sect > 256)
  494. return 1;
  495. }
  496. if (lba48) {
  497. tf->command = ATA_CMD_VERIFY_EXT;
  498. tf->hob_nsect = (n_sect >> 8) & 0xff;
  499. tf->hob_lbah = (sect >> 40) & 0xff;
  500. tf->hob_lbam = (sect >> 32) & 0xff;
  501. tf->hob_lbal = (sect >> 24) & 0xff;
  502. } else {
  503. tf->command = ATA_CMD_VERIFY;
  504. tf->device |= (sect >> 24) & 0xf;
  505. }
  506. tf->nsect = n_sect & 0xff;
  507. tf->lbah = (sect >> 16) & 0xff;
  508. tf->lbam = (sect >> 8) & 0xff;
  509. tf->lbal = sect & 0xff;
  510. return 0;
  511. }
  512. /**
  513. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  514. * @qc: Storage for translated ATA taskfile
  515. * @scsicmd: SCSI command to translate
  516. *
  517. * Converts any of six SCSI read/write commands into the
  518. * ATA counterpart, including starting sector (LBA),
  519. * sector count, and taking into account the device's LBA48
  520. * support.
  521. *
  522. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  523. * %WRITE_16 are currently supported.
  524. *
  525. * LOCKING:
  526. * spin_lock_irqsave(host_set lock)
  527. *
  528. * RETURNS:
  529. * Zero on success, non-zero on error.
  530. */
  531. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  532. {
  533. struct ata_taskfile *tf = &qc->tf;
  534. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  535. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  536. tf->protocol = qc->dev->xfer_protocol;
  537. tf->device |= ATA_LBA;
  538. if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
  539. scsicmd[0] == READ_16) {
  540. tf->command = qc->dev->read_cmd;
  541. } else {
  542. tf->command = qc->dev->write_cmd;
  543. tf->flags |= ATA_TFLAG_WRITE;
  544. }
  545. if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
  546. if (lba48) {
  547. tf->hob_nsect = scsicmd[7];
  548. tf->hob_lbal = scsicmd[2];
  549. qc->nsect = ((unsigned int)scsicmd[7] << 8) |
  550. scsicmd[8];
  551. } else {
  552. /* if we don't support LBA48 addressing, the request
  553. * -may- be too large. */
  554. if ((scsicmd[2] & 0xf0) || scsicmd[7])
  555. return 1;
  556. /* stores LBA27:24 in lower 4 bits of device reg */
  557. tf->device |= scsicmd[2];
  558. qc->nsect = scsicmd[8];
  559. }
  560. tf->nsect = scsicmd[8];
  561. tf->lbal = scsicmd[5];
  562. tf->lbam = scsicmd[4];
  563. tf->lbah = scsicmd[3];
  564. VPRINTK("ten-byte command\n");
  565. if (qc->nsect == 0) /* we don't support length==0 cmds */
  566. return 1;
  567. return 0;
  568. }
  569. if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
  570. qc->nsect = tf->nsect = scsicmd[4];
  571. if (!qc->nsect) {
  572. qc->nsect = 256;
  573. if (lba48)
  574. tf->hob_nsect = 1;
  575. }
  576. tf->lbal = scsicmd[3];
  577. tf->lbam = scsicmd[2];
  578. tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
  579. VPRINTK("six-byte command\n");
  580. return 0;
  581. }
  582. if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
  583. /* rule out impossible LBAs and sector counts */
  584. if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
  585. return 1;
  586. if (lba48) {
  587. tf->hob_nsect = scsicmd[12];
  588. tf->hob_lbal = scsicmd[6];
  589. tf->hob_lbam = scsicmd[5];
  590. tf->hob_lbah = scsicmd[4];
  591. qc->nsect = ((unsigned int)scsicmd[12] << 8) |
  592. scsicmd[13];
  593. } else {
  594. /* once again, filter out impossible non-zero values */
  595. if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
  596. (scsicmd[6] & 0xf0))
  597. return 1;
  598. /* stores LBA27:24 in lower 4 bits of device reg */
  599. tf->device |= scsicmd[6];
  600. qc->nsect = scsicmd[13];
  601. }
  602. tf->nsect = scsicmd[13];
  603. tf->lbal = scsicmd[9];
  604. tf->lbam = scsicmd[8];
  605. tf->lbah = scsicmd[7];
  606. VPRINTK("sixteen-byte command\n");
  607. if (qc->nsect == 0) /* we don't support length==0 cmds */
  608. return 1;
  609. return 0;
  610. }
  611. DPRINTK("no-byte command\n");
  612. return 1;
  613. }
  614. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  615. {
  616. struct scsi_cmnd *cmd = qc->scsicmd;
  617. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
  618. ata_to_sense_error(qc, drv_stat);
  619. else
  620. cmd->result = SAM_STAT_GOOD;
  621. qc->scsidone(cmd);
  622. return 0;
  623. }
  624. /**
  625. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  626. * @ap: ATA port to which the command is addressed
  627. * @dev: ATA device to which the command is addressed
  628. * @cmd: SCSI command to execute
  629. * @done: SCSI command completion function
  630. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  631. *
  632. * Our ->queuecommand() function has decided that the SCSI
  633. * command issued can be directly translated into an ATA
  634. * command, rather than handled internally.
  635. *
  636. * This function sets up an ata_queued_cmd structure for the
  637. * SCSI command, and sends that ata_queued_cmd to the hardware.
  638. *
  639. * LOCKING:
  640. * spin_lock_irqsave(host_set lock)
  641. */
  642. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  643. struct scsi_cmnd *cmd,
  644. void (*done)(struct scsi_cmnd *),
  645. ata_xlat_func_t xlat_func)
  646. {
  647. struct ata_queued_cmd *qc;
  648. u8 *scsicmd = cmd->cmnd;
  649. VPRINTK("ENTER\n");
  650. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  651. if (!qc)
  652. return;
  653. /* data is present; dma-map it */
  654. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  655. cmd->sc_data_direction == DMA_TO_DEVICE) {
  656. if (unlikely(cmd->request_bufflen < 1)) {
  657. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  658. ap->id, dev->devno);
  659. goto err_out;
  660. }
  661. if (cmd->use_sg)
  662. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  663. else
  664. ata_sg_init_one(qc, cmd->request_buffer,
  665. cmd->request_bufflen);
  666. qc->dma_dir = cmd->sc_data_direction;
  667. }
  668. qc->complete_fn = ata_scsi_qc_complete;
  669. if (xlat_func(qc, scsicmd))
  670. goto err_out;
  671. /* select device, send command to hardware */
  672. if (ata_qc_issue(qc))
  673. goto err_out;
  674. VPRINTK("EXIT\n");
  675. return;
  676. err_out:
  677. ata_qc_free(qc);
  678. ata_bad_cdb(cmd, done);
  679. DPRINTK("EXIT - badcmd\n");
  680. }
  681. /**
  682. * ata_scsi_rbuf_get - Map response buffer.
  683. * @cmd: SCSI command containing buffer to be mapped.
  684. * @buf_out: Pointer to mapped area.
  685. *
  686. * Maps buffer contained within SCSI command @cmd.
  687. *
  688. * LOCKING:
  689. * spin_lock_irqsave(host_set lock)
  690. *
  691. * RETURNS:
  692. * Length of response buffer.
  693. */
  694. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  695. {
  696. u8 *buf;
  697. unsigned int buflen;
  698. if (cmd->use_sg) {
  699. struct scatterlist *sg;
  700. sg = (struct scatterlist *) cmd->request_buffer;
  701. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  702. buflen = sg->length;
  703. } else {
  704. buf = cmd->request_buffer;
  705. buflen = cmd->request_bufflen;
  706. }
  707. *buf_out = buf;
  708. return buflen;
  709. }
  710. /**
  711. * ata_scsi_rbuf_put - Unmap response buffer.
  712. * @cmd: SCSI command containing buffer to be unmapped.
  713. * @buf: buffer to unmap
  714. *
  715. * Unmaps response buffer contained within @cmd.
  716. *
  717. * LOCKING:
  718. * spin_lock_irqsave(host_set lock)
  719. */
  720. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  721. {
  722. if (cmd->use_sg) {
  723. struct scatterlist *sg;
  724. sg = (struct scatterlist *) cmd->request_buffer;
  725. kunmap_atomic(buf - sg->offset, KM_USER0);
  726. }
  727. }
  728. /**
  729. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  730. * @args: device IDENTIFY data / SCSI command of interest.
  731. * @actor: Callback hook for desired SCSI command simulator
  732. *
  733. * Takes care of the hard work of simulating a SCSI command...
  734. * Mapping the response buffer, calling the command's handler,
  735. * and handling the handler's return value. This return value
  736. * indicates whether the handler wishes the SCSI command to be
  737. * completed successfully, or not.
  738. *
  739. * LOCKING:
  740. * spin_lock_irqsave(host_set lock)
  741. */
  742. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  743. unsigned int (*actor) (struct ata_scsi_args *args,
  744. u8 *rbuf, unsigned int buflen))
  745. {
  746. u8 *rbuf;
  747. unsigned int buflen, rc;
  748. struct scsi_cmnd *cmd = args->cmd;
  749. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  750. memset(rbuf, 0, buflen);
  751. rc = actor(args, rbuf, buflen);
  752. ata_scsi_rbuf_put(cmd, rbuf);
  753. if (rc)
  754. ata_bad_cdb(cmd, args->done);
  755. else {
  756. cmd->result = SAM_STAT_GOOD;
  757. args->done(cmd);
  758. }
  759. }
  760. /**
  761. * ata_scsiop_inq_std - Simulate INQUIRY command
  762. * @args: device IDENTIFY data / SCSI command of interest.
  763. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  764. * @buflen: Response buffer length.
  765. *
  766. * Returns standard device identification data associated
  767. * with non-EVPD INQUIRY command output.
  768. *
  769. * LOCKING:
  770. * spin_lock_irqsave(host_set lock)
  771. */
  772. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  773. unsigned int buflen)
  774. {
  775. u8 hdr[] = {
  776. TYPE_DISK,
  777. 0,
  778. 0x5, /* claim SPC-3 version compatibility */
  779. 2,
  780. 95 - 4
  781. };
  782. /* set scsi removeable (RMB) bit per ata bit */
  783. if (ata_id_removeable(args->id))
  784. hdr[1] |= (1 << 7);
  785. VPRINTK("ENTER\n");
  786. memcpy(rbuf, hdr, sizeof(hdr));
  787. if (buflen > 35) {
  788. memcpy(&rbuf[8], "ATA ", 8);
  789. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  790. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  791. if (rbuf[32] == 0 || rbuf[32] == ' ')
  792. memcpy(&rbuf[32], "n/a ", 4);
  793. }
  794. if (buflen > 63) {
  795. const u8 versions[] = {
  796. 0x60, /* SAM-3 (no version claimed) */
  797. 0x03,
  798. 0x20, /* SBC-2 (no version claimed) */
  799. 0x02,
  800. 0x60 /* SPC-3 (no version claimed) */
  801. };
  802. memcpy(rbuf + 59, versions, sizeof(versions));
  803. }
  804. return 0;
  805. }
  806. /**
  807. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  808. * @args: device IDENTIFY data / SCSI command of interest.
  809. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  810. * @buflen: Response buffer length.
  811. *
  812. * Returns list of inquiry EVPD pages available.
  813. *
  814. * LOCKING:
  815. * spin_lock_irqsave(host_set lock)
  816. */
  817. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  818. unsigned int buflen)
  819. {
  820. const u8 pages[] = {
  821. 0x00, /* page 0x00, this page */
  822. 0x80, /* page 0x80, unit serial no page */
  823. 0x83 /* page 0x83, device ident page */
  824. };
  825. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  826. if (buflen > 6)
  827. memcpy(rbuf + 4, pages, sizeof(pages));
  828. return 0;
  829. }
  830. /**
  831. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  832. * @args: device IDENTIFY data / SCSI command of interest.
  833. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  834. * @buflen: Response buffer length.
  835. *
  836. * Returns ATA device serial number.
  837. *
  838. * LOCKING:
  839. * spin_lock_irqsave(host_set lock)
  840. */
  841. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  842. unsigned int buflen)
  843. {
  844. const u8 hdr[] = {
  845. 0,
  846. 0x80, /* this page code */
  847. 0,
  848. ATA_SERNO_LEN, /* page len */
  849. };
  850. memcpy(rbuf, hdr, sizeof(hdr));
  851. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  852. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  853. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  854. return 0;
  855. }
  856. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  857. /**
  858. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  859. * @args: device IDENTIFY data / SCSI command of interest.
  860. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  861. * @buflen: Response buffer length.
  862. *
  863. * Returns device identification. Currently hardcoded to
  864. * return "Linux ATA-SCSI simulator".
  865. *
  866. * LOCKING:
  867. * spin_lock_irqsave(host_set lock)
  868. */
  869. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  870. unsigned int buflen)
  871. {
  872. rbuf[1] = 0x83; /* this page code */
  873. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  874. /* our one and only identification descriptor (vendor-specific) */
  875. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  876. rbuf[4 + 0] = 2; /* code set: ASCII */
  877. rbuf[4 + 3] = strlen(inq_83_str);
  878. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  879. }
  880. return 0;
  881. }
  882. /**
  883. * ata_scsiop_noop - Command handler that simply returns success.
  884. * @args: device IDENTIFY data / SCSI command of interest.
  885. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  886. * @buflen: Response buffer length.
  887. *
  888. * No operation. Simply returns success to caller, to indicate
  889. * that the caller should successfully complete this SCSI command.
  890. *
  891. * LOCKING:
  892. * spin_lock_irqsave(host_set lock)
  893. */
  894. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  895. unsigned int buflen)
  896. {
  897. VPRINTK("ENTER\n");
  898. return 0;
  899. }
  900. /**
  901. * ata_msense_push - Push data onto MODE SENSE data output buffer
  902. * @ptr_io: (input/output) Location to store more output data
  903. * @last: End of output data buffer
  904. * @buf: Pointer to BLOB being added to output buffer
  905. * @buflen: Length of BLOB
  906. *
  907. * Store MODE SENSE data on an output buffer.
  908. *
  909. * LOCKING:
  910. * None.
  911. */
  912. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  913. const u8 *buf, unsigned int buflen)
  914. {
  915. u8 *ptr = *ptr_io;
  916. if ((ptr + buflen - 1) > last)
  917. return;
  918. memcpy(ptr, buf, buflen);
  919. ptr += buflen;
  920. *ptr_io = ptr;
  921. }
  922. /**
  923. * ata_msense_caching - Simulate MODE SENSE caching info page
  924. * @id: device IDENTIFY data
  925. * @ptr_io: (input/output) Location to store more output data
  926. * @last: End of output data buffer
  927. *
  928. * Generate a caching info page, which conditionally indicates
  929. * write caching to the SCSI layer, depending on device
  930. * capabilities.
  931. *
  932. * LOCKING:
  933. * None.
  934. */
  935. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  936. const u8 *last)
  937. {
  938. u8 page[] = {
  939. 0x8, /* page code */
  940. 0x12, /* page length */
  941. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
  942. 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
  943. };
  944. if (ata_id_wcache_enabled(id))
  945. page[2] |= (1 << 2); /* write cache enable */
  946. if (!ata_id_rahead_enabled(id))
  947. page[12] |= (1 << 5); /* disable read ahead */
  948. ata_msense_push(ptr_io, last, page, sizeof(page));
  949. return sizeof(page);
  950. }
  951. /**
  952. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  953. * @dev: Device associated with this MODE SENSE command
  954. * @ptr_io: (input/output) Location to store more output data
  955. * @last: End of output data buffer
  956. *
  957. * Generate a generic MODE SENSE control mode page.
  958. *
  959. * LOCKING:
  960. * None.
  961. */
  962. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  963. {
  964. const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
  965. /* byte 2: set the descriptor format sense data bit (bit 2)
  966. * since we need to support returning this format for SAT
  967. * commands and any SCSI commands against a 48b LBA device.
  968. */
  969. ata_msense_push(ptr_io, last, page, sizeof(page));
  970. return sizeof(page);
  971. }
  972. /**
  973. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  974. * @dev: Device associated with this MODE SENSE command
  975. * @ptr_io: (input/output) Location to store more output data
  976. * @last: End of output data buffer
  977. *
  978. * Generate a generic MODE SENSE r/w error recovery page.
  979. *
  980. * LOCKING:
  981. * None.
  982. */
  983. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  984. {
  985. const u8 page[] = {
  986. 0x1, /* page code */
  987. 0xa, /* page length */
  988. (1 << 7) | (1 << 6), /* note auto r/w reallocation */
  989. 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
  990. };
  991. ata_msense_push(ptr_io, last, page, sizeof(page));
  992. return sizeof(page);
  993. }
  994. /**
  995. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  996. * @args: device IDENTIFY data / SCSI command of interest.
  997. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  998. * @buflen: Response buffer length.
  999. *
  1000. * Simulate MODE SENSE commands.
  1001. *
  1002. * LOCKING:
  1003. * spin_lock_irqsave(host_set lock)
  1004. */
  1005. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1006. unsigned int buflen)
  1007. {
  1008. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1009. unsigned int page_control, six_byte, output_len;
  1010. VPRINTK("ENTER\n");
  1011. six_byte = (scsicmd[0] == MODE_SENSE);
  1012. /* we only support saved and current values (which we treat
  1013. * in the same manner)
  1014. */
  1015. page_control = scsicmd[2] >> 6;
  1016. if ((page_control != 0) && (page_control != 3))
  1017. return 1;
  1018. if (six_byte)
  1019. output_len = 4;
  1020. else
  1021. output_len = 8;
  1022. p = rbuf + output_len;
  1023. last = rbuf + buflen - 1;
  1024. switch(scsicmd[2] & 0x3f) {
  1025. case 0x01: /* r/w error recovery */
  1026. output_len += ata_msense_rw_recovery(&p, last);
  1027. break;
  1028. case 0x08: /* caching */
  1029. output_len += ata_msense_caching(args->id, &p, last);
  1030. break;
  1031. case 0x0a: { /* control mode */
  1032. output_len += ata_msense_ctl_mode(&p, last);
  1033. break;
  1034. }
  1035. case 0x3f: /* all pages */
  1036. output_len += ata_msense_rw_recovery(&p, last);
  1037. output_len += ata_msense_caching(args->id, &p, last);
  1038. output_len += ata_msense_ctl_mode(&p, last);
  1039. break;
  1040. default: /* invalid page code */
  1041. return 1;
  1042. }
  1043. if (six_byte) {
  1044. output_len--;
  1045. rbuf[0] = output_len;
  1046. } else {
  1047. output_len -= 2;
  1048. rbuf[0] = output_len >> 8;
  1049. rbuf[1] = output_len;
  1050. }
  1051. return 0;
  1052. }
  1053. /**
  1054. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1055. * @args: device IDENTIFY data / SCSI command of interest.
  1056. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1057. * @buflen: Response buffer length.
  1058. *
  1059. * Simulate READ CAPACITY commands.
  1060. *
  1061. * LOCKING:
  1062. * spin_lock_irqsave(host_set lock)
  1063. */
  1064. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1065. unsigned int buflen)
  1066. {
  1067. u64 n_sectors;
  1068. u32 tmp;
  1069. VPRINTK("ENTER\n");
  1070. if (ata_id_has_lba48(args->id))
  1071. n_sectors = ata_id_u64(args->id, 100);
  1072. else
  1073. n_sectors = ata_id_u32(args->id, 60);
  1074. n_sectors--; /* ATA TotalUserSectors - 1 */
  1075. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1076. if( n_sectors >= 0xffffffffULL )
  1077. tmp = 0xffffffff ; /* Return max count on overflow */
  1078. else
  1079. tmp = n_sectors ;
  1080. /* sector count, 32-bit */
  1081. rbuf[0] = tmp >> (8 * 3);
  1082. rbuf[1] = tmp >> (8 * 2);
  1083. rbuf[2] = tmp >> (8 * 1);
  1084. rbuf[3] = tmp;
  1085. /* sector size */
  1086. tmp = ATA_SECT_SIZE;
  1087. rbuf[6] = tmp >> 8;
  1088. rbuf[7] = tmp;
  1089. } else {
  1090. /* sector count, 64-bit */
  1091. tmp = n_sectors >> (8 * 4);
  1092. rbuf[2] = tmp >> (8 * 3);
  1093. rbuf[3] = tmp >> (8 * 2);
  1094. rbuf[4] = tmp >> (8 * 1);
  1095. rbuf[5] = tmp;
  1096. tmp = n_sectors;
  1097. rbuf[6] = tmp >> (8 * 3);
  1098. rbuf[7] = tmp >> (8 * 2);
  1099. rbuf[8] = tmp >> (8 * 1);
  1100. rbuf[9] = tmp;
  1101. /* sector size */
  1102. tmp = ATA_SECT_SIZE;
  1103. rbuf[12] = tmp >> 8;
  1104. rbuf[13] = tmp;
  1105. }
  1106. return 0;
  1107. }
  1108. /**
  1109. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1110. * @args: device IDENTIFY data / SCSI command of interest.
  1111. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1112. * @buflen: Response buffer length.
  1113. *
  1114. * Simulate REPORT LUNS command.
  1115. *
  1116. * LOCKING:
  1117. * spin_lock_irqsave(host_set lock)
  1118. */
  1119. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1120. unsigned int buflen)
  1121. {
  1122. VPRINTK("ENTER\n");
  1123. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1124. return 0;
  1125. }
  1126. /**
  1127. * ata_scsi_badcmd - End a SCSI request with an error
  1128. * @cmd: SCSI request to be handled
  1129. * @done: SCSI command completion function
  1130. * @asc: SCSI-defined additional sense code
  1131. * @ascq: SCSI-defined additional sense code qualifier
  1132. *
  1133. * Helper function that completes a SCSI command with
  1134. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1135. * and the specified additional sense codes.
  1136. *
  1137. * LOCKING:
  1138. * spin_lock_irqsave(host_set lock)
  1139. */
  1140. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1141. {
  1142. DPRINTK("ENTER\n");
  1143. cmd->result = SAM_STAT_CHECK_CONDITION;
  1144. cmd->sense_buffer[0] = 0x70;
  1145. cmd->sense_buffer[2] = ILLEGAL_REQUEST;
  1146. cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
  1147. cmd->sense_buffer[12] = asc;
  1148. cmd->sense_buffer[13] = ascq;
  1149. done(cmd);
  1150. }
  1151. static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  1152. {
  1153. struct scsi_cmnd *cmd = qc->scsicmd;
  1154. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
  1155. DPRINTK("request check condition\n");
  1156. cmd->result = SAM_STAT_CHECK_CONDITION;
  1157. qc->scsidone(cmd);
  1158. return 1;
  1159. } else {
  1160. u8 *scsicmd = cmd->cmnd;
  1161. if (scsicmd[0] == INQUIRY) {
  1162. u8 *buf = NULL;
  1163. unsigned int buflen;
  1164. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1165. buf[2] = 0x5;
  1166. buf[3] = (buf[3] & 0xf0) | 2;
  1167. ata_scsi_rbuf_put(cmd, buf);
  1168. }
  1169. cmd->result = SAM_STAT_GOOD;
  1170. }
  1171. qc->scsidone(cmd);
  1172. return 0;
  1173. }
  1174. /**
  1175. * atapi_xlat - Initialize PACKET taskfile
  1176. * @qc: command structure to be initialized
  1177. * @scsicmd: SCSI CDB associated with this PACKET command
  1178. *
  1179. * LOCKING:
  1180. * spin_lock_irqsave(host_set lock)
  1181. *
  1182. * RETURNS:
  1183. * Zero on success, non-zero on failure.
  1184. */
  1185. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  1186. {
  1187. struct scsi_cmnd *cmd = qc->scsicmd;
  1188. struct ata_device *dev = qc->dev;
  1189. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1190. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1191. if (!using_pio)
  1192. /* Check whether ATAPI DMA is safe */
  1193. if (ata_check_atapi_dma(qc))
  1194. using_pio = 1;
  1195. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1196. qc->complete_fn = atapi_qc_complete;
  1197. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1198. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1199. qc->tf.flags |= ATA_TFLAG_WRITE;
  1200. DPRINTK("direction: write\n");
  1201. }
  1202. qc->tf.command = ATA_CMD_PACKET;
  1203. /* no data, or PIO data xfer */
  1204. if (using_pio || nodata) {
  1205. if (nodata)
  1206. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1207. else
  1208. qc->tf.protocol = ATA_PROT_ATAPI;
  1209. qc->tf.lbam = (8 * 1024) & 0xff;
  1210. qc->tf.lbah = (8 * 1024) >> 8;
  1211. }
  1212. /* DMA data xfer */
  1213. else {
  1214. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1215. qc->tf.feature |= ATAPI_PKT_DMA;
  1216. #ifdef ATAPI_ENABLE_DMADIR
  1217. /* some SATA bridges need us to indicate data xfer direction */
  1218. if (cmd->sc_data_direction != DMA_TO_DEVICE)
  1219. qc->tf.feature |= ATAPI_DMADIR;
  1220. #endif
  1221. }
  1222. qc->nbytes = cmd->bufflen;
  1223. return 0;
  1224. }
  1225. /**
  1226. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1227. * @ap: ATA port to which the device is attached
  1228. * @scsidev: SCSI device from which we derive the ATA device
  1229. *
  1230. * Given various information provided in struct scsi_cmnd,
  1231. * map that onto an ATA bus, and using that mapping
  1232. * determine which ata_device is associated with the
  1233. * SCSI command to be sent.
  1234. *
  1235. * LOCKING:
  1236. * spin_lock_irqsave(host_set lock)
  1237. *
  1238. * RETURNS:
  1239. * Associated ATA device, or %NULL if not found.
  1240. */
  1241. static struct ata_device *
  1242. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
  1243. {
  1244. struct ata_device *dev;
  1245. /* skip commands not addressed to targets we simulate */
  1246. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1247. dev = &ap->device[scsidev->id];
  1248. else
  1249. return NULL;
  1250. if (unlikely((scsidev->channel != 0) ||
  1251. (scsidev->lun != 0)))
  1252. return NULL;
  1253. if (unlikely(!ata_dev_present(dev)))
  1254. return NULL;
  1255. if (!atapi_enabled) {
  1256. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1257. return NULL;
  1258. }
  1259. return dev;
  1260. }
  1261. /**
  1262. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1263. * @dev: ATA device
  1264. * @cmd: SCSI command opcode to consider
  1265. *
  1266. * Look up the SCSI command given, and determine whether the
  1267. * SCSI command is to be translated or simulated.
  1268. *
  1269. * RETURNS:
  1270. * Pointer to translation function if possible, %NULL if not.
  1271. */
  1272. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  1273. {
  1274. switch (cmd) {
  1275. case READ_6:
  1276. case READ_10:
  1277. case READ_16:
  1278. case WRITE_6:
  1279. case WRITE_10:
  1280. case WRITE_16:
  1281. return ata_scsi_rw_xlat;
  1282. case SYNCHRONIZE_CACHE:
  1283. if (ata_try_flush_cache(dev))
  1284. return ata_scsi_flush_xlat;
  1285. break;
  1286. case VERIFY:
  1287. case VERIFY_16:
  1288. return ata_scsi_verify_xlat;
  1289. case START_STOP:
  1290. return ata_scsi_start_stop_xlat;
  1291. }
  1292. return NULL;
  1293. }
  1294. /**
  1295. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  1296. * @ap: ATA port to which the command was being sent
  1297. * @cmd: SCSI command to dump
  1298. *
  1299. * Prints the contents of a SCSI command via printk().
  1300. */
  1301. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  1302. struct scsi_cmnd *cmd)
  1303. {
  1304. #ifdef ATA_DEBUG
  1305. struct scsi_device *scsidev = cmd->device;
  1306. u8 *scsicmd = cmd->cmnd;
  1307. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1308. ap->id,
  1309. scsidev->channel, scsidev->id, scsidev->lun,
  1310. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  1311. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  1312. scsicmd[8]);
  1313. #endif
  1314. }
  1315. /**
  1316. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  1317. * @cmd: SCSI command to be sent
  1318. * @done: Completion function, called when command is complete
  1319. *
  1320. * In some cases, this function translates SCSI commands into
  1321. * ATA taskfiles, and queues the taskfiles to be sent to
  1322. * hardware. In other cases, this function simulates a
  1323. * SCSI device by evaluating and responding to certain
  1324. * SCSI commands. This creates the overall effect of
  1325. * ATA and ATAPI devices appearing as SCSI devices.
  1326. *
  1327. * LOCKING:
  1328. * Releases scsi-layer-held lock, and obtains host_set lock.
  1329. *
  1330. * RETURNS:
  1331. * Zero.
  1332. */
  1333. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  1334. {
  1335. struct ata_port *ap;
  1336. struct ata_device *dev;
  1337. struct scsi_device *scsidev = cmd->device;
  1338. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  1339. ata_scsi_dump_cdb(ap, cmd);
  1340. dev = ata_scsi_find_dev(ap, scsidev);
  1341. if (unlikely(!dev)) {
  1342. cmd->result = (DID_BAD_TARGET << 16);
  1343. done(cmd);
  1344. goto out_unlock;
  1345. }
  1346. if (dev->class == ATA_DEV_ATA) {
  1347. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  1348. cmd->cmnd[0]);
  1349. if (xlat_func)
  1350. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  1351. else
  1352. ata_scsi_simulate(dev->id, cmd, done);
  1353. } else
  1354. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  1355. out_unlock:
  1356. return 0;
  1357. }
  1358. /**
  1359. * ata_scsi_simulate - simulate SCSI command on ATA device
  1360. * @id: current IDENTIFY data for target device.
  1361. * @cmd: SCSI command being sent to device.
  1362. * @done: SCSI command completion function.
  1363. *
  1364. * Interprets and directly executes a select list of SCSI commands
  1365. * that can be handled internally.
  1366. *
  1367. * LOCKING:
  1368. * spin_lock_irqsave(host_set lock)
  1369. */
  1370. void ata_scsi_simulate(u16 *id,
  1371. struct scsi_cmnd *cmd,
  1372. void (*done)(struct scsi_cmnd *))
  1373. {
  1374. struct ata_scsi_args args;
  1375. u8 *scsicmd = cmd->cmnd;
  1376. args.id = id;
  1377. args.cmd = cmd;
  1378. args.done = done;
  1379. switch(scsicmd[0]) {
  1380. /* no-op's, complete with success */
  1381. case SYNCHRONIZE_CACHE:
  1382. case REZERO_UNIT:
  1383. case SEEK_6:
  1384. case SEEK_10:
  1385. case TEST_UNIT_READY:
  1386. case FORMAT_UNIT: /* FIXME: correct? */
  1387. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  1388. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  1389. break;
  1390. case INQUIRY:
  1391. if (scsicmd[1] & 2) /* is CmdDt set? */
  1392. ata_bad_cdb(cmd, done);
  1393. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  1394. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  1395. else if (scsicmd[2] == 0x00)
  1396. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  1397. else if (scsicmd[2] == 0x80)
  1398. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  1399. else if (scsicmd[2] == 0x83)
  1400. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  1401. else
  1402. ata_bad_cdb(cmd, done);
  1403. break;
  1404. case MODE_SENSE:
  1405. case MODE_SENSE_10:
  1406. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  1407. break;
  1408. case MODE_SELECT: /* unconditionally return */
  1409. case MODE_SELECT_10: /* bad-field-in-cdb */
  1410. ata_bad_cdb(cmd, done);
  1411. break;
  1412. case READ_CAPACITY:
  1413. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1414. break;
  1415. case SERVICE_ACTION_IN:
  1416. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  1417. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1418. else
  1419. ata_bad_cdb(cmd, done);
  1420. break;
  1421. case REPORT_LUNS:
  1422. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  1423. break;
  1424. /* mandantory commands we haven't implemented yet */
  1425. case REQUEST_SENSE:
  1426. /* all other commands */
  1427. default:
  1428. ata_bad_scsiop(cmd, done);
  1429. break;
  1430. }
  1431. }