libata-pmp.c 26 KB

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