libata-sata.c 45 KB

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