libata-scsi.c 69 KB

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