blob: 224faabd7b7efd1adf769ded4f0d4989604a012c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Tejun Heo3af9a772007-09-23 13:19:54 +090013#include "libata.h"
14
Tejun Heo48515f62008-04-07 22:47:21 +090015const struct ata_port_operations sata_pmp_port_ops = {
16 .inherits = &sata_port_ops,
17 .pmp_prereset = ata_std_prereset,
18 .pmp_hardreset = sata_std_hardreset,
19 .pmp_postreset = ata_std_postreset,
20 .error_handler = sata_pmp_error_handler,
21};
22
Tejun Heo3af9a772007-09-23 13:19:54 +090023/**
24 * sata_pmp_read - read PMP register
25 * @link: link to read PMP register for
26 * @reg: register to read
27 * @r_val: resulting value
28 *
Tejun Heob06ce3e2007-10-09 15:06:48 +090029 * Read PMP register.
Tejun Heo3af9a772007-09-23 13:19:54 +090030 *
31 * LOCKING:
32 * Kernel thread context (may sleep).
33 *
34 * RETURNS:
Tejun Heob06ce3e2007-10-09 15:06:48 +090035 * 0 on success, AC_ERR_* mask on failure.
Tejun Heo3af9a772007-09-23 13:19:54 +090036 */
Tejun Heob06ce3e2007-10-09 15:06:48 +090037static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
Tejun Heo3af9a772007-09-23 13:19:54 +090038{
39 struct ata_port *ap = link->ap;
40 struct ata_device *pmp_dev = ap->link.device;
Tejun Heob06ce3e2007-10-09 15:06:48 +090041 struct ata_taskfile tf;
42 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +090043
Tejun Heob06ce3e2007-10-09 15:06:48 +090044 ata_tf_init(pmp_dev, &tf);
45 tf.command = ATA_CMD_PMP_READ;
46 tf.protocol = ATA_PROT_NODATA;
Mark Lord39f25e72008-02-21 15:52:25 -050047 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
Tejun Heob06ce3e2007-10-09 15:06:48 +090048 tf.feature = reg;
49 tf.device = link->pmp;
Tejun Heo3af9a772007-09-23 13:19:54 +090050
Tejun Heob06ce3e2007-10-09 15:06:48 +090051 err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
Tejun Heobf1bff62008-05-19 01:15:10 +090052 SATA_PMP_RW_TIMEOUT);
Tejun Heob06ce3e2007-10-09 15:06:48 +090053 if (err_mask)
54 return err_mask;
55
56 *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
57 return 0;
Tejun Heo3af9a772007-09-23 13:19:54 +090058}
59
60/**
61 * sata_pmp_write - write PMP register
62 * @link: link to write PMP register for
63 * @reg: register to write
64 * @r_val: value to write
65 *
Tejun Heob06ce3e2007-10-09 15:06:48 +090066 * Write PMP register.
Tejun Heo3af9a772007-09-23 13:19:54 +090067 *
68 * LOCKING:
69 * Kernel thread context (may sleep).
70 *
71 * RETURNS:
Tejun Heob06ce3e2007-10-09 15:06:48 +090072 * 0 on success, AC_ERR_* mask on failure.
Tejun Heo3af9a772007-09-23 13:19:54 +090073 */
Tejun Heob06ce3e2007-10-09 15:06:48 +090074static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
Tejun Heo3af9a772007-09-23 13:19:54 +090075{
76 struct ata_port *ap = link->ap;
77 struct ata_device *pmp_dev = ap->link.device;
Tejun Heob06ce3e2007-10-09 15:06:48 +090078 struct ata_taskfile tf;
Tejun Heo3af9a772007-09-23 13:19:54 +090079
Tejun Heob06ce3e2007-10-09 15:06:48 +090080 ata_tf_init(pmp_dev, &tf);
81 tf.command = ATA_CMD_PMP_WRITE;
82 tf.protocol = ATA_PROT_NODATA;
Mark Lord39f25e72008-02-21 15:52:25 -050083 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
Tejun Heob06ce3e2007-10-09 15:06:48 +090084 tf.feature = reg;
85 tf.device = link->pmp;
86 tf.nsect = val & 0xff;
87 tf.lbal = (val >> 8) & 0xff;
88 tf.lbam = (val >> 16) & 0xff;
89 tf.lbah = (val >> 24) & 0xff;
Tejun Heo3af9a772007-09-23 13:19:54 +090090
Tejun Heob06ce3e2007-10-09 15:06:48 +090091 return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
Tejun Heobf1bff62008-05-19 01:15:10 +090092 SATA_PMP_RW_TIMEOUT);
Tejun Heo3af9a772007-09-23 13:19:54 +090093}
94
95/**
Tejun Heo31f88382007-09-23 13:19:54 +090096 * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
97 * @qc: ATA command in question
98 *
99 * A host which has command switching PMP support cannot issue
100 * commands to multiple links simultaneously.
101 *
102 * LOCKING:
103 * spin_lock_irqsave(host lock)
104 *
105 * RETURNS:
106 * ATA_DEFER_* if deferring is needed, 0 otherwise.
107 */
108int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
109{
110 struct ata_link *link = qc->dev->link;
111 struct ata_port *ap = link->ap;
112
113 if (ap->excl_link == NULL || ap->excl_link == link) {
114 if (ap->nr_active_links == 0 || ata_link_active(link)) {
115 qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
116 return ata_std_qc_defer(qc);
117 }
118
119 ap->excl_link = link;
120 }
121
122 return ATA_DEFER_PORT;
123}
124
125/**
Tejun Heo3af9a772007-09-23 13:19:54 +0900126 * sata_pmp_scr_read - read PSCR
127 * @link: ATA link to read PSCR for
128 * @reg: PSCR to read
129 * @r_val: resulting value
130 *
131 * Read PSCR @reg into @r_val for @link, to be called from
132 * ata_scr_read().
133 *
134 * LOCKING:
135 * Kernel thread context (may sleep).
136 *
137 * RETURNS:
138 * 0 on success, -errno on failure.
139 */
140int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
141{
Tejun Heob06ce3e2007-10-09 15:06:48 +0900142 unsigned int err_mask;
143
Tejun Heo3af9a772007-09-23 13:19:54 +0900144 if (reg > SATA_PMP_PSCR_CONTROL)
145 return -EINVAL;
146
Tejun Heob06ce3e2007-10-09 15:06:48 +0900147 err_mask = sata_pmp_read(link, reg, r_val);
148 if (err_mask) {
149 ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
150 "(Emask=0x%x)\n", reg, err_mask);
151 return -EIO;
152 }
153 return 0;
Tejun Heo3af9a772007-09-23 13:19:54 +0900154}
155
156/**
157 * sata_pmp_scr_write - write PSCR
158 * @link: ATA link to write PSCR for
159 * @reg: PSCR to write
160 * @val: value to be written
161 *
162 * Write @val to PSCR @reg for @link, to be called from
163 * ata_scr_write() and ata_scr_write_flush().
164 *
165 * LOCKING:
166 * Kernel thread context (may sleep).
167 *
168 * RETURNS:
169 * 0 on success, -errno on failure.
170 */
171int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
172{
Tejun Heob06ce3e2007-10-09 15:06:48 +0900173 unsigned int err_mask;
174
Tejun Heo3af9a772007-09-23 13:19:54 +0900175 if (reg > SATA_PMP_PSCR_CONTROL)
176 return -EINVAL;
177
Tejun Heob06ce3e2007-10-09 15:06:48 +0900178 err_mask = sata_pmp_write(link, reg, val);
179 if (err_mask) {
180 ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
181 "(Emask=0x%x)\n", reg, err_mask);
182 return -EIO;
183 }
184 return 0;
Tejun Heo3af9a772007-09-23 13:19:54 +0900185}
186
187/**
Tejun Heo3af9a772007-09-23 13:19:54 +0900188 * sata_pmp_read_gscr - read GSCR block of SATA PMP
189 * @dev: PMP device
190 * @gscr: buffer to read GSCR block into
191 *
192 * Read selected PMP GSCRs from the PMP at @dev. This will serve
193 * as configuration and identification info for the PMP.
194 *
195 * LOCKING:
196 * Kernel thread context (may sleep).
197 *
198 * RETURNS:
199 * 0 on success, -errno on failure.
200 */
201static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
202{
203 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
Tejun Heob06ce3e2007-10-09 15:06:48 +0900204 int i;
Tejun Heo3af9a772007-09-23 13:19:54 +0900205
206 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
207 int reg = gscr_to_read[i];
Tejun Heob06ce3e2007-10-09 15:06:48 +0900208 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900209
Tejun Heob06ce3e2007-10-09 15:06:48 +0900210 err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
211 if (err_mask) {
212 ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
213 "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
214 return -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900215 }
216 }
217
218 return 0;
219}
220
221static const char *sata_pmp_spec_rev_str(const u32 *gscr)
222{
223 u32 rev = gscr[SATA_PMP_GSCR_REV];
224
Shane Huangdeeb0032009-09-08 17:37:01 +0800225 if (rev & (1 << 3))
226 return "1.2";
Tejun Heo3af9a772007-09-23 13:19:54 +0900227 if (rev & (1 << 2))
228 return "1.1";
229 if (rev & (1 << 1))
230 return "1.0";
231 return "<unknown>";
232}
233
Grant Grundler4f2c7742010-04-14 18:43:32 -0700234#define PMP_GSCR_SII_POL 129
235
Tejun Heo3af9a772007-09-23 13:19:54 +0900236static int sata_pmp_configure(struct ata_device *dev, int print_info)
237{
238 struct ata_port *ap = dev->link->ap;
239 u32 *gscr = dev->gscr;
Grant Grundler4f2c7742010-04-14 18:43:32 -0700240 u16 vendor = sata_pmp_gscr_vendor(gscr);
241 u16 devid = sata_pmp_gscr_devid(gscr);
Tejun Heob06ce3e2007-10-09 15:06:48 +0900242 unsigned int err_mask = 0;
Tejun Heo3af9a772007-09-23 13:19:54 +0900243 const char *reason;
244 int nr_ports, rc;
245
246 nr_ports = sata_pmp_gscr_ports(gscr);
247
248 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
249 rc = -EINVAL;
250 reason = "invalid nr_ports";
251 goto fail;
252 }
253
254 if ((ap->flags & ATA_FLAG_AN) &&
255 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
256 dev->flags |= ATA_DFLAG_AN;
257
258 /* monitor SERR_PHYRDY_CHG on fan-out ports */
Tejun Heob06ce3e2007-10-09 15:06:48 +0900259 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
260 SERR_PHYRDY_CHG);
261 if (err_mask) {
262 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900263 reason = "failed to write GSCR_ERROR_EN";
264 goto fail;
265 }
266
Grant Grundler4f2c7742010-04-14 18:43:32 -0700267 /* Disable sending Early R_OK.
268 * With "cached read" HDD testing and multiple ports busy on a SATA
269 * host controller, 3726 PMP will very rarely drop a deferred
270 * R_OK that was intended for the host. Symptom will be all
271 * 5 drives under test will timeout, get reset, and recover.
272 */
273 if (vendor == 0x1095 && devid == 0x3726) {
274 u32 reg;
275
276 err_mask = sata_pmp_read(&ap->link, PMP_GSCR_SII_POL, &reg);
277 if (err_mask) {
278 rc = -EIO;
279 reason = "failed to read Sil3726 Private Register";
280 goto fail;
281 }
282 reg &= ~0x1;
283 err_mask = sata_pmp_write(&ap->link, PMP_GSCR_SII_POL, reg);
284 if (err_mask) {
285 rc = -EIO;
286 reason = "failed to write Sil3726 Private Register";
287 goto fail;
288 }
289 }
290
Tejun Heo3af9a772007-09-23 13:19:54 +0900291 if (print_info) {
292 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
293 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
Grant Grundler4f2c7742010-04-14 18:43:32 -0700294 sata_pmp_spec_rev_str(gscr), vendor, devid,
Tejun Heo3af9a772007-09-23 13:19:54 +0900295 sata_pmp_gscr_rev(gscr),
296 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
297 gscr[SATA_PMP_GSCR_FEAT]);
298
299 if (!(dev->flags & ATA_DFLAG_AN))
300 ata_dev_printk(dev, KERN_INFO,
301 "Asynchronous notification not supported, "
302 "hotplug won't\n work on fan-out "
303 "ports. Use warm-plug instead.\n");
304 }
305
306 return 0;
307
308 fail:
309 ata_dev_printk(dev, KERN_ERR,
Tejun Heob06ce3e2007-10-09 15:06:48 +0900310 "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
311 reason, err_mask);
Tejun Heo3af9a772007-09-23 13:19:54 +0900312 return rc;
313}
314
315static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
316{
317 struct ata_link *pmp_link = ap->pmp_link;
318 int i;
319
320 if (!pmp_link) {
321 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
322 GFP_NOIO);
323 if (!pmp_link)
324 return -ENOMEM;
325
326 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
327 ata_link_init(ap, &pmp_link[i], i);
328
329 ap->pmp_link = pmp_link;
330 }
331
332 for (i = 0; i < nr_ports; i++) {
333 struct ata_link *link = &pmp_link[i];
334 struct ata_eh_context *ehc = &link->eh_context;
335
336 link->flags = 0;
Tejun Heob558edd2008-01-24 00:05:14 +0900337 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +0900338 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900339 }
340
341 return 0;
342}
343
344static void sata_pmp_quirks(struct ata_port *ap)
345{
346 u32 *gscr = ap->link.device->gscr;
347 u16 vendor = sata_pmp_gscr_vendor(gscr);
348 u16 devid = sata_pmp_gscr_devid(gscr);
349 struct ata_link *link;
350
351 if (vendor == 0x1095 && devid == 0x3726) {
352 /* sil3726 quirks */
Tejun Heo1eca4362008-11-03 20:03:17 +0900353 ata_for_each_link(link, ap, EDGE) {
Tejun Heo19ef9d52008-05-21 14:11:24 +0900354 /* Class code report is unreliable and SRST
355 * times out under certain configurations.
356 */
Tejun Heo3af9a772007-09-23 13:19:54 +0900357 if (link->pmp < 5)
Tejun Heo19ef9d52008-05-21 14:11:24 +0900358 link->flags |= ATA_LFLAG_NO_SRST |
359 ATA_LFLAG_ASSUME_ATA;
Tejun Heo3af9a772007-09-23 13:19:54 +0900360
361 /* port 5 is for SEMB device and it doesn't like SRST */
362 if (link->pmp == 5)
363 link->flags |= ATA_LFLAG_NO_SRST |
364 ATA_LFLAG_ASSUME_SEMB;
365 }
366 } else if (vendor == 0x1095 && devid == 0x4723) {
367 /* sil4723 quirks */
Tejun Heo1eca4362008-11-03 20:03:17 +0900368 ata_for_each_link(link, ap, EDGE) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900369 /* class code report is unreliable */
370 if (link->pmp < 2)
371 link->flags |= ATA_LFLAG_ASSUME_ATA;
372
373 /* the config device at port 2 locks up on SRST */
374 if (link->pmp == 2)
375 link->flags |= ATA_LFLAG_NO_SRST |
376 ATA_LFLAG_ASSUME_ATA;
377 }
378 } else if (vendor == 0x1095 && devid == 0x4726) {
379 /* sil4726 quirks */
Tejun Heo1eca4362008-11-03 20:03:17 +0900380 ata_for_each_link(link, ap, EDGE) {
Tejun Heo80483072008-01-10 13:38:46 +0900381 /* Class code report is unreliable and SRST
382 * times out under certain configurations.
383 * Config device can be at port 0 or 5 and
384 * locks up on SRST.
Tejun Heo3af9a772007-09-23 13:19:54 +0900385 */
Tejun Heo80483072008-01-10 13:38:46 +0900386 if (link->pmp <= 5)
Tejun Heo3af9a772007-09-23 13:19:54 +0900387 link->flags |= ATA_LFLAG_NO_SRST |
388 ATA_LFLAG_ASSUME_ATA;
389
390 /* Port 6 is for SEMB device which doesn't
391 * like SRST either.
392 */
393 if (link->pmp == 6)
394 link->flags |= ATA_LFLAG_NO_SRST |
395 ATA_LFLAG_ASSUME_SEMB;
396 }
397 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
398 devid == 0x5734 || devid == 0x5744)) {
399 /* sil5723/5744 quirks */
400
401 /* sil5723/5744 has either two or three downstream
402 * ports depending on operation mode. The last port
403 * is empty if any actual IO device is available or
404 * occupied by a pseudo configuration device
405 * otherwise. Don't try hard to recover it.
406 */
407 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
Tejun Heo3af9a772007-09-23 13:19:54 +0900408 }
409}
410
411/**
412 * sata_pmp_attach - attach a SATA PMP device
413 * @dev: SATA PMP device to attach
414 *
415 * Configure and attach SATA PMP device @dev. This function is
416 * also responsible for allocating and initializing PMP links.
417 *
418 * LOCKING:
419 * Kernel thread context (may sleep).
420 *
421 * RETURNS:
422 * 0 on success, -errno on failure.
423 */
424int sata_pmp_attach(struct ata_device *dev)
425{
426 struct ata_link *link = dev->link;
427 struct ata_port *ap = link->ap;
428 unsigned long flags;
429 struct ata_link *tlink;
430 int rc;
431
432 /* is it hanging off the right place? */
Tejun Heo071f44b2008-04-07 22:47:22 +0900433 if (!sata_pmp_supported(ap)) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900434 ata_dev_printk(dev, KERN_ERR,
435 "host does not support Port Multiplier\n");
436 return -EINVAL;
437 }
438
439 if (!ata_is_host_link(link)) {
440 ata_dev_printk(dev, KERN_ERR,
441 "Port Multipliers cannot be nested\n");
442 return -EINVAL;
443 }
444
445 if (dev->devno) {
446 ata_dev_printk(dev, KERN_ERR,
447 "Port Multiplier must be the first device\n");
448 return -EINVAL;
449 }
450
451 WARN_ON(link->pmp != 0);
452 link->pmp = SATA_PMP_CTRL_PORT;
453
454 /* read GSCR block */
455 rc = sata_pmp_read_gscr(dev, dev->gscr);
456 if (rc)
457 goto fail;
458
459 /* config PMP */
460 rc = sata_pmp_configure(dev, 1);
461 if (rc)
462 goto fail;
463
464 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
465 if (rc) {
466 ata_dev_printk(dev, KERN_INFO,
467 "failed to initialize PMP links\n");
468 goto fail;
469 }
470
471 /* attach it */
472 spin_lock_irqsave(ap->lock, flags);
473 WARN_ON(ap->nr_pmp_links);
474 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
475 spin_unlock_irqrestore(ap->lock, flags);
476
477 sata_pmp_quirks(ap);
478
479 if (ap->ops->pmp_attach)
480 ap->ops->pmp_attach(ap);
481
Tejun Heo1eca4362008-11-03 20:03:17 +0900482 ata_for_each_link(tlink, ap, EDGE)
Tejun Heo3af9a772007-09-23 13:19:54 +0900483 sata_link_init_spd(tlink);
484
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900485 ata_acpi_associate_sata_port(ap);
486
Tejun Heo3af9a772007-09-23 13:19:54 +0900487 return 0;
488
489 fail:
490 link->pmp = 0;
491 return rc;
492}
493
494/**
495 * sata_pmp_detach - detach a SATA PMP device
496 * @dev: SATA PMP device to detach
497 *
498 * Detach SATA PMP device @dev. This function is also
499 * responsible for deconfiguring PMP links.
500 *
501 * LOCKING:
502 * Kernel thread context (may sleep).
503 */
504static void sata_pmp_detach(struct ata_device *dev)
505{
506 struct ata_link *link = dev->link;
507 struct ata_port *ap = link->ap;
508 struct ata_link *tlink;
509 unsigned long flags;
510
511 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
512
513 WARN_ON(!ata_is_host_link(link) || dev->devno ||
514 link->pmp != SATA_PMP_CTRL_PORT);
515
516 if (ap->ops->pmp_detach)
517 ap->ops->pmp_detach(ap);
518
Tejun Heo1eca4362008-11-03 20:03:17 +0900519 ata_for_each_link(tlink, ap, EDGE)
Tejun Heo3af9a772007-09-23 13:19:54 +0900520 ata_eh_detach_dev(tlink->device);
521
522 spin_lock_irqsave(ap->lock, flags);
523 ap->nr_pmp_links = 0;
524 link->pmp = 0;
525 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heod0df8b5d2007-09-23 13:19:54 +0900526
527 ata_acpi_associate_sata_port(ap);
Tejun Heo3af9a772007-09-23 13:19:54 +0900528}
529
530/**
531 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
532 * @dev: PMP device to compare against
533 * @new_gscr: GSCR block of the new device
534 *
535 * Compare @new_gscr against @dev and determine whether @dev is
536 * the PMP described by @new_gscr.
537 *
538 * LOCKING:
539 * None.
540 *
541 * RETURNS:
542 * 1 if @dev matches @new_gscr, 0 otherwise.
543 */
544static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
545{
546 const u32 *old_gscr = dev->gscr;
547 u16 old_vendor, new_vendor, old_devid, new_devid;
548 int old_nr_ports, new_nr_ports;
549
550 old_vendor = sata_pmp_gscr_vendor(old_gscr);
551 new_vendor = sata_pmp_gscr_vendor(new_gscr);
552 old_devid = sata_pmp_gscr_devid(old_gscr);
553 new_devid = sata_pmp_gscr_devid(new_gscr);
554 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
555 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
556
557 if (old_vendor != new_vendor) {
558 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
559 "vendor mismatch '0x%x' != '0x%x'\n",
560 old_vendor, new_vendor);
561 return 0;
562 }
563
564 if (old_devid != new_devid) {
565 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
566 "device ID mismatch '0x%x' != '0x%x'\n",
567 old_devid, new_devid);
568 return 0;
569 }
570
571 if (old_nr_ports != new_nr_ports) {
572 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
573 "nr_ports mismatch '0x%x' != '0x%x'\n",
574 old_nr_ports, new_nr_ports);
575 return 0;
576 }
577
578 return 1;
579}
580
581/**
582 * sata_pmp_revalidate - revalidate SATA PMP
583 * @dev: PMP device to revalidate
584 * @new_class: new class code
585 *
586 * Re-read GSCR block and make sure @dev is still attached to the
587 * port and properly configured.
588 *
589 * LOCKING:
590 * Kernel thread context (may sleep).
591 *
592 * RETURNS:
593 * 0 on success, -errno otherwise.
594 */
595static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
596{
597 struct ata_link *link = dev->link;
598 struct ata_port *ap = link->ap;
599 u32 *gscr = (void *)ap->sector_buf;
600 int rc;
601
602 DPRINTK("ENTER\n");
603
604 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
605
606 if (!ata_dev_enabled(dev)) {
607 rc = -ENODEV;
608 goto fail;
609 }
610
611 /* wrong class? */
612 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
613 rc = -ENODEV;
614 goto fail;
615 }
616
617 /* read GSCR */
618 rc = sata_pmp_read_gscr(dev, gscr);
619 if (rc)
620 goto fail;
621
622 /* is the pmp still there? */
623 if (!sata_pmp_same_pmp(dev, gscr)) {
624 rc = -ENODEV;
625 goto fail;
626 }
627
628 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
629
630 rc = sata_pmp_configure(dev, 0);
631 if (rc)
632 goto fail;
633
634 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
635
636 DPRINTK("EXIT, rc=0\n");
637 return 0;
638
639 fail:
640 ata_dev_printk(dev, KERN_ERR,
641 "PMP revalidation failed (errno=%d)\n", rc);
642 DPRINTK("EXIT, rc=%d\n", rc);
643 return rc;
644}
645
646/**
647 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
648 * @dev: PMP device to revalidate
649 *
650 * Make sure the attached PMP is accessible.
651 *
652 * LOCKING:
653 * Kernel thread context (may sleep).
654 *
655 * RETURNS:
656 * 0 on success, -errno otherwise.
657 */
658static int sata_pmp_revalidate_quick(struct ata_device *dev)
659{
Tejun Heob06ce3e2007-10-09 15:06:48 +0900660 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900661 u32 prod_id;
Tejun Heo3af9a772007-09-23 13:19:54 +0900662
Tejun Heob06ce3e2007-10-09 15:06:48 +0900663 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
664 if (err_mask) {
665 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
666 "(Emask=0x%x)\n", err_mask);
667 return -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900668 }
669
670 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
671 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
672 /* something weird is going on, request full PMP recovery */
673 return -EIO;
674 }
675
676 return 0;
677}
678
679/**
680 * sata_pmp_eh_recover_pmp - recover PMP
681 * @ap: ATA port PMP is attached to
682 * @prereset: prereset method (can be NULL)
683 * @softreset: softreset method
684 * @hardreset: hardreset method
685 * @postreset: postreset method (can be NULL)
686 *
687 * Recover PMP attached to @ap. Recovery procedure is somewhat
688 * similar to that of ata_eh_recover() except that reset should
689 * always be performed in hard->soft sequence and recovery
690 * failure results in PMP detachment.
691 *
692 * LOCKING:
693 * Kernel thread context (may sleep).
694 *
695 * RETURNS:
696 * 0 on success, -errno on failure.
697 */
698static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
699 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
700 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
701{
702 struct ata_link *link = &ap->link;
703 struct ata_eh_context *ehc = &link->eh_context;
704 struct ata_device *dev = link->device;
705 int tries = ATA_EH_PMP_TRIES;
706 int detach = 0, rc = 0;
707 int reval_failed = 0;
708
709 DPRINTK("ENTER\n");
710
711 if (dev->flags & ATA_DFLAG_DETACH) {
712 detach = 1;
713 goto fail;
714 }
715
716 retry:
717 ehc->classes[0] = ATA_DEV_UNKNOWN;
718
Tejun Heocf480622008-01-24 00:05:14 +0900719 if (ehc->i.action & ATA_EH_RESET) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900720 struct ata_link *tlink;
721
Tejun Heo3af9a772007-09-23 13:19:54 +0900722 /* reset */
Tejun Heo3af9a772007-09-23 13:19:54 +0900723 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
724 postreset);
725 if (rc) {
726 ata_link_printk(link, KERN_ERR,
727 "failed to reset PMP, giving up\n");
728 goto fail;
729 }
730
Tejun Heo3af9a772007-09-23 13:19:54 +0900731 /* PMP is reset, SErrors cannot be trusted, scan all */
Tejun Heo1eca4362008-11-03 20:03:17 +0900732 ata_for_each_link(tlink, ap, EDGE) {
Tejun Heob558edd2008-01-24 00:05:14 +0900733 struct ata_eh_context *ehc = &tlink->eh_context;
734
735 ehc->i.probe_mask |= ATA_ALL_DEVICES;
736 ehc->i.action |= ATA_EH_RESET;
737 }
Tejun Heo3af9a772007-09-23 13:19:54 +0900738 }
739
740 /* If revalidation is requested, revalidate and reconfigure;
741 * otherwise, do quick revalidation.
742 */
743 if (ehc->i.action & ATA_EH_REVALIDATE)
744 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
745 else
746 rc = sata_pmp_revalidate_quick(dev);
747
748 if (rc) {
749 tries--;
750
751 if (rc == -ENODEV) {
Tejun Heob558edd2008-01-24 00:05:14 +0900752 ehc->i.probe_mask |= ATA_ALL_DEVICES;
Tejun Heo3af9a772007-09-23 13:19:54 +0900753 detach = 1;
754 /* give it just two more chances */
755 tries = min(tries, 2);
756 }
757
758 if (tries) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900759 /* consecutive revalidation failures? speed down */
760 if (reval_failed)
Tejun Heoa07d4992009-01-29 20:31:33 +0900761 sata_down_spd_limit(link, 0);
Tejun Heo3af9a772007-09-23 13:19:54 +0900762 else
763 reval_failed = 1;
764
Tejun Heocf480622008-01-24 00:05:14 +0900765 ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +0900766 goto retry;
767 } else {
768 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
769 "after %d tries, giving up\n",
770 ATA_EH_PMP_TRIES);
771 goto fail;
772 }
773 }
774
775 /* okay, PMP resurrected */
776 ehc->i.flags = 0;
777
778 DPRINTK("EXIT, rc=0\n");
779 return 0;
780
781 fail:
782 sata_pmp_detach(dev);
783 if (detach)
784 ata_eh_detach_dev(dev);
785 else
786 ata_dev_disable(dev);
787
788 DPRINTK("EXIT, rc=%d\n", rc);
789 return rc;
790}
791
792static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
793{
794 struct ata_link *link;
795 unsigned long flags;
796 int rc;
797
798 spin_lock_irqsave(ap->lock, flags);
799
Tejun Heo1eca4362008-11-03 20:03:17 +0900800 ata_for_each_link(link, ap, EDGE) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900801 if (!(link->flags & ATA_LFLAG_DISABLED))
802 continue;
803
804 spin_unlock_irqrestore(ap->lock, flags);
805
806 /* Some PMPs require hardreset sequence to get
807 * SError.N working.
808 */
Tejun Heocf480622008-01-24 00:05:14 +0900809 sata_link_hardreset(link, sata_deb_timing_normal,
Tejun Heo341c2c92008-05-20 02:17:51 +0900810 ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK),
811 NULL, NULL);
Tejun Heo3af9a772007-09-23 13:19:54 +0900812
813 /* unconditionally clear SError.N */
814 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
815 if (rc) {
816 ata_link_printk(link, KERN_ERR, "failed to clear "
817 "SError.N (errno=%d)\n", rc);
818 return rc;
819 }
820
821 spin_lock_irqsave(ap->lock, flags);
822 }
823
824 spin_unlock_irqrestore(ap->lock, flags);
825
826 return 0;
827}
828
829static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
830{
831 struct ata_port *ap = link->ap;
832 unsigned long flags;
833
834 if (link_tries[link->pmp] && --link_tries[link->pmp])
835 return 1;
836
837 /* disable this link */
838 if (!(link->flags & ATA_LFLAG_DISABLED)) {
839 ata_link_printk(link, KERN_WARNING,
840 "failed to recover link after %d tries, disabling\n",
841 ATA_EH_PMP_LINK_TRIES);
842
843 spin_lock_irqsave(ap->lock, flags);
844 link->flags |= ATA_LFLAG_DISABLED;
845 spin_unlock_irqrestore(ap->lock, flags);
846 }
847
848 ata_dev_disable(link->device);
849 link->eh_context.i.action = 0;
850
851 return 0;
852}
853
854/**
855 * sata_pmp_eh_recover - recover PMP-enabled port
856 * @ap: ATA port to recover
Tejun Heo3af9a772007-09-23 13:19:54 +0900857 *
858 * Drive EH recovery operation for PMP enabled port @ap. This
859 * function recovers host and PMP ports with proper retrials and
860 * fallbacks. Actual recovery operations are performed using
861 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
862 *
863 * LOCKING:
864 * Kernel thread context (may sleep).
865 *
866 * RETURNS:
867 * 0 on success, -errno on failure.
868 */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900869static int sata_pmp_eh_recover(struct ata_port *ap)
Tejun Heo3af9a772007-09-23 13:19:54 +0900870{
Tejun Heoa1efdab2008-03-25 12:22:50 +0900871 struct ata_port_operations *ops = ap->ops;
Tejun Heo3af9a772007-09-23 13:19:54 +0900872 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
873 struct ata_link *pmp_link = &ap->link;
874 struct ata_device *pmp_dev = pmp_link->device;
875 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
Tejun Heof1bbfb92008-05-19 01:15:11 +0900876 u32 *gscr = pmp_dev->gscr;
Tejun Heo3af9a772007-09-23 13:19:54 +0900877 struct ata_link *link;
878 struct ata_device *dev;
Tejun Heob06ce3e2007-10-09 15:06:48 +0900879 unsigned int err_mask;
Tejun Heo3af9a772007-09-23 13:19:54 +0900880 u32 gscr_error, sntf;
881 int cnt, rc;
882
883 pmp_tries = ATA_EH_PMP_TRIES;
Tejun Heo1eca4362008-11-03 20:03:17 +0900884 ata_for_each_link(link, ap, EDGE)
Tejun Heo3af9a772007-09-23 13:19:54 +0900885 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
886
887 retry:
888 /* PMP attached? */
Tejun Heo071f44b2008-04-07 22:47:22 +0900889 if (!sata_pmp_attached(ap)) {
Tejun Heoa1efdab2008-03-25 12:22:50 +0900890 rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
891 ops->hardreset, ops->postreset, NULL);
Tejun Heo3af9a772007-09-23 13:19:54 +0900892 if (rc) {
Tejun Heo1eca4362008-11-03 20:03:17 +0900893 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heo3af9a772007-09-23 13:19:54 +0900894 ata_dev_disable(dev);
895 return rc;
896 }
897
898 if (pmp_dev->class != ATA_DEV_PMP)
899 return 0;
900
901 /* new PMP online */
Tejun Heo1eca4362008-11-03 20:03:17 +0900902 ata_for_each_link(link, ap, EDGE)
Tejun Heo3af9a772007-09-23 13:19:54 +0900903 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
904
905 /* fall through */
906 }
907
908 /* recover pmp */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900909 rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
910 ops->hardreset, ops->postreset);
Tejun Heo3af9a772007-09-23 13:19:54 +0900911 if (rc)
912 goto pmp_fail;
913
Tejun Heof1bbfb92008-05-19 01:15:11 +0900914 /* PHY event notification can disturb reset and other recovery
915 * operations. Turn it off.
916 */
917 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
918 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
919
920 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
921 gscr[SATA_PMP_GSCR_FEAT_EN]);
922 if (err_mask) {
923 ata_link_printk(pmp_link, KERN_WARNING,
924 "failed to disable NOTIFY (err_mask=0x%x)\n",
925 err_mask);
926 goto pmp_fail;
927 }
928 }
929
Tejun Heo3af9a772007-09-23 13:19:54 +0900930 /* handle disabled links */
931 rc = sata_pmp_eh_handle_disabled_links(ap);
932 if (rc)
933 goto pmp_fail;
934
935 /* recover links */
Tejun Heoa1efdab2008-03-25 12:22:50 +0900936 rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
937 ops->pmp_hardreset, ops->pmp_postreset, &link);
Tejun Heo3af9a772007-09-23 13:19:54 +0900938 if (rc)
939 goto link_fail;
940
941 /* Connection status might have changed while resetting other
942 * links, check SATA_PMP_GSCR_ERROR before returning.
943 */
944
945 /* clear SNotification */
946 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
947 if (rc == 0)
948 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
949
950 /* enable notification */
951 if (pmp_dev->flags & ATA_DFLAG_AN) {
Tejun Heof1bbfb92008-05-19 01:15:11 +0900952 gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
Tejun Heo3af9a772007-09-23 13:19:54 +0900953
Tejun Heof1bbfb92008-05-19 01:15:11 +0900954 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
955 gscr[SATA_PMP_GSCR_FEAT_EN]);
Tejun Heob06ce3e2007-10-09 15:06:48 +0900956 if (err_mask) {
957 ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
958 "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
959 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900960 goto pmp_fail;
961 }
962 }
963
964 /* check GSCR_ERROR */
Tejun Heob06ce3e2007-10-09 15:06:48 +0900965 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
966 if (err_mask) {
967 ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
968 "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
969 rc = -EIO;
Tejun Heo3af9a772007-09-23 13:19:54 +0900970 goto pmp_fail;
971 }
972
973 cnt = 0;
Tejun Heo1eca4362008-11-03 20:03:17 +0900974 ata_for_each_link(link, ap, EDGE) {
Tejun Heo3af9a772007-09-23 13:19:54 +0900975 if (!(gscr_error & (1 << link->pmp)))
976 continue;
977
978 if (sata_pmp_handle_link_fail(link, link_tries)) {
979 ata_ehi_hotplugged(&link->eh_context.i);
980 cnt++;
981 } else {
982 ata_link_printk(link, KERN_WARNING,
983 "PHY status changed but maxed out on retries, "
984 "giving up\n");
985 ata_link_printk(link, KERN_WARNING,
986 "Manully issue scan to resume this link\n");
987 }
988 }
989
990 if (cnt) {
991 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
992 "ports, repeating recovery\n");
993 goto retry;
994 }
995
996 return 0;
997
998 link_fail:
999 if (sata_pmp_handle_link_fail(link, link_tries)) {
Tejun Heocf480622008-01-24 00:05:14 +09001000 pmp_ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +09001001 goto retry;
1002 }
1003
1004 /* fall through */
1005 pmp_fail:
1006 /* Control always ends up here after detaching PMP. Shut up
1007 * and return if we're unloading.
1008 */
1009 if (ap->pflags & ATA_PFLAG_UNLOADING)
1010 return rc;
1011
Tejun Heo071f44b2008-04-07 22:47:22 +09001012 if (!sata_pmp_attached(ap))
Tejun Heo3af9a772007-09-23 13:19:54 +09001013 goto retry;
1014
1015 if (--pmp_tries) {
Tejun Heocf480622008-01-24 00:05:14 +09001016 pmp_ehc->i.action |= ATA_EH_RESET;
Tejun Heo3af9a772007-09-23 13:19:54 +09001017 goto retry;
1018 }
1019
1020 ata_port_printk(ap, KERN_ERR,
1021 "failed to recover PMP after %d tries, giving up\n",
1022 ATA_EH_PMP_TRIES);
1023 sata_pmp_detach(pmp_dev);
1024 ata_dev_disable(pmp_dev);
1025
1026 return rc;
1027}
1028
1029/**
Tejun Heoa1efdab2008-03-25 12:22:50 +09001030 * sata_pmp_error_handler - do standard error handling for PMP-enabled host
Tejun Heo3af9a772007-09-23 13:19:54 +09001031 * @ap: host port to handle error for
Tejun Heo3af9a772007-09-23 13:19:54 +09001032 *
1033 * Perform standard error handling sequence for PMP-enabled host
1034 * @ap.
1035 *
1036 * LOCKING:
1037 * Kernel thread context (may sleep).
1038 */
Tejun Heoa1efdab2008-03-25 12:22:50 +09001039void sata_pmp_error_handler(struct ata_port *ap)
Tejun Heo3af9a772007-09-23 13:19:54 +09001040{
1041 ata_eh_autopsy(ap);
1042 ata_eh_report(ap);
Tejun Heoa1efdab2008-03-25 12:22:50 +09001043 sata_pmp_eh_recover(ap);
Tejun Heo3af9a772007-09-23 13:19:54 +09001044 ata_eh_finish(ap);
1045}
Tejun Heo48515f62008-04-07 22:47:21 +09001046
1047EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
1048EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
1049EXPORT_SYMBOL_GPL(sata_pmp_error_handler);