blob: 7daf4c0f62167af1735c7f5885903774312cb8ba [file] [log] [blame]
Tejun Heo3af9a772007-09-23 13:19:54 +09001/*
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
10#include <linux/kernel.h>
11#include <linux/libata.h>
12#include "libata.h"
13
Tejun Heo48515f62008-04-07 22:47:21 +090014const struct ata_port_operations sata_pmp_port_ops = {
15 .inherits = &sata_port_ops,
16 .pmp_prereset = ata_std_prereset,
17 .pmp_hardreset = sata_std_hardreset,
18 .pmp_postreset = ata_std_postreset,
19 .error_handler = sata_pmp_error_handler,
20};
21
Tejun Heo3af9a772007-09-23 13:19:54 +090022/**
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 *
Tejun Heob06ce3e2007-10-09 15:06:48 +090028 * Read PMP register.
Tejun Heo3af9a772007-09-23 13:19:54 +090029 *
30 * LOCKING:
31 * Kernel thread context (may sleep).
32 *
33 * RETURNS:
Tejun Heob06ce3e2007-10-09 15:06:48 +090034 * 0 on success, AC_ERR_* mask on failure.
Tejun Heo3af9a772007-09-23 13:19:54 +090035 */
Tejun Heob06ce3e2007-10-09 15:06:48 +090036static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
Tejun Heo3af9a772007-09-23 13:19:54 +090037{
38 struct ata_port *ap = link->ap;
39 struct ata_device *pmp_dev = ap->link.device;
Tejun Heob06ce3e2007-10-09 15:06:48 +090040 struct ata_taskfile tf;
41 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +090042
Tejun Heob06ce3e2007-10-09 15:06:48 +090043 ata_tf_init(pmp_dev, &tf);
44 tf.command = ATA_CMD_PMP_READ;
45 tf.protocol = ATA_PROT_NODATA;
Mark Lord39f25e72008-02-21 15:52:25 -050046 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
Tejun Heob06ce3e2007-10-09 15:06:48 +090047 tf.feature = reg;
48 tf.device = link->pmp;
Tejun Heo3af9a772007-09-23 13:19:54 +090049
Tejun Heob06ce3e2007-10-09 15:06:48 +090050 err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
Tejun Heobf1bff62008-05-19 01:15:10 +090051 SATA_PMP_RW_TIMEOUT);
Tejun Heob06ce3e2007-10-09 15:06:48 +090052 if (err_mask)
53 return err_mask;
54
55 *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
56 return 0;
Tejun Heo3af9a772007-09-23 13:19:54 +090057}
58
59/**
60 * sata_pmp_write - write PMP register
61 * @link: link to write PMP register for
62 * @reg: register to write
63 * @r_val: value to write
64 *
Tejun Heob06ce3e2007-10-09 15:06:48 +090065 * Write PMP register.
Tejun Heo3af9a772007-09-23 13:19:54 +090066 *
67 * LOCKING:
68 * Kernel thread context (may sleep).
69 *
70 * RETURNS:
Tejun Heob06ce3e2007-10-09 15:06:48 +090071 * 0 on success, AC_ERR_* mask on failure.
Tejun Heo3af9a772007-09-23 13:19:54 +090072 */
Tejun Heob06ce3e2007-10-09 15:06:48 +090073static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
Tejun Heo3af9a772007-09-23 13:19:54 +090074{
75 struct ata_port *ap = link->ap;
76 struct ata_device *pmp_dev = ap->link.device;
Tejun Heob06ce3e2007-10-09 15:06:48 +090077 struct ata_taskfile tf;
Tejun Heo3af9a772007-09-23 13:19:54 +090078
Tejun Heob06ce3e2007-10-09 15:06:48 +090079 ata_tf_init(pmp_dev, &tf);
80 tf.command = ATA_CMD_PMP_WRITE;
81 tf.protocol = ATA_PROT_NODATA;
Mark Lord39f25e72008-02-21 15:52:25 -050082 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
Tejun Heob06ce3e2007-10-09 15:06:48 +090083 tf.feature = reg;
84 tf.device = link->pmp;
85 tf.nsect = val & 0xff;
86 tf.lbal = (val >> 8) & 0xff;
87 tf.lbam = (val >> 16) & 0xff;
88 tf.lbah = (val >> 24) & 0xff;
Tejun Heo3af9a772007-09-23 13:19:54 +090089
Tejun Heob06ce3e2007-10-09 15:06:48 +090090 return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
Tejun Heobf1bff62008-05-19 01:15:10 +090091 SATA_PMP_RW_TIMEOUT);
Tejun Heo3af9a772007-09-23 13:19:54 +090092}
93
94/**
Tejun Heo31f88382007-09-23 13:19:54 +090095 * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
96 * @qc: ATA command in question
97 *
98 * A host which has command switching PMP support cannot issue
99 * commands to multiple links simultaneously.
100 *
101 * LOCKING:
102 * spin_lock_irqsave(host lock)
103 *
104 * RETURNS:
105 * ATA_DEFER_* if deferring is needed, 0 otherwise.
106 */
107int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
108{
109 struct ata_link *link = qc->dev->link;
110 struct ata_port *ap = link->ap;
111
112 if (ap->excl_link == NULL || ap->excl_link == link) {
113 if (ap->nr_active_links == 0 || ata_link_active(link)) {
114 qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
115 return ata_std_qc_defer(qc);
116 }
117
118 ap->excl_link = link;
119 }
120
121 return ATA_DEFER_PORT;
122}
123
124/**
Tejun Heo3af9a772007-09-23 13:19:54 +0900125 * sata_pmp_scr_read - read PSCR
126 * @link: ATA link to read PSCR for
127 * @reg: PSCR to read
128 * @r_val: resulting value
129 *
130 * Read PSCR @reg into @r_val for @link, to be called from
131 * ata_scr_read().
132 *
133 * LOCKING:
134 * Kernel thread context (may sleep).
135 *
136 * RETURNS:
137 * 0 on success, -errno on failure.
138 */
139int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
140{
Tejun Heob06ce3e2007-10-09 15:06:48 +0900141 unsigned int err_mask;
142
Tejun Heo3af9a772007-09-23 13:19:54 +0900143 if (reg > SATA_PMP_PSCR_CONTROL)
144 return -EINVAL;
145
Tejun Heob06ce3e2007-10-09 15:06:48 +0900146 err_mask = sata_pmp_read(link, reg, r_val);
147 if (err_mask) {
148 ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
149 "(Emask=0x%x)\n", reg, err_mask);
150 return -EIO;
151 }
152 return 0;
Tejun Heo3af9a772007-09-23 13:19:54 +0900153}
154
155/**
156 * sata_pmp_scr_write - write PSCR
157 * @link: ATA link to write PSCR for
158 * @reg: PSCR to write
159 * @val: value to be written
160 *
161 * Write @val to PSCR @reg for @link, to be called from
162 * ata_scr_write() and ata_scr_write_flush().
163 *
164 * LOCKING:
165 * Kernel thread context (may sleep).
166 *
167 * RETURNS:
168 * 0 on success, -errno on failure.
169 */
170int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
171{
Tejun Heob06ce3e2007-10-09 15:06:48 +0900172 unsigned int err_mask;
173
Tejun Heo3af9a772007-09-23 13:19:54 +0900174 if (reg > SATA_PMP_PSCR_CONTROL)
175 return -EINVAL;
176
Tejun Heob06ce3e2007-10-09 15:06:48 +0900177 err_mask = sata_pmp_write(link, reg, val);
178 if (err_mask) {
179 ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
180 "(Emask=0x%x)\n", reg, err_mask);
181 return -EIO;
182 }
183 return 0;
Tejun Heo3af9a772007-09-23 13:19:54 +0900184}
185
186/**
Tejun Heo3af9a772007-09-23 13:19:54 +0900187 * sata_pmp_read_gscr - read GSCR block of SATA PMP
188 * @dev: PMP device
189 * @gscr: buffer to read GSCR block into
190 *
191 * Read selected PMP GSCRs from the PMP at @dev. This will serve
192 * as configuration and identification info for the PMP.
193 *
194 * LOCKING:
195 * Kernel thread context (may sleep).
196 *
197 * RETURNS:
198 * 0 on success, -errno on failure.
199 */
200static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
201{
202 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
Tejun Heob06ce3e2007-10-09 15:06:48 +0900203 int i;
Tejun Heo3af9a772007-09-23 13:19:54 +0900204
205 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
206 int reg = gscr_to_read[i];
Tejun Heob06ce3e2007-10-09 15:06:48 +0900207 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900208
Tejun Heob06ce3e2007-10-09 15:06:48 +0900209 err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
210 if (err_mask) {
211 ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
212 "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
213 return -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900214 }
215 }
216
217 return 0;
218}
219
220static const char *sata_pmp_spec_rev_str(const u32 *gscr)
221{
222 u32 rev = gscr[SATA_PMP_GSCR_REV];
223
224 if (rev & (1 << 2))
225 return "1.1";
226 if (rev & (1 << 1))
227 return "1.0";
228 return "<unknown>";
229}
230
231static 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;
Tejun Heob06ce3e2007-10-09 15:06:48 +0900235 unsigned int err_mask = 0;
Tejun Heo3af9a772007-09-23 13:19:54 +0900236 const char *reason;
237 int nr_ports, rc;
238
239 nr_ports = sata_pmp_gscr_ports(gscr);
240
241 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
242 rc = -EINVAL;
243 reason = "invalid nr_ports";
244 goto fail;
245 }
246
247 if ((ap->flags & ATA_FLAG_AN) &&
248 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
249 dev->flags |= ATA_DFLAG_AN;
250
251 /* monitor SERR_PHYRDY_CHG on fan-out ports */
Tejun Heob06ce3e2007-10-09 15:06:48 +0900252 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
253 SERR_PHYRDY_CHG);
254 if (err_mask) {
255 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900256 reason = "failed to write GSCR_ERROR_EN";
257 goto fail;
258 }
259
Tejun Heo3af9a772007-09-23 13:19:54 +0900260 if (print_info) {
261 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
262 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
263 sata_pmp_spec_rev_str(gscr),
264 sata_pmp_gscr_vendor(gscr),
265 sata_pmp_gscr_devid(gscr),
266 sata_pmp_gscr_rev(gscr),
267 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
268 gscr[SATA_PMP_GSCR_FEAT]);
269
270 if (!(dev->flags & ATA_DFLAG_AN))
271 ata_dev_printk(dev, KERN_INFO,
272 "Asynchronous notification not supported, "
273 "hotplug won't\n work on fan-out "
274 "ports. Use warm-plug instead.\n");
275 }
276
277 return 0;
278
279 fail:
280 ata_dev_printk(dev, KERN_ERR,
Tejun Heob06ce3e2007-10-09 15:06:48 +0900281 "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
282 reason, err_mask);
Tejun Heo3af9a772007-09-23 13:19:54 +0900283 return rc;
284}
285
286static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
287{
288 struct ata_link *pmp_link = ap->pmp_link;
289 int i;
290
291 if (!pmp_link) {
292 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
293 GFP_NOIO);
294 if (!pmp_link)
295 return -ENOMEM;
296
297 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
298 ata_link_init(ap, &pmp_link[i], i);
299
300 ap->pmp_link = pmp_link;
301 }
302
303 for (i = 0; i < nr_ports; i++) {
304 struct ata_link *link = &pmp_link[i];
305 struct ata_eh_context *ehc = &link->eh_context;
306
307 link->flags = 0;
Tejun Heob558edd2008-01-24 00:05:14 +0900308 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +0900309 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900310 }
311
312 return 0;
313}
314
315static void sata_pmp_quirks(struct ata_port *ap)
316{
317 u32 *gscr = ap->link.device->gscr;
318 u16 vendor = sata_pmp_gscr_vendor(gscr);
319 u16 devid = sata_pmp_gscr_devid(gscr);
320 struct ata_link *link;
321
322 if (vendor == 0x1095 && devid == 0x3726) {
323 /* sil3726 quirks */
324 ata_port_for_each_link(link, ap) {
Tejun Heo19ef9d52008-05-21 14:11:24 +0900325 /* Class code report is unreliable and SRST
326 * times out under certain configurations.
327 */
Tejun Heo3af9a772007-09-23 13:19:54 +0900328 if (link->pmp < 5)
Tejun Heo19ef9d52008-05-21 14:11:24 +0900329 link->flags |= ATA_LFLAG_NO_SRST |
330 ATA_LFLAG_ASSUME_ATA;
Tejun Heo3af9a772007-09-23 13:19:54 +0900331
332 /* port 5 is for SEMB device and it doesn't like SRST */
333 if (link->pmp == 5)
334 link->flags |= ATA_LFLAG_NO_SRST |
335 ATA_LFLAG_ASSUME_SEMB;
336 }
337 } else if (vendor == 0x1095 && devid == 0x4723) {
338 /* sil4723 quirks */
339 ata_port_for_each_link(link, ap) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900340 /* class code report is unreliable */
341 if (link->pmp < 2)
342 link->flags |= ATA_LFLAG_ASSUME_ATA;
343
344 /* the config device at port 2 locks up on SRST */
345 if (link->pmp == 2)
346 link->flags |= ATA_LFLAG_NO_SRST |
347 ATA_LFLAG_ASSUME_ATA;
348 }
349 } else if (vendor == 0x1095 && devid == 0x4726) {
350 /* sil4726 quirks */
351 ata_port_for_each_link(link, ap) {
Tejun Heo80483072008-01-10 13:38:46 +0900352 /* Class code report is unreliable and SRST
353 * times out under certain configurations.
354 * Config device can be at port 0 or 5 and
355 * locks up on SRST.
Tejun Heo3af9a772007-09-23 13:19:54 +0900356 */
Tejun Heo80483072008-01-10 13:38:46 +0900357 if (link->pmp <= 5)
Tejun Heo3af9a772007-09-23 13:19:54 +0900358 link->flags |= ATA_LFLAG_NO_SRST |
359 ATA_LFLAG_ASSUME_ATA;
360
361 /* Port 6 is for SEMB device which doesn't
362 * like SRST either.
363 */
364 if (link->pmp == 6)
365 link->flags |= ATA_LFLAG_NO_SRST |
366 ATA_LFLAG_ASSUME_SEMB;
367 }
368 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
369 devid == 0x5734 || devid == 0x5744)) {
370 /* sil5723/5744 quirks */
371
372 /* sil5723/5744 has either two or three downstream
373 * ports depending on operation mode. The last port
374 * is empty if any actual IO device is available or
375 * occupied by a pseudo configuration device
376 * otherwise. Don't try hard to recover it.
377 */
378 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
Tejun Heo3af9a772007-09-23 13:19:54 +0900379 }
380}
381
382/**
383 * sata_pmp_attach - attach a SATA PMP device
384 * @dev: SATA PMP device to attach
385 *
386 * Configure and attach SATA PMP device @dev. This function is
387 * also responsible for allocating and initializing PMP links.
388 *
389 * LOCKING:
390 * Kernel thread context (may sleep).
391 *
392 * RETURNS:
393 * 0 on success, -errno on failure.
394 */
395int sata_pmp_attach(struct ata_device *dev)
396{
397 struct ata_link *link = dev->link;
398 struct ata_port *ap = link->ap;
399 unsigned long flags;
400 struct ata_link *tlink;
401 int rc;
402
403 /* is it hanging off the right place? */
Tejun Heo071f44b2008-04-07 22:47:22 +0900404 if (!sata_pmp_supported(ap)) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900405 ata_dev_printk(dev, KERN_ERR,
406 "host does not support Port Multiplier\n");
407 return -EINVAL;
408 }
409
410 if (!ata_is_host_link(link)) {
411 ata_dev_printk(dev, KERN_ERR,
412 "Port Multipliers cannot be nested\n");
413 return -EINVAL;
414 }
415
416 if (dev->devno) {
417 ata_dev_printk(dev, KERN_ERR,
418 "Port Multiplier must be the first device\n");
419 return -EINVAL;
420 }
421
422 WARN_ON(link->pmp != 0);
423 link->pmp = SATA_PMP_CTRL_PORT;
424
425 /* read GSCR block */
426 rc = sata_pmp_read_gscr(dev, dev->gscr);
427 if (rc)
428 goto fail;
429
430 /* config PMP */
431 rc = sata_pmp_configure(dev, 1);
432 if (rc)
433 goto fail;
434
435 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
436 if (rc) {
437 ata_dev_printk(dev, KERN_INFO,
438 "failed to initialize PMP links\n");
439 goto fail;
440 }
441
442 /* attach it */
443 spin_lock_irqsave(ap->lock, flags);
444 WARN_ON(ap->nr_pmp_links);
445 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
446 spin_unlock_irqrestore(ap->lock, flags);
447
448 sata_pmp_quirks(ap);
449
450 if (ap->ops->pmp_attach)
451 ap->ops->pmp_attach(ap);
452
453 ata_port_for_each_link(tlink, ap)
454 sata_link_init_spd(tlink);
455
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900456 ata_acpi_associate_sata_port(ap);
457
Tejun Heo3af9a772007-09-23 13:19:54 +0900458 return 0;
459
460 fail:
461 link->pmp = 0;
462 return rc;
463}
464
465/**
466 * sata_pmp_detach - detach a SATA PMP device
467 * @dev: SATA PMP device to detach
468 *
469 * Detach SATA PMP device @dev. This function is also
470 * responsible for deconfiguring PMP links.
471 *
472 * LOCKING:
473 * Kernel thread context (may sleep).
474 */
475static void sata_pmp_detach(struct ata_device *dev)
476{
477 struct ata_link *link = dev->link;
478 struct ata_port *ap = link->ap;
479 struct ata_link *tlink;
480 unsigned long flags;
481
482 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
483
484 WARN_ON(!ata_is_host_link(link) || dev->devno ||
485 link->pmp != SATA_PMP_CTRL_PORT);
486
487 if (ap->ops->pmp_detach)
488 ap->ops->pmp_detach(ap);
489
490 ata_port_for_each_link(tlink, ap)
491 ata_eh_detach_dev(tlink->device);
492
493 spin_lock_irqsave(ap->lock, flags);
494 ap->nr_pmp_links = 0;
495 link->pmp = 0;
496 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900497
498 ata_acpi_associate_sata_port(ap);
Tejun Heo3af9a772007-09-23 13:19:54 +0900499}
500
501/**
502 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
503 * @dev: PMP device to compare against
504 * @new_gscr: GSCR block of the new device
505 *
506 * Compare @new_gscr against @dev and determine whether @dev is
507 * the PMP described by @new_gscr.
508 *
509 * LOCKING:
510 * None.
511 *
512 * RETURNS:
513 * 1 if @dev matches @new_gscr, 0 otherwise.
514 */
515static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
516{
517 const u32 *old_gscr = dev->gscr;
518 u16 old_vendor, new_vendor, old_devid, new_devid;
519 int old_nr_ports, new_nr_ports;
520
521 old_vendor = sata_pmp_gscr_vendor(old_gscr);
522 new_vendor = sata_pmp_gscr_vendor(new_gscr);
523 old_devid = sata_pmp_gscr_devid(old_gscr);
524 new_devid = sata_pmp_gscr_devid(new_gscr);
525 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
526 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
527
528 if (old_vendor != new_vendor) {
529 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
530 "vendor mismatch '0x%x' != '0x%x'\n",
531 old_vendor, new_vendor);
532 return 0;
533 }
534
535 if (old_devid != new_devid) {
536 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
537 "device ID mismatch '0x%x' != '0x%x'\n",
538 old_devid, new_devid);
539 return 0;
540 }
541
542 if (old_nr_ports != new_nr_ports) {
543 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
544 "nr_ports mismatch '0x%x' != '0x%x'\n",
545 old_nr_ports, new_nr_ports);
546 return 0;
547 }
548
549 return 1;
550}
551
552/**
553 * sata_pmp_revalidate - revalidate SATA PMP
554 * @dev: PMP device to revalidate
555 * @new_class: new class code
556 *
557 * Re-read GSCR block and make sure @dev is still attached to the
558 * port and properly configured.
559 *
560 * LOCKING:
561 * Kernel thread context (may sleep).
562 *
563 * RETURNS:
564 * 0 on success, -errno otherwise.
565 */
566static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
567{
568 struct ata_link *link = dev->link;
569 struct ata_port *ap = link->ap;
570 u32 *gscr = (void *)ap->sector_buf;
571 int rc;
572
573 DPRINTK("ENTER\n");
574
575 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
576
577 if (!ata_dev_enabled(dev)) {
578 rc = -ENODEV;
579 goto fail;
580 }
581
582 /* wrong class? */
583 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
584 rc = -ENODEV;
585 goto fail;
586 }
587
588 /* read GSCR */
589 rc = sata_pmp_read_gscr(dev, gscr);
590 if (rc)
591 goto fail;
592
593 /* is the pmp still there? */
594 if (!sata_pmp_same_pmp(dev, gscr)) {
595 rc = -ENODEV;
596 goto fail;
597 }
598
599 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
600
601 rc = sata_pmp_configure(dev, 0);
602 if (rc)
603 goto fail;
604
605 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
606
607 DPRINTK("EXIT, rc=0\n");
608 return 0;
609
610 fail:
611 ata_dev_printk(dev, KERN_ERR,
612 "PMP revalidation failed (errno=%d)\n", rc);
613 DPRINTK("EXIT, rc=%d\n", rc);
614 return rc;
615}
616
617/**
618 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
619 * @dev: PMP device to revalidate
620 *
621 * Make sure the attached PMP is accessible.
622 *
623 * LOCKING:
624 * Kernel thread context (may sleep).
625 *
626 * RETURNS:
627 * 0 on success, -errno otherwise.
628 */
629static int sata_pmp_revalidate_quick(struct ata_device *dev)
630{
Tejun Heob06ce3e2007-10-09 15:06:48 +0900631 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900632 u32 prod_id;
Tejun Heo3af9a772007-09-23 13:19:54 +0900633
Tejun Heob06ce3e2007-10-09 15:06:48 +0900634 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
635 if (err_mask) {
636 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
637 "(Emask=0x%x)\n", err_mask);
638 return -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900639 }
640
641 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
642 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
643 /* something weird is going on, request full PMP recovery */
644 return -EIO;
645 }
646
647 return 0;
648}
649
650/**
651 * sata_pmp_eh_recover_pmp - recover PMP
652 * @ap: ATA port PMP is attached to
653 * @prereset: prereset method (can be NULL)
654 * @softreset: softreset method
655 * @hardreset: hardreset method
656 * @postreset: postreset method (can be NULL)
657 *
658 * Recover PMP attached to @ap. Recovery procedure is somewhat
659 * similar to that of ata_eh_recover() except that reset should
660 * always be performed in hard->soft sequence and recovery
661 * failure results in PMP detachment.
662 *
663 * LOCKING:
664 * Kernel thread context (may sleep).
665 *
666 * RETURNS:
667 * 0 on success, -errno on failure.
668 */
669static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
670 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
671 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
672{
673 struct ata_link *link = &ap->link;
674 struct ata_eh_context *ehc = &link->eh_context;
675 struct ata_device *dev = link->device;
676 int tries = ATA_EH_PMP_TRIES;
677 int detach = 0, rc = 0;
678 int reval_failed = 0;
679
680 DPRINTK("ENTER\n");
681
682 if (dev->flags & ATA_DFLAG_DETACH) {
683 detach = 1;
684 goto fail;
685 }
686
687 retry:
688 ehc->classes[0] = ATA_DEV_UNKNOWN;
689
Tejun Heocf480622008-01-24 00:05:14 +0900690 if (ehc->i.action & ATA_EH_RESET) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900691 struct ata_link *tlink;
692
Tejun Heo3af9a772007-09-23 13:19:54 +0900693 /* reset */
Tejun Heo3af9a772007-09-23 13:19:54 +0900694 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
695 postreset);
696 if (rc) {
697 ata_link_printk(link, KERN_ERR,
698 "failed to reset PMP, giving up\n");
699 goto fail;
700 }
701
Tejun Heo3af9a772007-09-23 13:19:54 +0900702 /* PMP is reset, SErrors cannot be trusted, scan all */
Tejun Heob558edd2008-01-24 00:05:14 +0900703 ata_port_for_each_link(tlink, ap) {
704 struct ata_eh_context *ehc = &tlink->eh_context;
705
706 ehc->i.probe_mask |= ATA_ALL_DEVICES;
707 ehc->i.action |= ATA_EH_RESET;
708 }
Tejun Heo3af9a772007-09-23 13:19:54 +0900709 }
710
711 /* If revalidation is requested, revalidate and reconfigure;
712 * otherwise, do quick revalidation.
713 */
714 if (ehc->i.action & ATA_EH_REVALIDATE)
715 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
716 else
717 rc = sata_pmp_revalidate_quick(dev);
718
719 if (rc) {
720 tries--;
721
722 if (rc == -ENODEV) {
Tejun Heob558edd2008-01-24 00:05:14 +0900723 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heo3af9a772007-09-23 13:19:54 +0900724 detach = 1;
725 /* give it just two more chances */
726 tries = min(tries, 2);
727 }
728
729 if (tries) {
730 int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
731
732 /* consecutive revalidation failures? speed down */
733 if (reval_failed)
734 sata_down_spd_limit(link);
735 else
736 reval_failed = 1;
737
738 ata_dev_printk(dev, KERN_WARNING,
Tejun Heocf480622008-01-24 00:05:14 +0900739 "retrying reset%s\n",
Tejun Heo3af9a772007-09-23 13:19:54 +0900740 sleep ? " in 5 secs" : "");
741 if (sleep)
742 ssleep(5);
Tejun Heocf480622008-01-24 00:05:14 +0900743 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900744 goto retry;
745 } else {
746 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
747 "after %d tries, giving up\n",
748 ATA_EH_PMP_TRIES);
749 goto fail;
750 }
751 }
752
753 /* okay, PMP resurrected */
754 ehc->i.flags = 0;
755
756 DPRINTK("EXIT, rc=0\n");
757 return 0;
758
759 fail:
760 sata_pmp_detach(dev);
761 if (detach)
762 ata_eh_detach_dev(dev);
763 else
764 ata_dev_disable(dev);
765
766 DPRINTK("EXIT, rc=%d\n", rc);
767 return rc;
768}
769
770static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
771{
772 struct ata_link *link;
773 unsigned long flags;
774 int rc;
775
776 spin_lock_irqsave(ap->lock, flags);
777
778 ata_port_for_each_link(link, ap) {
779 if (!(link->flags & ATA_LFLAG_DISABLED))
780 continue;
781
782 spin_unlock_irqrestore(ap->lock, flags);
783
784 /* Some PMPs require hardreset sequence to get
785 * SError.N working.
786 */
Tejun Heocf480622008-01-24 00:05:14 +0900787 sata_link_hardreset(link, sata_deb_timing_normal,
Tejun Heo9dadd452008-04-07 22:47:19 +0900788 jiffies + ATA_TMOUT_INTERNAL_QUICK, NULL, NULL);
Tejun Heo3af9a772007-09-23 13:19:54 +0900789
790 /* unconditionally clear SError.N */
791 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
792 if (rc) {
793 ata_link_printk(link, KERN_ERR, "failed to clear "
794 "SError.N (errno=%d)\n", rc);
795 return rc;
796 }
797
798 spin_lock_irqsave(ap->lock, flags);
799 }
800
801 spin_unlock_irqrestore(ap->lock, flags);
802
803 return 0;
804}
805
806static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
807{
808 struct ata_port *ap = link->ap;
809 unsigned long flags;
810
811 if (link_tries[link->pmp] && --link_tries[link->pmp])
812 return 1;
813
814 /* disable this link */
815 if (!(link->flags & ATA_LFLAG_DISABLED)) {
816 ata_link_printk(link, KERN_WARNING,
817 "failed to recover link after %d tries, disabling\n",
818 ATA_EH_PMP_LINK_TRIES);
819
820 spin_lock_irqsave(ap->lock, flags);
821 link->flags |= ATA_LFLAG_DISABLED;
822 spin_unlock_irqrestore(ap->lock, flags);
823 }
824
825 ata_dev_disable(link->device);
826 link->eh_context.i.action = 0;
827
828 return 0;
829}
830
831/**
832 * sata_pmp_eh_recover - recover PMP-enabled port
833 * @ap: ATA port to recover
Tejun Heo3af9a772007-09-23 13:19:54 +0900834 *
835 * Drive EH recovery operation for PMP enabled port @ap. This
836 * function recovers host and PMP ports with proper retrials and
837 * fallbacks. Actual recovery operations are performed using
838 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
839 *
840 * LOCKING:
841 * Kernel thread context (may sleep).
842 *
843 * RETURNS:
844 * 0 on success, -errno on failure.
845 */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900846static int sata_pmp_eh_recover(struct ata_port *ap)
Tejun Heo3af9a772007-09-23 13:19:54 +0900847{
Tejun Heoa1efdab2008-03-25 12:22:50 +0900848 struct ata_port_operations *ops = ap->ops;
Tejun Heo3af9a772007-09-23 13:19:54 +0900849 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
850 struct ata_link *pmp_link = &ap->link;
851 struct ata_device *pmp_dev = pmp_link->device;
852 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
Tejun Heof1bbfb92008-05-19 01:15:11 +0900853 u32 *gscr = pmp_dev->gscr;
Tejun Heo3af9a772007-09-23 13:19:54 +0900854 struct ata_link *link;
855 struct ata_device *dev;
Tejun Heob06ce3e2007-10-09 15:06:48 +0900856 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900857 u32 gscr_error, sntf;
858 int cnt, rc;
859
860 pmp_tries = ATA_EH_PMP_TRIES;
861 ata_port_for_each_link(link, ap)
862 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
863
864 retry:
865 /* PMP attached? */
Tejun Heo071f44b2008-04-07 22:47:22 +0900866 if (!sata_pmp_attached(ap)) {
Tejun Heoa1efdab2008-03-25 12:22:50 +0900867 rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
868 ops->hardreset, ops->postreset, NULL);
Tejun Heo3af9a772007-09-23 13:19:54 +0900869 if (rc) {
870 ata_link_for_each_dev(dev, &ap->link)
871 ata_dev_disable(dev);
872 return rc;
873 }
874
875 if (pmp_dev->class != ATA_DEV_PMP)
876 return 0;
877
878 /* new PMP online */
879 ata_port_for_each_link(link, ap)
880 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
881
882 /* fall through */
883 }
884
885 /* recover pmp */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900886 rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
887 ops->hardreset, ops->postreset);
Tejun Heo3af9a772007-09-23 13:19:54 +0900888 if (rc)
889 goto pmp_fail;
890
Tejun Heof1bbfb92008-05-19 01:15:11 +0900891 /* PHY event notification can disturb reset and other recovery
892 * operations. Turn it off.
893 */
894 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
895 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
896
897 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
898 gscr[SATA_PMP_GSCR_FEAT_EN]);
899 if (err_mask) {
900 ata_link_printk(pmp_link, KERN_WARNING,
901 "failed to disable NOTIFY (err_mask=0x%x)\n",
902 err_mask);
903 goto pmp_fail;
904 }
905 }
906
Tejun Heo3af9a772007-09-23 13:19:54 +0900907 /* handle disabled links */
908 rc = sata_pmp_eh_handle_disabled_links(ap);
909 if (rc)
910 goto pmp_fail;
911
912 /* recover links */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900913 rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
914 ops->pmp_hardreset, ops->pmp_postreset, &link);
Tejun Heo3af9a772007-09-23 13:19:54 +0900915 if (rc)
916 goto link_fail;
917
918 /* Connection status might have changed while resetting other
919 * links, check SATA_PMP_GSCR_ERROR before returning.
920 */
921
922 /* clear SNotification */
923 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
924 if (rc == 0)
925 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
926
927 /* enable notification */
928 if (pmp_dev->flags & ATA_DFLAG_AN) {
Tejun Heof1bbfb92008-05-19 01:15:11 +0900929 gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
Tejun Heo3af9a772007-09-23 13:19:54 +0900930
Tejun Heof1bbfb92008-05-19 01:15:11 +0900931 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
932 gscr[SATA_PMP_GSCR_FEAT_EN]);
Tejun Heob06ce3e2007-10-09 15:06:48 +0900933 if (err_mask) {
934 ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
935 "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
936 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900937 goto pmp_fail;
938 }
939 }
940
941 /* check GSCR_ERROR */
Tejun Heob06ce3e2007-10-09 15:06:48 +0900942 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
943 if (err_mask) {
944 ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
945 "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
946 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900947 goto pmp_fail;
948 }
949
950 cnt = 0;
951 ata_port_for_each_link(link, ap) {
952 if (!(gscr_error & (1 << link->pmp)))
953 continue;
954
955 if (sata_pmp_handle_link_fail(link, link_tries)) {
956 ata_ehi_hotplugged(&link->eh_context.i);
957 cnt++;
958 } else {
959 ata_link_printk(link, KERN_WARNING,
960 "PHY status changed but maxed out on retries, "
961 "giving up\n");
962 ata_link_printk(link, KERN_WARNING,
963 "Manully issue scan to resume this link\n");
964 }
965 }
966
967 if (cnt) {
968 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
969 "ports, repeating recovery\n");
970 goto retry;
971 }
972
973 return 0;
974
975 link_fail:
976 if (sata_pmp_handle_link_fail(link, link_tries)) {
Tejun Heocf480622008-01-24 00:05:14 +0900977 pmp_ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900978 goto retry;
979 }
980
981 /* fall through */
982 pmp_fail:
983 /* Control always ends up here after detaching PMP. Shut up
984 * and return if we're unloading.
985 */
986 if (ap->pflags & ATA_PFLAG_UNLOADING)
987 return rc;
988
Tejun Heo071f44b2008-04-07 22:47:22 +0900989 if (!sata_pmp_attached(ap))
Tejun Heo3af9a772007-09-23 13:19:54 +0900990 goto retry;
991
992 if (--pmp_tries) {
993 ata_port_printk(ap, KERN_WARNING,
994 "failed to recover PMP, retrying in 5 secs\n");
Tejun Heocf480622008-01-24 00:05:14 +0900995 pmp_ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900996 ssleep(5);
997 goto retry;
998 }
999
1000 ata_port_printk(ap, KERN_ERR,
1001 "failed to recover PMP after %d tries, giving up\n",
1002 ATA_EH_PMP_TRIES);
1003 sata_pmp_detach(pmp_dev);
1004 ata_dev_disable(pmp_dev);
1005
1006 return rc;
1007}
1008
1009/**
Tejun Heoa1efdab2008-03-25 12:22:50 +09001010 * sata_pmp_error_handler - do standard error handling for PMP-enabled host
Tejun Heo3af9a772007-09-23 13:19:54 +09001011 * @ap: host port to handle error for
Tejun Heo3af9a772007-09-23 13:19:54 +09001012 *
1013 * Perform standard error handling sequence for PMP-enabled host
1014 * @ap.
1015 *
1016 * LOCKING:
1017 * Kernel thread context (may sleep).
1018 */
Tejun Heoa1efdab2008-03-25 12:22:50 +09001019void sata_pmp_error_handler(struct ata_port *ap)
Tejun Heo3af9a772007-09-23 13:19:54 +09001020{
1021 ata_eh_autopsy(ap);
1022 ata_eh_report(ap);
Tejun Heoa1efdab2008-03-25 12:22:50 +09001023 sata_pmp_eh_recover(ap);
Tejun Heo3af9a772007-09-23 13:19:54 +09001024 ata_eh_finish(ap);
1025}
Tejun Heo48515f62008-04-07 22:47:21 +09001026
1027EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
1028EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
1029EXPORT_SYMBOL_GPL(sata_pmp_error_handler);