blob: 0f9386d4a5a06e080bc32cf921744e38d9207f4d [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 Heo3af9a772007-09-23 13:19:54 +0900325 /* class code report is unreliable */
326 if (link->pmp < 5)
327 link->flags |= ATA_LFLAG_ASSUME_ATA;
328
329 /* port 5 is for SEMB device and it doesn't like SRST */
330 if (link->pmp == 5)
331 link->flags |= ATA_LFLAG_NO_SRST |
332 ATA_LFLAG_ASSUME_SEMB;
333 }
334 } else if (vendor == 0x1095 && devid == 0x4723) {
335 /* sil4723 quirks */
336 ata_port_for_each_link(link, ap) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900337 /* class code report is unreliable */
338 if (link->pmp < 2)
339 link->flags |= ATA_LFLAG_ASSUME_ATA;
340
341 /* the config device at port 2 locks up on SRST */
342 if (link->pmp == 2)
343 link->flags |= ATA_LFLAG_NO_SRST |
344 ATA_LFLAG_ASSUME_ATA;
345 }
346 } else if (vendor == 0x1095 && devid == 0x4726) {
347 /* sil4726 quirks */
348 ata_port_for_each_link(link, ap) {
Tejun Heo80483072008-01-10 13:38:46 +0900349 /* Class code report is unreliable and SRST
350 * times out under certain configurations.
351 * Config device can be at port 0 or 5 and
352 * locks up on SRST.
Tejun Heo3af9a772007-09-23 13:19:54 +0900353 */
Tejun Heo80483072008-01-10 13:38:46 +0900354 if (link->pmp <= 5)
Tejun Heo3af9a772007-09-23 13:19:54 +0900355 link->flags |= ATA_LFLAG_NO_SRST |
356 ATA_LFLAG_ASSUME_ATA;
357
358 /* Port 6 is for SEMB device which doesn't
359 * like SRST either.
360 */
361 if (link->pmp == 6)
362 link->flags |= ATA_LFLAG_NO_SRST |
363 ATA_LFLAG_ASSUME_SEMB;
364 }
365 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
366 devid == 0x5734 || devid == 0x5744)) {
367 /* sil5723/5744 quirks */
368
369 /* sil5723/5744 has either two or three downstream
370 * ports depending on operation mode. The last port
371 * is empty if any actual IO device is available or
372 * occupied by a pseudo configuration device
373 * otherwise. Don't try hard to recover it.
374 */
375 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
Tejun Heo3af9a772007-09-23 13:19:54 +0900376 }
377}
378
379/**
380 * sata_pmp_attach - attach a SATA PMP device
381 * @dev: SATA PMP device to attach
382 *
383 * Configure and attach SATA PMP device @dev. This function is
384 * also responsible for allocating and initializing PMP links.
385 *
386 * LOCKING:
387 * Kernel thread context (may sleep).
388 *
389 * RETURNS:
390 * 0 on success, -errno on failure.
391 */
392int sata_pmp_attach(struct ata_device *dev)
393{
394 struct ata_link *link = dev->link;
395 struct ata_port *ap = link->ap;
396 unsigned long flags;
397 struct ata_link *tlink;
398 int rc;
399
400 /* is it hanging off the right place? */
Tejun Heo071f44b2008-04-07 22:47:22 +0900401 if (!sata_pmp_supported(ap)) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900402 ata_dev_printk(dev, KERN_ERR,
403 "host does not support Port Multiplier\n");
404 return -EINVAL;
405 }
406
407 if (!ata_is_host_link(link)) {
408 ata_dev_printk(dev, KERN_ERR,
409 "Port Multipliers cannot be nested\n");
410 return -EINVAL;
411 }
412
413 if (dev->devno) {
414 ata_dev_printk(dev, KERN_ERR,
415 "Port Multiplier must be the first device\n");
416 return -EINVAL;
417 }
418
419 WARN_ON(link->pmp != 0);
420 link->pmp = SATA_PMP_CTRL_PORT;
421
422 /* read GSCR block */
423 rc = sata_pmp_read_gscr(dev, dev->gscr);
424 if (rc)
425 goto fail;
426
427 /* config PMP */
428 rc = sata_pmp_configure(dev, 1);
429 if (rc)
430 goto fail;
431
432 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
433 if (rc) {
434 ata_dev_printk(dev, KERN_INFO,
435 "failed to initialize PMP links\n");
436 goto fail;
437 }
438
439 /* attach it */
440 spin_lock_irqsave(ap->lock, flags);
441 WARN_ON(ap->nr_pmp_links);
442 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
443 spin_unlock_irqrestore(ap->lock, flags);
444
445 sata_pmp_quirks(ap);
446
447 if (ap->ops->pmp_attach)
448 ap->ops->pmp_attach(ap);
449
450 ata_port_for_each_link(tlink, ap)
451 sata_link_init_spd(tlink);
452
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900453 ata_acpi_associate_sata_port(ap);
454
Tejun Heo3af9a772007-09-23 13:19:54 +0900455 return 0;
456
457 fail:
458 link->pmp = 0;
459 return rc;
460}
461
462/**
463 * sata_pmp_detach - detach a SATA PMP device
464 * @dev: SATA PMP device to detach
465 *
466 * Detach SATA PMP device @dev. This function is also
467 * responsible for deconfiguring PMP links.
468 *
469 * LOCKING:
470 * Kernel thread context (may sleep).
471 */
472static void sata_pmp_detach(struct ata_device *dev)
473{
474 struct ata_link *link = dev->link;
475 struct ata_port *ap = link->ap;
476 struct ata_link *tlink;
477 unsigned long flags;
478
479 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
480
481 WARN_ON(!ata_is_host_link(link) || dev->devno ||
482 link->pmp != SATA_PMP_CTRL_PORT);
483
484 if (ap->ops->pmp_detach)
485 ap->ops->pmp_detach(ap);
486
487 ata_port_for_each_link(tlink, ap)
488 ata_eh_detach_dev(tlink->device);
489
490 spin_lock_irqsave(ap->lock, flags);
491 ap->nr_pmp_links = 0;
492 link->pmp = 0;
493 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900494
495 ata_acpi_associate_sata_port(ap);
Tejun Heo3af9a772007-09-23 13:19:54 +0900496}
497
498/**
499 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
500 * @dev: PMP device to compare against
501 * @new_gscr: GSCR block of the new device
502 *
503 * Compare @new_gscr against @dev and determine whether @dev is
504 * the PMP described by @new_gscr.
505 *
506 * LOCKING:
507 * None.
508 *
509 * RETURNS:
510 * 1 if @dev matches @new_gscr, 0 otherwise.
511 */
512static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
513{
514 const u32 *old_gscr = dev->gscr;
515 u16 old_vendor, new_vendor, old_devid, new_devid;
516 int old_nr_ports, new_nr_ports;
517
518 old_vendor = sata_pmp_gscr_vendor(old_gscr);
519 new_vendor = sata_pmp_gscr_vendor(new_gscr);
520 old_devid = sata_pmp_gscr_devid(old_gscr);
521 new_devid = sata_pmp_gscr_devid(new_gscr);
522 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
523 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
524
525 if (old_vendor != new_vendor) {
526 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
527 "vendor mismatch '0x%x' != '0x%x'\n",
528 old_vendor, new_vendor);
529 return 0;
530 }
531
532 if (old_devid != new_devid) {
533 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
534 "device ID mismatch '0x%x' != '0x%x'\n",
535 old_devid, new_devid);
536 return 0;
537 }
538
539 if (old_nr_ports != new_nr_ports) {
540 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
541 "nr_ports mismatch '0x%x' != '0x%x'\n",
542 old_nr_ports, new_nr_ports);
543 return 0;
544 }
545
546 return 1;
547}
548
549/**
550 * sata_pmp_revalidate - revalidate SATA PMP
551 * @dev: PMP device to revalidate
552 * @new_class: new class code
553 *
554 * Re-read GSCR block and make sure @dev is still attached to the
555 * port and properly configured.
556 *
557 * LOCKING:
558 * Kernel thread context (may sleep).
559 *
560 * RETURNS:
561 * 0 on success, -errno otherwise.
562 */
563static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
564{
565 struct ata_link *link = dev->link;
566 struct ata_port *ap = link->ap;
567 u32 *gscr = (void *)ap->sector_buf;
568 int rc;
569
570 DPRINTK("ENTER\n");
571
572 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
573
574 if (!ata_dev_enabled(dev)) {
575 rc = -ENODEV;
576 goto fail;
577 }
578
579 /* wrong class? */
580 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
581 rc = -ENODEV;
582 goto fail;
583 }
584
585 /* read GSCR */
586 rc = sata_pmp_read_gscr(dev, gscr);
587 if (rc)
588 goto fail;
589
590 /* is the pmp still there? */
591 if (!sata_pmp_same_pmp(dev, gscr)) {
592 rc = -ENODEV;
593 goto fail;
594 }
595
596 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
597
598 rc = sata_pmp_configure(dev, 0);
599 if (rc)
600 goto fail;
601
602 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
603
604 DPRINTK("EXIT, rc=0\n");
605 return 0;
606
607 fail:
608 ata_dev_printk(dev, KERN_ERR,
609 "PMP revalidation failed (errno=%d)\n", rc);
610 DPRINTK("EXIT, rc=%d\n", rc);
611 return rc;
612}
613
614/**
615 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
616 * @dev: PMP device to revalidate
617 *
618 * Make sure the attached PMP is accessible.
619 *
620 * LOCKING:
621 * Kernel thread context (may sleep).
622 *
623 * RETURNS:
624 * 0 on success, -errno otherwise.
625 */
626static int sata_pmp_revalidate_quick(struct ata_device *dev)
627{
Tejun Heob06ce3e2007-10-09 15:06:48 +0900628 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900629 u32 prod_id;
Tejun Heo3af9a772007-09-23 13:19:54 +0900630
Tejun Heob06ce3e2007-10-09 15:06:48 +0900631 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
632 if (err_mask) {
633 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
634 "(Emask=0x%x)\n", err_mask);
635 return -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900636 }
637
638 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
639 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
640 /* something weird is going on, request full PMP recovery */
641 return -EIO;
642 }
643
644 return 0;
645}
646
647/**
648 * sata_pmp_eh_recover_pmp - recover PMP
649 * @ap: ATA port PMP is attached to
650 * @prereset: prereset method (can be NULL)
651 * @softreset: softreset method
652 * @hardreset: hardreset method
653 * @postreset: postreset method (can be NULL)
654 *
655 * Recover PMP attached to @ap. Recovery procedure is somewhat
656 * similar to that of ata_eh_recover() except that reset should
657 * always be performed in hard->soft sequence and recovery
658 * failure results in PMP detachment.
659 *
660 * LOCKING:
661 * Kernel thread context (may sleep).
662 *
663 * RETURNS:
664 * 0 on success, -errno on failure.
665 */
666static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
667 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
668 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
669{
670 struct ata_link *link = &ap->link;
671 struct ata_eh_context *ehc = &link->eh_context;
672 struct ata_device *dev = link->device;
673 int tries = ATA_EH_PMP_TRIES;
674 int detach = 0, rc = 0;
675 int reval_failed = 0;
676
677 DPRINTK("ENTER\n");
678
679 if (dev->flags & ATA_DFLAG_DETACH) {
680 detach = 1;
681 goto fail;
682 }
683
684 retry:
685 ehc->classes[0] = ATA_DEV_UNKNOWN;
686
Tejun Heocf480622008-01-24 00:05:14 +0900687 if (ehc->i.action & ATA_EH_RESET) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900688 struct ata_link *tlink;
689
Tejun Heo3af9a772007-09-23 13:19:54 +0900690 /* reset */
Tejun Heo3af9a772007-09-23 13:19:54 +0900691 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
692 postreset);
693 if (rc) {
694 ata_link_printk(link, KERN_ERR,
695 "failed to reset PMP, giving up\n");
696 goto fail;
697 }
698
Tejun Heo3af9a772007-09-23 13:19:54 +0900699 /* PMP is reset, SErrors cannot be trusted, scan all */
Tejun Heob558edd2008-01-24 00:05:14 +0900700 ata_port_for_each_link(tlink, ap) {
701 struct ata_eh_context *ehc = &tlink->eh_context;
702
703 ehc->i.probe_mask |= ATA_ALL_DEVICES;
704 ehc->i.action |= ATA_EH_RESET;
705 }
Tejun Heo3af9a772007-09-23 13:19:54 +0900706 }
707
708 /* If revalidation is requested, revalidate and reconfigure;
709 * otherwise, do quick revalidation.
710 */
711 if (ehc->i.action & ATA_EH_REVALIDATE)
712 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
713 else
714 rc = sata_pmp_revalidate_quick(dev);
715
716 if (rc) {
717 tries--;
718
719 if (rc == -ENODEV) {
Tejun Heob558edd2008-01-24 00:05:14 +0900720 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heo3af9a772007-09-23 13:19:54 +0900721 detach = 1;
722 /* give it just two more chances */
723 tries = min(tries, 2);
724 }
725
726 if (tries) {
727 int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
728
729 /* consecutive revalidation failures? speed down */
730 if (reval_failed)
731 sata_down_spd_limit(link);
732 else
733 reval_failed = 1;
734
735 ata_dev_printk(dev, KERN_WARNING,
Tejun Heocf480622008-01-24 00:05:14 +0900736 "retrying reset%s\n",
Tejun Heo3af9a772007-09-23 13:19:54 +0900737 sleep ? " in 5 secs" : "");
738 if (sleep)
739 ssleep(5);
Tejun Heocf480622008-01-24 00:05:14 +0900740 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900741 goto retry;
742 } else {
743 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
744 "after %d tries, giving up\n",
745 ATA_EH_PMP_TRIES);
746 goto fail;
747 }
748 }
749
750 /* okay, PMP resurrected */
751 ehc->i.flags = 0;
752
753 DPRINTK("EXIT, rc=0\n");
754 return 0;
755
756 fail:
757 sata_pmp_detach(dev);
758 if (detach)
759 ata_eh_detach_dev(dev);
760 else
761 ata_dev_disable(dev);
762
763 DPRINTK("EXIT, rc=%d\n", rc);
764 return rc;
765}
766
767static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
768{
769 struct ata_link *link;
770 unsigned long flags;
771 int rc;
772
773 spin_lock_irqsave(ap->lock, flags);
774
775 ata_port_for_each_link(link, ap) {
776 if (!(link->flags & ATA_LFLAG_DISABLED))
777 continue;
778
779 spin_unlock_irqrestore(ap->lock, flags);
780
781 /* Some PMPs require hardreset sequence to get
782 * SError.N working.
783 */
Tejun Heocf480622008-01-24 00:05:14 +0900784 sata_link_hardreset(link, sata_deb_timing_normal,
Tejun Heo9dadd452008-04-07 22:47:19 +0900785 jiffies + ATA_TMOUT_INTERNAL_QUICK, NULL, NULL);
Tejun Heo3af9a772007-09-23 13:19:54 +0900786
787 /* unconditionally clear SError.N */
788 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
789 if (rc) {
790 ata_link_printk(link, KERN_ERR, "failed to clear "
791 "SError.N (errno=%d)\n", rc);
792 return rc;
793 }
794
795 spin_lock_irqsave(ap->lock, flags);
796 }
797
798 spin_unlock_irqrestore(ap->lock, flags);
799
800 return 0;
801}
802
803static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
804{
805 struct ata_port *ap = link->ap;
806 unsigned long flags;
807
808 if (link_tries[link->pmp] && --link_tries[link->pmp])
809 return 1;
810
811 /* disable this link */
812 if (!(link->flags & ATA_LFLAG_DISABLED)) {
813 ata_link_printk(link, KERN_WARNING,
814 "failed to recover link after %d tries, disabling\n",
815 ATA_EH_PMP_LINK_TRIES);
816
817 spin_lock_irqsave(ap->lock, flags);
818 link->flags |= ATA_LFLAG_DISABLED;
819 spin_unlock_irqrestore(ap->lock, flags);
820 }
821
822 ata_dev_disable(link->device);
823 link->eh_context.i.action = 0;
824
825 return 0;
826}
827
828/**
829 * sata_pmp_eh_recover - recover PMP-enabled port
830 * @ap: ATA port to recover
Tejun Heo3af9a772007-09-23 13:19:54 +0900831 *
832 * Drive EH recovery operation for PMP enabled port @ap. This
833 * function recovers host and PMP ports with proper retrials and
834 * fallbacks. Actual recovery operations are performed using
835 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
836 *
837 * LOCKING:
838 * Kernel thread context (may sleep).
839 *
840 * RETURNS:
841 * 0 on success, -errno on failure.
842 */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900843static int sata_pmp_eh_recover(struct ata_port *ap)
Tejun Heo3af9a772007-09-23 13:19:54 +0900844{
Tejun Heoa1efdab2008-03-25 12:22:50 +0900845 struct ata_port_operations *ops = ap->ops;
Tejun Heo3af9a772007-09-23 13:19:54 +0900846 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
847 struct ata_link *pmp_link = &ap->link;
848 struct ata_device *pmp_dev = pmp_link->device;
849 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
Tejun Heof1bbfb92008-05-19 01:15:11 +0900850 u32 *gscr = pmp_dev->gscr;
Tejun Heo3af9a772007-09-23 13:19:54 +0900851 struct ata_link *link;
852 struct ata_device *dev;
Tejun Heob06ce3e2007-10-09 15:06:48 +0900853 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900854 u32 gscr_error, sntf;
855 int cnt, rc;
856
857 pmp_tries = ATA_EH_PMP_TRIES;
858 ata_port_for_each_link(link, ap)
859 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
860
861 retry:
862 /* PMP attached? */
Tejun Heo071f44b2008-04-07 22:47:22 +0900863 if (!sata_pmp_attached(ap)) {
Tejun Heoa1efdab2008-03-25 12:22:50 +0900864 rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
865 ops->hardreset, ops->postreset, NULL);
Tejun Heo3af9a772007-09-23 13:19:54 +0900866 if (rc) {
867 ata_link_for_each_dev(dev, &ap->link)
868 ata_dev_disable(dev);
869 return rc;
870 }
871
872 if (pmp_dev->class != ATA_DEV_PMP)
873 return 0;
874
875 /* new PMP online */
876 ata_port_for_each_link(link, ap)
877 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
878
879 /* fall through */
880 }
881
882 /* recover pmp */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900883 rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
884 ops->hardreset, ops->postreset);
Tejun Heo3af9a772007-09-23 13:19:54 +0900885 if (rc)
886 goto pmp_fail;
887
Tejun Heof1bbfb92008-05-19 01:15:11 +0900888 /* PHY event notification can disturb reset and other recovery
889 * operations. Turn it off.
890 */
891 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
892 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
893
894 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
895 gscr[SATA_PMP_GSCR_FEAT_EN]);
896 if (err_mask) {
897 ata_link_printk(pmp_link, KERN_WARNING,
898 "failed to disable NOTIFY (err_mask=0x%x)\n",
899 err_mask);
900 goto pmp_fail;
901 }
902 }
903
Tejun Heo3af9a772007-09-23 13:19:54 +0900904 /* handle disabled links */
905 rc = sata_pmp_eh_handle_disabled_links(ap);
906 if (rc)
907 goto pmp_fail;
908
909 /* recover links */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900910 rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
911 ops->pmp_hardreset, ops->pmp_postreset, &link);
Tejun Heo3af9a772007-09-23 13:19:54 +0900912 if (rc)
913 goto link_fail;
914
915 /* Connection status might have changed while resetting other
916 * links, check SATA_PMP_GSCR_ERROR before returning.
917 */
918
919 /* clear SNotification */
920 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
921 if (rc == 0)
922 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
923
924 /* enable notification */
925 if (pmp_dev->flags & ATA_DFLAG_AN) {
Tejun Heof1bbfb92008-05-19 01:15:11 +0900926 gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
Tejun Heo3af9a772007-09-23 13:19:54 +0900927
Tejun Heof1bbfb92008-05-19 01:15:11 +0900928 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
929 gscr[SATA_PMP_GSCR_FEAT_EN]);
Tejun Heob06ce3e2007-10-09 15:06:48 +0900930 if (err_mask) {
931 ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
932 "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
933 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900934 goto pmp_fail;
935 }
936 }
937
938 /* check GSCR_ERROR */
Tejun Heob06ce3e2007-10-09 15:06:48 +0900939 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
940 if (err_mask) {
941 ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
942 "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
943 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900944 goto pmp_fail;
945 }
946
947 cnt = 0;
948 ata_port_for_each_link(link, ap) {
949 if (!(gscr_error & (1 << link->pmp)))
950 continue;
951
952 if (sata_pmp_handle_link_fail(link, link_tries)) {
953 ata_ehi_hotplugged(&link->eh_context.i);
954 cnt++;
955 } else {
956 ata_link_printk(link, KERN_WARNING,
957 "PHY status changed but maxed out on retries, "
958 "giving up\n");
959 ata_link_printk(link, KERN_WARNING,
960 "Manully issue scan to resume this link\n");
961 }
962 }
963
964 if (cnt) {
965 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
966 "ports, repeating recovery\n");
967 goto retry;
968 }
969
970 return 0;
971
972 link_fail:
973 if (sata_pmp_handle_link_fail(link, link_tries)) {
Tejun Heocf480622008-01-24 00:05:14 +0900974 pmp_ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900975 goto retry;
976 }
977
978 /* fall through */
979 pmp_fail:
980 /* Control always ends up here after detaching PMP. Shut up
981 * and return if we're unloading.
982 */
983 if (ap->pflags & ATA_PFLAG_UNLOADING)
984 return rc;
985
Tejun Heo071f44b2008-04-07 22:47:22 +0900986 if (!sata_pmp_attached(ap))
Tejun Heo3af9a772007-09-23 13:19:54 +0900987 goto retry;
988
989 if (--pmp_tries) {
990 ata_port_printk(ap, KERN_WARNING,
991 "failed to recover PMP, retrying in 5 secs\n");
Tejun Heocf480622008-01-24 00:05:14 +0900992 pmp_ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900993 ssleep(5);
994 goto retry;
995 }
996
997 ata_port_printk(ap, KERN_ERR,
998 "failed to recover PMP after %d tries, giving up\n",
999 ATA_EH_PMP_TRIES);
1000 sata_pmp_detach(pmp_dev);
1001 ata_dev_disable(pmp_dev);
1002
1003 return rc;
1004}
1005
1006/**
Tejun Heoa1efdab2008-03-25 12:22:50 +09001007 * sata_pmp_error_handler - do standard error handling for PMP-enabled host
Tejun Heo3af9a772007-09-23 13:19:54 +09001008 * @ap: host port to handle error for
Tejun Heo3af9a772007-09-23 13:19:54 +09001009 *
1010 * Perform standard error handling sequence for PMP-enabled host
1011 * @ap.
1012 *
1013 * LOCKING:
1014 * Kernel thread context (may sleep).
1015 */
Tejun Heoa1efdab2008-03-25 12:22:50 +09001016void sata_pmp_error_handler(struct ata_port *ap)
Tejun Heo3af9a772007-09-23 13:19:54 +09001017{
1018 ata_eh_autopsy(ap);
1019 ata_eh_report(ap);
Tejun Heoa1efdab2008-03-25 12:22:50 +09001020 sata_pmp_eh_recover(ap);
Tejun Heo3af9a772007-09-23 13:19:54 +09001021 ata_eh_finish(ap);
1022}
Tejun Heo48515f62008-04-07 22:47:21 +09001023
1024EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
1025EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
1026EXPORT_SYMBOL_GPL(sata_pmp_error_handler);