blob: 4825902377ebaeb8782f6c55b966c4f847bf136c [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0+
matthieu castetd2770642008-03-19 19:40:52 +01002/*
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)
matthieu castetd2770642008-03-19 19:40:52 +01007 */
8
Alan Sternfcdb5142009-02-12 14:48:04 -05009#include <linux/module.h>
matthieu castetd2770642008-03-19 19:40:52 +010010#include <scsi/scsi.h>
11#include <scsi/scsi_cmnd.h>
12#include <scsi/scsi_eh.h>
13#include <linux/ata.h>
14
15#include "usb.h"
16#include "protocol.h"
17#include "scsiglue.h"
18#include "debug.h"
19
Akinobu Mitaaa519be2015-05-06 18:24:21 +090020#define DRV_NAME "ums-cypress"
21
Maciej Grela4246b062009-02-28 12:39:20 -080022MODULE_DESCRIPTION("SAT support for Cypress USB/ATA bridges with ATACB");
23MODULE_AUTHOR("Matthieu Castet <castet.matthieu@free.fr>");
24MODULE_LICENSE("GPL");
Alan Sternfcdb5142009-02-12 14:48:04 -050025
26/*
27 * The table of devices
28 */
29#define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax, \
30 vendorName, productName, useProtocol, useTransport, \
31 initFunction, flags) \
32{ USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax), \
Sebastian Andrzej Siewiorf61870e2012-08-28 22:37:13 +020033 .driver_info = (flags) }
Alan Sternfcdb5142009-02-12 14:48:04 -050034
Felipe Balbi60e3cfb2011-11-15 09:53:29 +020035static struct usb_device_id cypress_usb_ids[] = {
Alan Sternfcdb5142009-02-12 14:48:04 -050036# include "unusual_cypress.h"
37 { } /* Terminating entry */
38};
39MODULE_DEVICE_TABLE(usb, cypress_usb_ids);
40
41#undef UNUSUAL_DEV
42
43/*
44 * The flags table
45 */
46#define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
47 vendor_name, product_name, use_protocol, use_transport, \
48 init_function, Flags) \
49{ \
50 .vendorName = vendor_name, \
51 .productName = product_name, \
52 .useProtocol = use_protocol, \
53 .useTransport = use_transport, \
54 .initFunction = init_function, \
55}
56
57static struct us_unusual_dev cypress_unusual_dev_list[] = {
58# include "unusual_cypress.h"
59 { } /* Terminating entry */
60};
61
62#undef UNUSUAL_DEV
63
64
matthieu castetd2770642008-03-19 19:40:52 +010065/*
66 * ATACB is a protocol used on cypress usb<->ata bridge to
67 * send raw ATA command over mass storage
68 * There is a ATACB2 protocol that support LBA48 on newer chip.
69 * More info that be found on cy7c68310_8.pdf and cy7c68300c_8.pdf
70 * datasheet from cypress.com.
71 */
Alan Sternfcdb5142009-02-12 14:48:04 -050072static void cypress_atacb_passthrough(struct scsi_cmnd *srb, struct us_data *us)
matthieu castetd2770642008-03-19 19:40:52 +010073{
74 unsigned char save_cmnd[MAX_COMMAND_SIZE];
75
76 if (likely(srb->cmnd[0] != ATA_16 && srb->cmnd[0] != ATA_12)) {
77 usb_stor_transparent_scsi_command(srb, us);
78 return;
79 }
80
81 memcpy(save_cmnd, srb->cmnd, sizeof(save_cmnd));
Boaz Harrosh64a87b22008-04-30 11:19:47 +030082 memset(srb->cmnd, 0, MAX_COMMAND_SIZE);
matthieu castetd2770642008-03-19 19:40:52 +010083
84 /* check if we support the command */
85 if (save_cmnd[1] >> 5) /* MULTIPLE_COUNT */
86 goto invalid_fld;
87 /* check protocol */
Bas Peterse9c58592015-02-07 23:42:43 +010088 switch ((save_cmnd[1] >> 1) & 0xf) {
89 case 3: /*no DATA */
90 case 4: /* PIO in */
91 case 5: /* PIO out */
92 break;
93 default:
94 goto invalid_fld;
matthieu castetd2770642008-03-19 19:40:52 +010095 }
96
97 /* first build the ATACB command */
98 srb->cmd_len = 16;
99
Felipe Balbif0183a32016-04-18 13:09:11 +0300100 srb->cmnd[0] = 0x24; /*
101 * bVSCBSignature : vendor-specific command
102 * this value can change, but most(all ?) manufacturers
103 * keep the cypress default : 0x24
104 */
matthieu castetd2770642008-03-19 19:40:52 +0100105 srb->cmnd[1] = 0x24; /* bVSCBSubCommand : 0x24 for ATACB */
106
Felipe Balbif0183a32016-04-18 13:09:11 +0300107 srb->cmnd[3] = 0xff - 1; /*
108 * features, sector count, lba low, lba med
109 * lba high, device, command are valid
110 */
matthieu castetd2770642008-03-19 19:40:52 +0100111 srb->cmnd[4] = 1; /* TransferBlockCount : 512 */
112
113 if (save_cmnd[0] == ATA_16) {
114 srb->cmnd[ 6] = save_cmnd[ 4]; /* features */
115 srb->cmnd[ 7] = save_cmnd[ 6]; /* sector count */
116 srb->cmnd[ 8] = save_cmnd[ 8]; /* lba low */
117 srb->cmnd[ 9] = save_cmnd[10]; /* lba med */
118 srb->cmnd[10] = save_cmnd[12]; /* lba high */
119 srb->cmnd[11] = save_cmnd[13]; /* device */
120 srb->cmnd[12] = save_cmnd[14]; /* command */
121
122 if (save_cmnd[1] & 0x01) {/* extended bit set for LBA48 */
123 /* this could be supported by atacb2 */
124 if (save_cmnd[3] || save_cmnd[5] || save_cmnd[7] || save_cmnd[9]
125 || save_cmnd[11])
126 goto invalid_fld;
127 }
Bas Peterse9c58592015-02-07 23:42:43 +0100128 } else { /* ATA12 */
matthieu castetd2770642008-03-19 19:40:52 +0100129 srb->cmnd[ 6] = save_cmnd[3]; /* features */
130 srb->cmnd[ 7] = save_cmnd[4]; /* sector count */
131 srb->cmnd[ 8] = save_cmnd[5]; /* lba low */
132 srb->cmnd[ 9] = save_cmnd[6]; /* lba med */
133 srb->cmnd[10] = save_cmnd[7]; /* lba high */
134 srb->cmnd[11] = save_cmnd[8]; /* device */
135 srb->cmnd[12] = save_cmnd[9]; /* command */
136
137 }
138 /* Filter SET_FEATURES - XFER MODE command */
139 if ((srb->cmnd[12] == ATA_CMD_SET_FEATURES)
140 && (srb->cmnd[6] == SETFEATURES_XFER))
141 goto invalid_fld;
142
143 if (srb->cmnd[12] == ATA_CMD_ID_ATA || srb->cmnd[12] == ATA_CMD_ID_ATAPI)
144 srb->cmnd[2] |= (1<<7); /* set IdentifyPacketDevice for these cmds */
145
146
147 usb_stor_transparent_scsi_command(srb, us);
148
Felipe Balbif0183a32016-04-18 13:09:11 +0300149 /* if the device doesn't support ATACB */
matthieu castetd2770642008-03-19 19:40:52 +0100150 if (srb->result == SAM_STAT_CHECK_CONDITION &&
151 memcmp(srb->sense_buffer, usb_stor_sense_invalidCDB,
152 sizeof(usb_stor_sense_invalidCDB)) == 0) {
Joe Perches191648d2013-04-19 11:44:00 -0700153 usb_stor_dbg(us, "cypress atacb not supported ???\n");
matthieu castetd2770642008-03-19 19:40:52 +0100154 goto end;
155 }
156
Felipe Balbif0183a32016-04-18 13:09:11 +0300157 /*
158 * if ck_cond flags is set, and there wasn't critical error,
matthieu castetd2770642008-03-19 19:40:52 +0100159 * build the special sense
160 */
161 if ((srb->result != (DID_ERROR << 16) &&
162 srb->result != (DID_ABORT << 16)) &&
163 save_cmnd[2] & 0x20) {
164 struct scsi_eh_save ses;
165 unsigned char regs[8];
166 unsigned char *sb = srb->sense_buffer;
167 unsigned char *desc = sb + 8;
168 int tmp_result;
169
Felipe Balbif0183a32016-04-18 13:09:11 +0300170 /* build the command for reading the ATA registers */
Boaz Harrosh1f4159c2009-02-11 09:54:31 +0200171 scsi_eh_prep_cmnd(srb, &ses, NULL, 0, sizeof(regs));
172
Felipe Balbif0183a32016-04-18 13:09:11 +0300173 /*
174 * we use the same command as before, but we set
matthieu castetd2770642008-03-19 19:40:52 +0100175 * the read taskfile bit, for not executing atacb command,
176 * but reading register selected in srb->cmnd[4]
177 */
Boaz Harrosh1f4159c2009-02-11 09:54:31 +0200178 srb->cmd_len = 16;
179 srb->cmnd = ses.cmnd;
matthieu castetd2770642008-03-19 19:40:52 +0100180 srb->cmnd[2] = 1;
181
182 usb_stor_transparent_scsi_command(srb, us);
Boaz Harrosh1f4159c2009-02-11 09:54:31 +0200183 memcpy(regs, srb->sense_buffer, sizeof(regs));
matthieu castetd2770642008-03-19 19:40:52 +0100184 tmp_result = srb->result;
185 scsi_eh_restore_cmnd(srb, &ses);
186 /* we fail to get registers, report invalid command */
187 if (tmp_result != SAM_STAT_GOOD)
188 goto invalid_fld;
189
190 /* build the sense */
191 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
192
193 /* set sk, asc for a good command */
194 sb[1] = RECOVERED_ERROR;
195 sb[2] = 0; /* ATA PASS THROUGH INFORMATION AVAILABLE */
196 sb[3] = 0x1D;
197
Felipe Balbif0183a32016-04-18 13:09:11 +0300198 /*
199 * XXX we should generate sk, asc, ascq from status and error
matthieu castetd2770642008-03-19 19:40:52 +0100200 * regs
Boaz Harrosh1f4159c2009-02-11 09:54:31 +0200201 * (see 11.1 Error translation ATA device error to SCSI error
Felipe Balbif0183a32016-04-18 13:09:11 +0300202 * map, and ata_to_sense_error from libata.)
matthieu castetd2770642008-03-19 19:40:52 +0100203 */
204
205 /* Sense data is current and format is descriptor. */
206 sb[0] = 0x72;
207 desc[0] = 0x09; /* ATA_RETURN_DESCRIPTOR */
208
209 /* set length of additional sense data */
210 sb[7] = 14;
211 desc[1] = 12;
212
213 /* Copy registers into sense buffer. */
214 desc[ 2] = 0x00;
215 desc[ 3] = regs[1]; /* features */
216 desc[ 5] = regs[2]; /* sector count */
217 desc[ 7] = regs[3]; /* lba low */
218 desc[ 9] = regs[4]; /* lba med */
219 desc[11] = regs[5]; /* lba high */
220 desc[12] = regs[6]; /* device */
221 desc[13] = regs[7]; /* command */
222
223 srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
224 }
225 goto end;
226invalid_fld:
227 srb->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
228
229 memcpy(srb->sense_buffer,
230 usb_stor_sense_invalidCDB,
231 sizeof(usb_stor_sense_invalidCDB));
232end:
233 memcpy(srb->cmnd, save_cmnd, sizeof(save_cmnd));
234 if (srb->cmnd[0] == ATA_12)
235 srb->cmd_len = 12;
236}
Alan Sternfcdb5142009-02-12 14:48:04 -0500237
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900238static struct scsi_host_template cypress_host_template;
Alan Sternfcdb5142009-02-12 14:48:04 -0500239
240static int cypress_probe(struct usb_interface *intf,
241 const struct usb_device_id *id)
242{
243 struct us_data *us;
244 int result;
Tormod Volden671b4b22013-04-20 14:24:04 +0200245 struct usb_device *device;
Alan Sternfcdb5142009-02-12 14:48:04 -0500246
247 result = usb_stor_probe1(&us, intf, id,
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900248 (id - cypress_usb_ids) + cypress_unusual_dev_list,
249 &cypress_host_template);
Alan Sternfcdb5142009-02-12 14:48:04 -0500250 if (result)
251 return result;
252
Felipe Balbif0183a32016-04-18 13:09:11 +0300253 /*
254 * Among CY7C68300 chips, the A revision does not support Cypress ATACB
Tormod Volden671b4b22013-04-20 14:24:04 +0200255 * Filter out this revision from EEPROM default descriptor values
256 */
257 device = interface_to_usbdev(intf);
258 if (device->descriptor.iManufacturer != 0x38 ||
259 device->descriptor.iProduct != 0x4e ||
260 device->descriptor.iSerialNumber != 0x64) {
261 us->protocol_name = "Transparent SCSI with Cypress ATACB";
262 us->proto_handler = cypress_atacb_passthrough;
263 } else {
264 us->protocol_name = "Transparent SCSI";
265 us->proto_handler = usb_stor_transparent_scsi_command;
266 }
Alan Sternfcdb5142009-02-12 14:48:04 -0500267
268 result = usb_stor_probe2(us);
269 return result;
270}
271
272static struct usb_driver cypress_driver = {
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900273 .name = DRV_NAME,
Alan Sternfcdb5142009-02-12 14:48:04 -0500274 .probe = cypress_probe,
275 .disconnect = usb_stor_disconnect,
276 .suspend = usb_stor_suspend,
277 .resume = usb_stor_resume,
278 .reset_resume = usb_stor_reset_resume,
279 .pre_reset = usb_stor_pre_reset,
280 .post_reset = usb_stor_post_reset,
281 .id_table = cypress_usb_ids,
282 .soft_unbind = 1,
Huajun Lie73b2db2012-01-14 10:15:21 +0800283 .no_dynamic_id = 1,
Alan Sternfcdb5142009-02-12 14:48:04 -0500284};
285
Akinobu Mitaaa519be2015-05-06 18:24:21 +0900286module_usb_stor_driver(cypress_driver, cypress_host_template, DRV_NAME);