// SPDX-License-Identifier: GPL-2.0-or-later1/*2* SATA specific part of ATA helper library3*4* Copyright 2003-2004 Red Hat, Inc. All rights reserved.5* Copyright 2003-2004 Jeff Garzik6* Copyright 2006 Tejun Heo <[email protected]>7*/89#include <linux/kernel.h>10#include <linux/module.h>11#include <scsi/scsi_cmnd.h>12#include <scsi/scsi_device.h>13#include <scsi/scsi_eh.h>14#include <linux/libata.h>15#include <linux/unaligned.h>1617#include "libata.h"18#include "libata-transport.h"1920/* debounce timing parameters in msecs { interval, duration, timeout } */21const unsigned int sata_deb_timing_normal[] = { 5, 100, 2000 };22EXPORT_SYMBOL_GPL(sata_deb_timing_normal);23const unsigned int sata_deb_timing_hotplug[] = { 25, 500, 2000 };24EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);25const unsigned int sata_deb_timing_long[] = { 100, 2000, 5000 };26EXPORT_SYMBOL_GPL(sata_deb_timing_long);2728/**29* sata_scr_valid - test whether SCRs are accessible30* @link: ATA link to test SCR accessibility for31*32* Test whether SCRs are accessible for @link.33*34* LOCKING:35* None.36*37* RETURNS:38* 1 if SCRs are accessible, 0 otherwise.39*/40int sata_scr_valid(struct ata_link *link)41{42struct ata_port *ap = link->ap;4344return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;45}46EXPORT_SYMBOL_GPL(sata_scr_valid);4748/**49* sata_scr_read - read SCR register of the specified port50* @link: ATA link to read SCR for51* @reg: SCR to read52* @val: Place to store read value53*54* Read SCR register @reg of @link into *@val. This function is55* guaranteed to succeed if @link is ap->link, the cable type of56* the port is SATA and the port implements ->scr_read.57*58* LOCKING:59* None if @link is ap->link. Kernel thread context otherwise.60*61* RETURNS:62* 0 on success, negative errno on failure.63*/64int sata_scr_read(struct ata_link *link, int reg, u32 *val)65{66if (ata_is_host_link(link)) {67if (sata_scr_valid(link))68return link->ap->ops->scr_read(link, reg, val);69return -EOPNOTSUPP;70}7172return sata_pmp_scr_read(link, reg, val);73}74EXPORT_SYMBOL_GPL(sata_scr_read);7576/**77* sata_scr_write - write SCR register of the specified port78* @link: ATA link to write SCR for79* @reg: SCR to write80* @val: value to write81*82* Write @val to SCR register @reg of @link. This function is83* guaranteed to succeed if @link is ap->link, the cable type of84* the port is SATA and the port implements ->scr_read.85*86* LOCKING:87* None if @link is ap->link. Kernel thread context otherwise.88*89* RETURNS:90* 0 on success, negative errno on failure.91*/92int sata_scr_write(struct ata_link *link, int reg, u32 val)93{94if (ata_is_host_link(link)) {95if (sata_scr_valid(link))96return link->ap->ops->scr_write(link, reg, val);97return -EOPNOTSUPP;98}99100return sata_pmp_scr_write(link, reg, val);101}102EXPORT_SYMBOL_GPL(sata_scr_write);103104/**105* sata_scr_write_flush - write SCR register of the specified port and flush106* @link: ATA link to write SCR for107* @reg: SCR to write108* @val: value to write109*110* This function is identical to sata_scr_write() except that this111* function performs flush after writing to the register.112*113* LOCKING:114* None if @link is ap->link. Kernel thread context otherwise.115*116* RETURNS:117* 0 on success, negative errno on failure.118*/119int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)120{121if (ata_is_host_link(link)) {122int rc;123124if (sata_scr_valid(link)) {125rc = link->ap->ops->scr_write(link, reg, val);126if (rc == 0)127rc = link->ap->ops->scr_read(link, reg, &val);128return rc;129}130return -EOPNOTSUPP;131}132133return sata_pmp_scr_write(link, reg, val);134}135EXPORT_SYMBOL_GPL(sata_scr_write_flush);136137/**138* ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure139* @tf: Taskfile to convert140* @pmp: Port multiplier port141* @is_cmd: This FIS is for command142* @fis: Buffer into which data will output143*144* Converts a standard ATA taskfile to a Serial ATA145* FIS structure (Register - Host to Device).146*147* LOCKING:148* Inherited from caller.149*/150void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)151{152fis[0] = 0x27; /* Register - Host to Device FIS */153fis[1] = pmp & 0xf; /* Port multiplier number*/154if (is_cmd)155fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */156157fis[2] = tf->command;158fis[3] = tf->feature;159160fis[4] = tf->lbal;161fis[5] = tf->lbam;162fis[6] = tf->lbah;163fis[7] = tf->device;164165fis[8] = tf->hob_lbal;166fis[9] = tf->hob_lbam;167fis[10] = tf->hob_lbah;168fis[11] = tf->hob_feature;169170fis[12] = tf->nsect;171fis[13] = tf->hob_nsect;172fis[14] = 0;173fis[15] = tf->ctl;174175fis[16] = tf->auxiliary & 0xff;176fis[17] = (tf->auxiliary >> 8) & 0xff;177fis[18] = (tf->auxiliary >> 16) & 0xff;178fis[19] = (tf->auxiliary >> 24) & 0xff;179}180EXPORT_SYMBOL_GPL(ata_tf_to_fis);181182/**183* ata_tf_from_fis - Convert SATA FIS to ATA taskfile184* @fis: Buffer from which data will be input185* @tf: Taskfile to output186*187* Converts a serial ATA FIS structure to a standard ATA taskfile.188*189* LOCKING:190* Inherited from caller.191*/192193void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)194{195tf->status = fis[2];196tf->error = fis[3];197198tf->lbal = fis[4];199tf->lbam = fis[5];200tf->lbah = fis[6];201tf->device = fis[7];202203tf->hob_lbal = fis[8];204tf->hob_lbam = fis[9];205tf->hob_lbah = fis[10];206207tf->nsect = fis[12];208tf->hob_nsect = fis[13];209}210EXPORT_SYMBOL_GPL(ata_tf_from_fis);211212/**213* sata_link_debounce - debounce SATA phy status214* @link: ATA link to debounce SATA phy status for215* @params: timing parameters { interval, duration, timeout } in msec216* @deadline: deadline jiffies for the operation217*218* Make sure SStatus of @link reaches stable state, determined by219* holding the same value where DET is not 1 for @duration polled220* every @interval, before @timeout. Timeout constraints the221* beginning of the stable state. Because DET gets stuck at 1 on222* some controllers after hot unplugging, this functions waits223* until timeout then returns 0 if DET is stable at 1.224*225* @timeout is further limited by @deadline. The sooner of the226* two is used.227*228* LOCKING:229* Kernel thread context (may sleep)230*231* RETURNS:232* 0 on success, -errno on failure.233*/234int sata_link_debounce(struct ata_link *link, const unsigned int *params,235unsigned long deadline)236{237unsigned int interval = params[0];238unsigned int duration = params[1];239unsigned long last_jiffies, t;240u32 last, cur;241int rc;242243t = ata_deadline(jiffies, params[2]);244if (time_before(t, deadline))245deadline = t;246247if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))248return rc;249cur &= 0xf;250251last = cur;252last_jiffies = jiffies;253254while (1) {255ata_msleep(link->ap, interval);256if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))257return rc;258cur &= 0xf;259260/* DET stable? */261if (cur == last) {262if (cur == 1 && time_before(jiffies, deadline))263continue;264if (time_after(jiffies,265ata_deadline(last_jiffies, duration)))266return 0;267continue;268}269270/* unstable, start over */271last = cur;272last_jiffies = jiffies;273274/* Check deadline. If debouncing failed, return275* -EPIPE to tell upper layer to lower link speed.276*/277if (time_after(jiffies, deadline))278return -EPIPE;279}280}281EXPORT_SYMBOL_GPL(sata_link_debounce);282283/**284* sata_link_resume - resume SATA link285* @link: ATA link to resume SATA286* @params: timing parameters { interval, duration, timeout } in msec287* @deadline: deadline jiffies for the operation288*289* Resume SATA phy @link and debounce it.290*291* LOCKING:292* Kernel thread context (may sleep)293*294* RETURNS:295* 0 on success, -errno on failure.296*/297int sata_link_resume(struct ata_link *link, const unsigned int *params,298unsigned long deadline)299{300int tries = ATA_LINK_RESUME_TRIES;301u32 scontrol, serror;302int rc;303304if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))305return rc;306307/*308* Writes to SControl sometimes get ignored under certain309* controllers (ata_piix SIDPR). Make sure DET actually is310* cleared.311*/312do {313scontrol = (scontrol & 0x0f0) | 0x300;314if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))315return rc;316/*317* Some PHYs react badly if SStatus is pounded318* immediately after resuming. Delay 200ms before319* debouncing.320*/321if (!(link->flags & ATA_LFLAG_NO_DEBOUNCE_DELAY))322ata_msleep(link->ap, 200);323324/* is SControl restored correctly? */325if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))326return rc;327} while ((scontrol & 0xf0f) != 0x300 && --tries);328329if ((scontrol & 0xf0f) != 0x300) {330ata_link_warn(link, "failed to resume link (SControl %X)\n",331scontrol);332return 0;333}334335if (tries < ATA_LINK_RESUME_TRIES)336ata_link_warn(link, "link resume succeeded after %d retries\n",337ATA_LINK_RESUME_TRIES - tries);338339if ((rc = sata_link_debounce(link, params, deadline)))340return rc;341342/* clear SError, some PHYs require this even for SRST to work */343if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))344rc = sata_scr_write(link, SCR_ERROR, serror);345346return rc != -EINVAL ? rc : 0;347}348EXPORT_SYMBOL_GPL(sata_link_resume);349350/**351* sata_link_scr_lpm - manipulate SControl IPM and SPM fields352* @link: ATA link to manipulate SControl for353* @policy: LPM policy to configure354* @spm_wakeup: initiate LPM transition to active state355*356* Manipulate the IPM field of the SControl register of @link357* according to @policy. If @policy is ATA_LPM_MAX_POWER and358* @spm_wakeup is %true, the SPM field is manipulated to wake up359* the link. This function also clears PHYRDY_CHG before360* returning.361*362* LOCKING:363* EH context.364*365* RETURNS:366* 0 on success, -errno otherwise.367*/368int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,369bool spm_wakeup)370{371struct ata_eh_context *ehc = &link->eh_context;372bool woken_up = false;373u32 scontrol;374int rc;375376rc = sata_scr_read(link, SCR_CONTROL, &scontrol);377if (rc)378return rc;379380switch (policy) {381case ATA_LPM_MAX_POWER:382/* disable all LPM transitions */383scontrol |= (0x7 << 8);384/* initiate transition to active state */385if (spm_wakeup) {386scontrol |= (0x4 << 12);387woken_up = true;388}389break;390case ATA_LPM_MED_POWER:391/* allow LPM to PARTIAL */392scontrol &= ~(0x1 << 8);393scontrol |= (0x6 << 8);394break;395case ATA_LPM_MED_POWER_WITH_DIPM:396case ATA_LPM_MIN_POWER_WITH_PARTIAL:397case ATA_LPM_MIN_POWER:398if (ata_link_nr_enabled(link) > 0) {399/* assume no restrictions on LPM transitions */400scontrol &= ~(0x7 << 8);401402/*403* If the controller does not support partial, slumber,404* or devsleep, then disallow these transitions.405*/406if (link->ap->host->flags & ATA_HOST_NO_PART)407scontrol |= (0x1 << 8);408409if (link->ap->host->flags & ATA_HOST_NO_SSC)410scontrol |= (0x2 << 8);411412if (link->ap->host->flags & ATA_HOST_NO_DEVSLP)413scontrol |= (0x4 << 8);414} else {415/* empty port, power off */416scontrol &= ~0xf;417scontrol |= (0x1 << 2);418}419break;420default:421WARN_ON(1);422}423424rc = sata_scr_write(link, SCR_CONTROL, scontrol);425if (rc)426return rc;427428/* give the link time to transit out of LPM state */429if (woken_up)430msleep(10);431432/* clear PHYRDY_CHG from SError */433ehc->i.serror &= ~SERR_PHYRDY_CHG;434return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);435}436EXPORT_SYMBOL_GPL(sata_link_scr_lpm);437438static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)439{440struct ata_link *host_link = &link->ap->link;441u32 limit, target, spd;442443limit = link->sata_spd_limit;444445/* Don't configure downstream link faster than upstream link.446* It doesn't speed up anything and some PMPs choke on such447* configuration.448*/449if (!ata_is_host_link(link) && host_link->sata_spd)450limit &= (1 << host_link->sata_spd) - 1;451452if (limit == UINT_MAX)453target = 0;454else455target = fls(limit);456457spd = (*scontrol >> 4) & 0xf;458*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);459460return spd != target;461}462463/**464* sata_set_spd_needed - is SATA spd configuration needed465* @link: Link in question466*467* Test whether the spd limit in SControl matches468* @link->sata_spd_limit. This function is used to determine469* whether hardreset is necessary to apply SATA spd470* configuration.471*472* LOCKING:473* Inherited from caller.474*475* RETURNS:476* 1 if SATA spd configuration is needed, 0 otherwise.477*/478static int sata_set_spd_needed(struct ata_link *link)479{480u32 scontrol;481482if (sata_scr_read(link, SCR_CONTROL, &scontrol))483return 1;484485return __sata_set_spd_needed(link, &scontrol);486}487488/**489* sata_set_spd - set SATA spd according to spd limit490* @link: Link to set SATA spd for491*492* Set SATA spd of @link according to sata_spd_limit.493*494* LOCKING:495* Inherited from caller.496*497* RETURNS:498* 0 if spd doesn't need to be changed, 1 if spd has been499* changed. Negative errno if SCR registers are inaccessible.500*/501int sata_set_spd(struct ata_link *link)502{503u32 scontrol;504int rc;505506if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))507return rc;508509if (!__sata_set_spd_needed(link, &scontrol))510return 0;511512if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))513return rc;514515return 1;516}517EXPORT_SYMBOL_GPL(sata_set_spd);518519/**520* sata_down_spd_limit - adjust SATA spd limit downward521* @link: Link to adjust SATA spd limit for522* @spd_limit: Additional limit523*524* Adjust SATA spd limit of @link downward. Note that this525* function only adjusts the limit. The change must be applied526* using sata_set_spd().527*528* If @spd_limit is non-zero, the speed is limited to equal to or529* lower than @spd_limit if such speed is supported. If530* @spd_limit is slower than any supported speed, only the lowest531* supported speed is allowed.532*533* LOCKING:534* Inherited from caller.535*536* RETURNS:537* 0 on success, negative errno on failure538*/539int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)540{541u32 sstatus, spd, mask;542int rc, bit;543544if (!sata_scr_valid(link))545return -EOPNOTSUPP;546547/* If SCR can be read, use it to determine the current SPD.548* If not, use cached value in link->sata_spd.549*/550rc = sata_scr_read(link, SCR_STATUS, &sstatus);551if (rc == 0 && ata_sstatus_online(sstatus))552spd = (sstatus >> 4) & 0xf;553else554spd = link->sata_spd;555556mask = link->sata_spd_limit;557if (mask <= 1)558return -EINVAL;559560/* unconditionally mask off the highest bit */561bit = fls(mask) - 1;562mask &= ~(1 << bit);563564/*565* Mask off all speeds higher than or equal to the current one. At566* this point, if current SPD is not available and we previously567* recorded the link speed from SStatus, the driver has already568* masked off the highest bit so mask should already be 1 or 0.569* Otherwise, we should not force 1.5Gbps on a link where we have570* not previously recorded speed from SStatus. Just return in this571* case.572*/573if (spd > 1)574mask &= (1 << (spd - 1)) - 1;575else if (link->sata_spd)576return -EINVAL;577578/* were we already at the bottom? */579if (!mask)580return -EINVAL;581582if (spd_limit) {583if (mask & ((1 << spd_limit) - 1))584mask &= (1 << spd_limit) - 1;585else {586bit = ffs(mask) - 1;587mask = 1 << bit;588}589}590591link->sata_spd_limit = mask;592593ata_link_warn(link, "limiting SATA link speed to %s\n",594sata_spd_string(fls(mask)));595596return 0;597}598599/**600* sata_link_hardreset - reset link via SATA phy reset601* @link: link to reset602* @timing: timing parameters { interval, duration, timeout } in msec603* @deadline: deadline jiffies for the operation604* @online: optional out parameter indicating link onlineness605* @check_ready: optional callback to check link readiness606*607* SATA phy-reset @link using DET bits of SControl register.608* After hardreset, link readiness is waited upon using609* ata_wait_ready() if @check_ready is specified. LLDs are610* allowed to not specify @check_ready and wait itself after this611* function returns. Device classification is LLD's612* responsibility.613*614* *@online is set to one iff reset succeeded and @link is online615* after reset.616*617* LOCKING:618* Kernel thread context (may sleep)619*620* RETURNS:621* 0 on success, -errno otherwise.622*/623int sata_link_hardreset(struct ata_link *link, const unsigned int *timing,624unsigned long deadline,625bool *online, int (*check_ready)(struct ata_link *))626{627u32 scontrol;628int rc;629630if (online)631*online = false;632633if (sata_set_spd_needed(link)) {634/* SATA spec says nothing about how to reconfigure635* spd. To be on the safe side, turn off phy during636* reconfiguration. This works for at least ICH7 AHCI637* and Sil3124.638*/639if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))640goto out;641642scontrol = (scontrol & 0x0f0) | 0x304;643644if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))645goto out;646647sata_set_spd(link);648}649650/* issue phy wake/reset */651if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))652goto out;653654scontrol = (scontrol & 0x0f0) | 0x301;655656if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))657goto out;658659/* Couldn't find anything in SATA I/II specs, but AHCI-1.1660* 10.4.2 says at least 1 ms.661*/662ata_msleep(link->ap, 1);663664/* bring link back */665rc = sata_link_resume(link, timing, deadline);666if (rc)667goto out;668/* if link is offline nothing more to do */669if (ata_phys_link_offline(link))670goto out;671672/* Link is online. From this point, -ENODEV too is an error. */673if (online)674*online = true;675676if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {677/* If PMP is supported, we have to do follow-up SRST.678* Some PMPs don't send D2H Reg FIS after hardreset if679* the first port is empty. Wait only for680* ATA_TMOUT_PMP_SRST_WAIT.681*/682if (check_ready) {683unsigned long pmp_deadline;684685pmp_deadline = ata_deadline(jiffies,686ATA_TMOUT_PMP_SRST_WAIT);687if (time_after(pmp_deadline, deadline))688pmp_deadline = deadline;689ata_wait_ready(link, pmp_deadline, check_ready);690}691rc = -EAGAIN;692goto out;693}694695rc = 0;696if (check_ready)697rc = ata_wait_ready(link, deadline, check_ready);698out:699if (rc && rc != -EAGAIN) {700/* online is set iff link is online && reset succeeded */701if (online)702*online = false;703}704return rc;705}706EXPORT_SYMBOL_GPL(sata_link_hardreset);707708/**709* sata_std_hardreset - COMRESET w/o waiting or classification710* @link: link to reset711* @class: resulting class of attached device712* @deadline: deadline jiffies for the operation713*714* Standard SATA COMRESET w/o waiting or classification.715*716* LOCKING:717* Kernel thread context (may sleep)718*719* RETURNS:720* 0 if link offline, -EAGAIN if link online, -errno on errors.721*/722int sata_std_hardreset(struct ata_link *link, unsigned int *class,723unsigned long deadline)724{725const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context);726bool online;727int rc;728729rc = sata_link_hardreset(link, timing, deadline, &online, NULL);730if (online)731return -EAGAIN;732return rc;733}734EXPORT_SYMBOL_GPL(sata_std_hardreset);735736/**737* ata_qc_complete_multiple - Complete multiple qcs successfully738* @ap: port in question739* @qc_active: new qc_active mask740*741* Complete in-flight commands. This functions is meant to be742* called from low-level driver's interrupt routine to complete743* requests normally. ap->qc_active and @qc_active is compared744* and commands are completed accordingly.745*746* Always use this function when completing multiple NCQ commands747* from IRQ handlers instead of calling ata_qc_complete()748* multiple times to keep IRQ expect status properly in sync.749*750* LOCKING:751* spin_lock_irqsave(host lock)752*753* RETURNS:754* Number of completed commands on success, -errno otherwise.755*/756int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)757{758u64 done_mask, ap_qc_active = ap->qc_active;759int nr_done = 0;760761/*762* If the internal tag is set on ap->qc_active, then we care about763* bit0 on the passed in qc_active mask. Move that bit up to match764* the internal tag.765*/766if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {767qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;768qc_active ^= qc_active & 0x01;769}770771done_mask = ap_qc_active ^ qc_active;772773if (unlikely(done_mask & qc_active)) {774ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",775ap->qc_active, qc_active);776return -EINVAL;777}778779if (ap->ops->qc_ncq_fill_rtf)780ap->ops->qc_ncq_fill_rtf(ap, done_mask);781782while (done_mask) {783struct ata_queued_cmd *qc;784unsigned int tag = __ffs64(done_mask);785786qc = ata_qc_from_tag(ap, tag);787if (qc) {788ata_qc_complete(qc);789nr_done++;790}791done_mask &= ~(1ULL << tag);792}793794return nr_done;795}796EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);797798/**799* ata_slave_link_init - initialize slave link800* @ap: port to initialize slave link for801*802* Create and initialize slave link for @ap. This enables slave803* link handling on the port.804*805* In libata, a port contains links and a link contains devices.806* There is single host link but if a PMP is attached to it,807* there can be multiple fan-out links. On SATA, there's usually808* a single device connected to a link but PATA and SATA809* controllers emulating TF based interface can have two - master810* and slave.811*812* However, there are a few controllers which don't fit into this813* abstraction too well - SATA controllers which emulate TF814* interface with both master and slave devices but also have815* separate SCR register sets for each device. These controllers816* need separate links for physical link handling817* (e.g. onlineness, link speed) but should be treated like a818* traditional M/S controller for everything else (e.g. command819* issue, softreset).820*821* slave_link is libata's way of handling this class of822* controllers without impacting core layer too much. For823* anything other than physical link handling, the default host824* link is used for both master and slave. For physical link825* handling, separate @ap->slave_link is used. All dirty details826* are implemented inside libata core layer. From LLD's POV, the827* only difference is that prereset, hardreset and postreset are828* called once more for the slave link, so the reset sequence829* looks like the following.830*831* prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->832* softreset(M) -> postreset(M) -> postreset(S)833*834* Note that softreset is called only for the master. Softreset835* resets both M/S by definition, so SRST on master should handle836* both (the standard method will work just fine).837*838* LOCKING:839* Should be called before host is registered.840*841* RETURNS:842* 0 on success, -errno on failure.843*/844int ata_slave_link_init(struct ata_port *ap)845{846struct ata_link *link;847848WARN_ON(ap->slave_link);849WARN_ON(ap->flags & ATA_FLAG_PMP);850851link = kzalloc(sizeof(*link), GFP_KERNEL);852if (!link)853return -ENOMEM;854855ata_link_init(ap, link, 1);856ap->slave_link = link;857return 0;858}859EXPORT_SYMBOL_GPL(ata_slave_link_init);860861/**862* sata_lpm_ignore_phy_events - test if PHY event should be ignored863* @link: Link receiving the event864*865* Test whether the received PHY event has to be ignored or not.866*867* LOCKING:868* None:869*870* RETURNS:871* True if the event has to be ignored.872*/873bool sata_lpm_ignore_phy_events(struct ata_link *link)874{875unsigned long lpm_timeout = link->last_lpm_change +876msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);877878/* if LPM is enabled, PHYRDY doesn't mean anything */879if (link->lpm_policy > ATA_LPM_MAX_POWER)880return true;881882/* ignore the first PHY event after the LPM policy changed883* as it is might be spurious884*/885if ((link->flags & ATA_LFLAG_CHANGED) &&886time_before(jiffies, lpm_timeout))887return true;888889return false;890}891EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);892893static const char *ata_lpm_policy_names[] = {894[ATA_LPM_UNKNOWN] = "keep_firmware_settings",895[ATA_LPM_MAX_POWER] = "max_performance",896[ATA_LPM_MED_POWER] = "medium_power",897[ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",898[ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",899[ATA_LPM_MIN_POWER] = "min_power",900};901902/*903* Check if a port supports link power management.904* Must be called with the port locked.905*/906static bool ata_scsi_lpm_supported(struct ata_port *ap)907{908struct ata_link *link;909struct ata_device *dev;910911if (ap->flags & ATA_FLAG_NO_LPM)912return false;913914ata_for_each_link(link, ap, EDGE) {915ata_for_each_dev(dev, &ap->link, ENABLED) {916if (dev->quirks & ATA_QUIRK_NOLPM)917return false;918}919}920921return true;922}923924static ssize_t ata_scsi_lpm_supported_show(struct device *dev,925struct device_attribute *attr, char *buf)926{927struct Scsi_Host *shost = class_to_shost(dev);928struct ata_port *ap = ata_shost_to_port(shost);929unsigned long flags;930bool supported;931932spin_lock_irqsave(ap->lock, flags);933supported = ata_scsi_lpm_supported(ap);934spin_unlock_irqrestore(ap->lock, flags);935936return sysfs_emit(buf, "%d\n", supported);937}938DEVICE_ATTR(link_power_management_supported, S_IRUGO,939ata_scsi_lpm_supported_show, NULL);940EXPORT_SYMBOL_GPL(dev_attr_link_power_management_supported);941942static ssize_t ata_scsi_lpm_store(struct device *device,943struct device_attribute *attr,944const char *buf, size_t count)945{946struct Scsi_Host *shost = class_to_shost(device);947struct ata_port *ap = ata_shost_to_port(shost);948enum ata_lpm_policy policy;949unsigned long flags;950951/* UNKNOWN is internal state, iterate from MAX_POWER */952for (policy = ATA_LPM_MAX_POWER;953policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {954const char *name = ata_lpm_policy_names[policy];955956if (strncmp(name, buf, strlen(name)) == 0)957break;958}959if (policy == ARRAY_SIZE(ata_lpm_policy_names))960return -EINVAL;961962spin_lock_irqsave(ap->lock, flags);963964if (!ata_scsi_lpm_supported(ap)) {965count = -EOPNOTSUPP;966goto out_unlock;967}968969ap->target_lpm_policy = policy;970ata_port_schedule_eh(ap);971out_unlock:972spin_unlock_irqrestore(ap->lock, flags);973return count;974}975976static ssize_t ata_scsi_lpm_show(struct device *dev,977struct device_attribute *attr, char *buf)978{979struct Scsi_Host *shost = class_to_shost(dev);980struct ata_port *ap = ata_shost_to_port(shost);981982if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))983return -EINVAL;984985return sysfs_emit(buf, "%s\n",986ata_lpm_policy_names[ap->target_lpm_policy]);987}988DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,989ata_scsi_lpm_show, ata_scsi_lpm_store);990EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);991992/**993* ata_ncq_prio_supported - Check if device supports NCQ Priority994* @ap: ATA port of the target device995* @sdev: SCSI device996* @supported: Address of a boolean to store the result997*998* Helper to check if device supports NCQ Priority feature.999*1000* Context: Any context. Takes and releases @ap->lock.1001*1002* Return:1003* * %0 - OK. Status is stored into @supported1004* * %-ENODEV - Failed to find the ATA device1005*/1006int ata_ncq_prio_supported(struct ata_port *ap, struct scsi_device *sdev,1007bool *supported)1008{1009struct ata_device *dev;1010unsigned long flags;1011int rc = 0;10121013spin_lock_irqsave(ap->lock, flags);1014dev = ata_scsi_find_dev(ap, sdev);1015if (!dev)1016rc = -ENODEV;1017else1018*supported = dev->flags & ATA_DFLAG_NCQ_PRIO;1019spin_unlock_irqrestore(ap->lock, flags);10201021return rc;1022}1023EXPORT_SYMBOL_GPL(ata_ncq_prio_supported);10241025static ssize_t ata_ncq_prio_supported_show(struct device *device,1026struct device_attribute *attr,1027char *buf)1028{1029struct scsi_device *sdev = to_scsi_device(device);1030struct ata_port *ap = ata_shost_to_port(sdev->host);1031bool supported;1032int rc;10331034rc = ata_ncq_prio_supported(ap, sdev, &supported);1035if (rc)1036return rc;10371038return sysfs_emit(buf, "%d\n", supported);1039}10401041DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL);1042EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported);10431044/**1045* ata_ncq_prio_enabled - Check if NCQ Priority is enabled1046* @ap: ATA port of the target device1047* @sdev: SCSI device1048* @enabled: Address of a boolean to store the result1049*1050* Helper to check if NCQ Priority feature is enabled.1051*1052* Context: Any context. Takes and releases @ap->lock.1053*1054* Return:1055* * %0 - OK. Status is stored into @enabled1056* * %-ENODEV - Failed to find the ATA device1057*/1058int ata_ncq_prio_enabled(struct ata_port *ap, struct scsi_device *sdev,1059bool *enabled)1060{1061struct ata_device *dev;1062unsigned long flags;1063int rc = 0;10641065spin_lock_irqsave(ap->lock, flags);1066dev = ata_scsi_find_dev(ap, sdev);1067if (!dev)1068rc = -ENODEV;1069else1070*enabled = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED;1071spin_unlock_irqrestore(ap->lock, flags);10721073return rc;1074}1075EXPORT_SYMBOL_GPL(ata_ncq_prio_enabled);10761077static ssize_t ata_ncq_prio_enable_show(struct device *device,1078struct device_attribute *attr,1079char *buf)1080{1081struct scsi_device *sdev = to_scsi_device(device);1082struct ata_port *ap = ata_shost_to_port(sdev->host);1083bool enabled;1084int rc;10851086rc = ata_ncq_prio_enabled(ap, sdev, &enabled);1087if (rc)1088return rc;10891090return sysfs_emit(buf, "%d\n", enabled);1091}10921093/**1094* ata_ncq_prio_enable - Enable/disable NCQ Priority1095* @ap: ATA port of the target device1096* @sdev: SCSI device1097* @enable: true - enable NCQ Priority, false - disable NCQ Priority1098*1099* Helper to enable/disable NCQ Priority feature.1100*1101* Context: Any context. Takes and releases @ap->lock.1102*1103* Return:1104* * %0 - OK. Status is stored into @enabled1105* * %-ENODEV - Failed to find the ATA device1106* * %-EINVAL - NCQ Priority is not supported or CDL is enabled1107*/1108int ata_ncq_prio_enable(struct ata_port *ap, struct scsi_device *sdev,1109bool enable)1110{1111struct ata_device *dev;1112unsigned long flags;1113int rc = 0;11141115spin_lock_irqsave(ap->lock, flags);11161117dev = ata_scsi_find_dev(ap, sdev);1118if (!dev) {1119rc = -ENODEV;1120goto unlock;1121}11221123if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {1124rc = -EINVAL;1125goto unlock;1126}11271128if (enable) {1129if (dev->flags & ATA_DFLAG_CDL_ENABLED) {1130ata_dev_err(dev,1131"CDL must be disabled to enable NCQ priority\n");1132rc = -EINVAL;1133goto unlock;1134}1135dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLED;1136} else {1137dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;1138}11391140unlock:1141spin_unlock_irqrestore(ap->lock, flags);11421143return rc;1144}1145EXPORT_SYMBOL_GPL(ata_ncq_prio_enable);11461147static ssize_t ata_ncq_prio_enable_store(struct device *device,1148struct device_attribute *attr,1149const char *buf, size_t len)1150{1151struct scsi_device *sdev = to_scsi_device(device);1152struct ata_port *ap = ata_shost_to_port(sdev->host);1153bool enable;1154int rc;11551156rc = kstrtobool(buf, &enable);1157if (rc)1158return rc;11591160rc = ata_ncq_prio_enable(ap, sdev, enable);1161if (rc)1162return rc;11631164return len;1165}11661167DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,1168ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);1169EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);11701171static struct attribute *ata_ncq_sdev_attrs[] = {1172&dev_attr_unload_heads.attr,1173&dev_attr_ncq_prio_enable.attr,1174&dev_attr_ncq_prio_supported.attr,1175NULL1176};11771178static const struct attribute_group ata_ncq_sdev_attr_group = {1179.attrs = ata_ncq_sdev_attrs1180};11811182const struct attribute_group *ata_ncq_sdev_groups[] = {1183&ata_ncq_sdev_attr_group,1184NULL1185};1186EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups);11871188static ssize_t1189ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,1190const char *buf, size_t count)1191{1192struct Scsi_Host *shost = class_to_shost(dev);1193struct ata_port *ap = ata_shost_to_port(shost);1194if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))1195return ap->ops->em_store(ap, buf, count);1196return -EINVAL;1197}11981199static ssize_t1200ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,1201char *buf)1202{1203struct Scsi_Host *shost = class_to_shost(dev);1204struct ata_port *ap = ata_shost_to_port(shost);12051206if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))1207return ap->ops->em_show(ap, buf);1208return -EINVAL;1209}1210DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,1211ata_scsi_em_message_show, ata_scsi_em_message_store);1212EXPORT_SYMBOL_GPL(dev_attr_em_message);12131214static ssize_t1215ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,1216char *buf)1217{1218struct Scsi_Host *shost = class_to_shost(dev);1219struct ata_port *ap = ata_shost_to_port(shost);12201221return sysfs_emit(buf, "%d\n", ap->em_message_type);1222}1223DEVICE_ATTR(em_message_type, S_IRUGO,1224ata_scsi_em_message_type_show, NULL);1225EXPORT_SYMBOL_GPL(dev_attr_em_message_type);12261227static ssize_t1228ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,1229char *buf)1230{1231struct scsi_device *sdev = to_scsi_device(dev);1232struct ata_port *ap = ata_shost_to_port(sdev->host);1233struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);12341235if (atadev && ap->ops->sw_activity_show &&1236(ap->flags & ATA_FLAG_SW_ACTIVITY))1237return ap->ops->sw_activity_show(atadev, buf);1238return -EINVAL;1239}12401241static ssize_t1242ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,1243const char *buf, size_t count)1244{1245struct scsi_device *sdev = to_scsi_device(dev);1246struct ata_port *ap = ata_shost_to_port(sdev->host);1247struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);1248enum sw_activity val;1249int rc;12501251if (atadev && ap->ops->sw_activity_store &&1252(ap->flags & ATA_FLAG_SW_ACTIVITY)) {1253val = simple_strtoul(buf, NULL, 0);1254switch (val) {1255case OFF: case BLINK_ON: case BLINK_OFF:1256rc = ap->ops->sw_activity_store(atadev, val);1257if (!rc)1258return count;1259else1260return rc;1261}1262}1263return -EINVAL;1264}1265DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,1266ata_scsi_activity_store);1267EXPORT_SYMBOL_GPL(dev_attr_sw_activity);12681269/**1270* ata_change_queue_depth - Set a device maximum queue depth1271* @ap: ATA port of the target device1272* @sdev: SCSI device to configure queue depth for1273* @queue_depth: new queue depth1274*1275* Helper to set a device maximum queue depth, usable with both libsas1276* and libata.1277*1278*/1279int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,1280int queue_depth)1281{1282struct ata_device *dev;1283unsigned long flags;1284int max_queue_depth;12851286spin_lock_irqsave(ap->lock, flags);12871288dev = ata_scsi_find_dev(ap, sdev);1289if (!dev || queue_depth < 1 || queue_depth == sdev->queue_depth) {1290spin_unlock_irqrestore(ap->lock, flags);1291return sdev->queue_depth;1292}12931294/*1295* Make sure that the queue depth requested does not exceed the device1296* capabilities.1297*/1298max_queue_depth = min(ATA_MAX_QUEUE, sdev->host->can_queue);1299max_queue_depth = min(max_queue_depth, ata_id_queue_depth(dev->id));1300if (queue_depth > max_queue_depth) {1301spin_unlock_irqrestore(ap->lock, flags);1302return -EINVAL;1303}13041305/*1306* If NCQ is not supported by the device or if the target queue depth1307* is 1 (to disable drive side command queueing), turn off NCQ.1308*/1309if (queue_depth == 1 || !ata_ncq_supported(dev)) {1310dev->flags |= ATA_DFLAG_NCQ_OFF;1311queue_depth = 1;1312} else {1313dev->flags &= ~ATA_DFLAG_NCQ_OFF;1314}13151316spin_unlock_irqrestore(ap->lock, flags);13171318if (queue_depth == sdev->queue_depth)1319return sdev->queue_depth;13201321return scsi_change_queue_depth(sdev, queue_depth);1322}1323EXPORT_SYMBOL_GPL(ata_change_queue_depth);13241325/**1326* ata_scsi_change_queue_depth - SCSI callback for queue depth config1327* @sdev: SCSI device to configure queue depth for1328* @queue_depth: new queue depth1329*1330* This is libata standard hostt->change_queue_depth callback.1331* SCSI will call into this callback when user tries to set queue1332* depth via sysfs.1333*1334* LOCKING:1335* SCSI layer (we don't care)1336*1337* RETURNS:1338* Newly configured queue depth.1339*/1340int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)1341{1342struct ata_port *ap = ata_shost_to_port(sdev->host);13431344return ata_change_queue_depth(ap, sdev, queue_depth);1345}1346EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);13471348/**1349* ata_sas_sdev_configure - Default sdev_configure routine for libata1350* devices1351* @sdev: SCSI device to configure1352* @lim: queue limits1353* @ap: ATA port to which SCSI device is attached1354*1355* RETURNS:1356* Zero.1357*/13581359int ata_sas_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim,1360struct ata_port *ap)1361{1362ata_scsi_sdev_config(sdev);13631364return ata_scsi_dev_config(sdev, lim, ap->link.device);1365}1366EXPORT_SYMBOL_GPL(ata_sas_sdev_configure);13671368/**1369* ata_sas_queuecmd - Issue SCSI cdb to libata-managed device1370* @cmd: SCSI command to be sent1371* @ap: ATA port to which the command is being sent1372*1373* RETURNS:1374* Return value from __ata_scsi_queuecmd() if @cmd can be queued,1375* 0 otherwise.1376*/13771378int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)1379{1380int rc = 0;13811382if (likely(ata_dev_enabled(ap->link.device)))1383rc = __ata_scsi_queuecmd(cmd, ap->link.device);1384else {1385cmd->result = (DID_BAD_TARGET << 16);1386scsi_done(cmd);1387}1388return rc;1389}1390EXPORT_SYMBOL_GPL(ata_sas_queuecmd);13911392/**1393* sata_async_notification - SATA async notification handler1394* @ap: ATA port where async notification is received1395*1396* Handler to be called when async notification via SDB FIS is1397* received. This function schedules EH if necessary.1398*1399* LOCKING:1400* spin_lock_irqsave(host lock)1401*1402* RETURNS:1403* 1 if EH is scheduled, 0 otherwise.1404*/1405int sata_async_notification(struct ata_port *ap)1406{1407u32 sntf;1408int rc;14091410if (!(ap->flags & ATA_FLAG_AN))1411return 0;14121413rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);1414if (rc == 0)1415sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);14161417if (!sata_pmp_attached(ap) || rc) {1418/* PMP is not attached or SNTF is not available */1419if (!sata_pmp_attached(ap)) {1420/* PMP is not attached. Check whether ATAPI1421* AN is configured. If so, notify media1422* change.1423*/1424struct ata_device *dev = ap->link.device;14251426if ((dev->class == ATA_DEV_ATAPI) &&1427(dev->flags & ATA_DFLAG_AN))1428ata_scsi_media_change_notify(dev);1429return 0;1430} else {1431/* PMP is attached but SNTF is not available.1432* ATAPI async media change notification is1433* not used. The PMP must be reporting PHY1434* status change, schedule EH.1435*/1436ata_port_schedule_eh(ap);1437return 1;1438}1439} else {1440/* PMP is attached and SNTF is available */1441struct ata_link *link;14421443/* check and notify ATAPI AN */1444ata_for_each_link(link, ap, EDGE) {1445if (!(sntf & (1 << link->pmp)))1446continue;14471448if ((link->device->class == ATA_DEV_ATAPI) &&1449(link->device->flags & ATA_DFLAG_AN))1450ata_scsi_media_change_notify(link->device);1451}14521453/* If PMP is reporting that PHY status of some1454* downstream ports has changed, schedule EH.1455*/1456if (sntf & (1 << SATA_PMP_CTRL_PORT)) {1457ata_port_schedule_eh(ap);1458return 1;1459}14601461return 0;1462}1463}1464EXPORT_SYMBOL_GPL(sata_async_notification);14651466/**1467* ata_eh_read_log_10h - Read log page 10h for NCQ error details1468* @dev: Device to read log page 10h from1469* @tag: Resulting tag of the failed command1470* @tf: Resulting taskfile registers of the failed command1471*1472* Read log page 10h to obtain NCQ error details and clear error1473* condition.1474*1475* LOCKING:1476* Kernel thread context (may sleep).1477*1478* RETURNS:1479* 0 on success, -errno otherwise.1480*/1481static int ata_eh_read_log_10h(struct ata_device *dev,1482int *tag, struct ata_taskfile *tf)1483{1484u8 *buf = dev->sector_buf;1485unsigned int err_mask;1486u8 csum;1487int i;14881489err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);1490if (err_mask)1491return -EIO;14921493csum = 0;1494for (i = 0; i < ATA_SECT_SIZE; i++)1495csum += buf[i];1496if (csum)1497ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",1498csum);14991500if (buf[0] & 0x80)1501return -ENOENT;15021503*tag = buf[0] & 0x1f;15041505tf->status = buf[2];1506tf->error = buf[3];1507tf->lbal = buf[4];1508tf->lbam = buf[5];1509tf->lbah = buf[6];1510tf->device = buf[7];1511tf->hob_lbal = buf[8];1512tf->hob_lbam = buf[9];1513tf->hob_lbah = buf[10];1514tf->nsect = buf[12];1515tf->hob_nsect = buf[13];1516if (ata_id_has_ncq_autosense(dev->id) && (tf->status & ATA_SENSE))1517tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];15181519return 0;1520}15211522/**1523* ata_eh_get_ncq_success_sense - Read and process the sense data for1524* successful NCQ commands log page1525* @link: ATA link to get sense data for1526*1527* Read the sense data for successful NCQ commands log page to obtain1528* sense data for all NCQ commands that completed successfully with1529* the sense data available bit set.1530*1531* LOCKING:1532* Kernel thread context (may sleep).1533*1534* RETURNS:1535* 0 on success, -errno otherwise.1536*/1537int ata_eh_get_ncq_success_sense(struct ata_link *link)1538{1539struct ata_device *dev = link->device;1540struct ata_port *ap = dev->link->ap;1541u8 *buf = dev->cdl->ncq_sense_log_buf;1542struct ata_queued_cmd *qc;1543unsigned int err_mask, tag;1544u8 *sense, sk = 0, asc = 0, ascq = 0;1545u16 extended_sense;1546bool aux_icc_valid;1547u32 sense_valid;1548u64 val;1549int ret = 0;15501551err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2);1552if (err_mask) {1553ata_dev_err(dev,1554"Failed to read Sense Data for Successful NCQ Commands log\n");1555return -EIO;1556}15571558/* Check the log header */1559val = get_unaligned_le64(&buf[0]);1560if ((val & 0xffff) != 1 || ((val >> 16) & 0xff) != 0x0f) {1561ata_dev_err(dev,1562"Invalid Sense Data for Successful NCQ Commands log\n");1563return -EIO;1564}15651566sense_valid = get_unaligned_le32(&buf[8]);1567extended_sense = get_unaligned_le16(&buf[14]);1568aux_icc_valid = extended_sense & BIT(15);15691570ata_qc_for_each_raw(ap, qc, tag) {1571if (!(qc->flags & ATA_QCFLAG_EH) ||1572!(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) ||1573qc->err_mask ||1574ata_dev_phys_link(qc->dev) != link)1575continue;15761577/*1578* If the command does not have any sense data, clear ATA_SENSE.1579* Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished.1580*/1581if (!(sense_valid & BIT(tag))) {1582qc->result_tf.status &= ~ATA_SENSE;1583continue;1584}15851586sense = &buf[32 + 24 * tag];1587sk = sense[0];1588asc = sense[1];1589ascq = sense[2];15901591if (!ata_scsi_sense_is_valid(sk, asc, ascq)) {1592ret = -EIO;1593continue;1594}15951596qc->result_tf.nsect = sense[6];1597qc->result_tf.hob_nsect = sense[7];1598qc->result_tf.lbal = sense[8];1599qc->result_tf.lbam = sense[9];1600qc->result_tf.lbah = sense[10];1601qc->result_tf.hob_lbal = sense[11];1602qc->result_tf.hob_lbam = sense[12];1603qc->result_tf.hob_lbah = sense[13];1604if (aux_icc_valid)1605qc->result_tf.auxiliary = get_unaligned_le32(&sense[16]);16061607/* Set sense without also setting scsicmd->result */1608scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE,1609qc->scsicmd->sense_buffer, sk,1610asc, ascq);1611qc->flags |= ATA_QCFLAG_SENSE_VALID;16121613/*1614* No point in checking the return value, since the command has1615* already completed successfully.1616*/1617ata_eh_decide_disposition(qc);1618}16191620return ret;1621}16221623/**1624* ata_eh_analyze_ncq_error - analyze NCQ error1625* @link: ATA link to analyze NCQ error for1626*1627* Read log page 10h, determine the offending qc and acquire1628* error status TF. For NCQ device errors, all LLDDs have to do1629* is setting AC_ERR_DEV in ehi->err_mask. This function takes1630* care of the rest.1631*1632* LOCKING:1633* Kernel thread context (may sleep).1634*/1635void ata_eh_analyze_ncq_error(struct ata_link *link)1636{1637struct ata_port *ap = link->ap;1638struct ata_eh_context *ehc = &link->eh_context;1639struct ata_device *dev = link->device;1640struct ata_queued_cmd *qc;1641struct ata_taskfile tf;1642int tag, rc;16431644/* if frozen, we can't do much */1645if (ata_port_is_frozen(ap))1646return;16471648/* is it NCQ device error? */1649if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))1650return;16511652/* has LLDD analyzed already? */1653ata_qc_for_each_raw(ap, qc, tag) {1654if (!(qc->flags & ATA_QCFLAG_EH))1655continue;16561657if (qc->err_mask)1658return;1659}16601661/* okay, this error is ours */1662memset(&tf, 0, sizeof(tf));1663rc = ata_eh_read_log_10h(dev, &tag, &tf);1664if (rc) {1665ata_link_err(link, "failed to read log page 10h (errno=%d)\n",1666rc);1667return;1668}16691670if (!(link->sactive & BIT(tag))) {1671ata_link_err(link, "log page 10h reported inactive tag %d\n",1672tag);1673return;1674}16751676/* we've got the perpetrator, condemn it */1677qc = __ata_qc_from_tag(ap, tag);1678memcpy(&qc->result_tf, &tf, sizeof(tf));1679qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;1680qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;16811682/*1683* If the device supports NCQ autosense, ata_eh_read_log_10h() will have1684* stored the sense data in qc->result_tf.auxiliary.1685*/1686if (qc->result_tf.auxiliary) {1687char sense_key, asc, ascq;16881689sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;1690asc = (qc->result_tf.auxiliary >> 8) & 0xff;1691ascq = qc->result_tf.auxiliary & 0xff;1692if (ata_scsi_sense_is_valid(sense_key, asc, ascq)) {1693ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc,1694ascq);1695qc->flags |= ATA_QCFLAG_SENSE_VALID;1696}1697}16981699ata_qc_for_each_raw(ap, qc, tag) {1700if (!(qc->flags & ATA_QCFLAG_EH) ||1701qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD ||1702ata_dev_phys_link(qc->dev) != link)1703continue;17041705/* Skip the single QC which caused the NCQ error. */1706if (qc->err_mask)1707continue;17081709/*1710* For SATA, the STATUS and ERROR fields are shared for all NCQ1711* commands that were completed with the same SDB FIS.1712* Therefore, we have to clear the ATA_ERR bit for all QCs1713* except the one that caused the NCQ error.1714*/1715qc->result_tf.status &= ~ATA_ERR;1716qc->result_tf.error = 0;17171718/*1719* If we get a NCQ error, that means that a single command was1720* aborted. All other failed commands for our link should be1721* retried and has no business of going though further scrutiny1722* by ata_eh_link_autopsy().1723*/1724qc->flags |= ATA_QCFLAG_RETRY;1725}17261727ehc->i.err_mask &= ~AC_ERR_DEV;1728}1729EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);17301731const struct ata_port_operations sata_port_ops = {1732.inherits = &ata_base_port_ops,17331734.qc_defer = ata_std_qc_defer,1735.reset.hardreset = sata_std_hardreset,1736};1737EXPORT_SYMBOL_GPL(sata_port_ops);173817391740