cypress_atacb.c 8.1 KB

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