libata-scsi.c 41 KB

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