libata-pmp.c 28 KB

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