cypress_atacb.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Support for emulating SAT (ata pass through) on devices based
  4. * on the Cypress USB/ATA bridge supporting ATACB.
  5. *
  6. * Copyright (c) 2008 Matthieu Castet (castet.matthieu@free.fr)
  7. */
  8. #include <linux/module.h>
  9. #include <scsi/scsi.h>
  10. #include <scsi/scsi_cmnd.h>
  11. #include <scsi/scsi_eh.h>
  12. #include <linux/ata.h>
  13. #include "usb.h"
  14. #include "protocol.h"
  15. #include "scsiglue.h"
  16. #include "debug.h"
  17. #define DRV_NAME "ums-cypress"
  18. MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
  19. MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
  20. MODULE_LICENSE("GPL");
  21. /*
  22. * The table of devices
  23. */
  24. #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
  25. vendorName, productName, useProtocol, useTransport, \
  26. initFunction, flags) \
  27. { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
  28. .driver_info = (flags) }
  29. static struct usb_device_id cypress_usb_ids[] = {
  30. # include "unusual_cypress.h"
  31. { } /* Terminating entry */
  32. };
  33. MODULE_DEVICE_TABLE(usb, cypress_usb_ids);
  34. #undef UNUSUAL_DEV
  35. /*
  36. * The flags table
  37. */
  38. #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
  39. vendor_name, product_name, use_protocol, use_transport, \
  40. init_function, Flags) \
  41. { \
  42. .vendorName = vendor_name, \
  43. .productName = product_name, \
  44. .useProtocol = use_protocol, \
  45. .useTransport = use_transport, \
  46. .initFunction = init_function, \
  47. }
  48. static struct us_unusual_dev cypress_unusual_dev_list[] = {
  49. # include "unusual_cypress.h"
  50. { } /* Terminating entry */
  51. };
  52. #undef UNUSUAL_DEV
  53. /*
  54. * ATACB is a protocol used on cypress usb<->ata bridge to
  55. * send raw ATA command over mass storage
  56. * There is a ATACB2 protocol that support LBA48 on newer chip.
  57. * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
  58. * datasheet from cypress.com.
  59. */
  60. static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
  61. {
  62. unsigned char save_cmnd[MAX_COMMAND_SIZE];
  63. if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
  64. usb_stor_transparent_scsi_command(srb, us);
  65. return;
  66. }
  67. memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
  68. memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
  69. /* check if we support the command */
  70. if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
  71. goto invalid_fld;
  72. /* check protocol */
  73. switch ((save_cmnd[1] >> 1) & 0xf) {
  74. case 3: /*no DATA */
  75. case 4: /* PIO in */
  76. case 5: /* PIO out */
  77. break;
  78. default:
  79. goto invalid_fld;
  80. }
  81. /* first build the ATACB command */
  82. srb->cmd_len = 16;
  83. srb->cmnd[0] = 0x24; /*
  84. * bVSCBSignature : vendor-specific command
  85. * this value can change, but most(all ?) manufacturers
  86. * keep the cypress default : 0x24
  87. */
  88. srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
  89. srb->cmnd[3] = 0xff - 1; /*
  90. * features, sector count, lba low, lba med
  91. * lba high, device, command are valid
  92. */
  93. srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
  94. if (save_cmnd[0] == ATA_16) {
  95. srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
  96. srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
  97. srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
  98. srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
  99. srb->cmnd[10] = save_cmnd[12]; /* lba high */
  100. srb->cmnd[11] = save_cmnd[13]; /* device */
  101. srb->cmnd[12] = save_cmnd[14]; /* command */
  102. if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
  103. /* this could be supported by atacb2 */
  104. if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
  105. || save_cmnd[11])
  106. goto invalid_fld;
  107. }
  108. } else { /* ATA12 */
  109. srb->cmnd[ 6] = save_cmnd[3]; /* features */
  110. srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
  111. srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
  112. srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
  113. srb->cmnd[10] = save_cmnd[7]; /* lba high */
  114. srb->cmnd[11] = save_cmnd[8]; /* device */
  115. srb->cmnd[12] = save_cmnd[9]; /* command */
  116. }
  117. /* Filter SET_FEATURES - XFER MODE command */
  118. if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
  119. && (srb->cmnd[6] == SETFEATURES_XFER))
  120. goto invalid_fld;
  121. if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
  122. srb->cmnd[2] |= (1<<7); /* set IdentifyPacketDevice for these cmds */
  123. usb_stor_transparent_scsi_command(srb, us);
  124. /* if the device doesn't support ATACB */
  125. if (srb->result == SAM_STAT_CHECK_CONDITION &&
  126. memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
  127. sizeof(usb_stor_sense_invalidCDB)) == 0) {
  128. usb_stor_dbg(us, "cypress atacb not supported ???\n");
  129. goto end;
  130. }
  131. /*
  132. * if ck_cond flags is set, and there wasn't critical error,
  133. * build the special sense
  134. */
  135. if ((srb->result != (DID_ERROR << 16) &&
  136. srb->result != (DID_ABORT << 16)) &&
  137. save_cmnd[2] & 0x20) {
  138. struct scsi_eh_save ses;
  139. unsigned char regs[8];
  140. unsigned char *sb = srb->sense_buffer;
  141. unsigned char *desc = sb + 8;
  142. int tmp_result;
  143. /* build the command for reading the ATA registers */
  144. scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
  145. /*
  146. * we use the same command as before, but we set
  147. * the read taskfile bit, for not executing atacb command,
  148. * but reading register selected in srb->cmnd[4]
  149. */
  150. srb->cmd_len = 16;
  151. srb->cmnd = ses.cmnd;
  152. srb->cmnd[2] = 1;
  153. usb_stor_transparent_scsi_command(srb, us);
  154. memcpy(regs, srb->sense_buffer, sizeof(regs));
  155. tmp_result = srb->result;
  156. scsi_eh_restore_cmnd(srb, &ses);
  157. /* we fail to get registers, report invalid command */
  158. if (tmp_result != SAM_STAT_GOOD)
  159. goto invalid_fld;
  160. /* build the sense */
  161. memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
  162. /* set sk, asc for a good command */
  163. sb[1] = RECOVERED_ERROR;
  164. sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
  165. sb[3] = 0x1D;
  166. /*
  167. * XXX we should generate sk, asc, ascq from status and error
  168. * regs
  169. * (see 11.1 Error translation ATA device error to SCSI error
  170. * map, and ata_to_sense_error from libata.)
  171. */
  172. /* Sense data is current and format is descriptor. */
  173. sb[0] = 0x72;
  174. desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
  175. /* set length of additional sense data */
  176. sb[7] = 14;
  177. desc[1] = 12;
  178. /* Copy registers into sense buffer. */
  179. desc[ 2] = 0x00;
  180. desc[ 3] = regs[1]; /* features */
  181. desc[ 5] = regs[2]; /* sector count */
  182. desc[ 7] = regs[3]; /* lba low */
  183. desc[ 9] = regs[4]; /* lba med */
  184. desc[11] = regs[5]; /* lba high */
  185. desc[12] = regs[6]; /* device */
  186. desc[13] = regs[7]; /* command */
  187. srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  188. }
  189. goto end;
  190. invalid_fld:
  191. srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
  192. memcpy(srb->sense_buffer,
  193. usb_stor_sense_invalidCDB,
  194. sizeof(usb_stor_sense_invalidCDB));
  195. end:
  196. memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
  197. if (srb->cmnd[0] == ATA_12)
  198. srb->cmd_len = 12;
  199. }
  200. static struct scsi_host_template cypress_host_template;
  201. static int cypress_probe(struct usb_interface *intf,
  202. const struct usb_device_id *id)
  203. {
  204. struct us_data *us;
  205. int result;
  206. struct usb_device *device;
  207. result = usb_stor_probe1(&us, intf, id,
  208. (id - cypress_usb_ids) + cypress_unusual_dev_list,
  209. &cypress_host_template);
  210. if (result)
  211. return result;
  212. /*
  213. * Among CY7C68300 chips, the A revision does not support Cypress ATACB
  214. * Filter out this revision from EEPROM default descriptor values
  215. */
  216. device = interface_to_usbdev(intf);
  217. if (device->descriptor.iManufacturer != 0x38 ||
  218. device->descriptor.iProduct != 0x4e ||
  219. device->descriptor.iSerialNumber != 0x64) {
  220. us->protocol_name = "Transparent SCSI with Cypress ATACB";
  221. us->proto_handler = cypress_atacb_passthrough;
  222. } else {
  223. us->protocol_name = "Transparent SCSI";
  224. us->proto_handler = usb_stor_transparent_scsi_command;
  225. }
  226. result = usb_stor_probe2(us);
  227. return result;
  228. }
  229. static struct usb_driver cypress_driver = {
  230. .name = DRV_NAME,
  231. .probe = cypress_probe,
  232. .disconnect = usb_stor_disconnect,
  233. .suspend = usb_stor_suspend,
  234. .resume = usb_stor_resume,
  235. .reset_resume = usb_stor_reset_resume,
  236. .pre_reset = usb_stor_pre_reset,
  237. .post_reset = usb_stor_post_reset,
  238. .id_table = cypress_usb_ids,
  239. .soft_unbind = 1,
  240. .no_dynamic_id = 1,
  241. };
  242. module_usb_stor_driver(cypress_driver, cypress_host_template, DRV_NAME);