libata-scsi.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  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/scsi_host.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_request.h>
  42. #include <linux/libata.h>
  43. #include <linux/hdreg.h>
  44. #include <asm/uaccess.h>
  45. #include "libata.h"
  46. #define SECTOR_SIZE 512
  47. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
  48. static struct ata_device *
  49. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
  50. #define RW_RECOVERY_MPAGE 0x1
  51. #define RW_RECOVERY_MPAGE_LEN 12
  52. #define CACHE_MPAGE 0x8
  53. #define CACHE_MPAGE_LEN 20
  54. #define CONTROL_MPAGE 0xa
  55. #define CONTROL_MPAGE_LEN 12
  56. #define ALL_MPAGES 0x3f
  57. #define ALL_SUB_MPAGES 0xff
  58. static const u8 def_rw_recovery_mpage[] = {
  59. RW_RECOVERY_MPAGE,
  60. RW_RECOVERY_MPAGE_LEN - 2,
  61. (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
  62. (1 << 6), /* ARRE (auto read reallocation) */
  63. 0, /* read retry count */
  64. 0, 0, 0, 0,
  65. 0, /* write retry count */
  66. 0, 0, 0
  67. };
  68. static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
  69. CACHE_MPAGE,
  70. CACHE_MPAGE_LEN - 2,
  71. 0, /* contains WCE, needs to be 0 for logic */
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 0, /* contains DRA, needs to be 0 for logic */
  74. 0, 0, 0, 0, 0, 0, 0
  75. };
  76. static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
  77. CONTROL_MPAGE,
  78. CONTROL_MPAGE_LEN - 2,
  79. 2, /* DSENSE=0, GLTSD=1 */
  80. 0, /* [QAM+QERR may be 1, see 05-359r1] */
  81. 0, 0, 0, 0, 0xff, 0xff,
  82. 0, 30 /* extended self test time, see 05-359r1 */
  83. };
  84. static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
  85. void (*done)(struct scsi_cmnd *))
  86. {
  87. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  88. /* "Invalid field in cbd" */
  89. done(cmd);
  90. }
  91. /**
  92. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  93. * @sdev: SCSI device for which BIOS geometry is to be determined
  94. * @bdev: block device associated with @sdev
  95. * @capacity: capacity of SCSI device
  96. * @geom: location to which geometry will be output
  97. *
  98. * Generic bios head/sector/cylinder calculator
  99. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  100. * mapping. Some situations may arise where the disk is not
  101. * bootable if this is not used.
  102. *
  103. * LOCKING:
  104. * Defined by the SCSI layer. We don't really care.
  105. *
  106. * RETURNS:
  107. * Zero.
  108. */
  109. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  110. sector_t capacity, int geom[])
  111. {
  112. geom[0] = 255;
  113. geom[1] = 63;
  114. sector_div(capacity, 255*63);
  115. geom[2] = capacity;
  116. return 0;
  117. }
  118. /**
  119. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  120. * @dev: Device to whom we are issuing command
  121. * @arg: User provided data for issuing command
  122. *
  123. * LOCKING:
  124. * Defined by the SCSI layer. We don't really care.
  125. *
  126. * RETURNS:
  127. * Zero on success, negative errno on error.
  128. */
  129. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  130. {
  131. int rc = 0;
  132. u8 scsi_cmd[MAX_COMMAND_SIZE];
  133. u8 args[4], *argbuf = NULL;
  134. int argsize = 0;
  135. struct scsi_request *sreq;
  136. if (NULL == (void *)arg)
  137. return -EINVAL;
  138. if (copy_from_user(args, arg, sizeof(args)))
  139. return -EFAULT;
  140. sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
  141. if (!sreq)
  142. return -EINTR;
  143. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  144. if (args[3]) {
  145. argsize = SECTOR_SIZE * args[3];
  146. argbuf = kmalloc(argsize, GFP_KERNEL);
  147. if (argbuf == NULL) {
  148. rc = -ENOMEM;
  149. goto error;
  150. }
  151. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  152. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  153. block count in sector count field */
  154. sreq->sr_data_direction = DMA_FROM_DEVICE;
  155. } else {
  156. scsi_cmd[1] = (3 << 1); /* Non-data */
  157. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  158. sreq->sr_data_direction = DMA_NONE;
  159. }
  160. scsi_cmd[0] = ATA_16;
  161. scsi_cmd[4] = args[2];
  162. if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
  163. scsi_cmd[6] = args[3];
  164. scsi_cmd[8] = args[1];
  165. scsi_cmd[10] = 0x4f;
  166. scsi_cmd[12] = 0xc2;
  167. } else {
  168. scsi_cmd[6] = args[1];
  169. }
  170. scsi_cmd[14] = args[0];
  171. /* Good values for timeout and retries? Values below
  172. from scsi_ioctl_send_command() for default case... */
  173. scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
  174. if (sreq->sr_result) {
  175. rc = -EIO;
  176. goto error;
  177. }
  178. /* Need code to retrieve data from check condition? */
  179. if ((argbuf)
  180. && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
  181. rc = -EFAULT;
  182. error:
  183. scsi_release_request(sreq);
  184. if (argbuf)
  185. kfree(argbuf);
  186. return rc;
  187. }
  188. /**
  189. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  190. * @dev: Device to whom we are issuing command
  191. * @arg: User provided data for issuing command
  192. *
  193. * LOCKING:
  194. * Defined by the SCSI layer. We don't really care.
  195. *
  196. * RETURNS:
  197. * Zero on success, negative errno on error.
  198. */
  199. int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  200. {
  201. int rc = 0;
  202. u8 scsi_cmd[MAX_COMMAND_SIZE];
  203. u8 args[7];
  204. struct scsi_request *sreq;
  205. if (NULL == (void *)arg)
  206. return -EINVAL;
  207. if (copy_from_user(args, arg, sizeof(args)))
  208. return -EFAULT;
  209. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  210. scsi_cmd[0] = ATA_16;
  211. scsi_cmd[1] = (3 << 1); /* Non-data */
  212. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  213. scsi_cmd[4] = args[1];
  214. scsi_cmd[6] = args[2];
  215. scsi_cmd[8] = args[3];
  216. scsi_cmd[10] = args[4];
  217. scsi_cmd[12] = args[5];
  218. scsi_cmd[14] = args[0];
  219. sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
  220. if (!sreq) {
  221. rc = -EINTR;
  222. goto error;
  223. }
  224. sreq->sr_data_direction = DMA_NONE;
  225. /* Good values for timeout and retries? Values below
  226. from scsi_ioctl_send_command() for default case... */
  227. scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
  228. if (sreq->sr_result) {
  229. rc = -EIO;
  230. goto error;
  231. }
  232. /* Need code to retrieve data from check condition? */
  233. error:
  234. scsi_release_request(sreq);
  235. return rc;
  236. }
  237. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  238. {
  239. struct ata_port *ap;
  240. struct ata_device *dev;
  241. int val = -EINVAL, rc = -EINVAL;
  242. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  243. if (!ap)
  244. goto out;
  245. dev = ata_scsi_find_dev(ap, scsidev);
  246. if (!dev) {
  247. rc = -ENODEV;
  248. goto out;
  249. }
  250. switch (cmd) {
  251. case ATA_IOC_GET_IO32:
  252. val = 0;
  253. if (copy_to_user(arg, &val, 1))
  254. return -EFAULT;
  255. return 0;
  256. case ATA_IOC_SET_IO32:
  257. val = (unsigned long) arg;
  258. if (val != 0)
  259. return -EINVAL;
  260. return 0;
  261. case HDIO_DRIVE_CMD:
  262. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  263. return -EACCES;
  264. return ata_cmd_ioctl(scsidev, arg);
  265. case HDIO_DRIVE_TASK:
  266. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  267. return -EACCES;
  268. return ata_task_ioctl(scsidev, arg);
  269. default:
  270. rc = -ENOTTY;
  271. break;
  272. }
  273. out:
  274. return rc;
  275. }
  276. /**
  277. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  278. * @ap: ATA port to which the new command is attached
  279. * @dev: ATA device to which the new command is attached
  280. * @cmd: SCSI command that originated this ATA command
  281. * @done: SCSI command completion function
  282. *
  283. * Obtain a reference to an unused ata_queued_cmd structure,
  284. * which is the basic libata structure representing a single
  285. * ATA command sent to the hardware.
  286. *
  287. * If a command was available, fill in the SCSI-specific
  288. * portions of the structure with information on the
  289. * current command.
  290. *
  291. * LOCKING:
  292. * spin_lock_irqsave(host_set lock)
  293. *
  294. * RETURNS:
  295. * Command allocated, or %NULL if none available.
  296. */
  297. struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
  298. struct ata_device *dev,
  299. struct scsi_cmnd *cmd,
  300. void (*done)(struct scsi_cmnd *))
  301. {
  302. struct ata_queued_cmd *qc;
  303. qc = ata_qc_new_init(ap, dev);
  304. if (qc) {
  305. qc->scsicmd = cmd;
  306. qc->scsidone = done;
  307. if (cmd->use_sg) {
  308. qc->__sg = (struct scatterlist *) cmd->request_buffer;
  309. qc->n_elem = cmd->use_sg;
  310. } else {
  311. qc->__sg = &qc->sgent;
  312. qc->n_elem = 1;
  313. }
  314. } else {
  315. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  316. done(cmd);
  317. }
  318. return qc;
  319. }
  320. /**
  321. * ata_dump_status - user friendly display of error info
  322. * @id: id of the port in question
  323. * @tf: ptr to filled out taskfile
  324. *
  325. * Decode and dump the ATA error/status registers for the user so
  326. * that they have some idea what really happened at the non
  327. * make-believe layer.
  328. *
  329. * LOCKING:
  330. * inherited from caller
  331. */
  332. void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  333. {
  334. u8 stat = tf->command, err = tf->feature;
  335. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  336. if (stat & ATA_BUSY) {
  337. printk("Busy }\n"); /* Data is not valid in this case */
  338. } else {
  339. if (stat & 0x40) printk("DriveReady ");
  340. if (stat & 0x20) printk("DeviceFault ");
  341. if (stat & 0x10) printk("SeekComplete ");
  342. if (stat & 0x08) printk("DataRequest ");
  343. if (stat & 0x04) printk("CorrectedError ");
  344. if (stat & 0x02) printk("Index ");
  345. if (stat & 0x01) printk("Error ");
  346. printk("}\n");
  347. if (err) {
  348. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  349. if (err & 0x04) printk("DriveStatusError ");
  350. if (err & 0x80) {
  351. if (err & 0x04) printk("BadCRC ");
  352. else printk("Sector ");
  353. }
  354. if (err & 0x40) printk("UncorrectableError ");
  355. if (err & 0x10) printk("SectorIdNotFound ");
  356. if (err & 0x02) printk("TrackZeroNotFound ");
  357. if (err & 0x01) printk("AddrMarkNotFound ");
  358. printk("}\n");
  359. }
  360. }
  361. }
  362. /**
  363. * ata_to_sense_error - convert ATA error to SCSI error
  364. * @drv_stat: value contained in ATA status register
  365. * @drv_err: value contained in ATA error register
  366. * @sk: the sense key we'll fill out
  367. * @asc: the additional sense code we'll fill out
  368. * @ascq: the additional sense code qualifier we'll fill out
  369. *
  370. * Converts an ATA error into a SCSI error. Fill out pointers to
  371. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  372. * format sense blocks.
  373. *
  374. * LOCKING:
  375. * spin_lock_irqsave(host_set lock)
  376. */
  377. void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
  378. u8 *ascq)
  379. {
  380. int i;
  381. /* Based on the 3ware driver translation table */
  382. static unsigned char sense_table[][4] = {
  383. /* BBD|ECC|ID|MAR */
  384. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  385. /* BBD|ECC|ID */
  386. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  387. /* ECC|MC|MARK */
  388. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  389. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  390. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  391. /* MC|ID|ABRT|TRK0|MARK */
  392. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  393. /* MCR|MARK */
  394. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  395. /* Bad address mark */
  396. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  397. /* TRK0 */
  398. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  399. /* Abort & !ICRC */
  400. {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
  401. /* Media change request */
  402. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  403. /* SRV */
  404. {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
  405. /* Media change */
  406. {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
  407. /* ECC */
  408. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  409. /* BBD - block marked bad */
  410. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  411. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  412. };
  413. static unsigned char stat_table[][4] = {
  414. /* Must be first because BUSY means no other bits valid */
  415. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  416. {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
  417. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  418. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  419. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  420. };
  421. /*
  422. * Is this an error we can process/parse
  423. */
  424. if (drv_stat & ATA_BUSY) {
  425. drv_err = 0; /* Ignore the err bits, they're invalid */
  426. }
  427. if (drv_err) {
  428. /* Look for drv_err */
  429. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  430. /* Look for best matches first */
  431. if ((sense_table[i][0] & drv_err) ==
  432. sense_table[i][0]) {
  433. *sk = sense_table[i][1];
  434. *asc = sense_table[i][2];
  435. *ascq = sense_table[i][3];
  436. goto translate_done;
  437. }
  438. }
  439. /* No immediate match */
  440. printk(KERN_WARNING "ata%u: no sense translation for "
  441. "error 0x%02x\n", id, drv_err);
  442. }
  443. /* Fall back to interpreting status bits */
  444. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  445. if (stat_table[i][0] & drv_stat) {
  446. *sk = stat_table[i][1];
  447. *asc = stat_table[i][2];
  448. *ascq = stat_table[i][3];
  449. goto translate_done;
  450. }
  451. }
  452. /* No error? Undecoded? */
  453. printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
  454. id, drv_stat);
  455. /* For our last chance pick, use medium read error because
  456. * it's much more common than an ATA drive telling you a write
  457. * has failed.
  458. */
  459. *sk = MEDIUM_ERROR;
  460. *asc = 0x11; /* "unrecovered read error" */
  461. *ascq = 0x04; /* "auto-reallocation failed" */
  462. translate_done:
  463. printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
  464. "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
  465. *sk, *asc, *ascq);
  466. return;
  467. }
  468. /*
  469. * ata_gen_ata_desc_sense - Generate check condition sense block.
  470. * @qc: Command that completed.
  471. *
  472. * This function is specific to the ATA descriptor format sense
  473. * block specified for the ATA pass through commands. Regardless
  474. * of whether the command errored or not, return a sense
  475. * block. Copy all controller registers into the sense
  476. * block. Clear sense key, ASC & ASCQ if there is no error.
  477. *
  478. * LOCKING:
  479. * spin_lock_irqsave(host_set lock)
  480. */
  481. void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
  482. {
  483. struct scsi_cmnd *cmd = qc->scsicmd;
  484. struct ata_taskfile *tf = &qc->tf;
  485. unsigned char *sb = cmd->sense_buffer;
  486. unsigned char *desc = sb + 8;
  487. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  488. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  489. /*
  490. * Read the controller registers.
  491. */
  492. assert(NULL != qc->ap->ops->tf_read);
  493. qc->ap->ops->tf_read(qc->ap, tf);
  494. /*
  495. * Use ata_to_sense_error() to map status register bits
  496. * onto sense key, asc & ascq.
  497. */
  498. if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  499. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  500. &sb[1], &sb[2], &sb[3]);
  501. sb[1] &= 0x0f;
  502. }
  503. /*
  504. * Sense data is current and format is descriptor.
  505. */
  506. sb[0] = 0x72;
  507. desc[0] = 0x09;
  508. /*
  509. * Set length of additional sense data.
  510. * Since we only populate descriptor 0, the total
  511. * length is the same (fixed) length as descriptor 0.
  512. */
  513. desc[1] = sb[7] = 14;
  514. /*
  515. * Copy registers into sense buffer.
  516. */
  517. desc[2] = 0x00;
  518. desc[3] = tf->feature; /* == error reg */
  519. desc[5] = tf->nsect;
  520. desc[7] = tf->lbal;
  521. desc[9] = tf->lbam;
  522. desc[11] = tf->lbah;
  523. desc[12] = tf->device;
  524. desc[13] = tf->command; /* == status reg */
  525. /*
  526. * Fill in Extend bit, and the high order bytes
  527. * if applicable.
  528. */
  529. if (tf->flags & ATA_TFLAG_LBA48) {
  530. desc[2] |= 0x01;
  531. desc[4] = tf->hob_nsect;
  532. desc[6] = tf->hob_lbal;
  533. desc[8] = tf->hob_lbam;
  534. desc[10] = tf->hob_lbah;
  535. }
  536. }
  537. /**
  538. * ata_gen_fixed_sense - generate a SCSI fixed sense block
  539. * @qc: Command that we are erroring out
  540. *
  541. * Leverage ata_to_sense_error() to give us the codes. Fit our
  542. * LBA in here if there's room.
  543. *
  544. * LOCKING:
  545. * inherited from caller
  546. */
  547. void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
  548. {
  549. struct scsi_cmnd *cmd = qc->scsicmd;
  550. struct ata_taskfile *tf = &qc->tf;
  551. unsigned char *sb = cmd->sense_buffer;
  552. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  553. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  554. /*
  555. * Read the controller registers.
  556. */
  557. assert(NULL != qc->ap->ops->tf_read);
  558. qc->ap->ops->tf_read(qc->ap, tf);
  559. /*
  560. * Use ata_to_sense_error() to map status register bits
  561. * onto sense key, asc & ascq.
  562. */
  563. if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
  564. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  565. &sb[2], &sb[12], &sb[13]);
  566. sb[2] &= 0x0f;
  567. }
  568. sb[0] = 0x70;
  569. sb[7] = 0x0a;
  570. if (tf->flags & ATA_TFLAG_LBA48) {
  571. /* TODO: find solution for LBA48 descriptors */
  572. }
  573. else if (tf->flags & ATA_TFLAG_LBA) {
  574. /* A small (28b) LBA will fit in the 32b info field */
  575. sb[0] |= 0x80; /* set valid bit */
  576. sb[3] = tf->device & 0x0f;
  577. sb[4] = tf->lbah;
  578. sb[5] = tf->lbam;
  579. sb[6] = tf->lbal;
  580. }
  581. else {
  582. /* TODO: C/H/S */
  583. }
  584. }
  585. /**
  586. * ata_scsi_slave_config - Set SCSI device attributes
  587. * @sdev: SCSI device to examine
  588. *
  589. * This is called before we actually start reading
  590. * and writing to the device, to configure certain
  591. * SCSI mid-layer behaviors.
  592. *
  593. * LOCKING:
  594. * Defined by SCSI layer. We don't really care.
  595. */
  596. int ata_scsi_slave_config(struct scsi_device *sdev)
  597. {
  598. sdev->use_10_for_rw = 1;
  599. sdev->use_10_for_ms = 1;
  600. blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
  601. if (sdev->id < ATA_MAX_DEVICES) {
  602. struct ata_port *ap;
  603. struct ata_device *dev;
  604. ap = (struct ata_port *) &sdev->host->hostdata[0];
  605. dev = &ap->device[sdev->id];
  606. /* TODO: 1024 is an arbitrary number, not the
  607. * hardware maximum. This should be increased to
  608. * 65534 when Jens Axboe's patch for dynamically
  609. * determining max_sectors is merged.
  610. */
  611. if ((dev->flags & ATA_DFLAG_LBA48) &&
  612. ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
  613. /*
  614. * do not overwrite sdev->host->max_sectors, since
  615. * other drives on this host may not support LBA48
  616. */
  617. blk_queue_max_sectors(sdev->request_queue, 2048);
  618. }
  619. /*
  620. * SATA DMA transfers must be multiples of 4 byte, so
  621. * we need to pad ATAPI transfers using an extra sg.
  622. * Decrement max hw segments accordingly.
  623. */
  624. if (dev->class == ATA_DEV_ATAPI) {
  625. request_queue_t *q = sdev->request_queue;
  626. blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
  627. }
  628. }
  629. return 0; /* scsi layer doesn't check return value, sigh */
  630. }
  631. /**
  632. * ata_scsi_error - SCSI layer error handler callback
  633. * @host: SCSI host on which error occurred
  634. *
  635. * Handles SCSI-layer-thrown error events.
  636. *
  637. * LOCKING:
  638. * Inherited from SCSI layer (none, can sleep)
  639. *
  640. * RETURNS:
  641. * Zero.
  642. */
  643. int ata_scsi_error(struct Scsi_Host *host)
  644. {
  645. struct ata_port *ap;
  646. DPRINTK("ENTER\n");
  647. ap = (struct ata_port *) &host->hostdata[0];
  648. ap->ops->eng_timeout(ap);
  649. /* TODO: this is per-command; when queueing is supported
  650. * this code will either change or move to a more
  651. * appropriate place
  652. */
  653. host->host_failed--;
  654. INIT_LIST_HEAD(&host->eh_cmd_q);
  655. DPRINTK("EXIT\n");
  656. return 0;
  657. }
  658. /**
  659. * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
  660. * @qc: Storage for translated ATA taskfile
  661. * @scsicmd: SCSI command to translate
  662. *
  663. * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
  664. * (to start). Perhaps these commands should be preceded by
  665. * CHECK POWER MODE to see what power mode the device is already in.
  666. * [See SAT revision 5 at www.t10.org]
  667. *
  668. * LOCKING:
  669. * spin_lock_irqsave(host_set lock)
  670. *
  671. * RETURNS:
  672. * Zero on success, non-zero on error.
  673. */
  674. static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
  675. const u8 *scsicmd)
  676. {
  677. struct ata_taskfile *tf = &qc->tf;
  678. tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  679. tf->protocol = ATA_PROT_NODATA;
  680. if (scsicmd[1] & 0x1) {
  681. ; /* ignore IMMED bit, violates sat-r05 */
  682. }
  683. if (scsicmd[4] & 0x2)
  684. goto invalid_fld; /* LOEJ bit set not supported */
  685. if (((scsicmd[4] >> 4) & 0xf) != 0)
  686. goto invalid_fld; /* power conditions not supported */
  687. if (scsicmd[4] & 0x1) {
  688. tf->nsect = 1; /* 1 sector, lba=0 */
  689. if (qc->dev->flags & ATA_DFLAG_LBA) {
  690. qc->tf.flags |= ATA_TFLAG_LBA;
  691. tf->lbah = 0x0;
  692. tf->lbam = 0x0;
  693. tf->lbal = 0x0;
  694. tf->device |= ATA_LBA;
  695. } else {
  696. /* CHS */
  697. tf->lbal = 0x1; /* sect */
  698. tf->lbam = 0x0; /* cyl low */
  699. tf->lbah = 0x0; /* cyl high */
  700. }
  701. tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
  702. } else {
  703. tf->nsect = 0; /* time period value (0 implies now) */
  704. tf->command = ATA_CMD_STANDBY;
  705. /* Consider: ATA STANDBY IMMEDIATE command */
  706. }
  707. /*
  708. * Standby and Idle condition timers could be implemented but that
  709. * would require libata to implement the Power condition mode page
  710. * and allow the user to change it. Changing mode pages requires
  711. * MODE SELECT to be implemented.
  712. */
  713. return 0;
  714. invalid_fld:
  715. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  716. /* "Invalid field in cbd" */
  717. return 1;
  718. }
  719. /**
  720. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  721. * @qc: Storage for translated ATA taskfile
  722. * @scsicmd: SCSI command to translate (ignored)
  723. *
  724. * Sets up an ATA taskfile to issue FLUSH CACHE or
  725. * FLUSH CACHE EXT.
  726. *
  727. * LOCKING:
  728. * spin_lock_irqsave(host_set lock)
  729. *
  730. * RETURNS:
  731. * Zero on success, non-zero on error.
  732. */
  733. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  734. {
  735. struct ata_taskfile *tf = &qc->tf;
  736. tf->flags |= ATA_TFLAG_DEVICE;
  737. tf->protocol = ATA_PROT_NODATA;
  738. if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
  739. (ata_id_has_flush_ext(qc->dev->id)))
  740. tf->command = ATA_CMD_FLUSH_EXT;
  741. else
  742. tf->command = ATA_CMD_FLUSH;
  743. return 0;
  744. }
  745. /**
  746. * scsi_6_lba_len - Get LBA and transfer length
  747. * @scsicmd: SCSI command to translate
  748. *
  749. * Calculate LBA and transfer length for 6-byte commands.
  750. *
  751. * RETURNS:
  752. * @plba: the LBA
  753. * @plen: the transfer length
  754. */
  755. static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  756. {
  757. u64 lba = 0;
  758. u32 len = 0;
  759. VPRINTK("six-byte command\n");
  760. lba |= ((u64)scsicmd[2]) << 8;
  761. lba |= ((u64)scsicmd[3]);
  762. len |= ((u32)scsicmd[4]);
  763. *plba = lba;
  764. *plen = len;
  765. }
  766. /**
  767. * scsi_10_lba_len - Get LBA and transfer length
  768. * @scsicmd: SCSI command to translate
  769. *
  770. * Calculate LBA and transfer length for 10-byte commands.
  771. *
  772. * RETURNS:
  773. * @plba: the LBA
  774. * @plen: the transfer length
  775. */
  776. static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  777. {
  778. u64 lba = 0;
  779. u32 len = 0;
  780. VPRINTK("ten-byte command\n");
  781. lba |= ((u64)scsicmd[2]) << 24;
  782. lba |= ((u64)scsicmd[3]) << 16;
  783. lba |= ((u64)scsicmd[4]) << 8;
  784. lba |= ((u64)scsicmd[5]);
  785. len |= ((u32)scsicmd[7]) << 8;
  786. len |= ((u32)scsicmd[8]);
  787. *plba = lba;
  788. *plen = len;
  789. }
  790. /**
  791. * scsi_16_lba_len - Get LBA and transfer length
  792. * @scsicmd: SCSI command to translate
  793. *
  794. * Calculate LBA and transfer length for 16-byte commands.
  795. *
  796. * RETURNS:
  797. * @plba: the LBA
  798. * @plen: the transfer length
  799. */
  800. static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
  801. {
  802. u64 lba = 0;
  803. u32 len = 0;
  804. VPRINTK("sixteen-byte command\n");
  805. lba |= ((u64)scsicmd[2]) << 56;
  806. lba |= ((u64)scsicmd[3]) << 48;
  807. lba |= ((u64)scsicmd[4]) << 40;
  808. lba |= ((u64)scsicmd[5]) << 32;
  809. lba |= ((u64)scsicmd[6]) << 24;
  810. lba |= ((u64)scsicmd[7]) << 16;
  811. lba |= ((u64)scsicmd[8]) << 8;
  812. lba |= ((u64)scsicmd[9]);
  813. len |= ((u32)scsicmd[10]) << 24;
  814. len |= ((u32)scsicmd[11]) << 16;
  815. len |= ((u32)scsicmd[12]) << 8;
  816. len |= ((u32)scsicmd[13]);
  817. *plba = lba;
  818. *plen = len;
  819. }
  820. /**
  821. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  822. * @qc: Storage for translated ATA taskfile
  823. * @scsicmd: SCSI command to translate
  824. *
  825. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  826. *
  827. * LOCKING:
  828. * spin_lock_irqsave(host_set lock)
  829. *
  830. * RETURNS:
  831. * Zero on success, non-zero on error.
  832. */
  833. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  834. {
  835. struct ata_taskfile *tf = &qc->tf;
  836. struct ata_device *dev = qc->dev;
  837. u64 dev_sectors = qc->dev->n_sectors;
  838. u64 block;
  839. u32 n_block;
  840. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  841. tf->protocol = ATA_PROT_NODATA;
  842. if (scsicmd[0] == VERIFY)
  843. scsi_10_lba_len(scsicmd, &block, &n_block);
  844. else if (scsicmd[0] == VERIFY_16)
  845. scsi_16_lba_len(scsicmd, &block, &n_block);
  846. else
  847. goto invalid_fld;
  848. if (!n_block)
  849. goto nothing_to_do;
  850. if (block >= dev_sectors)
  851. goto out_of_range;
  852. if ((block + n_block) > dev_sectors)
  853. goto out_of_range;
  854. if (dev->flags & ATA_DFLAG_LBA) {
  855. tf->flags |= ATA_TFLAG_LBA;
  856. if (dev->flags & ATA_DFLAG_LBA48) {
  857. if (n_block > (64 * 1024))
  858. goto invalid_fld;
  859. /* use LBA48 */
  860. tf->flags |= ATA_TFLAG_LBA48;
  861. tf->command = ATA_CMD_VERIFY_EXT;
  862. tf->hob_nsect = (n_block >> 8) & 0xff;
  863. tf->hob_lbah = (block >> 40) & 0xff;
  864. tf->hob_lbam = (block >> 32) & 0xff;
  865. tf->hob_lbal = (block >> 24) & 0xff;
  866. } else {
  867. if (n_block > 256)
  868. goto invalid_fld;
  869. /* use LBA28 */
  870. tf->command = ATA_CMD_VERIFY;
  871. tf->device |= (block >> 24) & 0xf;
  872. }
  873. tf->nsect = n_block & 0xff;
  874. tf->lbah = (block >> 16) & 0xff;
  875. tf->lbam = (block >> 8) & 0xff;
  876. tf->lbal = block & 0xff;
  877. tf->device |= ATA_LBA;
  878. } else {
  879. /* CHS */
  880. u32 sect, head, cyl, track;
  881. if (n_block > 256)
  882. goto invalid_fld;
  883. /* Convert LBA to CHS */
  884. track = (u32)block / dev->sectors;
  885. cyl = track / dev->heads;
  886. head = track % dev->heads;
  887. sect = (u32)block % dev->sectors + 1;
  888. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  889. (u32)block, track, cyl, head, sect);
  890. /* Check whether the converted CHS can fit.
  891. Cylinder: 0-65535
  892. Head: 0-15
  893. Sector: 1-255*/
  894. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  895. goto out_of_range;
  896. tf->command = ATA_CMD_VERIFY;
  897. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  898. tf->lbal = sect;
  899. tf->lbam = cyl;
  900. tf->lbah = cyl >> 8;
  901. tf->device |= head;
  902. }
  903. return 0;
  904. invalid_fld:
  905. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  906. /* "Invalid field in cbd" */
  907. return 1;
  908. out_of_range:
  909. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  910. /* "Logical Block Address out of range" */
  911. return 1;
  912. nothing_to_do:
  913. qc->scsicmd->result = SAM_STAT_GOOD;
  914. return 1;
  915. }
  916. /**
  917. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  918. * @qc: Storage for translated ATA taskfile
  919. * @scsicmd: SCSI command to translate
  920. *
  921. * Converts any of six SCSI read/write commands into the
  922. * ATA counterpart, including starting sector (LBA),
  923. * sector count, and taking into account the device's LBA48
  924. * support.
  925. *
  926. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  927. * %WRITE_16 are currently supported.
  928. *
  929. * LOCKING:
  930. * spin_lock_irqsave(host_set lock)
  931. *
  932. * RETURNS:
  933. * Zero on success, non-zero on error.
  934. */
  935. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  936. {
  937. struct ata_taskfile *tf = &qc->tf;
  938. struct ata_device *dev = qc->dev;
  939. u64 block;
  940. u32 n_block;
  941. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  942. if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
  943. scsicmd[0] == WRITE_16)
  944. tf->flags |= ATA_TFLAG_WRITE;
  945. /* Calculate the SCSI LBA and transfer length. */
  946. switch (scsicmd[0]) {
  947. case READ_10:
  948. case WRITE_10:
  949. scsi_10_lba_len(scsicmd, &block, &n_block);
  950. break;
  951. case READ_6:
  952. case WRITE_6:
  953. scsi_6_lba_len(scsicmd, &block, &n_block);
  954. /* for 6-byte r/w commands, transfer length 0
  955. * means 256 blocks of data, not 0 block.
  956. */
  957. if (!n_block)
  958. n_block = 256;
  959. break;
  960. case READ_16:
  961. case WRITE_16:
  962. scsi_16_lba_len(scsicmd, &block, &n_block);
  963. break;
  964. default:
  965. DPRINTK("no-byte command\n");
  966. goto invalid_fld;
  967. }
  968. /* Check and compose ATA command */
  969. if (!n_block)
  970. /* For 10-byte and 16-byte SCSI R/W commands, transfer
  971. * length 0 means transfer 0 block of data.
  972. * However, for ATA R/W commands, sector count 0 means
  973. * 256 or 65536 sectors, not 0 sectors as in SCSI.
  974. */
  975. goto nothing_to_do;
  976. if (dev->flags & ATA_DFLAG_LBA) {
  977. tf->flags |= ATA_TFLAG_LBA;
  978. if (dev->flags & ATA_DFLAG_LBA48) {
  979. /* The request -may- be too large for LBA48. */
  980. if ((block >> 48) || (n_block > 65536))
  981. goto out_of_range;
  982. /* use LBA48 */
  983. tf->flags |= ATA_TFLAG_LBA48;
  984. tf->hob_nsect = (n_block >> 8) & 0xff;
  985. tf->hob_lbah = (block >> 40) & 0xff;
  986. tf->hob_lbam = (block >> 32) & 0xff;
  987. tf->hob_lbal = (block >> 24) & 0xff;
  988. } else {
  989. /* use LBA28 */
  990. /* The request -may- be too large for LBA28. */
  991. if ((block >> 28) || (n_block > 256))
  992. goto out_of_range;
  993. tf->device |= (block >> 24) & 0xf;
  994. }
  995. ata_rwcmd_protocol(qc);
  996. qc->nsect = n_block;
  997. tf->nsect = n_block & 0xff;
  998. tf->lbah = (block >> 16) & 0xff;
  999. tf->lbam = (block >> 8) & 0xff;
  1000. tf->lbal = block & 0xff;
  1001. tf->device |= ATA_LBA;
  1002. } else {
  1003. /* CHS */
  1004. u32 sect, head, cyl, track;
  1005. /* The request -may- be too large for CHS addressing. */
  1006. if ((block >> 28) || (n_block > 256))
  1007. goto out_of_range;
  1008. ata_rwcmd_protocol(qc);
  1009. /* Convert LBA to CHS */
  1010. track = (u32)block / dev->sectors;
  1011. cyl = track / dev->heads;
  1012. head = track % dev->heads;
  1013. sect = (u32)block % dev->sectors + 1;
  1014. DPRINTK("block %u track %u cyl %u head %u sect %u\n",
  1015. (u32)block, track, cyl, head, sect);
  1016. /* Check whether the converted CHS can fit.
  1017. Cylinder: 0-65535
  1018. Head: 0-15
  1019. Sector: 1-255*/
  1020. if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
  1021. goto out_of_range;
  1022. qc->nsect = n_block;
  1023. tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
  1024. tf->lbal = sect;
  1025. tf->lbam = cyl;
  1026. tf->lbah = cyl >> 8;
  1027. tf->device |= head;
  1028. }
  1029. return 0;
  1030. invalid_fld:
  1031. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1032. /* "Invalid field in cbd" */
  1033. return 1;
  1034. out_of_range:
  1035. ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
  1036. /* "Logical Block Address out of range" */
  1037. return 1;
  1038. nothing_to_do:
  1039. qc->scsicmd->result = SAM_STAT_GOOD;
  1040. return 1;
  1041. }
  1042. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc,
  1043. unsigned int err_mask)
  1044. {
  1045. struct scsi_cmnd *cmd = qc->scsicmd;
  1046. u8 *cdb = cmd->cmnd;
  1047. int need_sense = (err_mask != 0);
  1048. /* For ATA pass thru (SAT) commands, generate a sense block if
  1049. * user mandated it or if there's an error. Note that if we
  1050. * generate because the user forced us to, a check condition
  1051. * is generated and the ATA register values are returned
  1052. * whether the command completed successfully or not. If there
  1053. * was no error, SK, ASC and ASCQ will all be zero.
  1054. */
  1055. if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
  1056. ((cdb[2] & 0x20) || need_sense)) {
  1057. ata_gen_ata_desc_sense(qc);
  1058. } else {
  1059. if (!need_sense) {
  1060. cmd->result = SAM_STAT_GOOD;
  1061. } else {
  1062. /* TODO: decide which descriptor format to use
  1063. * for 48b LBA devices and call that here
  1064. * instead of the fixed desc, which is only
  1065. * good for smaller LBA (and maybe CHS?)
  1066. * devices.
  1067. */
  1068. ata_gen_fixed_sense(qc);
  1069. }
  1070. }
  1071. if (need_sense) {
  1072. /* The ata_gen_..._sense routines fill in tf */
  1073. ata_dump_status(qc->ap->id, &qc->tf);
  1074. }
  1075. qc->scsidone(cmd);
  1076. return 0;
  1077. }
  1078. /**
  1079. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  1080. * @ap: ATA port to which the command is addressed
  1081. * @dev: ATA device to which the command is addressed
  1082. * @cmd: SCSI command to execute
  1083. * @done: SCSI command completion function
  1084. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  1085. *
  1086. * Our ->queuecommand() function has decided that the SCSI
  1087. * command issued can be directly translated into an ATA
  1088. * command, rather than handled internally.
  1089. *
  1090. * This function sets up an ata_queued_cmd structure for the
  1091. * SCSI command, and sends that ata_queued_cmd to the hardware.
  1092. *
  1093. * The xlat_func argument (actor) returns 0 if ready to execute
  1094. * ATA command, else 1 to finish translation. If 1 is returned
  1095. * then cmd->result (and possibly cmd->sense_buffer) are assumed
  1096. * to be set reflecting an error condition or clean (early)
  1097. * termination.
  1098. *
  1099. * LOCKING:
  1100. * spin_lock_irqsave(host_set lock)
  1101. */
  1102. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  1103. struct scsi_cmnd *cmd,
  1104. void (*done)(struct scsi_cmnd *),
  1105. ata_xlat_func_t xlat_func)
  1106. {
  1107. struct ata_queued_cmd *qc;
  1108. u8 *scsicmd = cmd->cmnd;
  1109. VPRINTK("ENTER\n");
  1110. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  1111. if (!qc)
  1112. goto err_mem;
  1113. /* data is present; dma-map it */
  1114. if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
  1115. cmd->sc_data_direction == DMA_TO_DEVICE) {
  1116. if (unlikely(cmd->request_bufflen < 1)) {
  1117. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  1118. ap->id, dev->devno);
  1119. goto err_did;
  1120. }
  1121. if (cmd->use_sg)
  1122. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  1123. else
  1124. ata_sg_init_one(qc, cmd->request_buffer,
  1125. cmd->request_bufflen);
  1126. qc->dma_dir = cmd->sc_data_direction;
  1127. }
  1128. qc->complete_fn = ata_scsi_qc_complete;
  1129. if (xlat_func(qc, scsicmd))
  1130. goto early_finish;
  1131. /* select device, send command to hardware */
  1132. if (ata_qc_issue(qc))
  1133. goto err_did;
  1134. VPRINTK("EXIT\n");
  1135. return;
  1136. early_finish:
  1137. ata_qc_free(qc);
  1138. done(cmd);
  1139. DPRINTK("EXIT - early finish (good or error)\n");
  1140. return;
  1141. err_did:
  1142. ata_qc_free(qc);
  1143. err_mem:
  1144. cmd->result = (DID_ERROR << 16);
  1145. done(cmd);
  1146. DPRINTK("EXIT - internal\n");
  1147. return;
  1148. }
  1149. /**
  1150. * ata_scsi_rbuf_get - Map response buffer.
  1151. * @cmd: SCSI command containing buffer to be mapped.
  1152. * @buf_out: Pointer to mapped area.
  1153. *
  1154. * Maps buffer contained within SCSI command @cmd.
  1155. *
  1156. * LOCKING:
  1157. * spin_lock_irqsave(host_set lock)
  1158. *
  1159. * RETURNS:
  1160. * Length of response buffer.
  1161. */
  1162. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  1163. {
  1164. u8 *buf;
  1165. unsigned int buflen;
  1166. if (cmd->use_sg) {
  1167. struct scatterlist *sg;
  1168. sg = (struct scatterlist *) cmd->request_buffer;
  1169. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  1170. buflen = sg->length;
  1171. } else {
  1172. buf = cmd->request_buffer;
  1173. buflen = cmd->request_bufflen;
  1174. }
  1175. *buf_out = buf;
  1176. return buflen;
  1177. }
  1178. /**
  1179. * ata_scsi_rbuf_put - Unmap response buffer.
  1180. * @cmd: SCSI command containing buffer to be unmapped.
  1181. * @buf: buffer to unmap
  1182. *
  1183. * Unmaps response buffer contained within @cmd.
  1184. *
  1185. * LOCKING:
  1186. * spin_lock_irqsave(host_set lock)
  1187. */
  1188. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  1189. {
  1190. if (cmd->use_sg) {
  1191. struct scatterlist *sg;
  1192. sg = (struct scatterlist *) cmd->request_buffer;
  1193. kunmap_atomic(buf - sg->offset, KM_USER0);
  1194. }
  1195. }
  1196. /**
  1197. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  1198. * @args: device IDENTIFY data / SCSI command of interest.
  1199. * @actor: Callback hook for desired SCSI command simulator
  1200. *
  1201. * Takes care of the hard work of simulating a SCSI command...
  1202. * Mapping the response buffer, calling the command's handler,
  1203. * and handling the handler's return value. This return value
  1204. * indicates whether the handler wishes the SCSI command to be
  1205. * completed successfully (0), or not (in which case cmd->result
  1206. * and sense buffer are assumed to be set).
  1207. *
  1208. * LOCKING:
  1209. * spin_lock_irqsave(host_set lock)
  1210. */
  1211. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  1212. unsigned int (*actor) (struct ata_scsi_args *args,
  1213. u8 *rbuf, unsigned int buflen))
  1214. {
  1215. u8 *rbuf;
  1216. unsigned int buflen, rc;
  1217. struct scsi_cmnd *cmd = args->cmd;
  1218. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  1219. memset(rbuf, 0, buflen);
  1220. rc = actor(args, rbuf, buflen);
  1221. ata_scsi_rbuf_put(cmd, rbuf);
  1222. if (rc == 0)
  1223. cmd->result = SAM_STAT_GOOD;
  1224. args->done(cmd);
  1225. }
  1226. /**
  1227. * ata_scsiop_inq_std - Simulate INQUIRY command
  1228. * @args: device IDENTIFY data / SCSI command of interest.
  1229. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1230. * @buflen: Response buffer length.
  1231. *
  1232. * Returns standard device identification data associated
  1233. * with non-EVPD INQUIRY command output.
  1234. *
  1235. * LOCKING:
  1236. * spin_lock_irqsave(host_set lock)
  1237. */
  1238. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  1239. unsigned int buflen)
  1240. {
  1241. u8 hdr[] = {
  1242. TYPE_DISK,
  1243. 0,
  1244. 0x5, /* claim SPC-3 version compatibility */
  1245. 2,
  1246. 95 - 4
  1247. };
  1248. /* set scsi removeable (RMB) bit per ata bit */
  1249. if (ata_id_removeable(args->id))
  1250. hdr[1] |= (1 << 7);
  1251. VPRINTK("ENTER\n");
  1252. memcpy(rbuf, hdr, sizeof(hdr));
  1253. if (buflen > 35) {
  1254. memcpy(&rbuf[8], "ATA ", 8);
  1255. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  1256. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  1257. if (rbuf[32] == 0 || rbuf[32] == ' ')
  1258. memcpy(&rbuf[32], "n/a ", 4);
  1259. }
  1260. if (buflen > 63) {
  1261. const u8 versions[] = {
  1262. 0x60, /* SAM-3 (no version claimed) */
  1263. 0x03,
  1264. 0x20, /* SBC-2 (no version claimed) */
  1265. 0x02,
  1266. 0x60 /* SPC-3 (no version claimed) */
  1267. };
  1268. memcpy(rbuf + 59, versions, sizeof(versions));
  1269. }
  1270. return 0;
  1271. }
  1272. /**
  1273. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  1274. * @args: device IDENTIFY data / SCSI command of interest.
  1275. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1276. * @buflen: Response buffer length.
  1277. *
  1278. * Returns list of inquiry EVPD pages available.
  1279. *
  1280. * LOCKING:
  1281. * spin_lock_irqsave(host_set lock)
  1282. */
  1283. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  1284. unsigned int buflen)
  1285. {
  1286. const u8 pages[] = {
  1287. 0x00, /* page 0x00, this page */
  1288. 0x80, /* page 0x80, unit serial no page */
  1289. 0x83 /* page 0x83, device ident page */
  1290. };
  1291. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  1292. if (buflen > 6)
  1293. memcpy(rbuf + 4, pages, sizeof(pages));
  1294. return 0;
  1295. }
  1296. /**
  1297. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  1298. * @args: device IDENTIFY data / SCSI command of interest.
  1299. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1300. * @buflen: Response buffer length.
  1301. *
  1302. * Returns ATA device serial number.
  1303. *
  1304. * LOCKING:
  1305. * spin_lock_irqsave(host_set lock)
  1306. */
  1307. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  1308. unsigned int buflen)
  1309. {
  1310. const u8 hdr[] = {
  1311. 0,
  1312. 0x80, /* this page code */
  1313. 0,
  1314. ATA_SERNO_LEN, /* page len */
  1315. };
  1316. memcpy(rbuf, hdr, sizeof(hdr));
  1317. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  1318. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  1319. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1320. return 0;
  1321. }
  1322. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  1323. /**
  1324. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  1325. * @args: device IDENTIFY data / SCSI command of interest.
  1326. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1327. * @buflen: Response buffer length.
  1328. *
  1329. * Returns device identification. Currently hardcoded to
  1330. * return "Linux ATA-SCSI simulator".
  1331. *
  1332. * LOCKING:
  1333. * spin_lock_irqsave(host_set lock)
  1334. */
  1335. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  1336. unsigned int buflen)
  1337. {
  1338. rbuf[1] = 0x83; /* this page code */
  1339. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  1340. /* our one and only identification descriptor (vendor-specific) */
  1341. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  1342. rbuf[4 + 0] = 2; /* code set: ASCII */
  1343. rbuf[4 + 3] = strlen(inq_83_str);
  1344. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  1345. }
  1346. return 0;
  1347. }
  1348. /**
  1349. * ata_scsiop_noop - Command handler that simply returns success.
  1350. * @args: device IDENTIFY data / SCSI command of interest.
  1351. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1352. * @buflen: Response buffer length.
  1353. *
  1354. * No operation. Simply returns success to caller, to indicate
  1355. * that the caller should successfully complete this SCSI command.
  1356. *
  1357. * LOCKING:
  1358. * spin_lock_irqsave(host_set lock)
  1359. */
  1360. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  1361. unsigned int buflen)
  1362. {
  1363. VPRINTK("ENTER\n");
  1364. return 0;
  1365. }
  1366. /**
  1367. * ata_msense_push - Push data onto MODE SENSE data output buffer
  1368. * @ptr_io: (input/output) Location to store more output data
  1369. * @last: End of output data buffer
  1370. * @buf: Pointer to BLOB being added to output buffer
  1371. * @buflen: Length of BLOB
  1372. *
  1373. * Store MODE SENSE data on an output buffer.
  1374. *
  1375. * LOCKING:
  1376. * None.
  1377. */
  1378. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  1379. const u8 *buf, unsigned int buflen)
  1380. {
  1381. u8 *ptr = *ptr_io;
  1382. if ((ptr + buflen - 1) > last)
  1383. return;
  1384. memcpy(ptr, buf, buflen);
  1385. ptr += buflen;
  1386. *ptr_io = ptr;
  1387. }
  1388. /**
  1389. * ata_msense_caching - Simulate MODE SENSE caching info page
  1390. * @id: device IDENTIFY data
  1391. * @ptr_io: (input/output) Location to store more output data
  1392. * @last: End of output data buffer
  1393. *
  1394. * Generate a caching info page, which conditionally indicates
  1395. * write caching to the SCSI layer, depending on device
  1396. * capabilities.
  1397. *
  1398. * LOCKING:
  1399. * None.
  1400. */
  1401. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  1402. const u8 *last)
  1403. {
  1404. u8 page[CACHE_MPAGE_LEN];
  1405. memcpy(page, def_cache_mpage, sizeof(page));
  1406. if (ata_id_wcache_enabled(id))
  1407. page[2] |= (1 << 2); /* write cache enable */
  1408. if (!ata_id_rahead_enabled(id))
  1409. page[12] |= (1 << 5); /* disable read ahead */
  1410. ata_msense_push(ptr_io, last, page, sizeof(page));
  1411. return sizeof(page);
  1412. }
  1413. /**
  1414. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1415. * @dev: Device associated with this MODE SENSE command
  1416. * @ptr_io: (input/output) Location to store more output data
  1417. * @last: End of output data buffer
  1418. *
  1419. * Generate a generic MODE SENSE control mode page.
  1420. *
  1421. * LOCKING:
  1422. * None.
  1423. */
  1424. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  1425. {
  1426. ata_msense_push(ptr_io, last, def_control_mpage,
  1427. sizeof(def_control_mpage));
  1428. return sizeof(def_control_mpage);
  1429. }
  1430. /**
  1431. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1432. * @dev: Device associated with this MODE SENSE command
  1433. * @ptr_io: (input/output) Location to store more output data
  1434. * @last: End of output data buffer
  1435. *
  1436. * Generate a generic MODE SENSE r/w error recovery page.
  1437. *
  1438. * LOCKING:
  1439. * None.
  1440. */
  1441. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  1442. {
  1443. ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
  1444. sizeof(def_rw_recovery_mpage));
  1445. return sizeof(def_rw_recovery_mpage);
  1446. }
  1447. /**
  1448. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1449. * @args: device IDENTIFY data / SCSI command of interest.
  1450. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1451. * @buflen: Response buffer length.
  1452. *
  1453. * Simulate MODE SENSE commands. Assume this is invoked for direct
  1454. * access devices (e.g. disks) only. There should be no block
  1455. * descriptor for other device types.
  1456. *
  1457. * LOCKING:
  1458. * spin_lock_irqsave(host_set lock)
  1459. */
  1460. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1461. unsigned int buflen)
  1462. {
  1463. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1464. const u8 sat_blk_desc[] = {
  1465. 0, 0, 0, 0, /* number of blocks: sat unspecified */
  1466. 0,
  1467. 0, 0x2, 0x0 /* block length: 512 bytes */
  1468. };
  1469. u8 pg, spg;
  1470. unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
  1471. VPRINTK("ENTER\n");
  1472. six_byte = (scsicmd[0] == MODE_SENSE);
  1473. ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
  1474. /*
  1475. * LLBA bit in msense(10) ignored (compliant)
  1476. */
  1477. page_control = scsicmd[2] >> 6;
  1478. switch (page_control) {
  1479. case 0: /* current */
  1480. break; /* supported */
  1481. case 3: /* saved */
  1482. goto saving_not_supp;
  1483. case 1: /* changeable */
  1484. case 2: /* defaults */
  1485. default:
  1486. goto invalid_fld;
  1487. }
  1488. if (six_byte) {
  1489. output_len = 4 + (ebd ? 8 : 0);
  1490. alloc_len = scsicmd[4];
  1491. } else {
  1492. output_len = 8 + (ebd ? 8 : 0);
  1493. alloc_len = (scsicmd[7] << 8) + scsicmd[8];
  1494. }
  1495. minlen = (alloc_len < buflen) ? alloc_len : buflen;
  1496. p = rbuf + output_len;
  1497. last = rbuf + minlen - 1;
  1498. pg = scsicmd[2] & 0x3f;
  1499. spg = scsicmd[3];
  1500. /*
  1501. * No mode subpages supported (yet) but asking for _all_
  1502. * subpages may be valid
  1503. */
  1504. if (spg && (spg != ALL_SUB_MPAGES))
  1505. goto invalid_fld;
  1506. switch(pg) {
  1507. case RW_RECOVERY_MPAGE:
  1508. output_len += ata_msense_rw_recovery(&p, last);
  1509. break;
  1510. case CACHE_MPAGE:
  1511. output_len += ata_msense_caching(args->id, &p, last);
  1512. break;
  1513. case CONTROL_MPAGE: {
  1514. output_len += ata_msense_ctl_mode(&p, last);
  1515. break;
  1516. }
  1517. case ALL_MPAGES:
  1518. output_len += ata_msense_rw_recovery(&p, last);
  1519. output_len += ata_msense_caching(args->id, &p, last);
  1520. output_len += ata_msense_ctl_mode(&p, last);
  1521. break;
  1522. default: /* invalid page code */
  1523. goto invalid_fld;
  1524. }
  1525. if (minlen < 1)
  1526. return 0;
  1527. if (six_byte) {
  1528. output_len--;
  1529. rbuf[0] = output_len;
  1530. if (ebd) {
  1531. if (minlen > 3)
  1532. rbuf[3] = sizeof(sat_blk_desc);
  1533. if (minlen > 11)
  1534. memcpy(rbuf + 4, sat_blk_desc,
  1535. sizeof(sat_blk_desc));
  1536. }
  1537. } else {
  1538. output_len -= 2;
  1539. rbuf[0] = output_len >> 8;
  1540. if (minlen > 1)
  1541. rbuf[1] = output_len;
  1542. if (ebd) {
  1543. if (minlen > 7)
  1544. rbuf[7] = sizeof(sat_blk_desc);
  1545. if (minlen > 15)
  1546. memcpy(rbuf + 8, sat_blk_desc,
  1547. sizeof(sat_blk_desc));
  1548. }
  1549. }
  1550. return 0;
  1551. invalid_fld:
  1552. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
  1553. /* "Invalid field in cbd" */
  1554. return 1;
  1555. saving_not_supp:
  1556. ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
  1557. /* "Saving parameters not supported" */
  1558. return 1;
  1559. }
  1560. /**
  1561. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1562. * @args: device IDENTIFY data / SCSI command of interest.
  1563. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1564. * @buflen: Response buffer length.
  1565. *
  1566. * Simulate READ CAPACITY commands.
  1567. *
  1568. * LOCKING:
  1569. * spin_lock_irqsave(host_set lock)
  1570. */
  1571. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1572. unsigned int buflen)
  1573. {
  1574. u64 n_sectors;
  1575. u32 tmp;
  1576. VPRINTK("ENTER\n");
  1577. if (ata_id_has_lba(args->id)) {
  1578. if (ata_id_has_lba48(args->id))
  1579. n_sectors = ata_id_u64(args->id, 100);
  1580. else
  1581. n_sectors = ata_id_u32(args->id, 60);
  1582. } else {
  1583. /* CHS default translation */
  1584. n_sectors = args->id[1] * args->id[3] * args->id[6];
  1585. if (ata_id_current_chs_valid(args->id))
  1586. /* CHS current translation */
  1587. n_sectors = ata_id_u32(args->id, 57);
  1588. }
  1589. n_sectors--; /* ATA TotalUserSectors - 1 */
  1590. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1591. if( n_sectors >= 0xffffffffULL )
  1592. tmp = 0xffffffff ; /* Return max count on overflow */
  1593. else
  1594. tmp = n_sectors ;
  1595. /* sector count, 32-bit */
  1596. rbuf[0] = tmp >> (8 * 3);
  1597. rbuf[1] = tmp >> (8 * 2);
  1598. rbuf[2] = tmp >> (8 * 1);
  1599. rbuf[3] = tmp;
  1600. /* sector size */
  1601. tmp = ATA_SECT_SIZE;
  1602. rbuf[6] = tmp >> 8;
  1603. rbuf[7] = tmp;
  1604. } else {
  1605. /* sector count, 64-bit */
  1606. tmp = n_sectors >> (8 * 4);
  1607. rbuf[2] = tmp >> (8 * 3);
  1608. rbuf[3] = tmp >> (8 * 2);
  1609. rbuf[4] = tmp >> (8 * 1);
  1610. rbuf[5] = tmp;
  1611. tmp = n_sectors;
  1612. rbuf[6] = tmp >> (8 * 3);
  1613. rbuf[7] = tmp >> (8 * 2);
  1614. rbuf[8] = tmp >> (8 * 1);
  1615. rbuf[9] = tmp;
  1616. /* sector size */
  1617. tmp = ATA_SECT_SIZE;
  1618. rbuf[12] = tmp >> 8;
  1619. rbuf[13] = tmp;
  1620. }
  1621. return 0;
  1622. }
  1623. /**
  1624. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1625. * @args: device IDENTIFY data / SCSI command of interest.
  1626. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1627. * @buflen: Response buffer length.
  1628. *
  1629. * Simulate REPORT LUNS command.
  1630. *
  1631. * LOCKING:
  1632. * spin_lock_irqsave(host_set lock)
  1633. */
  1634. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1635. unsigned int buflen)
  1636. {
  1637. VPRINTK("ENTER\n");
  1638. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1639. return 0;
  1640. }
  1641. /**
  1642. * ata_scsi_set_sense - Set SCSI sense data and status
  1643. * @cmd: SCSI request to be handled
  1644. * @sk: SCSI-defined sense key
  1645. * @asc: SCSI-defined additional sense code
  1646. * @ascq: SCSI-defined additional sense code qualifier
  1647. *
  1648. * Helper function that builds a valid fixed format, current
  1649. * response code and the given sense key (sk), additional sense
  1650. * code (asc) and additional sense code qualifier (ascq) with
  1651. * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
  1652. * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
  1653. *
  1654. * LOCKING:
  1655. * Not required
  1656. */
  1657. void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
  1658. {
  1659. cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  1660. cmd->sense_buffer[0] = 0x70; /* fixed format, current */
  1661. cmd->sense_buffer[2] = sk;
  1662. cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
  1663. cmd->sense_buffer[12] = asc;
  1664. cmd->sense_buffer[13] = ascq;
  1665. }
  1666. /**
  1667. * ata_scsi_badcmd - End a SCSI request with an error
  1668. * @cmd: SCSI request to be handled
  1669. * @done: SCSI command completion function
  1670. * @asc: SCSI-defined additional sense code
  1671. * @ascq: SCSI-defined additional sense code qualifier
  1672. *
  1673. * Helper function that completes a SCSI command with
  1674. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1675. * and the specified additional sense codes.
  1676. *
  1677. * LOCKING:
  1678. * spin_lock_irqsave(host_set lock)
  1679. */
  1680. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1681. {
  1682. DPRINTK("ENTER\n");
  1683. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
  1684. done(cmd);
  1685. }
  1686. void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
  1687. struct scsi_cmnd *cmd)
  1688. {
  1689. DECLARE_COMPLETION(wait);
  1690. struct ata_queued_cmd *qc;
  1691. unsigned long flags;
  1692. int rc;
  1693. DPRINTK("ATAPI request sense\n");
  1694. qc = ata_qc_new_init(ap, dev);
  1695. BUG_ON(qc == NULL);
  1696. /* FIXME: is this needed? */
  1697. memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  1698. ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
  1699. qc->dma_dir = DMA_FROM_DEVICE;
  1700. memset(&qc->cdb, 0, ap->cdb_len);
  1701. qc->cdb[0] = REQUEST_SENSE;
  1702. qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1703. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1704. qc->tf.command = ATA_CMD_PACKET;
  1705. qc->tf.protocol = ATA_PROT_ATAPI;
  1706. qc->tf.lbam = (8 * 1024) & 0xff;
  1707. qc->tf.lbah = (8 * 1024) >> 8;
  1708. qc->nbytes = SCSI_SENSE_BUFFERSIZE;
  1709. qc->waiting = &wait;
  1710. qc->complete_fn = ata_qc_complete_noop;
  1711. spin_lock_irqsave(&ap->host_set->lock, flags);
  1712. rc = ata_qc_issue(qc);
  1713. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1714. if (rc)
  1715. ata_port_disable(ap);
  1716. else
  1717. wait_for_completion(&wait);
  1718. DPRINTK("EXIT\n");
  1719. }
  1720. static int atapi_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
  1721. {
  1722. struct scsi_cmnd *cmd = qc->scsicmd;
  1723. VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
  1724. if (unlikely(err_mask & AC_ERR_DEV)) {
  1725. DPRINTK("request check condition\n");
  1726. /* FIXME: command completion with check condition
  1727. * but no sense causes the error handler to run,
  1728. * which then issues REQUEST SENSE, fills in the sense
  1729. * buffer, and completes the command (for the second
  1730. * time). We need to issue REQUEST SENSE some other
  1731. * way, to avoid completing the command twice.
  1732. */
  1733. cmd->result = SAM_STAT_CHECK_CONDITION;
  1734. qc->scsidone(cmd);
  1735. return 1;
  1736. }
  1737. else if (unlikely(err_mask))
  1738. /* FIXME: not quite right; we don't want the
  1739. * translation of taskfile registers into
  1740. * a sense descriptors, since that's only
  1741. * correct for ATA, not ATAPI
  1742. */
  1743. ata_gen_ata_desc_sense(qc);
  1744. else {
  1745. u8 *scsicmd = cmd->cmnd;
  1746. if (scsicmd[0] == INQUIRY) {
  1747. u8 *buf = NULL;
  1748. unsigned int buflen;
  1749. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1750. /* ATAPI devices typically report zero for their SCSI version,
  1751. * and sometimes deviate from the spec WRT response data
  1752. * format. If SCSI version is reported as zero like normal,
  1753. * then we make the following fixups: 1) Fake MMC-5 version,
  1754. * to indicate to the Linux scsi midlayer this is a modern
  1755. * device. 2) Ensure response data format / ATAPI information
  1756. * are always correct.
  1757. */
  1758. /* FIXME: do we ever override EVPD pages and the like, with
  1759. * this code?
  1760. */
  1761. if (buf[2] == 0) {
  1762. buf[2] = 0x5;
  1763. buf[3] = 0x32;
  1764. }
  1765. ata_scsi_rbuf_put(cmd, buf);
  1766. }
  1767. cmd->result = SAM_STAT_GOOD;
  1768. }
  1769. qc->scsidone(cmd);
  1770. return 0;
  1771. }
  1772. /**
  1773. * atapi_xlat - Initialize PACKET taskfile
  1774. * @qc: command structure to be initialized
  1775. * @scsicmd: SCSI CDB associated with this PACKET command
  1776. *
  1777. * LOCKING:
  1778. * spin_lock_irqsave(host_set lock)
  1779. *
  1780. * RETURNS:
  1781. * Zero on success, non-zero on failure.
  1782. */
  1783. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
  1784. {
  1785. struct scsi_cmnd *cmd = qc->scsicmd;
  1786. struct ata_device *dev = qc->dev;
  1787. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1788. int nodata = (cmd->sc_data_direction == DMA_NONE);
  1789. if (!using_pio)
  1790. /* Check whether ATAPI DMA is safe */
  1791. if (ata_check_atapi_dma(qc))
  1792. using_pio = 1;
  1793. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1794. qc->complete_fn = atapi_qc_complete;
  1795. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1796. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  1797. qc->tf.flags |= ATA_TFLAG_WRITE;
  1798. DPRINTK("direction: write\n");
  1799. }
  1800. qc->tf.command = ATA_CMD_PACKET;
  1801. /* no data, or PIO data xfer */
  1802. if (using_pio || nodata) {
  1803. if (nodata)
  1804. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1805. else
  1806. qc->tf.protocol = ATA_PROT_ATAPI;
  1807. qc->tf.lbam = (8 * 1024) & 0xff;
  1808. qc->tf.lbah = (8 * 1024) >> 8;
  1809. }
  1810. /* DMA data xfer */
  1811. else {
  1812. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1813. qc->tf.feature |= ATAPI_PKT_DMA;
  1814. #ifdef ATAPI_ENABLE_DMADIR
  1815. /* some SATA bridges need us to indicate data xfer direction */
  1816. if (cmd->sc_data_direction != DMA_TO_DEVICE)
  1817. qc->tf.feature |= ATAPI_DMADIR;
  1818. #endif
  1819. }
  1820. qc->nbytes = cmd->bufflen;
  1821. return 0;
  1822. }
  1823. /**
  1824. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1825. * @ap: ATA port to which the device is attached
  1826. * @scsidev: SCSI device from which we derive the ATA device
  1827. *
  1828. * Given various information provided in struct scsi_cmnd,
  1829. * map that onto an ATA bus, and using that mapping
  1830. * determine which ata_device is associated with the
  1831. * SCSI command to be sent.
  1832. *
  1833. * LOCKING:
  1834. * spin_lock_irqsave(host_set lock)
  1835. *
  1836. * RETURNS:
  1837. * Associated ATA device, or %NULL if not found.
  1838. */
  1839. static struct ata_device *
  1840. ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
  1841. {
  1842. struct ata_device *dev;
  1843. /* skip commands not addressed to targets we simulate */
  1844. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1845. dev = &ap->device[scsidev->id];
  1846. else
  1847. return NULL;
  1848. if (unlikely((scsidev->channel != 0) ||
  1849. (scsidev->lun != 0)))
  1850. return NULL;
  1851. if (unlikely(!ata_dev_present(dev)))
  1852. return NULL;
  1853. if (!atapi_enabled) {
  1854. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1855. return NULL;
  1856. }
  1857. return dev;
  1858. }
  1859. /*
  1860. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  1861. * @byte1: Byte 1 from pass-thru CDB.
  1862. *
  1863. * RETURNS:
  1864. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  1865. */
  1866. static u8
  1867. ata_scsi_map_proto(u8 byte1)
  1868. {
  1869. switch((byte1 & 0x1e) >> 1) {
  1870. case 3: /* Non-data */
  1871. return ATA_PROT_NODATA;
  1872. case 6: /* DMA */
  1873. return ATA_PROT_DMA;
  1874. case 4: /* PIO Data-in */
  1875. case 5: /* PIO Data-out */
  1876. if (byte1 & 0xe0) {
  1877. return ATA_PROT_PIO_MULT;
  1878. }
  1879. return ATA_PROT_PIO;
  1880. case 10: /* Device Reset */
  1881. case 0: /* Hard Reset */
  1882. case 1: /* SRST */
  1883. case 2: /* Bus Idle */
  1884. case 7: /* Packet */
  1885. case 8: /* DMA Queued */
  1886. case 9: /* Device Diagnostic */
  1887. case 11: /* UDMA Data-in */
  1888. case 12: /* UDMA Data-Out */
  1889. case 13: /* FPDMA */
  1890. default: /* Reserved */
  1891. break;
  1892. }
  1893. return ATA_PROT_UNKNOWN;
  1894. }
  1895. /**
  1896. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  1897. * @qc: command structure to be initialized
  1898. * @cmd: SCSI command to convert
  1899. *
  1900. * Handles either 12 or 16-byte versions of the CDB.
  1901. *
  1902. * RETURNS:
  1903. * Zero on success, non-zero on failure.
  1904. */
  1905. static unsigned int
  1906. ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
  1907. {
  1908. struct ata_taskfile *tf = &(qc->tf);
  1909. struct scsi_cmnd *cmd = qc->scsicmd;
  1910. if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
  1911. return 1;
  1912. /*
  1913. * 12 and 16 byte CDBs use different offsets to
  1914. * provide the various register values.
  1915. */
  1916. if (scsicmd[0] == ATA_16) {
  1917. /*
  1918. * 16-byte CDB - may contain extended commands.
  1919. *
  1920. * If that is the case, copy the upper byte register values.
  1921. */
  1922. if (scsicmd[1] & 0x01) {
  1923. tf->hob_feature = scsicmd[3];
  1924. tf->hob_nsect = scsicmd[5];
  1925. tf->hob_lbal = scsicmd[7];
  1926. tf->hob_lbam = scsicmd[9];
  1927. tf->hob_lbah = scsicmd[11];
  1928. tf->flags |= ATA_TFLAG_LBA48;
  1929. } else
  1930. tf->flags &= ~ATA_TFLAG_LBA48;
  1931. /*
  1932. * Always copy low byte, device and command registers.
  1933. */
  1934. tf->feature = scsicmd[4];
  1935. tf->nsect = scsicmd[6];
  1936. tf->lbal = scsicmd[8];
  1937. tf->lbam = scsicmd[10];
  1938. tf->lbah = scsicmd[12];
  1939. tf->device = scsicmd[13];
  1940. tf->command = scsicmd[14];
  1941. } else {
  1942. /*
  1943. * 12-byte CDB - incapable of extended commands.
  1944. */
  1945. tf->flags &= ~ATA_TFLAG_LBA48;
  1946. tf->feature = scsicmd[3];
  1947. tf->nsect = scsicmd[4];
  1948. tf->lbal = scsicmd[5];
  1949. tf->lbam = scsicmd[6];
  1950. tf->lbah = scsicmd[7];
  1951. tf->device = scsicmd[8];
  1952. tf->command = scsicmd[9];
  1953. }
  1954. /*
  1955. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  1956. * SET_FEATURES - XFER MODE must be preceded/succeeded
  1957. * by an update to hardware-specific registers for each
  1958. * controller (i.e. the reason for ->set_piomode(),
  1959. * ->set_dmamode(), and ->post_set_mode() hooks).
  1960. */
  1961. if ((tf->command == ATA_CMD_SET_FEATURES)
  1962. && (tf->feature == SETFEATURES_XFER))
  1963. return 1;
  1964. /*
  1965. * Set flags so that all registers will be written,
  1966. * and pass on write indication (used for PIO/DMA
  1967. * setup.)
  1968. */
  1969. tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
  1970. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1971. tf->flags |= ATA_TFLAG_WRITE;
  1972. /*
  1973. * Set transfer length.
  1974. *
  1975. * TODO: find out if we need to do more here to
  1976. * cover scatter/gather case.
  1977. */
  1978. qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
  1979. return 0;
  1980. }
  1981. /**
  1982. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1983. * @dev: ATA device
  1984. * @cmd: SCSI command opcode to consider
  1985. *
  1986. * Look up the SCSI command given, and determine whether the
  1987. * SCSI command is to be translated or simulated.
  1988. *
  1989. * RETURNS:
  1990. * Pointer to translation function if possible, %NULL if not.
  1991. */
  1992. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  1993. {
  1994. switch (cmd) {
  1995. case READ_6:
  1996. case READ_10:
  1997. case READ_16:
  1998. case WRITE_6:
  1999. case WRITE_10:
  2000. case WRITE_16:
  2001. return ata_scsi_rw_xlat;
  2002. case SYNCHRONIZE_CACHE:
  2003. if (ata_try_flush_cache(dev))
  2004. return ata_scsi_flush_xlat;
  2005. break;
  2006. case VERIFY:
  2007. case VERIFY_16:
  2008. return ata_scsi_verify_xlat;
  2009. case ATA_12:
  2010. case ATA_16:
  2011. return ata_scsi_pass_thru;
  2012. case START_STOP:
  2013. return ata_scsi_start_stop_xlat;
  2014. }
  2015. return NULL;
  2016. }
  2017. /**
  2018. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  2019. * @ap: ATA port to which the command was being sent
  2020. * @cmd: SCSI command to dump
  2021. *
  2022. * Prints the contents of a SCSI command via printk().
  2023. */
  2024. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  2025. struct scsi_cmnd *cmd)
  2026. {
  2027. #ifdef ATA_DEBUG
  2028. struct scsi_device *scsidev = cmd->device;
  2029. u8 *scsicmd = cmd->cmnd;
  2030. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  2031. ap->id,
  2032. scsidev->channel, scsidev->id, scsidev->lun,
  2033. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  2034. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  2035. scsicmd[8]);
  2036. #endif
  2037. }
  2038. /**
  2039. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  2040. * @cmd: SCSI command to be sent
  2041. * @done: Completion function, called when command is complete
  2042. *
  2043. * In some cases, this function translates SCSI commands into
  2044. * ATA taskfiles, and queues the taskfiles to be sent to
  2045. * hardware. In other cases, this function simulates a
  2046. * SCSI device by evaluating and responding to certain
  2047. * SCSI commands. This creates the overall effect of
  2048. * ATA and ATAPI devices appearing as SCSI devices.
  2049. *
  2050. * LOCKING:
  2051. * Releases scsi-layer-held lock, and obtains host_set lock.
  2052. *
  2053. * RETURNS:
  2054. * Zero.
  2055. */
  2056. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  2057. {
  2058. struct ata_port *ap;
  2059. struct ata_device *dev;
  2060. struct scsi_device *scsidev = cmd->device;
  2061. struct Scsi_Host *shost = scsidev->host;
  2062. ap = (struct ata_port *) &shost->hostdata[0];
  2063. spin_unlock(shost->host_lock);
  2064. spin_lock(&ap->host_set->lock);
  2065. ata_scsi_dump_cdb(ap, cmd);
  2066. dev = ata_scsi_find_dev(ap, scsidev);
  2067. if (unlikely(!dev)) {
  2068. cmd->result = (DID_BAD_TARGET << 16);
  2069. done(cmd);
  2070. goto out_unlock;
  2071. }
  2072. if (dev->class == ATA_DEV_ATA) {
  2073. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  2074. cmd->cmnd[0]);
  2075. if (xlat_func)
  2076. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  2077. else
  2078. ata_scsi_simulate(dev->id, cmd, done);
  2079. } else
  2080. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  2081. out_unlock:
  2082. spin_unlock(&ap->host_set->lock);
  2083. spin_lock(shost->host_lock);
  2084. return 0;
  2085. }
  2086. /**
  2087. * ata_scsi_simulate - simulate SCSI command on ATA device
  2088. * @id: current IDENTIFY data for target device.
  2089. * @cmd: SCSI command being sent to device.
  2090. * @done: SCSI command completion function.
  2091. *
  2092. * Interprets and directly executes a select list of SCSI commands
  2093. * that can be handled internally.
  2094. *
  2095. * LOCKING:
  2096. * spin_lock_irqsave(host_set lock)
  2097. */
  2098. void ata_scsi_simulate(u16 *id,
  2099. struct scsi_cmnd *cmd,
  2100. void (*done)(struct scsi_cmnd *))
  2101. {
  2102. struct ata_scsi_args args;
  2103. const u8 *scsicmd = cmd->cmnd;
  2104. args.id = id;
  2105. args.cmd = cmd;
  2106. args.done = done;
  2107. switch(scsicmd[0]) {
  2108. /* no-op's, complete with success */
  2109. case SYNCHRONIZE_CACHE:
  2110. case REZERO_UNIT:
  2111. case SEEK_6:
  2112. case SEEK_10:
  2113. case TEST_UNIT_READY:
  2114. case FORMAT_UNIT: /* FIXME: correct? */
  2115. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  2116. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  2117. break;
  2118. case INQUIRY:
  2119. if (scsicmd[1] & 2) /* is CmdDt set? */
  2120. ata_scsi_invalid_field(cmd, done);
  2121. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  2122. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  2123. else if (scsicmd[2] == 0x00)
  2124. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  2125. else if (scsicmd[2] == 0x80)
  2126. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  2127. else if (scsicmd[2] == 0x83)
  2128. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  2129. else
  2130. ata_scsi_invalid_field(cmd, done);
  2131. break;
  2132. case MODE_SENSE:
  2133. case MODE_SENSE_10:
  2134. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  2135. break;
  2136. case MODE_SELECT: /* unconditionally return */
  2137. case MODE_SELECT_10: /* bad-field-in-cdb */
  2138. ata_scsi_invalid_field(cmd, done);
  2139. break;
  2140. case READ_CAPACITY:
  2141. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2142. break;
  2143. case SERVICE_ACTION_IN:
  2144. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  2145. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  2146. else
  2147. ata_scsi_invalid_field(cmd, done);
  2148. break;
  2149. case REPORT_LUNS:
  2150. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  2151. break;
  2152. /* mandatory commands we haven't implemented yet */
  2153. case REQUEST_SENSE:
  2154. /* all other commands */
  2155. default:
  2156. ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
  2157. /* "Invalid command operation code" */
  2158. done(cmd);
  2159. break;
  2160. }
  2161. }
  2162. void ata_scsi_scan_host(struct ata_port *ap)
  2163. {
  2164. struct ata_device *dev;
  2165. unsigned int i;
  2166. if (ap->flags & ATA_FLAG_PORT_DISABLED)
  2167. return;
  2168. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2169. dev = &ap->device[i];
  2170. if (ata_dev_present(dev))
  2171. scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
  2172. }
  2173. }