libata-pmp.c 24 KB

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