libata-sata.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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 <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->command = fis[2]; /* status */
  175. tf->feature = fis[3]; /* error */
  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_DB_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. /* no restrictions on LPM transitions */
  356. scontrol &= ~(0x7 << 8);
  357. else {
  358. /* empty port, power off */
  359. scontrol &= ~0xf;
  360. scontrol |= (0x1 << 2);
  361. }
  362. break;
  363. default:
  364. WARN_ON(1);
  365. }
  366. rc = sata_scr_write(link, SCR_CONTROL, scontrol);
  367. if (rc)
  368. return rc;
  369. /* give the link time to transit out of LPM state */
  370. if (woken_up)
  371. msleep(10);
  372. /* clear PHYRDY_CHG from SError */
  373. ehc->i.serror &= ~SERR_PHYRDY_CHG;
  374. return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
  375. }
  376. EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
  377. static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
  378. {
  379. struct ata_link *host_link = &link->ap->link;
  380. u32 limit, target, spd;
  381. limit = link->sata_spd_limit;
  382. /* Don't configure downstream link faster than upstream link.
  383. * It doesn't speed up anything and some PMPs choke on such
  384. * configuration.
  385. */
  386. if (!ata_is_host_link(link) && host_link->sata_spd)
  387. limit &= (1 << host_link->sata_spd) - 1;
  388. if (limit == UINT_MAX)
  389. target = 0;
  390. else
  391. target = fls(limit);
  392. spd = (*scontrol >> 4) & 0xf;
  393. *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
  394. return spd != target;
  395. }
  396. /**
  397. * sata_set_spd_needed - is SATA spd configuration needed
  398. * @link: Link in question
  399. *
  400. * Test whether the spd limit in SControl matches
  401. * @link->sata_spd_limit. This function is used to determine
  402. * whether hardreset is necessary to apply SATA spd
  403. * configuration.
  404. *
  405. * LOCKING:
  406. * Inherited from caller.
  407. *
  408. * RETURNS:
  409. * 1 if SATA spd configuration is needed, 0 otherwise.
  410. */
  411. static int sata_set_spd_needed(struct ata_link *link)
  412. {
  413. u32 scontrol;
  414. if (sata_scr_read(link, SCR_CONTROL, &scontrol))
  415. return 1;
  416. return __sata_set_spd_needed(link, &scontrol);
  417. }
  418. /**
  419. * sata_set_spd - set SATA spd according to spd limit
  420. * @link: Link to set SATA spd for
  421. *
  422. * Set SATA spd of @link according to sata_spd_limit.
  423. *
  424. * LOCKING:
  425. * Inherited from caller.
  426. *
  427. * RETURNS:
  428. * 0 if spd doesn't need to be changed, 1 if spd has been
  429. * changed. Negative errno if SCR registers are inaccessible.
  430. */
  431. int sata_set_spd(struct ata_link *link)
  432. {
  433. u32 scontrol;
  434. int rc;
  435. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  436. return rc;
  437. if (!__sata_set_spd_needed(link, &scontrol))
  438. return 0;
  439. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  440. return rc;
  441. return 1;
  442. }
  443. EXPORT_SYMBOL_GPL(sata_set_spd);
  444. /**
  445. * sata_link_hardreset - reset link via SATA phy reset
  446. * @link: link to reset
  447. * @timing: timing parameters { interval, duration, timeout } in msec
  448. * @deadline: deadline jiffies for the operation
  449. * @online: optional out parameter indicating link onlineness
  450. * @check_ready: optional callback to check link readiness
  451. *
  452. * SATA phy-reset @link using DET bits of SControl register.
  453. * After hardreset, link readiness is waited upon using
  454. * ata_wait_ready() if @check_ready is specified. LLDs are
  455. * allowed to not specify @check_ready and wait itself after this
  456. * function returns. Device classification is LLD's
  457. * responsibility.
  458. *
  459. * *@online is set to one iff reset succeeded and @link is online
  460. * after reset.
  461. *
  462. * LOCKING:
  463. * Kernel thread context (may sleep)
  464. *
  465. * RETURNS:
  466. * 0 on success, -errno otherwise.
  467. */
  468. int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
  469. unsigned long deadline,
  470. bool *online, int (*check_ready)(struct ata_link *))
  471. {
  472. u32 scontrol;
  473. int rc;
  474. DPRINTK("ENTER\n");
  475. if (online)
  476. *online = false;
  477. if (sata_set_spd_needed(link)) {
  478. /* SATA spec says nothing about how to reconfigure
  479. * spd. To be on the safe side, turn off phy during
  480. * reconfiguration. This works for at least ICH7 AHCI
  481. * and Sil3124.
  482. */
  483. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  484. goto out;
  485. scontrol = (scontrol & 0x0f0) | 0x304;
  486. if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
  487. goto out;
  488. sata_set_spd(link);
  489. }
  490. /* issue phy wake/reset */
  491. if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
  492. goto out;
  493. scontrol = (scontrol & 0x0f0) | 0x301;
  494. if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
  495. goto out;
  496. /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
  497. * 10.4.2 says at least 1 ms.
  498. */
  499. ata_msleep(link->ap, 1);
  500. /* bring link back */
  501. rc = sata_link_resume(link, timing, deadline);
  502. if (rc)
  503. goto out;
  504. /* if link is offline nothing more to do */
  505. if (ata_phys_link_offline(link))
  506. goto out;
  507. /* Link is online. From this point, -ENODEV too is an error. */
  508. if (online)
  509. *online = true;
  510. if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
  511. /* If PMP is supported, we have to do follow-up SRST.
  512. * Some PMPs don't send D2H Reg FIS after hardreset if
  513. * the first port is empty. Wait only for
  514. * ATA_TMOUT_PMP_SRST_WAIT.
  515. */
  516. if (check_ready) {
  517. unsigned long pmp_deadline;
  518. pmp_deadline = ata_deadline(jiffies,
  519. ATA_TMOUT_PMP_SRST_WAIT);
  520. if (time_after(pmp_deadline, deadline))
  521. pmp_deadline = deadline;
  522. ata_wait_ready(link, pmp_deadline, check_ready);
  523. }
  524. rc = -EAGAIN;
  525. goto out;
  526. }
  527. rc = 0;
  528. if (check_ready)
  529. rc = ata_wait_ready(link, deadline, check_ready);
  530. out:
  531. if (rc && rc != -EAGAIN) {
  532. /* online is set iff link is online && reset succeeded */
  533. if (online)
  534. *online = false;
  535. ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
  536. }
  537. DPRINTK("EXIT, rc=%d\n", rc);
  538. return rc;
  539. }
  540. EXPORT_SYMBOL_GPL(sata_link_hardreset);
  541. /**
  542. * ata_qc_complete_multiple - Complete multiple qcs successfully
  543. * @ap: port in question
  544. * @qc_active: new qc_active mask
  545. *
  546. * Complete in-flight commands. This functions is meant to be
  547. * called from low-level driver's interrupt routine to complete
  548. * requests normally. ap->qc_active and @qc_active is compared
  549. * and commands are completed accordingly.
  550. *
  551. * Always use this function when completing multiple NCQ commands
  552. * from IRQ handlers instead of calling ata_qc_complete()
  553. * multiple times to keep IRQ expect status properly in sync.
  554. *
  555. * LOCKING:
  556. * spin_lock_irqsave(host lock)
  557. *
  558. * RETURNS:
  559. * Number of completed commands on success, -errno otherwise.
  560. */
  561. int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
  562. {
  563. u64 done_mask, ap_qc_active = ap->qc_active;
  564. int nr_done = 0;
  565. /*
  566. * If the internal tag is set on ap->qc_active, then we care about
  567. * bit0 on the passed in qc_active mask. Move that bit up to match
  568. * the internal tag.
  569. */
  570. if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
  571. qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
  572. qc_active ^= qc_active & 0x01;
  573. }
  574. done_mask = ap_qc_active ^ qc_active;
  575. if (unlikely(done_mask & qc_active)) {
  576. ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
  577. ap->qc_active, qc_active);
  578. return -EINVAL;
  579. }
  580. while (done_mask) {
  581. struct ata_queued_cmd *qc;
  582. unsigned int tag = __ffs64(done_mask);
  583. qc = ata_qc_from_tag(ap, tag);
  584. if (qc) {
  585. ata_qc_complete(qc);
  586. nr_done++;
  587. }
  588. done_mask &= ~(1ULL << tag);
  589. }
  590. return nr_done;
  591. }
  592. EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
  593. /**
  594. * ata_slave_link_init - initialize slave link
  595. * @ap: port to initialize slave link for
  596. *
  597. * Create and initialize slave link for @ap. This enables slave
  598. * link handling on the port.
  599. *
  600. * In libata, a port contains links and a link contains devices.
  601. * There is single host link but if a PMP is attached to it,
  602. * there can be multiple fan-out links. On SATA, there's usually
  603. * a single device connected to a link but PATA and SATA
  604. * controllers emulating TF based interface can have two - master
  605. * and slave.
  606. *
  607. * However, there are a few controllers which don't fit into this
  608. * abstraction too well - SATA controllers which emulate TF
  609. * interface with both master and slave devices but also have
  610. * separate SCR register sets for each device. These controllers
  611. * need separate links for physical link handling
  612. * (e.g. onlineness, link speed) but should be treated like a
  613. * traditional M/S controller for everything else (e.g. command
  614. * issue, softreset).
  615. *
  616. * slave_link is libata's way of handling this class of
  617. * controllers without impacting core layer too much. For
  618. * anything other than physical link handling, the default host
  619. * link is used for both master and slave. For physical link
  620. * handling, separate @ap->slave_link is used. All dirty details
  621. * are implemented inside libata core layer. From LLD's POV, the
  622. * only difference is that prereset, hardreset and postreset are
  623. * called once more for the slave link, so the reset sequence
  624. * looks like the following.
  625. *
  626. * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
  627. * softreset(M) -> postreset(M) -> postreset(S)
  628. *
  629. * Note that softreset is called only for the master. Softreset
  630. * resets both M/S by definition, so SRST on master should handle
  631. * both (the standard method will work just fine).
  632. *
  633. * LOCKING:
  634. * Should be called before host is registered.
  635. *
  636. * RETURNS:
  637. * 0 on success, -errno on failure.
  638. */
  639. int ata_slave_link_init(struct ata_port *ap)
  640. {
  641. struct ata_link *link;
  642. WARN_ON(ap->slave_link);
  643. WARN_ON(ap->flags & ATA_FLAG_PMP);
  644. link = kzalloc(sizeof(*link), GFP_KERNEL);
  645. if (!link)
  646. return -ENOMEM;
  647. ata_link_init(ap, link, 1);
  648. ap->slave_link = link;
  649. return 0;
  650. }
  651. EXPORT_SYMBOL_GPL(ata_slave_link_init);
  652. /**
  653. * sata_lpm_ignore_phy_events - test if PHY event should be ignored
  654. * @link: Link receiving the event
  655. *
  656. * Test whether the received PHY event has to be ignored or not.
  657. *
  658. * LOCKING:
  659. * None:
  660. *
  661. * RETURNS:
  662. * True if the event has to be ignored.
  663. */
  664. bool sata_lpm_ignore_phy_events(struct ata_link *link)
  665. {
  666. unsigned long lpm_timeout = link->last_lpm_change +
  667. msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
  668. /* if LPM is enabled, PHYRDY doesn't mean anything */
  669. if (link->lpm_policy > ATA_LPM_MAX_POWER)
  670. return true;
  671. /* ignore the first PHY event after the LPM policy changed
  672. * as it is might be spurious
  673. */
  674. if ((link->flags & ATA_LFLAG_CHANGED) &&
  675. time_before(jiffies, lpm_timeout))
  676. return true;
  677. return false;
  678. }
  679. EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
  680. static const char *ata_lpm_policy_names[] = {
  681. [ATA_LPM_UNKNOWN] = "max_performance",
  682. [ATA_LPM_MAX_POWER] = "max_performance",
  683. [ATA_LPM_MED_POWER] = "medium_power",
  684. [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
  685. [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
  686. [ATA_LPM_MIN_POWER] = "min_power",
  687. };
  688. static ssize_t ata_scsi_lpm_store(struct device *device,
  689. struct device_attribute *attr,
  690. const char *buf, size_t count)
  691. {
  692. struct Scsi_Host *shost = class_to_shost(device);
  693. struct ata_port *ap = ata_shost_to_port(shost);
  694. struct ata_link *link;
  695. struct ata_device *dev;
  696. enum ata_lpm_policy policy;
  697. unsigned long flags;
  698. /* UNKNOWN is internal state, iterate from MAX_POWER */
  699. for (policy = ATA_LPM_MAX_POWER;
  700. policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
  701. const char *name = ata_lpm_policy_names[policy];
  702. if (strncmp(name, buf, strlen(name)) == 0)
  703. break;
  704. }
  705. if (policy == ARRAY_SIZE(ata_lpm_policy_names))
  706. return -EINVAL;
  707. spin_lock_irqsave(ap->lock, flags);
  708. ata_for_each_link(link, ap, EDGE) {
  709. ata_for_each_dev(dev, &ap->link, ENABLED) {
  710. if (dev->horkage & ATA_HORKAGE_NOLPM) {
  711. count = -EOPNOTSUPP;
  712. goto out_unlock;
  713. }
  714. }
  715. }
  716. ap->target_lpm_policy = policy;
  717. ata_port_schedule_eh(ap);
  718. out_unlock:
  719. spin_unlock_irqrestore(ap->lock, flags);
  720. return count;
  721. }
  722. static ssize_t ata_scsi_lpm_show(struct device *dev,
  723. struct device_attribute *attr, char *buf)
  724. {
  725. struct Scsi_Host *shost = class_to_shost(dev);
  726. struct ata_port *ap = ata_shost_to_port(shost);
  727. if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
  728. return -EINVAL;
  729. return snprintf(buf, PAGE_SIZE, "%s\n",
  730. ata_lpm_policy_names[ap->target_lpm_policy]);
  731. }
  732. DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
  733. ata_scsi_lpm_show, ata_scsi_lpm_store);
  734. EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
  735. static ssize_t ata_ncq_prio_enable_show(struct device *device,
  736. struct device_attribute *attr,
  737. char *buf)
  738. {
  739. struct scsi_device *sdev = to_scsi_device(device);
  740. struct ata_port *ap;
  741. struct ata_device *dev;
  742. bool ncq_prio_enable;
  743. int rc = 0;
  744. ap = ata_shost_to_port(sdev->host);
  745. spin_lock_irq(ap->lock);
  746. dev = ata_scsi_find_dev(ap, sdev);
  747. if (!dev) {
  748. rc = -ENODEV;
  749. goto unlock;
  750. }
  751. ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
  752. unlock:
  753. spin_unlock_irq(ap->lock);
  754. return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
  755. }
  756. static ssize_t ata_ncq_prio_enable_store(struct device *device,
  757. struct device_attribute *attr,
  758. const char *buf, size_t len)
  759. {
  760. struct scsi_device *sdev = to_scsi_device(device);
  761. struct ata_port *ap;
  762. struct ata_device *dev;
  763. long int input;
  764. int rc;
  765. rc = kstrtol(buf, 10, &input);
  766. if (rc)
  767. return rc;
  768. if ((input < 0) || (input > 1))
  769. return -EINVAL;
  770. ap = ata_shost_to_port(sdev->host);
  771. dev = ata_scsi_find_dev(ap, sdev);
  772. if (unlikely(!dev))
  773. return -ENODEV;
  774. spin_lock_irq(ap->lock);
  775. if (input)
  776. dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
  777. else
  778. dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
  779. dev->link->eh_info.action |= ATA_EH_REVALIDATE;
  780. dev->link->eh_info.flags |= ATA_EHI_QUIET;
  781. ata_port_schedule_eh(ap);
  782. spin_unlock_irq(ap->lock);
  783. ata_port_wait_eh(ap);
  784. if (input) {
  785. spin_lock_irq(ap->lock);
  786. if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
  787. dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
  788. rc = -EIO;
  789. }
  790. spin_unlock_irq(ap->lock);
  791. }
  792. return rc ? rc : len;
  793. }
  794. DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
  795. ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
  796. EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
  797. struct device_attribute *ata_ncq_sdev_attrs[] = {
  798. &dev_attr_unload_heads,
  799. &dev_attr_ncq_prio_enable,
  800. NULL
  801. };
  802. EXPORT_SYMBOL_GPL(ata_ncq_sdev_attrs);
  803. static ssize_t
  804. ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
  805. const char *buf, size_t count)
  806. {
  807. struct Scsi_Host *shost = class_to_shost(dev);
  808. struct ata_port *ap = ata_shost_to_port(shost);
  809. if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
  810. return ap->ops->em_store(ap, buf, count);
  811. return -EINVAL;
  812. }
  813. static ssize_t
  814. ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
  815. char *buf)
  816. {
  817. struct Scsi_Host *shost = class_to_shost(dev);
  818. struct ata_port *ap = ata_shost_to_port(shost);
  819. if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
  820. return ap->ops->em_show(ap, buf);
  821. return -EINVAL;
  822. }
  823. DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
  824. ata_scsi_em_message_show, ata_scsi_em_message_store);
  825. EXPORT_SYMBOL_GPL(dev_attr_em_message);
  826. static ssize_t
  827. ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
  828. char *buf)
  829. {
  830. struct Scsi_Host *shost = class_to_shost(dev);
  831. struct ata_port *ap = ata_shost_to_port(shost);
  832. return snprintf(buf, 23, "%d\n", ap->em_message_type);
  833. }
  834. DEVICE_ATTR(em_message_type, S_IRUGO,
  835. ata_scsi_em_message_type_show, NULL);
  836. EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
  837. static ssize_t
  838. ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
  839. char *buf)
  840. {
  841. struct scsi_device *sdev = to_scsi_device(dev);
  842. struct ata_port *ap = ata_shost_to_port(sdev->host);
  843. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  844. if (atadev && ap->ops->sw_activity_show &&
  845. (ap->flags & ATA_FLAG_SW_ACTIVITY))
  846. return ap->ops->sw_activity_show(atadev, buf);
  847. return -EINVAL;
  848. }
  849. static ssize_t
  850. ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
  851. const char *buf, size_t count)
  852. {
  853. struct scsi_device *sdev = to_scsi_device(dev);
  854. struct ata_port *ap = ata_shost_to_port(sdev->host);
  855. struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
  856. enum sw_activity val;
  857. int rc;
  858. if (atadev && ap->ops->sw_activity_store &&
  859. (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
  860. val = simple_strtoul(buf, NULL, 0);
  861. switch (val) {
  862. case OFF: case BLINK_ON: case BLINK_OFF:
  863. rc = ap->ops->sw_activity_store(atadev, val);
  864. if (!rc)
  865. return count;
  866. else
  867. return rc;
  868. }
  869. }
  870. return -EINVAL;
  871. }
  872. DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
  873. ata_scsi_activity_store);
  874. EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
  875. /**
  876. * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
  877. * @ap: ATA port to which the device change the queue depth
  878. * @sdev: SCSI device to configure queue depth for
  879. * @queue_depth: new queue depth
  880. *
  881. * libsas and libata have different approaches for associating a sdev to
  882. * its ata_port.
  883. *
  884. */
  885. int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
  886. int queue_depth)
  887. {
  888. struct ata_device *dev;
  889. unsigned long flags;
  890. if (queue_depth < 1 || queue_depth == sdev->queue_depth)
  891. return sdev->queue_depth;
  892. dev = ata_scsi_find_dev(ap, sdev);
  893. if (!dev || !ata_dev_enabled(dev))
  894. return sdev->queue_depth;
  895. /* NCQ enabled? */
  896. spin_lock_irqsave(ap->lock, flags);
  897. dev->flags &= ~ATA_DFLAG_NCQ_OFF;
  898. if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
  899. dev->flags |= ATA_DFLAG_NCQ_OFF;
  900. queue_depth = 1;
  901. }
  902. spin_unlock_irqrestore(ap->lock, flags);
  903. /* limit and apply queue depth */
  904. queue_depth = min(queue_depth, sdev->host->can_queue);
  905. queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
  906. queue_depth = min(queue_depth, ATA_MAX_QUEUE);
  907. if (sdev->queue_depth == queue_depth)
  908. return -EINVAL;
  909. return scsi_change_queue_depth(sdev, queue_depth);
  910. }
  911. EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
  912. /**
  913. * ata_scsi_change_queue_depth - SCSI callback for queue depth config
  914. * @sdev: SCSI device to configure queue depth for
  915. * @queue_depth: new queue depth
  916. *
  917. * This is libata standard hostt->change_queue_depth callback.
  918. * SCSI will call into this callback when user tries to set queue
  919. * depth via sysfs.
  920. *
  921. * LOCKING:
  922. * SCSI layer (we don't care)
  923. *
  924. * RETURNS:
  925. * Newly configured queue depth.
  926. */
  927. int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
  928. {
  929. struct ata_port *ap = ata_shost_to_port(sdev->host);
  930. return __ata_change_queue_depth(ap, sdev, queue_depth);
  931. }
  932. EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
  933. /**
  934. * port_alloc - Allocate port for a SAS attached SATA device
  935. * @host: ATA host container for all SAS ports
  936. * @port_info: Information from low-level host driver
  937. * @shost: SCSI host that the scsi device is attached to
  938. *
  939. * LOCKING:
  940. * PCI/etc. bus probe sem.
  941. *
  942. * RETURNS:
  943. * ata_port pointer on success / NULL on failure.
  944. */
  945. struct ata_port *ata_sas_port_alloc(struct ata_host *host,
  946. struct ata_port_info *port_info,
  947. struct Scsi_Host *shost)
  948. {
  949. struct ata_port *ap;
  950. ap = ata_port_alloc(host);
  951. if (!ap)
  952. return NULL;
  953. ap->port_no = 0;
  954. ap->lock = &host->lock;
  955. ap->pio_mask = port_info->pio_mask;
  956. ap->mwdma_mask = port_info->mwdma_mask;
  957. ap->udma_mask = port_info->udma_mask;
  958. ap->flags |= port_info->flags;
  959. ap->ops = port_info->port_ops;
  960. ap->cbl = ATA_CBL_SATA;
  961. return ap;
  962. }
  963. EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
  964. /**
  965. * ata_sas_port_start - Set port up for dma.
  966. * @ap: Port to initialize
  967. *
  968. * Called just after data structures for each port are
  969. * initialized.
  970. *
  971. * May be used as the port_start() entry in ata_port_operations.
  972. *
  973. * LOCKING:
  974. * Inherited from caller.
  975. */
  976. int ata_sas_port_start(struct ata_port *ap)
  977. {
  978. /*
  979. * the port is marked as frozen at allocation time, but if we don't
  980. * have new eh, we won't thaw it
  981. */
  982. if (!ap->ops->error_handler)
  983. ap->pflags &= ~ATA_PFLAG_FROZEN;
  984. return 0;
  985. }
  986. EXPORT_SYMBOL_GPL(ata_sas_port_start);
  987. /**
  988. * ata_port_stop - Undo ata_sas_port_start()
  989. * @ap: Port to shut down
  990. *
  991. * May be used as the port_stop() entry in ata_port_operations.
  992. *
  993. * LOCKING:
  994. * Inherited from caller.
  995. */
  996. void ata_sas_port_stop(struct ata_port *ap)
  997. {
  998. }
  999. EXPORT_SYMBOL_GPL(ata_sas_port_stop);
  1000. /**
  1001. * ata_sas_async_probe - simply schedule probing and return
  1002. * @ap: Port to probe
  1003. *
  1004. * For batch scheduling of probe for sas attached ata devices, assumes
  1005. * the port has already been through ata_sas_port_init()
  1006. */
  1007. void ata_sas_async_probe(struct ata_port *ap)
  1008. {
  1009. __ata_port_probe(ap);
  1010. }
  1011. EXPORT_SYMBOL_GPL(ata_sas_async_probe);
  1012. int ata_sas_sync_probe(struct ata_port *ap)
  1013. {
  1014. return ata_port_probe(ap);
  1015. }
  1016. EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
  1017. /**
  1018. * ata_sas_port_init - Initialize a SATA device
  1019. * @ap: SATA port to initialize
  1020. *
  1021. * LOCKING:
  1022. * PCI/etc. bus probe sem.
  1023. *
  1024. * RETURNS:
  1025. * Zero on success, non-zero on error.
  1026. */
  1027. int ata_sas_port_init(struct ata_port *ap)
  1028. {
  1029. int rc = ap->ops->port_start(ap);
  1030. if (rc)
  1031. return rc;
  1032. ap->print_id = atomic_inc_return(&ata_print_id);
  1033. return 0;
  1034. }
  1035. EXPORT_SYMBOL_GPL(ata_sas_port_init);
  1036. int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
  1037. {
  1038. return ata_tport_add(parent, ap);
  1039. }
  1040. EXPORT_SYMBOL_GPL(ata_sas_tport_add);
  1041. void ata_sas_tport_delete(struct ata_port *ap)
  1042. {
  1043. ata_tport_delete(ap);
  1044. }
  1045. EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
  1046. /**
  1047. * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
  1048. * @ap: SATA port to destroy
  1049. *
  1050. */
  1051. void ata_sas_port_destroy(struct ata_port *ap)
  1052. {
  1053. if (ap->ops->port_stop)
  1054. ap->ops->port_stop(ap);
  1055. kfree(ap);
  1056. }
  1057. EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
  1058. /**
  1059. * ata_sas_slave_configure - Default slave_config routine for libata devices
  1060. * @sdev: SCSI device to configure
  1061. * @ap: ATA port to which SCSI device is attached
  1062. *
  1063. * RETURNS:
  1064. * Zero.
  1065. */
  1066. int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
  1067. {
  1068. ata_scsi_sdev_config(sdev);
  1069. ata_scsi_dev_config(sdev, ap->link.device);
  1070. return 0;
  1071. }
  1072. EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
  1073. /**
  1074. * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
  1075. * @cmd: SCSI command to be sent
  1076. * @ap: ATA port to which the command is being sent
  1077. *
  1078. * RETURNS:
  1079. * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
  1080. * 0 otherwise.
  1081. */
  1082. int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
  1083. {
  1084. int rc = 0;
  1085. ata_scsi_dump_cdb(ap, cmd);
  1086. if (likely(ata_dev_enabled(ap->link.device)))
  1087. rc = __ata_scsi_queuecmd(cmd, ap->link.device);
  1088. else {
  1089. cmd->result = (DID_BAD_TARGET << 16);
  1090. cmd->scsi_done(cmd);
  1091. }
  1092. return rc;
  1093. }
  1094. EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
  1095. int ata_sas_allocate_tag(struct ata_port *ap)
  1096. {
  1097. unsigned int max_queue = ap->host->n_tags;
  1098. unsigned int i, tag;
  1099. for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
  1100. tag = tag < max_queue ? tag : 0;
  1101. /* the last tag is reserved for internal command. */
  1102. if (ata_tag_internal(tag))
  1103. continue;
  1104. if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
  1105. ap->sas_last_tag = tag;
  1106. return tag;
  1107. }
  1108. }
  1109. return -1;
  1110. }
  1111. void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
  1112. {
  1113. clear_bit(tag, &ap->sas_tag_allocated);
  1114. }
  1115. /**
  1116. * sata_async_notification - SATA async notification handler
  1117. * @ap: ATA port where async notification is received
  1118. *
  1119. * Handler to be called when async notification via SDB FIS is
  1120. * received. This function schedules EH if necessary.
  1121. *
  1122. * LOCKING:
  1123. * spin_lock_irqsave(host lock)
  1124. *
  1125. * RETURNS:
  1126. * 1 if EH is scheduled, 0 otherwise.
  1127. */
  1128. int sata_async_notification(struct ata_port *ap)
  1129. {
  1130. u32 sntf;
  1131. int rc;
  1132. if (!(ap->flags & ATA_FLAG_AN))
  1133. return 0;
  1134. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  1135. if (rc == 0)
  1136. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  1137. if (!sata_pmp_attached(ap) || rc) {
  1138. /* PMP is not attached or SNTF is not available */
  1139. if (!sata_pmp_attached(ap)) {
  1140. /* PMP is not attached. Check whether ATAPI
  1141. * AN is configured. If so, notify media
  1142. * change.
  1143. */
  1144. struct ata_device *dev = ap->link.device;
  1145. if ((dev->class == ATA_DEV_ATAPI) &&
  1146. (dev->flags & ATA_DFLAG_AN))
  1147. ata_scsi_media_change_notify(dev);
  1148. return 0;
  1149. } else {
  1150. /* PMP is attached but SNTF is not available.
  1151. * ATAPI async media change notification is
  1152. * not used. The PMP must be reporting PHY
  1153. * status change, schedule EH.
  1154. */
  1155. ata_port_schedule_eh(ap);
  1156. return 1;
  1157. }
  1158. } else {
  1159. /* PMP is attached and SNTF is available */
  1160. struct ata_link *link;
  1161. /* check and notify ATAPI AN */
  1162. ata_for_each_link(link, ap, EDGE) {
  1163. if (!(sntf & (1 << link->pmp)))
  1164. continue;
  1165. if ((link->device->class == ATA_DEV_ATAPI) &&
  1166. (link->device->flags & ATA_DFLAG_AN))
  1167. ata_scsi_media_change_notify(link->device);
  1168. }
  1169. /* If PMP is reporting that PHY status of some
  1170. * downstream ports has changed, schedule EH.
  1171. */
  1172. if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
  1173. ata_port_schedule_eh(ap);
  1174. return 1;
  1175. }
  1176. return 0;
  1177. }
  1178. }
  1179. EXPORT_SYMBOL_GPL(sata_async_notification);
  1180. /**
  1181. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  1182. * @dev: Device to read log page 10h from
  1183. * @tag: Resulting tag of the failed command
  1184. * @tf: Resulting taskfile registers of the failed command
  1185. *
  1186. * Read log page 10h to obtain NCQ error details and clear error
  1187. * condition.
  1188. *
  1189. * LOCKING:
  1190. * Kernel thread context (may sleep).
  1191. *
  1192. * RETURNS:
  1193. * 0 on success, -errno otherwise.
  1194. */
  1195. static int ata_eh_read_log_10h(struct ata_device *dev,
  1196. int *tag, struct ata_taskfile *tf)
  1197. {
  1198. u8 *buf = dev->link->ap->sector_buf;
  1199. unsigned int err_mask;
  1200. u8 csum;
  1201. int i;
  1202. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
  1203. if (err_mask)
  1204. return -EIO;
  1205. csum = 0;
  1206. for (i = 0; i < ATA_SECT_SIZE; i++)
  1207. csum += buf[i];
  1208. if (csum)
  1209. ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
  1210. csum);
  1211. if (buf[0] & 0x80)
  1212. return -ENOENT;
  1213. *tag = buf[0] & 0x1f;
  1214. tf->command = buf[2];
  1215. tf->feature = buf[3];
  1216. tf->lbal = buf[4];
  1217. tf->lbam = buf[5];
  1218. tf->lbah = buf[6];
  1219. tf->device = buf[7];
  1220. tf->hob_lbal = buf[8];
  1221. tf->hob_lbam = buf[9];
  1222. tf->hob_lbah = buf[10];
  1223. tf->nsect = buf[12];
  1224. tf->hob_nsect = buf[13];
  1225. if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
  1226. tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
  1227. return 0;
  1228. }
  1229. /**
  1230. * ata_eh_analyze_ncq_error - analyze NCQ error
  1231. * @link: ATA link to analyze NCQ error for
  1232. *
  1233. * Read log page 10h, determine the offending qc and acquire
  1234. * error status TF. For NCQ device errors, all LLDDs have to do
  1235. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1236. * care of the rest.
  1237. *
  1238. * LOCKING:
  1239. * Kernel thread context (may sleep).
  1240. */
  1241. void ata_eh_analyze_ncq_error(struct ata_link *link)
  1242. {
  1243. struct ata_port *ap = link->ap;
  1244. struct ata_eh_context *ehc = &link->eh_context;
  1245. struct ata_device *dev = link->device;
  1246. struct ata_queued_cmd *qc;
  1247. struct ata_taskfile tf;
  1248. int tag, rc;
  1249. /* if frozen, we can't do much */
  1250. if (ap->pflags & ATA_PFLAG_FROZEN)
  1251. return;
  1252. /* is it NCQ device error? */
  1253. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1254. return;
  1255. /* has LLDD analyzed already? */
  1256. ata_qc_for_each_raw(ap, qc, tag) {
  1257. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1258. continue;
  1259. if (qc->err_mask)
  1260. return;
  1261. }
  1262. /* okay, this error is ours */
  1263. memset(&tf, 0, sizeof(tf));
  1264. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1265. if (rc) {
  1266. ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
  1267. rc);
  1268. return;
  1269. }
  1270. if (!(link->sactive & (1 << tag))) {
  1271. ata_link_err(link, "log page 10h reported inactive tag %d\n",
  1272. tag);
  1273. return;
  1274. }
  1275. /* we've got the perpetrator, condemn it */
  1276. qc = __ata_qc_from_tag(ap, tag);
  1277. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1278. qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1279. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1280. if (dev->class == ATA_DEV_ZAC &&
  1281. ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) {
  1282. char sense_key, asc, ascq;
  1283. sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
  1284. asc = (qc->result_tf.auxiliary >> 8) & 0xff;
  1285. ascq = qc->result_tf.auxiliary & 0xff;
  1286. ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
  1287. ata_scsi_set_sense_information(dev, qc->scsicmd,
  1288. &qc->result_tf);
  1289. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1290. }
  1291. ehc->i.err_mask &= ~AC_ERR_DEV;
  1292. }
  1293. EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);