libata-scsi.c 62 KB

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