libata-scsi.c 68 KB

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