libata-scsi.c 67 KB

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