libata-pmp.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * libata-pmp.c - libata port multiplier support
  3. *
  4. * Copyright (c) 2007 SUSE Linux Products GmbH
  5. * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
  6. *
  7. * This file is released under the GPLv2.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/libata.h>
  11. #include "libata.h"
  12. /**
  13. * sata_pmp_read - read PMP register
  14. * @link: link to read PMP register for
  15. * @reg: register to read
  16. * @r_val: resulting value
  17. *
  18. * Read PMP register.
  19. *
  20. * LOCKING:
  21. * Kernel thread context (may sleep).
  22. *
  23. * RETURNS:
  24. * 0 on success, AC_ERR_* mask on failure.
  25. */
  26. static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
  27. {
  28. struct ata_port *ap = link->ap;
  29. struct ata_device *pmp_dev = ap->link.device;
  30. struct ata_taskfile tf;
  31. unsigned int err_mask;
  32. ata_tf_init(pmp_dev, &tf);
  33. tf.command = ATA_CMD_PMP_READ;
  34. tf.protocol = ATA_PROT_NODATA;
  35. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  36. tf.feature = reg;
  37. tf.device = link->pmp;
  38. err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
  39. SATA_PMP_SCR_TIMEOUT);
  40. if (err_mask)
  41. return err_mask;
  42. *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
  43. return 0;
  44. }
  45. /**
  46. * sata_pmp_write - write PMP register
  47. * @link: link to write PMP register for
  48. * @reg: register to write
  49. * @r_val: value to write
  50. *
  51. * Write PMP register.
  52. *
  53. * LOCKING:
  54. * Kernel thread context (may sleep).
  55. *
  56. * RETURNS:
  57. * 0 on success, AC_ERR_* mask on failure.
  58. */
  59. static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
  60. {
  61. struct ata_port *ap = link->ap;
  62. struct ata_device *pmp_dev = ap->link.device;
  63. struct ata_taskfile tf;
  64. ata_tf_init(pmp_dev, &tf);
  65. tf.command = ATA_CMD_PMP_WRITE;
  66. tf.protocol = ATA_PROT_NODATA;
  67. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  68. tf.feature = reg;
  69. tf.device = link->pmp;
  70. tf.nsect = val & 0xff;
  71. tf.lbal = (val >> 8) & 0xff;
  72. tf.lbam = (val >> 16) & 0xff;
  73. tf.lbah = (val >> 24) & 0xff;
  74. return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
  75. SATA_PMP_SCR_TIMEOUT);
  76. }
  77. /**
  78. * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
  79. * @qc: ATA command in question
  80. *
  81. * A host which has command switching PMP support cannot issue
  82. * commands to multiple links simultaneously.
  83. *
  84. * LOCKING:
  85. * spin_lock_irqsave(host lock)
  86. *
  87. * RETURNS:
  88. * ATA_DEFER_* if deferring is needed, 0 otherwise.
  89. */
  90. int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
  91. {
  92. struct ata_link *link = qc->dev->link;
  93. struct ata_port *ap = link->ap;
  94. if (ap->excl_link == NULL || ap->excl_link == link) {
  95. if (ap->nr_active_links == 0 || ata_link_active(link)) {
  96. qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
  97. return ata_std_qc_defer(qc);
  98. }
  99. ap->excl_link = link;
  100. }
  101. return ATA_DEFER_PORT;
  102. }
  103. /**
  104. * sata_pmp_scr_read - read PSCR
  105. * @link: ATA link to read PSCR for
  106. * @reg: PSCR to read
  107. * @r_val: resulting value
  108. *
  109. * Read PSCR @reg into @r_val for @link, to be called from
  110. * ata_scr_read().
  111. *
  112. * LOCKING:
  113. * Kernel thread context (may sleep).
  114. *
  115. * RETURNS:
  116. * 0 on success, -errno on failure.
  117. */
  118. int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
  119. {
  120. unsigned int err_mask;
  121. if (reg > SATA_PMP_PSCR_CONTROL)
  122. return -EINVAL;
  123. err_mask = sata_pmp_read(link, reg, r_val);
  124. if (err_mask) {
  125. ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
  126. "(Emask=0x%x)\n", reg, err_mask);
  127. return -EIO;
  128. }
  129. return 0;
  130. }
  131. /**
  132. * sata_pmp_scr_write - write PSCR
  133. * @link: ATA link to write PSCR for
  134. * @reg: PSCR to write
  135. * @val: value to be written
  136. *
  137. * Write @val to PSCR @reg for @link, to be called from
  138. * ata_scr_write() and ata_scr_write_flush().
  139. *
  140. * LOCKING:
  141. * Kernel thread context (may sleep).
  142. *
  143. * RETURNS:
  144. * 0 on success, -errno on failure.
  145. */
  146. int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
  147. {
  148. unsigned int err_mask;
  149. if (reg > SATA_PMP_PSCR_CONTROL)
  150. return -EINVAL;
  151. err_mask = sata_pmp_write(link, reg, val);
  152. if (err_mask) {
  153. ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
  154. "(Emask=0x%x)\n", reg, err_mask);
  155. return -EIO;
  156. }
  157. return 0;
  158. }
  159. /**
  160. * sata_pmp_std_prereset - prepare PMP link for reset
  161. * @link: link to be reset
  162. * @deadline: deadline jiffies for the operation
  163. *
  164. * @link is about to be reset. Initialize it.
  165. *
  166. * LOCKING:
  167. * Kernel thread context (may sleep)
  168. *
  169. * RETURNS:
  170. * 0 on success, -errno otherwise.
  171. */
  172. int sata_pmp_std_prereset(struct ata_link *link, unsigned long deadline)
  173. {
  174. struct ata_eh_context *ehc = &link->eh_context;
  175. const unsigned long *timing = sata_ehc_deb_timing(ehc);
  176. int rc;
  177. /* force HRST? */
  178. if (link->flags & ATA_LFLAG_NO_SRST)
  179. ehc->i.action |= ATA_EH_HARDRESET;
  180. /* handle link resume */
  181. if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
  182. (link->flags & ATA_LFLAG_HRST_TO_RESUME))
  183. ehc->i.action |= ATA_EH_HARDRESET;
  184. /* if we're about to do hardreset, nothing more to do */
  185. if (ehc->i.action & ATA_EH_HARDRESET)
  186. return 0;
  187. /* resume link */
  188. rc = sata_link_resume(link, timing, deadline);
  189. if (rc) {
  190. /* phy resume failed */
  191. ata_link_printk(link, KERN_WARNING, "failed to resume link "
  192. "for reset (errno=%d)\n", rc);
  193. return rc;
  194. }
  195. /* clear SError bits including .X which blocks the port when set */
  196. rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
  197. if (rc) {
  198. ata_link_printk(link, KERN_ERR,
  199. "failed to clear SError (errno=%d)\n", rc);
  200. return rc;
  201. }
  202. return 0;
  203. }
  204. /**
  205. * sata_pmp_std_hardreset - standard hardreset method for PMP link
  206. * @link: link to be reset
  207. * @class: resulting class of attached device
  208. * @deadline: deadline jiffies for the operation
  209. *
  210. * Hardreset PMP port @link. Note that this function doesn't
  211. * wait for BSY clearance. There simply isn't a generic way to
  212. * wait the event. Instead, this function return -EAGAIN thus
  213. * telling libata-EH to followup with softreset.
  214. *
  215. * LOCKING:
  216. * Kernel thread context (may sleep)
  217. *
  218. * RETURNS:
  219. * 0 on success, -errno otherwise.
  220. */
  221. int sata_pmp_std_hardreset(struct ata_link *link, unsigned int *class,
  222. unsigned long deadline)
  223. {
  224. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  225. u32 tmp;
  226. int rc;
  227. DPRINTK("ENTER\n");
  228. /* do hardreset */
  229. rc = sata_link_hardreset(link, timing, deadline);
  230. if (rc) {
  231. ata_link_printk(link, KERN_ERR,
  232. "COMRESET failed (errno=%d)\n", rc);
  233. goto out;
  234. }
  235. /* clear SError bits including .X which blocks the port when set */
  236. rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
  237. if (rc) {
  238. ata_link_printk(link, KERN_ERR, "failed to clear SError "
  239. "during hardreset (errno=%d)\n", rc);
  240. goto out;
  241. }
  242. /* if device is present, follow up with srst to wait for !BSY */
  243. if (ata_link_online(link))
  244. rc = -EAGAIN;
  245. out:
  246. /* if SCR isn't accessible, we need to reset the PMP */
  247. if (rc && rc != -EAGAIN && sata_scr_read(link, SCR_STATUS, &tmp))
  248. rc = -ERESTART;
  249. DPRINTK("EXIT, rc=%d\n", rc);
  250. return rc;
  251. }
  252. /**
  253. * ata_std_postreset - standard postreset method for PMP link
  254. * @link: the target ata_link
  255. * @classes: classes of attached devices
  256. *
  257. * This function is invoked after a successful reset. Note that
  258. * the device might have been reset more than once using
  259. * different reset methods before postreset is invoked.
  260. *
  261. * LOCKING:
  262. * Kernel thread context (may sleep)
  263. */
  264. void sata_pmp_std_postreset(struct ata_link *link, unsigned int *class)
  265. {
  266. u32 serror;
  267. DPRINTK("ENTER\n");
  268. /* clear SError */
  269. if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
  270. sata_scr_write(link, SCR_ERROR, serror);
  271. /* print link status */
  272. sata_print_link_status(link);
  273. DPRINTK("EXIT\n");
  274. }
  275. /**
  276. * sata_pmp_read_gscr - read GSCR block of SATA PMP
  277. * @dev: PMP device
  278. * @gscr: buffer to read GSCR block into
  279. *
  280. * Read selected PMP GSCRs from the PMP at @dev. This will serve
  281. * as configuration and identification info for the PMP.
  282. *
  283. * LOCKING:
  284. * Kernel thread context (may sleep).
  285. *
  286. * RETURNS:
  287. * 0 on success, -errno on failure.
  288. */
  289. static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
  290. {
  291. static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
  292. int i;
  293. for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
  294. int reg = gscr_to_read[i];
  295. unsigned int err_mask;
  296. err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
  297. if (err_mask) {
  298. ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
  299. "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
  300. return -EIO;
  301. }
  302. }
  303. return 0;
  304. }
  305. static const char *sata_pmp_spec_rev_str(const u32 *gscr)
  306. {
  307. u32 rev = gscr[SATA_PMP_GSCR_REV];
  308. if (rev & (1 << 2))
  309. return "1.1";
  310. if (rev & (1 << 1))
  311. return "1.0";
  312. return "<unknown>";
  313. }
  314. static int sata_pmp_configure(struct ata_device *dev, int print_info)
  315. {
  316. struct ata_port *ap = dev->link->ap;
  317. u32 *gscr = dev->gscr;
  318. unsigned int err_mask = 0;
  319. const char *reason;
  320. int nr_ports, rc;
  321. nr_ports = sata_pmp_gscr_ports(gscr);
  322. if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
  323. rc = -EINVAL;
  324. reason = "invalid nr_ports";
  325. goto fail;
  326. }
  327. if ((ap->flags & ATA_FLAG_AN) &&
  328. (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
  329. dev->flags |= ATA_DFLAG_AN;
  330. /* monitor SERR_PHYRDY_CHG on fan-out ports */
  331. err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
  332. SERR_PHYRDY_CHG);
  333. if (err_mask) {
  334. rc = -EIO;
  335. reason = "failed to write GSCR_ERROR_EN";
  336. goto fail;
  337. }
  338. /* turn off notification till fan-out ports are reset and configured */
  339. if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
  340. gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
  341. err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
  342. gscr[SATA_PMP_GSCR_FEAT_EN]);
  343. if (err_mask) {
  344. rc = -EIO;
  345. reason = "failed to write GSCR_FEAT_EN";
  346. goto fail;
  347. }
  348. }
  349. if (print_info) {
  350. ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
  351. "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
  352. sata_pmp_spec_rev_str(gscr),
  353. sata_pmp_gscr_vendor(gscr),
  354. sata_pmp_gscr_devid(gscr),
  355. sata_pmp_gscr_rev(gscr),
  356. nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
  357. gscr[SATA_PMP_GSCR_FEAT]);
  358. if (!(dev->flags & ATA_DFLAG_AN))
  359. ata_dev_printk(dev, KERN_INFO,
  360. "Asynchronous notification not supported, "
  361. "hotplug won't\n work on fan-out "
  362. "ports. Use warm-plug instead.\n");
  363. }
  364. return 0;
  365. fail:
  366. ata_dev_printk(dev, KERN_ERR,
  367. "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
  368. reason, err_mask);
  369. return rc;
  370. }
  371. static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
  372. {
  373. struct ata_link *pmp_link = ap->pmp_link;
  374. int i;
  375. if (!pmp_link) {
  376. pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
  377. GFP_NOIO);
  378. if (!pmp_link)
  379. return -ENOMEM;
  380. for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
  381. ata_link_init(ap, &pmp_link[i], i);
  382. ap->pmp_link = pmp_link;
  383. }
  384. for (i = 0; i < nr_ports; i++) {
  385. struct ata_link *link = &pmp_link[i];
  386. struct ata_eh_context *ehc = &link->eh_context;
  387. link->flags = 0;
  388. ehc->i.probe_mask |= 1;
  389. ehc->i.action |= ATA_EH_SOFTRESET;
  390. ehc->i.flags |= ATA_EHI_RESUME_LINK;
  391. }
  392. return 0;
  393. }
  394. static void sata_pmp_quirks(struct ata_port *ap)
  395. {
  396. u32 *gscr = ap->link.device->gscr;
  397. u16 vendor = sata_pmp_gscr_vendor(gscr);
  398. u16 devid = sata_pmp_gscr_devid(gscr);
  399. struct ata_link *link;
  400. if (vendor == 0x1095 && devid == 0x3726) {
  401. /* sil3726 quirks */
  402. ata_port_for_each_link(link, ap) {
  403. /* SError.N need a kick in the ass to get working */
  404. link->flags |= ATA_LFLAG_HRST_TO_RESUME;
  405. /* class code report is unreliable */
  406. if (link->pmp < 5)
  407. link->flags |= ATA_LFLAG_ASSUME_ATA;
  408. /* port 5 is for SEMB device and it doesn't like SRST */
  409. if (link->pmp == 5)
  410. link->flags |= ATA_LFLAG_NO_SRST |
  411. ATA_LFLAG_ASSUME_SEMB;
  412. }
  413. } else if (vendor == 0x1095 && devid == 0x4723) {
  414. /* sil4723 quirks */
  415. ata_port_for_each_link(link, ap) {
  416. /* SError.N need a kick in the ass to get working */
  417. link->flags |= ATA_LFLAG_HRST_TO_RESUME;
  418. /* class code report is unreliable */
  419. if (link->pmp < 2)
  420. link->flags |= ATA_LFLAG_ASSUME_ATA;
  421. /* the config device at port 2 locks up on SRST */
  422. if (link->pmp == 2)
  423. link->flags |= ATA_LFLAG_NO_SRST |
  424. ATA_LFLAG_ASSUME_ATA;
  425. }
  426. } else if (vendor == 0x1095 && devid == 0x4726) {
  427. /* sil4726 quirks */
  428. ata_port_for_each_link(link, ap) {
  429. /* SError.N need a kick in the ass to get working */
  430. link->flags |= ATA_LFLAG_HRST_TO_RESUME;
  431. /* Class code report is unreliable and SRST
  432. * times out under certain configurations.
  433. * Config device can be at port 0 or 5 and
  434. * locks up on SRST.
  435. */
  436. if (link->pmp <= 5)
  437. link->flags |= ATA_LFLAG_NO_SRST |
  438. ATA_LFLAG_ASSUME_ATA;
  439. /* Port 6 is for SEMB device which doesn't
  440. * like SRST either.
  441. */
  442. if (link->pmp == 6)
  443. link->flags |= ATA_LFLAG_NO_SRST |
  444. ATA_LFLAG_ASSUME_SEMB;
  445. }
  446. } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
  447. devid == 0x5734 || devid == 0x5744)) {
  448. /* sil5723/5744 quirks */
  449. /* sil5723/5744 has either two or three downstream
  450. * ports depending on operation mode. The last port
  451. * is empty if any actual IO device is available or
  452. * occupied by a pseudo configuration device
  453. * otherwise. Don't try hard to recover it.
  454. */
  455. ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
  456. } else if (vendor == 0x11ab && devid == 0x4140) {
  457. /* Marvell 88SM4140 quirks. Fan-out ports require PHY
  458. * reset to work; other than that, it behaves very
  459. * nicely.
  460. */
  461. ata_port_for_each_link(link, ap)
  462. link->flags |= ATA_LFLAG_HRST_TO_RESUME;
  463. }
  464. }
  465. /**
  466. * sata_pmp_attach - attach a SATA PMP device
  467. * @dev: SATA PMP device to attach
  468. *
  469. * Configure and attach SATA PMP device @dev. This function is
  470. * also responsible for allocating and initializing PMP links.
  471. *
  472. * LOCKING:
  473. * Kernel thread context (may sleep).
  474. *
  475. * RETURNS:
  476. * 0 on success, -errno on failure.
  477. */
  478. int sata_pmp_attach(struct ata_device *dev)
  479. {
  480. struct ata_link *link = dev->link;
  481. struct ata_port *ap = link->ap;
  482. unsigned long flags;
  483. struct ata_link *tlink;
  484. int rc;
  485. /* is it hanging off the right place? */
  486. if (!(ap->flags & ATA_FLAG_PMP)) {
  487. ata_dev_printk(dev, KERN_ERR,
  488. "host does not support Port Multiplier\n");
  489. return -EINVAL;
  490. }
  491. if (!ata_is_host_link(link)) {
  492. ata_dev_printk(dev, KERN_ERR,
  493. "Port Multipliers cannot be nested\n");
  494. return -EINVAL;
  495. }
  496. if (dev->devno) {
  497. ata_dev_printk(dev, KERN_ERR,
  498. "Port Multiplier must be the first device\n");
  499. return -EINVAL;
  500. }
  501. WARN_ON(link->pmp != 0);
  502. link->pmp = SATA_PMP_CTRL_PORT;
  503. /* read GSCR block */
  504. rc = sata_pmp_read_gscr(dev, dev->gscr);
  505. if (rc)
  506. goto fail;
  507. /* config PMP */
  508. rc = sata_pmp_configure(dev, 1);
  509. if (rc)
  510. goto fail;
  511. rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
  512. if (rc) {
  513. ata_dev_printk(dev, KERN_INFO,
  514. "failed to initialize PMP links\n");
  515. goto fail;
  516. }
  517. /* attach it */
  518. spin_lock_irqsave(ap->lock, flags);
  519. WARN_ON(ap->nr_pmp_links);
  520. ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
  521. spin_unlock_irqrestore(ap->lock, flags);
  522. sata_pmp_quirks(ap);
  523. if (ap->ops->pmp_attach)
  524. ap->ops->pmp_attach(ap);
  525. ata_port_for_each_link(tlink, ap)
  526. sata_link_init_spd(tlink);
  527. ata_acpi_associate_sata_port(ap);
  528. return 0;
  529. fail:
  530. link->pmp = 0;
  531. return rc;
  532. }
  533. /**
  534. * sata_pmp_detach - detach a SATA PMP device
  535. * @dev: SATA PMP device to detach
  536. *
  537. * Detach SATA PMP device @dev. This function is also
  538. * responsible for deconfiguring PMP links.
  539. *
  540. * LOCKING:
  541. * Kernel thread context (may sleep).
  542. */
  543. static void sata_pmp_detach(struct ata_device *dev)
  544. {
  545. struct ata_link *link = dev->link;
  546. struct ata_port *ap = link->ap;
  547. struct ata_link *tlink;
  548. unsigned long flags;
  549. ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
  550. WARN_ON(!ata_is_host_link(link) || dev->devno ||
  551. link->pmp != SATA_PMP_CTRL_PORT);
  552. if (ap->ops->pmp_detach)
  553. ap->ops->pmp_detach(ap);
  554. ata_port_for_each_link(tlink, ap)
  555. ata_eh_detach_dev(tlink->device);
  556. spin_lock_irqsave(ap->lock, flags);
  557. ap->nr_pmp_links = 0;
  558. link->pmp = 0;
  559. spin_unlock_irqrestore(ap->lock, flags);
  560. ata_acpi_associate_sata_port(ap);
  561. }
  562. /**
  563. * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
  564. * @dev: PMP device to compare against
  565. * @new_gscr: GSCR block of the new device
  566. *
  567. * Compare @new_gscr against @dev and determine whether @dev is
  568. * the PMP described by @new_gscr.
  569. *
  570. * LOCKING:
  571. * None.
  572. *
  573. * RETURNS:
  574. * 1 if @dev matches @new_gscr, 0 otherwise.
  575. */
  576. static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
  577. {
  578. const u32 *old_gscr = dev->gscr;
  579. u16 old_vendor, new_vendor, old_devid, new_devid;
  580. int old_nr_ports, new_nr_ports;
  581. old_vendor = sata_pmp_gscr_vendor(old_gscr);
  582. new_vendor = sata_pmp_gscr_vendor(new_gscr);
  583. old_devid = sata_pmp_gscr_devid(old_gscr);
  584. new_devid = sata_pmp_gscr_devid(new_gscr);
  585. old_nr_ports = sata_pmp_gscr_ports(old_gscr);
  586. new_nr_ports = sata_pmp_gscr_ports(new_gscr);
  587. if (old_vendor != new_vendor) {
  588. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  589. "vendor mismatch '0x%x' != '0x%x'\n",
  590. old_vendor, new_vendor);
  591. return 0;
  592. }
  593. if (old_devid != new_devid) {
  594. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  595. "device ID mismatch '0x%x' != '0x%x'\n",
  596. old_devid, new_devid);
  597. return 0;
  598. }
  599. if (old_nr_ports != new_nr_ports) {
  600. ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
  601. "nr_ports mismatch '0x%x' != '0x%x'\n",
  602. old_nr_ports, new_nr_ports);
  603. return 0;
  604. }
  605. return 1;
  606. }
  607. /**
  608. * sata_pmp_revalidate - revalidate SATA PMP
  609. * @dev: PMP device to revalidate
  610. * @new_class: new class code
  611. *
  612. * Re-read GSCR block and make sure @dev is still attached to the
  613. * port and properly configured.
  614. *
  615. * LOCKING:
  616. * Kernel thread context (may sleep).
  617. *
  618. * RETURNS:
  619. * 0 on success, -errno otherwise.
  620. */
  621. static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
  622. {
  623. struct ata_link *link = dev->link;
  624. struct ata_port *ap = link->ap;
  625. u32 *gscr = (void *)ap->sector_buf;
  626. int rc;
  627. DPRINTK("ENTER\n");
  628. ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
  629. if (!ata_dev_enabled(dev)) {
  630. rc = -ENODEV;
  631. goto fail;
  632. }
  633. /* wrong class? */
  634. if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
  635. rc = -ENODEV;
  636. goto fail;
  637. }
  638. /* read GSCR */
  639. rc = sata_pmp_read_gscr(dev, gscr);
  640. if (rc)
  641. goto fail;
  642. /* is the pmp still there? */
  643. if (!sata_pmp_same_pmp(dev, gscr)) {
  644. rc = -ENODEV;
  645. goto fail;
  646. }
  647. memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
  648. rc = sata_pmp_configure(dev, 0);
  649. if (rc)
  650. goto fail;
  651. ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
  652. DPRINTK("EXIT, rc=0\n");
  653. return 0;
  654. fail:
  655. ata_dev_printk(dev, KERN_ERR,
  656. "PMP revalidation failed (errno=%d)\n", rc);
  657. DPRINTK("EXIT, rc=%d\n", rc);
  658. return rc;
  659. }
  660. /**
  661. * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
  662. * @dev: PMP device to revalidate
  663. *
  664. * Make sure the attached PMP is accessible.
  665. *
  666. * LOCKING:
  667. * Kernel thread context (may sleep).
  668. *
  669. * RETURNS:
  670. * 0 on success, -errno otherwise.
  671. */
  672. static int sata_pmp_revalidate_quick(struct ata_device *dev)
  673. {
  674. unsigned int err_mask;
  675. u32 prod_id;
  676. err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
  677. if (err_mask) {
  678. ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
  679. "(Emask=0x%x)\n", err_mask);
  680. return -EIO;
  681. }
  682. if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
  683. ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
  684. /* something weird is going on, request full PMP recovery */
  685. return -EIO;
  686. }
  687. return 0;
  688. }
  689. /**
  690. * sata_pmp_eh_recover_pmp - recover PMP
  691. * @ap: ATA port PMP is attached to
  692. * @prereset: prereset method (can be NULL)
  693. * @softreset: softreset method
  694. * @hardreset: hardreset method
  695. * @postreset: postreset method (can be NULL)
  696. *
  697. * Recover PMP attached to @ap. Recovery procedure is somewhat
  698. * similar to that of ata_eh_recover() except that reset should
  699. * always be performed in hard->soft sequence and recovery
  700. * failure results in PMP detachment.
  701. *
  702. * LOCKING:
  703. * Kernel thread context (may sleep).
  704. *
  705. * RETURNS:
  706. * 0 on success, -errno on failure.
  707. */
  708. static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
  709. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  710. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  711. {
  712. struct ata_link *link = &ap->link;
  713. struct ata_eh_context *ehc = &link->eh_context;
  714. struct ata_device *dev = link->device;
  715. int tries = ATA_EH_PMP_TRIES;
  716. int detach = 0, rc = 0;
  717. int reval_failed = 0;
  718. DPRINTK("ENTER\n");
  719. if (dev->flags & ATA_DFLAG_DETACH) {
  720. detach = 1;
  721. goto fail;
  722. }
  723. retry:
  724. ehc->classes[0] = ATA_DEV_UNKNOWN;
  725. if (ehc->i.action & ATA_EH_RESET_MASK) {
  726. struct ata_link *tlink;
  727. ata_eh_freeze_port(ap);
  728. /* reset */
  729. ehc->i.action = ATA_EH_HARDRESET;
  730. rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
  731. postreset);
  732. if (rc) {
  733. ata_link_printk(link, KERN_ERR,
  734. "failed to reset PMP, giving up\n");
  735. goto fail;
  736. }
  737. ata_eh_thaw_port(ap);
  738. /* PMP is reset, SErrors cannot be trusted, scan all */
  739. ata_port_for_each_link(tlink, ap)
  740. ata_ehi_schedule_probe(&tlink->eh_context.i);
  741. }
  742. /* If revalidation is requested, revalidate and reconfigure;
  743. * otherwise, do quick revalidation.
  744. */
  745. if (ehc->i.action & ATA_EH_REVALIDATE)
  746. rc = sata_pmp_revalidate(dev, ehc->classes[0]);
  747. else
  748. rc = sata_pmp_revalidate_quick(dev);
  749. if (rc) {
  750. tries--;
  751. if (rc == -ENODEV) {
  752. ehc->i.probe_mask |= 1;
  753. detach = 1;
  754. /* give it just two more chances */
  755. tries = min(tries, 2);
  756. }
  757. if (tries) {
  758. int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
  759. /* consecutive revalidation failures? speed down */
  760. if (reval_failed)
  761. sata_down_spd_limit(link);
  762. else
  763. reval_failed = 1;
  764. ata_dev_printk(dev, KERN_WARNING,
  765. "retrying hardreset%s\n",
  766. sleep ? " in 5 secs" : "");
  767. if (sleep)
  768. ssleep(5);
  769. ehc->i.action |= ATA_EH_HARDRESET;
  770. goto retry;
  771. } else {
  772. ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
  773. "after %d tries, giving up\n",
  774. ATA_EH_PMP_TRIES);
  775. goto fail;
  776. }
  777. }
  778. /* okay, PMP resurrected */
  779. ehc->i.flags = 0;
  780. DPRINTK("EXIT, rc=0\n");
  781. return 0;
  782. fail:
  783. sata_pmp_detach(dev);
  784. if (detach)
  785. ata_eh_detach_dev(dev);
  786. else
  787. ata_dev_disable(dev);
  788. DPRINTK("EXIT, rc=%d\n", rc);
  789. return rc;
  790. }
  791. static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
  792. {
  793. struct ata_link *link;
  794. unsigned long flags;
  795. int rc;
  796. spin_lock_irqsave(ap->lock, flags);
  797. ata_port_for_each_link(link, ap) {
  798. if (!(link->flags & ATA_LFLAG_DISABLED))
  799. continue;
  800. spin_unlock_irqrestore(ap->lock, flags);
  801. /* Some PMPs require hardreset sequence to get
  802. * SError.N working.
  803. */
  804. if ((link->flags & ATA_LFLAG_HRST_TO_RESUME) &&
  805. (link->eh_context.i.flags & ATA_EHI_RESUME_LINK))
  806. sata_link_hardreset(link, sata_deb_timing_normal,
  807. jiffies + ATA_TMOUT_INTERNAL_QUICK);
  808. /* unconditionally clear SError.N */
  809. rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
  810. if (rc) {
  811. ata_link_printk(link, KERN_ERR, "failed to clear "
  812. "SError.N (errno=%d)\n", rc);
  813. return rc;
  814. }
  815. spin_lock_irqsave(ap->lock, flags);
  816. }
  817. spin_unlock_irqrestore(ap->lock, flags);
  818. return 0;
  819. }
  820. static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
  821. {
  822. struct ata_port *ap = link->ap;
  823. unsigned long flags;
  824. if (link_tries[link->pmp] && --link_tries[link->pmp])
  825. return 1;
  826. /* disable this link */
  827. if (!(link->flags & ATA_LFLAG_DISABLED)) {
  828. ata_link_printk(link, KERN_WARNING,
  829. "failed to recover link after %d tries, disabling\n",
  830. ATA_EH_PMP_LINK_TRIES);
  831. spin_lock_irqsave(ap->lock, flags);
  832. link->flags |= ATA_LFLAG_DISABLED;
  833. spin_unlock_irqrestore(ap->lock, flags);
  834. }
  835. ata_dev_disable(link->device);
  836. link->eh_context.i.action = 0;
  837. return 0;
  838. }
  839. /**
  840. * sata_pmp_eh_recover - recover PMP-enabled port
  841. * @ap: ATA port to recover
  842. * @prereset: prereset method (can be NULL)
  843. * @softreset: softreset method
  844. * @hardreset: hardreset method
  845. * @postreset: postreset method (can be NULL)
  846. * @pmp_prereset: PMP prereset method (can be NULL)
  847. * @pmp_softreset: PMP softreset method (can be NULL)
  848. * @pmp_hardreset: PMP hardreset method (can be NULL)
  849. * @pmp_postreset: PMP postreset method (can be NULL)
  850. *
  851. * Drive EH recovery operation for PMP enabled port @ap. This
  852. * function recovers host and PMP ports with proper retrials and
  853. * fallbacks. Actual recovery operations are performed using
  854. * ata_eh_recover() and sata_pmp_eh_recover_pmp().
  855. *
  856. * LOCKING:
  857. * Kernel thread context (may sleep).
  858. *
  859. * RETURNS:
  860. * 0 on success, -errno on failure.
  861. */
  862. static int sata_pmp_eh_recover(struct ata_port *ap,
  863. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  864. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
  865. ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
  866. ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
  867. {
  868. int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
  869. struct ata_link *pmp_link = &ap->link;
  870. struct ata_device *pmp_dev = pmp_link->device;
  871. struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
  872. struct ata_link *link;
  873. struct ata_device *dev;
  874. unsigned int err_mask;
  875. u32 gscr_error, sntf;
  876. int cnt, rc;
  877. pmp_tries = ATA_EH_PMP_TRIES;
  878. ata_port_for_each_link(link, ap)
  879. link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
  880. retry:
  881. /* PMP attached? */
  882. if (!ap->nr_pmp_links) {
  883. rc = ata_eh_recover(ap, prereset, softreset, hardreset,
  884. postreset, NULL);
  885. if (rc) {
  886. ata_link_for_each_dev(dev, &ap->link)
  887. ata_dev_disable(dev);
  888. return rc;
  889. }
  890. if (pmp_dev->class != ATA_DEV_PMP)
  891. return 0;
  892. /* new PMP online */
  893. ata_port_for_each_link(link, ap)
  894. link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
  895. /* fall through */
  896. }
  897. /* recover pmp */
  898. rc = sata_pmp_eh_recover_pmp(ap, prereset, softreset, hardreset,
  899. postreset);
  900. if (rc)
  901. goto pmp_fail;
  902. /* handle disabled links */
  903. rc = sata_pmp_eh_handle_disabled_links(ap);
  904. if (rc)
  905. goto pmp_fail;
  906. /* recover links */
  907. rc = ata_eh_recover(ap, pmp_prereset, pmp_softreset, pmp_hardreset,
  908. pmp_postreset, &link);
  909. if (rc)
  910. goto link_fail;
  911. /* Connection status might have changed while resetting other
  912. * links, check SATA_PMP_GSCR_ERROR before returning.
  913. */
  914. /* clear SNotification */
  915. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  916. if (rc == 0)
  917. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  918. /* enable notification */
  919. if (pmp_dev->flags & ATA_DFLAG_AN) {
  920. pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
  921. err_mask = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
  922. pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
  923. if (err_mask) {
  924. ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
  925. "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
  926. rc = -EIO;
  927. goto pmp_fail;
  928. }
  929. }
  930. /* check GSCR_ERROR */
  931. err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
  932. if (err_mask) {
  933. ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
  934. "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
  935. rc = -EIO;
  936. goto pmp_fail;
  937. }
  938. cnt = 0;
  939. ata_port_for_each_link(link, ap) {
  940. if (!(gscr_error & (1 << link->pmp)))
  941. continue;
  942. if (sata_pmp_handle_link_fail(link, link_tries)) {
  943. ata_ehi_hotplugged(&link->eh_context.i);
  944. cnt++;
  945. } else {
  946. ata_link_printk(link, KERN_WARNING,
  947. "PHY status changed but maxed out on retries, "
  948. "giving up\n");
  949. ata_link_printk(link, KERN_WARNING,
  950. "Manully issue scan to resume this link\n");
  951. }
  952. }
  953. if (cnt) {
  954. ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
  955. "ports, repeating recovery\n");
  956. goto retry;
  957. }
  958. return 0;
  959. link_fail:
  960. if (sata_pmp_handle_link_fail(link, link_tries)) {
  961. pmp_ehc->i.action |= ATA_EH_HARDRESET;
  962. goto retry;
  963. }
  964. /* fall through */
  965. pmp_fail:
  966. /* Control always ends up here after detaching PMP. Shut up
  967. * and return if we're unloading.
  968. */
  969. if (ap->pflags & ATA_PFLAG_UNLOADING)
  970. return rc;
  971. if (!ap->nr_pmp_links)
  972. goto retry;
  973. if (--pmp_tries) {
  974. ata_port_printk(ap, KERN_WARNING,
  975. "failed to recover PMP, retrying in 5 secs\n");
  976. pmp_ehc->i.action |= ATA_EH_HARDRESET;
  977. ssleep(5);
  978. goto retry;
  979. }
  980. ata_port_printk(ap, KERN_ERR,
  981. "failed to recover PMP after %d tries, giving up\n",
  982. ATA_EH_PMP_TRIES);
  983. sata_pmp_detach(pmp_dev);
  984. ata_dev_disable(pmp_dev);
  985. return rc;
  986. }
  987. /**
  988. * sata_pmp_do_eh - do standard error handling for PMP-enabled host
  989. * @ap: host port to handle error for
  990. * @prereset: prereset method (can be NULL)
  991. * @softreset: softreset method
  992. * @hardreset: hardreset method
  993. * @postreset: postreset method (can be NULL)
  994. * @pmp_prereset: PMP prereset method (can be NULL)
  995. * @pmp_softreset: PMP softreset method (can be NULL)
  996. * @pmp_hardreset: PMP hardreset method (can be NULL)
  997. * @pmp_postreset: PMP postreset method (can be NULL)
  998. *
  999. * Perform standard error handling sequence for PMP-enabled host
  1000. * @ap.
  1001. *
  1002. * LOCKING:
  1003. * Kernel thread context (may sleep).
  1004. */
  1005. void sata_pmp_do_eh(struct ata_port *ap,
  1006. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1007. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
  1008. ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
  1009. ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
  1010. {
  1011. ata_eh_autopsy(ap);
  1012. ata_eh_report(ap);
  1013. sata_pmp_eh_recover(ap, prereset, softreset, hardreset, postreset,
  1014. pmp_prereset, pmp_softreset, pmp_hardreset,
  1015. pmp_postreset);
  1016. ata_eh_finish(ap);
  1017. }