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