libata-pmp.c 29 KB

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