cypress_atacb.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Support for emulating SAT (ata pass through) on devices based
  3. * on the Cypress USB/ATA bridge supporting ATACB.
  4. *
  5. * Copyright (c) 2008 Matthieu Castet (castet.matthieu@free.fr)
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <scsi/scsi.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <scsi/scsi_eh.h>
  24. #include <linux/ata.h>
  25. #include "usb.h"
  26. #include "protocol.h"
  27. #include "scsiglue.h"
  28. #include "debug.h"
  29. /*
  30. * ATACB is a protocol used on cypress usb<->ata bridge to
  31. * send raw ATA command over mass storage
  32. * There is a ATACB2 protocol that support LBA48 on newer chip.
  33. * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
  34. * datasheet from cypress.com.
  35. */
  36. void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
  37. {
  38. unsigned char save_cmnd[MAX_COMMAND_SIZE];
  39. if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
  40. usb_stor_transparent_scsi_command(srb, us);
  41. return;
  42. }
  43. memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
  44. memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
  45. /* check if we support the command */
  46. if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
  47. goto invalid_fld;
  48. /* check protocol */
  49. switch((save_cmnd[1] >> 1) & 0xf) {
  50. case 3: /*no DATA */
  51. case 4: /* PIO in */
  52. case 5: /* PIO out */
  53. break;
  54. default:
  55. goto invalid_fld;
  56. }
  57. /* first build the ATACB command */
  58. srb->cmd_len = 16;
  59. srb->cmnd[0] = 0x24; /* bVSCBSignature : vendor-specific command
  60. this value can change, but most(all ?) manufacturers
  61. keep the cypress default : 0x24 */
  62. srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
  63. srb->cmnd[3] = 0xff - 1; /* features, sector count, lba low, lba med
  64. lba high, device, command are valid */
  65. srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
  66. if (save_cmnd[0] == ATA_16) {
  67. srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
  68. srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
  69. srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
  70. srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
  71. srb->cmnd[10] = save_cmnd[12]; /* lba high */
  72. srb->cmnd[11] = save_cmnd[13]; /* device */
  73. srb->cmnd[12] = save_cmnd[14]; /* command */
  74. if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
  75. /* this could be supported by atacb2 */
  76. if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
  77. || save_cmnd[11])
  78. goto invalid_fld;
  79. }
  80. }
  81. else { /* ATA12 */
  82. srb->cmnd[ 6] = save_cmnd[3]; /* features */
  83. srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
  84. srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
  85. srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
  86. srb->cmnd[10] = save_cmnd[7]; /* lba high */
  87. srb->cmnd[11] = save_cmnd[8]; /* device */
  88. srb->cmnd[12] = save_cmnd[9]; /* command */
  89. }
  90. /* Filter SET_FEATURES - XFER MODE command */
  91. if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
  92. && (srb->cmnd[6] == SETFEATURES_XFER))
  93. goto invalid_fld;
  94. if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
  95. srb->cmnd[2] |= (1<<7); /* set IdentifyPacketDevice for these cmds */
  96. usb_stor_transparent_scsi_command(srb, us);
  97. /* if the device doesn't support ATACB
  98. */
  99. if (srb->result == SAM_STAT_CHECK_CONDITION &&
  100. memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
  101. sizeof(usb_stor_sense_invalidCDB)) == 0) {
  102. US_DEBUGP("cypress atacb not supported ???\n");
  103. goto end;
  104. }
  105. /* if ck_cond flags is set, and there wasn't critical error,
  106. * build the special sense
  107. */
  108. if ((srb->result != (DID_ERROR << 16) &&
  109. srb->result != (DID_ABORT << 16)) &&
  110. save_cmnd[2] & 0x20) {
  111. struct scsi_eh_save ses;
  112. unsigned char regs[8];
  113. unsigned char *sb = srb->sense_buffer;
  114. unsigned char *desc = sb + 8;
  115. int tmp_result;
  116. /* build the command for
  117. * reading the ATA registers */
  118. scsi_eh_prep_cmnd(srb, &ses, NULL, 0, 0);
  119. srb->sdb.length = sizeof(regs);
  120. sg_init_one(&ses.sense_sgl, regs, srb->sdb.length);
  121. srb->sdb.table.sgl = &ses.sense_sgl;
  122. srb->sc_data_direction = DMA_FROM_DEVICE;
  123. srb->sdb.table.nents = 1;
  124. /* we use the same command as before, but we set
  125. * the read taskfile bit, for not executing atacb command,
  126. * but reading register selected in srb->cmnd[4]
  127. */
  128. srb->cmnd[2] = 1;
  129. usb_stor_transparent_scsi_command(srb, us);
  130. tmp_result = srb->result;
  131. scsi_eh_restore_cmnd(srb, &ses);
  132. /* we fail to get registers, report invalid command */
  133. if (tmp_result != SAM_STAT_GOOD)
  134. goto invalid_fld;
  135. /* build the sense */
  136. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  137. /* set sk, asc for a good command */
  138. sb[1] = RECOVERED_ERROR;
  139. sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
  140. sb[3] = 0x1D;
  141. /* XXX we should generate sk, asc, ascq from status and error
  142. * regs
  143. * (see 11.1 Error translation ­ ATA device error to SCSI error map)
  144. * and ata_to_sense_error from libata.
  145. */
  146. /* Sense data is current and format is descriptor. */
  147. sb[0] = 0x72;
  148. desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
  149. /* set length of additional sense data */
  150. sb[7] = 14;
  151. desc[1] = 12;
  152. /* Copy registers into sense buffer. */
  153. desc[ 2] = 0x00;
  154. desc[ 3] = regs[1]; /* features */
  155. desc[ 5] = regs[2]; /* sector count */
  156. desc[ 7] = regs[3]; /* lba low */
  157. desc[ 9] = regs[4]; /* lba med */
  158. desc[11] = regs[5]; /* lba high */
  159. desc[12] = regs[6]; /* device */
  160. desc[13] = regs[7]; /* command */
  161. srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  162. }
  163. goto end;
  164. invalid_fld:
  165. srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  166. memcpy(srb->sense_buffer,
  167. usb_stor_sense_invalidCDB,
  168. sizeof(usb_stor_sense_invalidCDB));
  169. end:
  170. memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
  171. if (srb->cmnd[0] == ATA_12)
  172. srb->cmd_len = 12;
  173. }