libata-scsi.c 67 KB

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