libata-scsi.c 39 KB

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