libata-scsi.c 62 KB

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