libata-scsi.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  1. /*
  2. libata-scsi.c - helper library for ATA
  3. Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  4. Copyright 2003-2004 Jeff Garzik
  5. The contents of this file are subject to the Open
  6. Software License version 1.1 that can be found at
  7. http://www.opensource.org/licenses/osl-1.1.txt and is included herein
  8. by reference.
  9. Alternatively, the contents of this file may be used under the terms
  10. of the GNU General Public License version 2 (the "GPL") as distributed
  11. in the kernel source COPYING file, in which case the provisions of
  12. the GPL are applicable instead of the above. If you wish to allow
  13. the use of your version of this file only under the terms of the
  14. GPL and not to allow others to use your version of this file under
  15. the OSL, indicate your decision by deleting the provisions above and
  16. replace them with the notice and other provisions required by the GPL.
  17. If you do not delete the provisions above, a recipient may use your
  18. version of this file under either the OSL or the GPL.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/spinlock.h>
  23. #include <scsi/scsi.h>
  24. #include "scsi.h"
  25. #include <scsi/scsi_host.h>
  26. #include <linux/libata.h>
  27. #include <linux/hdreg.h>
  28. #include <asm/uaccess.h>
  29. #include "libata.h"
  30. #define SECTOR_SIZE 512
  31. typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
  32. static struct ata_device *
  33. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
  34. /**
  35. * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
  36. * @sdev: SCSI device for which BIOS geometry is to be determined
  37. * @bdev: block device associated with @sdev
  38. * @capacity: capacity of SCSI device
  39. * @geom: location to which geometry will be output
  40. *
  41. * Generic bios head/sector/cylinder calculator
  42. * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
  43. * mapping. Some situations may arise where the disk is not
  44. * bootable if this is not used.
  45. *
  46. * LOCKING:
  47. * Defined by the SCSI layer. We don't really care.
  48. *
  49. * RETURNS:
  50. * Zero.
  51. */
  52. int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
  53. sector_t capacity, int geom[])
  54. {
  55. geom[0] = 255;
  56. geom[1] = 63;
  57. sector_div(capacity, 255*63);
  58. geom[2] = capacity;
  59. return 0;
  60. }
  61. /**
  62. * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
  63. * @dev: Device to whom we are issuing command
  64. * @arg: User provided data for issuing command
  65. *
  66. * LOCKING:
  67. * Defined by the SCSI layer. We don't really care.
  68. *
  69. * RETURNS:
  70. * Zero on success, negative errno on error.
  71. */
  72. int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
  73. {
  74. int rc = 0;
  75. u8 scsi_cmd[MAX_COMMAND_SIZE];
  76. u8 args[4], *argbuf = NULL;
  77. int argsize = 0;
  78. struct scsi_request *sreq;
  79. if (NULL == (void *)arg)
  80. return -EINVAL;
  81. if (copy_from_user(args, arg, sizeof(args)))
  82. return -EFAULT;
  83. sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
  84. if (!sreq)
  85. return -EINTR;
  86. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  87. if (args[3]) {
  88. argsize = SECTOR_SIZE * args[3];
  89. argbuf = kmalloc(argsize, GFP_KERNEL);
  90. if (argbuf == NULL)
  91. return -ENOMEM;
  92. scsi_cmd[1] = (4 << 1); /* PIO Data-in */
  93. scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
  94. block count in sector count field */
  95. sreq->sr_data_direction = DMA_FROM_DEVICE;
  96. } else {
  97. scsi_cmd[1] = (3 << 1); /* Non-data */
  98. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  99. sreq->sr_data_direction = DMA_NONE;
  100. }
  101. scsi_cmd[0] = ATA_16;
  102. scsi_cmd[4] = args[2];
  103. if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
  104. scsi_cmd[6] = args[3];
  105. scsi_cmd[8] = args[1];
  106. scsi_cmd[10] = 0x4f;
  107. scsi_cmd[12] = 0xc2;
  108. } else {
  109. scsi_cmd[6] = args[1];
  110. }
  111. scsi_cmd[14] = args[0];
  112. /* Good values for timeout and retries? Values below
  113. from scsi_ioctl_send_command() for default case... */
  114. scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
  115. if (sreq->sr_result) {
  116. rc = -EIO;
  117. goto error;
  118. }
  119. /* Need code to retrieve data from check condition? */
  120. if ((argbuf)
  121. && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
  122. rc = -EFAULT;
  123. error:
  124. scsi_release_request(sreq);
  125. if (argbuf)
  126. kfree(argbuf);
  127. return rc;
  128. }
  129. /**
  130. * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
  131. * @dev: Device to whom 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_task_ioctl(struct scsi_device *scsidev, void __user *arg)
  141. {
  142. int rc = 0;
  143. u8 scsi_cmd[MAX_COMMAND_SIZE];
  144. u8 args[7];
  145. struct scsi_request *sreq;
  146. if (NULL == (void *)arg)
  147. return -EINVAL;
  148. if (copy_from_user(args, arg, sizeof(args)))
  149. return -EFAULT;
  150. memset(scsi_cmd, 0, sizeof(scsi_cmd));
  151. scsi_cmd[0] = ATA_16;
  152. scsi_cmd[1] = (3 << 1); /* Non-data */
  153. /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
  154. scsi_cmd[4] = args[1];
  155. scsi_cmd[6] = args[2];
  156. scsi_cmd[8] = args[3];
  157. scsi_cmd[10] = args[4];
  158. scsi_cmd[12] = args[5];
  159. scsi_cmd[14] = args[0];
  160. sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
  161. if (!sreq) {
  162. rc = -EINTR;
  163. goto error;
  164. }
  165. sreq->sr_data_direction = DMA_NONE;
  166. /* Good values for timeout and retries? Values below
  167. from scsi_ioctl_send_command() for default case... */
  168. scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
  169. if (sreq->sr_result) {
  170. rc = -EIO;
  171. goto error;
  172. }
  173. /* Need code to retrieve data from check condition? */
  174. error:
  175. scsi_release_request(sreq);
  176. return rc;
  177. }
  178. int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
  179. {
  180. struct ata_port *ap;
  181. struct ata_device *dev;
  182. int val = -EINVAL, rc = -EINVAL;
  183. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  184. if (!ap)
  185. goto out;
  186. dev = ata_scsi_find_dev(ap, scsidev);
  187. if (!dev) {
  188. rc = -ENODEV;
  189. goto out;
  190. }
  191. switch (cmd) {
  192. case ATA_IOC_GET_IO32:
  193. val = 0;
  194. if (copy_to_user(arg, &val, 1))
  195. return -EFAULT;
  196. return 0;
  197. case ATA_IOC_SET_IO32:
  198. val = (unsigned long) arg;
  199. if (val != 0)
  200. return -EINVAL;
  201. return 0;
  202. case HDIO_DRIVE_CMD:
  203. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  204. return -EACCES;
  205. return ata_cmd_ioctl(scsidev, arg);
  206. case HDIO_DRIVE_TASK:
  207. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  208. return -EACCES;
  209. return ata_task_ioctl(scsidev, arg);
  210. default:
  211. rc = -ENOTTY;
  212. break;
  213. }
  214. out:
  215. return rc;
  216. }
  217. /**
  218. * ata_scsi_qc_new - acquire new ata_queued_cmd reference
  219. * @ap: ATA port to which the new command is attached
  220. * @dev: ATA device to which the new command is attached
  221. * @cmd: SCSI command that originated this ATA command
  222. * @done: SCSI command completion function
  223. *
  224. * Obtain a reference to an unused ata_queued_cmd structure,
  225. * which is the basic libata structure representing a single
  226. * ATA command sent to the hardware.
  227. *
  228. * If a command was available, fill in the SCSI-specific
  229. * portions of the structure with information on the
  230. * current command.
  231. *
  232. * LOCKING:
  233. * spin_lock_irqsave(host_set lock)
  234. *
  235. * RETURNS:
  236. * Command allocated, or %NULL if none available.
  237. */
  238. struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
  239. struct ata_device *dev,
  240. struct scsi_cmnd *cmd,
  241. void (*done)(struct scsi_cmnd *))
  242. {
  243. struct ata_queued_cmd *qc;
  244. qc = ata_qc_new_init(ap, dev);
  245. if (qc) {
  246. qc->scsicmd = cmd;
  247. qc->scsidone = done;
  248. if (cmd->use_sg) {
  249. qc->sg = (struct scatterlist *) cmd->request_buffer;
  250. qc->n_elem = cmd->use_sg;
  251. } else {
  252. qc->sg = &qc->sgent;
  253. qc->n_elem = 1;
  254. }
  255. } else {
  256. cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
  257. done(cmd);
  258. }
  259. return qc;
  260. }
  261. /**
  262. * ata_dump_status - user friendly display of error info
  263. * @id: id of the port in question
  264. * @tf: ptr to filled out taskfile
  265. *
  266. * Decode and dump the ATA error/status registers for the user so
  267. * that they have some idea what really happened at the non
  268. * make-believe layer.
  269. *
  270. * LOCKING:
  271. * inherited from caller
  272. */
  273. void ata_dump_status(unsigned id, struct ata_taskfile *tf)
  274. {
  275. u8 stat = tf->command, err = tf->feature;
  276. printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
  277. if (stat & ATA_BUSY) {
  278. printk("Busy }\n"); /* Data is not valid in this case */
  279. } else {
  280. if (stat & 0x40) printk("DriveReady ");
  281. if (stat & 0x20) printk("DeviceFault ");
  282. if (stat & 0x10) printk("SeekComplete ");
  283. if (stat & 0x08) printk("DataRequest ");
  284. if (stat & 0x04) printk("CorrectedError ");
  285. if (stat & 0x02) printk("Index ");
  286. if (stat & 0x01) printk("Error ");
  287. printk("}\n");
  288. if (err) {
  289. printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
  290. if (err & 0x04) printk("DriveStatusError ");
  291. if (err & 0x80) {
  292. if (err & 0x04) printk("BadCRC ");
  293. else printk("Sector ");
  294. }
  295. if (err & 0x40) printk("UncorrectableError ");
  296. if (err & 0x10) printk("SectorIdNotFound ");
  297. if (err & 0x02) printk("TrackZeroNotFound ");
  298. if (err & 0x01) printk("AddrMarkNotFound ");
  299. printk("}\n");
  300. }
  301. }
  302. }
  303. /**
  304. * ata_to_sense_error - convert ATA error to SCSI error
  305. * @drv_stat: value contained in ATA status register
  306. * @drv_err: value contained in ATA error register
  307. * @sk: the sense key we'll fill out
  308. * @asc: the additional sense code we'll fill out
  309. * @ascq: the additional sense code qualifier we'll fill out
  310. *
  311. * Converts an ATA error into a SCSI error. Fill out pointers to
  312. * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
  313. * format sense blocks.
  314. *
  315. * LOCKING:
  316. * spin_lock_irqsave(host_set lock)
  317. */
  318. void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
  319. u8 *ascq)
  320. {
  321. int i;
  322. /* Based on the 3ware driver translation table */
  323. static unsigned char sense_table[][4] = {
  324. /* BBD|ECC|ID|MAR */
  325. {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  326. /* BBD|ECC|ID */
  327. {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
  328. /* ECC|MC|MARK */
  329. {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
  330. /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
  331. {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
  332. /* MC|ID|ABRT|TRK0|MARK */
  333. {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
  334. /* MCR|MARK */
  335. {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
  336. /* Bad address mark */
  337. {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
  338. /* TRK0 */
  339. {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
  340. /* Abort & !ICRC */
  341. {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
  342. /* Media change request */
  343. {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
  344. /* SRV */
  345. {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
  346. /* Media change */
  347. {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
  348. /* ECC */
  349. {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
  350. /* BBD - block marked bad */
  351. {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
  352. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  353. };
  354. static unsigned char stat_table[][4] = {
  355. /* Must be first because BUSY means no other bits valid */
  356. {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
  357. {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
  358. {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
  359. {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
  360. {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
  361. };
  362. /*
  363. * Is this an error we can process/parse
  364. */
  365. if (drv_stat & ATA_BUSY) {
  366. drv_err = 0; /* Ignore the err bits, they're invalid */
  367. }
  368. if (drv_err) {
  369. /* Look for drv_err */
  370. for (i = 0; sense_table[i][0] != 0xFF; i++) {
  371. /* Look for best matches first */
  372. if ((sense_table[i][0] & drv_err) ==
  373. sense_table[i][0]) {
  374. *sk = sense_table[i][1];
  375. *asc = sense_table[i][2];
  376. *ascq = sense_table[i][3];
  377. goto translate_done;
  378. }
  379. }
  380. /* No immediate match */
  381. printk(KERN_WARNING "ata%u: no sense translation for "
  382. "error 0x%02x\n", id, drv_err);
  383. }
  384. /* Fall back to interpreting status bits */
  385. for (i = 0; stat_table[i][0] != 0xFF; i++) {
  386. if (stat_table[i][0] & drv_stat) {
  387. *sk = stat_table[i][1];
  388. *asc = stat_table[i][2];
  389. *ascq = stat_table[i][3];
  390. goto translate_done;
  391. }
  392. }
  393. /* No error? Undecoded? */
  394. printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
  395. id, drv_stat);
  396. /* For our last chance pick, use medium read error because
  397. * it's much more common than an ATA drive telling you a write
  398. * has failed.
  399. */
  400. *sk = MEDIUM_ERROR;
  401. *asc = 0x11; /* "unrecovered read error" */
  402. *ascq = 0x04; /* "auto-reallocation failed" */
  403. translate_done:
  404. printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
  405. "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
  406. *sk, *asc, *ascq);
  407. return;
  408. }
  409. /*
  410. * ata_gen_ata_desc_sense - Generate check condition sense block.
  411. * @qc: Command that completed.
  412. *
  413. * This function is specific to the ATA descriptor format sense
  414. * block specified for the ATA pass through commands. Regardless
  415. * of whether the command errored or not, return a sense
  416. * block. Copy all controller registers into the sense
  417. * block. Clear sense key, ASC & ASCQ if there is no error.
  418. *
  419. * LOCKING:
  420. * spin_lock_irqsave(host_set lock)
  421. */
  422. void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
  423. {
  424. struct scsi_cmnd *cmd = qc->scsicmd;
  425. struct ata_taskfile *tf = &qc->tf;
  426. unsigned char *sb = cmd->sense_buffer;
  427. unsigned char *desc = sb + 8;
  428. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  429. cmd->result = SAM_STAT_CHECK_CONDITION;
  430. /*
  431. * Read the controller registers.
  432. */
  433. assert(NULL != qc->ap->ops->tf_read);
  434. qc->ap->ops->tf_read(qc->ap, tf);
  435. /*
  436. * Use ata_to_sense_error() to map status register bits
  437. * onto sense key, asc & ascq.
  438. */
  439. if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
  440. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  441. &sb[1], &sb[2], &sb[3]);
  442. sb[1] &= 0x0f;
  443. }
  444. /*
  445. * Sense data is current and format is descriptor.
  446. */
  447. sb[0] = 0x72;
  448. desc[0] = 0x09;
  449. /*
  450. * Set length of additional sense data.
  451. * Since we only populate descriptor 0, the total
  452. * length is the same (fixed) length as descriptor 0.
  453. */
  454. desc[1] = sb[7] = 14;
  455. /*
  456. * Copy registers into sense buffer.
  457. */
  458. desc[2] = 0x00;
  459. desc[3] = tf->feature; /* == error reg */
  460. desc[5] = tf->nsect;
  461. desc[7] = tf->lbal;
  462. desc[9] = tf->lbam;
  463. desc[11] = tf->lbah;
  464. desc[12] = tf->device;
  465. desc[13] = tf->command; /* == status reg */
  466. /*
  467. * Fill in Extend bit, and the high order bytes
  468. * if applicable.
  469. */
  470. if (tf->flags & ATA_TFLAG_LBA48) {
  471. desc[2] |= 0x01;
  472. desc[4] = tf->hob_nsect;
  473. desc[6] = tf->hob_lbal;
  474. desc[8] = tf->hob_lbam;
  475. desc[10] = tf->hob_lbah;
  476. }
  477. }
  478. /**
  479. * ata_gen_fixed_sense - generate a SCSI fixed sense block
  480. * @qc: Command that we are erroring out
  481. *
  482. * Leverage ata_to_sense_error() to give us the codes. Fit our
  483. * LBA in here if there's room.
  484. *
  485. * LOCKING:
  486. * inherited from caller
  487. */
  488. void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
  489. {
  490. struct scsi_cmnd *cmd = qc->scsicmd;
  491. struct ata_taskfile *tf = &qc->tf;
  492. unsigned char *sb = cmd->sense_buffer;
  493. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  494. cmd->result = SAM_STAT_CHECK_CONDITION;
  495. /*
  496. * Read the controller registers.
  497. */
  498. assert(NULL != qc->ap->ops->tf_read);
  499. qc->ap->ops->tf_read(qc->ap, tf);
  500. /*
  501. * Use ata_to_sense_error() to map status register bits
  502. * onto sense key, asc & ascq.
  503. */
  504. if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
  505. ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
  506. &sb[2], &sb[12], &sb[13]);
  507. sb[2] &= 0x0f;
  508. }
  509. sb[0] = 0x70;
  510. sb[7] = 0x0a;
  511. if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
  512. /* A small (28b) LBA will fit in the 32b info field */
  513. sb[0] |= 0x80; /* set valid bit */
  514. sb[3] = tf->device & 0x0f;
  515. sb[4] = tf->lbah;
  516. sb[5] = tf->lbam;
  517. sb[6] = tf->lbal;
  518. }
  519. }
  520. /**
  521. * ata_scsi_slave_config - Set SCSI device attributes
  522. * @sdev: SCSI device to examine
  523. *
  524. * This is called before we actually start reading
  525. * and writing to the device, to configure certain
  526. * SCSI mid-layer behaviors.
  527. *
  528. * LOCKING:
  529. * Defined by SCSI layer. We don't really care.
  530. */
  531. int ata_scsi_slave_config(struct scsi_device *sdev)
  532. {
  533. sdev->use_10_for_rw = 1;
  534. sdev->use_10_for_ms = 1;
  535. blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
  536. if (sdev->id < ATA_MAX_DEVICES) {
  537. struct ata_port *ap;
  538. struct ata_device *dev;
  539. ap = (struct ata_port *) &sdev->host->hostdata[0];
  540. dev = &ap->device[sdev->id];
  541. /* TODO: 1024 is an arbitrary number, not the
  542. * hardware maximum. This should be increased to
  543. * 65534 when Jens Axboe's patch for dynamically
  544. * determining max_sectors is merged.
  545. */
  546. if ((dev->flags & ATA_DFLAG_LBA48) &&
  547. ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
  548. sdev->host->max_sectors = 2048;
  549. blk_queue_max_sectors(sdev->request_queue, 2048);
  550. }
  551. }
  552. return 0; /* scsi layer doesn't check return value, sigh */
  553. }
  554. /**
  555. * ata_scsi_error - SCSI layer error handler callback
  556. * @host: SCSI host on which error occurred
  557. *
  558. * Handles SCSI-layer-thrown error events.
  559. *
  560. * LOCKING:
  561. * Inherited from SCSI layer (none, can sleep)
  562. *
  563. * RETURNS:
  564. * Zero.
  565. */
  566. int ata_scsi_error(struct Scsi_Host *host)
  567. {
  568. struct ata_port *ap;
  569. DPRINTK("ENTER\n");
  570. ap = (struct ata_port *) &host->hostdata[0];
  571. ap->ops->eng_timeout(ap);
  572. /* TODO: this is per-command; when queueing is supported
  573. * this code will either change or move to a more
  574. * appropriate place
  575. */
  576. host->host_failed--;
  577. DPRINTK("EXIT\n");
  578. return 0;
  579. }
  580. /**
  581. * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
  582. * @qc: Storage for translated ATA taskfile
  583. * @scsicmd: SCSI command to translate (ignored)
  584. *
  585. * Sets up an ATA taskfile to issue FLUSH CACHE or
  586. * FLUSH CACHE EXT.
  587. *
  588. * LOCKING:
  589. * spin_lock_irqsave(host_set lock)
  590. *
  591. * RETURNS:
  592. * Zero on success, non-zero on error.
  593. */
  594. static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  595. {
  596. struct ata_taskfile *tf = &qc->tf;
  597. tf->flags |= ATA_TFLAG_DEVICE;
  598. tf->protocol = ATA_PROT_NODATA;
  599. if ((tf->flags & ATA_TFLAG_LBA48) &&
  600. (ata_id_has_flush_ext(qc->dev->id)))
  601. tf->command = ATA_CMD_FLUSH_EXT;
  602. else
  603. tf->command = ATA_CMD_FLUSH;
  604. return 0;
  605. }
  606. /**
  607. * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
  608. * @qc: Storage for translated ATA taskfile
  609. * @scsicmd: SCSI command to translate
  610. *
  611. * Converts SCSI VERIFY command to an ATA READ VERIFY command.
  612. *
  613. * LOCKING:
  614. * spin_lock_irqsave(host_set lock)
  615. *
  616. * RETURNS:
  617. * Zero on success, non-zero on error.
  618. */
  619. static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  620. {
  621. struct ata_taskfile *tf = &qc->tf;
  622. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  623. u64 dev_sectors = qc->dev->n_sectors;
  624. u64 sect = 0;
  625. u32 n_sect = 0;
  626. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  627. tf->protocol = ATA_PROT_NODATA;
  628. tf->device |= ATA_LBA;
  629. if (scsicmd[0] == VERIFY) {
  630. sect |= ((u64)scsicmd[2]) << 24;
  631. sect |= ((u64)scsicmd[3]) << 16;
  632. sect |= ((u64)scsicmd[4]) << 8;
  633. sect |= ((u64)scsicmd[5]);
  634. n_sect |= ((u32)scsicmd[7]) << 8;
  635. n_sect |= ((u32)scsicmd[8]);
  636. }
  637. else if (scsicmd[0] == VERIFY_16) {
  638. sect |= ((u64)scsicmd[2]) << 56;
  639. sect |= ((u64)scsicmd[3]) << 48;
  640. sect |= ((u64)scsicmd[4]) << 40;
  641. sect |= ((u64)scsicmd[5]) << 32;
  642. sect |= ((u64)scsicmd[6]) << 24;
  643. sect |= ((u64)scsicmd[7]) << 16;
  644. sect |= ((u64)scsicmd[8]) << 8;
  645. sect |= ((u64)scsicmd[9]);
  646. n_sect |= ((u32)scsicmd[10]) << 24;
  647. n_sect |= ((u32)scsicmd[11]) << 16;
  648. n_sect |= ((u32)scsicmd[12]) << 8;
  649. n_sect |= ((u32)scsicmd[13]);
  650. }
  651. else
  652. return 1;
  653. if (!n_sect)
  654. return 1;
  655. if (sect >= dev_sectors)
  656. return 1;
  657. if ((sect + n_sect) > dev_sectors)
  658. return 1;
  659. if (lba48) {
  660. if (n_sect > (64 * 1024))
  661. return 1;
  662. } else {
  663. if (n_sect > 256)
  664. return 1;
  665. }
  666. if (lba48) {
  667. tf->command = ATA_CMD_VERIFY_EXT;
  668. tf->hob_nsect = (n_sect >> 8) & 0xff;
  669. tf->hob_lbah = (sect >> 40) & 0xff;
  670. tf->hob_lbam = (sect >> 32) & 0xff;
  671. tf->hob_lbal = (sect >> 24) & 0xff;
  672. } else {
  673. tf->command = ATA_CMD_VERIFY;
  674. tf->device |= (sect >> 24) & 0xf;
  675. }
  676. tf->nsect = n_sect & 0xff;
  677. tf->lbah = (sect >> 16) & 0xff;
  678. tf->lbam = (sect >> 8) & 0xff;
  679. tf->lbal = sect & 0xff;
  680. return 0;
  681. }
  682. /**
  683. * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
  684. * @qc: Storage for translated ATA taskfile
  685. * @scsicmd: SCSI command to translate
  686. *
  687. * Converts any of six SCSI read/write commands into the
  688. * ATA counterpart, including starting sector (LBA),
  689. * sector count, and taking into account the device's LBA48
  690. * support.
  691. *
  692. * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
  693. * %WRITE_16 are currently supported.
  694. *
  695. * LOCKING:
  696. * spin_lock_irqsave(host_set lock)
  697. *
  698. * RETURNS:
  699. * Zero on success, non-zero on error.
  700. */
  701. static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  702. {
  703. struct ata_taskfile *tf = &qc->tf;
  704. unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
  705. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  706. tf->protocol = qc->dev->xfer_protocol;
  707. tf->device |= ATA_LBA;
  708. if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
  709. scsicmd[0] == READ_16) {
  710. tf->command = qc->dev->read_cmd;
  711. } else {
  712. tf->command = qc->dev->write_cmd;
  713. tf->flags |= ATA_TFLAG_WRITE;
  714. }
  715. if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
  716. if (lba48) {
  717. tf->hob_nsect = scsicmd[7];
  718. tf->hob_lbal = scsicmd[2];
  719. qc->nsect = ((unsigned int)scsicmd[7] << 8) |
  720. scsicmd[8];
  721. } else {
  722. /* if we don't support LBA48 addressing, the request
  723. * -may- be too large. */
  724. if ((scsicmd[2] & 0xf0) || scsicmd[7])
  725. return 1;
  726. /* stores LBA27:24 in lower 4 bits of device reg */
  727. tf->device |= scsicmd[2];
  728. qc->nsect = scsicmd[8];
  729. }
  730. tf->nsect = scsicmd[8];
  731. tf->lbal = scsicmd[5];
  732. tf->lbam = scsicmd[4];
  733. tf->lbah = scsicmd[3];
  734. VPRINTK("ten-byte command\n");
  735. return 0;
  736. }
  737. if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
  738. qc->nsect = tf->nsect = scsicmd[4];
  739. tf->lbal = scsicmd[3];
  740. tf->lbam = scsicmd[2];
  741. tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
  742. VPRINTK("six-byte command\n");
  743. return 0;
  744. }
  745. if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
  746. /* rule out impossible LBAs and sector counts */
  747. if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
  748. return 1;
  749. if (lba48) {
  750. tf->hob_nsect = scsicmd[12];
  751. tf->hob_lbal = scsicmd[6];
  752. tf->hob_lbam = scsicmd[5];
  753. tf->hob_lbah = scsicmd[4];
  754. qc->nsect = ((unsigned int)scsicmd[12] << 8) |
  755. scsicmd[13];
  756. } else {
  757. /* once again, filter out impossible non-zero values */
  758. if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
  759. (scsicmd[6] & 0xf0))
  760. return 1;
  761. /* stores LBA27:24 in lower 4 bits of device reg */
  762. tf->device |= scsicmd[6];
  763. qc->nsect = scsicmd[13];
  764. }
  765. tf->nsect = scsicmd[13];
  766. tf->lbal = scsicmd[9];
  767. tf->lbam = scsicmd[8];
  768. tf->lbah = scsicmd[7];
  769. VPRINTK("sixteen-byte command\n");
  770. return 0;
  771. }
  772. DPRINTK("no-byte command\n");
  773. return 1;
  774. }
  775. static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  776. {
  777. struct scsi_cmnd *cmd = qc->scsicmd;
  778. int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
  779. /* For ATA pass thru (SAT) commands, generate a sense block if
  780. * user mandated it or if there's an error. Note that if we
  781. * generate because the user forced us to, a check condition
  782. * is generated and the ATA register values are returned
  783. * whether the command completed successfully or not. If there
  784. * was no error, SK, ASC and ASCQ will all be zero.
  785. */
  786. if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
  787. ((cmd->cmnd[2] & 0x20) || need_sense)) {
  788. ata_gen_ata_desc_sense(qc);
  789. } else {
  790. if (!need_sense) {
  791. cmd->result = SAM_STAT_GOOD;
  792. } else {
  793. /* TODO: decide which descriptor format to use
  794. * for 48b LBA devices and call that here
  795. * instead of the fixed desc, which is only
  796. * good for smaller LBA (and maybe CHS?)
  797. * devices.
  798. */
  799. ata_gen_fixed_sense(qc);
  800. }
  801. }
  802. if (need_sense) {
  803. /* The ata_gen_..._sense routines fill in tf */
  804. ata_dump_status(qc->ap->id, &qc->tf);
  805. }
  806. qc->scsidone(cmd);
  807. return 0;
  808. }
  809. /**
  810. * ata_scsi_translate - Translate then issue SCSI command to ATA device
  811. * @ap: ATA port to which the command is addressed
  812. * @dev: ATA device to which the command is addressed
  813. * @cmd: SCSI command to execute
  814. * @done: SCSI command completion function
  815. * @xlat_func: Actor which translates @cmd to an ATA taskfile
  816. *
  817. * Our ->queuecommand() function has decided that the SCSI
  818. * command issued can be directly translated into an ATA
  819. * command, rather than handled internally.
  820. *
  821. * This function sets up an ata_queued_cmd structure for the
  822. * SCSI command, and sends that ata_queued_cmd to the hardware.
  823. *
  824. * LOCKING:
  825. * spin_lock_irqsave(host_set lock)
  826. */
  827. static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
  828. struct scsi_cmnd *cmd,
  829. void (*done)(struct scsi_cmnd *),
  830. ata_xlat_func_t xlat_func)
  831. {
  832. struct ata_queued_cmd *qc;
  833. u8 *scsicmd = cmd->cmnd;
  834. VPRINTK("ENTER\n");
  835. qc = ata_scsi_qc_new(ap, dev, cmd, done);
  836. if (!qc)
  837. return;
  838. /* data is present; dma-map it */
  839. if (cmd->sc_data_direction == SCSI_DATA_READ ||
  840. cmd->sc_data_direction == SCSI_DATA_WRITE) {
  841. if (unlikely(cmd->request_bufflen < 1)) {
  842. printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
  843. ap->id, dev->devno);
  844. goto err_out;
  845. }
  846. if (cmd->use_sg)
  847. ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
  848. else
  849. ata_sg_init_one(qc, cmd->request_buffer,
  850. cmd->request_bufflen);
  851. qc->dma_dir = cmd->sc_data_direction;
  852. }
  853. qc->complete_fn = ata_scsi_qc_complete;
  854. if (xlat_func(qc, scsicmd))
  855. goto err_out;
  856. /* select device, send command to hardware */
  857. if (ata_qc_issue(qc))
  858. goto err_out;
  859. VPRINTK("EXIT\n");
  860. return;
  861. err_out:
  862. ata_qc_free(qc);
  863. ata_bad_cdb(cmd, done);
  864. DPRINTK("EXIT - badcmd\n");
  865. }
  866. /**
  867. * ata_scsi_rbuf_get - Map response buffer.
  868. * @cmd: SCSI command containing buffer to be mapped.
  869. * @buf_out: Pointer to mapped area.
  870. *
  871. * Maps buffer contained within SCSI command @cmd.
  872. *
  873. * LOCKING:
  874. * spin_lock_irqsave(host_set lock)
  875. *
  876. * RETURNS:
  877. * Length of response buffer.
  878. */
  879. static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
  880. {
  881. u8 *buf;
  882. unsigned int buflen;
  883. if (cmd->use_sg) {
  884. struct scatterlist *sg;
  885. sg = (struct scatterlist *) cmd->request_buffer;
  886. buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
  887. buflen = sg->length;
  888. } else {
  889. buf = cmd->request_buffer;
  890. buflen = cmd->request_bufflen;
  891. }
  892. *buf_out = buf;
  893. return buflen;
  894. }
  895. /**
  896. * ata_scsi_rbuf_put - Unmap response buffer.
  897. * @cmd: SCSI command containing buffer to be unmapped.
  898. * @buf: buffer to unmap
  899. *
  900. * Unmaps response buffer contained within @cmd.
  901. *
  902. * LOCKING:
  903. * spin_lock_irqsave(host_set lock)
  904. */
  905. static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  906. {
  907. if (cmd->use_sg) {
  908. struct scatterlist *sg;
  909. sg = (struct scatterlist *) cmd->request_buffer;
  910. kunmap_atomic(buf - sg->offset, KM_USER0);
  911. }
  912. }
  913. /**
  914. * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
  915. * @args: device IDENTIFY data / SCSI command of interest.
  916. * @actor: Callback hook for desired SCSI command simulator
  917. *
  918. * Takes care of the hard work of simulating a SCSI command...
  919. * Mapping the response buffer, calling the command's handler,
  920. * and handling the handler's return value. This return value
  921. * indicates whether the handler wishes the SCSI command to be
  922. * completed successfully, or not.
  923. *
  924. * LOCKING:
  925. * spin_lock_irqsave(host_set lock)
  926. */
  927. void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
  928. unsigned int (*actor) (struct ata_scsi_args *args,
  929. u8 *rbuf, unsigned int buflen))
  930. {
  931. u8 *rbuf;
  932. unsigned int buflen, rc;
  933. struct scsi_cmnd *cmd = args->cmd;
  934. buflen = ata_scsi_rbuf_get(cmd, &rbuf);
  935. memset(rbuf, 0, buflen);
  936. rc = actor(args, rbuf, buflen);
  937. ata_scsi_rbuf_put(cmd, rbuf);
  938. if (rc)
  939. ata_bad_cdb(cmd, args->done);
  940. else {
  941. cmd->result = SAM_STAT_GOOD;
  942. args->done(cmd);
  943. }
  944. }
  945. /**
  946. * ata_scsiop_inq_std - Simulate INQUIRY command
  947. * @args: device IDENTIFY data / SCSI command of interest.
  948. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  949. * @buflen: Response buffer length.
  950. *
  951. * Returns standard device identification data associated
  952. * with non-EVPD INQUIRY command output.
  953. *
  954. * LOCKING:
  955. * spin_lock_irqsave(host_set lock)
  956. */
  957. unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
  958. unsigned int buflen)
  959. {
  960. u8 hdr[] = {
  961. TYPE_DISK,
  962. 0,
  963. 0x5, /* claim SPC-3 version compatibility */
  964. 2,
  965. 95 - 4
  966. };
  967. /* set scsi removeable (RMB) bit per ata bit */
  968. if (ata_id_removeable(args->id))
  969. hdr[1] |= (1 << 7);
  970. VPRINTK("ENTER\n");
  971. memcpy(rbuf, hdr, sizeof(hdr));
  972. if (buflen > 35) {
  973. memcpy(&rbuf[8], "ATA ", 8);
  974. ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
  975. ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
  976. if (rbuf[32] == 0 || rbuf[32] == ' ')
  977. memcpy(&rbuf[32], "n/a ", 4);
  978. }
  979. if (buflen > 63) {
  980. const u8 versions[] = {
  981. 0x60, /* SAM-3 (no version claimed) */
  982. 0x03,
  983. 0x20, /* SBC-2 (no version claimed) */
  984. 0x02,
  985. 0x60 /* SPC-3 (no version claimed) */
  986. };
  987. memcpy(rbuf + 59, versions, sizeof(versions));
  988. }
  989. return 0;
  990. }
  991. /**
  992. * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
  993. * @args: device IDENTIFY data / SCSI command of interest.
  994. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  995. * @buflen: Response buffer length.
  996. *
  997. * Returns list of inquiry EVPD pages available.
  998. *
  999. * LOCKING:
  1000. * spin_lock_irqsave(host_set lock)
  1001. */
  1002. unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
  1003. unsigned int buflen)
  1004. {
  1005. const u8 pages[] = {
  1006. 0x00, /* page 0x00, this page */
  1007. 0x80, /* page 0x80, unit serial no page */
  1008. 0x83 /* page 0x83, device ident page */
  1009. };
  1010. rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
  1011. if (buflen > 6)
  1012. memcpy(rbuf + 4, pages, sizeof(pages));
  1013. return 0;
  1014. }
  1015. /**
  1016. * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
  1017. * @args: device IDENTIFY data / SCSI command of interest.
  1018. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1019. * @buflen: Response buffer length.
  1020. *
  1021. * Returns ATA device serial number.
  1022. *
  1023. * LOCKING:
  1024. * spin_lock_irqsave(host_set lock)
  1025. */
  1026. unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
  1027. unsigned int buflen)
  1028. {
  1029. const u8 hdr[] = {
  1030. 0,
  1031. 0x80, /* this page code */
  1032. 0,
  1033. ATA_SERNO_LEN, /* page len */
  1034. };
  1035. memcpy(rbuf, hdr, sizeof(hdr));
  1036. if (buflen > (ATA_SERNO_LEN + 4 - 1))
  1037. ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
  1038. ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
  1039. return 0;
  1040. }
  1041. static const char *inq_83_str = "Linux ATA-SCSI simulator";
  1042. /**
  1043. * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
  1044. * @args: device IDENTIFY data / SCSI command of interest.
  1045. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1046. * @buflen: Response buffer length.
  1047. *
  1048. * Returns device identification. Currently hardcoded to
  1049. * return "Linux ATA-SCSI simulator".
  1050. *
  1051. * LOCKING:
  1052. * spin_lock_irqsave(host_set lock)
  1053. */
  1054. unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
  1055. unsigned int buflen)
  1056. {
  1057. rbuf[1] = 0x83; /* this page code */
  1058. rbuf[3] = 4 + strlen(inq_83_str); /* page len */
  1059. /* our one and only identification descriptor (vendor-specific) */
  1060. if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
  1061. rbuf[4 + 0] = 2; /* code set: ASCII */
  1062. rbuf[4 + 3] = strlen(inq_83_str);
  1063. memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
  1064. }
  1065. return 0;
  1066. }
  1067. /**
  1068. * ata_scsiop_noop -
  1069. * @args: device IDENTIFY data / SCSI command of interest.
  1070. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1071. * @buflen: Response buffer length.
  1072. *
  1073. * No operation. Simply returns success to caller, to indicate
  1074. * that the caller should successfully complete this SCSI command.
  1075. *
  1076. * LOCKING:
  1077. * spin_lock_irqsave(host_set lock)
  1078. */
  1079. unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
  1080. unsigned int buflen)
  1081. {
  1082. VPRINTK("ENTER\n");
  1083. return 0;
  1084. }
  1085. /**
  1086. * ata_msense_push - Push data onto MODE SENSE data output buffer
  1087. * @ptr_io: (input/output) Location to store more output data
  1088. * @last: End of output data buffer
  1089. * @buf: Pointer to BLOB being added to output buffer
  1090. * @buflen: Length of BLOB
  1091. *
  1092. * Store MODE SENSE data on an output buffer.
  1093. *
  1094. * LOCKING:
  1095. * None.
  1096. */
  1097. static void ata_msense_push(u8 **ptr_io, const u8 *last,
  1098. const u8 *buf, unsigned int buflen)
  1099. {
  1100. u8 *ptr = *ptr_io;
  1101. if ((ptr + buflen - 1) > last)
  1102. return;
  1103. memcpy(ptr, buf, buflen);
  1104. ptr += buflen;
  1105. *ptr_io = ptr;
  1106. }
  1107. /**
  1108. * ata_msense_caching - Simulate MODE SENSE caching info page
  1109. * @id: device IDENTIFY data
  1110. * @ptr_io: (input/output) Location to store more output data
  1111. * @last: End of output data buffer
  1112. *
  1113. * Generate a caching info page, which conditionally indicates
  1114. * write caching to the SCSI layer, depending on device
  1115. * capabilities.
  1116. *
  1117. * LOCKING:
  1118. * None.
  1119. */
  1120. static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
  1121. const u8 *last)
  1122. {
  1123. u8 page[] = {
  1124. 0x8, /* page code */
  1125. 0x12, /* page length */
  1126. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
  1127. 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
  1128. };
  1129. if (ata_id_wcache_enabled(id))
  1130. page[2] |= (1 << 2); /* write cache enable */
  1131. if (!ata_id_rahead_enabled(id))
  1132. page[12] |= (1 << 5); /* disable read ahead */
  1133. ata_msense_push(ptr_io, last, page, sizeof(page));
  1134. return sizeof(page);
  1135. }
  1136. /**
  1137. * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
  1138. * @dev: Device associated with this MODE SENSE command
  1139. * @ptr_io: (input/output) Location to store more output data
  1140. * @last: End of output data buffer
  1141. *
  1142. * Generate a generic MODE SENSE control mode page.
  1143. *
  1144. * LOCKING:
  1145. * None.
  1146. */
  1147. static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
  1148. {
  1149. const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
  1150. /* byte 2: set the descriptor format sense data bit (bit 2)
  1151. * since we need to support returning this format for SAT
  1152. * commands and any SCSI commands against a 48b LBA device.
  1153. */
  1154. ata_msense_push(ptr_io, last, page, sizeof(page));
  1155. return sizeof(page);
  1156. }
  1157. /**
  1158. * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
  1159. * @dev: Device associated with this MODE SENSE command
  1160. * @ptr_io: (input/output) Location to store more output data
  1161. * @last: End of output data buffer
  1162. *
  1163. * Generate a generic MODE SENSE r/w error recovery page.
  1164. *
  1165. * LOCKING:
  1166. * None.
  1167. */
  1168. static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
  1169. {
  1170. const u8 page[] = {
  1171. 0x1, /* page code */
  1172. 0xa, /* page length */
  1173. (1 << 7) | (1 << 6), /* note auto r/w reallocation */
  1174. 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
  1175. };
  1176. ata_msense_push(ptr_io, last, page, sizeof(page));
  1177. return sizeof(page);
  1178. }
  1179. /**
  1180. * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
  1181. * @args: device IDENTIFY data / SCSI command of interest.
  1182. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1183. * @buflen: Response buffer length.
  1184. *
  1185. * Simulate MODE SENSE commands.
  1186. *
  1187. * LOCKING:
  1188. * spin_lock_irqsave(host_set lock)
  1189. */
  1190. unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
  1191. unsigned int buflen)
  1192. {
  1193. u8 *scsicmd = args->cmd->cmnd, *p, *last;
  1194. unsigned int page_control, six_byte, output_len;
  1195. VPRINTK("ENTER\n");
  1196. six_byte = (scsicmd[0] == MODE_SENSE);
  1197. /* we only support saved and current values (which we treat
  1198. * in the same manner)
  1199. */
  1200. page_control = scsicmd[2] >> 6;
  1201. if ((page_control != 0) && (page_control != 3))
  1202. return 1;
  1203. if (six_byte)
  1204. output_len = 4;
  1205. else
  1206. output_len = 8;
  1207. p = rbuf + output_len;
  1208. last = rbuf + buflen - 1;
  1209. switch(scsicmd[2] & 0x3f) {
  1210. case 0x01: /* r/w error recovery */
  1211. output_len += ata_msense_rw_recovery(&p, last);
  1212. break;
  1213. case 0x08: /* caching */
  1214. output_len += ata_msense_caching(args->id, &p, last);
  1215. break;
  1216. case 0x0a: { /* control mode */
  1217. output_len += ata_msense_ctl_mode(&p, last);
  1218. break;
  1219. }
  1220. case 0x3f: /* all pages */
  1221. output_len += ata_msense_rw_recovery(&p, last);
  1222. output_len += ata_msense_caching(args->id, &p, last);
  1223. output_len += ata_msense_ctl_mode(&p, last);
  1224. break;
  1225. default: /* invalid page code */
  1226. return 1;
  1227. }
  1228. if (six_byte) {
  1229. output_len--;
  1230. rbuf[0] = output_len;
  1231. } else {
  1232. output_len -= 2;
  1233. rbuf[0] = output_len >> 8;
  1234. rbuf[1] = output_len;
  1235. }
  1236. return 0;
  1237. }
  1238. /**
  1239. * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
  1240. * @args: device IDENTIFY data / SCSI command of interest.
  1241. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1242. * @buflen: Response buffer length.
  1243. *
  1244. * Simulate READ CAPACITY commands.
  1245. *
  1246. * LOCKING:
  1247. * spin_lock_irqsave(host_set lock)
  1248. */
  1249. unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
  1250. unsigned int buflen)
  1251. {
  1252. u64 n_sectors;
  1253. u32 tmp;
  1254. VPRINTK("ENTER\n");
  1255. if (ata_id_has_lba48(args->id))
  1256. n_sectors = ata_id_u64(args->id, 100);
  1257. else
  1258. n_sectors = ata_id_u32(args->id, 60);
  1259. n_sectors--; /* ATA TotalUserSectors - 1 */
  1260. tmp = n_sectors; /* note: truncates, if lba48 */
  1261. if (args->cmd->cmnd[0] == READ_CAPACITY) {
  1262. /* sector count, 32-bit */
  1263. rbuf[0] = tmp >> (8 * 3);
  1264. rbuf[1] = tmp >> (8 * 2);
  1265. rbuf[2] = tmp >> (8 * 1);
  1266. rbuf[3] = tmp;
  1267. /* sector size */
  1268. tmp = ATA_SECT_SIZE;
  1269. rbuf[6] = tmp >> 8;
  1270. rbuf[7] = tmp;
  1271. } else {
  1272. /* sector count, 64-bit */
  1273. rbuf[2] = n_sectors >> (8 * 7);
  1274. rbuf[3] = n_sectors >> (8 * 6);
  1275. rbuf[4] = n_sectors >> (8 * 5);
  1276. rbuf[5] = n_sectors >> (8 * 4);
  1277. rbuf[6] = tmp >> (8 * 3);
  1278. rbuf[7] = tmp >> (8 * 2);
  1279. rbuf[8] = tmp >> (8 * 1);
  1280. rbuf[9] = tmp;
  1281. /* sector size */
  1282. tmp = ATA_SECT_SIZE;
  1283. rbuf[12] = tmp >> 8;
  1284. rbuf[13] = tmp;
  1285. }
  1286. return 0;
  1287. }
  1288. /**
  1289. * ata_scsiop_report_luns - Simulate REPORT LUNS command
  1290. * @args: device IDENTIFY data / SCSI command of interest.
  1291. * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
  1292. * @buflen: Response buffer length.
  1293. *
  1294. * Simulate REPORT LUNS command.
  1295. *
  1296. * LOCKING:
  1297. * spin_lock_irqsave(host_set lock)
  1298. */
  1299. unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
  1300. unsigned int buflen)
  1301. {
  1302. VPRINTK("ENTER\n");
  1303. rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
  1304. return 0;
  1305. }
  1306. /**
  1307. * ata_scsi_badcmd - End a SCSI request with an error
  1308. * @cmd: SCSI request to be handled
  1309. * @done: SCSI command completion function
  1310. * @asc: SCSI-defined additional sense code
  1311. * @ascq: SCSI-defined additional sense code qualifier
  1312. *
  1313. * Helper function that completes a SCSI command with
  1314. * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
  1315. * and the specified additional sense codes.
  1316. *
  1317. * LOCKING:
  1318. * spin_lock_irqsave(host_set lock)
  1319. */
  1320. void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
  1321. {
  1322. DPRINTK("ENTER\n");
  1323. cmd->result = SAM_STAT_CHECK_CONDITION;
  1324. cmd->sense_buffer[0] = 0x70;
  1325. cmd->sense_buffer[2] = ILLEGAL_REQUEST;
  1326. cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
  1327. cmd->sense_buffer[12] = asc;
  1328. cmd->sense_buffer[13] = ascq;
  1329. done(cmd);
  1330. }
  1331. static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
  1332. {
  1333. struct scsi_cmnd *cmd = qc->scsicmd;
  1334. if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
  1335. DPRINTK("request check condition\n");
  1336. cmd->result = SAM_STAT_CHECK_CONDITION;
  1337. qc->scsidone(cmd);
  1338. return 1;
  1339. } else {
  1340. u8 *scsicmd = cmd->cmnd;
  1341. if (scsicmd[0] == INQUIRY) {
  1342. u8 *buf = NULL;
  1343. unsigned int buflen;
  1344. buflen = ata_scsi_rbuf_get(cmd, &buf);
  1345. buf[2] = 0x5;
  1346. buf[3] = (buf[3] & 0xf0) | 2;
  1347. ata_scsi_rbuf_put(cmd, buf);
  1348. }
  1349. cmd->result = SAM_STAT_GOOD;
  1350. }
  1351. qc->scsidone(cmd);
  1352. return 0;
  1353. }
  1354. /**
  1355. * atapi_xlat - Initialize PACKET taskfile
  1356. * @qc: command structure to be initialized
  1357. * @scsicmd: SCSI CDB associated with this PACKET command
  1358. *
  1359. * LOCKING:
  1360. * spin_lock_irqsave(host_set lock)
  1361. *
  1362. * RETURNS:
  1363. * Zero on success, non-zero on failure.
  1364. */
  1365. static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
  1366. {
  1367. struct scsi_cmnd *cmd = qc->scsicmd;
  1368. struct ata_device *dev = qc->dev;
  1369. int using_pio = (dev->flags & ATA_DFLAG_PIO);
  1370. int nodata = (cmd->sc_data_direction == SCSI_DATA_NONE);
  1371. if (!using_pio)
  1372. /* Check whether ATAPI DMA is safe */
  1373. if (ata_check_atapi_dma(qc))
  1374. using_pio = 1;
  1375. memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
  1376. qc->complete_fn = atapi_qc_complete;
  1377. qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1378. if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
  1379. qc->tf.flags |= ATA_TFLAG_WRITE;
  1380. DPRINTK("direction: write\n");
  1381. }
  1382. qc->tf.command = ATA_CMD_PACKET;
  1383. /* no data, or PIO data xfer */
  1384. if (using_pio || nodata) {
  1385. if (nodata)
  1386. qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
  1387. else
  1388. qc->tf.protocol = ATA_PROT_ATAPI;
  1389. qc->tf.lbam = (8 * 1024) & 0xff;
  1390. qc->tf.lbah = (8 * 1024) >> 8;
  1391. }
  1392. /* DMA data xfer */
  1393. else {
  1394. qc->tf.protocol = ATA_PROT_ATAPI_DMA;
  1395. qc->tf.feature |= ATAPI_PKT_DMA;
  1396. #ifdef ATAPI_ENABLE_DMADIR
  1397. /* some SATA bridges need us to indicate data xfer direction */
  1398. if (cmd->sc_data_direction != SCSI_DATA_WRITE)
  1399. qc->tf.feature |= ATAPI_DMADIR;
  1400. #endif
  1401. }
  1402. qc->nbytes = cmd->bufflen;
  1403. return 0;
  1404. }
  1405. /**
  1406. * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
  1407. * @ap: ATA port to which the device is attached
  1408. * @scsidev: SCSI device from which we derive the ATA device
  1409. *
  1410. * Given various information provided in struct scsi_cmnd,
  1411. * map that onto an ATA bus, and using that mapping
  1412. * determine which ata_device is associated with the
  1413. * SCSI command to be sent.
  1414. *
  1415. * LOCKING:
  1416. * spin_lock_irqsave(host_set lock)
  1417. *
  1418. * RETURNS:
  1419. * Associated ATA device, or %NULL if not found.
  1420. */
  1421. static struct ata_device *
  1422. ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
  1423. {
  1424. struct ata_device *dev;
  1425. /* skip commands not addressed to targets we simulate */
  1426. if (likely(scsidev->id < ATA_MAX_DEVICES))
  1427. dev = &ap->device[scsidev->id];
  1428. else
  1429. return NULL;
  1430. if (unlikely((scsidev->channel != 0) ||
  1431. (scsidev->lun != 0)))
  1432. return NULL;
  1433. if (unlikely(!ata_dev_present(dev)))
  1434. return NULL;
  1435. #ifndef ATA_ENABLE_ATAPI
  1436. if (unlikely(dev->class == ATA_DEV_ATAPI))
  1437. return NULL;
  1438. #endif
  1439. return dev;
  1440. }
  1441. /*
  1442. * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
  1443. * @byte1: Byte 1 from pass-thru CDB.
  1444. *
  1445. * RETURNS:
  1446. * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
  1447. */
  1448. static u8
  1449. ata_scsi_map_proto(u8 byte1)
  1450. {
  1451. switch((byte1 & 0x1e) >> 1) {
  1452. case 3: /* Non-data */
  1453. return ATA_PROT_NODATA;
  1454. case 6: /* DMA */
  1455. return ATA_PROT_DMA;
  1456. case 4: /* PIO Data-in */
  1457. case 5: /* PIO Data-out */
  1458. if (byte1 & 0xe0) {
  1459. return ATA_PROT_PIO_MULT;
  1460. }
  1461. return ATA_PROT_PIO;
  1462. case 10: /* Device Reset */
  1463. case 0: /* Hard Reset */
  1464. case 1: /* SRST */
  1465. case 2: /* Bus Idle */
  1466. case 7: /* Packet */
  1467. case 8: /* DMA Queued */
  1468. case 9: /* Device Diagnostic */
  1469. case 11: /* UDMA Data-in */
  1470. case 12: /* UDMA Data-Out */
  1471. case 13: /* FPDMA */
  1472. default: /* Reserved */
  1473. break;
  1474. }
  1475. return ATA_PROT_UNKNOWN;
  1476. }
  1477. /**
  1478. * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
  1479. * @qc: command structure to be initialized
  1480. * @cmd: SCSI command to convert
  1481. *
  1482. * Handles either 12 or 16-byte versions of the CDB.
  1483. *
  1484. * RETURNS:
  1485. * Zero on success, non-zero on failure.
  1486. */
  1487. static unsigned int
  1488. ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
  1489. {
  1490. struct ata_taskfile *tf = &(qc->tf);
  1491. struct scsi_cmnd *cmd = qc->scsicmd;
  1492. if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
  1493. return 1;
  1494. /*
  1495. * 12 and 16 byte CDBs use different offsets to
  1496. * provide the various register values.
  1497. */
  1498. if (scsicmd[0] == ATA_16) {
  1499. /*
  1500. * 16-byte CDB - may contain extended commands.
  1501. *
  1502. * If that is the case, copy the upper byte register values.
  1503. */
  1504. if (scsicmd[1] & 0x01) {
  1505. tf->hob_feature = scsicmd[3];
  1506. tf->hob_nsect = scsicmd[5];
  1507. tf->hob_lbal = scsicmd[7];
  1508. tf->hob_lbam = scsicmd[9];
  1509. tf->hob_lbah = scsicmd[11];
  1510. tf->flags |= ATA_TFLAG_LBA48;
  1511. } else
  1512. tf->flags &= ~ATA_TFLAG_LBA48;
  1513. /*
  1514. * Always copy low byte, device and command registers.
  1515. */
  1516. tf->feature = scsicmd[4];
  1517. tf->nsect = scsicmd[6];
  1518. tf->lbal = scsicmd[8];
  1519. tf->lbam = scsicmd[10];
  1520. tf->lbah = scsicmd[12];
  1521. tf->device = scsicmd[13];
  1522. tf->command = scsicmd[14];
  1523. } else {
  1524. /*
  1525. * 12-byte CDB - incapable of extended commands.
  1526. */
  1527. tf->flags &= ~ATA_TFLAG_LBA48;
  1528. tf->feature = scsicmd[3];
  1529. tf->nsect = scsicmd[4];
  1530. tf->lbal = scsicmd[5];
  1531. tf->lbam = scsicmd[6];
  1532. tf->lbah = scsicmd[7];
  1533. tf->device = scsicmd[8];
  1534. tf->command = scsicmd[9];
  1535. }
  1536. /*
  1537. * Filter SET_FEATURES - XFER MODE command -- otherwise,
  1538. * SET_FEATURES - XFER MODE must be preceded/succeeded
  1539. * by an update to hardware-specific registers for each
  1540. * controller (i.e. the reason for ->set_piomode(),
  1541. * ->set_dmamode(), and ->post_set_mode() hooks).
  1542. */
  1543. if ((tf->command == ATA_CMD_SET_FEATURES)
  1544. && (tf->feature == SETFEATURES_XFER))
  1545. return 1;
  1546. /*
  1547. * Set flags so that all registers will be written,
  1548. * and pass on write indication (used for PIO/DMA
  1549. * setup.)
  1550. */
  1551. tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
  1552. if (cmd->sc_data_direction == SCSI_DATA_WRITE)
  1553. tf->flags |= ATA_TFLAG_WRITE;
  1554. /*
  1555. * Set transfer length.
  1556. *
  1557. * TODO: find out if we need to do more here to
  1558. * cover scatter/gather case.
  1559. */
  1560. qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
  1561. return 0;
  1562. }
  1563. /**
  1564. * ata_get_xlat_func - check if SCSI to ATA translation is possible
  1565. * @dev: ATA device
  1566. * @cmd: SCSI command opcode to consider
  1567. *
  1568. * Look up the SCSI command given, and determine whether the
  1569. * SCSI command is to be translated or simulated.
  1570. *
  1571. * RETURNS:
  1572. * Pointer to translation function if possible, %NULL if not.
  1573. */
  1574. static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
  1575. {
  1576. switch (cmd) {
  1577. case READ_6:
  1578. case READ_10:
  1579. case READ_16:
  1580. case WRITE_6:
  1581. case WRITE_10:
  1582. case WRITE_16:
  1583. return ata_scsi_rw_xlat;
  1584. case SYNCHRONIZE_CACHE:
  1585. if (ata_try_flush_cache(dev))
  1586. return ata_scsi_flush_xlat;
  1587. break;
  1588. case VERIFY:
  1589. case VERIFY_16:
  1590. return ata_scsi_verify_xlat;
  1591. case ATA_12:
  1592. case ATA_16:
  1593. return ata_scsi_pass_thru;
  1594. }
  1595. return NULL;
  1596. }
  1597. /**
  1598. * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
  1599. * @ap: ATA port to which the command was being sent
  1600. * @cmd: SCSI command to dump
  1601. *
  1602. * Prints the contents of a SCSI command via printk().
  1603. */
  1604. static inline void ata_scsi_dump_cdb(struct ata_port *ap,
  1605. struct scsi_cmnd *cmd)
  1606. {
  1607. #ifdef ATA_DEBUG
  1608. struct scsi_device *scsidev = cmd->device;
  1609. u8 *scsicmd = cmd->cmnd;
  1610. DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
  1611. ap->id,
  1612. scsidev->channel, scsidev->id, scsidev->lun,
  1613. scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
  1614. scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
  1615. scsicmd[8]);
  1616. #endif
  1617. }
  1618. /**
  1619. * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
  1620. * @cmd: SCSI command to be sent
  1621. * @done: Completion function, called when command is complete
  1622. *
  1623. * In some cases, this function translates SCSI commands into
  1624. * ATA taskfiles, and queues the taskfiles to be sent to
  1625. * hardware. In other cases, this function simulates a
  1626. * SCSI device by evaluating and responding to certain
  1627. * SCSI commands. This creates the overall effect of
  1628. * ATA and ATAPI devices appearing as SCSI devices.
  1629. *
  1630. * LOCKING:
  1631. * Releases scsi-layer-held lock, and obtains host_set lock.
  1632. *
  1633. * RETURNS:
  1634. * Zero.
  1635. */
  1636. int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  1637. {
  1638. struct ata_port *ap;
  1639. struct ata_device *dev;
  1640. struct scsi_device *scsidev = cmd->device;
  1641. ap = (struct ata_port *) &scsidev->host->hostdata[0];
  1642. ata_scsi_dump_cdb(ap, cmd);
  1643. dev = ata_scsi_find_dev(ap, scsidev);
  1644. if (unlikely(!dev)) {
  1645. cmd->result = (DID_BAD_TARGET << 16);
  1646. done(cmd);
  1647. goto out_unlock;
  1648. }
  1649. if (dev->class == ATA_DEV_ATA) {
  1650. ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
  1651. cmd->cmnd[0]);
  1652. if (xlat_func)
  1653. ata_scsi_translate(ap, dev, cmd, done, xlat_func);
  1654. else
  1655. ata_scsi_simulate(dev->id, cmd, done);
  1656. } else
  1657. ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
  1658. out_unlock:
  1659. return 0;
  1660. }
  1661. /**
  1662. * ata_scsi_simulate - simulate SCSI command on ATA device
  1663. * @id: current IDENTIFY data for target device.
  1664. * @cmd: SCSI command being sent to device.
  1665. * @done: SCSI command completion function.
  1666. *
  1667. * Interprets and directly executes a select list of SCSI commands
  1668. * that can be handled internally.
  1669. *
  1670. * LOCKING:
  1671. * spin_lock_irqsave(host_set lock)
  1672. */
  1673. void ata_scsi_simulate(u16 *id,
  1674. struct scsi_cmnd *cmd,
  1675. void (*done)(struct scsi_cmnd *))
  1676. {
  1677. struct ata_scsi_args args;
  1678. u8 *scsicmd = cmd->cmnd;
  1679. args.id = id;
  1680. args.cmd = cmd;
  1681. args.done = done;
  1682. switch(scsicmd[0]) {
  1683. /* no-op's, complete with success */
  1684. case SYNCHRONIZE_CACHE:
  1685. case REZERO_UNIT:
  1686. case SEEK_6:
  1687. case SEEK_10:
  1688. case TEST_UNIT_READY:
  1689. case FORMAT_UNIT: /* FIXME: correct? */
  1690. case SEND_DIAGNOSTIC: /* FIXME: correct? */
  1691. ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
  1692. break;
  1693. case INQUIRY:
  1694. if (scsicmd[1] & 2) /* is CmdDt set? */
  1695. ata_bad_cdb(cmd, done);
  1696. else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
  1697. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
  1698. else if (scsicmd[2] == 0x00)
  1699. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
  1700. else if (scsicmd[2] == 0x80)
  1701. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
  1702. else if (scsicmd[2] == 0x83)
  1703. ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
  1704. else
  1705. ata_bad_cdb(cmd, done);
  1706. break;
  1707. case MODE_SENSE:
  1708. case MODE_SENSE_10:
  1709. ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
  1710. break;
  1711. case MODE_SELECT: /* unconditionally return */
  1712. case MODE_SELECT_10: /* bad-field-in-cdb */
  1713. ata_bad_cdb(cmd, done);
  1714. break;
  1715. case READ_CAPACITY:
  1716. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1717. break;
  1718. case SERVICE_ACTION_IN:
  1719. if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
  1720. ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
  1721. else
  1722. ata_bad_cdb(cmd, done);
  1723. break;
  1724. case REPORT_LUNS:
  1725. ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
  1726. break;
  1727. /* mandatory commands we haven't implemented yet */
  1728. case REQUEST_SENSE:
  1729. /* all other commands */
  1730. default:
  1731. ata_bad_scsiop(cmd, done);
  1732. break;
  1733. }
  1734. }