libata-pmp.c 26 KB

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