libata-sata.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SATA specific part of ATA helper library
  4. *
  5. * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
  6. * Copyright 2003-2004 Jeff Garzik
  7. * Copyright 2006 Tejun Heo <htejun@gmail.com>
  8. */
  9. #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>
  16. #include "libata.h"
  17. #include "libata-transport.h"
  18. /* debounce timing parameters in msecs { interval, duration, timeout } */
  19. const unsigned int sata_deb_timing_normal[] = { 5, 100, 2000 };
  20. EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
  21. const unsigned int sata_deb_timing_hotplug[] = { 25, 500, 2000 };
  22. EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
  23. const unsigned int sata_deb_timing_long[] = { 100, 2000, 5000 };
  24. EXPORT_SYMBOL_GPL(sata_deb_timing_long);
  25. /**
  26. * sata_scr_valid - test whether SCRs are accessible
  27. * @link: ATA link to test SCR accessibility for
  28. *
  29. * Test whether SCRs are accessible for @link.
  30. *
  31. * LOCKING:
  32. * None.
  33. *
  34. * RETURNS:
  35. * 1 if SCRs are accessible, 0 otherwise.
  36. */
  37. int sata_scr_valid(struct ata_link *link)
  38. {
  39. struct ata_port *ap = link->ap;
  40. return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
  41. }
  42. EXPORT_SYMBOL_GPL(sata_scr_valid);
  43. /**
  44. * sata_scr_read - read SCR register of the specified port
  45. * @link: ATA link to read SCR for
  46. * @reg: SCR to read
  47. * @val: Place to store read value
  48. *
  49. * Read SCR register @reg of @link into *@val. This function is
  50. * guaranteed to succeed if @link is ap->link, the cable type of
  51. * the port is SATA and the port implements ->scr_read.
  52. *
  53. * LOCKING:
  54. * None if @link is ap->link. Kernel thread context otherwise.
  55. *
  56. * RETURNS:
  57. * 0 on success, negative errno on failure.
  58. */
  59. int sata_scr_read(struct ata_link *link, int reg, u32 *val)
  60. {
  61. if (ata_is_host_link(link)) {
  62. if (sata_scr_valid(link))
  63. return link->ap->ops->scr_read(link, reg, val);
  64. return -EOPNOTSUPP;
  65. }
  66. return sata_pmp_scr_read(link, reg, val);
  67. }
  68. EXPORT_SYMBOL_GPL(sata_scr_read);
  69. /**
  70. * sata_scr_write - write SCR register of the specified port
  71. * @link: ATA link to write SCR for
  72. * @reg: SCR to write
  73. * @val: value to write
  74. *
  75. * Write @val to SCR register @reg of @link. This function is
  76. * guaranteed to succeed if @link is ap->link, the cable type of
  77. * the port is SATA and the port implements ->scr_read.
  78. *
  79. * LOCKING:
  80. * None if @link is ap->link. Kernel thread context otherwise.
  81. *
  82. * RETURNS:
  83. * 0 on success, negative errno on failure.
  84. */
  85. int sata_scr_write(struct ata_link *link, int reg, u32 val)
  86. {
  87. if (ata_is_host_link(link)) {
  88. if (sata_scr_valid(link))
  89. return link->ap->ops->scr_write(link, reg, val);
  90. return -EOPNOTSUPP;
  91. }
  92. return sata_pmp_scr_write(link, reg, val);
  93. }
  94. EXPORT_SYMBOL_GPL(sata_scr_write);
  95. /**
  96. * sata_scr_write_flush - write SCR register of the specified port and flush
  97. * @link: ATA link to write SCR for
  98. * @reg: SCR to write
  99. * @val: value to write
  100. *
  101. * This function is identical to sata_scr_write() except that this
  102. * function performs flush after writing to the register.
  103. *
  104. * LOCKING:
  105. * None if @link is ap->link. Kernel thread context otherwise.
  106. *
  107. * RETURNS:
  108. * 0 on success, negative errno on failure.
  109. */
  110. int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
  111. {
  112. if (ata_is_host_link(link)) {
  113. int rc;
  114. if (sata_scr_valid(link)) {
  115. rc = link->ap->ops->scr_write(link, reg, val);
  116. if (rc == 0)
  117. rc = link->ap->ops->scr_read(link, reg, &val);
  118. return rc;
  119. }
  120. return -EOPNOTSUPP;
  121. }
  122. return sata_pmp_scr_write(link, reg, val);
  123. }
  124. EXPORT_SYMBOL_GPL(sata_scr_write_flush);
  125. /**
  126. * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
  127. * @tf: Taskfile to convert
  128. * @pmp: Port multiplier port
  129. * @is_cmd: This FIS is for command
  130. * @fis: Buffer into which data will output
  131. *
  132. * Converts a standard ATA taskfile to a Serial ATA
  133. * FIS structure (Register - Host to Device).
  134. *
  135. * LOCKING:
  136. * Inherited from caller.
  137. */
  138. void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
  139. {
  140. fis[0] = 0x27; /* Register - Host to Device FIS */
  141. fis[1] = pmp & 0xf; /* Port multiplier number*/
  142. if (is_cmd)
  143. fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
  144. fis[2] = tf->command;
  145. fis[3] = tf->feature;
  146. fis[4] = tf->lbal;
  147. fis[5] = tf->lbam;
  148. fis[6] = tf->lbah;
  149. fis[7] = tf->device;
  150. fis[8] = tf->hob_lbal;
  151. fis[9] = tf->hob_lbam;
  152. fis[10] = tf->hob_lbah;
  153. fis[11] = tf->hob_feature;
  154. fis[12] = tf->nsect;
  155. fis[13] = tf->hob_nsect;
  156. fis[14] = 0;
  157. fis[15] = tf->ctl;
  158. fis[16] = tf->auxiliary & 0xff;
  159. fis[17] = (tf->auxiliary >> 8) & 0xff;
  160. fis[18] = (tf->auxiliary >> 16) & 0xff;
  161. fis[19] = (tf->auxiliary >> 24) & 0xff;
  162. }
  163. EXPORT_SYMBOL_GPL(ata_tf_to_fis);
  164. /**
  165. * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
  166. * @fis: Buffer from which data will be input
  167. * @tf: Taskfile to output
  168. *
  169. * Converts a serial ATA FIS structure to a standard ATA taskfile.
  170. *
  171. * LOCKING:
  172. * Inherited from caller.
  173. */
  174. void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
  175. {
  176. tf->status = fis[2];
  177. tf->error = fis[3];
  178. tf->lbal = fis[4];
  179. tf->lbam = fis[5];
  180. tf->lbah = fis[6];
  181. tf->device = fis[7];
  182. tf->hob_lbal = fis[8];
  183. tf->hob_lbam = fis[9];
  184. tf->hob_lbah = fis[10];
  185. tf->nsect = fis[12];
  186. tf->hob_nsect = fis[13];
  187. }
  188. EXPORT_SYMBOL_GPL(ata_tf_from_fis);
  189. /**
  190. * sata_link_debounce - debounce SATA phy status
  191. * @link: ATA link to debounce SATA phy status for
  192. * @params: timing parameters { interval, duration, timeout } in msec
  193. * @deadline: deadline jiffies for the operation
  194. *
  195. * Make sure SStatus of @link reaches stable state, determined by
  196. * holding the same value where DET is not 1 for @duration polled
  197. * every @interval, before @timeout. Timeout constraints the
  198. * beginning of the stable state. Because DET gets stuck at 1 on
  199. * some controllers after hot unplugging, this functions waits
  200. * until timeout then returns 0 if DET is stable at 1.
  201. *
  202. * @timeout is further limited by @deadline. The sooner of the
  203. * two is used.
  204. *
  205. * LOCKING:
  206. * Kernel thread context (may sleep)
  207. *
  208. * RETURNS:
  209. * 0 on success, -errno on failure.
  210. */
  211. int sata_link_debounce(struct ata_link *link, const unsigned int *params,
  212. unsigned long deadline)
  213. {
  214. unsigned int interval = params[0];
  215. unsigned int duration = params[1];
  216. unsigned long last_jiffies, t;
  217. u32 last, cur;
  218. int rc;
  219. t = ata_deadline(jiffies, params[2]);
  220. if (time_before(t, deadline))
  221. deadline = t;
  222. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  223. return rc;
  224. cur &= 0xf;
  225. last = cur;
  226. last_jiffies = jiffies;
  227. while (1) {
  228. ata_msleep(link->ap, interval);
  229. if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
  230. return rc;
  231. cur &= 0xf;
  232. /* DET stable? */
  233. if (cur == last) {
  234. if (cur == 1 && time_before(jiffies, deadline))
  235. continue;
  236. if (time_after(jiffies,
  237. ata_deadline(last_jiffies, duration)))
  238. return 0;
  239. continue;
  240. }
  241. /* unstable, start over */
  242. last = cur;
  243. last_jiffies = jiffies;
  244. /* Check deadline. If debouncing failed, return
  245. * -EPIPE to tell upper layer to lower link speed.
  246. */
  247. if (time_after(jiffies, deadline))
  248. return -EPIPE;
  249. }
  250. }
  251. EXPORT_SYMBOL_GPL(sata_link_debounce);
  252. /**
  253. * sata_link_resume - resume SATA link
  254. * @link: ATA link to resume SATA
  255. * @params: timing parameters { interval, duration, timeout } in msec
  256. * @deadline: deadline jiffies for the operation
  257. *
  258. * Resume SATA phy @link and debounce it.
  259. *
  260. * LOCKING:
  261. * Kernel thread context (may sleep)
  262. *
  263. * RETURNS:
  264. * 0 on success, -errno on failure.
  265. */
  266. int sata_link_resume(struct ata_link *link, const unsigned int *params,
  267. unsigned long deadline)
  268. {
  269. int tries = ATA_LINK_RESUME_TRIES;
  270. u32 scontrol, serror;
  271. int rc;
  272. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  273. return rc;
  274. /*
  275. * Writes to SControl sometimes get ignored under certain
  276. * controllers (ata_piix SIDPR). Make sure DET actually is
  277. * cleared.
  278. */
  279. do {
  280. scontrol = (scontrol & 0x0f0) | 0x300;
  281. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  282. return rc;
  283. /*
  284. * Some PHYs react badly if SStatus is pounded
  285. * immediately after resuming. Delay 200ms before
  286. * debouncing.
  287. */
  288. if (!(link->flags & ATA_LFLAG_NO_DEBOUNCE_DELAY))
  289. ata_msleep(link->ap, 200);
  290. /* is SControl restored correctly? */
  291. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  292. return rc;
  293. } while ((scontrol & 0xf0f) != 0x300 && --tries);
  294. if ((scontrol & 0xf0f) != 0x300) {
  295. ata_link_warn(link, "failed to resume link (SControl %X)\n",
  296. scontrol);
  297. return 0;
  298. }
  299. if (tries < ATA_LINK_RESUME_TRIES)
  300. ata_link_warn(link, "link resume succeeded after %d retries\n",
  301. ATA_LINK_RESUME_TRIES - tries);
  302. if ((rc = sata_link_debounce(link, params, deadline)))
  303. return rc;
  304. /* clear SError, some PHYs require this even for SRST to work */
  305. if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
  306. rc = sata_scr_write(link, SCR_ERROR, serror);
  307. return rc != -EINVAL ? rc : 0;
  308. }
  309. EXPORT_SYMBOL_GPL(sata_link_resume);
  310. /**
  311. * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
  312. * @link: ATA link to manipulate SControl for
  313. * @policy: LPM policy to configure
  314. * @spm_wakeup: initiate LPM transition to active state
  315. *
  316. * Manipulate the IPM field of the SControl register of @link
  317. * according to @policy. If @policy is ATA_LPM_MAX_POWER and
  318. * @spm_wakeup is %true, the SPM field is manipulated to wake up
  319. * the link. This function also clears PHYRDY_CHG before
  320. * returning.
  321. *
  322. * LOCKING:
  323. * EH context.
  324. *
  325. * RETURNS:
  326. * 0 on success, -errno otherwise.
  327. */
  328. int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
  329. bool spm_wakeup)
  330. {
  331. struct ata_eh_context *ehc = &link->eh_context;
  332. bool woken_up = false;
  333. u32 scontrol;
  334. int rc;
  335. rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
  336. if (rc)
  337. return rc;
  338. switch (policy) {
  339. case ATA_LPM_MAX_POWER:
  340. /* disable all LPM transitions */
  341. scontrol |= (0x7 << 8);
  342. /* initiate transition to active state */
  343. if (spm_wakeup) {
  344. scontrol |= (0x4 << 12);
  345. woken_up = true;
  346. }
  347. break;
  348. case ATA_LPM_MED_POWER:
  349. /* allow LPM to PARTIAL */
  350. scontrol &= ~(0x1 << 8);
  351. scontrol |= (0x6 << 8);
  352. break;
  353. case ATA_LPM_MED_POWER_WITH_DIPM:
  354. case ATA_LPM_MIN_POWER_WITH_PARTIAL:
  355. case ATA_LPM_MIN_POWER:
  356. if (ata_link_nr_enabled(link) > 0) {
  357. /* assume no restrictions on LPM transitions */
  358. scontrol &= ~(0x7 << 8);
  359. /*
  360. * If the controller does not support partial, slumber,
  361. * or devsleep, then disallow these transitions.
  362. */
  363. if (link->ap->host->flags & ATA_HOST_NO_PART)
  364. scontrol |= (0x1 << 8);
  365. if (link->ap->host->flags & ATA_HOST_NO_SSC)
  366. scontrol |= (0x2 << 8);
  367. if (link->ap->host->flags & ATA_HOST_NO_DEVSLP)
  368. scontrol |= (0x4 << 8);
  369. } else {
  370. /* empty port, power off */
  371. scontrol &= ~0xf;
  372. scontrol |= (0x1 << 2);
  373. }
  374. break;
  375. default:
  376. WARN_ON(1);
  377. }
  378. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  379. if (rc)
  380. return rc;
  381. /* give the link time to transit out of LPM state */
  382. if (woken_up)
  383. msleep(10);
  384. /* clear PHYRDY_CHG from SError */
  385. ehc->i.serror &= ~SERR_PHYRDY_CHG;
  386. return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
  387. }
  388. EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
  389. static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
  390. {
  391. struct ata_link *host_link = &link->ap->link;
  392. u32 limit, target, spd;
  393. limit = link->sata_spd_limit;
  394. /* Don't configure downstream link faster than upstream link.
  395. * It doesn't speed up anything and some PMPs choke on such
  396. * configuration.
  397. */
  398. if (!ata_is_host_link(link) && host_link->sata_spd)
  399. limit &= (1 << host_link->sata_spd) - 1;
  400. if (limit == UINT_MAX)
  401. target = 0;
  402. else
  403. target = fls(limit);
  404. spd = (*scontrol >> 4) & 0xf;
  405. *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
  406. return spd != target;
  407. }
  408. /**
  409. * sata_set_spd_needed - is SATA spd configuration needed
  410. * @link: Link in question
  411. *
  412. * Test whether the spd limit in SControl matches
  413. * @link->sata_spd_limit. This function is used to determine
  414. * whether hardreset is necessary to apply SATA spd
  415. * configuration.
  416. *
  417. * LOCKING:
  418. * Inherited from caller.
  419. *
  420. * RETURNS:
  421. * 1 if SATA spd configuration is needed, 0 otherwise.
  422. */
  423. static int sata_set_spd_needed(struct ata_link *link)
  424. {
  425. u32 scontrol;
  426. if (sata_scr_read(link, SCR_CONTROL, &scontrol))
  427. return 1;
  428. return __sata_set_spd_needed(link, &scontrol);
  429. }
  430. /**
  431. * sata_set_spd - set SATA spd according to spd limit
  432. * @link: Link to set SATA spd for
  433. *
  434. * Set SATA spd of @link according to sata_spd_limit.
  435. *
  436. * LOCKING:
  437. * Inherited from caller.
  438. *
  439. * RETURNS:
  440. * 0 if spd doesn't need to be changed, 1 if spd has been
  441. * changed. Negative errno if SCR registers are inaccessible.
  442. */
  443. int sata_set_spd(struct ata_link *link)
  444. {
  445. u32 scontrol;
  446. int rc;
  447. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  448. return rc;
  449. if (!__sata_set_spd_needed(link, &scontrol))
  450. return 0;
  451. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  452. return rc;
  453. return 1;
  454. }
  455. EXPORT_SYMBOL_GPL(sata_set_spd);
  456. /**
  457. * sata_down_spd_limit - adjust SATA spd limit downward
  458. * @link: Link to adjust SATA spd limit for
  459. * @spd_limit: Additional limit
  460. *
  461. * Adjust SATA spd limit of @link downward. Note that this
  462. * function only adjusts the limit. The change must be applied
  463. * using sata_set_spd().
  464. *
  465. * If @spd_limit is non-zero, the speed is limited to equal to or
  466. * lower than @spd_limit if such speed is supported. If
  467. * @spd_limit is slower than any supported speed, only the lowest
  468. * supported speed is allowed.
  469. *
  470. * LOCKING:
  471. * Inherited from caller.
  472. *
  473. * RETURNS:
  474. * 0 on success, negative errno on failure
  475. */
  476. int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
  477. {
  478. u32 sstatus, spd, mask;
  479. int rc, bit;
  480. if (!sata_scr_valid(link))
  481. return -EOPNOTSUPP;
  482. /* If SCR can be read, use it to determine the current SPD.
  483. * If not, use cached value in link->sata_spd.
  484. */
  485. rc = sata_scr_read(link, SCR_STATUS, &sstatus);
  486. if (rc == 0 && ata_sstatus_online(sstatus))
  487. spd = (sstatus >> 4) & 0xf;
  488. else
  489. spd = link->sata_spd;
  490. mask = link->sata_spd_limit;
  491. if (mask <= 1)
  492. return -EINVAL;
  493. /* unconditionally mask off the highest bit */
  494. bit = fls(mask) - 1;
  495. mask &= ~(1 << bit);
  496. /*
  497. * Mask off all speeds higher than or equal to the current one. At
  498. * this point, if current SPD is not available and we previously
  499. * recorded the link speed from SStatus, the driver has already
  500. * masked off the highest bit so mask should already be 1 or 0.
  501. * Otherwise, we should not force 1.5Gbps on a link where we have
  502. * not previously recorded speed from SStatus. Just return in this
  503. * case.
  504. */
  505. if (spd > 1)
  506. mask &= (1 << (spd - 1)) - 1;
  507. else if (link->sata_spd)
  508. return -EINVAL;
  509. /* were we already at the bottom? */
  510. if (!mask)
  511. return -EINVAL;
  512. if (spd_limit) {
  513. if (mask & ((1 << spd_limit) - 1))
  514. mask &= (1 << spd_limit) - 1;
  515. else {
  516. bit = ffs(mask) - 1;
  517. mask = 1 << bit;
  518. }
  519. }
  520. link->sata_spd_limit = mask;
  521. ata_link_warn(link, "limiting SATA link speed to %s\n",
  522. sata_spd_string(fls(mask)));
  523. return 0;
  524. }
  525. /**
  526. * sata_link_hardreset - reset link via SATA phy reset
  527. * @link: link to reset
  528. * @timing: timing parameters { interval, duration, timeout } in msec
  529. * @deadline: deadline jiffies for the operation
  530. * @online: optional out parameter indicating link onlineness
  531. * @check_ready: optional callback to check link readiness
  532. *
  533. * SATA phy-reset @link using DET bits of SControl register.
  534. * After hardreset, link readiness is waited upon using
  535. * ata_wait_ready() if @check_ready is specified. LLDs are
  536. * allowed to not specify @check_ready and wait itself after this
  537. * function returns. Device classification is LLD's
  538. * responsibility.
  539. *
  540. * *@online is set to one iff reset succeeded and @link is online
  541. * after reset.
  542. *
  543. * LOCKING:
  544. * Kernel thread context (may sleep)
  545. *
  546. * RETURNS:
  547. * 0 on success, -errno otherwise.
  548. */
  549. int sata_link_hardreset(struct ata_link *link, const unsigned int *timing,
  550. unsigned long deadline,
  551. bool *online, int (*check_ready)(struct ata_link *))
  552. {
  553. u32 scontrol;
  554. int rc;
  555. if (online)
  556. *online = false;
  557. if (sata_set_spd_needed(link)) {
  558. /* SATA spec says nothing about how to reconfigure
  559. * spd. To be on the safe side, turn off phy during
  560. * reconfiguration. This works for at least ICH7 AHCI
  561. * and Sil3124.
  562. */
  563. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  564. goto out;
  565. scontrol = (scontrol & 0x0f0) | 0x304;
  566. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  567. goto out;
  568. sata_set_spd(link);
  569. }
  570. /* issue phy wake/reset */
  571. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  572. goto out;
  573. scontrol = (scontrol & 0x0f0) | 0x301;
  574. if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
  575. goto out;
  576. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  577. * 10.4.2 says at least 1 ms.
  578. */
  579. ata_msleep(link->ap, 1);
  580. /* bring link back */
  581. rc = sata_link_resume(link, timing, deadline);
  582. if (rc)
  583. goto out;
  584. /* if link is offline nothing more to do */
  585. if (ata_phys_link_offline(link))
  586. goto out;
  587. /* Link is online. From this point, -ENODEV too is an error. */
  588. if (online)
  589. *online = true;
  590. if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
  591. /* If PMP is supported, we have to do follow-up SRST.
  592. * Some PMPs don't send D2H Reg FIS after hardreset if
  593. * the first port is empty. Wait only for
  594. * ATA_TMOUT_PMP_SRST_WAIT.
  595. */
  596. if (check_ready) {
  597. unsigned long pmp_deadline;
  598. pmp_deadline = ata_deadline(jiffies,
  599. ATA_TMOUT_PMP_SRST_WAIT);
  600. if (time_after(pmp_deadline, deadline))
  601. pmp_deadline = deadline;
  602. ata_wait_ready(link, pmp_deadline, check_ready);
  603. }
  604. rc = -EAGAIN;
  605. goto out;
  606. }
  607. rc = 0;
  608. if (check_ready)
  609. rc = ata_wait_ready(link, deadline, check_ready);
  610. out:
  611. if (rc && rc != -EAGAIN) {
  612. /* online is set iff link is online && reset succeeded */
  613. if (online)
  614. *online = false;
  615. }
  616. return rc;
  617. }
  618. EXPORT_SYMBOL_GPL(sata_link_hardreset);
  619. /**
  620. * sata_std_hardreset - COMRESET w/o waiting or classification
  621. * @link: link to reset
  622. * @class: resulting class of attached device
  623. * @deadline: deadline jiffies for the operation
  624. *
  625. * Standard SATA COMRESET w/o waiting or classification.
  626. *
  627. * LOCKING:
  628. * Kernel thread context (may sleep)
  629. *
  630. * RETURNS:
  631. * 0 if link offline, -EAGAIN if link online, -errno on errors.
  632. */
  633. int sata_std_hardreset(struct ata_link *link, unsigned int *class,
  634. unsigned long deadline)
  635. {
  636. const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context);
  637. bool online;
  638. int rc;
  639. rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
  640. if (online)
  641. return -EAGAIN;
  642. return rc;
  643. }
  644. EXPORT_SYMBOL_GPL(sata_std_hardreset);
  645. /**
  646. * ata_qc_complete_multiple - Complete multiple qcs successfully
  647. * @ap: port in question
  648. * @qc_active: new qc_active mask
  649. *
  650. * Complete in-flight commands. This functions is meant to be
  651. * called from low-level driver's interrupt routine to complete
  652. * requests normally. ap->qc_active and @qc_active is compared
  653. * and commands are completed accordingly.
  654. *
  655. * Always use this function when completing multiple NCQ commands
  656. * from IRQ handlers instead of calling ata_qc_complete()
  657. * multiple times to keep IRQ expect status properly in sync.
  658. *
  659. * LOCKING:
  660. * spin_lock_irqsave(host lock)
  661. *
  662. * RETURNS:
  663. * Number of completed commands on success, -errno otherwise.
  664. */
  665. int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
  666. {
  667. u64 done_mask, ap_qc_active = ap->qc_active;
  668. int nr_done = 0;
  669. /*
  670. * If the internal tag is set on ap->qc_active, then we care about
  671. * bit0 on the passed in qc_active mask. Move that bit up to match
  672. * the internal tag.
  673. */
  674. if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
  675. qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
  676. qc_active ^= qc_active & 0x01;
  677. }
  678. done_mask = ap_qc_active ^ qc_active;
  679. if (unlikely(done_mask & qc_active)) {
  680. ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
  681. ap->qc_active, qc_active);
  682. return -EINVAL;
  683. }
  684. if (ap->ops->qc_ncq_fill_rtf)
  685. ap->ops->qc_ncq_fill_rtf(ap, done_mask);
  686. while (done_mask) {
  687. struct ata_queued_cmd *qc;
  688. unsigned int tag = __ffs64(done_mask);
  689. qc = ata_qc_from_tag(ap, tag);
  690. if (qc) {
  691. ata_qc_complete(qc);
  692. nr_done++;
  693. }
  694. done_mask &= ~(1ULL << tag);
  695. }
  696. return nr_done;
  697. }
  698. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  699. /**
  700. * ata_slave_link_init - initialize slave link
  701. * @ap: port to initialize slave link for
  702. *
  703. * Create and initialize slave link for @ap. This enables slave
  704. * link handling on the port.
  705. *
  706. * In libata, a port contains links and a link contains devices.
  707. * There is single host link but if a PMP is attached to it,
  708. * there can be multiple fan-out links. On SATA, there's usually
  709. * a single device connected to a link but PATA and SATA
  710. * controllers emulating TF based interface can have two - master
  711. * and slave.
  712. *
  713. * However, there are a few controllers which don't fit into this
  714. * abstraction too well - SATA controllers which emulate TF
  715. * interface with both master and slave devices but also have
  716. * separate SCR register sets for each device. These controllers
  717. * need separate links for physical link handling
  718. * (e.g. onlineness, link speed) but should be treated like a
  719. * traditional M/S controller for everything else (e.g. command
  720. * issue, softreset).
  721. *
  722. * slave_link is libata's way of handling this class of
  723. * controllers without impacting core layer too much. For
  724. * anything other than physical link handling, the default host
  725. * link is used for both master and slave. For physical link
  726. * handling, separate @ap->slave_link is used. All dirty details
  727. * are implemented inside libata core layer. From LLD's POV, the
  728. * only difference is that prereset, hardreset and postreset are
  729. * called once more for the slave link, so the reset sequence
  730. * looks like the following.
  731. *
  732. * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
  733. * softreset(M) -> postreset(M) -> postreset(S)
  734. *
  735. * Note that softreset is called only for the master. Softreset
  736. * resets both M/S by definition, so SRST on master should handle
  737. * both (the standard method will work just fine).
  738. *
  739. * LOCKING:
  740. * Should be called before host is registered.
  741. *
  742. * RETURNS:
  743. * 0 on success, -errno on failure.
  744. */
  745. int ata_slave_link_init(struct ata_port *ap)
  746. {
  747. struct ata_link *link;
  748. WARN_ON(ap->slave_link);
  749. WARN_ON(ap->flags & ATA_FLAG_PMP);
  750. link = kzalloc_obj(*link);
  751. if (!link)
  752. return -ENOMEM;
  753. ata_link_init(ap, link, 1);
  754. ap->slave_link = link;
  755. return 0;
  756. }
  757. EXPORT_SYMBOL_GPL(ata_slave_link_init);
  758. /**
  759. * sata_lpm_ignore_phy_events - test if PHY event should be ignored
  760. * @link: Link receiving the event
  761. *
  762. * Test whether the received PHY event has to be ignored or not.
  763. *
  764. * LOCKING:
  765. * None:
  766. *
  767. * RETURNS:
  768. * True if the event has to be ignored.
  769. */
  770. bool sata_lpm_ignore_phy_events(struct ata_link *link)
  771. {
  772. unsigned long lpm_timeout = link->last_lpm_change +
  773. msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
  774. /* if LPM is enabled, PHYRDY doesn't mean anything */
  775. if (link->lpm_policy > ATA_LPM_MAX_POWER)
  776. return true;
  777. /* ignore the first PHY event after the LPM policy changed
  778. * as it is might be spurious
  779. */
  780. if ((link->flags & ATA_LFLAG_CHANGED) &&
  781. time_before(jiffies, lpm_timeout))
  782. return true;
  783. return false;
  784. }
  785. EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
  786. static const char *ata_lpm_policy_names[] = {
  787. [ATA_LPM_UNKNOWN] = "keep_firmware_settings",
  788. [ATA_LPM_MAX_POWER] = "max_performance",
  789. [ATA_LPM_MED_POWER] = "medium_power",
  790. [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
  791. [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
  792. [ATA_LPM_MIN_POWER] = "min_power",
  793. };
  794. /*
  795. * Check if a port supports link power management.
  796. * Must be called with the port locked.
  797. */
  798. static bool ata_scsi_lpm_supported(struct ata_port *ap)
  799. {
  800. struct ata_link *link;
  801. struct ata_device *dev;
  802. if ((ap->flags & ATA_FLAG_NO_LPM) || !ap->ops->set_lpm)
  803. return false;
  804. ata_for_each_link(link, ap, EDGE) {
  805. ata_for_each_dev(dev, &ap->link, ENABLED) {
  806. if (dev->quirks & ATA_QUIRK_NOLPM)
  807. return false;
  808. }
  809. }
  810. return true;
  811. }
  812. static ssize_t ata_scsi_lpm_supported_show(struct device *dev,
  813. struct device_attribute *attr, char *buf)
  814. {
  815. struct Scsi_Host *shost = class_to_shost(dev);
  816. struct ata_port *ap = ata_shost_to_port(shost);
  817. unsigned long flags;
  818. bool supported;
  819. spin_lock_irqsave(ap->lock, flags);
  820. supported = ata_scsi_lpm_supported(ap);
  821. spin_unlock_irqrestore(ap->lock, flags);
  822. return sysfs_emit(buf, "%d\n", supported);
  823. }
  824. DEVICE_ATTR(link_power_management_supported, S_IRUGO,
  825. ata_scsi_lpm_supported_show, NULL);
  826. EXPORT_SYMBOL_GPL(dev_attr_link_power_management_supported);
  827. static ssize_t ata_scsi_lpm_store(struct device *device,
  828. struct device_attribute *attr,
  829. const char *buf, size_t count)
  830. {
  831. struct Scsi_Host *shost = class_to_shost(device);
  832. struct ata_port *ap = ata_shost_to_port(shost);
  833. enum ata_lpm_policy policy;
  834. unsigned long flags;
  835. /* UNKNOWN is internal state, iterate from MAX_POWER */
  836. for (policy = ATA_LPM_MAX_POWER;
  837. policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
  838. const char *name = ata_lpm_policy_names[policy];
  839. if (strncmp(name, buf, strlen(name)) == 0)
  840. break;
  841. }
  842. if (policy == ARRAY_SIZE(ata_lpm_policy_names))
  843. return -EINVAL;
  844. spin_lock_irqsave(ap->lock, flags);
  845. if (!ata_scsi_lpm_supported(ap)) {
  846. count = -EOPNOTSUPP;
  847. goto out_unlock;
  848. }
  849. ap->target_lpm_policy = policy;
  850. ata_port_schedule_eh(ap);
  851. out_unlock:
  852. spin_unlock_irqrestore(ap->lock, flags);
  853. return count;
  854. }
  855. static ssize_t ata_scsi_lpm_show(struct device *dev,
  856. struct device_attribute *attr, char *buf)
  857. {
  858. struct Scsi_Host *shost = class_to_shost(dev);
  859. struct ata_port *ap = ata_shost_to_port(shost);
  860. if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
  861. return -EINVAL;
  862. return sysfs_emit(buf, "%s\n",
  863. ata_lpm_policy_names[ap->target_lpm_policy]);
  864. }
  865. DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
  866. ata_scsi_lpm_show, ata_scsi_lpm_store);
  867. EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
  868. /**
  869. * ata_ncq_prio_supported - Check if device supports NCQ Priority
  870. * @ap: ATA port of the target device
  871. * @sdev: SCSI device
  872. * @supported: Address of a boolean to store the result
  873. *
  874. * Helper to check if device supports NCQ Priority feature.
  875. *
  876. * Context: Any context. Takes and releases @ap->lock.
  877. *
  878. * Return:
  879. * * %0 - OK. Status is stored into @supported
  880. * * %-ENODEV - Failed to find the ATA device
  881. */
  882. int ata_ncq_prio_supported(struct ata_port *ap, struct scsi_device *sdev,
  883. bool *supported)
  884. {
  885. struct ata_device *dev;
  886. unsigned long flags;
  887. int rc = 0;
  888. spin_lock_irqsave(ap->lock, flags);
  889. dev = ata_scsi_find_dev(ap, sdev);
  890. if (!dev)
  891. rc = -ENODEV;
  892. else
  893. *supported = dev->flags & ATA_DFLAG_NCQ_PRIO;
  894. spin_unlock_irqrestore(ap->lock, flags);
  895. return rc;
  896. }
  897. EXPORT_SYMBOL_GPL(ata_ncq_prio_supported);
  898. static ssize_t ata_ncq_prio_supported_show(struct device *device,
  899. struct device_attribute *attr,
  900. char *buf)
  901. {
  902. struct scsi_device *sdev = to_scsi_device(device);
  903. struct ata_port *ap = ata_shost_to_port(sdev->host);
  904. bool supported;
  905. int rc;
  906. rc = ata_ncq_prio_supported(ap, sdev, &supported);
  907. if (rc)
  908. return rc;
  909. return sysfs_emit(buf, "%d\n", supported);
  910. }
  911. DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL);
  912. EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported);
  913. /**
  914. * ata_ncq_prio_enabled - Check if NCQ Priority is enabled
  915. * @ap: ATA port of the target device
  916. * @sdev: SCSI device
  917. * @enabled: Address of a boolean to store the result
  918. *
  919. * Helper to check if NCQ Priority feature is enabled.
  920. *
  921. * Context: Any context. Takes and releases @ap->lock.
  922. *
  923. * Return:
  924. * * %0 - OK. Status is stored into @enabled
  925. * * %-ENODEV - Failed to find the ATA device
  926. */
  927. int ata_ncq_prio_enabled(struct ata_port *ap, struct scsi_device *sdev,
  928. bool *enabled)
  929. {
  930. struct ata_device *dev;
  931. unsigned long flags;
  932. int rc = 0;
  933. spin_lock_irqsave(ap->lock, flags);
  934. dev = ata_scsi_find_dev(ap, sdev);
  935. if (!dev)
  936. rc = -ENODEV;
  937. else
  938. *enabled = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED;
  939. spin_unlock_irqrestore(ap->lock, flags);
  940. return rc;
  941. }
  942. EXPORT_SYMBOL_GPL(ata_ncq_prio_enabled);
  943. static ssize_t ata_ncq_prio_enable_show(struct device *device,
  944. struct device_attribute *attr,
  945. char *buf)
  946. {
  947. struct scsi_device *sdev = to_scsi_device(device);
  948. struct ata_port *ap = ata_shost_to_port(sdev->host);
  949. bool enabled;
  950. int rc;
  951. rc = ata_ncq_prio_enabled(ap, sdev, &enabled);
  952. if (rc)
  953. return rc;
  954. return sysfs_emit(buf, "%d\n", enabled);
  955. }
  956. /**
  957. * ata_ncq_prio_enable - Enable/disable NCQ Priority
  958. * @ap: ATA port of the target device
  959. * @sdev: SCSI device
  960. * @enable: true - enable NCQ Priority, false - disable NCQ Priority
  961. *
  962. * Helper to enable/disable NCQ Priority feature.
  963. *
  964. * Context: Any context. Takes and releases @ap->lock.
  965. *
  966. * Return:
  967. * * %0 - OK. Status is stored into @enabled
  968. * * %-ENODEV - Failed to find the ATA device
  969. * * %-EINVAL - NCQ Priority is not supported or CDL is enabled
  970. */
  971. int ata_ncq_prio_enable(struct ata_port *ap, struct scsi_device *sdev,
  972. bool enable)
  973. {
  974. struct ata_device *dev;
  975. unsigned long flags;
  976. int rc = 0;
  977. spin_lock_irqsave(ap->lock, flags);
  978. dev = ata_scsi_find_dev(ap, sdev);
  979. if (!dev) {
  980. rc = -ENODEV;
  981. goto unlock;
  982. }
  983. if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
  984. rc = -EINVAL;
  985. goto unlock;
  986. }
  987. if (enable) {
  988. if (dev->flags & ATA_DFLAG_CDL_ENABLED) {
  989. ata_dev_err(dev,
  990. "CDL must be disabled to enable NCQ priority\n");
  991. rc = -EINVAL;
  992. goto unlock;
  993. }
  994. dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLED;
  995. } else {
  996. dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
  997. }
  998. unlock:
  999. spin_unlock_irqrestore(ap->lock, flags);
  1000. return rc;
  1001. }
  1002. EXPORT_SYMBOL_GPL(ata_ncq_prio_enable);
  1003. static ssize_t ata_ncq_prio_enable_store(struct device *device,
  1004. struct device_attribute *attr,
  1005. const char *buf, size_t len)
  1006. {
  1007. struct scsi_device *sdev = to_scsi_device(device);
  1008. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1009. bool enable;
  1010. int rc;
  1011. rc = kstrtobool(buf, &enable);
  1012. if (rc)
  1013. return rc;
  1014. rc = ata_ncq_prio_enable(ap, sdev, enable);
  1015. if (rc)
  1016. return rc;
  1017. return len;
  1018. }
  1019. DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
  1020. ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
  1021. EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
  1022. static struct attribute *ata_ncq_sdev_attrs[] = {
  1023. &dev_attr_unload_heads.attr,
  1024. &dev_attr_ncq_prio_enable.attr,
  1025. &dev_attr_ncq_prio_supported.attr,
  1026. NULL
  1027. };
  1028. static const struct attribute_group ata_ncq_sdev_attr_group = {
  1029. .attrs = ata_ncq_sdev_attrs
  1030. };
  1031. const struct attribute_group *ata_ncq_sdev_groups[] = {
  1032. &ata_ncq_sdev_attr_group,
  1033. NULL
  1034. };
  1035. EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups);
  1036. static ssize_t
  1037. ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
  1038. const char *buf, size_t count)
  1039. {
  1040. struct Scsi_Host *shost = class_to_shost(dev);
  1041. struct ata_port *ap = ata_shost_to_port(shost);
  1042. if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
  1043. return ap->ops->em_store(ap, buf, count);
  1044. return -EINVAL;
  1045. }
  1046. static ssize_t
  1047. ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
  1048. char *buf)
  1049. {
  1050. struct Scsi_Host *shost = class_to_shost(dev);
  1051. struct ata_port *ap = ata_shost_to_port(shost);
  1052. if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
  1053. return ap->ops->em_show(ap, buf);
  1054. return -EINVAL;
  1055. }
  1056. DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
  1057. ata_scsi_em_message_show, ata_scsi_em_message_store);
  1058. EXPORT_SYMBOL_GPL(dev_attr_em_message);
  1059. static ssize_t
  1060. ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
  1061. char *buf)
  1062. {
  1063. struct Scsi_Host *shost = class_to_shost(dev);
  1064. struct ata_port *ap = ata_shost_to_port(shost);
  1065. return sysfs_emit(buf, "%d\n", ap->em_message_type);
  1066. }
  1067. DEVICE_ATTR(em_message_type, S_IRUGO,
  1068. ata_scsi_em_message_type_show, NULL);
  1069. EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
  1070. static ssize_t
  1071. ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
  1072. char *buf)
  1073. {
  1074. struct scsi_device *sdev = to_scsi_device(dev);
  1075. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1076. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  1077. if (atadev && ap->ops->sw_activity_show &&
  1078. (ap->flags & ATA_FLAG_SW_ACTIVITY))
  1079. return ap->ops->sw_activity_show(atadev, buf);
  1080. return -EINVAL;
  1081. }
  1082. static ssize_t
  1083. ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
  1084. const char *buf, size_t count)
  1085. {
  1086. struct scsi_device *sdev = to_scsi_device(dev);
  1087. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1088. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  1089. enum sw_activity val;
  1090. int rc;
  1091. if (atadev && ap->ops->sw_activity_store &&
  1092. (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
  1093. val = simple_strtoul(buf, NULL, 0);
  1094. switch (val) {
  1095. case OFF: case BLINK_ON: case BLINK_OFF:
  1096. rc = ap->ops->sw_activity_store(atadev, val);
  1097. if (!rc)
  1098. return count;
  1099. else
  1100. return rc;
  1101. }
  1102. }
  1103. return -EINVAL;
  1104. }
  1105. DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
  1106. ata_scsi_activity_store);
  1107. EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
  1108. /**
  1109. * ata_change_queue_depth - Set a device maximum queue depth
  1110. * @ap: ATA port of the target device
  1111. * @sdev: SCSI device to configure queue depth for
  1112. * @queue_depth: new queue depth
  1113. *
  1114. * Helper to set a device maximum queue depth, usable with both libsas
  1115. * and libata.
  1116. *
  1117. */
  1118. int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
  1119. int queue_depth)
  1120. {
  1121. struct ata_device *dev;
  1122. unsigned long flags;
  1123. int max_queue_depth;
  1124. spin_lock_irqsave(ap->lock, flags);
  1125. dev = ata_scsi_find_dev(ap, sdev);
  1126. if (!dev || queue_depth < 1 || queue_depth == sdev->queue_depth) {
  1127. spin_unlock_irqrestore(ap->lock, flags);
  1128. return sdev->queue_depth;
  1129. }
  1130. /*
  1131. * Make sure that the queue depth requested does not exceed the device
  1132. * capabilities.
  1133. */
  1134. max_queue_depth = min(ATA_MAX_QUEUE, sdev->host->can_queue);
  1135. max_queue_depth = min(max_queue_depth, ata_id_queue_depth(dev->id));
  1136. if (queue_depth > max_queue_depth) {
  1137. spin_unlock_irqrestore(ap->lock, flags);
  1138. return -EINVAL;
  1139. }
  1140. /*
  1141. * If NCQ is not supported by the device or if the target queue depth
  1142. * is 1 (to disable drive side command queueing), turn off NCQ.
  1143. */
  1144. if (queue_depth == 1 || !ata_ncq_supported(dev)) {
  1145. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1146. queue_depth = 1;
  1147. } else {
  1148. dev->flags &= ~ATA_DFLAG_NCQ_OFF;
  1149. }
  1150. spin_unlock_irqrestore(ap->lock, flags);
  1151. if (queue_depth == sdev->queue_depth)
  1152. return sdev->queue_depth;
  1153. return scsi_change_queue_depth(sdev, queue_depth);
  1154. }
  1155. EXPORT_SYMBOL_GPL(ata_change_queue_depth);
  1156. /**
  1157. * ata_scsi_change_queue_depth - SCSI callback for queue depth config
  1158. * @sdev: SCSI device to configure queue depth for
  1159. * @queue_depth: new queue depth
  1160. *
  1161. * This is libata standard hostt->change_queue_depth callback.
  1162. * SCSI will call into this callback when user tries to set queue
  1163. * depth via sysfs.
  1164. *
  1165. * LOCKING:
  1166. * SCSI layer (we don't care)
  1167. *
  1168. * RETURNS:
  1169. * Newly configured queue depth.
  1170. */
  1171. int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
  1172. {
  1173. struct ata_port *ap = ata_shost_to_port(sdev->host);
  1174. return ata_change_queue_depth(ap, sdev, queue_depth);
  1175. }
  1176. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  1177. /**
  1178. * ata_sas_sdev_configure - Default sdev_configure routine for libata
  1179. * devices
  1180. * @sdev: SCSI device to configure
  1181. * @lim: queue limits
  1182. * @ap: ATA port to which SCSI device is attached
  1183. *
  1184. * RETURNS:
  1185. * Zero.
  1186. */
  1187. int ata_sas_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim,
  1188. struct ata_port *ap)
  1189. {
  1190. ata_scsi_sdev_config(sdev);
  1191. return ata_scsi_dev_config(sdev, lim, ap->link.device);
  1192. }
  1193. EXPORT_SYMBOL_GPL(ata_sas_sdev_configure);
  1194. /**
  1195. * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
  1196. * @cmd: SCSI command to be sent
  1197. * @ap: ATA port to which the command is being sent
  1198. *
  1199. * RETURNS:
  1200. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  1201. * 0 otherwise.
  1202. */
  1203. int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
  1204. {
  1205. int rc = 0;
  1206. if (likely(ata_dev_enabled(ap->link.device)))
  1207. rc = __ata_scsi_queuecmd(cmd, ap->link.device);
  1208. else {
  1209. cmd->result = (DID_BAD_TARGET << 16);
  1210. scsi_done(cmd);
  1211. }
  1212. return rc;
  1213. }
  1214. EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
  1215. /**
  1216. * sata_async_notification - SATA async notification handler
  1217. * @ap: ATA port where async notification is received
  1218. *
  1219. * Handler to be called when async notification via SDB FIS is
  1220. * received. This function schedules EH if necessary.
  1221. *
  1222. * LOCKING:
  1223. * spin_lock_irqsave(host lock)
  1224. *
  1225. * RETURNS:
  1226. * 1 if EH is scheduled, 0 otherwise.
  1227. */
  1228. int sata_async_notification(struct ata_port *ap)
  1229. {
  1230. u32 sntf;
  1231. int rc;
  1232. if (!(ap->flags & ATA_FLAG_AN))
  1233. return 0;
  1234. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  1235. if (rc == 0)
  1236. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  1237. if (!sata_pmp_attached(ap) || rc) {
  1238. /* PMP is not attached or SNTF is not available */
  1239. if (!sata_pmp_attached(ap)) {
  1240. /* PMP is not attached. Check whether ATAPI
  1241. * AN is configured. If so, notify media
  1242. * change.
  1243. */
  1244. struct ata_device *dev = ap->link.device;
  1245. if ((dev->class == ATA_DEV_ATAPI) &&
  1246. (dev->flags & ATA_DFLAG_AN))
  1247. ata_scsi_media_change_notify(dev);
  1248. return 0;
  1249. } else {
  1250. /* PMP is attached but SNTF is not available.
  1251. * ATAPI async media change notification is
  1252. * not used. The PMP must be reporting PHY
  1253. * status change, schedule EH.
  1254. */
  1255. ata_port_schedule_eh(ap);
  1256. return 1;
  1257. }
  1258. } else {
  1259. /* PMP is attached and SNTF is available */
  1260. struct ata_link *link;
  1261. /* check and notify ATAPI AN */
  1262. ata_for_each_link(link, ap, EDGE) {
  1263. if (!(sntf & (1 << link->pmp)))
  1264. continue;
  1265. if ((link->device->class == ATA_DEV_ATAPI) &&
  1266. (link->device->flags & ATA_DFLAG_AN))
  1267. ata_scsi_media_change_notify(link->device);
  1268. }
  1269. /* If PMP is reporting that PHY status of some
  1270. * downstream ports has changed, schedule EH.
  1271. */
  1272. if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
  1273. ata_port_schedule_eh(ap);
  1274. return 1;
  1275. }
  1276. return 0;
  1277. }
  1278. }
  1279. EXPORT_SYMBOL_GPL(sata_async_notification);
  1280. /**
  1281. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  1282. * @dev: Device to read log page 10h from
  1283. * @tag: Resulting tag of the failed command
  1284. * @tf: Resulting taskfile registers of the failed command
  1285. *
  1286. * Read log page 10h to obtain NCQ error details and clear error
  1287. * condition.
  1288. *
  1289. * LOCKING:
  1290. * Kernel thread context (may sleep).
  1291. *
  1292. * RETURNS:
  1293. * 0 on success, -errno otherwise.
  1294. */
  1295. static int ata_eh_read_log_10h(struct ata_device *dev,
  1296. int *tag, struct ata_taskfile *tf)
  1297. {
  1298. u8 *buf = dev->sector_buf;
  1299. unsigned int err_mask;
  1300. u8 csum;
  1301. int i;
  1302. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
  1303. if (err_mask)
  1304. return -EIO;
  1305. csum = 0;
  1306. for (i = 0; i < ATA_SECT_SIZE; i++)
  1307. csum += buf[i];
  1308. if (csum)
  1309. ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
  1310. csum);
  1311. if (buf[0] & 0x80)
  1312. return -ENOENT;
  1313. *tag = buf[0] & 0x1f;
  1314. tf->status = buf[2];
  1315. tf->error = buf[3];
  1316. tf->lbal = buf[4];
  1317. tf->lbam = buf[5];
  1318. tf->lbah = buf[6];
  1319. tf->device = buf[7];
  1320. tf->hob_lbal = buf[8];
  1321. tf->hob_lbam = buf[9];
  1322. tf->hob_lbah = buf[10];
  1323. tf->nsect = buf[12];
  1324. tf->hob_nsect = buf[13];
  1325. if (ata_id_has_ncq_autosense(dev->id) && (tf->status & ATA_SENSE))
  1326. tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
  1327. return 0;
  1328. }
  1329. /**
  1330. * ata_eh_get_ncq_success_sense - Read and process the sense data for
  1331. * successful NCQ commands log page
  1332. * @link: ATA link to get sense data for
  1333. *
  1334. * Read the sense data for successful NCQ commands log page to obtain
  1335. * sense data for all NCQ commands that completed successfully with
  1336. * the sense data available bit set.
  1337. *
  1338. * LOCKING:
  1339. * Kernel thread context (may sleep).
  1340. *
  1341. * RETURNS:
  1342. * 0 on success, -errno otherwise.
  1343. */
  1344. int ata_eh_get_ncq_success_sense(struct ata_link *link)
  1345. {
  1346. struct ata_device *dev = link->device;
  1347. struct ata_port *ap = dev->link->ap;
  1348. u8 *buf = dev->cdl->ncq_sense_log_buf;
  1349. struct ata_queued_cmd *qc;
  1350. unsigned int err_mask, tag;
  1351. u8 *sense, sk = 0, asc = 0, ascq = 0;
  1352. u16 extended_sense;
  1353. bool aux_icc_valid;
  1354. u32 sense_valid;
  1355. u64 val;
  1356. int ret = 0;
  1357. err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2);
  1358. if (err_mask) {
  1359. ata_dev_err(dev,
  1360. "Failed to read Sense Data for Successful NCQ Commands log\n");
  1361. return -EIO;
  1362. }
  1363. /* Check the log header */
  1364. val = get_unaligned_le64(&buf[0]);
  1365. if ((val & 0xffff) != 1 || ((val >> 16) & 0xff) != 0x0f) {
  1366. ata_dev_err(dev,
  1367. "Invalid Sense Data for Successful NCQ Commands log\n");
  1368. return -EIO;
  1369. }
  1370. sense_valid = get_unaligned_le32(&buf[8]);
  1371. extended_sense = get_unaligned_le16(&buf[14]);
  1372. aux_icc_valid = extended_sense & BIT(15);
  1373. ata_qc_for_each_raw(ap, qc, tag) {
  1374. if (!(qc->flags & ATA_QCFLAG_EH) ||
  1375. !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) ||
  1376. qc->err_mask ||
  1377. ata_dev_phys_link(qc->dev) != link)
  1378. continue;
  1379. /*
  1380. * If the command does not have any sense data, clear ATA_SENSE.
  1381. * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished.
  1382. */
  1383. if (!(sense_valid & BIT(tag))) {
  1384. qc->result_tf.status &= ~ATA_SENSE;
  1385. continue;
  1386. }
  1387. sense = &buf[32 + 24 * tag];
  1388. sk = sense[0];
  1389. asc = sense[1];
  1390. ascq = sense[2];
  1391. if (!ata_scsi_sense_is_valid(sk, asc, ascq)) {
  1392. ret = -EIO;
  1393. continue;
  1394. }
  1395. qc->result_tf.nsect = sense[6];
  1396. qc->result_tf.hob_nsect = sense[7];
  1397. qc->result_tf.lbal = sense[8];
  1398. qc->result_tf.lbam = sense[9];
  1399. qc->result_tf.lbah = sense[10];
  1400. qc->result_tf.hob_lbal = sense[11];
  1401. qc->result_tf.hob_lbam = sense[12];
  1402. qc->result_tf.hob_lbah = sense[13];
  1403. if (aux_icc_valid)
  1404. qc->result_tf.auxiliary = get_unaligned_le32(&sense[16]);
  1405. /* Set sense without also setting scsicmd->result */
  1406. scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE,
  1407. qc->scsicmd->sense_buffer, sk,
  1408. asc, ascq);
  1409. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1410. /*
  1411. * No point in checking the return value, since the command has
  1412. * already completed successfully.
  1413. */
  1414. ata_eh_decide_disposition(qc);
  1415. }
  1416. return ret;
  1417. }
  1418. /**
  1419. * ata_eh_analyze_ncq_error - analyze NCQ error
  1420. * @link: ATA link to analyze NCQ error for
  1421. *
  1422. * Read log page 10h, determine the offending qc and acquire
  1423. * error status TF. For NCQ device errors, all LLDDs have to do
  1424. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1425. * care of the rest.
  1426. *
  1427. * LOCKING:
  1428. * Kernel thread context (may sleep).
  1429. */
  1430. void ata_eh_analyze_ncq_error(struct ata_link *link)
  1431. {
  1432. struct ata_port *ap = link->ap;
  1433. struct ata_eh_context *ehc = &link->eh_context;
  1434. struct ata_device *dev = link->device;
  1435. struct ata_queued_cmd *qc;
  1436. struct ata_taskfile tf;
  1437. int tag, rc;
  1438. /* if frozen, we can't do much */
  1439. if (ata_port_is_frozen(ap))
  1440. return;
  1441. /* is it NCQ device error? */
  1442. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1443. return;
  1444. /* has LLDD analyzed already? */
  1445. ata_qc_for_each_raw(ap, qc, tag) {
  1446. if (!(qc->flags & ATA_QCFLAG_EH))
  1447. continue;
  1448. if (qc->err_mask)
  1449. return;
  1450. }
  1451. /* okay, this error is ours */
  1452. memset(&tf, 0, sizeof(tf));
  1453. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1454. if (rc) {
  1455. ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
  1456. rc);
  1457. return;
  1458. }
  1459. if (!(link->sactive & BIT(tag))) {
  1460. ata_link_err(link, "log page 10h reported inactive tag %d\n",
  1461. tag);
  1462. return;
  1463. }
  1464. /* we've got the perpetrator, condemn it */
  1465. qc = __ata_qc_from_tag(ap, tag);
  1466. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1467. qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1468. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1469. /*
  1470. * If the device supports NCQ autosense, ata_eh_read_log_10h() will have
  1471. * stored the sense data in qc->result_tf.auxiliary.
  1472. */
  1473. if (qc->result_tf.auxiliary) {
  1474. char sense_key, asc, ascq;
  1475. sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
  1476. asc = (qc->result_tf.auxiliary >> 8) & 0xff;
  1477. ascq = qc->result_tf.auxiliary & 0xff;
  1478. if (ata_scsi_sense_is_valid(sense_key, asc, ascq)) {
  1479. ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc,
  1480. ascq);
  1481. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1482. }
  1483. }
  1484. ata_qc_for_each_raw(ap, qc, tag) {
  1485. if (!(qc->flags & ATA_QCFLAG_EH) ||
  1486. qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD ||
  1487. ata_dev_phys_link(qc->dev) != link)
  1488. continue;
  1489. /* Skip the single QC which caused the NCQ error. */
  1490. if (qc->err_mask)
  1491. continue;
  1492. /*
  1493. * For SATA, the STATUS and ERROR fields are shared for all NCQ
  1494. * commands that were completed with the same SDB FIS.
  1495. * Therefore, we have to clear the ATA_ERR bit for all QCs
  1496. * except the one that caused the NCQ error.
  1497. */
  1498. qc->result_tf.status &= ~ATA_ERR;
  1499. qc->result_tf.error = 0;
  1500. /*
  1501. * If we get a NCQ error, that means that a single command was
  1502. * aborted. All other failed commands for our link should be
  1503. * retried and has no business of going though further scrutiny
  1504. * by ata_eh_link_autopsy().
  1505. */
  1506. qc->flags |= ATA_QCFLAG_RETRY;
  1507. }
  1508. ehc->i.err_mask &= ~AC_ERR_DEV;
  1509. }
  1510. EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
  1511. const struct ata_port_operations sata_port_ops = {
  1512. .inherits = &ata_base_port_ops,
  1513. .qc_defer = ata_std_qc_defer,
  1514. .reset.hardreset = sata_std_hardreset,
  1515. };
  1516. EXPORT_SYMBOL_GPL(sata_port_ops);