Lines Matching +full:sata +full:- +full:port
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SATA specific part of ATA helper library
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
17 #include "libata-transport.h"
28 * sata_scr_valid - test whether SCRs are accessible
41 struct ata_port *ap = link->ap; in sata_scr_valid()
43 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read; in sata_scr_valid()
48 * sata_scr_read - read SCR register of the specified port
54 * guaranteed to succeed if @link is ap->link, the cable type of
55 * the port is SATA and the port implements ->scr_read.
58 * None if @link is ap->link. Kernel thread context otherwise.
67 return link->ap->ops->scr_read(link, reg, val); in sata_scr_read()
68 return -EOPNOTSUPP; in sata_scr_read()
76 * sata_scr_write - write SCR register of the specified port
82 * guaranteed to succeed if @link is ap->link, the cable type of
83 * the port is SATA and the port implements ->scr_read.
86 * None if @link is ap->link. Kernel thread context otherwise.
95 return link->ap->ops->scr_write(link, reg, val); in sata_scr_write()
96 return -EOPNOTSUPP; in sata_scr_write()
104 * sata_scr_write_flush - write SCR register of the specified port and flush
113 * None if @link is ap->link. Kernel thread context otherwise.
124 rc = link->ap->ops->scr_write(link, reg, val); in sata_scr_write_flush()
126 rc = link->ap->ops->scr_read(link, reg, &val); in sata_scr_write_flush()
129 return -EOPNOTSUPP; in sata_scr_write_flush()
137 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
139 * @pmp: Port multiplier port
144 * FIS structure (Register - Host to Device).
151 fis[0] = 0x27; /* Register - Host to Device FIS */ in ata_tf_to_fis()
152 fis[1] = pmp & 0xf; /* Port multiplier number*/ in ata_tf_to_fis()
156 fis[2] = tf->command; in ata_tf_to_fis()
157 fis[3] = tf->feature; in ata_tf_to_fis()
159 fis[4] = tf->lbal; in ata_tf_to_fis()
160 fis[5] = tf->lbam; in ata_tf_to_fis()
161 fis[6] = tf->lbah; in ata_tf_to_fis()
162 fis[7] = tf->device; in ata_tf_to_fis()
164 fis[8] = tf->hob_lbal; in ata_tf_to_fis()
165 fis[9] = tf->hob_lbam; in ata_tf_to_fis()
166 fis[10] = tf->hob_lbah; in ata_tf_to_fis()
167 fis[11] = tf->hob_feature; in ata_tf_to_fis()
169 fis[12] = tf->nsect; in ata_tf_to_fis()
170 fis[13] = tf->hob_nsect; in ata_tf_to_fis()
172 fis[15] = tf->ctl; in ata_tf_to_fis()
174 fis[16] = tf->auxiliary & 0xff; in ata_tf_to_fis()
175 fis[17] = (tf->auxiliary >> 8) & 0xff; in ata_tf_to_fis()
176 fis[18] = (tf->auxiliary >> 16) & 0xff; in ata_tf_to_fis()
177 fis[19] = (tf->auxiliary >> 24) & 0xff; in ata_tf_to_fis()
182 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
194 tf->command = fis[2]; /* status */ in ata_tf_from_fis()
195 tf->feature = fis[3]; /* error */ in ata_tf_from_fis()
197 tf->lbal = fis[4]; in ata_tf_from_fis()
198 tf->lbam = fis[5]; in ata_tf_from_fis()
199 tf->lbah = fis[6]; in ata_tf_from_fis()
200 tf->device = fis[7]; in ata_tf_from_fis()
202 tf->hob_lbal = fis[8]; in ata_tf_from_fis()
203 tf->hob_lbam = fis[9]; in ata_tf_from_fis()
204 tf->hob_lbah = fis[10]; in ata_tf_from_fis()
206 tf->nsect = fis[12]; in ata_tf_from_fis()
207 tf->hob_nsect = fis[13]; in ata_tf_from_fis()
212 * sata_link_debounce - debounce SATA phy status
213 * @link: ATA link to debounce SATA phy status for
231 * 0 on success, -errno on failure.
254 ata_msleep(link->ap, interval); in sata_link_debounce()
274 * -EPIPE to tell upper layer to lower link speed. in sata_link_debounce()
277 return -EPIPE; in sata_link_debounce()
283 * sata_link_resume - resume SATA link
284 * @link: ATA link to resume SATA
288 * Resume SATA phy @link and debounce it.
294 * 0 on success, -errno on failure.
320 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY)) in sata_link_resume()
321 ata_msleep(link->ap, 200); in sata_link_resume()
326 } while ((scontrol & 0xf0f) != 0x300 && --tries); in sata_link_resume()
336 ATA_LINK_RESUME_TRIES - tries); in sata_link_resume()
345 return rc != -EINVAL ? rc : 0; in sata_link_resume()
350 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
365 * 0 on success, -errno otherwise.
370 struct ata_eh_context *ehc = &link->eh_context; in sata_link_scr_lpm()
401 /* empty port, power off */ in sata_link_scr_lpm()
419 ehc->i.serror &= ~SERR_PHYRDY_CHG; in sata_link_scr_lpm()
426 struct ata_link *host_link = &link->ap->link; in __sata_set_spd_needed()
429 limit = link->sata_spd_limit; in __sata_set_spd_needed()
435 if (!ata_is_host_link(link) && host_link->sata_spd) in __sata_set_spd_needed()
436 limit &= (1 << host_link->sata_spd) - 1; in __sata_set_spd_needed()
450 * sata_set_spd_needed - is SATA spd configuration needed
454 * @link->sata_spd_limit. This function is used to determine
455 * whether hardreset is necessary to apply SATA spd
462 * 1 if SATA spd configuration is needed, 0 otherwise.
475 * sata_set_spd - set SATA spd according to spd limit
476 * @link: Link to set SATA spd for
478 * Set SATA spd of @link according to sata_spd_limit.
506 * sata_link_hardreset - reset link via SATA phy reset
513 * SATA phy-reset @link using DET bits of SControl register.
527 * 0 on success, -errno otherwise.
542 /* SATA spec says nothing about how to reconfigure in sata_link_hardreset()
567 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1 in sata_link_hardreset()
570 ata_msleep(link->ap, 1); in sata_link_hardreset()
580 /* Link is online. From this point, -ENODEV too is an error. */ in sata_link_hardreset()
584 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) { in sata_link_hardreset()
585 /* If PMP is supported, we have to do follow-up SRST. in sata_link_hardreset()
587 * the first port is empty. Wait only for in sata_link_hardreset()
599 rc = -EAGAIN; in sata_link_hardreset()
607 if (rc && rc != -EAGAIN) { in sata_link_hardreset()
619 * ata_qc_complete_multiple - Complete multiple qcs successfully
620 * @ap: port in question
623 * Complete in-flight commands. This functions is meant to be
624 * called from low-level driver's interrupt routine to complete
625 * requests normally. ap->qc_active and @qc_active is compared
636 * Number of completed commands on success, -errno otherwise.
640 u64 done_mask, ap_qc_active = ap->qc_active; in ata_qc_complete_multiple()
644 * If the internal tag is set on ap->qc_active, then we care about in ata_qc_complete_multiple()
656 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n", in ata_qc_complete_multiple()
657 ap->qc_active, qc_active); in ata_qc_complete_multiple()
658 return -EINVAL; in ata_qc_complete_multiple()
678 * ata_slave_link_init - initialize slave link
679 * @ap: port to initialize slave link for
682 * link handling on the port.
684 * In libata, a port contains links and a link contains devices.
686 * there can be multiple fan-out links. On SATA, there's usually
687 * a single device connected to a link but PATA and SATA
688 * controllers emulating TF based interface can have two - master
692 * abstraction too well - SATA controllers which emulate TF
704 * handling, separate @ap->slave_link is used. All dirty details
710 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
711 * softreset(M) -> postreset(M) -> postreset(S)
721 * 0 on success, -errno on failure.
727 WARN_ON(ap->slave_link); in ata_slave_link_init()
728 WARN_ON(ap->flags & ATA_FLAG_PMP); in ata_slave_link_init()
732 return -ENOMEM; in ata_slave_link_init()
735 ap->slave_link = link; in ata_slave_link_init()
741 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
754 unsigned long lpm_timeout = link->last_lpm_change + in sata_lpm_ignore_phy_events()
758 if (link->lpm_policy > ATA_LPM_MAX_POWER) in sata_lpm_ignore_phy_events()
764 if ((link->flags & ATA_LFLAG_CHANGED) && in sata_lpm_ignore_phy_events()
801 return -EINVAL; in ata_scsi_lpm_store()
803 spin_lock_irqsave(ap->lock, flags); in ata_scsi_lpm_store()
806 ata_for_each_dev(dev, &ap->link, ENABLED) { in ata_scsi_lpm_store()
807 if (dev->horkage & ATA_HORKAGE_NOLPM) { in ata_scsi_lpm_store()
808 count = -EOPNOTSUPP; in ata_scsi_lpm_store()
814 ap->target_lpm_policy = policy; in ata_scsi_lpm_store()
817 spin_unlock_irqrestore(ap->lock, flags); in ata_scsi_lpm_store()
827 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names)) in ata_scsi_lpm_show()
828 return -EINVAL; in ata_scsi_lpm_show()
831 ata_lpm_policy_names[ap->target_lpm_policy]); in ata_scsi_lpm_show()
847 ap = ata_shost_to_port(sdev->host); in ata_ncq_prio_enable_show()
849 spin_lock_irq(ap->lock); in ata_ncq_prio_enable_show()
852 rc = -ENODEV; in ata_ncq_prio_enable_show()
856 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE; in ata_ncq_prio_enable_show()
859 spin_unlock_irq(ap->lock); in ata_ncq_prio_enable_show()
878 return -EINVAL; in ata_ncq_prio_enable_store()
880 ap = ata_shost_to_port(sdev->host); in ata_ncq_prio_enable_store()
883 return -ENODEV; in ata_ncq_prio_enable_store()
885 spin_lock_irq(ap->lock); in ata_ncq_prio_enable_store()
887 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE; in ata_ncq_prio_enable_store()
889 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE; in ata_ncq_prio_enable_store()
891 dev->link->eh_info.action |= ATA_EH_REVALIDATE; in ata_ncq_prio_enable_store()
892 dev->link->eh_info.flags |= ATA_EHI_QUIET; in ata_ncq_prio_enable_store()
894 spin_unlock_irq(ap->lock); in ata_ncq_prio_enable_store()
899 spin_lock_irq(ap->lock); in ata_ncq_prio_enable_store()
900 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) { in ata_ncq_prio_enable_store()
901 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE; in ata_ncq_prio_enable_store()
902 rc = -EIO; in ata_ncq_prio_enable_store()
904 spin_unlock_irq(ap->lock); in ata_ncq_prio_enable_store()
927 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM)) in ata_scsi_em_message_store()
928 return ap->ops->em_store(ap, buf, count); in ata_scsi_em_message_store()
929 return -EINVAL; in ata_scsi_em_message_store()
939 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM)) in ata_scsi_em_message_show()
940 return ap->ops->em_show(ap, buf); in ata_scsi_em_message_show()
941 return -EINVAL; in ata_scsi_em_message_show()
954 return snprintf(buf, 23, "%d\n", ap->em_message_type); in ata_scsi_em_message_type_show()
965 struct ata_port *ap = ata_shost_to_port(sdev->host); in ata_scsi_activity_show()
968 if (atadev && ap->ops->sw_activity_show && in ata_scsi_activity_show()
969 (ap->flags & ATA_FLAG_SW_ACTIVITY)) in ata_scsi_activity_show()
970 return ap->ops->sw_activity_show(atadev, buf); in ata_scsi_activity_show()
971 return -EINVAL; in ata_scsi_activity_show()
979 struct ata_port *ap = ata_shost_to_port(sdev->host); in ata_scsi_activity_store()
984 if (atadev && ap->ops->sw_activity_store && in ata_scsi_activity_store()
985 (ap->flags & ATA_FLAG_SW_ACTIVITY)) { in ata_scsi_activity_store()
989 rc = ap->ops->sw_activity_store(atadev, val); in ata_scsi_activity_store()
996 return -EINVAL; in ata_scsi_activity_store()
1003 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
1004 * @ap: ATA port to which the device change the queue depth
1018 if (queue_depth < 1 || queue_depth == sdev->queue_depth) in __ata_change_queue_depth()
1019 return sdev->queue_depth; in __ata_change_queue_depth()
1023 return sdev->queue_depth; in __ata_change_queue_depth()
1026 spin_lock_irqsave(ap->lock, flags); in __ata_change_queue_depth()
1027 dev->flags &= ~ATA_DFLAG_NCQ_OFF; in __ata_change_queue_depth()
1029 dev->flags |= ATA_DFLAG_NCQ_OFF; in __ata_change_queue_depth()
1032 spin_unlock_irqrestore(ap->lock, flags); in __ata_change_queue_depth()
1035 queue_depth = min(queue_depth, sdev->host->can_queue); in __ata_change_queue_depth()
1036 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id)); in __ata_change_queue_depth()
1039 if (sdev->queue_depth == queue_depth) in __ata_change_queue_depth()
1040 return -EINVAL; in __ata_change_queue_depth()
1047 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1051 * This is libata standard hostt->change_queue_depth callback.
1063 struct ata_port *ap = ata_shost_to_port(sdev->host); in ata_scsi_change_queue_depth()
1070 * port_alloc - Allocate port for a SAS attached SATA device
1072 * @port_info: Information from low-level host driver
1092 ap->port_no = 0; in ata_sas_port_alloc()
1093 ap->lock = &host->lock; in ata_sas_port_alloc()
1094 ap->pio_mask = port_info->pio_mask; in ata_sas_port_alloc()
1095 ap->mwdma_mask = port_info->mwdma_mask; in ata_sas_port_alloc()
1096 ap->udma_mask = port_info->udma_mask; in ata_sas_port_alloc()
1097 ap->flags |= port_info->flags; in ata_sas_port_alloc()
1098 ap->ops = port_info->port_ops; in ata_sas_port_alloc()
1099 ap->cbl = ATA_CBL_SATA; in ata_sas_port_alloc()
1106 * ata_sas_port_start - Set port up for dma.
1107 * @ap: Port to initialize
1109 * Called just after data structures for each port are
1120 * the port is marked as frozen at allocation time, but if we don't in ata_sas_port_start()
1123 if (!ap->ops->error_handler) in ata_sas_port_start()
1124 ap->pflags &= ~ATA_PFLAG_FROZEN; in ata_sas_port_start()
1130 * ata_port_stop - Undo ata_sas_port_start()
1131 * @ap: Port to shut down
1145 * ata_sas_async_probe - simply schedule probing and return
1146 * @ap: Port to probe
1149 * the port has already been through ata_sas_port_init()
1165 * ata_sas_port_init - Initialize a SATA device
1166 * @ap: SATA port to initialize
1172 * Zero on success, non-zero on error.
1177 int rc = ap->ops->port_start(ap); in ata_sas_port_init()
1181 ap->print_id = atomic_inc_return(&ata_print_id); in ata_sas_port_init()
1199 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
1200 * @ap: SATA port to destroy
1206 if (ap->ops->port_stop) in ata_sas_port_destroy()
1207 ap->ops->port_stop(ap); in ata_sas_port_destroy()
1213 * ata_sas_slave_configure - Default slave_config routine for libata devices
1215 * @ap: ATA port to which SCSI device is attached
1224 ata_scsi_dev_config(sdev, ap->link.device); in ata_sas_slave_configure()
1230 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
1232 * @ap: ATA port to which the command is being sent
1245 if (likely(ata_dev_enabled(ap->link.device))) in ata_sas_queuecmd()
1246 rc = __ata_scsi_queuecmd(cmd, ap->link.device); in ata_sas_queuecmd()
1248 cmd->result = (DID_BAD_TARGET << 16); in ata_sas_queuecmd()
1249 cmd->scsi_done(cmd); in ata_sas_queuecmd()
1257 unsigned int max_queue = ap->host->n_tags; in ata_sas_allocate_tag()
1260 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) { in ata_sas_allocate_tag()
1267 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) { in ata_sas_allocate_tag()
1268 ap->sas_last_tag = tag; in ata_sas_allocate_tag()
1272 return -1; in ata_sas_allocate_tag()
1277 clear_bit(tag, &ap->sas_tag_allocated); in ata_sas_free_tag()
1281 * sata_async_notification - SATA async notification handler
1282 * @ap: ATA port where async notification is received
1298 if (!(ap->flags & ATA_FLAG_AN)) in sata_async_notification()
1301 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf); in sata_async_notification()
1303 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf); in sata_async_notification()
1312 struct ata_device *dev = ap->link.device; in sata_async_notification()
1314 if ((dev->class == ATA_DEV_ATAPI) && in sata_async_notification()
1315 (dev->flags & ATA_DFLAG_AN)) in sata_async_notification()
1333 if (!(sntf & (1 << link->pmp))) in sata_async_notification()
1336 if ((link->device->class == ATA_DEV_ATAPI) && in sata_async_notification()
1337 (link->device->flags & ATA_DFLAG_AN)) in sata_async_notification()
1338 ata_scsi_media_change_notify(link->device); in sata_async_notification()
1355 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1367 * 0 on success, -errno otherwise.
1372 u8 *buf = dev->link->ap->sector_buf; in ata_eh_read_log_10h()
1379 return -EIO; in ata_eh_read_log_10h()
1389 return -ENOENT; in ata_eh_read_log_10h()
1393 tf->command = buf[2]; in ata_eh_read_log_10h()
1394 tf->feature = buf[3]; in ata_eh_read_log_10h()
1395 tf->lbal = buf[4]; in ata_eh_read_log_10h()
1396 tf->lbam = buf[5]; in ata_eh_read_log_10h()
1397 tf->lbah = buf[6]; in ata_eh_read_log_10h()
1398 tf->device = buf[7]; in ata_eh_read_log_10h()
1399 tf->hob_lbal = buf[8]; in ata_eh_read_log_10h()
1400 tf->hob_lbam = buf[9]; in ata_eh_read_log_10h()
1401 tf->hob_lbah = buf[10]; in ata_eh_read_log_10h()
1402 tf->nsect = buf[12]; in ata_eh_read_log_10h()
1403 tf->hob_nsect = buf[13]; in ata_eh_read_log_10h()
1404 if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id)) in ata_eh_read_log_10h()
1405 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16]; in ata_eh_read_log_10h()
1411 * ata_eh_analyze_ncq_error - analyze NCQ error
1416 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1424 struct ata_port *ap = link->ap; in ata_eh_analyze_ncq_error()
1425 struct ata_eh_context *ehc = &link->eh_context; in ata_eh_analyze_ncq_error()
1426 struct ata_device *dev = link->device; in ata_eh_analyze_ncq_error()
1432 if (ap->pflags & ATA_PFLAG_FROZEN) in ata_eh_analyze_ncq_error()
1436 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV)) in ata_eh_analyze_ncq_error()
1441 if (!(qc->flags & ATA_QCFLAG_FAILED)) in ata_eh_analyze_ncq_error()
1444 if (qc->err_mask) in ata_eh_analyze_ncq_error()
1457 if (!(link->sactive & (1 << tag))) { in ata_eh_analyze_ncq_error()
1465 memcpy(&qc->result_tf, &tf, sizeof(tf)); in ata_eh_analyze_ncq_error()
1466 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48; in ata_eh_analyze_ncq_error()
1467 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ; in ata_eh_analyze_ncq_error()
1468 if (dev->class == ATA_DEV_ZAC && in ata_eh_analyze_ncq_error()
1469 ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) { in ata_eh_analyze_ncq_error()
1472 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff; in ata_eh_analyze_ncq_error()
1473 asc = (qc->result_tf.auxiliary >> 8) & 0xff; in ata_eh_analyze_ncq_error()
1474 ascq = qc->result_tf.auxiliary & 0xff; in ata_eh_analyze_ncq_error()
1475 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq); in ata_eh_analyze_ncq_error()
1476 ata_scsi_set_sense_information(dev, qc->scsicmd, in ata_eh_analyze_ncq_error()
1477 &qc->result_tf); in ata_eh_analyze_ncq_error()
1478 qc->flags |= ATA_QCFLAG_SENSE_VALID; in ata_eh_analyze_ncq_error()
1481 ehc->i.err_mask &= ~AC_ERR_DEV; in ata_eh_analyze_ncq_error()