]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
af36d7f0 JG |
2 | * libata-scsi.c - helper library for ATA |
3 | * | |
4 | * Maintained by: Jeff Garzik <jgarzik@pobox.com> | |
5 | * Please ALWAYS copy linux-ide@vger.kernel.org | |
6 | * on emails. | |
7 | * | |
8 | * Copyright 2003-2004 Red Hat, Inc. All rights reserved. | |
9 | * Copyright 2003-2004 Jeff Garzik | |
10 | * | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2, or (at your option) | |
15 | * any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; see the file COPYING. If not, write to | |
24 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | * | |
26 | * | |
27 | * libata documentation is available via 'make {ps|pdf}docs', | |
28 | * as Documentation/DocBook/libata.* | |
29 | * | |
30 | * Hardware documentation available from | |
31 | * - http://www.t10.org/ | |
32 | * - http://www.t13.org/ | |
33 | * | |
1da177e4 LT |
34 | */ |
35 | ||
5a0e3ad6 | 36 | #include <linux/slab.h> |
1da177e4 LT |
37 | #include <linux/kernel.h> |
38 | #include <linux/blkdev.h> | |
39 | #include <linux/spinlock.h> | |
40 | #include <scsi/scsi.h> | |
1da177e4 | 41 | #include <scsi/scsi_host.h> |
beb40487 | 42 | #include <scsi/scsi_cmnd.h> |
85837ebd | 43 | #include <scsi/scsi_eh.h> |
005a5a06 | 44 | #include <scsi/scsi_device.h> |
a6e6ce8e | 45 | #include <scsi/scsi_tcq.h> |
30afc84c | 46 | #include <scsi/scsi_transport.h> |
1da177e4 | 47 | #include <linux/libata.h> |
b095518e | 48 | #include <linux/hdreg.h> |
2dcb407e | 49 | #include <linux/uaccess.h> |
2a6e58d2 | 50 | #include <linux/suspend.h> |
18f0f978 | 51 | #include <asm/unaligned.h> |
1da177e4 LT |
52 | |
53 | #include "libata.h" | |
d9027470 | 54 | #include "libata-transport.h" |
1da177e4 | 55 | |
87340e98 TH |
56 | #define ATA_SCSI_RBUF_SIZE 4096 |
57 | ||
58 | static DEFINE_SPINLOCK(ata_scsi_rbuf_lock); | |
59 | static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE]; | |
b095518e | 60 | |
ad706991 | 61 | typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc); |
ab5b3a5b | 62 | |
2dcb407e | 63 | static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap, |
ab5b3a5b | 64 | const struct scsi_device *scsidev); |
2dcb407e | 65 | static struct ata_device *ata_scsi_find_dev(struct ata_port *ap, |
ab5b3a5b | 66 | const struct scsi_device *scsidev); |
1da177e4 | 67 | |
00ac37f5 DG |
68 | #define RW_RECOVERY_MPAGE 0x1 |
69 | #define RW_RECOVERY_MPAGE_LEN 12 | |
70 | #define CACHE_MPAGE 0x8 | |
71 | #define CACHE_MPAGE_LEN 20 | |
72 | #define CONTROL_MPAGE 0xa | |
73 | #define CONTROL_MPAGE_LEN 12 | |
74 | #define ALL_MPAGES 0x3f | |
75 | #define ALL_SUB_MPAGES 0xff | |
76 | ||
77 | ||
24f75686 | 78 | static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = { |
00ac37f5 DG |
79 | RW_RECOVERY_MPAGE, |
80 | RW_RECOVERY_MPAGE_LEN - 2, | |
24f75686 | 81 | (1 << 7), /* AWRE */ |
00ac37f5 DG |
82 | 0, /* read retry count */ |
83 | 0, 0, 0, 0, | |
84 | 0, /* write retry count */ | |
85 | 0, 0, 0 | |
86 | }; | |
87 | ||
88 | static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = { | |
89 | CACHE_MPAGE, | |
90 | CACHE_MPAGE_LEN - 2, | |
91 | 0, /* contains WCE, needs to be 0 for logic */ | |
92 | 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
93 | 0, /* contains DRA, needs to be 0 for logic */ | |
94 | 0, 0, 0, 0, 0, 0, 0 | |
95 | }; | |
96 | ||
97 | static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = { | |
98 | CONTROL_MPAGE, | |
99 | CONTROL_MPAGE_LEN - 2, | |
100 | 2, /* DSENSE=0, GLTSD=1 */ | |
101 | 0, /* [QAM+QERR may be 1, see 05-359r1] */ | |
102 | 0, 0, 0, 0, 0xff, 0xff, | |
103 | 0, 30 /* extended self test time, see 05-359r1 */ | |
104 | }; | |
105 | ||
c93b263e TH |
106 | static const char *ata_lpm_policy_names[] = { |
107 | [ATA_LPM_UNKNOWN] = "max_performance", | |
108 | [ATA_LPM_MAX_POWER] = "max_performance", | |
109 | [ATA_LPM_MED_POWER] = "medium_power", | |
110 | [ATA_LPM_MIN_POWER] = "min_power", | |
ca77329f KCA |
111 | }; |
112 | ||
c93b263e TH |
113 | static ssize_t ata_scsi_lpm_store(struct device *dev, |
114 | struct device_attribute *attr, | |
115 | const char *buf, size_t count) | |
ca77329f | 116 | { |
ee959b00 | 117 | struct Scsi_Host *shost = class_to_shost(dev); |
ca77329f | 118 | struct ata_port *ap = ata_shost_to_port(shost); |
c93b263e TH |
119 | enum ata_lpm_policy policy; |
120 | ||
121 | /* UNKNOWN is internal state, iterate from MAX_POWER */ | |
122 | for (policy = ATA_LPM_MAX_POWER; | |
123 | policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) { | |
124 | const char *name = ata_lpm_policy_names[policy]; | |
125 | ||
126 | if (strncmp(name, buf, strlen(name)) == 0) | |
ca77329f | 127 | break; |
ca77329f | 128 | } |
c93b263e | 129 | if (policy == ARRAY_SIZE(ata_lpm_policy_names)) |
ca77329f KCA |
130 | return -EINVAL; |
131 | ||
132 | ata_lpm_schedule(ap, policy); | |
133 | return count; | |
134 | } | |
135 | ||
c93b263e TH |
136 | static ssize_t ata_scsi_lpm_show(struct device *dev, |
137 | struct device_attribute *attr, char *buf) | |
ca77329f | 138 | { |
ee959b00 | 139 | struct Scsi_Host *shost = class_to_shost(dev); |
ca77329f | 140 | struct ata_port *ap = ata_shost_to_port(shost); |
ca77329f | 141 | |
c93b263e | 142 | if (ap->lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names)) |
ca77329f KCA |
143 | return -EINVAL; |
144 | ||
c93b263e TH |
145 | return snprintf(buf, PAGE_SIZE, "%s\n", |
146 | ata_lpm_policy_names[ap->lpm_policy]); | |
ca77329f | 147 | } |
ee959b00 | 148 | DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR, |
c93b263e | 149 | ata_scsi_lpm_show, ata_scsi_lpm_store); |
ee959b00 | 150 | EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy); |
ca77329f | 151 | |
45fabbb7 EO |
152 | static ssize_t ata_scsi_park_show(struct device *device, |
153 | struct device_attribute *attr, char *buf) | |
154 | { | |
155 | struct scsi_device *sdev = to_scsi_device(device); | |
156 | struct ata_port *ap; | |
157 | struct ata_link *link; | |
158 | struct ata_device *dev; | |
a464189d | 159 | unsigned long flags, now; |
45fabbb7 EO |
160 | unsigned int uninitialized_var(msecs); |
161 | int rc = 0; | |
162 | ||
163 | ap = ata_shost_to_port(sdev->host); | |
164 | ||
165 | spin_lock_irqsave(ap->lock, flags); | |
166 | dev = ata_scsi_find_dev(ap, sdev); | |
167 | if (!dev) { | |
168 | rc = -ENODEV; | |
169 | goto unlock; | |
170 | } | |
171 | if (dev->flags & ATA_DFLAG_NO_UNLOAD) { | |
172 | rc = -EOPNOTSUPP; | |
173 | goto unlock; | |
174 | } | |
175 | ||
176 | link = dev->link; | |
a464189d | 177 | now = jiffies; |
45fabbb7 EO |
178 | if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS && |
179 | link->eh_context.unloaded_mask & (1 << dev->devno) && | |
a464189d EO |
180 | time_after(dev->unpark_deadline, now)) |
181 | msecs = jiffies_to_msecs(dev->unpark_deadline - now); | |
45fabbb7 EO |
182 | else |
183 | msecs = 0; | |
184 | ||
185 | unlock: | |
186 | spin_unlock_irq(ap->lock); | |
187 | ||
188 | return rc ? rc : snprintf(buf, 20, "%u\n", msecs); | |
189 | } | |
190 | ||
191 | static ssize_t ata_scsi_park_store(struct device *device, | |
192 | struct device_attribute *attr, | |
193 | const char *buf, size_t len) | |
194 | { | |
195 | struct scsi_device *sdev = to_scsi_device(device); | |
196 | struct ata_port *ap; | |
197 | struct ata_device *dev; | |
198 | long int input; | |
199 | unsigned long flags; | |
200 | int rc; | |
201 | ||
202 | rc = strict_strtol(buf, 10, &input); | |
203 | if (rc || input < -2) | |
204 | return -EINVAL; | |
205 | if (input > ATA_TMOUT_MAX_PARK) { | |
206 | rc = -EOVERFLOW; | |
207 | input = ATA_TMOUT_MAX_PARK; | |
208 | } | |
209 | ||
210 | ap = ata_shost_to_port(sdev->host); | |
211 | ||
212 | spin_lock_irqsave(ap->lock, flags); | |
213 | dev = ata_scsi_find_dev(ap, sdev); | |
214 | if (unlikely(!dev)) { | |
215 | rc = -ENODEV; | |
216 | goto unlock; | |
217 | } | |
218 | if (dev->class != ATA_DEV_ATA) { | |
219 | rc = -EOPNOTSUPP; | |
220 | goto unlock; | |
221 | } | |
222 | ||
223 | if (input >= 0) { | |
224 | if (dev->flags & ATA_DFLAG_NO_UNLOAD) { | |
225 | rc = -EOPNOTSUPP; | |
226 | goto unlock; | |
227 | } | |
228 | ||
229 | dev->unpark_deadline = ata_deadline(jiffies, input); | |
230 | dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK; | |
231 | ata_port_schedule_eh(ap); | |
232 | complete(&ap->park_req_pending); | |
233 | } else { | |
234 | switch (input) { | |
235 | case -1: | |
236 | dev->flags &= ~ATA_DFLAG_NO_UNLOAD; | |
237 | break; | |
238 | case -2: | |
239 | dev->flags |= ATA_DFLAG_NO_UNLOAD; | |
240 | break; | |
241 | } | |
242 | } | |
243 | unlock: | |
244 | spin_unlock_irqrestore(ap->lock, flags); | |
245 | ||
246 | return rc ? rc : len; | |
247 | } | |
248 | DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR, | |
249 | ata_scsi_park_show, ata_scsi_park_store); | |
250 | EXPORT_SYMBOL_GPL(dev_attr_unload_heads); | |
251 | ||
f0761be3 TH |
252 | static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq) |
253 | { | |
254 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; | |
255 | ||
256 | scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq); | |
257 | } | |
258 | ||
18f7ba4c KCA |
259 | static ssize_t |
260 | ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr, | |
261 | const char *buf, size_t count) | |
262 | { | |
263 | struct Scsi_Host *shost = class_to_shost(dev); | |
264 | struct ata_port *ap = ata_shost_to_port(shost); | |
265 | if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM)) | |
266 | return ap->ops->em_store(ap, buf, count); | |
267 | return -EINVAL; | |
268 | } | |
269 | ||
270 | static ssize_t | |
271 | ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr, | |
272 | char *buf) | |
273 | { | |
274 | struct Scsi_Host *shost = class_to_shost(dev); | |
275 | struct ata_port *ap = ata_shost_to_port(shost); | |
276 | ||
277 | if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM)) | |
278 | return ap->ops->em_show(ap, buf); | |
279 | return -EINVAL; | |
280 | } | |
ea7a5ed5 | 281 | DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR, |
18f7ba4c KCA |
282 | ata_scsi_em_message_show, ata_scsi_em_message_store); |
283 | EXPORT_SYMBOL_GPL(dev_attr_em_message); | |
284 | ||
285 | static ssize_t | |
286 | ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr, | |
287 | char *buf) | |
288 | { | |
289 | struct Scsi_Host *shost = class_to_shost(dev); | |
290 | struct ata_port *ap = ata_shost_to_port(shost); | |
291 | ||
292 | return snprintf(buf, 23, "%d\n", ap->em_message_type); | |
293 | } | |
294 | DEVICE_ATTR(em_message_type, S_IRUGO, | |
295 | ata_scsi_em_message_type_show, NULL); | |
296 | EXPORT_SYMBOL_GPL(dev_attr_em_message_type); | |
297 | ||
298 | static ssize_t | |
299 | ata_scsi_activity_show(struct device *dev, struct device_attribute *attr, | |
300 | char *buf) | |
301 | { | |
302 | struct scsi_device *sdev = to_scsi_device(dev); | |
303 | struct ata_port *ap = ata_shost_to_port(sdev->host); | |
304 | struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); | |
305 | ||
306 | if (ap->ops->sw_activity_show && (ap->flags & ATA_FLAG_SW_ACTIVITY)) | |
307 | return ap->ops->sw_activity_show(atadev, buf); | |
308 | return -EINVAL; | |
309 | } | |
310 | ||
311 | static ssize_t | |
312 | ata_scsi_activity_store(struct device *dev, struct device_attribute *attr, | |
313 | const char *buf, size_t count) | |
314 | { | |
315 | struct scsi_device *sdev = to_scsi_device(dev); | |
316 | struct ata_port *ap = ata_shost_to_port(sdev->host); | |
317 | struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); | |
318 | enum sw_activity val; | |
319 | int rc; | |
320 | ||
321 | if (ap->ops->sw_activity_store && (ap->flags & ATA_FLAG_SW_ACTIVITY)) { | |
322 | val = simple_strtoul(buf, NULL, 0); | |
323 | switch (val) { | |
324 | case OFF: case BLINK_ON: case BLINK_OFF: | |
325 | rc = ap->ops->sw_activity_store(atadev, val); | |
326 | if (!rc) | |
327 | return count; | |
328 | else | |
329 | return rc; | |
330 | } | |
331 | } | |
332 | return -EINVAL; | |
333 | } | |
ea7a5ed5 | 334 | DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show, |
18f7ba4c KCA |
335 | ata_scsi_activity_store); |
336 | EXPORT_SYMBOL_GPL(dev_attr_sw_activity); | |
337 | ||
45fabbb7 EO |
338 | struct device_attribute *ata_common_sdev_attrs[] = { |
339 | &dev_attr_unload_heads, | |
340 | NULL | |
341 | }; | |
342 | EXPORT_SYMBOL_GPL(ata_common_sdev_attrs); | |
343 | ||
ae006510 DG |
344 | static void ata_scsi_invalid_field(struct scsi_cmnd *cmd, |
345 | void (*done)(struct scsi_cmnd *)) | |
346 | { | |
347 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0); | |
348 | /* "Invalid field in cbd" */ | |
349 | done(cmd); | |
350 | } | |
351 | ||
1da177e4 LT |
352 | /** |
353 | * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. | |
354 | * @sdev: SCSI device for which BIOS geometry is to be determined | |
355 | * @bdev: block device associated with @sdev | |
356 | * @capacity: capacity of SCSI device | |
357 | * @geom: location to which geometry will be output | |
358 | * | |
359 | * Generic bios head/sector/cylinder calculator | |
360 | * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS) | |
361 | * mapping. Some situations may arise where the disk is not | |
362 | * bootable if this is not used. | |
363 | * | |
364 | * LOCKING: | |
365 | * Defined by the SCSI layer. We don't really care. | |
366 | * | |
367 | * RETURNS: | |
368 | * Zero. | |
369 | */ | |
370 | int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev, | |
371 | sector_t capacity, int geom[]) | |
372 | { | |
373 | geom[0] = 255; | |
374 | geom[1] = 63; | |
375 | sector_div(capacity, 255*63); | |
376 | geom[2] = capacity; | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
d8d9129e TH |
381 | /** |
382 | * ata_scsi_unlock_native_capacity - unlock native capacity | |
383 | * @sdev: SCSI device to adjust device capacity for | |
384 | * | |
385 | * This function is called if a partition on @sdev extends beyond | |
386 | * the end of the device. It requests EH to unlock HPA. | |
387 | * | |
388 | * LOCKING: | |
389 | * Defined by the SCSI layer. Might sleep. | |
390 | */ | |
391 | void ata_scsi_unlock_native_capacity(struct scsi_device *sdev) | |
392 | { | |
393 | struct ata_port *ap = ata_shost_to_port(sdev->host); | |
394 | struct ata_device *dev; | |
395 | unsigned long flags; | |
396 | ||
397 | spin_lock_irqsave(ap->lock, flags); | |
398 | ||
399 | dev = ata_scsi_find_dev(ap, sdev); | |
400 | if (dev && dev->n_sectors < dev->n_native_sectors) { | |
401 | dev->flags |= ATA_DFLAG_UNLOCK_HPA; | |
402 | dev->link->eh_info.action |= ATA_EH_RESET; | |
403 | ata_port_schedule_eh(ap); | |
404 | } | |
405 | ||
406 | spin_unlock_irqrestore(ap->lock, flags); | |
407 | ata_port_wait_eh(ap); | |
408 | } | |
409 | ||
5924b74c TH |
410 | /** |
411 | * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl | |
5eb66fe0 | 412 | * @ap: target port |
5924b74c TH |
413 | * @sdev: SCSI device to get identify data for |
414 | * @arg: User buffer area for identify data | |
415 | * | |
416 | * LOCKING: | |
417 | * Defined by the SCSI layer. We don't really care. | |
418 | * | |
419 | * RETURNS: | |
420 | * Zero on success, negative errno on error. | |
421 | */ | |
94be9a58 JG |
422 | static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev, |
423 | void __user *arg) | |
5924b74c | 424 | { |
5924b74c TH |
425 | struct ata_device *dev = ata_scsi_find_dev(ap, sdev); |
426 | u16 __user *dst = arg; | |
427 | char buf[40]; | |
428 | ||
429 | if (!dev) | |
430 | return -ENOMSG; | |
431 | ||
432 | if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16))) | |
433 | return -EFAULT; | |
434 | ||
435 | ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN); | |
436 | if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN)) | |
437 | return -EFAULT; | |
438 | ||
439 | ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN); | |
440 | if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN)) | |
441 | return -EFAULT; | |
442 | ||
443 | ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN); | |
444 | if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN)) | |
445 | return -EFAULT; | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
b095518e JG |
450 | /** |
451 | * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl | |
8e8b77dd | 452 | * @scsidev: Device to which we are issuing command |
b095518e JG |
453 | * @arg: User provided data for issuing command |
454 | * | |
455 | * LOCKING: | |
456 | * Defined by the SCSI layer. We don't really care. | |
457 | * | |
458 | * RETURNS: | |
459 | * Zero on success, negative errno on error. | |
460 | */ | |
b095518e JG |
461 | int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg) |
462 | { | |
463 | int rc = 0; | |
464 | u8 scsi_cmd[MAX_COMMAND_SIZE]; | |
bbe1fe7e | 465 | u8 args[4], *argbuf = NULL, *sensebuf = NULL; |
b095518e | 466 | int argsize = 0; |
85837ebd | 467 | enum dma_data_direction data_dir; |
bbe1fe7e | 468 | int cmd_result; |
b095518e | 469 | |
c893a3ae | 470 | if (arg == NULL) |
b095518e JG |
471 | return -EINVAL; |
472 | ||
473 | if (copy_from_user(args, arg, sizeof(args))) | |
474 | return -EFAULT; | |
475 | ||
bbe1fe7e ET |
476 | sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO); |
477 | if (!sensebuf) | |
478 | return -ENOMEM; | |
479 | ||
b095518e JG |
480 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); |
481 | ||
482 | if (args[3]) { | |
295124dc | 483 | argsize = ATA_SECT_SIZE * args[3]; |
b095518e | 484 | argbuf = kmalloc(argsize, GFP_KERNEL); |
54dac83c JR |
485 | if (argbuf == NULL) { |
486 | rc = -ENOMEM; | |
487 | goto error; | |
488 | } | |
b095518e JG |
489 | |
490 | scsi_cmd[1] = (4 << 1); /* PIO Data-in */ | |
491 | scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev, | |
2dcb407e | 492 | block count in sector count field */ |
85837ebd | 493 | data_dir = DMA_FROM_DEVICE; |
b095518e JG |
494 | } else { |
495 | scsi_cmd[1] = (3 << 1); /* Non-data */ | |
bbe1fe7e | 496 | scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */ |
85837ebd | 497 | data_dir = DMA_NONE; |
b095518e JG |
498 | } |
499 | ||
500 | scsi_cmd[0] = ATA_16; | |
501 | ||
502 | scsi_cmd[4] = args[2]; | |
a4f19040 | 503 | if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */ |
b095518e JG |
504 | scsi_cmd[6] = args[3]; |
505 | scsi_cmd[8] = args[1]; | |
506 | scsi_cmd[10] = 0x4f; | |
507 | scsi_cmd[12] = 0xc2; | |
508 | } else { | |
509 | scsi_cmd[6] = args[1]; | |
510 | } | |
511 | scsi_cmd[14] = args[0]; | |
512 | ||
513 | /* Good values for timeout and retries? Values below | |
514 | from scsi_ioctl_send_command() for default case... */ | |
bbe1fe7e | 515 | cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize, |
f4f4e47e | 516 | sensebuf, (10*HZ), 5, 0, NULL); |
bbe1fe7e ET |
517 | |
518 | if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */ | |
519 | u8 *desc = sensebuf + 8; | |
520 | cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */ | |
521 | ||
522 | /* If we set cc then ATA pass-through will cause a | |
523 | * check condition even if no error. Filter that. */ | |
524 | if (cmd_result & SAM_STAT_CHECK_CONDITION) { | |
525 | struct scsi_sense_hdr sshdr; | |
526 | scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, | |
2dcb407e JG |
527 | &sshdr); |
528 | if (sshdr.sense_key == 0 && | |
529 | sshdr.asc == 0 && sshdr.ascq == 0) | |
bbe1fe7e ET |
530 | cmd_result &= ~SAM_STAT_CHECK_CONDITION; |
531 | } | |
532 | ||
533 | /* Send userspace a few ATA registers (same as drivers/ide) */ | |
2dcb407e JG |
534 | if (sensebuf[0] == 0x72 && /* format is "descriptor" */ |
535 | desc[0] == 0x09) { /* code is "ATA Descriptor" */ | |
536 | args[0] = desc[13]; /* status */ | |
537 | args[1] = desc[3]; /* error */ | |
538 | args[2] = desc[5]; /* sector count (0:7) */ | |
bbe1fe7e ET |
539 | if (copy_to_user(arg, args, sizeof(args))) |
540 | rc = -EFAULT; | |
541 | } | |
542 | } | |
543 | ||
544 | ||
545 | if (cmd_result) { | |
b095518e JG |
546 | rc = -EIO; |
547 | goto error; | |
548 | } | |
549 | ||
b095518e | 550 | if ((argbuf) |
c893a3ae | 551 | && copy_to_user(arg + sizeof(args), argbuf, argsize)) |
b095518e JG |
552 | rc = -EFAULT; |
553 | error: | |
bbe1fe7e | 554 | kfree(sensebuf); |
8f760780 | 555 | kfree(argbuf); |
b095518e JG |
556 | return rc; |
557 | } | |
558 | ||
559 | /** | |
560 | * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl | |
8e8b77dd | 561 | * @scsidev: Device to which we are issuing command |
b095518e JG |
562 | * @arg: User provided data for issuing command |
563 | * | |
564 | * LOCKING: | |
565 | * Defined by the SCSI layer. We don't really care. | |
566 | * | |
567 | * RETURNS: | |
568 | * Zero on success, negative errno on error. | |
569 | */ | |
570 | int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg) | |
571 | { | |
572 | int rc = 0; | |
573 | u8 scsi_cmd[MAX_COMMAND_SIZE]; | |
af068bd1 DM |
574 | u8 args[7], *sensebuf = NULL; |
575 | int cmd_result; | |
b095518e | 576 | |
c893a3ae | 577 | if (arg == NULL) |
b095518e JG |
578 | return -EINVAL; |
579 | ||
580 | if (copy_from_user(args, arg, sizeof(args))) | |
581 | return -EFAULT; | |
582 | ||
af068bd1 DM |
583 | sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO); |
584 | if (!sensebuf) | |
585 | return -ENOMEM; | |
586 | ||
b095518e JG |
587 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); |
588 | scsi_cmd[0] = ATA_16; | |
589 | scsi_cmd[1] = (3 << 1); /* Non-data */ | |
af068bd1 | 590 | scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */ |
b095518e JG |
591 | scsi_cmd[4] = args[1]; |
592 | scsi_cmd[6] = args[2]; | |
593 | scsi_cmd[8] = args[3]; | |
594 | scsi_cmd[10] = args[4]; | |
595 | scsi_cmd[12] = args[5]; | |
277239f2 | 596 | scsi_cmd[13] = args[6] & 0x4f; |
b095518e JG |
597 | scsi_cmd[14] = args[0]; |
598 | ||
b095518e | 599 | /* Good values for timeout and retries? Values below |
2e9edbf8 | 600 | from scsi_ioctl_send_command() for default case... */ |
af068bd1 | 601 | cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0, |
f4f4e47e | 602 | sensebuf, (10*HZ), 5, 0, NULL); |
af068bd1 DM |
603 | |
604 | if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */ | |
605 | u8 *desc = sensebuf + 8; | |
606 | cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */ | |
607 | ||
608 | /* If we set cc then ATA pass-through will cause a | |
609 | * check condition even if no error. Filter that. */ | |
610 | if (cmd_result & SAM_STAT_CHECK_CONDITION) { | |
611 | struct scsi_sense_hdr sshdr; | |
612 | scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, | |
613 | &sshdr); | |
2dcb407e JG |
614 | if (sshdr.sense_key == 0 && |
615 | sshdr.asc == 0 && sshdr.ascq == 0) | |
af068bd1 DM |
616 | cmd_result &= ~SAM_STAT_CHECK_CONDITION; |
617 | } | |
618 | ||
619 | /* Send userspace ATA registers */ | |
620 | if (sensebuf[0] == 0x72 && /* format is "descriptor" */ | |
621 | desc[0] == 0x09) {/* code is "ATA Descriptor" */ | |
622 | args[0] = desc[13]; /* status */ | |
623 | args[1] = desc[3]; /* error */ | |
624 | args[2] = desc[5]; /* sector count (0:7) */ | |
625 | args[3] = desc[7]; /* lbal */ | |
626 | args[4] = desc[9]; /* lbam */ | |
627 | args[5] = desc[11]; /* lbah */ | |
628 | args[6] = desc[12]; /* select */ | |
629 | if (copy_to_user(arg, args, sizeof(args))) | |
630 | rc = -EFAULT; | |
631 | } | |
632 | } | |
633 | ||
634 | if (cmd_result) { | |
b095518e | 635 | rc = -EIO; |
af068bd1 DM |
636 | goto error; |
637 | } | |
b095518e | 638 | |
af068bd1 DM |
639 | error: |
640 | kfree(sensebuf); | |
b095518e JG |
641 | return rc; |
642 | } | |
643 | ||
e3cf95dd AC |
644 | static int ata_ioc32(struct ata_port *ap) |
645 | { | |
646 | if (ap->flags & ATA_FLAG_PIO_DMA) | |
647 | return 1; | |
648 | if (ap->pflags & ATA_PFLAG_PIO32) | |
649 | return 1; | |
650 | return 0; | |
651 | } | |
652 | ||
94be9a58 JG |
653 | int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev, |
654 | int cmd, void __user *arg) | |
1da177e4 | 655 | { |
1da177e4 | 656 | int val = -EINVAL, rc = -EINVAL; |
e3cf95dd | 657 | unsigned long flags; |
1da177e4 | 658 | |
1da177e4 LT |
659 | switch (cmd) { |
660 | case ATA_IOC_GET_IO32: | |
e3cf95dd AC |
661 | spin_lock_irqsave(ap->lock, flags); |
662 | val = ata_ioc32(ap); | |
663 | spin_unlock_irqrestore(ap->lock, flags); | |
1da177e4 LT |
664 | if (copy_to_user(arg, &val, 1)) |
665 | return -EFAULT; | |
666 | return 0; | |
667 | ||
668 | case ATA_IOC_SET_IO32: | |
669 | val = (unsigned long) arg; | |
e3cf95dd AC |
670 | rc = 0; |
671 | spin_lock_irqsave(ap->lock, flags); | |
672 | if (ap->pflags & ATA_PFLAG_PIO32CHANGE) { | |
673 | if (val) | |
674 | ap->pflags |= ATA_PFLAG_PIO32; | |
675 | else | |
676 | ap->pflags &= ~ATA_PFLAG_PIO32; | |
677 | } else { | |
678 | if (val != ata_ioc32(ap)) | |
679 | rc = -EINVAL; | |
680 | } | |
681 | spin_unlock_irqrestore(ap->lock, flags); | |
682 | return rc; | |
1da177e4 | 683 | |
5924b74c | 684 | case HDIO_GET_IDENTITY: |
94be9a58 | 685 | return ata_get_identity(ap, scsidev, arg); |
5924b74c | 686 | |
b095518e JG |
687 | case HDIO_DRIVE_CMD: |
688 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) | |
689 | return -EACCES; | |
690 | return ata_cmd_ioctl(scsidev, arg); | |
691 | ||
692 | case HDIO_DRIVE_TASK: | |
693 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) | |
694 | return -EACCES; | |
695 | return ata_task_ioctl(scsidev, arg); | |
696 | ||
1da177e4 LT |
697 | default: |
698 | rc = -ENOTTY; | |
699 | break; | |
700 | } | |
701 | ||
1da177e4 LT |
702 | return rc; |
703 | } | |
94be9a58 JG |
704 | EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl); |
705 | ||
706 | int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) | |
707 | { | |
708 | return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host), | |
709 | scsidev, cmd, arg); | |
710 | } | |
711 | EXPORT_SYMBOL_GPL(ata_scsi_ioctl); | |
1da177e4 LT |
712 | |
713 | /** | |
714 | * ata_scsi_qc_new - acquire new ata_queued_cmd reference | |
1da177e4 LT |
715 | * @dev: ATA device to which the new command is attached |
716 | * @cmd: SCSI command that originated this ATA command | |
717 | * @done: SCSI command completion function | |
718 | * | |
719 | * Obtain a reference to an unused ata_queued_cmd structure, | |
720 | * which is the basic libata structure representing a single | |
721 | * ATA command sent to the hardware. | |
722 | * | |
723 | * If a command was available, fill in the SCSI-specific | |
724 | * portions of the structure with information on the | |
725 | * current command. | |
726 | * | |
727 | * LOCKING: | |
cca3974e | 728 | * spin_lock_irqsave(host lock) |
1da177e4 LT |
729 | * |
730 | * RETURNS: | |
731 | * Command allocated, or %NULL if none available. | |
732 | */ | |
7102d230 AB |
733 | static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev, |
734 | struct scsi_cmnd *cmd, | |
735 | void (*done)(struct scsi_cmnd *)) | |
1da177e4 LT |
736 | { |
737 | struct ata_queued_cmd *qc; | |
738 | ||
8a8bc223 | 739 | qc = ata_qc_new_init(dev); |
1da177e4 LT |
740 | if (qc) { |
741 | qc->scsicmd = cmd; | |
742 | qc->scsidone = done; | |
743 | ||
ff2aeb1e | 744 | qc->sg = scsi_sglist(cmd); |
7120165c | 745 | qc->n_elem = scsi_sg_count(cmd); |
1da177e4 LT |
746 | } else { |
747 | cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1); | |
748 | done(cmd); | |
749 | } | |
750 | ||
751 | return qc; | |
752 | } | |
753 | ||
aacda375 TH |
754 | static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc) |
755 | { | |
756 | struct scsi_cmnd *scmd = qc->scsicmd; | |
757 | ||
758 | qc->extrabytes = scmd->request->extra_len; | |
759 | qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes; | |
760 | } | |
761 | ||
b095518e JG |
762 | /** |
763 | * ata_dump_status - user friendly display of error info | |
764 | * @id: id of the port in question | |
765 | * @tf: ptr to filled out taskfile | |
766 | * | |
767 | * Decode and dump the ATA error/status registers for the user so | |
768 | * that they have some idea what really happened at the non | |
769 | * make-believe layer. | |
770 | * | |
771 | * LOCKING: | |
772 | * inherited from caller | |
773 | */ | |
7102d230 | 774 | static void ata_dump_status(unsigned id, struct ata_taskfile *tf) |
b095518e JG |
775 | { |
776 | u8 stat = tf->command, err = tf->feature; | |
777 | ||
778 | printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat); | |
779 | if (stat & ATA_BUSY) { | |
780 | printk("Busy }\n"); /* Data is not valid in this case */ | |
781 | } else { | |
782 | if (stat & 0x40) printk("DriveReady "); | |
783 | if (stat & 0x20) printk("DeviceFault "); | |
784 | if (stat & 0x10) printk("SeekComplete "); | |
785 | if (stat & 0x08) printk("DataRequest "); | |
786 | if (stat & 0x04) printk("CorrectedError "); | |
787 | if (stat & 0x02) printk("Index "); | |
788 | if (stat & 0x01) printk("Error "); | |
789 | printk("}\n"); | |
790 | ||
791 | if (err) { | |
792 | printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err); | |
793 | if (err & 0x04) printk("DriveStatusError "); | |
794 | if (err & 0x80) { | |
795 | if (err & 0x04) printk("BadCRC "); | |
796 | else printk("Sector "); | |
797 | } | |
798 | if (err & 0x40) printk("UncorrectableError "); | |
799 | if (err & 0x10) printk("SectorIdNotFound "); | |
800 | if (err & 0x02) printk("TrackZeroNotFound "); | |
801 | if (err & 0x01) printk("AddrMarkNotFound "); | |
802 | printk("}\n"); | |
803 | } | |
804 | } | |
805 | } | |
806 | ||
1da177e4 LT |
807 | /** |
808 | * ata_to_sense_error - convert ATA error to SCSI error | |
8e8b77dd | 809 | * @id: ATA device number |
1da177e4 | 810 | * @drv_stat: value contained in ATA status register |
b095518e JG |
811 | * @drv_err: value contained in ATA error register |
812 | * @sk: the sense key we'll fill out | |
813 | * @asc: the additional sense code we'll fill out | |
814 | * @ascq: the additional sense code qualifier we'll fill out | |
246619da | 815 | * @verbose: be verbose |
1da177e4 | 816 | * |
b095518e JG |
817 | * Converts an ATA error into a SCSI error. Fill out pointers to |
818 | * SK, ASC, and ASCQ bytes for later use in fixed or descriptor | |
819 | * format sense blocks. | |
1da177e4 LT |
820 | * |
821 | * LOCKING: | |
cca3974e | 822 | * spin_lock_irqsave(host lock) |
1da177e4 | 823 | */ |
7102d230 AB |
824 | static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, |
825 | u8 *asc, u8 *ascq, int verbose) | |
1da177e4 | 826 | { |
b095518e | 827 | int i; |
ffe75ef6 | 828 | |
1da177e4 | 829 | /* Based on the 3ware driver translation table */ |
98ac62de | 830 | static const unsigned char sense_table[][4] = { |
1da177e4 LT |
831 | /* BBD|ECC|ID|MAR */ |
832 | {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command | |
833 | /* BBD|ECC|ID */ | |
834 | {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command | |
835 | /* ECC|MC|MARK */ | |
836 | {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error | |
837 | /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */ | |
838 | {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error | |
839 | /* MC|ID|ABRT|TRK0|MARK */ | |
840 | {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready | |
841 | /* MCR|MARK */ | |
842 | {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready | |
843 | /* Bad address mark */ | |
844 | {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field | |
845 | /* TRK0 */ | |
846 | {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error | |
847 | /* Abort & !ICRC */ | |
848 | {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command | |
849 | /* Media change request */ | |
850 | {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline | |
851 | /* SRV */ | |
852 | {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found | |
853 | /* Media change */ | |
854 | {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline | |
855 | /* ECC */ | |
856 | {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error | |
857 | /* BBD - block marked bad */ | |
858 | {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error | |
859 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark | |
860 | }; | |
98ac62de | 861 | static const unsigned char stat_table[][4] = { |
1da177e4 LT |
862 | /* Must be first because BUSY means no other bits valid */ |
863 | {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now | |
864 | {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault | |
865 | {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now | |
866 | {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered | |
867 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark | |
868 | }; | |
1da177e4 | 869 | |
1da177e4 LT |
870 | /* |
871 | * Is this an error we can process/parse | |
872 | */ | |
b095518e JG |
873 | if (drv_stat & ATA_BUSY) { |
874 | drv_err = 0; /* Ignore the err bits, they're invalid */ | |
1da177e4 | 875 | } |
b095518e JG |
876 | |
877 | if (drv_err) { | |
878 | /* Look for drv_err */ | |
879 | for (i = 0; sense_table[i][0] != 0xFF; i++) { | |
880 | /* Look for best matches first */ | |
2e9edbf8 | 881 | if ((sense_table[i][0] & drv_err) == |
b095518e JG |
882 | sense_table[i][0]) { |
883 | *sk = sense_table[i][1]; | |
884 | *asc = sense_table[i][2]; | |
885 | *ascq = sense_table[i][3]; | |
886 | goto translate_done; | |
887 | } | |
888 | } | |
889 | /* No immediate match */ | |
246619da TH |
890 | if (verbose) |
891 | printk(KERN_WARNING "ata%u: no sense translation for " | |
892 | "error 0x%02x\n", id, drv_err); | |
1da177e4 | 893 | } |
b095518e JG |
894 | |
895 | /* Fall back to interpreting status bits */ | |
896 | for (i = 0; stat_table[i][0] != 0xFF; i++) { | |
897 | if (stat_table[i][0] & drv_stat) { | |
898 | *sk = stat_table[i][1]; | |
899 | *asc = stat_table[i][2]; | |
900 | *ascq = stat_table[i][3]; | |
901 | goto translate_done; | |
1da177e4 | 902 | } |
b095518e JG |
903 | } |
904 | /* No error? Undecoded? */ | |
246619da TH |
905 | if (verbose) |
906 | printk(KERN_WARNING "ata%u: no sense translation for " | |
907 | "status: 0x%02x\n", id, drv_stat); | |
b095518e | 908 | |
2d202024 AC |
909 | /* We need a sensible error return here, which is tricky, and one |
910 | that won't cause people to do things like return a disk wrongly */ | |
911 | *sk = ABORTED_COMMAND; | |
912 | *asc = 0x00; | |
913 | *ascq = 0x00; | |
b095518e JG |
914 | |
915 | translate_done: | |
246619da TH |
916 | if (verbose) |
917 | printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x " | |
918 | "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", | |
919 | id, drv_stat, drv_err, *sk, *asc, *ascq); | |
b095518e JG |
920 | return; |
921 | } | |
922 | ||
923 | /* | |
750426aa | 924 | * ata_gen_passthru_sense - Generate check condition sense block. |
b095518e JG |
925 | * @qc: Command that completed. |
926 | * | |
927 | * This function is specific to the ATA descriptor format sense | |
928 | * block specified for the ATA pass through commands. Regardless | |
929 | * of whether the command errored or not, return a sense | |
930 | * block. Copy all controller registers into the sense | |
931 | * block. Clear sense key, ASC & ASCQ if there is no error. | |
932 | * | |
933 | * LOCKING: | |
750426aa | 934 | * None. |
b095518e | 935 | */ |
750426aa | 936 | static void ata_gen_passthru_sense(struct ata_queued_cmd *qc) |
b095518e JG |
937 | { |
938 | struct scsi_cmnd *cmd = qc->scsicmd; | |
e61e0672 | 939 | struct ata_taskfile *tf = &qc->result_tf; |
b095518e JG |
940 | unsigned char *sb = cmd->sense_buffer; |
941 | unsigned char *desc = sb + 8; | |
246619da | 942 | int verbose = qc->ap->ops->error_handler == NULL; |
b095518e JG |
943 | |
944 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); | |
945 | ||
0e5dec47 | 946 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
b095518e | 947 | |
b095518e JG |
948 | /* |
949 | * Use ata_to_sense_error() to map status register bits | |
950 | * onto sense key, asc & ascq. | |
951 | */ | |
058e55e1 TH |
952 | if (qc->err_mask || |
953 | tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { | |
44877b4e | 954 | ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature, |
246619da | 955 | &sb[1], &sb[2], &sb[3], verbose); |
b095518e | 956 | sb[1] &= 0x0f; |
1da177e4 LT |
957 | } |
958 | ||
b095518e JG |
959 | /* |
960 | * Sense data is current and format is descriptor. | |
961 | */ | |
962 | sb[0] = 0x72; | |
1da177e4 | 963 | |
b095518e JG |
964 | desc[0] = 0x09; |
965 | ||
f38621b3 TH |
966 | /* set length of additional sense data */ |
967 | sb[7] = 14; | |
968 | desc[1] = 12; | |
b095518e JG |
969 | |
970 | /* | |
971 | * Copy registers into sense buffer. | |
972 | */ | |
973 | desc[2] = 0x00; | |
974 | desc[3] = tf->feature; /* == error reg */ | |
975 | desc[5] = tf->nsect; | |
976 | desc[7] = tf->lbal; | |
977 | desc[9] = tf->lbam; | |
978 | desc[11] = tf->lbah; | |
979 | desc[12] = tf->device; | |
980 | desc[13] = tf->command; /* == status reg */ | |
981 | ||
982 | /* | |
983 | * Fill in Extend bit, and the high order bytes | |
984 | * if applicable. | |
985 | */ | |
986 | if (tf->flags & ATA_TFLAG_LBA48) { | |
987 | desc[2] |= 0x01; | |
988 | desc[4] = tf->hob_nsect; | |
989 | desc[6] = tf->hob_lbal; | |
990 | desc[8] = tf->hob_lbam; | |
991 | desc[10] = tf->hob_lbah; | |
1da177e4 | 992 | } |
b095518e | 993 | } |
1da177e4 | 994 | |
b095518e | 995 | /** |
750426aa | 996 | * ata_gen_ata_sense - generate a SCSI fixed sense block |
b095518e JG |
997 | * @qc: Command that we are erroring out |
998 | * | |
d25614ba TH |
999 | * Generate sense block for a failed ATA command @qc. Descriptor |
1000 | * format is used to accomodate LBA48 block address. | |
b095518e JG |
1001 | * |
1002 | * LOCKING: | |
750426aa | 1003 | * None. |
b095518e | 1004 | */ |
750426aa | 1005 | static void ata_gen_ata_sense(struct ata_queued_cmd *qc) |
b095518e | 1006 | { |
d25614ba | 1007 | struct ata_device *dev = qc->dev; |
b095518e | 1008 | struct scsi_cmnd *cmd = qc->scsicmd; |
e61e0672 | 1009 | struct ata_taskfile *tf = &qc->result_tf; |
b095518e | 1010 | unsigned char *sb = cmd->sense_buffer; |
d25614ba | 1011 | unsigned char *desc = sb + 8; |
246619da | 1012 | int verbose = qc->ap->ops->error_handler == NULL; |
d25614ba | 1013 | u64 block; |
b095518e JG |
1014 | |
1015 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); | |
1016 | ||
0e5dec47 | 1017 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
b095518e | 1018 | |
d25614ba TH |
1019 | /* sense data is current and format is descriptor */ |
1020 | sb[0] = 0x72; | |
1021 | ||
1022 | /* Use ata_to_sense_error() to map status register bits | |
b095518e JG |
1023 | * onto sense key, asc & ascq. |
1024 | */ | |
058e55e1 TH |
1025 | if (qc->err_mask || |
1026 | tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { | |
44877b4e | 1027 | ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature, |
d25614ba TH |
1028 | &sb[1], &sb[2], &sb[3], verbose); |
1029 | sb[1] &= 0x0f; | |
1da177e4 | 1030 | } |
1da177e4 | 1031 | |
d25614ba | 1032 | block = ata_tf_read_block(&qc->result_tf, dev); |
a7dac447 | 1033 | |
d25614ba TH |
1034 | /* information sense data descriptor */ |
1035 | sb[7] = 12; | |
1036 | desc[0] = 0x00; | |
1037 | desc[1] = 10; | |
a7dac447 | 1038 | |
d25614ba TH |
1039 | desc[2] |= 0x80; /* valid */ |
1040 | desc[6] = block >> 40; | |
1041 | desc[7] = block >> 32; | |
1042 | desc[8] = block >> 24; | |
1043 | desc[9] = block >> 16; | |
1044 | desc[10] = block >> 8; | |
1045 | desc[11] = block; | |
1da177e4 LT |
1046 | } |
1047 | ||
a6cce2a7 BK |
1048 | static void ata_scsi_sdev_config(struct scsi_device *sdev) |
1049 | { | |
1050 | sdev->use_10_for_rw = 1; | |
1051 | sdev->use_10_for_ms = 1; | |
31cc23b3 TH |
1052 | |
1053 | /* Schedule policy is determined by ->qc_defer() callback and | |
1054 | * it needs to see every deferred qc. Set dev_blocked to 1 to | |
1055 | * prevent SCSI midlayer from automatically deferring | |
1056 | * requests. | |
1057 | */ | |
1058 | sdev->max_device_blocked = 1; | |
a6cce2a7 BK |
1059 | } |
1060 | ||
fa2fc7f4 JB |
1061 | /** |
1062 | * atapi_drain_needed - Check whether data transfer may overflow | |
73fd8b6d | 1063 | * @rq: request to be checked |
fa2fc7f4 JB |
1064 | * |
1065 | * ATAPI commands which transfer variable length data to host | |
1066 | * might overflow due to application error or hardare bug. This | |
1067 | * function checks whether overflow should be drained and ignored | |
1068 | * for @request. | |
1069 | * | |
1070 | * LOCKING: | |
1071 | * None. | |
1072 | * | |
1073 | * RETURNS: | |
1074 | * 1 if ; otherwise, 0. | |
1075 | */ | |
1076 | static int atapi_drain_needed(struct request *rq) | |
1077 | { | |
33659ebb | 1078 | if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC)) |
fa2fc7f4 JB |
1079 | return 0; |
1080 | ||
7b6d91da | 1081 | if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE)) |
fa2fc7f4 JB |
1082 | return 0; |
1083 | ||
1084 | return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC; | |
1085 | } | |
1086 | ||
1087 | static int ata_scsi_dev_config(struct scsi_device *sdev, | |
1088 | struct ata_device *dev) | |
a6cce2a7 | 1089 | { |
45fabbb7 EO |
1090 | if (!ata_id_has_unload(dev->id)) |
1091 | dev->flags |= ATA_DFLAG_NO_UNLOAD; | |
1092 | ||
914ed354 | 1093 | /* configure max sectors */ |
086fa5ff | 1094 | blk_queue_max_hw_sectors(sdev->request_queue, dev->max_sectors); |
a6cce2a7 | 1095 | |
fa2fc7f4 JB |
1096 | if (dev->class == ATA_DEV_ATAPI) { |
1097 | struct request_queue *q = sdev->request_queue; | |
1098 | void *buf; | |
1099 | ||
e3790c7d | 1100 | /* set the min alignment and padding */ |
d0ad3bc9 JB |
1101 | blk_queue_update_dma_alignment(sdev->request_queue, |
1102 | ATA_DMA_PAD_SZ - 1); | |
27f8221a FT |
1103 | blk_queue_update_dma_pad(sdev->request_queue, |
1104 | ATA_DMA_PAD_SZ - 1); | |
fa2fc7f4 JB |
1105 | |
1106 | /* configure draining */ | |
1107 | buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL); | |
1108 | if (!buf) { | |
1109 | ata_dev_printk(dev, KERN_ERR, | |
1110 | "drain buffer allocation failed\n"); | |
1111 | return -ENOMEM; | |
1112 | } | |
1113 | ||
1114 | blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN); | |
1115 | } else { | |
d0ad3bc9 | 1116 | /* ATA devices must be sector aligned */ |
295124dc | 1117 | sdev->sector_size = ata_id_logical_sector_size(dev->id); |
d0ad3bc9 | 1118 | blk_queue_update_dma_alignment(sdev->request_queue, |
295124dc | 1119 | sdev->sector_size - 1); |
d8cf5389 | 1120 | sdev->manage_start_stop = 1; |
dde20207 | 1121 | } |
d8cf5389 | 1122 | |
f26792d5 JG |
1123 | if (dev->flags & ATA_DFLAG_AN) |
1124 | set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events); | |
1125 | ||
a6e6ce8e TH |
1126 | if (dev->flags & ATA_DFLAG_NCQ) { |
1127 | int depth; | |
1128 | ||
1129 | depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id)); | |
1130 | depth = min(ATA_MAX_QUEUE - 1, depth); | |
8a8bc223 | 1131 | scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth); |
a6e6ce8e | 1132 | } |
fa2fc7f4 | 1133 | |
295124dc | 1134 | dev->sdev = sdev; |
fa2fc7f4 | 1135 | return 0; |
a6cce2a7 BK |
1136 | } |
1137 | ||
1da177e4 LT |
1138 | /** |
1139 | * ata_scsi_slave_config - Set SCSI device attributes | |
1140 | * @sdev: SCSI device to examine | |
1141 | * | |
1142 | * This is called before we actually start reading | |
1143 | * and writing to the device, to configure certain | |
1144 | * SCSI mid-layer behaviors. | |
1145 | * | |
1146 | * LOCKING: | |
1147 | * Defined by SCSI layer. We don't really care. | |
1148 | */ | |
1149 | ||
1150 | int ata_scsi_slave_config(struct scsi_device *sdev) | |
1151 | { | |
31534363 TH |
1152 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
1153 | struct ata_device *dev = __ata_scsi_find_dev(ap, sdev); | |
fa2fc7f4 | 1154 | int rc = 0; |
31534363 | 1155 | |
a6cce2a7 | 1156 | ata_scsi_sdev_config(sdev); |
1da177e4 | 1157 | |
31534363 | 1158 | if (dev) |
fa2fc7f4 | 1159 | rc = ata_scsi_dev_config(sdev, dev); |
1da177e4 | 1160 | |
fa2fc7f4 | 1161 | return rc; |
1da177e4 LT |
1162 | } |
1163 | ||
83c47bcb TH |
1164 | /** |
1165 | * ata_scsi_slave_destroy - SCSI device is about to be destroyed | |
1166 | * @sdev: SCSI device to be destroyed | |
1167 | * | |
1168 | * @sdev is about to be destroyed for hot/warm unplugging. If | |
1169 | * this unplugging was initiated by libata as indicated by NULL | |
1170 | * dev->sdev, this function doesn't have to do anything. | |
1171 | * Otherwise, SCSI layer initiated warm-unplug is in progress. | |
1172 | * Clear dev->sdev, schedule the device for ATA detach and invoke | |
1173 | * EH. | |
1174 | * | |
1175 | * LOCKING: | |
1176 | * Defined by SCSI layer. We don't really care. | |
1177 | */ | |
1178 | void ata_scsi_slave_destroy(struct scsi_device *sdev) | |
1179 | { | |
1180 | struct ata_port *ap = ata_shost_to_port(sdev->host); | |
fa2fc7f4 | 1181 | struct request_queue *q = sdev->request_queue; |
83c47bcb TH |
1182 | unsigned long flags; |
1183 | struct ata_device *dev; | |
1184 | ||
1185 | if (!ap->ops->error_handler) | |
1186 | return; | |
1187 | ||
ba6a1308 | 1188 | spin_lock_irqsave(ap->lock, flags); |
83c47bcb TH |
1189 | dev = __ata_scsi_find_dev(ap, sdev); |
1190 | if (dev && dev->sdev) { | |
1191 | /* SCSI device already in CANCEL state, no need to offline it */ | |
1192 | dev->sdev = NULL; | |
1193 | dev->flags |= ATA_DFLAG_DETACH; | |
1194 | ata_port_schedule_eh(ap); | |
1195 | } | |
ba6a1308 | 1196 | spin_unlock_irqrestore(ap->lock, flags); |
fa2fc7f4 JB |
1197 | |
1198 | kfree(q->dma_drain_buffer); | |
1199 | q->dma_drain_buffer = NULL; | |
1200 | q->dma_drain_size = 0; | |
83c47bcb TH |
1201 | } |
1202 | ||
a6e6ce8e TH |
1203 | /** |
1204 | * ata_scsi_change_queue_depth - SCSI callback for queue depth config | |
1205 | * @sdev: SCSI device to configure queue depth for | |
1206 | * @queue_depth: new queue depth | |
e881a172 | 1207 | * @reason: calling context |
a6e6ce8e TH |
1208 | * |
1209 | * This is libata standard hostt->change_queue_depth callback. | |
1210 | * SCSI will call into this callback when user tries to set queue | |
1211 | * depth via sysfs. | |
1212 | * | |
1213 | * LOCKING: | |
1214 | * SCSI layer (we don't care) | |
1215 | * | |
1216 | * RETURNS: | |
1217 | * Newly configured queue depth. | |
1218 | */ | |
e881a172 MC |
1219 | int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth, |
1220 | int reason) | |
a6e6ce8e TH |
1221 | { |
1222 | struct ata_port *ap = ata_shost_to_port(sdev->host); | |
1223 | struct ata_device *dev; | |
360f654e | 1224 | unsigned long flags; |
a6e6ce8e | 1225 | |
e881a172 MC |
1226 | if (reason != SCSI_QDEPTH_DEFAULT) |
1227 | return -EOPNOTSUPP; | |
1228 | ||
c3c70c44 | 1229 | if (queue_depth < 1 || queue_depth == sdev->queue_depth) |
a6e6ce8e TH |
1230 | return sdev->queue_depth; |
1231 | ||
1232 | dev = ata_scsi_find_dev(ap, sdev); | |
1233 | if (!dev || !ata_dev_enabled(dev)) | |
1234 | return sdev->queue_depth; | |
1235 | ||
c3c70c44 | 1236 | /* NCQ enabled? */ |
360f654e | 1237 | spin_lock_irqsave(ap->lock, flags); |
c3c70c44 TH |
1238 | dev->flags &= ~ATA_DFLAG_NCQ_OFF; |
1239 | if (queue_depth == 1 || !ata_ncq_enabled(dev)) { | |
360f654e | 1240 | dev->flags |= ATA_DFLAG_NCQ_OFF; |
c3c70c44 TH |
1241 | queue_depth = 1; |
1242 | } | |
360f654e TH |
1243 | spin_unlock_irqrestore(ap->lock, flags); |
1244 | ||
c3c70c44 TH |
1245 | /* limit and apply queue depth */ |
1246 | queue_depth = min(queue_depth, sdev->host->can_queue); | |
1247 | queue_depth = min(queue_depth, ata_id_queue_depth(dev->id)); | |
1248 | queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1); | |
1249 | ||
1250 | if (sdev->queue_depth == queue_depth) | |
1251 | return -EINVAL; | |
1252 | ||
1253 | scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth); | |
a6e6ce8e TH |
1254 | return queue_depth; |
1255 | } | |
1256 | ||
972dcafb DG |
1257 | /** |
1258 | * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command | |
1259 | * @qc: Storage for translated ATA taskfile | |
972dcafb DG |
1260 | * |
1261 | * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY | |
1262 | * (to start). Perhaps these commands should be preceded by | |
1263 | * CHECK POWER MODE to see what power mode the device is already in. | |
1264 | * [See SAT revision 5 at www.t10.org] | |
1265 | * | |
1266 | * LOCKING: | |
cca3974e | 1267 | * spin_lock_irqsave(host lock) |
972dcafb DG |
1268 | * |
1269 | * RETURNS: | |
1270 | * Zero on success, non-zero on error. | |
1271 | */ | |
ad706991 | 1272 | static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc) |
972dcafb | 1273 | { |
542b1444 | 1274 | struct scsi_cmnd *scmd = qc->scsicmd; |
972dcafb | 1275 | struct ata_taskfile *tf = &qc->tf; |
ad706991 | 1276 | const u8 *cdb = scmd->cmnd; |
972dcafb | 1277 | |
2e5704f6 TH |
1278 | if (scmd->cmd_len < 5) |
1279 | goto invalid_fld; | |
1280 | ||
972dcafb DG |
1281 | tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
1282 | tf->protocol = ATA_PROT_NODATA; | |
542b1444 | 1283 | if (cdb[1] & 0x1) { |
972dcafb DG |
1284 | ; /* ignore IMMED bit, violates sat-r05 */ |
1285 | } | |
542b1444 | 1286 | if (cdb[4] & 0x2) |
ae006510 | 1287 | goto invalid_fld; /* LOEJ bit set not supported */ |
542b1444 | 1288 | if (((cdb[4] >> 4) & 0xf) != 0) |
ae006510 | 1289 | goto invalid_fld; /* power conditions not supported */ |
e31e8531 | 1290 | |
542b1444 | 1291 | if (cdb[4] & 0x1) { |
972dcafb | 1292 | tf->nsect = 1; /* 1 sector, lba=0 */ |
9d5b1302 AL |
1293 | |
1294 | if (qc->dev->flags & ATA_DFLAG_LBA) { | |
c44078c0 | 1295 | tf->flags |= ATA_TFLAG_LBA; |
9d5b1302 AL |
1296 | |
1297 | tf->lbah = 0x0; | |
1298 | tf->lbam = 0x0; | |
1299 | tf->lbal = 0x0; | |
1300 | tf->device |= ATA_LBA; | |
1301 | } else { | |
1302 | /* CHS */ | |
1303 | tf->lbal = 0x1; /* sect */ | |
1304 | tf->lbam = 0x0; /* cyl low */ | |
1305 | tf->lbah = 0x0; /* cyl high */ | |
1306 | } | |
1307 | ||
972dcafb | 1308 | tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ |
920a4b10 | 1309 | } else { |
2a6e58d2 RW |
1310 | /* Some odd clown BIOSen issue spindown on power off (ACPI S4 |
1311 | * or S5) causing some drives to spin up and down again. | |
1312 | */ | |
1313 | if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) && | |
1314 | system_state == SYSTEM_POWER_OFF) | |
1315 | goto skip; | |
1316 | ||
1317 | if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) && | |
1318 | system_entering_hibernation()) | |
1319 | goto skip; | |
1320 | ||
78981a7c RH |
1321 | /* Issue ATA STANDBY IMMEDIATE command */ |
1322 | tf->command = ATA_CMD_STANDBYNOW1; | |
920a4b10 | 1323 | } |
78981a7c | 1324 | |
972dcafb DG |
1325 | /* |
1326 | * Standby and Idle condition timers could be implemented but that | |
1327 | * would require libata to implement the Power condition mode page | |
1328 | * and allow the user to change it. Changing mode pages requires | |
1329 | * MODE SELECT to be implemented. | |
1330 | */ | |
1331 | ||
1332 | return 0; | |
ae006510 | 1333 | |
2a6e58d2 | 1334 | invalid_fld: |
542b1444 | 1335 | ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0); |
ae006510 DG |
1336 | /* "Invalid field in cbd" */ |
1337 | return 1; | |
2a6e58d2 RW |
1338 | skip: |
1339 | scmd->result = SAM_STAT_GOOD; | |
1340 | return 1; | |
972dcafb DG |
1341 | } |
1342 | ||
1343 | ||
1da177e4 LT |
1344 | /** |
1345 | * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command | |
1346 | * @qc: Storage for translated ATA taskfile | |
1da177e4 LT |
1347 | * |
1348 | * Sets up an ATA taskfile to issue FLUSH CACHE or | |
1349 | * FLUSH CACHE EXT. | |
1350 | * | |
1351 | * LOCKING: | |
cca3974e | 1352 | * spin_lock_irqsave(host lock) |
1da177e4 LT |
1353 | * |
1354 | * RETURNS: | |
1355 | * Zero on success, non-zero on error. | |
1356 | */ | |
ad706991 | 1357 | static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc) |
1da177e4 LT |
1358 | { |
1359 | struct ata_taskfile *tf = &qc->tf; | |
1360 | ||
1361 | tf->flags |= ATA_TFLAG_DEVICE; | |
1362 | tf->protocol = ATA_PROT_NODATA; | |
1363 | ||
6fc49adb | 1364 | if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT) |
1da177e4 LT |
1365 | tf->command = ATA_CMD_FLUSH_EXT; |
1366 | else | |
1367 | tf->command = ATA_CMD_FLUSH; | |
1368 | ||
b666da35 TH |
1369 | /* flush is critical for IO integrity, consider it an IO command */ |
1370 | qc->flags |= ATA_QCFLAG_IO; | |
1371 | ||
1da177e4 LT |
1372 | return 0; |
1373 | } | |
1374 | ||
3aef5231 AL |
1375 | /** |
1376 | * scsi_6_lba_len - Get LBA and transfer length | |
542b1444 | 1377 | * @cdb: SCSI command to translate |
3aef5231 AL |
1378 | * |
1379 | * Calculate LBA and transfer length for 6-byte commands. | |
1380 | * | |
1381 | * RETURNS: | |
1382 | * @plba: the LBA | |
1383 | * @plen: the transfer length | |
1384 | */ | |
542b1444 | 1385 | static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen) |
3aef5231 AL |
1386 | { |
1387 | u64 lba = 0; | |
6c7b7d2b | 1388 | u32 len; |
3aef5231 AL |
1389 | |
1390 | VPRINTK("six-byte command\n"); | |
1391 | ||
6c7b7d2b | 1392 | lba |= ((u64)(cdb[1] & 0x1f)) << 16; |
542b1444 TH |
1393 | lba |= ((u64)cdb[2]) << 8; |
1394 | lba |= ((u64)cdb[3]); | |
3aef5231 | 1395 | |
6c7b7d2b | 1396 | len = cdb[4]; |
3aef5231 AL |
1397 | |
1398 | *plba = lba; | |
1399 | *plen = len; | |
1400 | } | |
1401 | ||
1402 | /** | |
1403 | * scsi_10_lba_len - Get LBA and transfer length | |
542b1444 | 1404 | * @cdb: SCSI command to translate |
3aef5231 AL |
1405 | * |
1406 | * Calculate LBA and transfer length for 10-byte commands. | |
1407 | * | |
1408 | * RETURNS: | |
1409 | * @plba: the LBA | |
1410 | * @plen: the transfer length | |
1411 | */ | |
542b1444 | 1412 | static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen) |
3aef5231 AL |
1413 | { |
1414 | u64 lba = 0; | |
1415 | u32 len = 0; | |
1416 | ||
1417 | VPRINTK("ten-byte command\n"); | |
1418 | ||
542b1444 TH |
1419 | lba |= ((u64)cdb[2]) << 24; |
1420 | lba |= ((u64)cdb[3]) << 16; | |
1421 | lba |= ((u64)cdb[4]) << 8; | |
1422 | lba |= ((u64)cdb[5]); | |
3aef5231 | 1423 | |
542b1444 TH |
1424 | len |= ((u32)cdb[7]) << 8; |
1425 | len |= ((u32)cdb[8]); | |
3aef5231 AL |
1426 | |
1427 | *plba = lba; | |
1428 | *plen = len; | |
1429 | } | |
1430 | ||
1431 | /** | |
1432 | * scsi_16_lba_len - Get LBA and transfer length | |
542b1444 | 1433 | * @cdb: SCSI command to translate |
3aef5231 AL |
1434 | * |
1435 | * Calculate LBA and transfer length for 16-byte commands. | |
1436 | * | |
1437 | * RETURNS: | |
1438 | * @plba: the LBA | |
1439 | * @plen: the transfer length | |
1440 | */ | |
542b1444 | 1441 | static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen) |
3aef5231 AL |
1442 | { |
1443 | u64 lba = 0; | |
1444 | u32 len = 0; | |
1445 | ||
1446 | VPRINTK("sixteen-byte command\n"); | |
1447 | ||
542b1444 TH |
1448 | lba |= ((u64)cdb[2]) << 56; |
1449 | lba |= ((u64)cdb[3]) << 48; | |
1450 | lba |= ((u64)cdb[4]) << 40; | |
1451 | lba |= ((u64)cdb[5]) << 32; | |
1452 | lba |= ((u64)cdb[6]) << 24; | |
1453 | lba |= ((u64)cdb[7]) << 16; | |
1454 | lba |= ((u64)cdb[8]) << 8; | |
1455 | lba |= ((u64)cdb[9]); | |
3aef5231 | 1456 | |
542b1444 TH |
1457 | len |= ((u32)cdb[10]) << 24; |
1458 | len |= ((u32)cdb[11]) << 16; | |
1459 | len |= ((u32)cdb[12]) << 8; | |
1460 | len |= ((u32)cdb[13]); | |
3aef5231 AL |
1461 | |
1462 | *plba = lba; | |
1463 | *plen = len; | |
1464 | } | |
1465 | ||
1da177e4 LT |
1466 | /** |
1467 | * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one | |
1468 | * @qc: Storage for translated ATA taskfile | |
1da177e4 LT |
1469 | * |
1470 | * Converts SCSI VERIFY command to an ATA READ VERIFY command. | |
1471 | * | |
1472 | * LOCKING: | |
cca3974e | 1473 | * spin_lock_irqsave(host lock) |
1da177e4 LT |
1474 | * |
1475 | * RETURNS: | |
1476 | * Zero on success, non-zero on error. | |
1477 | */ | |
ad706991 | 1478 | static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc) |
1da177e4 | 1479 | { |
542b1444 | 1480 | struct scsi_cmnd *scmd = qc->scsicmd; |
1da177e4 | 1481 | struct ata_taskfile *tf = &qc->tf; |
8bf62ece | 1482 | struct ata_device *dev = qc->dev; |
1da177e4 | 1483 | u64 dev_sectors = qc->dev->n_sectors; |
ad706991 | 1484 | const u8 *cdb = scmd->cmnd; |
3aef5231 AL |
1485 | u64 block; |
1486 | u32 n_block; | |
1da177e4 LT |
1487 | |
1488 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
1489 | tf->protocol = ATA_PROT_NODATA; | |
1da177e4 | 1490 | |
2e5704f6 TH |
1491 | if (cdb[0] == VERIFY) { |
1492 | if (scmd->cmd_len < 10) | |
1493 | goto invalid_fld; | |
542b1444 | 1494 | scsi_10_lba_len(cdb, &block, &n_block); |
2e5704f6 TH |
1495 | } else if (cdb[0] == VERIFY_16) { |
1496 | if (scmd->cmd_len < 16) | |
1497 | goto invalid_fld; | |
542b1444 | 1498 | scsi_16_lba_len(cdb, &block, &n_block); |
2e5704f6 | 1499 | } else |
ae006510 | 1500 | goto invalid_fld; |
1da177e4 | 1501 | |
8bf62ece | 1502 | if (!n_block) |
ae006510 | 1503 | goto nothing_to_do; |
8bf62ece | 1504 | if (block >= dev_sectors) |
ae006510 | 1505 | goto out_of_range; |
8bf62ece | 1506 | if ((block + n_block) > dev_sectors) |
ae006510 | 1507 | goto out_of_range; |
1da177e4 | 1508 | |
07506697 AL |
1509 | if (dev->flags & ATA_DFLAG_LBA) { |
1510 | tf->flags |= ATA_TFLAG_LBA; | |
1511 | ||
c6a33e24 AL |
1512 | if (lba_28_ok(block, n_block)) { |
1513 | /* use LBA28 */ | |
1514 | tf->command = ATA_CMD_VERIFY; | |
1515 | tf->device |= (block >> 24) & 0xf; | |
1516 | } else if (lba_48_ok(block, n_block)) { | |
1517 | if (!(dev->flags & ATA_DFLAG_LBA48)) | |
1518 | goto out_of_range; | |
07506697 AL |
1519 | |
1520 | /* use LBA48 */ | |
1521 | tf->flags |= ATA_TFLAG_LBA48; | |
8bf62ece | 1522 | tf->command = ATA_CMD_VERIFY_EXT; |
1da177e4 | 1523 | |
8bf62ece | 1524 | tf->hob_nsect = (n_block >> 8) & 0xff; |
1da177e4 | 1525 | |
8bf62ece AL |
1526 | tf->hob_lbah = (block >> 40) & 0xff; |
1527 | tf->hob_lbam = (block >> 32) & 0xff; | |
1528 | tf->hob_lbal = (block >> 24) & 0xff; | |
c6a33e24 AL |
1529 | } else |
1530 | /* request too large even for LBA48 */ | |
1531 | goto out_of_range; | |
8bf62ece AL |
1532 | |
1533 | tf->nsect = n_block & 0xff; | |
1da177e4 | 1534 | |
8bf62ece AL |
1535 | tf->lbah = (block >> 16) & 0xff; |
1536 | tf->lbam = (block >> 8) & 0xff; | |
1537 | tf->lbal = block & 0xff; | |
1da177e4 | 1538 | |
8bf62ece AL |
1539 | tf->device |= ATA_LBA; |
1540 | } else { | |
1541 | /* CHS */ | |
1542 | u32 sect, head, cyl, track; | |
1543 | ||
c6a33e24 AL |
1544 | if (!lba_28_ok(block, n_block)) |
1545 | goto out_of_range; | |
07506697 | 1546 | |
8bf62ece AL |
1547 | /* Convert LBA to CHS */ |
1548 | track = (u32)block / dev->sectors; | |
1549 | cyl = track / dev->heads; | |
1550 | head = track % dev->heads; | |
1551 | sect = (u32)block % dev->sectors + 1; | |
1552 | ||
c187c4b5 AL |
1553 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
1554 | (u32)block, track, cyl, head, sect); | |
2e9edbf8 JG |
1555 | |
1556 | /* Check whether the converted CHS can fit. | |
1557 | Cylinder: 0-65535 | |
8bf62ece AL |
1558 | Head: 0-15 |
1559 | Sector: 1-255*/ | |
2e9edbf8 | 1560 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
ae006510 | 1561 | goto out_of_range; |
2e9edbf8 | 1562 | |
8bf62ece AL |
1563 | tf->command = ATA_CMD_VERIFY; |
1564 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ | |
1565 | tf->lbal = sect; | |
1566 | tf->lbam = cyl; | |
1567 | tf->lbah = cyl >> 8; | |
1568 | tf->device |= head; | |
1569 | } | |
1da177e4 LT |
1570 | |
1571 | return 0; | |
ae006510 DG |
1572 | |
1573 | invalid_fld: | |
542b1444 | 1574 | ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0); |
ae006510 DG |
1575 | /* "Invalid field in cbd" */ |
1576 | return 1; | |
1577 | ||
1578 | out_of_range: | |
542b1444 | 1579 | ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0); |
ae006510 DG |
1580 | /* "Logical Block Address out of range" */ |
1581 | return 1; | |
1582 | ||
1583 | nothing_to_do: | |
542b1444 | 1584 | scmd->result = SAM_STAT_GOOD; |
ae006510 | 1585 | return 1; |
1da177e4 LT |
1586 | } |
1587 | ||
1588 | /** | |
1589 | * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one | |
1590 | * @qc: Storage for translated ATA taskfile | |
1da177e4 LT |
1591 | * |
1592 | * Converts any of six SCSI read/write commands into the | |
1593 | * ATA counterpart, including starting sector (LBA), | |
1594 | * sector count, and taking into account the device's LBA48 | |
1595 | * support. | |
1596 | * | |
1597 | * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and | |
1598 | * %WRITE_16 are currently supported. | |
1599 | * | |
1600 | * LOCKING: | |
cca3974e | 1601 | * spin_lock_irqsave(host lock) |
1da177e4 LT |
1602 | * |
1603 | * RETURNS: | |
1604 | * Zero on success, non-zero on error. | |
1605 | */ | |
ad706991 | 1606 | static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc) |
1da177e4 | 1607 | { |
542b1444 | 1608 | struct scsi_cmnd *scmd = qc->scsicmd; |
ad706991 | 1609 | const u8 *cdb = scmd->cmnd; |
bd056d7e | 1610 | unsigned int tf_flags = 0; |
3aef5231 AL |
1611 | u64 block; |
1612 | u32 n_block; | |
bd056d7e | 1613 | int rc; |
1da177e4 | 1614 | |
542b1444 | 1615 | if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16) |
bd056d7e | 1616 | tf_flags |= ATA_TFLAG_WRITE; |
1da177e4 | 1617 | |
9a3dccc4 | 1618 | /* Calculate the SCSI LBA, transfer length and FUA. */ |
542b1444 | 1619 | switch (cdb[0]) { |
3aef5231 AL |
1620 | case READ_10: |
1621 | case WRITE_10: | |
2e5704f6 TH |
1622 | if (unlikely(scmd->cmd_len < 10)) |
1623 | goto invalid_fld; | |
542b1444 TH |
1624 | scsi_10_lba_len(cdb, &block, &n_block); |
1625 | if (unlikely(cdb[1] & (1 << 3))) | |
bd056d7e | 1626 | tf_flags |= ATA_TFLAG_FUA; |
3aef5231 AL |
1627 | break; |
1628 | case READ_6: | |
1629 | case WRITE_6: | |
2e5704f6 TH |
1630 | if (unlikely(scmd->cmd_len < 6)) |
1631 | goto invalid_fld; | |
542b1444 | 1632 | scsi_6_lba_len(cdb, &block, &n_block); |
c187c4b5 AL |
1633 | |
1634 | /* for 6-byte r/w commands, transfer length 0 | |
1635 | * means 256 blocks of data, not 0 block. | |
1636 | */ | |
76b2bf9b JG |
1637 | if (!n_block) |
1638 | n_block = 256; | |
3aef5231 AL |
1639 | break; |
1640 | case READ_16: | |
1641 | case WRITE_16: | |
2e5704f6 TH |
1642 | if (unlikely(scmd->cmd_len < 16)) |
1643 | goto invalid_fld; | |
542b1444 TH |
1644 | scsi_16_lba_len(cdb, &block, &n_block); |
1645 | if (unlikely(cdb[1] & (1 << 3))) | |
bd056d7e | 1646 | tf_flags |= ATA_TFLAG_FUA; |
3aef5231 AL |
1647 | break; |
1648 | default: | |
8bf62ece | 1649 | DPRINTK("no-byte command\n"); |
ae006510 | 1650 | goto invalid_fld; |
1da177e4 LT |
1651 | } |
1652 | ||
8bf62ece AL |
1653 | /* Check and compose ATA command */ |
1654 | if (!n_block) | |
c187c4b5 AL |
1655 | /* For 10-byte and 16-byte SCSI R/W commands, transfer |
1656 | * length 0 means transfer 0 block of data. | |
1657 | * However, for ATA R/W commands, sector count 0 means | |
1658 | * 256 or 65536 sectors, not 0 sectors as in SCSI. | |
f51750d5 AC |
1659 | * |
1660 | * WARNING: one or two older ATA drives treat 0 as 0... | |
c187c4b5 | 1661 | */ |
ae006510 | 1662 | goto nothing_to_do; |
1da177e4 | 1663 | |
bd056d7e | 1664 | qc->flags |= ATA_QCFLAG_IO; |
295124dc | 1665 | qc->nbytes = n_block * scmd->device->sector_size; |
1da177e4 | 1666 | |
bd056d7e TH |
1667 | rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags, |
1668 | qc->tag); | |
1669 | if (likely(rc == 0)) | |
1670 | return 0; | |
ae006510 | 1671 | |
bd056d7e TH |
1672 | if (rc == -ERANGE) |
1673 | goto out_of_range; | |
1674 | /* treat all other errors as -EINVAL, fall through */ | |
ae006510 | 1675 | invalid_fld: |
542b1444 | 1676 | ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0); |
ae006510 DG |
1677 | /* "Invalid field in cbd" */ |
1678 | return 1; | |
1679 | ||
1680 | out_of_range: | |
542b1444 | 1681 | ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0); |
ae006510 DG |
1682 | /* "Logical Block Address out of range" */ |
1683 | return 1; | |
1684 | ||
1685 | nothing_to_do: | |
542b1444 | 1686 | scmd->result = SAM_STAT_GOOD; |
ae006510 | 1687 | return 1; |
1da177e4 LT |
1688 | } |
1689 | ||
77853bf2 | 1690 | static void ata_scsi_qc_complete(struct ata_queued_cmd *qc) |
1da177e4 | 1691 | { |
c31f571d | 1692 | struct ata_port *ap = qc->ap; |
1da177e4 | 1693 | struct scsi_cmnd *cmd = qc->scsicmd; |
a7dac447 | 1694 | u8 *cdb = cmd->cmnd; |
2dcb407e | 1695 | int need_sense = (qc->err_mask != 0); |
b095518e JG |
1696 | |
1697 | /* For ATA pass thru (SAT) commands, generate a sense block if | |
1698 | * user mandated it or if there's an error. Note that if we | |
1699 | * generate because the user forced us to, a check condition | |
1700 | * is generated and the ATA register values are returned | |
1701 | * whether the command completed successfully or not. If there | |
1702 | * was no error, SK, ASC and ASCQ will all be zero. | |
1703 | */ | |
a7dac447 | 1704 | if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) && |
2dcb407e | 1705 | ((cdb[2] & 0x20) || need_sense)) { |
750426aa | 1706 | ata_gen_passthru_sense(qc); |
b095518e JG |
1707 | } else { |
1708 | if (!need_sense) { | |
1709 | cmd->result = SAM_STAT_GOOD; | |
1710 | } else { | |
1711 | /* TODO: decide which descriptor format to use | |
1712 | * for 48b LBA devices and call that here | |
1713 | * instead of the fixed desc, which is only | |
1714 | * good for smaller LBA (and maybe CHS?) | |
1715 | * devices. | |
1716 | */ | |
750426aa | 1717 | ata_gen_ata_sense(qc); |
b095518e JG |
1718 | } |
1719 | } | |
1da177e4 | 1720 | |
c31f571d | 1721 | if (need_sense && !ap->ops->error_handler) |
44877b4e | 1722 | ata_dump_status(ap->print_id, &qc->result_tf); |
1da177e4 LT |
1723 | |
1724 | qc->scsidone(cmd); | |
1725 | ||
77853bf2 | 1726 | ata_qc_free(qc); |
1da177e4 LT |
1727 | } |
1728 | ||
1729 | /** | |
1730 | * ata_scsi_translate - Translate then issue SCSI command to ATA device | |
1da177e4 LT |
1731 | * @dev: ATA device to which the command is addressed |
1732 | * @cmd: SCSI command to execute | |
1733 | * @done: SCSI command completion function | |
1734 | * @xlat_func: Actor which translates @cmd to an ATA taskfile | |
1735 | * | |
1736 | * Our ->queuecommand() function has decided that the SCSI | |
1737 | * command issued can be directly translated into an ATA | |
1738 | * command, rather than handled internally. | |
1739 | * | |
1740 | * This function sets up an ata_queued_cmd structure for the | |
1741 | * SCSI command, and sends that ata_queued_cmd to the hardware. | |
1742 | * | |
ae006510 DG |
1743 | * The xlat_func argument (actor) returns 0 if ready to execute |
1744 | * ATA command, else 1 to finish translation. If 1 is returned | |
1745 | * then cmd->result (and possibly cmd->sense_buffer) are assumed | |
1746 | * to be set reflecting an error condition or clean (early) | |
1747 | * termination. | |
1748 | * | |
1da177e4 | 1749 | * LOCKING: |
cca3974e | 1750 | * spin_lock_irqsave(host lock) |
2115ea94 TH |
1751 | * |
1752 | * RETURNS: | |
1753 | * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command | |
1754 | * needs to be deferred. | |
1da177e4 | 1755 | */ |
2115ea94 TH |
1756 | static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd, |
1757 | void (*done)(struct scsi_cmnd *), | |
1758 | ata_xlat_func_t xlat_func) | |
1da177e4 | 1759 | { |
31cc23b3 | 1760 | struct ata_port *ap = dev->link->ap; |
1da177e4 | 1761 | struct ata_queued_cmd *qc; |
31cc23b3 | 1762 | int rc; |
1da177e4 LT |
1763 | |
1764 | VPRINTK("ENTER\n"); | |
1765 | ||
3373efd8 | 1766 | qc = ata_scsi_qc_new(dev, cmd, done); |
1da177e4 | 1767 | if (!qc) |
ae006510 | 1768 | goto err_mem; |
1da177e4 LT |
1769 | |
1770 | /* data is present; dma-map it */ | |
be7db055 CH |
1771 | if (cmd->sc_data_direction == DMA_FROM_DEVICE || |
1772 | cmd->sc_data_direction == DMA_TO_DEVICE) { | |
7120165c | 1773 | if (unlikely(scsi_bufflen(cmd) < 1)) { |
f15a1daf TH |
1774 | ata_dev_printk(dev, KERN_WARNING, |
1775 | "WARNING: zero len r/w req\n"); | |
ae006510 | 1776 | goto err_did; |
1da177e4 LT |
1777 | } |
1778 | ||
7120165c | 1779 | ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd)); |
1da177e4 LT |
1780 | |
1781 | qc->dma_dir = cmd->sc_data_direction; | |
1782 | } | |
1783 | ||
1784 | qc->complete_fn = ata_scsi_qc_complete; | |
1785 | ||
ad706991 | 1786 | if (xlat_func(qc)) |
ae006510 | 1787 | goto early_finish; |
1da177e4 | 1788 | |
31cc23b3 TH |
1789 | if (ap->ops->qc_defer) { |
1790 | if ((rc = ap->ops->qc_defer(qc))) | |
1791 | goto defer; | |
1792 | } | |
1793 | ||
1da177e4 | 1794 | /* select device, send command to hardware */ |
8e0e694a | 1795 | ata_qc_issue(qc); |
1da177e4 LT |
1796 | |
1797 | VPRINTK("EXIT\n"); | |
2115ea94 | 1798 | return 0; |
1da177e4 | 1799 | |
ae006510 | 1800 | early_finish: |
2dcb407e | 1801 | ata_qc_free(qc); |
da071b42 | 1802 | qc->scsidone(cmd); |
ae006510 | 1803 | DPRINTK("EXIT - early finish (good or error)\n"); |
2115ea94 | 1804 | return 0; |
ae006510 DG |
1805 | |
1806 | err_did: | |
1da177e4 | 1807 | ata_qc_free(qc); |
ae006510 | 1808 | cmd->result = (DID_ERROR << 16); |
da071b42 | 1809 | qc->scsidone(cmd); |
253b92ec | 1810 | err_mem: |
ae006510 | 1811 | DPRINTK("EXIT - internal\n"); |
2115ea94 | 1812 | return 0; |
3dc1d881 TH |
1813 | |
1814 | defer: | |
31cc23b3 | 1815 | ata_qc_free(qc); |
3dc1d881 | 1816 | DPRINTK("EXIT - defer\n"); |
31cc23b3 TH |
1817 | if (rc == ATA_DEFER_LINK) |
1818 | return SCSI_MLQUEUE_DEVICE_BUSY; | |
1819 | else | |
1820 | return SCSI_MLQUEUE_HOST_BUSY; | |
1da177e4 LT |
1821 | } |
1822 | ||
1823 | /** | |
1824 | * ata_scsi_rbuf_get - Map response buffer. | |
ec2a20e6 | 1825 | * @cmd: SCSI command containing buffer to be mapped. |
87340e98 TH |
1826 | * @flags: unsigned long variable to store irq enable status |
1827 | * @copy_in: copy in from user buffer | |
1da177e4 | 1828 | * |
87340e98 | 1829 | * Prepare buffer for simulated SCSI commands. |
1da177e4 LT |
1830 | * |
1831 | * LOCKING: | |
87340e98 | 1832 | * spin_lock_irqsave(ata_scsi_rbuf_lock) on success |
1da177e4 LT |
1833 | * |
1834 | * RETURNS: | |
87340e98 | 1835 | * Pointer to response buffer. |
1da177e4 | 1836 | */ |
87340e98 TH |
1837 | static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in, |
1838 | unsigned long *flags) | |
1da177e4 | 1839 | { |
87340e98 | 1840 | spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags); |
1da177e4 | 1841 | |
87340e98 TH |
1842 | memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE); |
1843 | if (copy_in) | |
1844 | sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), | |
1845 | ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE); | |
1846 | return ata_scsi_rbuf; | |
1da177e4 LT |
1847 | } |
1848 | ||
1849 | /** | |
1850 | * ata_scsi_rbuf_put - Unmap response buffer. | |
1851 | * @cmd: SCSI command containing buffer to be unmapped. | |
87340e98 TH |
1852 | * @copy_out: copy out result |
1853 | * @flags: @flags passed to ata_scsi_rbuf_get() | |
1da177e4 | 1854 | * |
87340e98 TH |
1855 | * Returns rbuf buffer. The result is copied to @cmd's buffer if |
1856 | * @copy_back is true. | |
1da177e4 LT |
1857 | * |
1858 | * LOCKING: | |
87340e98 | 1859 | * Unlocks ata_scsi_rbuf_lock. |
1da177e4 | 1860 | */ |
87340e98 TH |
1861 | static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out, |
1862 | unsigned long *flags) | |
1da177e4 | 1863 | { |
87340e98 TH |
1864 | if (copy_out) |
1865 | sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), | |
1866 | ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE); | |
1867 | spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags); | |
1da177e4 LT |
1868 | } |
1869 | ||
1870 | /** | |
1871 | * ata_scsi_rbuf_fill - wrapper for SCSI command simulators | |
1872 | * @args: device IDENTIFY data / SCSI command of interest. | |
1873 | * @actor: Callback hook for desired SCSI command simulator | |
1874 | * | |
1875 | * Takes care of the hard work of simulating a SCSI command... | |
1876 | * Mapping the response buffer, calling the command's handler, | |
1877 | * and handling the handler's return value. This return value | |
1878 | * indicates whether the handler wishes the SCSI command to be | |
ae006510 DG |
1879 | * completed successfully (0), or not (in which case cmd->result |
1880 | * and sense buffer are assumed to be set). | |
1da177e4 LT |
1881 | * |
1882 | * LOCKING: | |
cca3974e | 1883 | * spin_lock_irqsave(host lock) |
1da177e4 | 1884 | */ |
f0761be3 | 1885 | static void ata_scsi_rbuf_fill(struct ata_scsi_args *args, |
87340e98 | 1886 | unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf)) |
1da177e4 LT |
1887 | { |
1888 | u8 *rbuf; | |
87340e98 | 1889 | unsigned int rc; |
1da177e4 | 1890 | struct scsi_cmnd *cmd = args->cmd; |
b445c568 JG |
1891 | unsigned long flags; |
1892 | ||
87340e98 TH |
1893 | rbuf = ata_scsi_rbuf_get(cmd, false, &flags); |
1894 | rc = actor(args, rbuf); | |
1895 | ata_scsi_rbuf_put(cmd, rc == 0, &flags); | |
b445c568 | 1896 | |
ae006510 | 1897 | if (rc == 0) |
1da177e4 | 1898 | cmd->result = SAM_STAT_GOOD; |
ae006510 | 1899 | args->done(cmd); |
1da177e4 LT |
1900 | } |
1901 | ||
1902 | /** | |
1903 | * ata_scsiop_inq_std - Simulate INQUIRY command | |
1904 | * @args: device IDENTIFY data / SCSI command of interest. | |
1905 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 LT |
1906 | * |
1907 | * Returns standard device identification data associated | |
b142eb65 | 1908 | * with non-VPD INQUIRY command output. |
1da177e4 LT |
1909 | * |
1910 | * LOCKING: | |
cca3974e | 1911 | * spin_lock_irqsave(host lock) |
1da177e4 | 1912 | */ |
87340e98 | 1913 | static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 | 1914 | { |
87340e98 TH |
1915 | const u8 versions[] = { |
1916 | 0x60, /* SAM-3 (no version claimed) */ | |
1917 | ||
1918 | 0x03, | |
1919 | 0x20, /* SBC-2 (no version claimed) */ | |
1920 | ||
1921 | 0x02, | |
1922 | 0x60 /* SPC-3 (no version claimed) */ | |
1923 | }; | |
1da177e4 LT |
1924 | u8 hdr[] = { |
1925 | TYPE_DISK, | |
1926 | 0, | |
1927 | 0x5, /* claim SPC-3 version compatibility */ | |
1928 | 2, | |
1929 | 95 - 4 | |
1930 | }; | |
1931 | ||
87340e98 TH |
1932 | VPRINTK("ENTER\n"); |
1933 | ||
1da177e4 LT |
1934 | /* set scsi removeable (RMB) bit per ata bit */ |
1935 | if (ata_id_removeable(args->id)) | |
1936 | hdr[1] |= (1 << 7); | |
1937 | ||
1da177e4 | 1938 | memcpy(rbuf, hdr, sizeof(hdr)); |
87340e98 TH |
1939 | memcpy(&rbuf[8], "ATA ", 8); |
1940 | ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16); | |
1941 | ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4); | |
1da177e4 | 1942 | |
87340e98 TH |
1943 | if (rbuf[32] == 0 || rbuf[32] == ' ') |
1944 | memcpy(&rbuf[32], "n/a ", 4); | |
1da177e4 | 1945 | |
87340e98 | 1946 | memcpy(rbuf + 59, versions, sizeof(versions)); |
1da177e4 LT |
1947 | |
1948 | return 0; | |
1949 | } | |
1950 | ||
1951 | /** | |
b142eb65 | 1952 | * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages |
1da177e4 LT |
1953 | * @args: device IDENTIFY data / SCSI command of interest. |
1954 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 | 1955 | * |
b142eb65 | 1956 | * Returns list of inquiry VPD pages available. |
1da177e4 LT |
1957 | * |
1958 | * LOCKING: | |
cca3974e | 1959 | * spin_lock_irqsave(host lock) |
1da177e4 | 1960 | */ |
87340e98 | 1961 | static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 LT |
1962 | { |
1963 | const u8 pages[] = { | |
1964 | 0x00, /* page 0x00, this page */ | |
1965 | 0x80, /* page 0x80, unit serial no page */ | |
1e9dbc92 MW |
1966 | 0x83, /* page 0x83, device ident page */ |
1967 | 0x89, /* page 0x89, ata info page */ | |
18f0f978 | 1968 | 0xb0, /* page 0xb0, block limits page */ |
1e9dbc92 | 1969 | 0xb1, /* page 0xb1, block device characteristics page */ |
1da177e4 | 1970 | }; |
1da177e4 | 1971 | |
87340e98 TH |
1972 | rbuf[3] = sizeof(pages); /* number of supported VPD pages */ |
1973 | memcpy(rbuf + 4, pages, sizeof(pages)); | |
1da177e4 LT |
1974 | return 0; |
1975 | } | |
1976 | ||
1977 | /** | |
b142eb65 | 1978 | * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number |
1da177e4 LT |
1979 | * @args: device IDENTIFY data / SCSI command of interest. |
1980 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 LT |
1981 | * |
1982 | * Returns ATA device serial number. | |
1983 | * | |
1984 | * LOCKING: | |
cca3974e | 1985 | * spin_lock_irqsave(host lock) |
1da177e4 | 1986 | */ |
87340e98 | 1987 | static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 LT |
1988 | { |
1989 | const u8 hdr[] = { | |
1990 | 0, | |
1991 | 0x80, /* this page code */ | |
1992 | 0, | |
a0cf733b | 1993 | ATA_ID_SERNO_LEN, /* page len */ |
1da177e4 | 1994 | }; |
1da177e4 | 1995 | |
87340e98 TH |
1996 | memcpy(rbuf, hdr, sizeof(hdr)); |
1997 | ata_id_string(args->id, (unsigned char *) &rbuf[4], | |
1998 | ATA_ID_SERNO, ATA_ID_SERNO_LEN); | |
1da177e4 LT |
1999 | return 0; |
2000 | } | |
2001 | ||
1da177e4 | 2002 | /** |
b142eb65 | 2003 | * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity |
1da177e4 LT |
2004 | * @args: device IDENTIFY data / SCSI command of interest. |
2005 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 | 2006 | * |
b142eb65 JG |
2007 | * Yields two logical unit device identification designators: |
2008 | * - vendor specific ASCII containing the ATA serial number | |
2009 | * - SAT defined "t10 vendor id based" containing ASCII vendor | |
2010 | * name ("ATA "), model and serial numbers. | |
1da177e4 LT |
2011 | * |
2012 | * LOCKING: | |
cca3974e | 2013 | * spin_lock_irqsave(host lock) |
1da177e4 | 2014 | */ |
87340e98 | 2015 | static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 | 2016 | { |
b142eb65 | 2017 | const int sat_model_serial_desc_len = 68; |
87340e98 | 2018 | int num; |
1da177e4 | 2019 | |
b142eb65 JG |
2020 | rbuf[1] = 0x83; /* this page code */ |
2021 | num = 4; | |
2022 | ||
87340e98 TH |
2023 | /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */ |
2024 | rbuf[num + 0] = 2; | |
2025 | rbuf[num + 3] = ATA_ID_SERNO_LEN; | |
2026 | num += 4; | |
2027 | ata_id_string(args->id, (unsigned char *) rbuf + num, | |
2028 | ATA_ID_SERNO, ATA_ID_SERNO_LEN); | |
2029 | num += ATA_ID_SERNO_LEN; | |
2030 | ||
2031 | /* SAT defined lu model and serial numbers descriptor */ | |
2032 | /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */ | |
2033 | rbuf[num + 0] = 2; | |
2034 | rbuf[num + 1] = 1; | |
2035 | rbuf[num + 3] = sat_model_serial_desc_len; | |
2036 | num += 4; | |
2037 | memcpy(rbuf + num, "ATA ", 8); | |
2038 | num += 8; | |
2039 | ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD, | |
2040 | ATA_ID_PROD_LEN); | |
2041 | num += ATA_ID_PROD_LEN; | |
2042 | ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO, | |
2043 | ATA_ID_SERNO_LEN); | |
2044 | num += ATA_ID_SERNO_LEN; | |
2045 | ||
b142eb65 | 2046 | rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */ |
1da177e4 LT |
2047 | return 0; |
2048 | } | |
2049 | ||
ad355b46 JG |
2050 | /** |
2051 | * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info | |
2052 | * @args: device IDENTIFY data / SCSI command of interest. | |
2053 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
ad355b46 JG |
2054 | * |
2055 | * Yields SAT-specified ATA VPD page. | |
2056 | * | |
2057 | * LOCKING: | |
2058 | * spin_lock_irqsave(host lock) | |
2059 | */ | |
87340e98 | 2060 | static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf) |
ad355b46 | 2061 | { |
ad355b46 | 2062 | struct ata_taskfile tf; |
ad355b46 | 2063 | |
ad355b46 JG |
2064 | memset(&tf, 0, sizeof(tf)); |
2065 | ||
87340e98 TH |
2066 | rbuf[1] = 0x89; /* our page code */ |
2067 | rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */ | |
2068 | rbuf[3] = (0x238 & 0xff); | |
ad355b46 | 2069 | |
87340e98 TH |
2070 | memcpy(&rbuf[8], "linux ", 8); |
2071 | memcpy(&rbuf[16], "libata ", 16); | |
2072 | memcpy(&rbuf[32], DRV_VERSION, 4); | |
2073 | ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4); | |
ad355b46 JG |
2074 | |
2075 | /* we don't store the ATA device signature, so we fake it */ | |
2076 | ||
2077 | tf.command = ATA_DRDY; /* really, this is Status reg */ | |
2078 | tf.lbal = 0x1; | |
2079 | tf.nsect = 0x1; | |
2080 | ||
87340e98 TH |
2081 | ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */ |
2082 | rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */ | |
ad355b46 | 2083 | |
87340e98 | 2084 | rbuf[56] = ATA_CMD_ID_ATA; |
ad355b46 | 2085 | |
87340e98 | 2086 | memcpy(&rbuf[60], &args->id[0], 512); |
ad355b46 JG |
2087 | return 0; |
2088 | } | |
2089 | ||
18f0f978 CH |
2090 | static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf) |
2091 | { | |
295124dc | 2092 | u16 min_io_sectors; |
18f0f978 CH |
2093 | |
2094 | rbuf[1] = 0xb0; | |
2095 | rbuf[3] = 0x3c; /* required VPD size with unmap support */ | |
2096 | ||
2097 | /* | |
2098 | * Optimal transfer length granularity. | |
2099 | * | |
2100 | * This is always one physical block, but for disks with a smaller | |
2101 | * logical than physical sector size we need to figure out what the | |
2102 | * latter is. | |
2103 | */ | |
295124dc | 2104 | min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id); |
18f0f978 CH |
2105 | put_unaligned_be16(min_io_sectors, &rbuf[6]); |
2106 | ||
2107 | /* | |
2108 | * Optimal unmap granularity. | |
2109 | * | |
2110 | * The ATA spec doesn't even know about a granularity or alignment | |
2111 | * for the TRIM command. We can leave away most of the unmap related | |
2112 | * VPD page entries, but we have specifify a granularity to signal | |
2113 | * that we support some form of unmap - in thise case via WRITE SAME | |
2114 | * with the unmap bit set. | |
2115 | */ | |
e78db4df MP |
2116 | if (ata_id_has_trim(args->id)) { |
2117 | put_unaligned_be32(65535 * 512 / 8, &rbuf[20]); | |
18f0f978 | 2118 | put_unaligned_be32(1, &rbuf[28]); |
e78db4df | 2119 | } |
18f0f978 CH |
2120 | |
2121 | return 0; | |
2122 | } | |
2123 | ||
1e9dbc92 MW |
2124 | static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf) |
2125 | { | |
4bca3286 MP |
2126 | int form_factor = ata_id_form_factor(args->id); |
2127 | int media_rotation_rate = ata_id_rotation_rate(args->id); | |
2128 | ||
1e9dbc92 MW |
2129 | rbuf[1] = 0xb1; |
2130 | rbuf[3] = 0x3c; | |
4bca3286 MP |
2131 | rbuf[4] = media_rotation_rate >> 8; |
2132 | rbuf[5] = media_rotation_rate; | |
2133 | rbuf[7] = form_factor; | |
1e9dbc92 MW |
2134 | |
2135 | return 0; | |
2136 | } | |
2137 | ||
1da177e4 | 2138 | /** |
0cba632b | 2139 | * ata_scsiop_noop - Command handler that simply returns success. |
1da177e4 LT |
2140 | * @args: device IDENTIFY data / SCSI command of interest. |
2141 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 LT |
2142 | * |
2143 | * No operation. Simply returns success to caller, to indicate | |
2144 | * that the caller should successfully complete this SCSI command. | |
2145 | * | |
2146 | * LOCKING: | |
cca3974e | 2147 | * spin_lock_irqsave(host lock) |
1da177e4 | 2148 | */ |
87340e98 | 2149 | static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 LT |
2150 | { |
2151 | VPRINTK("ENTER\n"); | |
2152 | return 0; | |
2153 | } | |
2154 | ||
1da177e4 LT |
2155 | /** |
2156 | * ata_msense_caching - Simulate MODE SENSE caching info page | |
2157 | * @id: device IDENTIFY data | |
87340e98 | 2158 | * @buf: output buffer |
1da177e4 LT |
2159 | * |
2160 | * Generate a caching info page, which conditionally indicates | |
2161 | * write caching to the SCSI layer, depending on device | |
2162 | * capabilities. | |
2163 | * | |
2164 | * LOCKING: | |
2165 | * None. | |
2166 | */ | |
87340e98 | 2167 | static unsigned int ata_msense_caching(u16 *id, u8 *buf) |
1da177e4 | 2168 | { |
87340e98 | 2169 | memcpy(buf, def_cache_mpage, sizeof(def_cache_mpage)); |
1da177e4 | 2170 | if (ata_id_wcache_enabled(id)) |
87340e98 | 2171 | buf[2] |= (1 << 2); /* write cache enable */ |
1da177e4 | 2172 | if (!ata_id_rahead_enabled(id)) |
87340e98 TH |
2173 | buf[12] |= (1 << 5); /* disable read ahead */ |
2174 | return sizeof(def_cache_mpage); | |
1da177e4 LT |
2175 | } |
2176 | ||
2177 | /** | |
2178 | * ata_msense_ctl_mode - Simulate MODE SENSE control mode page | |
87340e98 | 2179 | * @buf: output buffer |
1da177e4 LT |
2180 | * |
2181 | * Generate a generic MODE SENSE control mode page. | |
2182 | * | |
2183 | * LOCKING: | |
2184 | * None. | |
2185 | */ | |
87340e98 | 2186 | static unsigned int ata_msense_ctl_mode(u8 *buf) |
1da177e4 | 2187 | { |
87340e98 | 2188 | memcpy(buf, def_control_mpage, sizeof(def_control_mpage)); |
00ac37f5 | 2189 | return sizeof(def_control_mpage); |
1da177e4 LT |
2190 | } |
2191 | ||
2192 | /** | |
2193 | * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page | |
ec2a20e6 | 2194 | * @buf: output buffer |
1da177e4 LT |
2195 | * |
2196 | * Generate a generic MODE SENSE r/w error recovery page. | |
2197 | * | |
2198 | * LOCKING: | |
2199 | * None. | |
2200 | */ | |
87340e98 | 2201 | static unsigned int ata_msense_rw_recovery(u8 *buf) |
1da177e4 | 2202 | { |
87340e98 | 2203 | memcpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage)); |
00ac37f5 | 2204 | return sizeof(def_rw_recovery_mpage); |
1da177e4 LT |
2205 | } |
2206 | ||
48bdc8ec JA |
2207 | /* |
2208 | * We can turn this into a real blacklist if it's needed, for now just | |
2209 | * blacklist any Maxtor BANC1G10 revision firmware | |
2210 | */ | |
2211 | static int ata_dev_supports_fua(u16 *id) | |
2212 | { | |
a0cf733b | 2213 | unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1]; |
48bdc8ec | 2214 | |
c3c013a2 JG |
2215 | if (!libata_fua) |
2216 | return 0; | |
48bdc8ec JA |
2217 | if (!ata_id_has_fua(id)) |
2218 | return 0; | |
2219 | ||
a0cf733b TH |
2220 | ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model)); |
2221 | ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw)); | |
48bdc8ec | 2222 | |
2e02671d | 2223 | if (strcmp(model, "Maxtor")) |
48bdc8ec | 2224 | return 1; |
2e02671d | 2225 | if (strcmp(fw, "BANC1G10")) |
48bdc8ec JA |
2226 | return 1; |
2227 | ||
2228 | return 0; /* blacklisted */ | |
2229 | } | |
2230 | ||
1da177e4 LT |
2231 | /** |
2232 | * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands | |
2233 | * @args: device IDENTIFY data / SCSI command of interest. | |
2234 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 | 2235 | * |
00ac37f5 DG |
2236 | * Simulate MODE SENSE commands. Assume this is invoked for direct |
2237 | * access devices (e.g. disks) only. There should be no block | |
2238 | * descriptor for other device types. | |
1da177e4 LT |
2239 | * |
2240 | * LOCKING: | |
cca3974e | 2241 | * spin_lock_irqsave(host lock) |
1da177e4 | 2242 | */ |
87340e98 | 2243 | static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 | 2244 | { |
9a3dccc4 | 2245 | struct ata_device *dev = args->dev; |
87340e98 | 2246 | u8 *scsicmd = args->cmd->cmnd, *p = rbuf; |
00ac37f5 DG |
2247 | const u8 sat_blk_desc[] = { |
2248 | 0, 0, 0, 0, /* number of blocks: sat unspecified */ | |
2249 | 0, | |
2250 | 0, 0x2, 0x0 /* block length: 512 bytes */ | |
2251 | }; | |
2252 | u8 pg, spg; | |
87340e98 | 2253 | unsigned int ebd, page_control, six_byte; |
9a3dccc4 | 2254 | u8 dpofua; |
1da177e4 LT |
2255 | |
2256 | VPRINTK("ENTER\n"); | |
2257 | ||
2258 | six_byte = (scsicmd[0] == MODE_SENSE); | |
00ac37f5 DG |
2259 | ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */ |
2260 | /* | |
2261 | * LLBA bit in msense(10) ignored (compliant) | |
1da177e4 | 2262 | */ |
00ac37f5 | 2263 | |
1da177e4 | 2264 | page_control = scsicmd[2] >> 6; |
ae006510 DG |
2265 | switch (page_control) { |
2266 | case 0: /* current */ | |
2267 | break; /* supported */ | |
2268 | case 3: /* saved */ | |
2269 | goto saving_not_supp; | |
2270 | case 1: /* changeable */ | |
2271 | case 2: /* defaults */ | |
2272 | default: | |
2273 | goto invalid_fld; | |
2274 | } | |
1da177e4 | 2275 | |
87340e98 TH |
2276 | if (six_byte) |
2277 | p += 4 + (ebd ? 8 : 0); | |
2278 | else | |
2279 | p += 8 + (ebd ? 8 : 0); | |
1da177e4 | 2280 | |
00ac37f5 DG |
2281 | pg = scsicmd[2] & 0x3f; |
2282 | spg = scsicmd[3]; | |
2283 | /* | |
2284 | * No mode subpages supported (yet) but asking for _all_ | |
2285 | * subpages may be valid | |
2286 | */ | |
2287 | if (spg && (spg != ALL_SUB_MPAGES)) | |
2288 | goto invalid_fld; | |
2289 | ||
2290 | switch(pg) { | |
2291 | case RW_RECOVERY_MPAGE: | |
87340e98 | 2292 | p += ata_msense_rw_recovery(p); |
1da177e4 LT |
2293 | break; |
2294 | ||
00ac37f5 | 2295 | case CACHE_MPAGE: |
87340e98 | 2296 | p += ata_msense_caching(args->id, p); |
1da177e4 LT |
2297 | break; |
2298 | ||
87340e98 TH |
2299 | case CONTROL_MPAGE: |
2300 | p += ata_msense_ctl_mode(p); | |
1da177e4 | 2301 | break; |
1da177e4 | 2302 | |
00ac37f5 | 2303 | case ALL_MPAGES: |
87340e98 TH |
2304 | p += ata_msense_rw_recovery(p); |
2305 | p += ata_msense_caching(args->id, p); | |
2306 | p += ata_msense_ctl_mode(p); | |
1da177e4 LT |
2307 | break; |
2308 | ||
2309 | default: /* invalid page code */ | |
ae006510 | 2310 | goto invalid_fld; |
1da177e4 LT |
2311 | } |
2312 | ||
9a3dccc4 | 2313 | dpofua = 0; |
f79d409f | 2314 | if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) && |
9a3dccc4 TH |
2315 | (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count)) |
2316 | dpofua = 1 << 4; | |
2317 | ||
1da177e4 | 2318 | if (six_byte) { |
87340e98 TH |
2319 | rbuf[0] = p - rbuf - 1; |
2320 | rbuf[2] |= dpofua; | |
00ac37f5 | 2321 | if (ebd) { |
87340e98 TH |
2322 | rbuf[3] = sizeof(sat_blk_desc); |
2323 | memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc)); | |
00ac37f5 | 2324 | } |
1da177e4 | 2325 | } else { |
87340e98 TH |
2326 | unsigned int output_len = p - rbuf - 2; |
2327 | ||
1da177e4 | 2328 | rbuf[0] = output_len >> 8; |
87340e98 TH |
2329 | rbuf[1] = output_len; |
2330 | rbuf[3] |= dpofua; | |
00ac37f5 | 2331 | if (ebd) { |
87340e98 TH |
2332 | rbuf[7] = sizeof(sat_blk_desc); |
2333 | memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc)); | |
00ac37f5 | 2334 | } |
1da177e4 | 2335 | } |
1da177e4 | 2336 | return 0; |
ae006510 DG |
2337 | |
2338 | invalid_fld: | |
2339 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0); | |
2340 | /* "Invalid field in cbd" */ | |
2341 | return 1; | |
2342 | ||
2343 | saving_not_supp: | |
2344 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0); | |
2345 | /* "Saving parameters not supported" */ | |
2346 | return 1; | |
1da177e4 LT |
2347 | } |
2348 | ||
2349 | /** | |
2350 | * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands | |
2351 | * @args: device IDENTIFY data / SCSI command of interest. | |
2352 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 LT |
2353 | * |
2354 | * Simulate READ CAPACITY commands. | |
2355 | * | |
2356 | * LOCKING: | |
6a36261e | 2357 | * None. |
1da177e4 | 2358 | */ |
87340e98 | 2359 | static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 | 2360 | { |
61d79a8e MP |
2361 | struct ata_device *dev = args->dev; |
2362 | u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */ | |
295124dc GG |
2363 | u32 sector_size; /* physical sector size in bytes */ |
2364 | u8 log2_per_phys; | |
2365 | u16 lowest_aligned; | |
2366 | ||
2367 | sector_size = ata_id_logical_sector_size(dev->id); | |
2368 | log2_per_phys = ata_id_log2_per_physical_sector(dev->id); | |
2369 | lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys); | |
1da177e4 LT |
2370 | |
2371 | VPRINTK("ENTER\n"); | |
2372 | ||
1da177e4 | 2373 | if (args->cmd->cmnd[0] == READ_CAPACITY) { |
6a36261e TH |
2374 | if (last_lba >= 0xffffffffULL) |
2375 | last_lba = 0xffffffff; | |
0c144d0d | 2376 | |
1da177e4 | 2377 | /* sector count, 32-bit */ |
87340e98 TH |
2378 | rbuf[0] = last_lba >> (8 * 3); |
2379 | rbuf[1] = last_lba >> (8 * 2); | |
2380 | rbuf[2] = last_lba >> (8 * 1); | |
2381 | rbuf[3] = last_lba; | |
1da177e4 LT |
2382 | |
2383 | /* sector size */ | |
295124dc GG |
2384 | rbuf[4] = sector_size >> (8 * 3); |
2385 | rbuf[5] = sector_size >> (8 * 2); | |
2386 | rbuf[6] = sector_size >> (8 * 1); | |
2387 | rbuf[7] = sector_size; | |
1da177e4 LT |
2388 | } else { |
2389 | /* sector count, 64-bit */ | |
87340e98 TH |
2390 | rbuf[0] = last_lba >> (8 * 7); |
2391 | rbuf[1] = last_lba >> (8 * 6); | |
2392 | rbuf[2] = last_lba >> (8 * 5); | |
2393 | rbuf[3] = last_lba >> (8 * 4); | |
2394 | rbuf[4] = last_lba >> (8 * 3); | |
2395 | rbuf[5] = last_lba >> (8 * 2); | |
2396 | rbuf[6] = last_lba >> (8 * 1); | |
2397 | rbuf[7] = last_lba; | |
1da177e4 LT |
2398 | |
2399 | /* sector size */ | |
295124dc GG |
2400 | rbuf[ 8] = sector_size >> (8 * 3); |
2401 | rbuf[ 9] = sector_size >> (8 * 2); | |
2402 | rbuf[10] = sector_size >> (8 * 1); | |
2403 | rbuf[11] = sector_size; | |
61d79a8e MP |
2404 | |
2405 | rbuf[12] = 0; | |
295124dc | 2406 | rbuf[13] = log2_per_phys; |
61d79a8e MP |
2407 | rbuf[14] = (lowest_aligned >> 8) & 0x3f; |
2408 | rbuf[15] = lowest_aligned; | |
18f0f978 | 2409 | |
e78db4df MP |
2410 | if (ata_id_has_trim(args->id)) { |
2411 | rbuf[14] |= 0x80; /* TPE */ | |
2412 | ||
2413 | if (ata_id_has_zero_after_trim(args->id)) | |
2414 | rbuf[14] |= 0x40; /* TPRZ */ | |
2415 | } | |
1da177e4 LT |
2416 | } |
2417 | ||
2418 | return 0; | |
2419 | } | |
2420 | ||
2421 | /** | |
2422 | * ata_scsiop_report_luns - Simulate REPORT LUNS command | |
2423 | * @args: device IDENTIFY data / SCSI command of interest. | |
2424 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1da177e4 LT |
2425 | * |
2426 | * Simulate REPORT LUNS command. | |
2427 | * | |
2428 | * LOCKING: | |
cca3974e | 2429 | * spin_lock_irqsave(host lock) |
1da177e4 | 2430 | */ |
87340e98 | 2431 | static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf) |
1da177e4 LT |
2432 | { |
2433 | VPRINTK("ENTER\n"); | |
2434 | rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */ | |
2435 | ||
2436 | return 0; | |
2437 | } | |
2438 | ||
77853bf2 | 2439 | static void atapi_sense_complete(struct ata_queued_cmd *qc) |
a939c963 | 2440 | { |
74e6c8c3 | 2441 | if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) { |
c6e6e666 JG |
2442 | /* FIXME: not quite right; we don't want the |
2443 | * translation of taskfile registers into | |
2444 | * a sense descriptors, since that's only | |
2445 | * correct for ATA, not ATAPI | |
2446 | */ | |
750426aa | 2447 | ata_gen_passthru_sense(qc); |
74e6c8c3 | 2448 | } |
a939c963 | 2449 | |
c6e6e666 | 2450 | qc->scsidone(qc->scsicmd); |
77853bf2 | 2451 | ata_qc_free(qc); |
c6e6e666 | 2452 | } |
a939c963 | 2453 | |
c6e6e666 JG |
2454 | /* is it pointless to prefer PIO for "safety reasons"? */ |
2455 | static inline int ata_pio_use_silly(struct ata_port *ap) | |
2456 | { | |
2457 | return (ap->flags & ATA_FLAG_PIO_DMA); | |
2458 | } | |
2459 | ||
2460 | static void atapi_request_sense(struct ata_queued_cmd *qc) | |
2461 | { | |
2462 | struct ata_port *ap = qc->ap; | |
2463 | struct scsi_cmnd *cmd = qc->scsicmd; | |
2464 | ||
2465 | DPRINTK("ATAPI request sense\n"); | |
a939c963 JG |
2466 | |
2467 | /* FIXME: is this needed? */ | |
7ccd720d | 2468 | memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE); |
a939c963 | 2469 | |
127102ae | 2470 | #ifdef CONFIG_ATA_SFF |
855d854a JB |
2471 | if (ap->ops->sff_tf_read) |
2472 | ap->ops->sff_tf_read(ap, &qc->tf); | |
127102ae | 2473 | #endif |
c6e6e666 JG |
2474 | |
2475 | /* fill these in, for the case where they are -not- overwritten */ | |
2476 | cmd->sense_buffer[0] = 0x70; | |
2477 | cmd->sense_buffer[2] = qc->tf.feature >> 4; | |
2478 | ||
2479 | ata_qc_reinit(qc); | |
2480 | ||
93f8fecb | 2481 | /* setup sg table and init transfer direction */ |
cadb7345 | 2482 | sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE); |
93f8fecb | 2483 | ata_sg_init(qc, &qc->sgent, 1); |
a939c963 JG |
2484 | qc->dma_dir = DMA_FROM_DEVICE; |
2485 | ||
6e7846e9 | 2486 | memset(&qc->cdb, 0, qc->dev->cdb_len); |
a939c963 JG |
2487 | qc->cdb[0] = REQUEST_SENSE; |
2488 | qc->cdb[4] = SCSI_SENSE_BUFFERSIZE; | |
2489 | ||
2490 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
2491 | qc->tf.command = ATA_CMD_PACKET; | |
2492 | ||
c6e6e666 | 2493 | if (ata_pio_use_silly(ap)) { |
0dc36888 | 2494 | qc->tf.protocol = ATAPI_PROT_DMA; |
c6e6e666 JG |
2495 | qc->tf.feature |= ATAPI_PKT_DMA; |
2496 | } else { | |
0dc36888 | 2497 | qc->tf.protocol = ATAPI_PROT_PIO; |
2db78dd3 AC |
2498 | qc->tf.lbam = SCSI_SENSE_BUFFERSIZE; |
2499 | qc->tf.lbah = 0; | |
c6e6e666 | 2500 | } |
a939c963 JG |
2501 | qc->nbytes = SCSI_SENSE_BUFFERSIZE; |
2502 | ||
c6e6e666 | 2503 | qc->complete_fn = atapi_sense_complete; |
a939c963 | 2504 | |
8e0e694a | 2505 | ata_qc_issue(qc); |
a939c963 JG |
2506 | |
2507 | DPRINTK("EXIT\n"); | |
2508 | } | |
2509 | ||
77853bf2 | 2510 | static void atapi_qc_complete(struct ata_queued_cmd *qc) |
1da177e4 LT |
2511 | { |
2512 | struct scsi_cmnd *cmd = qc->scsicmd; | |
a22e2eb0 | 2513 | unsigned int err_mask = qc->err_mask; |
1da177e4 | 2514 | |
a7dac447 | 2515 | VPRINTK("ENTER, err_mask 0x%X\n", err_mask); |
e12669e7 | 2516 | |
246619da TH |
2517 | /* handle completion from new EH */ |
2518 | if (unlikely(qc->ap->ops->error_handler && | |
2519 | (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) { | |
2520 | ||
2521 | if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) { | |
2522 | /* FIXME: not quite right; we don't want the | |
2523 | * translation of taskfile registers into a | |
2524 | * sense descriptors, since that's only | |
2525 | * correct for ATA, not ATAPI | |
2526 | */ | |
750426aa | 2527 | ata_gen_passthru_sense(qc); |
246619da TH |
2528 | } |
2529 | ||
22aac089 TH |
2530 | /* SCSI EH automatically locks door if sdev->locked is |
2531 | * set. Sometimes door lock request continues to | |
2532 | * fail, for example, when no media is present. This | |
2533 | * creates a loop - SCSI EH issues door lock which | |
2534 | * fails and gets invoked again to acquire sense data | |
2535 | * for the failed command. | |
2536 | * | |
2537 | * If door lock fails, always clear sdev->locked to | |
2538 | * avoid this infinite loop. | |
2539 | */ | |
2540 | if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL) | |
2541 | qc->dev->sdev->locked = 0; | |
2542 | ||
246619da TH |
2543 | qc->scsicmd->result = SAM_STAT_CHECK_CONDITION; |
2544 | qc->scsidone(cmd); | |
2545 | ata_qc_free(qc); | |
2546 | return; | |
2547 | } | |
2548 | ||
2549 | /* successful completion or old EH failure path */ | |
a7dac447 | 2550 | if (unlikely(err_mask & AC_ERR_DEV)) { |
1da177e4 | 2551 | cmd->result = SAM_STAT_CHECK_CONDITION; |
c6e6e666 | 2552 | atapi_request_sense(qc); |
77853bf2 | 2553 | return; |
74e6c8c3 | 2554 | } else if (unlikely(err_mask)) { |
a7dac447 JG |
2555 | /* FIXME: not quite right; we don't want the |
2556 | * translation of taskfile registers into | |
2557 | * a sense descriptors, since that's only | |
2558 | * correct for ATA, not ATAPI | |
2559 | */ | |
750426aa | 2560 | ata_gen_passthru_sense(qc); |
74e6c8c3 | 2561 | } else { |
1da177e4 LT |
2562 | u8 *scsicmd = cmd->cmnd; |
2563 | ||
fd71da46 | 2564 | if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) { |
b445c568 | 2565 | unsigned long flags; |
87340e98 | 2566 | u8 *buf; |
b445c568 | 2567 | |
87340e98 | 2568 | buf = ata_scsi_rbuf_get(cmd, true, &flags); |
a15dbeb4 JG |
2569 | |
2570 | /* ATAPI devices typically report zero for their SCSI version, | |
2571 | * and sometimes deviate from the spec WRT response data | |
2572 | * format. If SCSI version is reported as zero like normal, | |
2573 | * then we make the following fixups: 1) Fake MMC-5 version, | |
2574 | * to indicate to the Linux scsi midlayer this is a modern | |
2575 | * device. 2) Ensure response data format / ATAPI information | |
2576 | * are always correct. | |
a15dbeb4 JG |
2577 | */ |
2578 | if (buf[2] == 0) { | |
2579 | buf[2] = 0x5; | |
2580 | buf[3] = 0x32; | |
2581 | } | |
2582 | ||
87340e98 | 2583 | ata_scsi_rbuf_put(cmd, true, &flags); |
1da177e4 | 2584 | } |
a15dbeb4 | 2585 | |
1da177e4 LT |
2586 | cmd->result = SAM_STAT_GOOD; |
2587 | } | |
2588 | ||
2589 | qc->scsidone(cmd); | |
77853bf2 | 2590 | ata_qc_free(qc); |
1da177e4 LT |
2591 | } |
2592 | /** | |
2593 | * atapi_xlat - Initialize PACKET taskfile | |
2594 | * @qc: command structure to be initialized | |
1da177e4 LT |
2595 | * |
2596 | * LOCKING: | |
cca3974e | 2597 | * spin_lock_irqsave(host lock) |
1da177e4 LT |
2598 | * |
2599 | * RETURNS: | |
2600 | * Zero on success, non-zero on failure. | |
2601 | */ | |
ad706991 | 2602 | static unsigned int atapi_xlat(struct ata_queued_cmd *qc) |
1da177e4 | 2603 | { |
542b1444 | 2604 | struct scsi_cmnd *scmd = qc->scsicmd; |
1da177e4 | 2605 | struct ata_device *dev = qc->dev; |
542b1444 | 2606 | int nodata = (scmd->sc_data_direction == DMA_NONE); |
5895ef9a | 2607 | int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO); |
2db78dd3 | 2608 | unsigned int nbytes; |
1da177e4 | 2609 | |
2e5704f6 TH |
2610 | memset(qc->cdb, 0, dev->cdb_len); |
2611 | memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len); | |
1da177e4 LT |
2612 | |
2613 | qc->complete_fn = atapi_qc_complete; | |
2614 | ||
2615 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
542b1444 | 2616 | if (scmd->sc_data_direction == DMA_TO_DEVICE) { |
1da177e4 LT |
2617 | qc->tf.flags |= ATA_TFLAG_WRITE; |
2618 | DPRINTK("direction: write\n"); | |
2619 | } | |
2620 | ||
2621 | qc->tf.command = ATA_CMD_PACKET; | |
aacda375 | 2622 | ata_qc_set_pc_nbytes(qc); |
e00f1ff3 TH |
2623 | |
2624 | /* check whether ATAPI DMA is safe */ | |
5895ef9a | 2625 | if (!nodata && !using_pio && atapi_check_dma(qc)) |
e00f1ff3 | 2626 | using_pio = 1; |
1da177e4 | 2627 | |
e190222d TH |
2628 | /* Some controller variants snoop this value for Packet |
2629 | * transfers to do state machine and FIFO management. Thus we | |
2630 | * want to set it properly, and for DMA where it is | |
2631 | * effectively meaningless. | |
2632 | */ | |
aacda375 | 2633 | nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024); |
2db78dd3 | 2634 | |
e190222d TH |
2635 | /* Most ATAPI devices which honor transfer chunk size don't |
2636 | * behave according to the spec when odd chunk size which | |
2637 | * matches the transfer length is specified. If the number of | |
2638 | * bytes to transfer is 2n+1. According to the spec, what | |
2639 | * should happen is to indicate that 2n+1 is going to be | |
2640 | * transferred and transfer 2n+2 bytes where the last byte is | |
2641 | * padding. | |
2642 | * | |
2643 | * In practice, this doesn't happen. ATAPI devices first | |
2644 | * indicate and transfer 2n bytes and then indicate and | |
2645 | * transfer 2 bytes where the last byte is padding. | |
2646 | * | |
2647 | * This inconsistency confuses several controllers which | |
2648 | * perform PIO using DMA such as Intel AHCIs and sil3124/32. | |
2649 | * These controllers use actual number of transferred bytes to | |
2650 | * update DMA poitner and transfer of 4n+2 bytes make those | |
2651 | * controller push DMA pointer by 4n+4 bytes because SATA data | |
2652 | * FISes are aligned to 4 bytes. This causes data corruption | |
2653 | * and buffer overrun. | |
2654 | * | |
2655 | * Always setting nbytes to even number solves this problem | |
2656 | * because then ATAPI devices don't have to split data at 2n | |
2657 | * boundaries. | |
2658 | */ | |
2659 | if (nbytes & 0x1) | |
2660 | nbytes++; | |
2661 | ||
2db78dd3 AC |
2662 | qc->tf.lbam = (nbytes & 0xFF); |
2663 | qc->tf.lbah = (nbytes >> 8); | |
2664 | ||
5895ef9a TH |
2665 | if (nodata) |
2666 | qc->tf.protocol = ATAPI_PROT_NODATA; | |
2667 | else if (using_pio) | |
2668 | qc->tf.protocol = ATAPI_PROT_PIO; | |
2669 | else { | |
e00f1ff3 | 2670 | /* DMA data xfer */ |
0dc36888 | 2671 | qc->tf.protocol = ATAPI_PROT_DMA; |
1da177e4 LT |
2672 | qc->tf.feature |= ATAPI_PKT_DMA; |
2673 | ||
91163006 TH |
2674 | if ((dev->flags & ATA_DFLAG_DMADIR) && |
2675 | (scmd->sc_data_direction != DMA_TO_DEVICE)) | |
95de719a | 2676 | /* some SATA bridges need us to indicate data xfer direction */ |
1da177e4 | 2677 | qc->tf.feature |= ATAPI_DMADIR; |
1da177e4 LT |
2678 | } |
2679 | ||
2db78dd3 AC |
2680 | |
2681 | /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE | |
2682 | as ATAPI tape drives don't get this right otherwise */ | |
1da177e4 LT |
2683 | return 0; |
2684 | } | |
2685 | ||
2dcb407e | 2686 | static struct ata_device *ata_find_dev(struct ata_port *ap, int devno) |
ab5b3a5b | 2687 | { |
071f44b1 | 2688 | if (!sata_pmp_attached(ap)) { |
41bda9c9 TH |
2689 | if (likely(devno < ata_link_max_devices(&ap->link))) |
2690 | return &ap->link.device[devno]; | |
2691 | } else { | |
2692 | if (likely(devno < ap->nr_pmp_links)) | |
2693 | return &ap->pmp_link[devno].device[0]; | |
2694 | } | |
2695 | ||
ab5b3a5b TH |
2696 | return NULL; |
2697 | } | |
2698 | ||
2dcb407e JG |
2699 | static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap, |
2700 | const struct scsi_device *scsidev) | |
ab5b3a5b | 2701 | { |
41bda9c9 TH |
2702 | int devno; |
2703 | ||
ab5b3a5b | 2704 | /* skip commands not addressed to targets we simulate */ |
071f44b1 | 2705 | if (!sata_pmp_attached(ap)) { |
41bda9c9 TH |
2706 | if (unlikely(scsidev->channel || scsidev->lun)) |
2707 | return NULL; | |
2708 | devno = scsidev->id; | |
2709 | } else { | |
2710 | if (unlikely(scsidev->id || scsidev->lun)) | |
2711 | return NULL; | |
2712 | devno = scsidev->channel; | |
2713 | } | |
ab5b3a5b | 2714 | |
41bda9c9 | 2715 | return ata_find_dev(ap, devno); |
ab5b3a5b TH |
2716 | } |
2717 | ||
1da177e4 LT |
2718 | /** |
2719 | * ata_scsi_find_dev - lookup ata_device from scsi_cmnd | |
2720 | * @ap: ATA port to which the device is attached | |
2721 | * @scsidev: SCSI device from which we derive the ATA device | |
2722 | * | |
2723 | * Given various information provided in struct scsi_cmnd, | |
2724 | * map that onto an ATA bus, and using that mapping | |
2725 | * determine which ata_device is associated with the | |
2726 | * SCSI command to be sent. | |
2727 | * | |
2728 | * LOCKING: | |
cca3974e | 2729 | * spin_lock_irqsave(host lock) |
1da177e4 LT |
2730 | * |
2731 | * RETURNS: | |
2732 | * Associated ATA device, or %NULL if not found. | |
2733 | */ | |
1da177e4 | 2734 | static struct ata_device * |
057ace5e | 2735 | ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev) |
1da177e4 | 2736 | { |
ab5b3a5b | 2737 | struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev); |
1da177e4 | 2738 | |
2486fa56 | 2739 | if (unlikely(!dev || !ata_dev_enabled(dev))) |
1da177e4 LT |
2740 | return NULL; |
2741 | ||
1da177e4 LT |
2742 | return dev; |
2743 | } | |
2744 | ||
b095518e JG |
2745 | /* |
2746 | * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value. | |
2747 | * @byte1: Byte 1 from pass-thru CDB. | |
2748 | * | |
2749 | * RETURNS: | |
2750 | * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise. | |
2751 | */ | |
2752 | static u8 | |
2753 | ata_scsi_map_proto(u8 byte1) | |
2754 | { | |
2755 | switch((byte1 & 0x1e) >> 1) { | |
2dcb407e JG |
2756 | case 3: /* Non-data */ |
2757 | return ATA_PROT_NODATA; | |
2758 | ||
2759 | case 6: /* DMA */ | |
2760 | case 10: /* UDMA Data-in */ | |
2761 | case 11: /* UDMA Data-Out */ | |
2762 | return ATA_PROT_DMA; | |
2763 | ||
2764 | case 4: /* PIO Data-in */ | |
2765 | case 5: /* PIO Data-out */ | |
2766 | return ATA_PROT_PIO; | |
2767 | ||
2768 | case 0: /* Hard Reset */ | |
2769 | case 1: /* SRST */ | |
2770 | case 8: /* Device Diagnostic */ | |
2771 | case 9: /* Device Reset */ | |
2772 | case 7: /* DMA Queued */ | |
2773 | case 12: /* FPDMA */ | |
2774 | case 15: /* Return Response Info */ | |
2775 | default: /* Reserved */ | |
2776 | break; | |
b095518e JG |
2777 | } |
2778 | ||
2779 | return ATA_PROT_UNKNOWN; | |
2780 | } | |
2781 | ||
2782 | /** | |
2783 | * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile | |
2784 | * @qc: command structure to be initialized | |
b095518e JG |
2785 | * |
2786 | * Handles either 12 or 16-byte versions of the CDB. | |
2787 | * | |
2788 | * RETURNS: | |
2789 | * Zero on success, non-zero on failure. | |
2790 | */ | |
ad706991 | 2791 | static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc) |
b095518e JG |
2792 | { |
2793 | struct ata_taskfile *tf = &(qc->tf); | |
542b1444 | 2794 | struct scsi_cmnd *scmd = qc->scsicmd; |
f79d409f | 2795 | struct ata_device *dev = qc->dev; |
ad706991 | 2796 | const u8 *cdb = scmd->cmnd; |
b095518e | 2797 | |
542b1444 | 2798 | if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN) |
9a405257 | 2799 | goto invalid_fld; |
8190bdb9 | 2800 | |
b095518e JG |
2801 | /* |
2802 | * 12 and 16 byte CDBs use different offsets to | |
2803 | * provide the various register values. | |
2804 | */ | |
542b1444 | 2805 | if (cdb[0] == ATA_16) { |
b095518e JG |
2806 | /* |
2807 | * 16-byte CDB - may contain extended commands. | |
2808 | * | |
2809 | * If that is the case, copy the upper byte register values. | |
2810 | */ | |
542b1444 TH |
2811 | if (cdb[1] & 0x01) { |
2812 | tf->hob_feature = cdb[3]; | |
2813 | tf->hob_nsect = cdb[5]; | |
2814 | tf->hob_lbal = cdb[7]; | |
2815 | tf->hob_lbam = cdb[9]; | |
2816 | tf->hob_lbah = cdb[11]; | |
b095518e JG |
2817 | tf->flags |= ATA_TFLAG_LBA48; |
2818 | } else | |
2819 | tf->flags &= ~ATA_TFLAG_LBA48; | |
2820 | ||
2821 | /* | |
2822 | * Always copy low byte, device and command registers. | |
2823 | */ | |
542b1444 TH |
2824 | tf->feature = cdb[4]; |
2825 | tf->nsect = cdb[6]; | |
2826 | tf->lbal = cdb[8]; | |
2827 | tf->lbam = cdb[10]; | |
2828 | tf->lbah = cdb[12]; | |
2829 | tf->device = cdb[13]; | |
2830 | tf->command = cdb[14]; | |
b095518e JG |
2831 | } else { |
2832 | /* | |
2833 | * 12-byte CDB - incapable of extended commands. | |
2834 | */ | |
2835 | tf->flags &= ~ATA_TFLAG_LBA48; | |
2836 | ||
542b1444 TH |
2837 | tf->feature = cdb[3]; |
2838 | tf->nsect = cdb[4]; | |
2839 | tf->lbal = cdb[5]; | |
2840 | tf->lbam = cdb[6]; | |
2841 | tf->lbah = cdb[7]; | |
2842 | tf->device = cdb[8]; | |
2843 | tf->command = cdb[9]; | |
b095518e | 2844 | } |
fa4453c4 AL |
2845 | |
2846 | /* enforce correct master/slave bit */ | |
2847 | tf->device = dev->devno ? | |
2848 | tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1; | |
b095518e | 2849 | |
bd30add8 | 2850 | switch (tf->command) { |
295124dc | 2851 | /* READ/WRITE LONG use a non-standard sect_size */ |
bd30add8 TH |
2852 | case ATA_CMD_READ_LONG: |
2853 | case ATA_CMD_READ_LONG_ONCE: | |
2854 | case ATA_CMD_WRITE_LONG: | |
2855 | case ATA_CMD_WRITE_LONG_ONCE: | |
2856 | if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) | |
2857 | goto invalid_fld; | |
2858 | qc->sect_size = scsi_bufflen(scmd); | |
295124dc GG |
2859 | break; |
2860 | ||
2861 | /* commands using reported Logical Block size (e.g. 512 or 4K) */ | |
2862 | case ATA_CMD_CFA_WRITE_NE: | |
2863 | case ATA_CMD_CFA_TRANS_SECT: | |
2864 | case ATA_CMD_CFA_WRITE_MULT_NE: | |
2865 | /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */ | |
2866 | case ATA_CMD_READ: | |
2867 | case ATA_CMD_READ_EXT: | |
2868 | case ATA_CMD_READ_QUEUED: | |
2869 | /* XXX: case ATA_CMD_READ_QUEUED_EXT: */ | |
2870 | case ATA_CMD_FPDMA_READ: | |
2871 | case ATA_CMD_READ_MULTI: | |
2872 | case ATA_CMD_READ_MULTI_EXT: | |
2873 | case ATA_CMD_PIO_READ: | |
2874 | case ATA_CMD_PIO_READ_EXT: | |
2875 | case ATA_CMD_READ_STREAM_DMA_EXT: | |
2876 | case ATA_CMD_READ_STREAM_EXT: | |
2877 | case ATA_CMD_VERIFY: | |
2878 | case ATA_CMD_VERIFY_EXT: | |
2879 | case ATA_CMD_WRITE: | |
2880 | case ATA_CMD_WRITE_EXT: | |
2881 | case ATA_CMD_WRITE_FUA_EXT: | |
2882 | case ATA_CMD_WRITE_QUEUED: | |
2883 | case ATA_CMD_WRITE_QUEUED_FUA_EXT: | |
2884 | case ATA_CMD_FPDMA_WRITE: | |
2885 | case ATA_CMD_WRITE_MULTI: | |
2886 | case ATA_CMD_WRITE_MULTI_EXT: | |
2887 | case ATA_CMD_WRITE_MULTI_FUA_EXT: | |
2888 | case ATA_CMD_PIO_WRITE: | |
2889 | case ATA_CMD_PIO_WRITE_EXT: | |
2890 | case ATA_CMD_WRITE_STREAM_DMA_EXT: | |
2891 | case ATA_CMD_WRITE_STREAM_EXT: | |
2892 | qc->sect_size = scmd->device->sector_size; | |
2893 | break; | |
2894 | ||
2895 | /* Everything else uses 512 byte "sectors" */ | |
2896 | default: | |
2897 | qc->sect_size = ATA_SECT_SIZE; | |
bd30add8 TH |
2898 | } |
2899 | ||
2900 | /* | |
2901 | * Set flags so that all registers will be written, pass on | |
2902 | * write indication (used for PIO/DMA setup), result TF is | |
2903 | * copied back and we don't whine too much about its failure. | |
2904 | */ | |
bc496ed0 | 2905 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
bd30add8 TH |
2906 | if (scmd->sc_data_direction == DMA_TO_DEVICE) |
2907 | tf->flags |= ATA_TFLAG_WRITE; | |
2908 | ||
2909 | qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET; | |
2910 | ||
2911 | /* | |
2912 | * Set transfer length. | |
2913 | * | |
2914 | * TODO: find out if we need to do more here to | |
2915 | * cover scatter/gather case. | |
2916 | */ | |
2917 | ata_qc_set_pc_nbytes(qc); | |
2918 | ||
2919 | /* We may not issue DMA commands if no DMA mode is set */ | |
2920 | if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) | |
2921 | goto invalid_fld; | |
2922 | ||
1dce589c AL |
2923 | /* sanity check for pio multi commands */ |
2924 | if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) | |
2925 | goto invalid_fld; | |
2926 | ||
2927 | if (is_multi_taskfile(tf)) { | |
2928 | unsigned int multi_count = 1 << (cdb[1] >> 5); | |
2929 | ||
2930 | /* compare the passed through multi_count | |
2931 | * with the cached multi_count of libata | |
2932 | */ | |
2933 | if (multi_count != dev->multi_count) | |
2934 | ata_dev_printk(dev, KERN_WARNING, | |
2935 | "invalid multi_count %u ignored\n", | |
2936 | multi_count); | |
d26fc955 | 2937 | } |
1dce589c | 2938 | |
b095518e JG |
2939 | /* |
2940 | * Filter SET_FEATURES - XFER MODE command -- otherwise, | |
2941 | * SET_FEATURES - XFER MODE must be preceded/succeeded | |
2942 | * by an update to hardware-specific registers for each | |
2943 | * controller (i.e. the reason for ->set_piomode(), | |
2944 | * ->set_dmamode(), and ->post_set_mode() hooks). | |
2945 | */ | |
bd30add8 TH |
2946 | if (tf->command == ATA_CMD_SET_FEATURES && |
2947 | tf->feature == SETFEATURES_XFER) | |
9a405257 | 2948 | goto invalid_fld; |
b095518e JG |
2949 | |
2950 | /* | |
bd30add8 TH |
2951 | * Filter TPM commands by default. These provide an |
2952 | * essentially uncontrolled encrypted "back door" between | |
2953 | * applications and the disk. Set libata.allow_tpm=1 if you | |
2954 | * have a real reason for wanting to use them. This ensures | |
2955 | * that installed software cannot easily mess stuff up without | |
2956 | * user intent. DVR type users will probably ship with this enabled | |
2957 | * for movie content management. | |
b095518e | 2958 | * |
bd30add8 TH |
2959 | * Note that for ATA8 we can issue a DCS change and DCS freeze lock |
2960 | * for this and should do in future but that it is not sufficient as | |
2961 | * DCS is an optional feature set. Thus we also do the software filter | |
2962 | * so that we comply with the TC consortium stated goal that the user | |
2963 | * can turn off TC features of their system. | |
b095518e | 2964 | */ |
bd30add8 TH |
2965 | if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) |
2966 | goto invalid_fld; | |
e61e0672 | 2967 | |
b095518e | 2968 | return 0; |
9a405257 TH |
2969 | |
2970 | invalid_fld: | |
542b1444 | 2971 | ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00); |
9a405257 TH |
2972 | /* "Invalid field in cdb" */ |
2973 | return 1; | |
b095518e JG |
2974 | } |
2975 | ||
18f0f978 CH |
2976 | static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc) |
2977 | { | |
2978 | struct ata_taskfile *tf = &qc->tf; | |
2979 | struct scsi_cmnd *scmd = qc->scsicmd; | |
2980 | struct ata_device *dev = qc->dev; | |
2981 | const u8 *cdb = scmd->cmnd; | |
2982 | u64 block; | |
2983 | u32 n_block; | |
2984 | u32 size; | |
2985 | void *buf; | |
2986 | ||
2987 | /* we may not issue DMA commands if no DMA mode is set */ | |
2988 | if (unlikely(!dev->dma_mode)) | |
2989 | goto invalid_fld; | |
2990 | ||
2991 | if (unlikely(scmd->cmd_len < 16)) | |
2992 | goto invalid_fld; | |
2993 | scsi_16_lba_len(cdb, &block, &n_block); | |
2994 | ||
2995 | /* for now we only support WRITE SAME with the unmap bit set */ | |
2996 | if (unlikely(!(cdb[1] & 0x8))) | |
2997 | goto invalid_fld; | |
2998 | ||
2999 | /* | |
3000 | * WRITE SAME always has a sector sized buffer as payload, this | |
3001 | * should never be a multiple entry S/G list. | |
3002 | */ | |
3003 | if (!scsi_sg_count(scmd)) | |
3004 | goto invalid_fld; | |
3005 | ||
3006 | buf = page_address(sg_page(scsi_sglist(scmd))); | |
d0634c4a | 3007 | size = ata_set_lba_range_entries(buf, 512, block, n_block); |
18f0f978 CH |
3008 | |
3009 | tf->protocol = ATA_PROT_DMA; | |
3010 | tf->hob_feature = 0; | |
3011 | tf->feature = ATA_DSM_TRIM; | |
3012 | tf->hob_nsect = (size / 512) >> 8; | |
3013 | tf->nsect = size / 512; | |
3014 | tf->command = ATA_CMD_DSM; | |
3015 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | | |
3016 | ATA_TFLAG_WRITE; | |
3017 | ||
3018 | ata_qc_set_pc_nbytes(qc); | |
3019 | ||
3020 | return 0; | |
3021 | ||
3022 | invalid_fld: | |
3023 | ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00); | |
3024 | /* "Invalid field in cdb" */ | |
3025 | return 1; | |
3026 | } | |
3027 | ||
1da177e4 LT |
3028 | /** |
3029 | * ata_get_xlat_func - check if SCSI to ATA translation is possible | |
3030 | * @dev: ATA device | |
3031 | * @cmd: SCSI command opcode to consider | |
3032 | * | |
3033 | * Look up the SCSI command given, and determine whether the | |
3034 | * SCSI command is to be translated or simulated. | |
3035 | * | |
3036 | * RETURNS: | |
3037 | * Pointer to translation function if possible, %NULL if not. | |
3038 | */ | |
3039 | ||
3040 | static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd) | |
3041 | { | |
3042 | switch (cmd) { | |
3043 | case READ_6: | |
3044 | case READ_10: | |
3045 | case READ_16: | |
3046 | ||
3047 | case WRITE_6: | |
3048 | case WRITE_10: | |
3049 | case WRITE_16: | |
3050 | return ata_scsi_rw_xlat; | |
3051 | ||
0cdd6eb7 | 3052 | case WRITE_SAME_16: |
18f0f978 CH |
3053 | return ata_scsi_write_same_xlat; |
3054 | ||
1da177e4 LT |
3055 | case SYNCHRONIZE_CACHE: |
3056 | if (ata_try_flush_cache(dev)) | |
3057 | return ata_scsi_flush_xlat; | |
3058 | break; | |
3059 | ||
3060 | case VERIFY: | |
3061 | case VERIFY_16: | |
3062 | return ata_scsi_verify_xlat; | |
b095518e JG |
3063 | |
3064 | case ATA_12: | |
3065 | case ATA_16: | |
3066 | return ata_scsi_pass_thru; | |
da61396d | 3067 | |
972dcafb DG |
3068 | case START_STOP: |
3069 | return ata_scsi_start_stop_xlat; | |
1da177e4 LT |
3070 | } |
3071 | ||
3072 | return NULL; | |
3073 | } | |
3074 | ||
3075 | /** | |
3076 | * ata_scsi_dump_cdb - dump SCSI command contents to dmesg | |
3077 | * @ap: ATA port to which the command was being sent | |
3078 | * @cmd: SCSI command to dump | |
3079 | * | |
3080 | * Prints the contents of a SCSI command via printk(). | |
3081 | */ | |
3082 | ||
3083 | static inline void ata_scsi_dump_cdb(struct ata_port *ap, | |
3084 | struct scsi_cmnd *cmd) | |
3085 | { | |
3086 | #ifdef ATA_DEBUG | |
3087 | struct scsi_device *scsidev = cmd->device; | |
3088 | u8 *scsicmd = cmd->cmnd; | |
3089 | ||
3090 | DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", | |
44877b4e | 3091 | ap->print_id, |
1da177e4 LT |
3092 | scsidev->channel, scsidev->id, scsidev->lun, |
3093 | scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3], | |
3094 | scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7], | |
3095 | scsicmd[8]); | |
3096 | #endif | |
3097 | } | |
3098 | ||
542b1444 | 3099 | static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, |
2115ea94 TH |
3100 | void (*done)(struct scsi_cmnd *), |
3101 | struct ata_device *dev) | |
eb3f0f9c | 3102 | { |
baf4fdfa ML |
3103 | u8 scsi_op = scmd->cmnd[0]; |
3104 | ata_xlat_func_t xlat_func; | |
2115ea94 TH |
3105 | int rc = 0; |
3106 | ||
eb3f0f9c | 3107 | if (dev->class == ATA_DEV_ATA) { |
baf4fdfa ML |
3108 | if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len)) |
3109 | goto bad_cdb_len; | |
eb3f0f9c | 3110 | |
baf4fdfa ML |
3111 | xlat_func = ata_get_xlat_func(dev, scsi_op); |
3112 | } else { | |
3113 | if (unlikely(!scmd->cmd_len)) | |
3114 | goto bad_cdb_len; | |
3115 | ||
3116 | xlat_func = NULL; | |
3117 | if (likely((scsi_op != ATA_16) || !atapi_passthru16)) { | |
3118 | /* relay SCSI command to ATAPI device */ | |
607126c2 ML |
3119 | int len = COMMAND_SIZE(scsi_op); |
3120 | if (unlikely(len > scmd->cmd_len || len > dev->cdb_len)) | |
baf4fdfa ML |
3121 | goto bad_cdb_len; |
3122 | ||
3123 | xlat_func = atapi_xlat; | |
3124 | } else { | |
3125 | /* ATA_16 passthru, treat as an ATA command */ | |
3126 | if (unlikely(scmd->cmd_len > 16)) | |
3127 | goto bad_cdb_len; | |
3128 | ||
3129 | xlat_func = ata_get_xlat_func(dev, scsi_op); | |
3130 | } | |
3131 | } | |
3132 | ||
3133 | if (xlat_func) | |
3134 | rc = ata_scsi_translate(dev, scmd, done, xlat_func); | |
3135 | else | |
3136 | ata_scsi_simulate(dev, scmd, done); | |
2115ea94 TH |
3137 | |
3138 | return rc; | |
baf4fdfa ML |
3139 | |
3140 | bad_cdb_len: | |
3141 | DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n", | |
3142 | scmd->cmd_len, scsi_op, dev->cdb_len); | |
3143 | scmd->result = DID_ERROR << 16; | |
3144 | done(scmd); | |
3145 | return 0; | |
eb3f0f9c BK |
3146 | } |
3147 | ||
1da177e4 LT |
3148 | /** |
3149 | * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device | |
3150 | * @cmd: SCSI command to be sent | |
3151 | * @done: Completion function, called when command is complete | |
3152 | * | |
3153 | * In some cases, this function translates SCSI commands into | |
3154 | * ATA taskfiles, and queues the taskfiles to be sent to | |
3155 | * hardware. In other cases, this function simulates a | |
3156 | * SCSI device by evaluating and responding to certain | |
3157 | * SCSI commands. This creates the overall effect of | |
3158 | * ATA and ATAPI devices appearing as SCSI devices. | |
3159 | * | |
3160 | * LOCKING: | |
cca3974e | 3161 | * Releases scsi-layer-held lock, and obtains host lock. |
1da177e4 LT |
3162 | * |
3163 | * RETURNS: | |
2115ea94 TH |
3164 | * Return value from __ata_scsi_queuecmd() if @cmd can be queued, |
3165 | * 0 otherwise. | |
1da177e4 | 3166 | */ |
1da177e4 LT |
3167 | int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) |
3168 | { | |
3169 | struct ata_port *ap; | |
3170 | struct ata_device *dev; | |
3171 | struct scsi_device *scsidev = cmd->device; | |
005a5a06 | 3172 | struct Scsi_Host *shost = scsidev->host; |
2115ea94 | 3173 | int rc = 0; |
1da177e4 | 3174 | |
35bb94b1 | 3175 | ap = ata_shost_to_port(shost); |
005a5a06 JG |
3176 | |
3177 | spin_unlock(shost->host_lock); | |
ba6a1308 | 3178 | spin_lock(ap->lock); |
1da177e4 LT |
3179 | |
3180 | ata_scsi_dump_cdb(ap, cmd); | |
3181 | ||
3182 | dev = ata_scsi_find_dev(ap, scsidev); | |
eb3f0f9c | 3183 | if (likely(dev)) |
2115ea94 | 3184 | rc = __ata_scsi_queuecmd(cmd, done, dev); |
eb3f0f9c | 3185 | else { |
1da177e4 LT |
3186 | cmd->result = (DID_BAD_TARGET << 16); |
3187 | done(cmd); | |
1da177e4 LT |
3188 | } |
3189 | ||
ba6a1308 | 3190 | spin_unlock(ap->lock); |
005a5a06 | 3191 | spin_lock(shost->host_lock); |
2115ea94 | 3192 | return rc; |
1da177e4 LT |
3193 | } |
3194 | ||
3195 | /** | |
3196 | * ata_scsi_simulate - simulate SCSI command on ATA device | |
c893a3ae | 3197 | * @dev: the target device |
1da177e4 LT |
3198 | * @cmd: SCSI command being sent to device. |
3199 | * @done: SCSI command completion function. | |
3200 | * | |
3201 | * Interprets and directly executes a select list of SCSI commands | |
3202 | * that can be handled internally. | |
3203 | * | |
3204 | * LOCKING: | |
cca3974e | 3205 | * spin_lock_irqsave(host lock) |
1da177e4 LT |
3206 | */ |
3207 | ||
3373efd8 | 3208 | void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd, |
1da177e4 LT |
3209 | void (*done)(struct scsi_cmnd *)) |
3210 | { | |
3211 | struct ata_scsi_args args; | |
057ace5e | 3212 | const u8 *scsicmd = cmd->cmnd; |
45394145 | 3213 | u8 tmp8; |
1da177e4 | 3214 | |
9a3dccc4 TH |
3215 | args.dev = dev; |
3216 | args.id = dev->id; | |
1da177e4 LT |
3217 | args.cmd = cmd; |
3218 | args.done = done; | |
3219 | ||
3220 | switch(scsicmd[0]) { | |
2dcb407e JG |
3221 | /* TODO: worth improving? */ |
3222 | case FORMAT_UNIT: | |
3223 | ata_scsi_invalid_field(cmd, done); | |
3224 | break; | |
3225 | ||
3226 | case INQUIRY: | |
3227 | if (scsicmd[1] & 2) /* is CmdDt set? */ | |
00bd0202 | 3228 | ata_scsi_invalid_field(cmd, done); |
2dcb407e JG |
3229 | else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ |
3230 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); | |
3231 | else switch (scsicmd[2]) { | |
3232 | case 0x00: | |
3233 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00); | |
1da177e4 | 3234 | break; |
2dcb407e JG |
3235 | case 0x80: |
3236 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80); | |
1da177e4 | 3237 | break; |
2dcb407e JG |
3238 | case 0x83: |
3239 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); | |
1da177e4 | 3240 | break; |
2dcb407e JG |
3241 | case 0x89: |
3242 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89); | |
3243 | break; | |
18f0f978 CH |
3244 | case 0xb0: |
3245 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0); | |
3246 | break; | |
1e9dbc92 MW |
3247 | case 0xb1: |
3248 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1); | |
3249 | break; | |
2dcb407e | 3250 | default: |
ae006510 | 3251 | ata_scsi_invalid_field(cmd, done); |
1da177e4 | 3252 | break; |
2dcb407e JG |
3253 | } |
3254 | break; | |
3255 | ||
3256 | case MODE_SENSE: | |
3257 | case MODE_SENSE_10: | |
3258 | ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense); | |
3259 | break; | |
3260 | ||
3261 | case MODE_SELECT: /* unconditionally return */ | |
3262 | case MODE_SELECT_10: /* bad-field-in-cdb */ | |
3263 | ata_scsi_invalid_field(cmd, done); | |
3264 | break; | |
1da177e4 | 3265 | |
2dcb407e JG |
3266 | case READ_CAPACITY: |
3267 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); | |
3268 | break; | |
3269 | ||
3270 | case SERVICE_ACTION_IN: | |
3271 | if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) | |
1da177e4 | 3272 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); |
2dcb407e JG |
3273 | else |
3274 | ata_scsi_invalid_field(cmd, done); | |
3275 | break; | |
1da177e4 | 3276 | |
2dcb407e JG |
3277 | case REPORT_LUNS: |
3278 | ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); | |
3279 | break; | |
1da177e4 | 3280 | |
2dcb407e JG |
3281 | case REQUEST_SENSE: |
3282 | ata_scsi_set_sense(cmd, 0, 0, 0); | |
3283 | cmd->result = (DRIVER_SENSE << 24); | |
3284 | done(cmd); | |
3285 | break; | |
1da177e4 | 3286 | |
2dcb407e JG |
3287 | /* if we reach this, then writeback caching is disabled, |
3288 | * turning this into a no-op. | |
3289 | */ | |
3290 | case SYNCHRONIZE_CACHE: | |
3291 | /* fall through */ | |
3292 | ||
3293 | /* no-op's, complete with success */ | |
3294 | case REZERO_UNIT: | |
3295 | case SEEK_6: | |
3296 | case SEEK_10: | |
3297 | case TEST_UNIT_READY: | |
3298 | ata_scsi_rbuf_fill(&args, ata_scsiop_noop); | |
3299 | break; | |
45394145 | 3300 | |
2dcb407e JG |
3301 | case SEND_DIAGNOSTIC: |
3302 | tmp8 = scsicmd[1] & ~(1 << 3); | |
3303 | if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4])) | |
00bd0202 | 3304 | ata_scsi_rbuf_fill(&args, ata_scsiop_noop); |
2dcb407e JG |
3305 | else |
3306 | ata_scsi_invalid_field(cmd, done); | |
3307 | break; | |
1da177e4 | 3308 | |
2dcb407e JG |
3309 | /* all other commands */ |
3310 | default: | |
3311 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0); | |
3312 | /* "Invalid command operation code" */ | |
3313 | done(cmd); | |
3314 | break; | |
1da177e4 LT |
3315 | } |
3316 | } | |
3317 | ||
f3187195 TH |
3318 | int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht) |
3319 | { | |
3320 | int i, rc; | |
3321 | ||
3322 | for (i = 0; i < host->n_ports; i++) { | |
3323 | struct ata_port *ap = host->ports[i]; | |
3324 | struct Scsi_Host *shost; | |
3325 | ||
3326 | rc = -ENOMEM; | |
3327 | shost = scsi_host_alloc(sht, sizeof(struct ata_port *)); | |
3328 | if (!shost) | |
3329 | goto err_alloc; | |
3330 | ||
3331 | *(struct ata_port **)&shost->hostdata[0] = ap; | |
3332 | ap->scsi_host = shost; | |
3333 | ||
d9027470 | 3334 | shost->transportt = ata_scsi_transport_template; |
f3187195 TH |
3335 | shost->unique_id = ap->print_id; |
3336 | shost->max_id = 16; | |
3337 | shost->max_lun = 1; | |
3338 | shost->max_channel = 1; | |
3339 | shost->max_cmd_len = 16; | |
3340 | ||
31cc23b3 TH |
3341 | /* Schedule policy is determined by ->qc_defer() |
3342 | * callback and it needs to see every deferred qc. | |
3343 | * Set host_blocked to 1 to prevent SCSI midlayer from | |
3344 | * automatically deferring requests. | |
3345 | */ | |
3346 | shost->max_host_blocked = 1; | |
3347 | ||
f3187195 TH |
3348 | rc = scsi_add_host(ap->scsi_host, ap->host->dev); |
3349 | if (rc) | |
3350 | goto err_add; | |
3351 | } | |
3352 | ||
3353 | return 0; | |
3354 | ||
3355 | err_add: | |
3356 | scsi_host_put(host->ports[i]->scsi_host); | |
3357 | err_alloc: | |
3358 | while (--i >= 0) { | |
3359 | struct Scsi_Host *shost = host->ports[i]->scsi_host; | |
3360 | ||
3361 | scsi_remove_host(shost); | |
3362 | scsi_host_put(shost); | |
3363 | } | |
3364 | return rc; | |
3365 | } | |
3366 | ||
1ae46317 | 3367 | void ata_scsi_scan_host(struct ata_port *ap, int sync) |
644dd0cc | 3368 | { |
1ae46317 TH |
3369 | int tries = 5; |
3370 | struct ata_device *last_failed_dev = NULL; | |
41bda9c9 | 3371 | struct ata_link *link; |
1ae46317 | 3372 | struct ata_device *dev; |
644dd0cc | 3373 | |
1ae46317 | 3374 | repeat: |
1eca4365 TH |
3375 | ata_for_each_link(link, ap, EDGE) { |
3376 | ata_for_each_dev(dev, link, ENABLED) { | |
41bda9c9 TH |
3377 | struct scsi_device *sdev; |
3378 | int channel = 0, id = 0; | |
3edebac4 | 3379 | |
1eca4365 | 3380 | if (dev->sdev) |
41bda9c9 | 3381 | continue; |
3f19ee8c | 3382 | |
41bda9c9 TH |
3383 | if (ata_is_host_link(link)) |
3384 | id = dev->devno; | |
3385 | else | |
3386 | channel = link->pmp; | |
3387 | ||
3388 | sdev = __scsi_add_device(ap->scsi_host, channel, id, 0, | |
3389 | NULL); | |
3390 | if (!IS_ERR(sdev)) { | |
3391 | dev->sdev = sdev; | |
3392 | scsi_device_put(sdev); | |
295124dc GG |
3393 | } else { |
3394 | dev->sdev = NULL; | |
41bda9c9 | 3395 | } |
3edebac4 | 3396 | } |
3f19ee8c | 3397 | } |
1ae46317 TH |
3398 | |
3399 | /* If we scanned while EH was in progress or allocation | |
3400 | * failure occurred, scan would have failed silently. Check | |
3401 | * whether all devices are attached. | |
3402 | */ | |
1eca4365 TH |
3403 | ata_for_each_link(link, ap, EDGE) { |
3404 | ata_for_each_dev(dev, link, ENABLED) { | |
3405 | if (!dev->sdev) | |
41bda9c9 TH |
3406 | goto exit_loop; |
3407 | } | |
1ae46317 | 3408 | } |
41bda9c9 TH |
3409 | exit_loop: |
3410 | if (!link) | |
1ae46317 TH |
3411 | return; |
3412 | ||
3413 | /* we're missing some SCSI devices */ | |
3414 | if (sync) { | |
3415 | /* If caller requested synchrnous scan && we've made | |
3416 | * any progress, sleep briefly and repeat. | |
3417 | */ | |
3418 | if (dev != last_failed_dev) { | |
3419 | msleep(100); | |
3420 | last_failed_dev = dev; | |
3421 | goto repeat; | |
3422 | } | |
3423 | ||
3424 | /* We might be failing to detect boot device, give it | |
3425 | * a few more chances. | |
3426 | */ | |
3427 | if (--tries) { | |
3428 | msleep(100); | |
3429 | goto repeat; | |
3430 | } | |
3431 | ||
3432 | ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan " | |
3433 | "failed without making any progress,\n" | |
3434 | " switching to async\n"); | |
3435 | } | |
3436 | ||
ad72cf98 | 3437 | queue_delayed_work(system_long_wq, &ap->hotplug_task, |
1ae46317 | 3438 | round_jiffies_relative(HZ)); |
644dd0cc | 3439 | } |
0ea035a3 TH |
3440 | |
3441 | /** | |
3442 | * ata_scsi_offline_dev - offline attached SCSI device | |
3443 | * @dev: ATA device to offline attached SCSI device for | |
3444 | * | |
3445 | * This function is called from ata_eh_hotplug() and responsible | |
3446 | * for taking the SCSI device attached to @dev offline. This | |
cca3974e | 3447 | * function is called with host lock which protects dev->sdev |
0ea035a3 TH |
3448 | * against clearing. |
3449 | * | |
3450 | * LOCKING: | |
cca3974e | 3451 | * spin_lock_irqsave(host lock) |
0ea035a3 TH |
3452 | * |
3453 | * RETURNS: | |
3454 | * 1 if attached SCSI device exists, 0 otherwise. | |
3455 | */ | |
3456 | int ata_scsi_offline_dev(struct ata_device *dev) | |
3457 | { | |
3458 | if (dev->sdev) { | |
3459 | scsi_device_set_state(dev->sdev, SDEV_OFFLINE); | |
3460 | return 1; | |
3461 | } | |
3462 | return 0; | |
3463 | } | |
580b2102 TH |
3464 | |
3465 | /** | |
3466 | * ata_scsi_remove_dev - remove attached SCSI device | |
3467 | * @dev: ATA device to remove attached SCSI device for | |
3468 | * | |
3469 | * This function is called from ata_eh_scsi_hotplug() and | |
3470 | * responsible for removing the SCSI device attached to @dev. | |
3471 | * | |
3472 | * LOCKING: | |
3473 | * Kernel thread context (may sleep). | |
3474 | */ | |
3475 | static void ata_scsi_remove_dev(struct ata_device *dev) | |
3476 | { | |
9af5c9c9 | 3477 | struct ata_port *ap = dev->link->ap; |
580b2102 TH |
3478 | struct scsi_device *sdev; |
3479 | unsigned long flags; | |
3480 | ||
3481 | /* Alas, we need to grab scan_mutex to ensure SCSI device | |
3482 | * state doesn't change underneath us and thus | |
3483 | * scsi_device_get() always succeeds. The mutex locking can | |
3484 | * be removed if there is __scsi_device_get() interface which | |
3485 | * increments reference counts regardless of device state. | |
3486 | */ | |
cca3974e | 3487 | mutex_lock(&ap->scsi_host->scan_mutex); |
ba6a1308 | 3488 | spin_lock_irqsave(ap->lock, flags); |
580b2102 | 3489 | |
cca3974e | 3490 | /* clearing dev->sdev is protected by host lock */ |
580b2102 TH |
3491 | sdev = dev->sdev; |
3492 | dev->sdev = NULL; | |
3493 | ||
3494 | if (sdev) { | |
3495 | /* If user initiated unplug races with us, sdev can go | |
cca3974e | 3496 | * away underneath us after the host lock and |
580b2102 TH |
3497 | * scan_mutex are released. Hold onto it. |
3498 | */ | |
3499 | if (scsi_device_get(sdev) == 0) { | |
3500 | /* The following ensures the attached sdev is | |
3501 | * offline on return from ata_scsi_offline_dev() | |
3502 | * regardless it wins or loses the race | |
3503 | * against this function. | |
3504 | */ | |
3505 | scsi_device_set_state(sdev, SDEV_OFFLINE); | |
3506 | } else { | |
3507 | WARN_ON(1); | |
3508 | sdev = NULL; | |
3509 | } | |
3510 | } | |
3511 | ||
ba6a1308 | 3512 | spin_unlock_irqrestore(ap->lock, flags); |
cca3974e | 3513 | mutex_unlock(&ap->scsi_host->scan_mutex); |
580b2102 TH |
3514 | |
3515 | if (sdev) { | |
3516 | ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n", | |
b9d5fc41 | 3517 | dev_name(&sdev->sdev_gendev)); |
580b2102 TH |
3518 | |
3519 | scsi_remove_device(sdev); | |
3520 | scsi_device_put(sdev); | |
3521 | } | |
3522 | } | |
3523 | ||
41bda9c9 TH |
3524 | static void ata_scsi_handle_link_detach(struct ata_link *link) |
3525 | { | |
3526 | struct ata_port *ap = link->ap; | |
3527 | struct ata_device *dev; | |
3528 | ||
1eca4365 | 3529 | ata_for_each_dev(dev, link, ALL) { |
41bda9c9 TH |
3530 | unsigned long flags; |
3531 | ||
3532 | if (!(dev->flags & ATA_DFLAG_DETACHED)) | |
3533 | continue; | |
3534 | ||
3535 | spin_lock_irqsave(ap->lock, flags); | |
3536 | dev->flags &= ~ATA_DFLAG_DETACHED; | |
3537 | spin_unlock_irqrestore(ap->lock, flags); | |
3538 | ||
3539 | ata_scsi_remove_dev(dev); | |
3540 | } | |
3541 | } | |
3542 | ||
2f294968 KCA |
3543 | /** |
3544 | * ata_scsi_media_change_notify - send media change event | |
c5d0e6a0 | 3545 | * @dev: Pointer to the disk device with media change event |
2f294968 KCA |
3546 | * |
3547 | * Tell the block layer to send a media change notification | |
3548 | * event. | |
3549 | * | |
3550 | * LOCKING: | |
854c73a2 | 3551 | * spin_lock_irqsave(host lock) |
2f294968 | 3552 | */ |
854c73a2 | 3553 | void ata_scsi_media_change_notify(struct ata_device *dev) |
2f294968 | 3554 | { |
854c73a2 | 3555 | if (dev->sdev) |
f26792d5 JG |
3556 | sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE, |
3557 | GFP_ATOMIC); | |
2f294968 | 3558 | } |
2f294968 | 3559 | |
580b2102 TH |
3560 | /** |
3561 | * ata_scsi_hotplug - SCSI part of hotplug | |
65f27f38 | 3562 | * @work: Pointer to ATA port to perform SCSI hotplug on |
580b2102 TH |
3563 | * |
3564 | * Perform SCSI part of hotplug. It's executed from a separate | |
3565 | * workqueue after EH completes. This is necessary because SCSI | |
3566 | * hot plugging requires working EH and hot unplugging is | |
3567 | * synchronized with hot plugging with a mutex. | |
3568 | * | |
3569 | * LOCKING: | |
3570 | * Kernel thread context (may sleep). | |
3571 | */ | |
65f27f38 | 3572 | void ata_scsi_hotplug(struct work_struct *work) |
580b2102 | 3573 | { |
65f27f38 DH |
3574 | struct ata_port *ap = |
3575 | container_of(work, struct ata_port, hotplug_task.work); | |
41bda9c9 | 3576 | int i; |
580b2102 | 3577 | |
b51e9e5d | 3578 | if (ap->pflags & ATA_PFLAG_UNLOADING) { |
580b2102 TH |
3579 | DPRINTK("ENTER/EXIT - unloading\n"); |
3580 | return; | |
3581 | } | |
3582 | ||
3583 | DPRINTK("ENTER\n"); | |
ad72cf98 | 3584 | mutex_lock(&ap->scsi_scan_mutex); |
580b2102 | 3585 | |
41bda9c9 TH |
3586 | /* Unplug detached devices. We cannot use link iterator here |
3587 | * because PMP links have to be scanned even if PMP is | |
3588 | * currently not attached. Iterate manually. | |
3589 | */ | |
3590 | ata_scsi_handle_link_detach(&ap->link); | |
3591 | if (ap->pmp_link) | |
3592 | for (i = 0; i < SATA_PMP_MAX_PORTS; i++) | |
3593 | ata_scsi_handle_link_detach(&ap->pmp_link[i]); | |
580b2102 TH |
3594 | |
3595 | /* scan for new ones */ | |
1ae46317 | 3596 | ata_scsi_scan_host(ap, 0); |
580b2102 | 3597 | |
ad72cf98 | 3598 | mutex_unlock(&ap->scsi_scan_mutex); |
580b2102 TH |
3599 | DPRINTK("EXIT\n"); |
3600 | } | |
83c47bcb TH |
3601 | |
3602 | /** | |
3603 | * ata_scsi_user_scan - indication for user-initiated bus scan | |
3604 | * @shost: SCSI host to scan | |
3605 | * @channel: Channel to scan | |
3606 | * @id: ID to scan | |
3607 | * @lun: LUN to scan | |
3608 | * | |
3609 | * This function is called when user explicitly requests bus | |
3610 | * scan. Set probe pending flag and invoke EH. | |
3611 | * | |
3612 | * LOCKING: | |
3613 | * SCSI layer (we don't care) | |
3614 | * | |
3615 | * RETURNS: | |
3616 | * Zero. | |
3617 | */ | |
d9027470 GG |
3618 | int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel, |
3619 | unsigned int id, unsigned int lun) | |
83c47bcb TH |
3620 | { |
3621 | struct ata_port *ap = ata_shost_to_port(shost); | |
3622 | unsigned long flags; | |
41bda9c9 | 3623 | int devno, rc = 0; |
83c47bcb TH |
3624 | |
3625 | if (!ap->ops->error_handler) | |
3626 | return -EOPNOTSUPP; | |
3627 | ||
41bda9c9 | 3628 | if (lun != SCAN_WILD_CARD && lun) |
83c47bcb TH |
3629 | return -EINVAL; |
3630 | ||
071f44b1 | 3631 | if (!sata_pmp_attached(ap)) { |
41bda9c9 TH |
3632 | if (channel != SCAN_WILD_CARD && channel) |
3633 | return -EINVAL; | |
3634 | devno = id; | |
3635 | } else { | |
3636 | if (id != SCAN_WILD_CARD && id) | |
3637 | return -EINVAL; | |
3638 | devno = channel; | |
3639 | } | |
3640 | ||
ba6a1308 | 3641 | spin_lock_irqsave(ap->lock, flags); |
83c47bcb | 3642 | |
41bda9c9 TH |
3643 | if (devno == SCAN_WILD_CARD) { |
3644 | struct ata_link *link; | |
3645 | ||
1eca4365 | 3646 | ata_for_each_link(link, ap, EDGE) { |
41bda9c9 | 3647 | struct ata_eh_info *ehi = &link->eh_info; |
b558eddd | 3648 | ehi->probe_mask |= ATA_ALL_DEVICES; |
cf480626 | 3649 | ehi->action |= ATA_EH_RESET; |
41bda9c9 | 3650 | } |
83c47bcb | 3651 | } else { |
41bda9c9 | 3652 | struct ata_device *dev = ata_find_dev(ap, devno); |
83c47bcb TH |
3653 | |
3654 | if (dev) { | |
41bda9c9 | 3655 | struct ata_eh_info *ehi = &dev->link->eh_info; |
9af5c9c9 | 3656 | ehi->probe_mask |= 1 << dev->devno; |
cf480626 | 3657 | ehi->action |= ATA_EH_RESET; |
83c47bcb TH |
3658 | } else |
3659 | rc = -EINVAL; | |
3660 | } | |
3661 | ||
309afcb5 | 3662 | if (rc == 0) { |
83c47bcb | 3663 | ata_port_schedule_eh(ap); |
309afcb5 TH |
3664 | spin_unlock_irqrestore(ap->lock, flags); |
3665 | ata_port_wait_eh(ap); | |
3666 | } else | |
3667 | spin_unlock_irqrestore(ap->lock, flags); | |
83c47bcb TH |
3668 | |
3669 | return rc; | |
3670 | } | |
3057ac3c | 3671 | |
3672 | /** | |
d0171269 | 3673 | * ata_scsi_dev_rescan - initiate scsi_rescan_device() |
65f27f38 | 3674 | * @work: Pointer to ATA port to perform scsi_rescan_device() |
3057ac3c | 3675 | * |
d0171269 | 3676 | * After ATA pass thru (SAT) commands are executed successfully, |
ad72cf98 | 3677 | * libata need to propagate the changes to SCSI layer. |
3057ac3c | 3678 | * |
d0171269 TH |
3679 | * LOCKING: |
3680 | * Kernel thread context (may sleep). | |
3057ac3c | 3681 | */ |
65f27f38 | 3682 | void ata_scsi_dev_rescan(struct work_struct *work) |
3057ac3c | 3683 | { |
65f27f38 DH |
3684 | struct ata_port *ap = |
3685 | container_of(work, struct ata_port, scsi_rescan_task); | |
41bda9c9 | 3686 | struct ata_link *link; |
f58229f8 | 3687 | struct ata_device *dev; |
f84e7e41 | 3688 | unsigned long flags; |
3057ac3c | 3689 | |
ad72cf98 | 3690 | mutex_lock(&ap->scsi_scan_mutex); |
f84e7e41 TH |
3691 | spin_lock_irqsave(ap->lock, flags); |
3692 | ||
1eca4365 TH |
3693 | ata_for_each_link(link, ap, EDGE) { |
3694 | ata_for_each_dev(dev, link, ENABLED) { | |
41bda9c9 | 3695 | struct scsi_device *sdev = dev->sdev; |
3057ac3c | 3696 | |
1eca4365 | 3697 | if (!sdev) |
41bda9c9 TH |
3698 | continue; |
3699 | if (scsi_device_get(sdev)) | |
3700 | continue; | |
f84e7e41 | 3701 | |
41bda9c9 TH |
3702 | spin_unlock_irqrestore(ap->lock, flags); |
3703 | scsi_rescan_device(&(sdev->sdev_gendev)); | |
3704 | scsi_device_put(sdev); | |
3705 | spin_lock_irqsave(ap->lock, flags); | |
3706 | } | |
3057ac3c | 3707 | } |
f84e7e41 TH |
3708 | |
3709 | spin_unlock_irqrestore(ap->lock, flags); | |
ad72cf98 | 3710 | mutex_unlock(&ap->scsi_scan_mutex); |
3057ac3c | 3711 | } |
80289167 BK |
3712 | |
3713 | /** | |
3714 | * ata_sas_port_alloc - Allocate port for a SAS attached SATA device | |
4f931374 | 3715 | * @host: ATA host container for all SAS ports |
80289167 | 3716 | * @port_info: Information from low-level host driver |
cca3974e | 3717 | * @shost: SCSI host that the scsi device is attached to |
80289167 BK |
3718 | * |
3719 | * LOCKING: | |
3720 | * PCI/etc. bus probe sem. | |
3721 | * | |
3722 | * RETURNS: | |
3723 | * ata_port pointer on success / NULL on failure. | |
3724 | */ | |
3725 | ||
cca3974e | 3726 | struct ata_port *ata_sas_port_alloc(struct ata_host *host, |
80289167 | 3727 | struct ata_port_info *port_info, |
cca3974e | 3728 | struct Scsi_Host *shost) |
80289167 | 3729 | { |
f3187195 | 3730 | struct ata_port *ap; |
80289167 | 3731 | |
f3187195 | 3732 | ap = ata_port_alloc(host); |
80289167 BK |
3733 | if (!ap) |
3734 | return NULL; | |
3735 | ||
f3187195 | 3736 | ap->port_no = 0; |
cca3974e | 3737 | ap->lock = shost->host_lock; |
f3187195 TH |
3738 | ap->pio_mask = port_info->pio_mask; |
3739 | ap->mwdma_mask = port_info->mwdma_mask; | |
3740 | ap->udma_mask = port_info->udma_mask; | |
3741 | ap->flags |= port_info->flags; | |
3742 | ap->ops = port_info->port_ops; | |
3743 | ap->cbl = ATA_CBL_SATA; | |
3744 | ||
80289167 BK |
3745 | return ap; |
3746 | } | |
3747 | EXPORT_SYMBOL_GPL(ata_sas_port_alloc); | |
3748 | ||
3749 | /** | |
3750 | * ata_sas_port_start - Set port up for dma. | |
3751 | * @ap: Port to initialize | |
3752 | * | |
3753 | * Called just after data structures for each port are | |
dde20207 | 3754 | * initialized. |
80289167 BK |
3755 | * |
3756 | * May be used as the port_start() entry in ata_port_operations. | |
3757 | * | |
3758 | * LOCKING: | |
3759 | * Inherited from caller. | |
3760 | */ | |
3761 | int ata_sas_port_start(struct ata_port *ap) | |
3762 | { | |
dde20207 | 3763 | return 0; |
80289167 BK |
3764 | } |
3765 | EXPORT_SYMBOL_GPL(ata_sas_port_start); | |
3766 | ||
3767 | /** | |
3768 | * ata_port_stop - Undo ata_sas_port_start() | |
3769 | * @ap: Port to shut down | |
3770 | * | |
80289167 BK |
3771 | * May be used as the port_stop() entry in ata_port_operations. |
3772 | * | |
3773 | * LOCKING: | |
3774 | * Inherited from caller. | |
3775 | */ | |
3776 | ||
3777 | void ata_sas_port_stop(struct ata_port *ap) | |
3778 | { | |
80289167 BK |
3779 | } |
3780 | EXPORT_SYMBOL_GPL(ata_sas_port_stop); | |
3781 | ||
3782 | /** | |
3783 | * ata_sas_port_init - Initialize a SATA device | |
3784 | * @ap: SATA port to initialize | |
3785 | * | |
3786 | * LOCKING: | |
3787 | * PCI/etc. bus probe sem. | |
3788 | * | |
3789 | * RETURNS: | |
3790 | * Zero on success, non-zero on error. | |
3791 | */ | |
3792 | ||
3793 | int ata_sas_port_init(struct ata_port *ap) | |
3794 | { | |
3795 | int rc = ap->ops->port_start(ap); | |
3796 | ||
f3187195 TH |
3797 | if (!rc) { |
3798 | ap->print_id = ata_print_id++; | |
80289167 | 3799 | rc = ata_bus_probe(ap); |
f3187195 | 3800 | } |
80289167 BK |
3801 | |
3802 | return rc; | |
3803 | } | |
3804 | EXPORT_SYMBOL_GPL(ata_sas_port_init); | |
3805 | ||
3806 | /** | |
3807 | * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc | |
3808 | * @ap: SATA port to destroy | |
3809 | * | |
3810 | */ | |
3811 | ||
3812 | void ata_sas_port_destroy(struct ata_port *ap) | |
3813 | { | |
f0d36efd TH |
3814 | if (ap->ops->port_stop) |
3815 | ap->ops->port_stop(ap); | |
80289167 BK |
3816 | kfree(ap); |
3817 | } | |
3818 | EXPORT_SYMBOL_GPL(ata_sas_port_destroy); | |
3819 | ||
3820 | /** | |
3821 | * ata_sas_slave_configure - Default slave_config routine for libata devices | |
3822 | * @sdev: SCSI device to configure | |
3823 | * @ap: ATA port to which SCSI device is attached | |
3824 | * | |
3825 | * RETURNS: | |
3826 | * Zero. | |
3827 | */ | |
3828 | ||
3829 | int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap) | |
3830 | { | |
3831 | ata_scsi_sdev_config(sdev); | |
9af5c9c9 | 3832 | ata_scsi_dev_config(sdev, ap->link.device); |
80289167 BK |
3833 | return 0; |
3834 | } | |
3835 | EXPORT_SYMBOL_GPL(ata_sas_slave_configure); | |
3836 | ||
3837 | /** | |
3838 | * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device | |
3839 | * @cmd: SCSI command to be sent | |
3840 | * @done: Completion function, called when command is complete | |
3841 | * @ap: ATA port to which the command is being sent | |
3842 | * | |
3843 | * RETURNS: | |
08475a19 BK |
3844 | * Return value from __ata_scsi_queuecmd() if @cmd can be queued, |
3845 | * 0 otherwise. | |
80289167 BK |
3846 | */ |
3847 | ||
3848 | int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), | |
3849 | struct ata_port *ap) | |
3850 | { | |
08475a19 BK |
3851 | int rc = 0; |
3852 | ||
80289167 BK |
3853 | ata_scsi_dump_cdb(ap, cmd); |
3854 | ||
2486fa56 | 3855 | if (likely(ata_dev_enabled(ap->link.device))) |
9af5c9c9 | 3856 | rc = __ata_scsi_queuecmd(cmd, done, ap->link.device); |
80289167 BK |
3857 | else { |
3858 | cmd->result = (DID_BAD_TARGET << 16); | |
3859 | done(cmd); | |
3860 | } | |
08475a19 | 3861 | return rc; |
80289167 BK |
3862 | } |
3863 | EXPORT_SYMBOL_GPL(ata_sas_queuecmd); |