libata-sata.c 38 KB

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