libata-scsi.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  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. struct ata_device *dev = qc->dev;
  453. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  454. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  455. u64 dev_sectors = qc->dev->n_sectors;
  456. u64 block = 0;
  457. u32 n_block = 0;
  458. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  459. tf->protocol = ATA_PROT_NODATA;
  460. if (scsicmd[0] == VERIFY) {
  461. block |= ((u64)scsicmd[2]) << 24;
  462. block |= ((u64)scsicmd[3]) << 16;
  463. block |= ((u64)scsicmd[4]) << 8;
  464. block |= ((u64)scsicmd[5]);
  465. n_block |= ((u32)scsicmd[7]) << 8;
  466. n_block |= ((u32)scsicmd[8]);
  467. }
  468. else if (scsicmd[0] == VERIFY_16) {
  469. block |= ((u64)scsicmd[2]) << 56;
  470. block |= ((u64)scsicmd[3]) << 48;
  471. block |= ((u64)scsicmd[4]) << 40;
  472. block |= ((u64)scsicmd[5]) << 32;
  473. block |= ((u64)scsicmd[6]) << 24;
  474. block |= ((u64)scsicmd[7]) << 16;
  475. block |= ((u64)scsicmd[8]) << 8;
  476. block |= ((u64)scsicmd[9]);
  477. n_block |= ((u32)scsicmd[10]) << 24;
  478. n_block |= ((u32)scsicmd[11]) << 16;
  479. n_block |= ((u32)scsicmd[12]) << 8;
  480. n_block |= ((u32)scsicmd[13]);
  481. }
  482. else
  483. return 1;
  484. if (!n_block)
  485. return 1;
  486. if (block >= dev_sectors)
  487. return 1;
  488. if ((block + n_block) > dev_sectors)
  489. return 1;
  490. if (lba48) {
  491. if (n_block > (64 * 1024))
  492. return 1;
  493. } else {
  494. if (n_block > 256)
  495. return 1;
  496. }
  497. if (lba) {
  498. if (lba48) {
  499. tf->command = ATA_CMD_VERIFY_EXT;
  500. tf->hob_nsect = (n_block >> 8) & 0xff;
  501. tf->hob_lbah = (block >> 40) & 0xff;
  502. tf->hob_lbam = (block >> 32) & 0xff;
  503. tf->hob_lbal = (block >> 24) & 0xff;
  504. } else {
  505. tf->command = ATA_CMD_VERIFY;
  506. tf->device |= (block >> 24) & 0xf;
  507. }
  508. tf->nsect = n_block & 0xff;
  509. tf->lbah = (block >> 16) & 0xff;
  510. tf->lbam = (block >> 8) & 0xff;
  511. tf->lbal = block & 0xff;
  512. tf->device |= ATA_LBA;
  513. } else {
  514. /* CHS */
  515. u32 sect, head, cyl, track;
  516. /* Convert LBA to CHS */
  517. track = (u32)block / dev->sectors;
  518. cyl = track / dev->heads;
  519. head = track % dev->heads;
  520. sect = (u32)block % dev->sectors + 1;
  521. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  522. (u32)block, track, cyl, head, sect);
  523. /* Check whether the converted CHS can fit.
  524. Cylinder: 0-65535
  525. Head: 0-15
  526. Sector: 1-255*/
  527. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  528. return 1;
  529. tf->command = ATA_CMD_VERIFY;
  530. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  531. tf->lbal = sect;
  532. tf->lbam = cyl;
  533. tf->lbah = cyl >> 8;
  534. tf->device |= head;
  535. }
  536. return 0;
  537. }
  538. /**
  539. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  540. * @qc: Storage for translated ATA taskfile
  541. * @scsicmd: SCSI command to translate
  542. *
  543. * Converts any of six SCSI read/write commands into the
  544. * ATA counterpart, including starting sector (LBA),
  545. * sector count, and taking into account the device's LBA48
  546. * support.
  547. *
  548. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  549. * %WRITE_16 are currently supported.
  550. *
  551. * LOCKING:
  552. * spin_lock_irqsave(host_set lock)
  553. *
  554. * RETURNS:
  555. * Zero on success, non-zero on error.
  556. */
  557. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  558. {
  559. struct ata_taskfile *tf = &qc->tf;
  560. struct ata_device *dev = qc->dev;
  561. unsigned int lba = tf->flags & ATA_TFLAG_LBA;
  562. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  563. u64 block = 0;
  564. u32 n_block = 0;
  565. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  566. tf->protocol = qc->dev->xfer_protocol;
  567. if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
  568. scsicmd[0] == READ_16) {
  569. tf->command = qc->dev->read_cmd;
  570. } else {
  571. tf->command = qc->dev->write_cmd;
  572. tf->flags |= ATA_TFLAG_WRITE;
  573. }
  574. /* Calculate the SCSI LBA and transfer length. */
  575. if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
  576. block |= ((u64)scsicmd[2]) << 24;
  577. block |= ((u64)scsicmd[3]) << 16;
  578. block |= ((u64)scsicmd[4]) << 8;
  579. block |= ((u64)scsicmd[5]);
  580. n_block |= ((u32)scsicmd[7]) << 8;
  581. n_block |= ((u32)scsicmd[8]);
  582. VPRINTK("ten-byte command\n");
  583. } else if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
  584. block |= ((u64)scsicmd[2]) << 8;
  585. block |= ((u64)scsicmd[3]);
  586. n_block |= ((u32)scsicmd[4]);
  587. /* for 6-byte r/w commands, transfer length 0
  588. * means 256 blocks of data, not 0 block.
  589. */
  590. if (!n_block)
  591. n_block = 256;
  592. VPRINTK("six-byte command\n");
  593. } else if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
  594. block |= ((u64)scsicmd[2]) << 56;
  595. block |= ((u64)scsicmd[3]) << 48;
  596. block |= ((u64)scsicmd[4]) << 40;
  597. block |= ((u64)scsicmd[5]) << 32;
  598. block |= ((u64)scsicmd[6]) << 24;
  599. block |= ((u64)scsicmd[7]) << 16;
  600. block |= ((u64)scsicmd[8]) << 8;
  601. block |= ((u64)scsicmd[9]);
  602. n_block |= ((u32)scsicmd[10]) << 24;
  603. n_block |= ((u32)scsicmd[11]) << 16;
  604. n_block |= ((u32)scsicmd[12]) << 8;
  605. n_block |= ((u32)scsicmd[13]);
  606. VPRINTK("sixteen-byte command\n");
  607. } else {
  608. DPRINTK("no-byte command\n");
  609. return 1;
  610. }
  611. /* Check and compose ATA command */
  612. if (!n_block)
  613. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  614. * length 0 means transfer 0 block of data.
  615. * However, for ATA R/W commands, sector count 0 means
  616. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  617. */
  618. return 1;
  619. if (lba) {
  620. if (lba48) {
  621. /* The request -may- be too large for LBA48. */
  622. if ((block >> 48) || (n_block > 65536))
  623. return 1;
  624. tf->hob_nsect = (n_block >> 8) & 0xff;
  625. tf->hob_lbah = (block >> 40) & 0xff;
  626. tf->hob_lbam = (block >> 32) & 0xff;
  627. tf->hob_lbal = (block >> 24) & 0xff;
  628. } else {
  629. /* LBA28 */
  630. /* The request -may- be too large for LBA28. */
  631. if ((block >> 28) || (n_block > 256))
  632. return 1;
  633. tf->device |= (block >> 24) & 0xf;
  634. }
  635. qc->nsect = n_block;
  636. tf->nsect = n_block & 0xff;
  637. tf->lbah = (block >> 16) & 0xff;
  638. tf->lbam = (block >> 8) & 0xff;
  639. tf->lbal = block & 0xff;
  640. tf->device |= ATA_LBA;
  641. } else {
  642. /* CHS */
  643. u32 sect, head, cyl, track;
  644. /* The request -may- be too large for CHS addressing. */
  645. if ((block >> 28) || (n_block > 256))
  646. return 1;
  647. /* Convert LBA to CHS */
  648. track = (u32)block / dev->sectors;
  649. cyl = track / dev->heads;
  650. head = track % dev->heads;
  651. sect = (u32)block % dev->sectors + 1;
  652. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  653. (u32)block, track, cyl, head, sect);
  654. /* Check whether the converted CHS can fit.
  655. Cylinder: 0-65535
  656. Head: 0-15
  657. Sector: 1-255*/
  658. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  659. return 1;
  660. qc->nsect = n_block;
  661. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  662. tf->lbal = sect;
  663. tf->lbam = cyl;
  664. tf->lbah = cyl >> 8;
  665. tf->device |= head;
  666. }
  667. return 0;
  668. }
  669. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  670. {
  671. struct scsi_cmnd *cmd = qc->scsicmd;
  672. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
  673. ata_to_sense_error(qc, drv_stat);
  674. else
  675. cmd->result = SAM_STAT_GOOD;
  676. qc->scsidone(cmd);
  677. return 0;
  678. }
  679. /**
  680. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  681. * @ap: ATA port to which the command is addressed
  682. * @dev: ATA device to which the command is addressed
  683. * @cmd: SCSI command to execute
  684. * @done: SCSI command completion function
  685. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  686. *
  687. * Our ->queuecommand() function has decided that the SCSI
  688. * command issued can be directly translated into an ATA
  689. * command, rather than handled internally.
  690. *
  691. * This function sets up an ata_queued_cmd structure for the
  692. * SCSI command, and sends that ata_queued_cmd to the hardware.
  693. *
  694. * LOCKING:
  695. * spin_lock_irqsave(host_set lock)
  696. */
  697. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  698. struct scsi_cmnd *cmd,
  699. void (*done)(struct scsi_cmnd *),
  700. ata_xlat_func_t xlat_func)
  701. {
  702. struct ata_queued_cmd *qc;
  703. u8 *scsicmd = cmd->cmnd;
  704. VPRINTK("ENTER\n");
  705. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  706. if (!qc)
  707. return;
  708. /* data is present; dma-map it */
  709. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  710. cmd->sc_data_direction == DMA_TO_DEVICE) {
  711. if (unlikely(cmd->request_bufflen < 1)) {
  712. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  713. ap->id, dev->devno);
  714. goto err_out;
  715. }
  716. if (cmd->use_sg)
  717. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  718. else
  719. ata_sg_init_one(qc, cmd->request_buffer,
  720. cmd->request_bufflen);
  721. qc->dma_dir = cmd->sc_data_direction;
  722. }
  723. qc->complete_fn = ata_scsi_qc_complete;
  724. if (xlat_func(qc, scsicmd))
  725. goto err_out;
  726. /* select device, send command to hardware */
  727. if (ata_qc_issue(qc))
  728. goto err_out;
  729. VPRINTK("EXIT\n");
  730. return;
  731. err_out:
  732. ata_qc_free(qc);
  733. ata_bad_cdb(cmd, done);
  734. DPRINTK("EXIT - badcmd\n");
  735. }
  736. /**
  737. * ata_scsi_rbuf_get - Map response buffer.
  738. * @cmd: SCSI command containing buffer to be mapped.
  739. * @buf_out: Pointer to mapped area.
  740. *
  741. * Maps buffer contained within SCSI command @cmd.
  742. *
  743. * LOCKING:
  744. * spin_lock_irqsave(host_set lock)
  745. *
  746. * RETURNS:
  747. * Length of response buffer.
  748. */
  749. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  750. {
  751. u8 *buf;
  752. unsigned int buflen;
  753. if (cmd->use_sg) {
  754. struct scatterlist *sg;
  755. sg = (struct scatterlist *) cmd->request_buffer;
  756. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  757. buflen = sg->length;
  758. } else {
  759. buf = cmd->request_buffer;
  760. buflen = cmd->request_bufflen;
  761. }
  762. *buf_out = buf;
  763. return buflen;
  764. }
  765. /**
  766. * ata_scsi_rbuf_put - Unmap response buffer.
  767. * @cmd: SCSI command containing buffer to be unmapped.
  768. * @buf: buffer to unmap
  769. *
  770. * Unmaps response buffer contained within @cmd.
  771. *
  772. * LOCKING:
  773. * spin_lock_irqsave(host_set lock)
  774. */
  775. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  776. {
  777. if (cmd->use_sg) {
  778. struct scatterlist *sg;
  779. sg = (struct scatterlist *) cmd->request_buffer;
  780. kunmap_atomic(buf - sg->offset, KM_USER0);
  781. }
  782. }
  783. /**
  784. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  785. * @args: device IDENTIFY data / SCSI command of interest.
  786. * @actor: Callback hook for desired SCSI command simulator
  787. *
  788. * Takes care of the hard work of simulating a SCSI command...
  789. * Mapping the response buffer, calling the command's handler,
  790. * and handling the handler's return value. This return value
  791. * indicates whether the handler wishes the SCSI command to be
  792. * completed successfully, or not.
  793. *
  794. * LOCKING:
  795. * spin_lock_irqsave(host_set lock)
  796. */
  797. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  798. unsigned int (*actor) (struct ata_scsi_args *args,
  799. u8 *rbuf, unsigned int buflen))
  800. {
  801. u8 *rbuf;
  802. unsigned int buflen, rc;
  803. struct scsi_cmnd *cmd = args->cmd;
  804. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  805. memset(rbuf, 0, buflen);
  806. rc = actor(args, rbuf, buflen);
  807. ata_scsi_rbuf_put(cmd, rbuf);
  808. if (rc)
  809. ata_bad_cdb(cmd, args->done);
  810. else {
  811. cmd->result = SAM_STAT_GOOD;
  812. args->done(cmd);
  813. }
  814. }
  815. /**
  816. * ata_scsiop_inq_std - Simulate INQUIRY command
  817. * @args: device IDENTIFY data / SCSI command of interest.
  818. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  819. * @buflen: Response buffer length.
  820. *
  821. * Returns standard device identification data associated
  822. * with non-EVPD INQUIRY command output.
  823. *
  824. * LOCKING:
  825. * spin_lock_irqsave(host_set lock)
  826. */
  827. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  828. unsigned int buflen)
  829. {
  830. u8 hdr[] = {
  831. TYPE_DISK,
  832. 0,
  833. 0x5, /* claim SPC-3 version compatibility */
  834. 2,
  835. 95 - 4
  836. };
  837. /* set scsi removeable (RMB) bit per ata bit */
  838. if (ata_id_removeable(args->id))
  839. hdr[1] |= (1 << 7);
  840. VPRINTK("ENTER\n");
  841. memcpy(rbuf, hdr, sizeof(hdr));
  842. if (buflen > 35) {
  843. memcpy(&rbuf[8], "ATA ", 8);
  844. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  845. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  846. if (rbuf[32] == 0 || rbuf[32] == ' ')
  847. memcpy(&rbuf[32], "n/a ", 4);
  848. }
  849. if (buflen > 63) {
  850. const u8 versions[] = {
  851. 0x60, /* SAM-3 (no version claimed) */
  852. 0x03,
  853. 0x20, /* SBC-2 (no version claimed) */
  854. 0x02,
  855. 0x60 /* SPC-3 (no version claimed) */
  856. };
  857. memcpy(rbuf + 59, versions, sizeof(versions));
  858. }
  859. return 0;
  860. }
  861. /**
  862. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  863. * @args: device IDENTIFY data / SCSI command of interest.
  864. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  865. * @buflen: Response buffer length.
  866. *
  867. * Returns list of inquiry EVPD pages available.
  868. *
  869. * LOCKING:
  870. * spin_lock_irqsave(host_set lock)
  871. */
  872. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  873. unsigned int buflen)
  874. {
  875. const u8 pages[] = {
  876. 0x00, /* page 0x00, this page */
  877. 0x80, /* page 0x80, unit serial no page */
  878. 0x83 /* page 0x83, device ident page */
  879. };
  880. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  881. if (buflen > 6)
  882. memcpy(rbuf + 4, pages, sizeof(pages));
  883. return 0;
  884. }
  885. /**
  886. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  887. * @args: device IDENTIFY data / SCSI command of interest.
  888. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  889. * @buflen: Response buffer length.
  890. *
  891. * Returns ATA device serial number.
  892. *
  893. * LOCKING:
  894. * spin_lock_irqsave(host_set lock)
  895. */
  896. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  897. unsigned int buflen)
  898. {
  899. const u8 hdr[] = {
  900. 0,
  901. 0x80, /* this page code */
  902. 0,
  903. ATA_SERNO_LEN, /* page len */
  904. };
  905. memcpy(rbuf, hdr, sizeof(hdr));
  906. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  907. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  908. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  909. return 0;
  910. }
  911. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  912. /**
  913. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  914. * @args: device IDENTIFY data / SCSI command of interest.
  915. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  916. * @buflen: Response buffer length.
  917. *
  918. * Returns device identification. Currently hardcoded to
  919. * return "Linux ATA-SCSI simulator".
  920. *
  921. * LOCKING:
  922. * spin_lock_irqsave(host_set lock)
  923. */
  924. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  925. unsigned int buflen)
  926. {
  927. rbuf[1] = 0x83; /* this page code */
  928. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  929. /* our one and only identification descriptor (vendor-specific) */
  930. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  931. rbuf[4 + 0] = 2; /* code set: ASCII */
  932. rbuf[4 + 3] = strlen(inq_83_str);
  933. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  934. }
  935. return 0;
  936. }
  937. /**
  938. * ata_scsiop_noop - Command handler that simply returns success.
  939. * @args: device IDENTIFY data / SCSI command of interest.
  940. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  941. * @buflen: Response buffer length.
  942. *
  943. * No operation. Simply returns success to caller, to indicate
  944. * that the caller should successfully complete this SCSI command.
  945. *
  946. * LOCKING:
  947. * spin_lock_irqsave(host_set lock)
  948. */
  949. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  950. unsigned int buflen)
  951. {
  952. VPRINTK("ENTER\n");
  953. return 0;
  954. }
  955. /**
  956. * ata_msense_push - Push data onto MODE SENSE data output buffer
  957. * @ptr_io: (input/output) Location to store more output data
  958. * @last: End of output data buffer
  959. * @buf: Pointer to BLOB being added to output buffer
  960. * @buflen: Length of BLOB
  961. *
  962. * Store MODE SENSE data on an output buffer.
  963. *
  964. * LOCKING:
  965. * None.
  966. */
  967. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  968. const u8 *buf, unsigned int buflen)
  969. {
  970. u8 *ptr = *ptr_io;
  971. if ((ptr + buflen - 1) > last)
  972. return;
  973. memcpy(ptr, buf, buflen);
  974. ptr += buflen;
  975. *ptr_io = ptr;
  976. }
  977. /**
  978. * ata_msense_caching - Simulate MODE SENSE caching info page
  979. * @id: device IDENTIFY data
  980. * @ptr_io: (input/output) Location to store more output data
  981. * @last: End of output data buffer
  982. *
  983. * Generate a caching info page, which conditionally indicates
  984. * write caching to the SCSI layer, depending on device
  985. * capabilities.
  986. *
  987. * LOCKING:
  988. * None.
  989. */
  990. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  991. const u8 *last)
  992. {
  993. u8 page[] = {
  994. 0x8, /* page code */
  995. 0x12, /* page length */
  996. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
  997. 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
  998. };
  999. if (ata_id_wcache_enabled(id))
  1000. page[2] |= (1 << 2); /* write cache enable */
  1001. if (!ata_id_rahead_enabled(id))
  1002. page[12] |= (1 << 5); /* disable read ahead */
  1003. ata_msense_push(ptr_io, last, page, sizeof(page));
  1004. return sizeof(page);
  1005. }
  1006. /**
  1007. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1008. * @dev: Device associated with this MODE SENSE command
  1009. * @ptr_io: (input/output) Location to store more output data
  1010. * @last: End of output data buffer
  1011. *
  1012. * Generate a generic MODE SENSE control mode page.
  1013. *
  1014. * LOCKING:
  1015. * None.
  1016. */
  1017. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  1018. {
  1019. const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
  1020. /* byte 2: set the descriptor format sense data bit (bit 2)
  1021. * since we need to support returning this format for SAT
  1022. * commands and any SCSI commands against a 48b LBA device.
  1023. */
  1024. ata_msense_push(ptr_io, last, page, sizeof(page));
  1025. return sizeof(page);
  1026. }
  1027. /**
  1028. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1029. * @dev: Device associated with this MODE SENSE command
  1030. * @ptr_io: (input/output) Location to store more output data
  1031. * @last: End of output data buffer
  1032. *
  1033. * Generate a generic MODE SENSE r/w error recovery page.
  1034. *
  1035. * LOCKING:
  1036. * None.
  1037. */
  1038. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  1039. {
  1040. const u8 page[] = {
  1041. 0x1, /* page code */
  1042. 0xa, /* page length */
  1043. (1 << 7) | (1 << 6), /* note auto r/w reallocation */
  1044. 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
  1045. };
  1046. ata_msense_push(ptr_io, last, page, sizeof(page));
  1047. return sizeof(page);
  1048. }
  1049. /**
  1050. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1051. * @args: device IDENTIFY data / SCSI command of interest.
  1052. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1053. * @buflen: Response buffer length.
  1054. *
  1055. * Simulate MODE SENSE commands.
  1056. *
  1057. * LOCKING:
  1058. * spin_lock_irqsave(host_set lock)
  1059. */
  1060. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1061. unsigned int buflen)
  1062. {
  1063. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1064. unsigned int page_control, six_byte, output_len;
  1065. VPRINTK("ENTER\n");
  1066. six_byte = (scsicmd[0] == MODE_SENSE);
  1067. /* we only support saved and current values (which we treat
  1068. * in the same manner)
  1069. */
  1070. page_control = scsicmd[2] >> 6;
  1071. if ((page_control != 0) && (page_control != 3))
  1072. return 1;
  1073. if (six_byte)
  1074. output_len = 4;
  1075. else
  1076. output_len = 8;
  1077. p = rbuf + output_len;
  1078. last = rbuf + buflen - 1;
  1079. switch(scsicmd[2] & 0x3f) {
  1080. case 0x01: /* r/w error recovery */
  1081. output_len += ata_msense_rw_recovery(&p, last);
  1082. break;
  1083. case 0x08: /* caching */
  1084. output_len += ata_msense_caching(args->id, &p, last);
  1085. break;
  1086. case 0x0a: { /* control mode */
  1087. output_len += ata_msense_ctl_mode(&p, last);
  1088. break;
  1089. }
  1090. case 0x3f: /* all pages */
  1091. output_len += ata_msense_rw_recovery(&p, last);
  1092. output_len += ata_msense_caching(args->id, &p, last);
  1093. output_len += ata_msense_ctl_mode(&p, last);
  1094. break;
  1095. default: /* invalid page code */
  1096. return 1;
  1097. }
  1098. if (six_byte) {
  1099. output_len--;
  1100. rbuf[0] = output_len;
  1101. } else {
  1102. output_len -= 2;
  1103. rbuf[0] = output_len >> 8;
  1104. rbuf[1] = output_len;
  1105. }
  1106. return 0;
  1107. }
  1108. /**
  1109. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  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 READ CAPACITY commands.
  1115. *
  1116. * LOCKING:
  1117. * spin_lock_irqsave(host_set lock)
  1118. */
  1119. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1120. unsigned int buflen)
  1121. {
  1122. u64 n_sectors;
  1123. u32 tmp;
  1124. VPRINTK("ENTER\n");
  1125. if (ata_id_has_lba(args->id)) {
  1126. if (ata_id_has_lba48(args->id))
  1127. n_sectors = ata_id_u64(args->id, 100);
  1128. else
  1129. n_sectors = ata_id_u32(args->id, 60);
  1130. } else {
  1131. /* CHS default translation */
  1132. n_sectors = args->id[1] * args->id[3] * args->id[6];
  1133. if (ata_id_current_chs_valid(args->id))
  1134. /* CHS current translation */
  1135. n_sectors = ata_id_u32(args->id, 57);
  1136. }
  1137. n_sectors--; /* ATA TotalUserSectors - 1 */
  1138. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1139. if( n_sectors >= 0xffffffffULL )
  1140. tmp = 0xffffffff ; /* Return max count on overflow */
  1141. else
  1142. tmp = n_sectors ;
  1143. /* sector count, 32-bit */
  1144. rbuf[0] = tmp >> (8 * 3);
  1145. rbuf[1] = tmp >> (8 * 2);
  1146. rbuf[2] = tmp >> (8 * 1);
  1147. rbuf[3] = tmp;
  1148. /* sector size */
  1149. tmp = ATA_SECT_SIZE;
  1150. rbuf[6] = tmp >> 8;
  1151. rbuf[7] = tmp;
  1152. } else {
  1153. /* sector count, 64-bit */
  1154. tmp = n_sectors >> (8 * 4);
  1155. rbuf[2] = tmp >> (8 * 3);
  1156. rbuf[3] = tmp >> (8 * 2);
  1157. rbuf[4] = tmp >> (8 * 1);
  1158. rbuf[5] = tmp;
  1159. tmp = n_sectors;
  1160. rbuf[6] = tmp >> (8 * 3);
  1161. rbuf[7] = tmp >> (8 * 2);
  1162. rbuf[8] = tmp >> (8 * 1);
  1163. rbuf[9] = tmp;
  1164. /* sector size */
  1165. tmp = ATA_SECT_SIZE;
  1166. rbuf[12] = tmp >> 8;
  1167. rbuf[13] = tmp;
  1168. }
  1169. return 0;
  1170. }
  1171. /**
  1172. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1173. * @args: device IDENTIFY data / SCSI command of interest.
  1174. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1175. * @buflen: Response buffer length.
  1176. *
  1177. * Simulate REPORT LUNS command.
  1178. *
  1179. * LOCKING:
  1180. * spin_lock_irqsave(host_set lock)
  1181. */
  1182. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1183. unsigned int buflen)
  1184. {
  1185. VPRINTK("ENTER\n");
  1186. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1187. return 0;
  1188. }
  1189. /**
  1190. * ata_scsi_badcmd - End a SCSI request with an error
  1191. * @cmd: SCSI request to be handled
  1192. * @done: SCSI command completion function
  1193. * @asc: SCSI-defined additional sense code
  1194. * @ascq: SCSI-defined additional sense code qualifier
  1195. *
  1196. * Helper function that completes a SCSI command with
  1197. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1198. * and the specified additional sense codes.
  1199. *
  1200. * LOCKING:
  1201. * spin_lock_irqsave(host_set lock)
  1202. */
  1203. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1204. {
  1205. DPRINTK("ENTER\n");
  1206. cmd->result = SAM_STAT_CHECK_CONDITION;
  1207. cmd->sense_buffer[0] = 0x70;
  1208. cmd->sense_buffer[2] = ILLEGAL_REQUEST;
  1209. cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
  1210. cmd->sense_buffer[12] = asc;
  1211. cmd->sense_buffer[13] = ascq;
  1212. done(cmd);
  1213. }
  1214. static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  1215. {
  1216. struct scsi_cmnd *cmd = qc->scsicmd;
  1217. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
  1218. DPRINTK("request check condition\n");
  1219. cmd->result = SAM_STAT_CHECK_CONDITION;
  1220. qc->scsidone(cmd);
  1221. return 1;
  1222. } else {
  1223. u8 *scsicmd = cmd->cmnd;
  1224. if (scsicmd[0] == INQUIRY) {
  1225. u8 *buf = NULL;
  1226. unsigned int buflen;
  1227. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1228. buf[2] = 0x5;
  1229. buf[3] = (buf[3] & 0xf0) | 2;
  1230. ata_scsi_rbuf_put(cmd, buf);
  1231. }
  1232. cmd->result = SAM_STAT_GOOD;
  1233. }
  1234. qc->scsidone(cmd);
  1235. return 0;
  1236. }
  1237. /**
  1238. * atapi_xlat - Initialize PACKET taskfile
  1239. * @qc: command structure to be initialized
  1240. * @scsicmd: SCSI CDB associated with this PACKET command
  1241. *
  1242. * LOCKING:
  1243. * spin_lock_irqsave(host_set lock)
  1244. *
  1245. * RETURNS:
  1246. * Zero on success, non-zero on failure.
  1247. */
  1248. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  1249. {
  1250. struct scsi_cmnd *cmd = qc->scsicmd;
  1251. struct ata_device *dev = qc->dev;
  1252. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1253. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1254. if (!using_pio)
  1255. /* Check whether ATAPI DMA is safe */
  1256. if (ata_check_atapi_dma(qc))
  1257. using_pio = 1;
  1258. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1259. qc->complete_fn = atapi_qc_complete;
  1260. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1261. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1262. qc->tf.flags |= ATA_TFLAG_WRITE;
  1263. DPRINTK("direction: write\n");
  1264. }
  1265. qc->tf.command = ATA_CMD_PACKET;
  1266. /* no data, or PIO data xfer */
  1267. if (using_pio || nodata) {
  1268. if (nodata)
  1269. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1270. else
  1271. qc->tf.protocol = ATA_PROT_ATAPI;
  1272. qc->tf.lbam = (8 * 1024) & 0xff;
  1273. qc->tf.lbah = (8 * 1024) >> 8;
  1274. }
  1275. /* DMA data xfer */
  1276. else {
  1277. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1278. qc->tf.feature |= ATAPI_PKT_DMA;
  1279. #ifdef ATAPI_ENABLE_DMADIR
  1280. /* some SATA bridges need us to indicate data xfer direction */
  1281. if (cmd->sc_data_direction != DMA_TO_DEVICE)
  1282. qc->tf.feature |= ATAPI_DMADIR;
  1283. #endif
  1284. }
  1285. qc->nbytes = cmd->bufflen;
  1286. return 0;
  1287. }
  1288. /**
  1289. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1290. * @ap: ATA port to which the device is attached
  1291. * @scsidev: SCSI device from which we derive the ATA device
  1292. *
  1293. * Given various information provided in struct scsi_cmnd,
  1294. * map that onto an ATA bus, and using that mapping
  1295. * determine which ata_device is associated with the
  1296. * SCSI command to be sent.
  1297. *
  1298. * LOCKING:
  1299. * spin_lock_irqsave(host_set lock)
  1300. *
  1301. * RETURNS:
  1302. * Associated ATA device, or %NULL if not found.
  1303. */
  1304. static struct ata_device *
  1305. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
  1306. {
  1307. struct ata_device *dev;
  1308. /* skip commands not addressed to targets we simulate */
  1309. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1310. dev = &ap->device[scsidev->id];
  1311. else
  1312. return NULL;
  1313. if (unlikely((scsidev->channel != 0) ||
  1314. (scsidev->lun != 0)))
  1315. return NULL;
  1316. if (unlikely(!ata_dev_present(dev)))
  1317. return NULL;
  1318. if (!atapi_enabled) {
  1319. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1320. return NULL;
  1321. }
  1322. return dev;
  1323. }
  1324. /**
  1325. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1326. * @dev: ATA device
  1327. * @cmd: SCSI command opcode to consider
  1328. *
  1329. * Look up the SCSI command given, and determine whether the
  1330. * SCSI command is to be translated or simulated.
  1331. *
  1332. * RETURNS:
  1333. * Pointer to translation function if possible, %NULL if not.
  1334. */
  1335. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  1336. {
  1337. switch (cmd) {
  1338. case READ_6:
  1339. case READ_10:
  1340. case READ_16:
  1341. case WRITE_6:
  1342. case WRITE_10:
  1343. case WRITE_16:
  1344. return ata_scsi_rw_xlat;
  1345. case SYNCHRONIZE_CACHE:
  1346. if (ata_try_flush_cache(dev))
  1347. return ata_scsi_flush_xlat;
  1348. break;
  1349. case VERIFY:
  1350. case VERIFY_16:
  1351. return ata_scsi_verify_xlat;
  1352. case START_STOP:
  1353. return ata_scsi_start_stop_xlat;
  1354. }
  1355. return NULL;
  1356. }
  1357. /**
  1358. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  1359. * @ap: ATA port to which the command was being sent
  1360. * @cmd: SCSI command to dump
  1361. *
  1362. * Prints the contents of a SCSI command via printk().
  1363. */
  1364. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  1365. struct scsi_cmnd *cmd)
  1366. {
  1367. #ifdef ATA_DEBUG
  1368. struct scsi_device *scsidev = cmd->device;
  1369. u8 *scsicmd = cmd->cmnd;
  1370. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1371. ap->id,
  1372. scsidev->channel, scsidev->id, scsidev->lun,
  1373. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  1374. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  1375. scsicmd[8]);
  1376. #endif
  1377. }
  1378. /**
  1379. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  1380. * @cmd: SCSI command to be sent
  1381. * @done: Completion function, called when command is complete
  1382. *
  1383. * In some cases, this function translates SCSI commands into
  1384. * ATA taskfiles, and queues the taskfiles to be sent to
  1385. * hardware. In other cases, this function simulates a
  1386. * SCSI device by evaluating and responding to certain
  1387. * SCSI commands. This creates the overall effect of
  1388. * ATA and ATAPI devices appearing as SCSI devices.
  1389. *
  1390. * LOCKING:
  1391. * Releases scsi-layer-held lock, and obtains host_set lock.
  1392. *
  1393. * RETURNS:
  1394. * Zero.
  1395. */
  1396. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  1397. {
  1398. struct ata_port *ap;
  1399. struct ata_device *dev;
  1400. struct scsi_device *scsidev = cmd->device;
  1401. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  1402. ata_scsi_dump_cdb(ap, cmd);
  1403. dev = ata_scsi_find_dev(ap, scsidev);
  1404. if (unlikely(!dev)) {
  1405. cmd->result = (DID_BAD_TARGET << 16);
  1406. done(cmd);
  1407. goto out_unlock;
  1408. }
  1409. if (dev->class == ATA_DEV_ATA) {
  1410. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  1411. cmd->cmnd[0]);
  1412. if (xlat_func)
  1413. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  1414. else
  1415. ata_scsi_simulate(dev->id, cmd, done);
  1416. } else
  1417. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  1418. out_unlock:
  1419. return 0;
  1420. }
  1421. /**
  1422. * ata_scsi_simulate - simulate SCSI command on ATA device
  1423. * @id: current IDENTIFY data for target device.
  1424. * @cmd: SCSI command being sent to device.
  1425. * @done: SCSI command completion function.
  1426. *
  1427. * Interprets and directly executes a select list of SCSI commands
  1428. * that can be handled internally.
  1429. *
  1430. * LOCKING:
  1431. * spin_lock_irqsave(host_set lock)
  1432. */
  1433. void ata_scsi_simulate(u16 *id,
  1434. struct scsi_cmnd *cmd,
  1435. void (*done)(struct scsi_cmnd *))
  1436. {
  1437. struct ata_scsi_args args;
  1438. u8 *scsicmd = cmd->cmnd;
  1439. args.id = id;
  1440. args.cmd = cmd;
  1441. args.done = done;
  1442. switch(scsicmd[0]) {
  1443. /* no-op's, complete with success */
  1444. case SYNCHRONIZE_CACHE:
  1445. case REZERO_UNIT:
  1446. case SEEK_6:
  1447. case SEEK_10:
  1448. case TEST_UNIT_READY:
  1449. case FORMAT_UNIT: /* FIXME: correct? */
  1450. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  1451. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  1452. break;
  1453. case INQUIRY:
  1454. if (scsicmd[1] & 2) /* is CmdDt set? */
  1455. ata_bad_cdb(cmd, done);
  1456. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  1457. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  1458. else if (scsicmd[2] == 0x00)
  1459. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  1460. else if (scsicmd[2] == 0x80)
  1461. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  1462. else if (scsicmd[2] == 0x83)
  1463. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  1464. else
  1465. ata_bad_cdb(cmd, done);
  1466. break;
  1467. case MODE_SENSE:
  1468. case MODE_SENSE_10:
  1469. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  1470. break;
  1471. case MODE_SELECT: /* unconditionally return */
  1472. case MODE_SELECT_10: /* bad-field-in-cdb */
  1473. ata_bad_cdb(cmd, done);
  1474. break;
  1475. case READ_CAPACITY:
  1476. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1477. break;
  1478. case SERVICE_ACTION_IN:
  1479. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  1480. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1481. else
  1482. ata_bad_cdb(cmd, done);
  1483. break;
  1484. case REPORT_LUNS:
  1485. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  1486. break;
  1487. /* mandantory commands we haven't implemented yet */
  1488. case REQUEST_SENSE:
  1489. /* all other commands */
  1490. default:
  1491. ata_bad_scsiop(cmd, done);
  1492. break;
  1493. }
  1494. }