libata-scsi.c 47 KB

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