hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/ata/libata-scsi.c
....@@ -1,28 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * libata-scsi.c - helper library for ATA
34 *
4
- * Maintained by: Tejun Heo <tj@kernel.org>
5
- * Please ALWAYS copy linux-ide@vger.kernel.org
6
- * on emails.
7
- *
85 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
96 * 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
- *
267 *
278 * libata documentation is available via 'make {ps|pdf}docs',
289 * as Documentation/driver-api/libata.rst
....@@ -30,9 +11,9 @@
3011 * Hardware documentation available from
3112 * - http://www.t10.org/
3213 * - http://www.t13.org/
33
- *
3414 */
3515
16
+#include <linux/compat.h>
3617 #include <linux/slab.h>
3718 #include <linux/kernel.h>
3819 #include <linux/blkdev.h>
....@@ -51,11 +32,12 @@
5132 #include <linux/suspend.h>
5233 #include <asm/unaligned.h>
5334 #include <linux/ioprio.h>
35
+#include <linux/of.h>
5436
5537 #include "libata.h"
5638 #include "libata-transport.h"
5739
58
-#define ATA_SCSI_RBUF_SIZE 4096
40
+#define ATA_SCSI_RBUF_SIZE 576
5941
6042 static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
6143 static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
....@@ -64,8 +46,6 @@
6446
6547 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
6648 const struct scsi_device *scsidev);
67
-static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
68
- const struct scsi_device *scsidev);
6949
7050 #define RW_RECOVERY_MPAGE 0x1
7151 #define RW_RECOVERY_MPAGE_LEN 12
....@@ -105,71 +85,6 @@
10585 0, 30 /* extended self test time, see 05-359r1 */
10686 };
10787
108
-static const char *ata_lpm_policy_names[] = {
109
- [ATA_LPM_UNKNOWN] = "max_performance",
110
- [ATA_LPM_MAX_POWER] = "max_performance",
111
- [ATA_LPM_MED_POWER] = "medium_power",
112
- [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
113
- [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
114
- [ATA_LPM_MIN_POWER] = "min_power",
115
-};
116
-
117
-static ssize_t ata_scsi_lpm_store(struct device *device,
118
- struct device_attribute *attr,
119
- const char *buf, size_t count)
120
-{
121
- struct Scsi_Host *shost = class_to_shost(device);
122
- struct ata_port *ap = ata_shost_to_port(shost);
123
- struct ata_link *link;
124
- struct ata_device *dev;
125
- enum ata_lpm_policy policy;
126
- unsigned long flags;
127
-
128
- /* UNKNOWN is internal state, iterate from MAX_POWER */
129
- for (policy = ATA_LPM_MAX_POWER;
130
- policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
131
- const char *name = ata_lpm_policy_names[policy];
132
-
133
- if (strncmp(name, buf, strlen(name)) == 0)
134
- break;
135
- }
136
- if (policy == ARRAY_SIZE(ata_lpm_policy_names))
137
- return -EINVAL;
138
-
139
- spin_lock_irqsave(ap->lock, flags);
140
-
141
- ata_for_each_link(link, ap, EDGE) {
142
- ata_for_each_dev(dev, &ap->link, ENABLED) {
143
- if (dev->horkage & ATA_HORKAGE_NOLPM) {
144
- count = -EOPNOTSUPP;
145
- goto out_unlock;
146
- }
147
- }
148
- }
149
-
150
- ap->target_lpm_policy = policy;
151
- ata_port_schedule_eh(ap);
152
-out_unlock:
153
- spin_unlock_irqrestore(ap->lock, flags);
154
- return count;
155
-}
156
-
157
-static ssize_t ata_scsi_lpm_show(struct device *dev,
158
- struct device_attribute *attr, char *buf)
159
-{
160
- struct Scsi_Host *shost = class_to_shost(dev);
161
- struct ata_port *ap = ata_shost_to_port(shost);
162
-
163
- if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
164
- return -EINVAL;
165
-
166
- return snprintf(buf, PAGE_SIZE, "%s\n",
167
- ata_lpm_policy_names[ap->target_lpm_policy]);
168
-}
169
-DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
170
- ata_scsi_lpm_show, ata_scsi_lpm_store);
171
-EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
172
-
17388 static ssize_t ata_scsi_park_show(struct device *device,
17489 struct device_attribute *attr, char *buf)
17590 {
....@@ -178,7 +93,7 @@
17893 struct ata_link *link;
17994 struct ata_device *dev;
18095 unsigned long now;
181
- unsigned int uninitialized_var(msecs);
96
+ unsigned int msecs;
18297 int rc = 0;
18398
18499 ap = ata_shost_to_port(sdev->host);
....@@ -273,83 +188,6 @@
273188 ata_scsi_park_show, ata_scsi_park_store);
274189 EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
275190
276
-static ssize_t ata_ncq_prio_enable_show(struct device *device,
277
- struct device_attribute *attr,
278
- char *buf)
279
-{
280
- struct scsi_device *sdev = to_scsi_device(device);
281
- struct ata_port *ap;
282
- struct ata_device *dev;
283
- bool ncq_prio_enable;
284
- int rc = 0;
285
-
286
- ap = ata_shost_to_port(sdev->host);
287
-
288
- spin_lock_irq(ap->lock);
289
- dev = ata_scsi_find_dev(ap, sdev);
290
- if (!dev) {
291
- rc = -ENODEV;
292
- goto unlock;
293
- }
294
-
295
- ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
296
-
297
-unlock:
298
- spin_unlock_irq(ap->lock);
299
-
300
- return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
301
-}
302
-
303
-static ssize_t ata_ncq_prio_enable_store(struct device *device,
304
- struct device_attribute *attr,
305
- const char *buf, size_t len)
306
-{
307
- struct scsi_device *sdev = to_scsi_device(device);
308
- struct ata_port *ap;
309
- struct ata_device *dev;
310
- long int input;
311
- int rc;
312
-
313
- rc = kstrtol(buf, 10, &input);
314
- if (rc)
315
- return rc;
316
- if ((input < 0) || (input > 1))
317
- return -EINVAL;
318
-
319
- ap = ata_shost_to_port(sdev->host);
320
- dev = ata_scsi_find_dev(ap, sdev);
321
- if (unlikely(!dev))
322
- return -ENODEV;
323
-
324
- spin_lock_irq(ap->lock);
325
- if (input)
326
- dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
327
- else
328
- dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
329
-
330
- dev->link->eh_info.action |= ATA_EH_REVALIDATE;
331
- dev->link->eh_info.flags |= ATA_EHI_QUIET;
332
- ata_port_schedule_eh(ap);
333
- spin_unlock_irq(ap->lock);
334
-
335
- ata_port_wait_eh(ap);
336
-
337
- if (input) {
338
- spin_lock_irq(ap->lock);
339
- if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
340
- dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
341
- rc = -EIO;
342
- }
343
- spin_unlock_irq(ap->lock);
344
- }
345
-
346
- return rc ? rc : len;
347
-}
348
-
349
-DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
350
- ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
351
-EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
352
-
353191 void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
354192 u8 sk, u8 asc, u8 ascq)
355193 {
....@@ -398,90 +236,8 @@
398236 field, 0xff, 0);
399237 }
400238
401
-static ssize_t
402
-ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
403
- const char *buf, size_t count)
404
-{
405
- struct Scsi_Host *shost = class_to_shost(dev);
406
- struct ata_port *ap = ata_shost_to_port(shost);
407
- if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
408
- return ap->ops->em_store(ap, buf, count);
409
- return -EINVAL;
410
-}
411
-
412
-static ssize_t
413
-ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
414
- char *buf)
415
-{
416
- struct Scsi_Host *shost = class_to_shost(dev);
417
- struct ata_port *ap = ata_shost_to_port(shost);
418
-
419
- if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
420
- return ap->ops->em_show(ap, buf);
421
- return -EINVAL;
422
-}
423
-DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
424
- ata_scsi_em_message_show, ata_scsi_em_message_store);
425
-EXPORT_SYMBOL_GPL(dev_attr_em_message);
426
-
427
-static ssize_t
428
-ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
429
- char *buf)
430
-{
431
- struct Scsi_Host *shost = class_to_shost(dev);
432
- struct ata_port *ap = ata_shost_to_port(shost);
433
-
434
- return snprintf(buf, 23, "%d\n", ap->em_message_type);
435
-}
436
-DEVICE_ATTR(em_message_type, S_IRUGO,
437
- ata_scsi_em_message_type_show, NULL);
438
-EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
439
-
440
-static ssize_t
441
-ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
442
- char *buf)
443
-{
444
- struct scsi_device *sdev = to_scsi_device(dev);
445
- struct ata_port *ap = ata_shost_to_port(sdev->host);
446
- struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
447
-
448
- if (atadev && ap->ops->sw_activity_show &&
449
- (ap->flags & ATA_FLAG_SW_ACTIVITY))
450
- return ap->ops->sw_activity_show(atadev, buf);
451
- return -EINVAL;
452
-}
453
-
454
-static ssize_t
455
-ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
456
- const char *buf, size_t count)
457
-{
458
- struct scsi_device *sdev = to_scsi_device(dev);
459
- struct ata_port *ap = ata_shost_to_port(sdev->host);
460
- struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
461
- enum sw_activity val;
462
- int rc;
463
-
464
- if (atadev && ap->ops->sw_activity_store &&
465
- (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
466
- val = simple_strtoul(buf, NULL, 0);
467
- switch (val) {
468
- case OFF: case BLINK_ON: case BLINK_OFF:
469
- rc = ap->ops->sw_activity_store(atadev, val);
470
- if (!rc)
471
- return count;
472
- else
473
- return rc;
474
- }
475
- }
476
- return -EINVAL;
477
-}
478
-DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
479
- ata_scsi_activity_store);
480
-EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
481
-
482239 struct device_attribute *ata_common_sdev_attrs[] = {
483240 &dev_attr_unload_heads,
484
- &dev_attr_ncq_prio_enable,
485241 NULL
486242 };
487243 EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
....@@ -514,6 +270,7 @@
514270
515271 return 0;
516272 }
273
+EXPORT_SYMBOL_GPL(ata_std_bios_param);
517274
518275 /**
519276 * ata_scsi_unlock_native_capacity - unlock native capacity
....@@ -543,6 +300,7 @@
543300 spin_unlock_irqrestore(ap->lock, flags);
544301 ata_port_wait_eh(ap);
545302 }
303
+EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
546304
547305 /**
548306 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
....@@ -639,8 +397,8 @@
639397 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
640398 scsi_cmd[6] = args[3];
641399 scsi_cmd[8] = args[1];
642
- scsi_cmd[10] = 0x4f;
643
- scsi_cmd[12] = 0xc2;
400
+ scsi_cmd[10] = ATA_SMART_LBAM_PASS;
401
+ scsi_cmd[12] = ATA_SMART_LBAH_PASS;
644402 } else {
645403 scsi_cmd[6] = args[1];
646404 }
....@@ -777,8 +535,12 @@
777535 return 0;
778536 }
779537
538
+/*
539
+ * This handles both native and compat commands, so anything added
540
+ * here must have a compatible argument, or check in_compat_syscall()
541
+ */
780542 int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
781
- int cmd, void __user *arg)
543
+ unsigned int cmd, void __user *arg)
782544 {
783545 unsigned long val;
784546 int rc = -EINVAL;
....@@ -789,6 +551,10 @@
789551 spin_lock_irqsave(ap->lock, flags);
790552 val = ata_ioc32(ap);
791553 spin_unlock_irqrestore(ap->lock, flags);
554
+#ifdef CONFIG_COMPAT
555
+ if (in_compat_syscall())
556
+ return put_user(val, (compat_ulong_t __user *)arg);
557
+#endif
792558 return put_user(val, (unsigned long __user *)arg);
793559
794560 case HDIO_SET_32BIT:
....@@ -829,7 +595,8 @@
829595 }
830596 EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
831597
832
-int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
598
+int ata_scsi_ioctl(struct scsi_device *scsidev, unsigned int cmd,
599
+ void __user *arg)
833600 {
834601 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
835602 scsidev, cmd, arg);
....@@ -882,7 +649,7 @@
882649 {
883650 struct scsi_cmnd *scmd = qc->scsicmd;
884651
885
- qc->extrabytes = scmd->request->extra_len;
652
+ qc->extrabytes = scmd->extra_len;
886653 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
887654 }
888655
....@@ -1221,7 +988,7 @@
1221988 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
1222989 }
1223990
1224
-static void ata_scsi_sdev_config(struct scsi_device *sdev)
991
+void ata_scsi_sdev_config(struct scsi_device *sdev)
1225992 {
1226993 sdev->use_10_for_rw = 1;
1227994 sdev->use_10_for_ms = 1;
....@@ -1236,7 +1003,7 @@
12361003 }
12371004
12381005 /**
1239
- * atapi_drain_needed - Check whether data transfer may overflow
1006
+ * ata_scsi_dma_need_drain - Check whether data transfer may overflow
12401007 * @rq: request to be checked
12411008 *
12421009 * ATAPI commands which transfer variable length data to host
....@@ -1250,19 +1017,13 @@
12501017 * RETURNS:
12511018 * 1 if ; otherwise, 0.
12521019 */
1253
-static int atapi_drain_needed(struct request *rq)
1020
+bool ata_scsi_dma_need_drain(struct request *rq)
12541021 {
1255
- if (likely(!blk_rq_is_passthrough(rq)))
1256
- return 0;
1257
-
1258
- if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
1259
- return 0;
1260
-
12611022 return atapi_cmd_type(scsi_req(rq)->cmd[0]) == ATAPI_MISC;
12621023 }
1024
+EXPORT_SYMBOL_GPL(ata_scsi_dma_need_drain);
12631025
1264
-static int ata_scsi_dev_config(struct scsi_device *sdev,
1265
- struct ata_device *dev)
1026
+int ata_scsi_dev_config(struct scsi_device *sdev, struct ata_device *dev)
12661027 {
12671028 struct request_queue *q = sdev->request_queue;
12681029
....@@ -1273,21 +1034,21 @@
12731034 blk_queue_max_hw_sectors(q, dev->max_sectors);
12741035
12751036 if (dev->class == ATA_DEV_ATAPI) {
1276
- void *buf;
1277
-
12781037 sdev->sector_size = ATA_SECT_SIZE;
12791038
12801039 /* set DMA padding */
12811040 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
12821041
1283
- /* configure draining */
1284
- buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1285
- if (!buf) {
1042
+ /* make room for appending the drain */
1043
+ blk_queue_max_segments(q, queue_max_segments(q) - 1);
1044
+
1045
+ sdev->dma_drain_len = ATAPI_MAX_DRAIN;
1046
+ sdev->dma_drain_buf = kmalloc(sdev->dma_drain_len,
1047
+ q->bounce_gfp | GFP_KERNEL);
1048
+ if (!sdev->dma_drain_buf) {
12861049 ata_dev_err(dev, "drain buffer allocation failed\n");
12871050 return -ENOMEM;
12881051 }
1289
-
1290
- blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
12911052 } else {
12921053 sdev->sector_size = ata_id_logical_sector_size(dev->id);
12931054 sdev->manage_start_stop = 1;
....@@ -1317,8 +1078,6 @@
13171078 depth = min(ATA_MAX_QUEUE, depth);
13181079 scsi_change_queue_depth(sdev, depth);
13191080 }
1320
-
1321
- blk_queue_flush_queueable(q, false);
13221081
13231082 if (dev->flags & ATA_DFLAG_TRUSTED)
13241083 sdev->security_supported = 1;
....@@ -1352,6 +1111,7 @@
13521111
13531112 return rc;
13541113 }
1114
+EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
13551115
13561116 /**
13571117 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
....@@ -1370,7 +1130,6 @@
13701130 void ata_scsi_slave_destroy(struct scsi_device *sdev)
13711131 {
13721132 struct ata_port *ap = ata_shost_to_port(sdev->host);
1373
- struct request_queue *q = sdev->request_queue;
13741133 unsigned long flags;
13751134 struct ata_device *dev;
13761135
....@@ -1387,75 +1146,9 @@
13871146 }
13881147 spin_unlock_irqrestore(ap->lock, flags);
13891148
1390
- kfree(q->dma_drain_buffer);
1391
- q->dma_drain_buffer = NULL;
1392
- q->dma_drain_size = 0;
1149
+ kfree(sdev->dma_drain_buf);
13931150 }
1394
-
1395
-/**
1396
- * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
1397
- * @ap: ATA port to which the device change the queue depth
1398
- * @sdev: SCSI device to configure queue depth for
1399
- * @queue_depth: new queue depth
1400
- *
1401
- * libsas and libata have different approaches for associating a sdev to
1402
- * its ata_port.
1403
- *
1404
- */
1405
-int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1406
- int queue_depth)
1407
-{
1408
- struct ata_device *dev;
1409
- unsigned long flags;
1410
-
1411
- if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1412
- return sdev->queue_depth;
1413
-
1414
- dev = ata_scsi_find_dev(ap, sdev);
1415
- if (!dev || !ata_dev_enabled(dev))
1416
- return sdev->queue_depth;
1417
-
1418
- /* NCQ enabled? */
1419
- spin_lock_irqsave(ap->lock, flags);
1420
- dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1421
- if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1422
- dev->flags |= ATA_DFLAG_NCQ_OFF;
1423
- queue_depth = 1;
1424
- }
1425
- spin_unlock_irqrestore(ap->lock, flags);
1426
-
1427
- /* limit and apply queue depth */
1428
- queue_depth = min(queue_depth, sdev->host->can_queue);
1429
- queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1430
- queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1431
-
1432
- if (sdev->queue_depth == queue_depth)
1433
- return -EINVAL;
1434
-
1435
- return scsi_change_queue_depth(sdev, queue_depth);
1436
-}
1437
-
1438
-/**
1439
- * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1440
- * @sdev: SCSI device to configure queue depth for
1441
- * @queue_depth: new queue depth
1442
- *
1443
- * This is libata standard hostt->change_queue_depth callback.
1444
- * SCSI will call into this callback when user tries to set queue
1445
- * depth via sysfs.
1446
- *
1447
- * LOCKING:
1448
- * SCSI layer (we don't care)
1449
- *
1450
- * RETURNS:
1451
- * Newly configured queue depth.
1452
- */
1453
-int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1454
-{
1455
- struct ata_port *ap = ata_shost_to_port(sdev->host);
1456
-
1457
- return __ata_change_queue_depth(ap, sdev, queue_depth);
1458
-}
1151
+EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
14591152
14601153 /**
14611154 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
....@@ -2362,10 +2055,6 @@
23622055 */
23632056 static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
23642057 {
2365
- struct ata_taskfile tf;
2366
-
2367
- memset(&tf, 0, sizeof(tf));
2368
-
23692058 rbuf[1] = 0x89; /* our page code */
23702059 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
23712060 rbuf[3] = (0x238 & 0xff);
....@@ -2374,14 +2063,14 @@
23742063 memcpy(&rbuf[16], "libata ", 16);
23752064 memcpy(&rbuf[32], DRV_VERSION, 4);
23762065
2377
- /* we don't store the ATA device signature, so we fake it */
2378
-
2379
- tf.command = ATA_DRDY; /* really, this is Status reg */
2380
- tf.lbal = 0x1;
2381
- tf.nsect = 0x1;
2382
-
2383
- ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
23842066 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
2067
+ rbuf[37] = (1 << 7); /* bit 7 indicates Command FIS */
2068
+ /* TODO: PMP? */
2069
+
2070
+ /* we don't store the ATA device signature, so we fake it */
2071
+ rbuf[38] = ATA_DRDY; /* really, this is Status reg */
2072
+ rbuf[40] = 0x1;
2073
+ rbuf[48] = 0x1;
23852074
23862075 rbuf[56] = ATA_CMD_ID_ATA;
23872076
....@@ -3017,7 +2706,7 @@
30172706 * This inconsistency confuses several controllers which
30182707 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
30192708 * These controllers use actual number of transferred bytes to
3020
- * update DMA poitner and transfer of 4n+2 bytes make those
2709
+ * update DMA pointer and transfer of 4n+2 bytes make those
30212710 * controller push DMA pointer by 4n+4 bytes because SATA data
30222711 * FISes are aligned to 4 bytes. This causes data corruption
30232712 * and buffer overrun.
....@@ -3053,17 +2742,35 @@
30532742 return 0;
30542743 }
30552744
3056
-static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
2745
+static struct ata_device *ata_find_dev(struct ata_port *ap, unsigned int devno)
30572746 {
3058
- if (!sata_pmp_attached(ap)) {
3059
- if (likely(devno >= 0 &&
3060
- devno < ata_link_max_devices(&ap->link)))
2747
+ /*
2748
+ * For the non-PMP case, ata_link_max_devices() returns 1 (SATA case),
2749
+ * or 2 (IDE master + slave case). However, the former case includes
2750
+ * libsas hosted devices which are numbered per scsi host, leading
2751
+ * to devno potentially being larger than 0 but with each struct
2752
+ * ata_device having its own struct ata_port and struct ata_link.
2753
+ * To accommodate these, ignore devno and always use device number 0.
2754
+ */
2755
+ if (likely(!sata_pmp_attached(ap))) {
2756
+ int link_max_devices = ata_link_max_devices(&ap->link);
2757
+
2758
+ if (link_max_devices == 1)
2759
+ return &ap->link.device[0];
2760
+
2761
+ if (devno < link_max_devices)
30612762 return &ap->link.device[devno];
3062
- } else {
3063
- if (likely(devno >= 0 &&
3064
- devno < ap->nr_pmp_links))
3065
- return &ap->pmp_link[devno].device[0];
2763
+
2764
+ return NULL;
30662765 }
2766
+
2767
+ /*
2768
+ * For PMP-attached devices, the device number corresponds to C
2769
+ * (channel) of SCSI [H:C:I:L], indicating the port pmp link
2770
+ * for the device.
2771
+ */
2772
+ if (devno < ap->nr_pmp_links)
2773
+ return &ap->pmp_link[devno].device[0];
30672774
30682775 return NULL;
30692776 }
....@@ -3103,7 +2810,7 @@
31032810 * RETURNS:
31042811 * Associated ATA device, or %NULL if not found.
31052812 */
3106
-static struct ata_device *
2813
+struct ata_device *
31072814 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
31082815 {
31092816 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
....@@ -3614,6 +3321,7 @@
36143321 case REPORT_LUNS:
36153322 case REQUEST_SENSE:
36163323 case SYNCHRONIZE_CACHE:
3324
+ case SYNCHRONIZE_CACHE_16:
36173325 case REZERO_UNIT:
36183326 case SEEK_6:
36193327 case SEEK_10:
....@@ -4280,6 +3988,7 @@
42803988 return ata_scsi_write_same_xlat;
42813989
42823990 case SYNCHRONIZE_CACHE:
3991
+ case SYNCHRONIZE_CACHE_16:
42833992 if (ata_try_flush_cache(dev))
42843993 return ata_scsi_flush_xlat;
42853994 break;
....@@ -4327,8 +4036,7 @@
43274036 * Prints the contents of a SCSI command via printk().
43284037 */
43294038
4330
-static inline void ata_scsi_dump_cdb(struct ata_port *ap,
4331
- struct scsi_cmnd *cmd)
4039
+void ata_scsi_dump_cdb(struct ata_port *ap, struct scsi_cmnd *cmd)
43324040 {
43334041 #ifdef ATA_VERBOSE_DEBUG
43344042 struct scsi_device *scsidev = cmd->device;
....@@ -4340,47 +4048,53 @@
43404048 #endif
43414049 }
43424050
4343
-static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
4344
- struct ata_device *dev)
4051
+int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, struct ata_device *dev)
43454052 {
4053
+ struct ata_port *ap = dev->link->ap;
43464054 u8 scsi_op = scmd->cmnd[0];
43474055 ata_xlat_func_t xlat_func;
4348
- int rc = 0;
4056
+
4057
+ /*
4058
+ * scsi_queue_rq() will defer commands if scsi_host_in_recovery().
4059
+ * However, this check is done without holding the ap->lock (a libata
4060
+ * specific lock), so we can have received an error irq since then,
4061
+ * therefore we must check if EH is pending, while holding ap->lock.
4062
+ */
4063
+ if (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS))
4064
+ return SCSI_MLQUEUE_DEVICE_BUSY;
4065
+
4066
+ if (unlikely(!scmd->cmd_len))
4067
+ goto bad_cdb_len;
43494068
43504069 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
4351
- if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
4070
+ if (unlikely(scmd->cmd_len > dev->cdb_len))
43524071 goto bad_cdb_len;
43534072
43544073 xlat_func = ata_get_xlat_func(dev, scsi_op);
4355
- } else {
4356
- if (unlikely(!scmd->cmd_len))
4074
+ } else if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4075
+ /* relay SCSI command to ATAPI device */
4076
+ int len = COMMAND_SIZE(scsi_op);
4077
+
4078
+ if (unlikely(len > scmd->cmd_len ||
4079
+ len > dev->cdb_len ||
4080
+ scmd->cmd_len > ATAPI_CDB_LEN))
43574081 goto bad_cdb_len;
43584082
4359
- xlat_func = NULL;
4360
- if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4361
- /* relay SCSI command to ATAPI device */
4362
- int len = COMMAND_SIZE(scsi_op);
4363
- if (unlikely(len > scmd->cmd_len ||
4364
- len > dev->cdb_len ||
4365
- scmd->cmd_len > ATAPI_CDB_LEN))
4366
- goto bad_cdb_len;
4083
+ xlat_func = atapi_xlat;
4084
+ } else {
4085
+ /* ATA_16 passthru, treat as an ATA command */
4086
+ if (unlikely(scmd->cmd_len > 16))
4087
+ goto bad_cdb_len;
43674088
4368
- xlat_func = atapi_xlat;
4369
- } else {
4370
- /* ATA_16 passthru, treat as an ATA command */
4371
- if (unlikely(scmd->cmd_len > 16))
4372
- goto bad_cdb_len;
4373
-
4374
- xlat_func = ata_get_xlat_func(dev, scsi_op);
4375
- }
4089
+ xlat_func = ata_get_xlat_func(dev, scsi_op);
43764090 }
43774091
43784092 if (xlat_func)
4379
- rc = ata_scsi_translate(dev, scmd, xlat_func);
4380
- else
4381
- ata_scsi_simulate(dev, scmd);
4093
+ return ata_scsi_translate(dev, scmd, xlat_func);
43824094
4383
- return rc;
4095
+ ata_scsi_simulate(dev, scmd);
4096
+
4097
+ return 0;
43844098
43854099 bad_cdb_len:
43864100 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
....@@ -4435,6 +4149,7 @@
44354149
44364150 return rc;
44374151 }
4152
+EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
44384153
44394154 /**
44404155 * ata_scsi_simulate - simulate SCSI command on ATA device
....@@ -4491,7 +4206,7 @@
44914206 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
44924207 break;
44934208 }
4494
- /* Fallthrough */
4209
+ fallthrough;
44954210 default:
44964211 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
44974212 break;
....@@ -4527,7 +4242,8 @@
45274242 * turning this into a no-op.
45284243 */
45294244 case SYNCHRONIZE_CACHE:
4530
- /* fall through */
4245
+ case SYNCHRONIZE_CACHE_16:
4246
+ fallthrough;
45314247
45324248 /* no-op's, complete with success */
45334249 case REZERO_UNIT:
....@@ -4543,7 +4259,7 @@
45434259 break;
45444260
45454261 case MAINTENANCE_IN:
4546
- if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
4262
+ if ((scsicmd[1] & 0x1f) == MI_REPORT_SUPPORTED_OPERATION_CODES)
45474263 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
45484264 else
45494265 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
....@@ -4607,6 +4323,34 @@
46074323 return rc;
46084324 }
46094325
4326
+#ifdef CONFIG_OF
4327
+static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4328
+{
4329
+ struct scsi_device *sdev = dev->sdev;
4330
+ struct device *d = ap->host->dev;
4331
+ struct device_node *np = d->of_node;
4332
+ struct device_node *child;
4333
+
4334
+ for_each_available_child_of_node(np, child) {
4335
+ int ret;
4336
+ u32 val;
4337
+
4338
+ ret = of_property_read_u32(child, "reg", &val);
4339
+ if (ret)
4340
+ continue;
4341
+ if (val == dev->devno) {
4342
+ dev_dbg(d, "found matching device node\n");
4343
+ sdev->sdev_gendev.of_node = child;
4344
+ return;
4345
+ }
4346
+ }
4347
+}
4348
+#else
4349
+static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4350
+{
4351
+}
4352
+#endif
4353
+
46104354 void ata_scsi_scan_host(struct ata_port *ap, int sync)
46114355 {
46124356 int tries = 5;
....@@ -4632,6 +4376,7 @@
46324376 NULL);
46334377 if (!IS_ERR(sdev)) {
46344378 dev->sdev = sdev;
4379
+ ata_scsi_assign_ofnode(dev, ap);
46354380 scsi_device_put(sdev);
46364381 } else {
46374382 dev->sdev = NULL;
....@@ -4825,27 +4570,6 @@
48254570 return;
48264571 }
48274572
4828
- /*
4829
- * XXX - UGLY HACK
4830
- *
4831
- * The block layer suspend/resume path is fundamentally broken due
4832
- * to freezable kthreads and workqueue and may deadlock if a block
4833
- * device gets removed while resume is in progress. I don't know
4834
- * what the solution is short of removing freezable kthreads and
4835
- * workqueues altogether.
4836
- *
4837
- * The following is an ugly hack to avoid kicking off device
4838
- * removal while freezer is active. This is a joke but does avoid
4839
- * this particular deadlock scenario.
4840
- *
4841
- * https://bugzilla.kernel.org/show_bug.cgi?id=62801
4842
- * http://marc.info/?l=linux-kernel&m=138695698516487
4843
- */
4844
-#ifdef CONFIG_FREEZER
4845
- while (pm_freezing)
4846
- msleep(10);
4847
-#endif
4848
-
48494573 DPRINTK("ENTER\n");
48504574 mutex_lock(&ap->scsi_scan_mutex);
48514575
....@@ -4974,215 +4698,4 @@
49744698
49754699 spin_unlock_irqrestore(ap->lock, flags);
49764700 mutex_unlock(&ap->scsi_scan_mutex);
4977
-}
4978
-
4979
-/**
4980
- * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
4981
- * @host: ATA host container for all SAS ports
4982
- * @port_info: Information from low-level host driver
4983
- * @shost: SCSI host that the scsi device is attached to
4984
- *
4985
- * LOCKING:
4986
- * PCI/etc. bus probe sem.
4987
- *
4988
- * RETURNS:
4989
- * ata_port pointer on success / NULL on failure.
4990
- */
4991
-
4992
-struct ata_port *ata_sas_port_alloc(struct ata_host *host,
4993
- struct ata_port_info *port_info,
4994
- struct Scsi_Host *shost)
4995
-{
4996
- struct ata_port *ap;
4997
-
4998
- ap = ata_port_alloc(host);
4999
- if (!ap)
5000
- return NULL;
5001
-
5002
- ap->port_no = 0;
5003
- ap->lock = &host->lock;
5004
- ap->pio_mask = port_info->pio_mask;
5005
- ap->mwdma_mask = port_info->mwdma_mask;
5006
- ap->udma_mask = port_info->udma_mask;
5007
- ap->flags |= port_info->flags;
5008
- ap->ops = port_info->port_ops;
5009
- ap->cbl = ATA_CBL_SATA;
5010
-
5011
- return ap;
5012
-}
5013
-EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
5014
-
5015
-/**
5016
- * ata_sas_port_start - Set port up for dma.
5017
- * @ap: Port to initialize
5018
- *
5019
- * Called just after data structures for each port are
5020
- * initialized.
5021
- *
5022
- * May be used as the port_start() entry in ata_port_operations.
5023
- *
5024
- * LOCKING:
5025
- * Inherited from caller.
5026
- */
5027
-int ata_sas_port_start(struct ata_port *ap)
5028
-{
5029
- /*
5030
- * the port is marked as frozen at allocation time, but if we don't
5031
- * have new eh, we won't thaw it
5032
- */
5033
- if (!ap->ops->error_handler)
5034
- ap->pflags &= ~ATA_PFLAG_FROZEN;
5035
- return 0;
5036
-}
5037
-EXPORT_SYMBOL_GPL(ata_sas_port_start);
5038
-
5039
-/**
5040
- * ata_port_stop - Undo ata_sas_port_start()
5041
- * @ap: Port to shut down
5042
- *
5043
- * May be used as the port_stop() entry in ata_port_operations.
5044
- *
5045
- * LOCKING:
5046
- * Inherited from caller.
5047
- */
5048
-
5049
-void ata_sas_port_stop(struct ata_port *ap)
5050
-{
5051
-}
5052
-EXPORT_SYMBOL_GPL(ata_sas_port_stop);
5053
-
5054
-/**
5055
- * ata_sas_async_probe - simply schedule probing and return
5056
- * @ap: Port to probe
5057
- *
5058
- * For batch scheduling of probe for sas attached ata devices, assumes
5059
- * the port has already been through ata_sas_port_init()
5060
- */
5061
-void ata_sas_async_probe(struct ata_port *ap)
5062
-{
5063
- __ata_port_probe(ap);
5064
-}
5065
-EXPORT_SYMBOL_GPL(ata_sas_async_probe);
5066
-
5067
-int ata_sas_sync_probe(struct ata_port *ap)
5068
-{
5069
- return ata_port_probe(ap);
5070
-}
5071
-EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
5072
-
5073
-
5074
-/**
5075
- * ata_sas_port_init - Initialize a SATA device
5076
- * @ap: SATA port to initialize
5077
- *
5078
- * LOCKING:
5079
- * PCI/etc. bus probe sem.
5080
- *
5081
- * RETURNS:
5082
- * Zero on success, non-zero on error.
5083
- */
5084
-
5085
-int ata_sas_port_init(struct ata_port *ap)
5086
-{
5087
- int rc = ap->ops->port_start(ap);
5088
-
5089
- if (rc)
5090
- return rc;
5091
- ap->print_id = atomic_inc_return(&ata_print_id);
5092
- return 0;
5093
-}
5094
-EXPORT_SYMBOL_GPL(ata_sas_port_init);
5095
-
5096
-int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
5097
-{
5098
- return ata_tport_add(parent, ap);
5099
-}
5100
-EXPORT_SYMBOL_GPL(ata_sas_tport_add);
5101
-
5102
-void ata_sas_tport_delete(struct ata_port *ap)
5103
-{
5104
- ata_tport_delete(ap);
5105
-}
5106
-EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
5107
-
5108
-/**
5109
- * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
5110
- * @ap: SATA port to destroy
5111
- *
5112
- */
5113
-
5114
-void ata_sas_port_destroy(struct ata_port *ap)
5115
-{
5116
- if (ap->ops->port_stop)
5117
- ap->ops->port_stop(ap);
5118
- kfree(ap);
5119
-}
5120
-EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
5121
-
5122
-/**
5123
- * ata_sas_slave_configure - Default slave_config routine for libata devices
5124
- * @sdev: SCSI device to configure
5125
- * @ap: ATA port to which SCSI device is attached
5126
- *
5127
- * RETURNS:
5128
- * Zero.
5129
- */
5130
-
5131
-int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
5132
-{
5133
- ata_scsi_sdev_config(sdev);
5134
- ata_scsi_dev_config(sdev, ap->link.device);
5135
- return 0;
5136
-}
5137
-EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
5138
-
5139
-/**
5140
- * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
5141
- * @cmd: SCSI command to be sent
5142
- * @ap: ATA port to which the command is being sent
5143
- *
5144
- * RETURNS:
5145
- * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
5146
- * 0 otherwise.
5147
- */
5148
-
5149
-int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
5150
-{
5151
- int rc = 0;
5152
-
5153
- ata_scsi_dump_cdb(ap, cmd);
5154
-
5155
- if (likely(ata_dev_enabled(ap->link.device)))
5156
- rc = __ata_scsi_queuecmd(cmd, ap->link.device);
5157
- else {
5158
- cmd->result = (DID_BAD_TARGET << 16);
5159
- cmd->scsi_done(cmd);
5160
- }
5161
- return rc;
5162
-}
5163
-EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
5164
-
5165
-int ata_sas_allocate_tag(struct ata_port *ap)
5166
-{
5167
- unsigned int max_queue = ap->host->n_tags;
5168
- unsigned int i, tag;
5169
-
5170
- for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
5171
- tag = tag < max_queue ? tag : 0;
5172
-
5173
- /* the last tag is reserved for internal command. */
5174
- if (ata_tag_internal(tag))
5175
- continue;
5176
-
5177
- if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
5178
- ap->sas_last_tag = tag;
5179
- return tag;
5180
- }
5181
- }
5182
- return -1;
5183
-}
5184
-
5185
-void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
5186
-{
5187
- clear_bit(tag, &ap->sas_tag_allocated);
51884701 }