source: trunk/src/os2ahci/ahci.c@ 208

Last change on this file since 208 was 207, checked in by David Azarewicz, 4 years ago

Add 64/48 bit LBA support.

File size: 64.2 KB
Line 
1/**
2 * ahci.c - ahci hardware access functions
3 *
4 * Copyright (c) 2011 thi.guten Software Development
5 * Copyright (c) 2011 Mensys B.V.
6 * Copyright (c) 2013-2021 David Azarewicz <david@88watts.net>
7 *
8 * Authors: Christian Mueller, Markus Thielen
9 *
10 * Parts copied from/inspired by the Linux AHCI driver;
11 * those parts are (c) Linux AHCI/ATA maintainers
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include "os2ahci.h"
29#include "ata.h"
30#include "atapi.h"
31
32/* produce ata/atapi function pointer with the given func name */
33#define cmd_func(iorb, func) ad_infos[iorb_unit_adapter(iorb)]. \
34 ports[iorb_unit_port(iorb)]. \
35 devs[iorb_unit_device(iorb)].atapi \
36 ? atapi_##func : ata_##func
37
38
39/* Initial driver status flags indexed by the board_* constants in os2ahci.h
40 *
41 * NOTE: The Linux AHCI driver uses a combination of board-specific quirk
42 * flags and overriding certain libata service functions to handle
43 * adapter flaws. However, there were only three overrides at the time
44 * os2ahci was written, one for hard adapter resets and two for port
45 * resets, and we can easily implement those within the corresponding
46 * reset handlers. If this becomes more complex, this array of flags
47 * should be converted into a structure array which contains function
48 * pointers to all handler functions which may need to be overridden.
49 */
50u16 initial_flags[] =
51{
52 0, /* board_ahci */
53 AHCI_HFLAG_NO_NCQ | /* board_ahci_vt8251 */
54 AHCI_HFLAG_NO_PMP,
55 AHCI_HFLAG_IGN_IRQ_IF_ERR, /* board_ahci_ign_iferr */
56 AHCI_HFLAG_IGN_SERR_INTERNAL | /* board_ahci_sb600 */
57 AHCI_HFLAG_NO_MSI |
58 AHCI_HFLAG_SECT255 |
59 AHCI_HFLAG_32BIT_ONLY,
60 AHCI_HFLAG_NO_NCQ | /* board_ahci_mv */
61 AHCI_HFLAG_NO_MSI |
62 AHCI_HFLAG_MV_PATA |
63 AHCI_HFLAG_NO_PMP,
64 AHCI_HFLAG_IGN_SERR_INTERNAL, /* board_ahci_sb700 */
65 AHCI_HFLAG_YES_NCQ, /* board_ahci_mcp65 */
66 AHCI_HFLAG_NO_PMP, /* board_ahci_nopmp */
67 AHCI_HFLAG_YES_NCQ, /* board_ahci_yesncq */
68 AHCI_HFLAG_NO_SNTF, /* board_ahci_nosntf */
69};
70
71#define MAX_IRQ_HANDLERS 8 /* This is the maximum number of handlers that Dev32Help_SetIRQ can register */
72static u16 irq_used[MAX_IRQ_HANDLERS]; /* IRQ level for each used IRQ */
73static int irq_used_cnt; /* number of IRQs used */
74
75#ifdef DEBUG
76void ahci_dump_host_regs(AD_INFO *ai, int bios_regs)
77{
78 int i;
79 u32 version;
80
81 dprintf(0,"AHCI global registers for adapter %d %d:%d:%d irq=%d addr=0x%x\n",
82 ad_no(ai),
83 PCI_BUS_FROM_BDF(ai->bus_dev_func), PCI_DEV_FROM_BDF(ai->bus_dev_func),
84 PCI_FUNC_FROM_BDF(ai->bus_dev_func), ai->irq, ai->mmio_phys);
85
86 for (i = 0; i <= HOST_CAP2; i += sizeof(u32))
87 {
88 u32 val;
89
90 if (bios_regs) val = ai->bios_config[i/sizeof(u32)];
91 else
92 {
93 /* HOST_CAP2 only exists for AHCI V1.2 and later */
94 if ((i == HOST_CAP2) && (version < 0x00010200L)) val = 0;
95 else val = readl(ai->mmio + i);
96 }
97 if (i == HOST_VERSION) version = val;
98
99 dprintf(0," %02x: %08lx", i, val);
100
101 if (i == HOST_CAP)
102 {
103 dprintf(0," -");
104 if (val & HOST_CAP_64) dprintf(0," 64bit");
105 if (val & HOST_CAP_NCQ) dprintf(0," ncq");
106 if (val & HOST_CAP_SNTF) dprintf(0," sntf");
107 if (val & HOST_CAP_MPS) dprintf(0," mps");
108 if (val & HOST_CAP_SSS) dprintf(0," sss");
109 if (val & HOST_CAP_ALPM) dprintf(0," alpm");
110 if (val & HOST_CAP_LED) dprintf(0," led");
111 if (val & HOST_CAP_CLO) dprintf(0," clo");
112 if (val & HOST_CAP_ONLY) dprintf(0," ahci_only");
113 if (val & HOST_CAP_PMP) dprintf(0," pmp");
114 if (val & HOST_CAP_FBS) dprintf(0," fbs");
115 if (val & HOST_CAP_PIO_MULTI) dprintf(0," pio_multi");
116 if (val & HOST_CAP_SSC) dprintf(0," ssc");
117 if (val & HOST_CAP_PART) dprintf(0," part");
118 if (val & HOST_CAP_CCC) dprintf(0," ccc");
119 if (val & HOST_CAP_EMS) dprintf(0," ems");
120 if (val & HOST_CAP_SXS) dprintf(0," sxs");
121 dprintf(0," cmd_slots:%d", ((val >> 8) & 0x1f) + 1);
122 dprintf(0," ports:%d", (val & 0x1f) + 1);
123 }
124 else if (i == HOST_CTL)
125 {
126 dprintf(0," -");
127 if (val & HOST_AHCI_EN) dprintf(0," ahci_enabled");
128 if (val & HOST_IRQ_EN) dprintf(0," irq_enabled");
129 if (val & HOST_RESET) dprintf(0," resetting");
130 }
131 else if (i == HOST_CAP2)
132 {
133 dprintf(0," -");
134 if (val & HOST_CAP2_BOH) dprintf(0," boh");
135 if (val & HOST_CAP2_NVMHCI) dprintf(0," nvmhci");
136 if (val & HOST_CAP2_APST) dprintf(0," apst");
137 }
138 dprintf(0,"\n");
139 }
140}
141
142void ahci_dump_port_regs(AD_INFO *ai, int p)
143{
144 u8 *port_mmio = port_base(ai, p);
145
146 dprintf(0,"AHCI port %d registers:\n", p);
147 dprintf(0," PORT_CMD = 0x%x\n", readl(port_mmio + PORT_CMD));
148 dprintf(0," command engine status:\n");
149 dprintf(0," PORT_SCR_ACT = 0x%x\n", readl(port_mmio + PORT_SCR_ACT));
150 dprintf(0," PORT_CMD_ISSUE = 0x%x\n", readl(port_mmio + PORT_CMD_ISSUE));
151 dprintf(0," link/device status:\n");
152 dprintf(0," PORT_SCR_STAT = 0x%x\n", readl(port_mmio + PORT_SCR_STAT));
153 dprintf(0," PORT_SCR_CTL = 0x%x\n", readl(port_mmio + PORT_SCR_CTL));
154 dprintf(0," PORT_SCR_ERR = 0x%x\n", readl(port_mmio + PORT_SCR_ERR));
155 dprintf(0," PORT_TFDATA = 0x%x\n", readl(port_mmio + PORT_TFDATA));
156 dprintf(0," interrupt status:\n");
157 dprintf(0," PORT_IRQ_STAT = 0x%x\n", readl(port_mmio + PORT_IRQ_STAT));
158 dprintf(0," PORT_IRQ_MASK = 0x%x\n", readl(port_mmio + PORT_IRQ_MASK));
159 dprintf(0," HOST_IRQ_STAT = 0x%x\n", readl(ai->mmio + HOST_IRQ_STAT));
160}
161#endif
162
163/******************************************************************************
164 * setup the CAPS and other adapter information for this adapter.
165 *
166 * This function saves working copies of the CAP and CAP2 registers
167 * as well as the initial port map in the AD_INFO structure after
168 * removing features which are known to cause trouble on this specific
169 * piece of hardware.
170 */
171int ahci_config_caps(AD_INFO *ai)
172{
173 int ports;
174 int i;
175
176 ai->cap = readl(ai->mmio + HOST_CAP);
177 ai->port_map = readl(ai->mmio + HOST_PORTS_IMPL);
178
179 /* HOST_CAP2 only exists for AHCI V1.2 and later */
180 if (readl(ai->mmio + HOST_VERSION) >= 0x00010200L) ai->cap2 = readl(ai->mmio + HOST_CAP2);
181
182 if (ai->pci->board >= sizeof(initial_flags) / sizeof(*initial_flags))
183 {
184 dprintf(0,"error: invalid board index in PCI info\n");
185 return(-1);
186 }
187 ai->flags = initial_flags[ai->pci->board];
188 ai->hw_ports = (ai->cap & 0x1f) + 1;
189
190 if ((ai->cap & HOST_CAP_64) && (ai->flags & AHCI_HFLAG_32BIT_ONLY))
191 {
192 /* disable 64-bit support for faulty controllers; OS/2 can't do 64 bits at
193 * this point, of course, but who knows where all this will be in a few
194 * years...
195 */
196 ai->cap &= ~HOST_CAP_64;
197 }
198
199 /* Remove broken feature bits. This is largely copied from the Linux AHCI driver -- the wisdom
200 * around quirks and faulty hardware is hard to come by...
201 */
202 if ((ai->cap & HOST_CAP_NCQ) && (ai->flags & AHCI_HFLAG_NO_NCQ))
203 {
204 DPRINTF(1,"controller can't do NCQ, turning off CAP_NCQ\n");
205 ai->cap &= ~HOST_CAP_NCQ;
206 }
207
208 if (!(ai->cap & HOST_CAP_NCQ) && (ai->flags & AHCI_HFLAG_YES_NCQ))
209 {
210 DPRINTF(1,"controller can do NCQ, turning on CAP_NCQ\n");
211 ai->cap |= HOST_CAP_NCQ;
212 }
213
214 if ((ai->cap & HOST_CAP_PMP) && (ai->flags & AHCI_HFLAG_NO_PMP))
215 {
216 DPRINTF(1,"controller can't do PMP, turning off CAP_PMP\n");
217 ai->cap |= HOST_CAP_PMP;
218 }
219
220 if ((ai->cap & HOST_CAP_SNTF) && (ai->flags & AHCI_HFLAG_NO_SNTF))
221 {
222 DPRINTF(1,"controller can't do SNTF, turning off CAP_SNTF\n");
223 ai->cap &= ~HOST_CAP_SNTF;
224 }
225
226 if (ai->pci_vendor == PCI_VENDOR_ID_JMICRON && ai->pci_device == 0x2361 && ai->port_map != 1)
227 {
228 DPRINTF(1,"JMB361 has only one port, port_map 0x%x -> 0x%x\n", ai->port_map, 1);
229 ai->port_map = 1;
230 ai->hw_ports = 1;
231 }
232
233 /* Correlate port map to number of ports reported in HOST_CAP
234 *
235 * NOTE: Port map and number of ports handling differs a bit from the
236 * Linux AHCI driver because we're storing both in AI_INFO. As in the
237 * Linux driver, the port map is the main driver for port scanning but
238 * we're also saving a maximum port number in AI_INFO to reduce the
239 * number of IORB queues to look at in trigger_engine(). This is done
240 * in ahci_scan_ports().
241 */
242 ports = ai->hw_ports;
243 for (i = 0; i < AHCI_MAX_PORTS; i++)
244 {
245 if (ai->port_map & (1UL << i)) ports--;
246 }
247 if (ports < 0)
248 {
249 /* more ports in port_map than in HOST_CAP & 0x1f */
250 ports = ai->hw_ports;
251 DPRINTF(0,"implemented port map (0x%x) contains more ports than hw_ports (%d), using hw_ports\n", ai->port_map, ports);
252 ai->port_map = (1UL << ports) - 1UL;
253 }
254
255 /* set maximum command slot number */
256 ai->cmd_max = ((ai->cap >> 8) & 0x1f);
257
258 return(0);
259}
260
261/******************************************************************************
262 * Save BIOS configuration of AHCI adapter. As a side effect, this also saves
263 * generic configuration information which we may have to restore after an
264 * adapter reset.
265 */
266int ahci_save_bios_config(AD_INFO *ai)
267{
268 int i;
269
270 /* save BIOS configuration */
271 for (i = 0; i < HOST_CAP2; i += sizeof(u32))
272 {
273 ai->bios_config[i / sizeof(u32)] = readl(ai->mmio + i);
274 }
275
276 DPRINTF(3,__func__": BIOS AHCI mode is %d\n", ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN);
277
278 if ((ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN) == 0 && ai->pci_vendor == PCI_VENDOR_ID_INTEL)
279 {
280 /* Adapter is not in AHCI mode and the spec says a COMRESET is
281 * required when switching from SATA to AHCI mode and vice versa.
282 */
283 init_reset = 1;
284 }
285
286 DUMP_HOST_REGS(6,ai,1);
287
288 return(0);
289}
290
291/******************************************************************************
292 * Restore BIOS configuration of AHCI adapter. This is needed after scanning
293 * for devices because we still need the BIOS until the initial boot sequence
294 * has completed.
295 */
296int ahci_restore_bios_config(AD_INFO *ai)
297{
298 DPRINTF(3,__func__": restoring AHCI BIOS configuration on adapter %d\n", ad_no(ai));
299
300 /* Restore saved BIOS configuration; please note that HOST_CTL is restored
301 * last because it may cause AHCI mode to be turned off again.
302 */
303 writel(ai->mmio + HOST_CCC, ai->bios_config[HOST_CCC / sizeof(u32)]);
304 writel(ai->mmio + HOST_CCC_PORTS, ai->bios_config[HOST_CCC_PORTS / sizeof(u32)]);
305 writel(ai->mmio + HOST_EM_CTL, ai->bios_config[HOST_EM_CTL / sizeof(u32)]);
306 writel(ai->mmio + HOST_CTL, ai->bios_config[HOST_CTL / sizeof(u32)]);
307
308 /* flush PCI MMIO delayed write buffers */
309 readl(ai->mmio + HOST_CTL);
310
311 if ((ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN) == 0 && ai->pci_vendor == PCI_VENDOR_ID_INTEL)
312 {
313 /* This BIOS apparently accesses the controller via SATA registers and
314 * the AHCI spec says that we should issue a COMRESET on each port after
315 * disabling AHCI mode to allow the SATA controller to re-recognize attached
316 * devices. How to do this depends on the controller, of course, but so
317 * far I've only seen Dell notebook BIOSs with Intel chipsets to behave
318 * like this; all other BIOS implementations I've seen so far seem to take
319 * AHCI mode literally and operate the controller in AHCI mode from the
320 * beginning.
321 *
322 * We'll use a feature on Intel ICH7/8 controllers which provides MMIO
323 * mappings for the AHCI SCR registers even when not in AHCI mode.
324 */
325 int p;
326
327 for (p = 0; p <= ai->port_max; p++)
328 {
329 if (ai->port_map & (1UL << p))
330 {
331 u8 *port_mmio = port_base(ai, p);
332 u32 tmp;
333
334 tmp = readl(port_mmio + PORT_SCR_CTL) & ~0x0000000fUL;
335 writel(port_mmio + PORT_SCR_CTL, tmp | 1);
336 readl(port_mmio + PORT_SCR_CTL); /* flush */
337
338 /* spec says "leave reset bit on for at least 1ms"; make it 2ms */
339 udelay(2000);
340
341 writel(port_mmio + PORT_SCR_CTL, tmp);
342 readl(port_mmio + PORT_SCR_CTL); /* flush */
343 }
344 }
345 }
346
347 return(0);
348}
349
350/******************************************************************************
351 * Restore initial configuration (e.g. after an adapter reset). This relies
352 * on information saved by 'ahci_save_bios_config()'.
353 */
354int ahci_restore_initial_config(AD_INFO *ai)
355{
356 DPRINTF(3,__func__": restoring initial configuration on adapter %d\n", ad_no(ai));
357
358 /* restore saved BIOS configuration */
359 //writel(ai->mmio + HOST_CCC, ai->bios_config[HOST_CCC / sizeof(u32)]);
360 //writel(ai->mmio + HOST_CCC_PORTS, ai->bios_config[HOST_CCC_PORTS / sizeof(u32)]);
361 //writel(ai->mmio + HOST_EM_CTL, ai->bios_config[HOST_EM_CTL / sizeof(u32)]);
362 //writel(ai->mmio + HOST_CTL, ai->bios_config[HOST_CTL / sizeof(u32)]);
363
364 writel(ai->mmio + HOST_CAP, ai->bios_config[HOST_CAP / sizeof(u32)]);
365 if (ai->bios_config[HOST_CAP2 / sizeof(u32)])
366 writel(ai->mmio + HOST_CAP2, ai->bios_config[HOST_CAP2 / sizeof(u32)]);
367 writel(ai->mmio + HOST_PORTS_IMPL, ai->bios_config[HOST_PORTS_IMPL / sizeof(u32)]);
368
369 /* flush PCI MMIO delayed write buffers */
370 readl(ai->mmio + HOST_PORTS_IMPL);
371
372 return(0);
373}
374
375#ifdef NOT_USED
376int ahci_reset_controller(AD_INFO *ai)
377{
378 u32 tmp;
379 TIMER Timer;
380
381 DPRINTF(2,"controller reset starting on adapter %d\n", ad_no(ai));
382
383 /* we must be in AHCI mode, before using anything AHCI-specific, such as HOST_RESET. */
384 ahci_enable_ahci(ai);
385
386 /* global controller reset */
387 tmp = readl(ai->mmio + HOST_CTL);
388 if ((tmp & HOST_RESET) == 0) {
389 writel(ai->mmio + HOST_CTL, tmp | HOST_RESET);
390 readl(ai->mmio + HOST_CTL); /* flush */
391 }
392
393 /*
394 * to perform host reset, OS should set HOST_RESET
395 * and poll until this bit is read to be "0".
396 * reset must complete within 1 second, or
397 * the hardware should be considered fried.
398 */
399 TimerInit(&Timer, 1000);
400 while (((tmp = readl(ai->mmio + HOST_CTL)) & HOST_RESET) != 0) {
401 if (TimerCheckAndBlock(&Timer)) {
402 dprintf(0,"controller reset failed (0x%x)\n", tmp);
403 return(-1);
404 }
405 }
406
407 /* turn on AHCI mode */
408 ahci_enable_ahci(ai);
409
410 /* Some registers might be cleared on reset. Restore initial values. */
411 ahci_restore_initial_config(ai);
412
413 if (ai->pci_vendor == PCI_VENDOR_ID_INTEL) {
414 u32 tmp16 = 0;
415
416 DPRINTF(1,"ahci_reset_controller: intel detected\n");
417 /* configure PCS */
418 PciReadConfig(ai->bus, ai->dev_func, 0x92, sizeof(u16), &tmp16);
419 if ((tmp16 & ai->port_map) != ai->port_map) {
420 DPRINTF(3,"ahci_reset_controller: updating PCS %x/%x\n", tmp16, ai->port_map);
421 tmp16 |= ai->port_map;
422 PciWriteConfig(ai->bus, ai->dev_func, 0x92, sizeof(u16), tmp16);
423 }
424 }
425
426 return 0;
427}
428#endif
429
430/******************************************************************************
431 * Save port configuration. This is primarily used to save the BIOS port
432 * configuration (command list and FIS buffers and the IRQ mask).
433 *
434 * The port configuration returned by this function is dynamically allocated
435 * and automatically freed when calling ahci_restore_port_config().
436 */
437AHCI_PORT_CFG *ahci_save_port_config(AD_INFO *ai, int p)
438{
439 AHCI_PORT_CFG *pc;
440 u8 *port_mmio = port_base(ai, p);
441
442 if ((pc = MemAlloc(sizeof(*pc))) == NULL) return(NULL);
443
444 pc->cmd_list = readl(port_mmio + PORT_LST_ADDR);
445 pc->cmd_list_h = readl(port_mmio + PORT_LST_ADDR_HI);
446 pc->fis_rx = readl(port_mmio + PORT_FIS_ADDR);
447 pc->fis_rx_h = readl(port_mmio + PORT_FIS_ADDR_HI);
448 pc->irq_mask = readl(port_mmio + PORT_IRQ_MASK);
449 pc->port_cmd = readl(port_mmio + PORT_CMD);
450
451 return(pc);
452}
453
454/******************************************************************************
455 * Restore port configuration. This is primarily used to restore the BIOS port
456 * configuration (command list and FIS buffers and the IRQ mask).
457 *
458 * The port configuration is automatically freed.
459 */
460void ahci_restore_port_config(AD_INFO *ai, int p, AHCI_PORT_CFG *pc)
461{
462 u8 *port_mmio = port_base(ai, p);
463
464 /* stop the port, first */
465 ahci_stop_port(ai, p);
466
467 if (ai->bios_config[HOST_CTL / sizeof(u32)] & HOST_AHCI_EN)
468 {
469 /* BIOS uses AHCI, too, so we need to restore the port settings;
470 * restoring PORT_CMD may well start the port again but that's what
471 * this function is all about.
472 */
473 writel(port_mmio + PORT_LST_ADDR, pc->cmd_list);
474 writel(port_mmio + PORT_LST_ADDR_HI, pc->cmd_list_h);
475 writel(port_mmio + PORT_FIS_ADDR, pc->fis_rx);
476 writel(port_mmio + PORT_FIS_ADDR_HI, pc->fis_rx_h);
477 writel(port_mmio + PORT_IRQ_MASK, pc->irq_mask);
478 writel(port_mmio + PORT_CMD, pc->port_cmd);
479
480 readl(port_base(ai, p) + PORT_IRQ_MASK); /* flush */
481 }
482
483 MemFree(pc);
484}
485
486/******************************************************************************
487 * Enable AHCI mode on this controller.
488 */
489int ahci_enable_ahci(AD_INFO *ai)
490{
491 u32 ctl = readl(ai->mmio + HOST_CTL);
492 int i;
493
494 if (ctl & HOST_AHCI_EN)
495 {
496 /* AHCI mode already enabled */
497 return(0);
498 }
499
500 if (ai->pci_vendor == PCI_VENDOR_ID_INTEL)
501 {
502 /* Adapter is not in AHCI mode and the spec says a COMRESET is
503 * required when switching from SATA to AHCI mode and vice versa.
504 */
505 init_reset = 1;
506 }
507
508 /* some controllers need AHCI_EN to be written multiple times */
509 for (i = 0; i < 5; i++)
510 {
511 ctl |= HOST_AHCI_EN;
512 writel(ai->mmio + HOST_CTL, ctl);
513 ctl = readl(ai->mmio + HOST_CTL); /* flush && sanity check */
514 if (ctl & HOST_AHCI_EN)
515 {
516 return(0);
517 }
518 msleep(10);
519 }
520
521 /* couldn't enable AHCI mode */
522 dprintf(0,"failed to enable AHCI mode on adapter %d\n", ad_no(ai));
523 return(1);
524}
525
526/******************************************************************************
527 * Complete initialization of adapter. This includes restarting all active
528 * ports and initializing interrupt processing. This is called when receiving
529 * the IOCM_COMPLETE_INIT request.
530 */
531int ahci_complete_init(AD_INFO *ai)
532{
533 int rc;
534 u32 p;
535 int i;
536
537 DPRINTF(2,__func__": completing initialization of adapter #%d\n", ad_no(ai));
538
539 if (!ai->int_set)
540 {
541 /* register IRQ handler; each IRQ level is registered only once */
542 p = 1; /* int count */
543 if (!(ai->flags & AHCI_HFLAG_NO_MSI))
544 {
545 if (PsdMsiAlloc(ai->bus_dev_func, &p, &ai->irq)) p = 1; /* shared flag */
546 else
547 {
548 /* we have an msi interrupt */
549 ai->irq_pin = 0;
550 p = 0; /* exclusive flag */
551 }
552 }
553 for (i = 0; i < irq_used_cnt; i++)
554 {
555 if (irq_used[i] == ai->irq) break; /* we already have this IRQ registered */
556 }
557 if (i >= irq_used_cnt)
558 {
559 if (i >= MAX_IRQ_HANDLERS) return -1; /* no more handlers available */
560
561 DPRINTF(2,"registering interrupt %d pin=%d\n", ai->irq, ai->irq_pin);
562
563 rc = Dev32Help_SetIRQ(ahci_intr, ai->irq, p, ai->irq);
564 if (rc && p) /* if failed and was shared */
565 {
566 p = 0; /* try exclusive */
567 rc = Dev32Help_SetIRQ(ahci_intr, ai->irq, p, ai->irq);
568 }
569 if (rc)
570 {
571 dprintf(0,"failed to register interrupt %d\n", ai->irq);
572 return(-1);
573 }
574 irq_used[irq_used_cnt++] = ai->irq;
575 ai->int_set = 1;
576 RmUpdateAddIrq(rm_drvh, ai->rm_adh, ai->irq, ai->irq_pin, p?RS_IRQ_SHARED:RS_IRQ_EXCLUSIVE);
577 }
578 }
579
580 /* enable AHCI mode */
581 if ((rc = ahci_enable_ahci(ai)) != 0) return(rc);
582
583 /* Start all ports. The main purpose is to set the command list and FIS
584 * receive area addresses properly and to enable port-level interrupts; we
585 * don't really care about the return status because we'll find out soon
586 * enough if a previously detected device has problems.
587 */
588 for (p = 0; p <= ai->port_max; p++)
589 {
590 if (ai->port_map & (1UL << p))
591 {
592 if (init_reset)
593 {
594 DPRINTF(3,__func__": resetting port %d\n", p);
595 ahci_reset_port(ai, p, 1);
596 }
597 else
598 {
599 DPRINTF(3,__func__": restarting port #%d\n", p);
600 ahci_stop_port(ai, p);
601 ahci_start_port(ai, p, 1);
602 }
603 }
604 }
605
606 /* clear pending interrupt status */
607 writel(ai->mmio + HOST_IRQ_STAT, readl(ai->mmio + HOST_IRQ_STAT));
608 readl(ai->mmio + HOST_IRQ_STAT); /* flush */
609
610 /* enable adapter-level interrupts */
611 writel(ai->mmio + HOST_CTL, readl(ai->mmio + HOST_CTL) | HOST_IRQ_EN);
612 readl(ai->mmio + HOST_CTL); /* flush */
613
614 /* enable interrupts on PCI-level (PCI 2.3 added a feature to disable INTs) */
615 /* pci_enable_int(ai->bus, ai->dev_func); */
616
617 DPRINTF(2,__func__": done\n");
618 return(0);
619}
620
621static int IsUsableDisk(AD_INFO *ai, int p, int d)
622{
623 union {
624 u8 b[512];
625 u16 w[256];
626 u32 l[128];
627 } *pSector0;
628 int iRetVal;
629
630 if (!use_mbr_test) return 1;
631 if (ai->ports[p].devs[d].removable) return 1;
632
633 do
634 {
635 iRetVal = 0;
636 pSector0 = MemAlloc(512);
637 if (!pSector0) break;
638
639 if (ahci_exec_polled_cmd(ai, p, 0, 500, ATA_CMD_READ,
640 AP_SECTOR_28, 0,
641 AP_COUNT, 1,
642 AP_VADDR, (void *)pSector0, 512,
643 AP_DEVICE, 0x40,
644 AP_END)) break;
645
646 DHEXDUMP(5, pSector0, 512, "Sector0:\n");
647
648 /* check for wiped disk */
649 if ((pSector0->l[0] == 0) && (pSector0->l[127] == 0))
650 {
651 iRetVal = 1;
652 break;
653 }
654
655 /* check for a valid MBR */
656 if (pSector0->w[255] != 0xaa55) break; /* Not valid if no MBR signature */
657 if (pSector0->b[0x1c2] == 0xee) break; /* Not valid if guard partition */
658 if (pSector0->b[0x1d2] == 0xee) break; /* Not valid if guard partition */
659 if (pSector0->b[0x1e2] == 0xee) break; /* Not valid if guard partition */
660 if (pSector0->b[0x1f2] == 0xee) break; /* Not valid if guard partition */
661 iRetVal = 1;
662 } while (0);
663
664 if (pSector0) MemFree(pSector0);
665 return iRetVal;
666}
667
668/******************************************************************************
669 * Set up device attached to the specified port based on ATA_IDENTFY_DEVICE or
670 * ATA_IDENTFY_PACKET_DEVICE data.
671 *
672 * NOTE: Port multipliers are not supported, yet, thus the device number is
673 * expected to be 0 for the time being.
674 */
675static void ahci_setup_device(AD_INFO *ai, int p, int d, u16 *id_buf)
676{
677 DEVICESTRUCT ds;
678 ADJUNCT adj;
679 HDEVICE dh;
680 char dev_name[RM_MAX_PREFIX_LEN+ATA_ID_PROD_LEN+1];
681 char *pDevName;
682
683 if (p >= AHCI_MAX_PORTS) return;
684 if (d >= AHCI_MAX_DEVS) return;
685
686 if (ai->port_max < p) ai->port_max = p;
687 if (ai->ports[p].dev_max < d) ai->ports[p].dev_max = d;
688 memset(ai->ports[p].devs + d, 0x00, sizeof(*ai->ports[p].devs));
689
690 /* set generic device information (assuming an ATA disk device for now) */
691 ai->ports[p].devs[d].present = 1;
692 ai->ports[p].devs[d].removable = (id_buf[ATA_ID_CONFIG] & 0x0080U) != 0;
693 ai->ports[p].devs[d].dev_type = UIB_TYPE_DISK;
694 pDevName = ai->ports[p].devs[d].dev_name;
695 strlcpy(pDevName, ata_dev_name(id_buf), sizeof(ai->ports[0].devs[0].dev_name));
696
697 if (id_buf[ATA_ID_CONFIG] & 0x8000U)
698 {
699 /* this is an ATAPI device; augment device information */
700 ai->ports[p].devs[d].atapi = 1;
701 ai->ports[p].devs[d].atapi_16 = (id_buf[ATA_ID_CONFIG] & 0x0001U) != 0;
702 ai->ports[p].devs[d].dev_type = (id_buf[ATA_ID_CONFIG] & 0x1f00U) >> 8;
703 ai->ports[p].devs[d].ncq_max = 1;
704
705 }
706 else
707 {
708 /* complete ATA-specific device information */
709 if (enable_ncq[ad_no(ai)][p])
710 {
711 ai->ports[p].devs[d].ncq_max = id_buf[ATA_ID_QUEUE_DEPTH] & 0x001fU;
712 }
713 if (ai->ports[p].devs[d].ncq_max < 1)
714 {
715 /* NCQ not enabled for this device, or device doesn't support NCQ */
716 ai->ports[p].devs[d].ncq_max = 1;
717 }
718 if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x0400U)
719 {
720 ai->ports[p].devs[d].lba48 = 1;
721 }
722 }
723
724 DPRINTF(2,"found device %d.%d.%d: removable=%d dev_type=%d atapi=%d ncq_max=%d\n",
725 ad_no(ai), p, d,
726 ai->ports[p].devs[d].removable,
727 ai->ports[p].devs[d].dev_type,
728 ai->ports[p].devs[d].atapi,
729 ai->ports[p].devs[d].ncq_max);
730
731 /* add device to resource manager; we don't really care about errors here */
732 memset(&ds, 0x00, sizeof(ds));
733 memset(&adj, 0x00, sizeof(adj));
734
735 adj.pNextAdj = NULL;
736 adj.AdjLength = sizeof(adj);
737 adj.AdjType = ADJ_ADD_UNIT;
738 adj.Add_Unit.ADDHandle = add_handle;
739 adj.Add_Unit.UnitHandle = iorb_unit(ad_no(ai), p, d);
740
741 /* create Resource Manager device key string;
742 * we distinguish only HDs and CD drives for now
743 */
744 if (ai->ports[p].devs[d].removable)
745 {
746 snprintf(dev_name, sizeof(dev_name), RM_CD_PREFIX "%s", p, d, pDevName);
747 }
748 else
749 {
750 snprintf(dev_name, sizeof(dev_name), RM_HD_PREFIX "%s", p, d, pDevName);
751 }
752
753 ds.DevDescriptName = dev_name;
754 ds.DevFlags = (ai->ports[p].devs[d].removable) ? DS_REMOVEABLE_MEDIA
755 : DS_FIXED_LOGICALNAME;
756 ds.DevType = ai->ports[p].devs[d].dev_type;
757 ds.pAdjunctList = &adj;
758
759 RMCreateDevice(rm_drvh, &dh, &ds, ai->rm_adh, NULL);
760
761 /* try to detect virtualbox environment to enable a hack for IRQ routing */
762 if (ai == ad_infos && ai->pci_vendor == 0x8086 && ai->pci_device == 0x2829 &&
763 !memcmp(pDevName, "VBOX HARDDISK", 13))
764 {
765 /* running inside virtualbox */
766 pci_hack_virtualbox();
767 }
768}
769
770/******************************************************************************
771 * Scan all ports for connected devices and fill in the corresponding device
772 * information.
773 *
774 * NOTES:
775 *
776 * - The adapter is temporarily configured for os2ahci but the original BIOS
777 * configuration will be restored when done. This happens only until we
778 * have received the IOCC_COMPLETE_INIT command.
779 *
780 * - Subsequent calls are currently not planned but may be required for
781 * suspend/resume handling, hot swap functionality, etc.
782 *
783 * - This function is expected to be called with the spinlock released but
784 * the corresponding adapter's busy flag set. It will aquire the spinlock
785 * temporarily to allocate/free memory for the ATA identify buffer.
786 *
787 * Called from iocm_device_table()
788 */
789int ahci_scan_ports(AD_INFO *ai)
790{
791 AHCI_PORT_CFG *pc = NULL;
792 u16 *id_buf;
793 int is_ata;
794 int rc;
795 int p;
796 int i;
797 TIMER Timer;
798
799 if ((id_buf = MemAlloc(ATA_ID_WORDS * sizeof(u16))) == NULL) return(-1);
800
801 if (ai->bios_config[0] == 0) ahci_save_bios_config(ai); /* first call */
802
803 if (ahci_enable_ahci(ai)) goto exit_port_scan;
804
805 /* perform port scan */
806 DPRINTF(1,__func__": scanning ports on adapter %d\n", ad_no(ai));
807 for (p = 0; p < AHCI_MAX_PORTS; p++)
808 {
809 if (!(ai->port_map & (1UL << p))) continue;
810 if (port_ignore[ad_no(ai)][p]) continue;
811
812 // DAZ allocate port structure here
813
814 DPRINTF(3,__func__": Wait till not busy on port %d\n", p);
815 /* wait until all active commands have completed on this port */
816 TimerInit(&Timer, 250);
817 while (ahci_port_busy(ai, p))
818 {
819 if (TimerCheckAndBlock(&Timer)) break;
820 }
821
822 if (!init_complete)
823 {
824 if ((pc = ahci_save_port_config(ai, p)) == NULL) goto exit_port_scan;
825 }
826
827 /* start/reset port; if no device is attached, this is expected to fail */
828 if (init_reset)
829 {
830 rc = ahci_reset_port(ai, p, 0);
831 }
832 else
833 {
834 DPRINTF(3,__func__": (re)starting port %d\n", p);
835 ahci_stop_port(ai, p);
836 rc = ahci_start_port(ai, p, 0);
837 }
838
839 if (rc == 0)
840 {
841 /* this port seems to have a device attached and ready for commands */
842 DPRINTF(2,__func__": port %d seems to be attached to a device; probing...\n", p);
843
844 #ifdef DAZ_NEW_CODE
845 ai->ports[p].dma_buf = MemAllocAlign(AHCI_PORT_PRIV_DMA_SZ, 1024);
846 ai->ports[p].dma_buf_phys = MemPhysAdr(ai->ports[p].dma_buf);
847 #endif
848
849 /* Get ATA(PI) identity. The so-called signature gives us a hint whether
850 * this is an ATA or an ATAPI device but we'll try both in either case;
851 * the signature will merely determine whether we're going to probe for
852 * an ATA or ATAPI device, first, in order to reduce the chance of sending
853 * the wrong command (which would result in a port reset given the way
854 * ahci_exec_polled_cmd() was implemented).
855 */
856 is_ata = readl(port_base(ai, p) + PORT_SIG) == 0x00000101UL;
857 for (i = 0; i < 2; i++)
858 {
859 rc = ahci_exec_polled_cmd(ai, p, 0, 500,
860 (is_ata) ? ATA_CMD_ID_ATA : ATA_CMD_ID_ATAPI,
861 AP_VADDR, (void *) id_buf, ATA_ID_WORDS * sizeof(u16),
862 AP_END);
863 if (rc == 0) break;
864
865 /* try again with ATA/ATAPI swapped */
866 is_ata = !is_ata;
867 }
868 }
869
870 if (rc == 0)
871 {
872 /* we have a valid IDENTIFY or IDENTIFY_PACKET response */
873 DHEXDUMP(5,id_buf, ATA_ID_WORDS * sizeof(u16), "ATA_IDENTIFY%s results:\n", (is_ata) ? "" : "_PACKET");
874 ahci_setup_device(ai, p, 0, id_buf);
875 if (!IsUsableDisk(ai, p, 0)) ai->ports[p].devs[0].ignored = 1;
876 }
877 else
878 {
879 /* no device attached to this port */
880 ai->port_map &= ~(1UL << p);
881 #ifdef DAZ_NEW_CODE
882 if (ai->ports[p].dma_buf) MemFree(ai->ports[p].dma_buf);
883 ai->ports[p].dma_buf = NULL;
884 #endif
885 }
886
887 if (pc != NULL) ahci_restore_port_config(ai, p, pc);
888 }
889
890exit_port_scan:
891 if (!init_complete)
892 {
893 ahci_restore_bios_config(ai);
894 }
895 MemFree(id_buf);
896 return(0);
897}
898
899/******************************************************************************
900 * Reset specified port. This function is typically called during adapter
901 * initialization and first gets the port into a defined status, then resets
902 * the port by sending a COMRESET signal.
903 *
904 * This function is also the location of the link speed initialization (link
905 * needs to be restablished after changing link speed, anyway).
906 *
907 * NOTE: This function uses a busy loop to wait for DMA engines to stop and
908 * the COMRESET to complete. It should only be called at task time
909 * during initialization or in a context hook.
910 */
911int ahci_reset_port(AD_INFO *ai, int p, int ei)
912{
913 u8 *port_mmio = port_base(ai, p);
914 u32 tmp;
915 TIMER Timer;
916
917 DPRINTF(3,__func__": resetting port %d.%d\n", ad_no(ai), p);
918 DUMP_PORT_REGS(3,ai,p);
919
920 /* stop port engines (we don't care whether there is an error doing so) */
921 ahci_stop_port(ai, p);
922
923 /* clear SError */
924 tmp = readl(port_mmio + PORT_SCR_ERR);
925 writel(port_mmio + PORT_SCR_ERR, tmp);
926
927 /* Some hardware reports incorrect status so just set these bits unconditionally */
928 tmp = readl(port_mmio + PORT_CMD);
929 tmp &= ~PORT_CMD_ALPE; /* turn off agressive power management */
930 tmp |= (PORT_CMD_SPIN_UP | PORT_CMD_POWER_ON); /* power up and spin up the drive */
931 writel(port_mmio + PORT_CMD, tmp);
932
933 /* set link speed and power management options */
934 DPRINTF(3,__func__": setting link speed and power management options\n");
935 tmp = readl(port_mmio + PORT_SCR_CTL) & ~0x00000fffUL;
936 tmp |= (link_speed[ad_no(ai)][p] & 0x0f) << 4;
937 tmp |= (link_power[ad_no(ai)][p] & 0x0f) << 8;
938 writel(port_mmio + PORT_SCR_CTL, tmp);
939
940 /* issue COMRESET on the port */
941 DPRINTF(3,__func__": issuing COMRESET on port %d\n", p);
942 writel(port_mmio + PORT_SCR_CTL, tmp | 1);
943 readl(port_mmio + PORT_SCR_CTL); /* flush */
944
945 /* spec says "leave reset bit on for at least 1ms"; make it 2ms */
946 udelay(2000);
947
948 writel(port_mmio + PORT_SCR_CTL, tmp);
949 readl(port_mmio + PORT_SCR_CTL); /* flush */
950
951 /* wait for communication to be re-established after port reset */
952 DPRINTF(3,"Wait for communication...\n");
953 TimerInit(&Timer, 500);
954 while (((tmp = readl(port_mmio + PORT_SCR_STAT)) & 3) != 3)
955 {
956 if (TimerCheckAndBlock(&Timer))
957 {
958 DPRINTF(2,"no device present after resetting port #%d (PORT_SCR_STAT = 0x%x)\n", p, tmp);
959 return(-1);
960 }
961 }
962
963 /* clear SError again (recommended by AHCI spec) */
964 tmp = readl(port_mmio + PORT_SCR_ERR);
965 writel(port_mmio + PORT_SCR_ERR, tmp);
966
967 /* start port so we can receive the COMRESET FIS */
968 DPRINTF(3,__func__": starting port %d again\n", p);
969 ahci_start_port(ai, p, ei);
970
971 /* wait for device to be ready ((PxTFD & (BSY | DRQ | ERR)) == 0) */
972 TimerInit(&Timer, 1000);
973 while (((tmp = readl(port_mmio + PORT_TFDATA)) & 0x89) != 0)
974 {
975 if (TimerCheckAndBlock(&Timer))
976 {
977 DPRINTF(0,"device not ready on port #%d (PORT_TFDATA = 0x%x)\n", p, tmp);
978 ahci_stop_port(ai, p);
979 return(-1);
980 }
981 }
982 DPRINTF(3,__func__": PORT_TFDATA = 0x%x\n", readl(port_mmio + PORT_TFDATA));
983
984 return(0);
985}
986
987/******************************************************************************
988 * Start specified port.
989 */
990int ahci_start_port(AD_INFO *ai, int p, int ei)
991{
992 u8 *port_mmio = port_base(ai, p);
993 u32 status;
994
995 DPRINTF(3,__func__": %d.%d\n", ad_no(ai), p);
996 /* check whether device presence is detected and link established */
997
998 status = readl(port_mmio + PORT_SCR_STAT);
999 DPRINTF(3,__func__": PORT_SCR_STAT = 0x%x\n", status);
1000 if ((status & 0xf) != 3) return(-1);
1001
1002 /* clear SError, if any */
1003 status = readl(port_mmio + PORT_SCR_ERR);
1004 DPRINTF(3,__func__": PORT_SCR_ERR = 0x%x\n", status);
1005 writel(port_mmio + PORT_SCR_ERR, status);
1006
1007 /* enable FIS reception */
1008 ahci_start_fis_rx(ai, p);
1009
1010 /* enable command engine */
1011 ahci_start_engine(ai, p);
1012
1013 if (ei)
1014 {
1015 /* clear any pending interrupts on this port */
1016 if ((status = readl(port_mmio + PORT_IRQ_STAT)) != 0)
1017 {
1018 writel(port_mmio + PORT_IRQ_STAT, status);
1019 }
1020
1021 /* enable port interrupts */
1022 writel(port_mmio + PORT_IRQ_MASK, PORT_IRQ_TF_ERR |
1023 PORT_IRQ_HBUS_ERR |
1024 PORT_IRQ_HBUS_DATA_ERR |
1025 PORT_IRQ_IF_ERR |
1026 PORT_IRQ_OVERFLOW |
1027 PORT_IRQ_BAD_PMP |
1028 PORT_IRQ_UNK_FIS |
1029 PORT_IRQ_SDB_FIS |
1030 PORT_IRQ_DMAS_FIS |
1031 PORT_IRQ_PIOS_FIS |
1032 PORT_IRQ_D2H_REG_FIS);
1033 }
1034 else
1035 {
1036 writel(port_mmio + PORT_IRQ_MASK, 0);
1037 }
1038 readl(port_mmio + PORT_IRQ_MASK); /* flush */
1039
1040 return(0);
1041}
1042
1043/******************************************************************************
1044 * Start port FIS reception. Copied from Linux AHCI driver and adopted to
1045 * OS2AHCI.
1046 */
1047void ahci_start_fis_rx(AD_INFO *ai, int p)
1048{
1049 u8 *port_mmio = port_base(ai, p);
1050 u32 port_dma = port_dma_base_phys(ai, p);
1051 u32 tmp;
1052
1053 /* set command header and FIS address registers */
1054 writel(port_mmio + PORT_LST_ADDR, port_dma + offsetof(AHCI_PORT_DMA, cmd_hdr));
1055 writel(port_mmio + PORT_LST_ADDR_HI, 0);
1056 writel(port_mmio + PORT_FIS_ADDR, port_dma + offsetof(AHCI_PORT_DMA, rx_fis));
1057 writel(port_mmio + PORT_FIS_ADDR_HI, 0);
1058
1059 /* enable FIS reception */
1060 tmp = readl(port_mmio + PORT_CMD);
1061 tmp |= PORT_CMD_FIS_RX;
1062 writel(port_mmio + PORT_CMD, tmp);
1063
1064 /* flush */
1065 readl(port_mmio + PORT_CMD);
1066}
1067
1068/******************************************************************************
1069 * Start port HW engine. Copied from Linux AHCI driver and adopted to OS2AHCI.
1070 */
1071void ahci_start_engine(AD_INFO *ai, int p)
1072{
1073 u8 *port_mmio = port_base(ai, p);
1074 u32 tmp;
1075
1076 /* start DMA */
1077 tmp = readl(port_mmio + PORT_CMD);
1078 tmp |= PORT_CMD_START;
1079 writel(port_mmio + PORT_CMD, tmp);
1080 readl(port_mmio + PORT_CMD); /* flush */
1081}
1082
1083/******************************************************************************
1084 * Stop specified port
1085 */
1086int ahci_stop_port(AD_INFO *ai, int p)
1087{
1088 u8 *port_mmio = port_base(ai, p);
1089 u32 tmp;
1090 int rc;
1091
1092 DPRINTF(3,__func__": %d.%d\n", ad_no(ai), p);
1093
1094 /* disable port interrupts */
1095 writel(port_mmio + PORT_IRQ_MASK, 0);
1096
1097 /* disable FIS reception */
1098 if ((rc = ahci_stop_fis_rx(ai, p)) != 0)
1099 {
1100 dprintf(0,__func__": failed to stop FIS receive (%d)\n", rc);
1101 return(rc);
1102 }
1103
1104 /* disable command engine */
1105 if ((rc = ahci_stop_engine(ai, p)) != 0)
1106 {
1107 dprintf(0,__func__": failed to stop port HW engine (%d)\n", rc);
1108 return(rc);
1109 }
1110
1111 /* clear any pending port IRQs */
1112 tmp = readl(port_mmio + PORT_IRQ_STAT);
1113 if (tmp) writel(port_mmio + PORT_IRQ_STAT, tmp);
1114 writel(ai->mmio + HOST_IRQ_STAT, 1UL << p);
1115
1116 /* reset PxSACT register (tagged command queues, not reset by COMRESET) */
1117 writel(port_mmio + PORT_SCR_ACT, 0);
1118 readl(port_mmio + PORT_SCR_ACT); /* flush */
1119
1120 return(0);
1121}
1122
1123/******************************************************************************
1124 * Stop port FIS reception. Copied from Linux AHCI driver and adopted to
1125 * OS2AHCI.
1126 *
1127 * NOTE: This function uses a busy loop to wait for the DMA engine to stop. It
1128 * should only be called at task time during initialization or in a
1129 * context hook (e.g. when resetting a port).
1130 */
1131int ahci_stop_fis_rx(AD_INFO *ai, int p)
1132{
1133 u8 *port_mmio = port_base(ai, p);
1134 TIMER Timer;
1135 u32 tmp;
1136 int status;
1137
1138 /* disable FIS reception */
1139 tmp = readl(port_mmio + PORT_CMD);
1140 tmp &= ~PORT_CMD_FIS_RX;
1141 writel(port_mmio + PORT_CMD, tmp);
1142
1143 /* wait for completion, spec says 500ms, give it 1000ms */
1144 status = 0;
1145 TimerInit(&Timer, 1000);
1146 while (readl(port_mmio + PORT_CMD) & PORT_CMD_FIS_ON)
1147 {
1148 status = TimerCheckAndBlock(&Timer);
1149 if (status) break;
1150 }
1151
1152 return(status ? -1 : 0);
1153}
1154
1155/******************************************************************************
1156 * Stop port HW engine. Copied from Linux AHCI driver and adopted to OS2AHCI.
1157 *
1158 * NOTE: This function uses a busy loop to wait for the DMA engine to stop. It
1159 * should only be called at task time during initialization or in a
1160 * context hook (e.g. when resetting a port).
1161 */
1162int ahci_stop_engine(AD_INFO *ai, int p)
1163{
1164 u8 *port_mmio = port_base(ai, p);
1165 TIMER Timer;
1166 int status;
1167 u32 tmp;
1168
1169 tmp = readl(port_mmio + PORT_CMD);
1170
1171 /* check if the port is already stopped */
1172 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0) return 0;
1173
1174 /* set port to idle */
1175 tmp &= ~PORT_CMD_START;
1176 writel(port_mmio + PORT_CMD, tmp);
1177
1178 /* wait for engine to stop. This could be as long as 500 msec */
1179 status = 0;
1180 TimerInit(&Timer, 500);
1181 while (readl(port_mmio + PORT_CMD) & PORT_CMD_LIST_ON)
1182 {
1183 status = TimerCheckAndBlock(&Timer);
1184 if (status) break;
1185 }
1186
1187 return(status ? -1 : 0);
1188}
1189
1190/******************************************************************************
1191 * Determine whether a port is busy executing commands.
1192 */
1193int ahci_port_busy(AD_INFO *ai, int p)
1194{
1195 u8 *port_mmio = port_base(ai, p);
1196
1197 return(readl(port_mmio + PORT_SCR_ACT) != 0 || readl(port_mmio + PORT_CMD_ISSUE) != 0);
1198}
1199
1200/******************************************************************************
1201 * Execute AHCI command for given IORB. This includes all steps typically
1202 * required by any of the ahci_*() IORB processing functions.
1203 *
1204 * NOTE: In order to prevent race conditions with port restart and reset
1205 * handlers, we either need to keep the spinlock during the whole
1206 * operation or set the adapter's busy flag. Since the expectation
1207 * is that command preparation will be quick (it certainly doesn't
1208 * involve delays), we're going with the spinlock for the time being.
1209 */
1210void ahci_exec_iorb(IORBH FAR16DATA *vIorb, IORBH *pIorb, int ncq_capable, int (*func)(IORBH FAR16DATA *, IORBH *pIorb, int))
1211{
1212 volatile u32 *cmds;
1213 ADD_WORKSPACE *aws = add_workspace(pIorb);
1214 AD_INFO *ai = &ad_infos[iorb_unit_adapter(pIorb)];
1215 P_INFO *port = &ai->ports[iorb_unit_port(pIorb)];
1216 ULONG timeout;
1217 u8 *port_mmio = port_base(ai, iorb_unit_port(pIorb));
1218 u16 cmd_max = ai->cmd_max;
1219 int i;
1220
1221 /* determine timeout in milliseconds */
1222 switch (pIorb->Timeout)
1223 {
1224 case 0:
1225 timeout = DEFAULT_TIMEOUT;
1226 break;
1227 case 0xffffffffUL:
1228 timeout = 0xffffffffUL;
1229 break;
1230 default:
1231 timeout = pIorb->Timeout * 1000;
1232 break;
1233 }
1234
1235 DPRINTF(7,"---------- "__func__": iorb=%x\n", vIorb);
1236
1237 /* Enable AHCI mode; apparently, the AHCI mode may end up becoming
1238 * disabled, either during the boot sequence (by the BIOS) or by
1239 * something else. The Linux AHCI drivers have this call in the
1240 * command processing chain, and apparently for a good reason because
1241 * without this, commands won't be executed.
1242 */
1243 ahci_enable_ahci(ai);
1244
1245 /* determine whether this will be an NCQ request */
1246 aws->is_ncq = 0;
1247 if (ncq_capable && port->devs[iorb_unit_device(pIorb)].ncq_max > 1 &&
1248 (ai->cap & HOST_CAP_NCQ) && !aws->no_ncq && init_complete)
1249 {
1250
1251 /* We can make this an NCQ request; limit command slots to the maximum
1252 * NCQ tag number reported by the device - 1. Why "minus one"? I seem to
1253 * recall an issue related to using all 32 tag numbers but can't quite
1254 * pinpoint it right now. One less won't make much of a difference...
1255 */
1256 aws->is_ncq = 1;
1257 if ((cmd_max = port->devs[iorb_unit_device(pIorb)].ncq_max - 1) > ai->cmd_max)
1258 {
1259 cmd_max = ai->cmd_max;
1260 }
1261 DPRINTF(8,__func__": NCQ command; cmd_max = %d->%d\n", ai->cmd_max, cmd_max);
1262 }
1263
1264 /* make sure adapter is available */
1265 spin_lock(drv_lock);
1266 if (!ai->busy)
1267 {
1268
1269 if (!init_complete)
1270 {
1271 /* no IRQ handlers or context hooks availabe at this point */
1272 ai->busy = 1;
1273 spin_unlock(drv_lock);
1274 ahci_exec_polled_iorb(vIorb, pIorb, func, timeout);
1275 ai->busy = 0;
1276 return;
1277 }
1278
1279 /* make sure we don't mix NCQ and regular commands */
1280 if (aws->is_ncq && port->reg_cmds == 0 || !aws->is_ncq && port->ncq_cmds == 0)
1281 {
1282 /* Find next available command slot. We use a simple round-robin
1283 * algorithm for this to prevent commands with higher slot indexes
1284 * from stalling when new commands are coming in frequently.
1285 */
1286 cmds = (aws->is_ncq) ? &port->ncq_cmds : &port->reg_cmds;
1287 for (i = 0; i <= cmd_max; i++)
1288 {
1289 if (++(port->cmd_slot) > cmd_max) port->cmd_slot = 0;
1290 if ((*cmds & (1UL << port->cmd_slot)) == 0) break;
1291 }
1292
1293 if ((*cmds & (1UL << port->cmd_slot)) == 0)
1294 {
1295 /* found idle command slot; prepare command */
1296 if (func(vIorb, pIorb, port->cmd_slot))
1297 {
1298 /* Command preparation failed, or no HW command required; IORB
1299 * will already have the error code if there was an error.
1300 */
1301 spin_unlock(drv_lock);
1302 iorb_done(vIorb, pIorb);
1303 return;
1304 }
1305
1306 /* start timer for this IORB */
1307 Timer_StartTimerMS(&aws->timer, timeout, timeout_callback, CastFar16ToULONG(vIorb));
1308
1309 /* issue command to hardware */
1310 *cmds |= (1UL << port->cmd_slot);
1311 aws->queued_hw = 1;
1312 aws->cmd_slot = port->cmd_slot;
1313
1314 DPRINTF(7,__func__": Issuing command Slot=%d cmds=%x\n", port->cmd_slot, *cmds);
1315 if (aws->is_ncq)
1316 {
1317 writel(port_mmio + PORT_SCR_ACT, (1UL << port->cmd_slot));
1318 readl(port_mmio + PORT_SCR_ACT); /* flush */
1319 }
1320 writel(port_mmio + PORT_CMD_ISSUE, (1UL << port->cmd_slot));
1321 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1322
1323 spin_unlock(drv_lock);
1324 return;
1325 }
1326 }
1327 }
1328
1329 /* requeue this IORB; it will be picked up again in trigger_engine() */
1330 aws->processing = 0;
1331 spin_unlock(drv_lock);
1332}
1333
1334/******************************************************************************
1335 * Execute polled IORB command. This function is called by ahci_exec_iorb()
1336 * when the initialization has not yet completed. The reasons for polling until
1337 * initialization has completed are:
1338 *
1339 * - We need to restore the BIOS configuration after we're done with this
1340 * command because someone might still call int 13h routines; sending
1341 * asynchronous commands and waiting for interrupts to indicate completion
1342 * won't work in such a scenario.
1343 * - Our context hooks won't work while the device managers are initializing
1344 * (they can't yield at init time).
1345 * - The device managers typically poll for command completion during
1346 * initialization so it won't make much of a difference, anyway.
1347 *
1348 * NOTE: This function must be called with the adapter-level busy flag set but
1349 * without the driver-level spinlock held.
1350 */
1351void ahci_exec_polled_iorb(IORBH FAR16DATA *vIorb, IORBH *pIorb, int (*func)(IORBH FAR16DATA *, IORBH *pIorb, int), ULONG timeout)
1352{
1353 AHCI_PORT_CFG *pc = NULL;
1354 AD_INFO *ai = ad_infos + iorb_unit_adapter(vIorb);
1355 int p = iorb_unit_port(pIorb);
1356 u8 *port_mmio = port_base(ai, p);
1357 TIMER Timer;
1358 int rc;
1359
1360 /* enable AHCI mode */
1361 if (ahci_enable_ahci(ai) != 0)
1362 {
1363 iorb_seterr(pIorb, IOERR_ADAPTER_NONSPECIFIC);
1364 goto restore_bios_config;
1365 }
1366
1367 /* check whether command slot 0 is available */
1368 if ((readl(port_mmio + PORT_CMD_ISSUE) & 1) != 0)
1369 {
1370 iorb_seterr(pIorb, IOERR_DEVICE_BUSY);
1371 goto restore_bios_config;
1372 }
1373
1374 /* save port configuration */
1375 if ((pc = ahci_save_port_config(ai, p)) == NULL)
1376 {
1377 iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
1378 goto restore_bios_config;
1379 }
1380
1381 /* restart/reset port (includes the necessary port configuration) */
1382 if (init_reset)
1383 {
1384 /* As outlined in ahci_restore_bios_config(), switching back and
1385 * forth between SATA and AHCI mode requires a COMRESET to force
1386 * the corresponding controller subsystem to rediscover attached
1387 * devices. Thus, we'll reset the port instead of stopping and
1388 * starting it.
1389 */
1390 if (ahci_reset_port(ai, p, 0))
1391 {
1392 iorb_seterr(pIorb, IOERR_ADAPTER_NONSPECIFIC);
1393 goto restore_bios_config;
1394 }
1395
1396 }
1397 else if (ahci_stop_port(ai, p) || ahci_start_port(ai, p, 0))
1398 {
1399 iorb_seterr(pIorb, IOERR_ADAPTER_NONSPECIFIC);
1400 goto restore_bios_config;
1401 }
1402
1403 /* prepare command */
1404 if (func(vIorb, pIorb, 0) == 0)
1405 {
1406 /* successfully prepared cmd; issue cmd and wait for completion */
1407 DPRINTF(3,"---------- "__func__" executing polled cmd on slot 0...");
1408 writel(port_mmio + PORT_CMD_ISSUE, 1);
1409 TimerInit(&Timer, timeout);
1410 while (readl(port_mmio + PORT_CMD_ISSUE) & 1)
1411 {
1412 rc = TimerCheckAndBlock(&Timer);
1413 if (rc) break;
1414 }
1415
1416 /* 0x89 = BSY(0x80) | DRQ(0x08) | ERR(0x01) */
1417 if (rc)
1418 {
1419 DPRINTF(3," timeout for IORB %x port=%x", vIorb, p);
1420 iorb_seterr(pIorb, IOERR_ADAPTER_TIMEOUT);
1421 }
1422 else if (readl(port_mmio + PORT_SCR_ERR) != 0 || readl(port_mmio + PORT_TFDATA) & 0x89)
1423 {
1424 DPRINTF(3," polled cmd error for IORB %x", vIorb);
1425 iorb_seterr(pIorb, IOERR_DEVICE_NONSPECIFIC);
1426 ahci_reset_port(ai, iorb_unit_port(pIorb), 0);
1427 }
1428 else
1429 {
1430 /* successfully executed command */
1431 if (add_workspace(pIorb)->ppfunc != NULL)
1432 {
1433 add_workspace(pIorb)->ppfunc(vIorb, pIorb);
1434 }
1435 else
1436 {
1437 add_workspace(pIorb)->complete = 1;
1438 }
1439 }
1440 DPRINTF(3,"\n");
1441 }
1442
1443restore_bios_config:
1444 /* restore BIOS configuration */
1445 if (pc != NULL)
1446 {
1447 ahci_restore_port_config(ai, p, pc);
1448 }
1449 ahci_restore_bios_config(ai);
1450
1451 if (add_workspace(pIorb)->complete | (pIorb->Status | IORB_ERROR))
1452 {
1453 iorb_done(vIorb, pIorb);
1454 }
1455 return;
1456}
1457
1458/******************************************************************************
1459 * Execute polled ATA/ATAPI command. This function will block until the command
1460 * has completed or the timeout has expired, thus it should only be used during
1461 * initialization. Furthermore, it will always use command slot zero.
1462 *
1463 * The difference to ahci_exec_polled_iorb() is that this function executes
1464 * arbitrary ATA/ATAPI commands outside the context of an IORB. It's typically
1465 * used when scanning for devices during initialization.
1466 */
1467int ahci_exec_polled_cmd(AD_INFO *ai, int p, int d, int timeout, int cmd, ...)
1468{
1469 va_list va;
1470 u8 *port_mmio = port_base(ai, p);
1471 u32 tmp;
1472 int rc;
1473 TIMER Timer;
1474
1475 /* verify that command slot 0 is idle */
1476 if (readl(port_mmio + PORT_CMD_ISSUE) & 1)
1477 {
1478 DPRINTF(3,__func__": port %d slot 0 is not idle; not executing polled cmd\n", p);
1479 return(-1);
1480 }
1481
1482 /* fill in command slot 0 */
1483 va_start(va, cmd);
1484 if ((rc = v_ata_cmd(ai, p, d, 0, cmd, va)) != 0) return(rc);
1485
1486 /* start command execution for slot 0 */
1487 DPRINTF(3,"---------- "__func__" executing polled cmd...");
1488 writel(port_mmio + PORT_CMD_ISSUE, 1);
1489
1490 /* wait until command has completed */
1491 TimerInit(&Timer, timeout);
1492 rc = 0;
1493 while (readl(port_mmio + PORT_CMD_ISSUE) & 1)
1494 {
1495 rc = TimerCheckAndBlock(&Timer);
1496 if (rc)
1497 {
1498 DPRINTF(2," Timeout");
1499 break;
1500 }
1501 }
1502
1503 tmp = readl(port_mmio + PORT_SCR_ERR);
1504 if (tmp & PORT_ERR_FAIL_BITS)
1505 {
1506 DPRINTF(2," SERR = 0x%08lx", tmp);
1507 rc = 1;
1508 }
1509 /* 0x89 = BSY(0x80) | DRQ(0x08) | ERR(0x01) */
1510 if (((tmp = readl(port_mmio + PORT_TFDATA)) & 0x89) != 0)
1511 {
1512 DPRINTF(2," TFDATA = 0x%08lx", tmp);
1513 rc = 1;
1514 }
1515
1516 if (rc)
1517 {
1518 DPRINTF(3,"failed\n");
1519 ahci_reset_port(ai, p, 0);
1520 return(-1);
1521 }
1522 DPRINTF(3,"success\n");
1523 return(0);
1524}
1525
1526/******************************************************************************
1527 * Flush write cache of the specified device. Since there's no equivalent IORB
1528 * command, we'll execute this command directly using polling. Otherwise, we
1529 * would have to create a fake IORB, add it to the port's IORB queue, ...
1530 *
1531 * Besides, this function is only called when shutting down and the code there
1532 * would have to wait for the flush cache command to complete as well, using
1533 * polling just the same...
1534 */
1535int ahci_flush_cache(AD_INFO *ai, int p, int d)
1536{
1537 if (!ai->ports[p].devs[d].atapi)
1538 {
1539 DPRINTF(2,__func__": flushing cache on %d.%d.%d\n", ad_no(ai), p, d);
1540 return(ahci_exec_polled_cmd(ai, p, d, 30000,
1541 ai->ports[p].devs[d].lba48 ? ATA_CMD_FLUSH_EXT : ATA_CMD_FLUSH, AP_END));
1542 }
1543 return 0;
1544}
1545
1546/******************************************************************************
1547 * Set device into IDLE mode (spin down); this was used during
1548 * debugging/testing and is now unused; it's still there in case we need it
1549 * again...
1550 *
1551 * If 'idle' is != 0, the idle timeout is set to 5 seconds, otherwise it
1552 * is turned off.
1553 */
1554int ahci_set_dev_idle(AD_INFO *ai, int p, int d, int idle)
1555{
1556 DPRINTF(3,__func__": sending IDLE=%d command to port %d\n", idle, p);
1557 return ahci_exec_polled_cmd(ai, p, d, 500, ATA_CMD_IDLE, AP_COUNT, idle ? 1 : 0, AP_END);
1558}
1559
1560/******************************************************************************
1561 * AHCI top-level hardware interrupt handler. This handler finds the adapters
1562 * and ports which have issued the interrupt and calls the corresponding
1563 * port interrupt handler.
1564 *
1565 * On entry, OS/2 will have processor interrupts enabled because we're using
1566 * shared IRQs but we won't be preempted by another interrupt on the same
1567 * IRQ level until we indicated EOI. We'll keep it this way, only requesting
1568 * the driver-level spinlock when actually changing the driver state (IORB
1569 * queues, ...)
1570 */
1571#pragma aux ahci_intr parm [eax]
1572int ahci_intr(u32 irq)
1573{
1574 u32 irq_stat;
1575 int handled = 0;
1576 int a;
1577 int p;
1578
1579 /* find adapter(s) with pending interrupts */
1580 for (a = 0; a < ad_info_cnt; a++)
1581 {
1582 AD_INFO *ai = ad_infos + a;
1583
1584 if (ai->irq == irq && (irq_stat = readl(ai->mmio + HOST_IRQ_STAT)) != 0)
1585 {
1586 /* this adapter has interrupts pending */
1587 u32 irq_masked = irq_stat & ai->port_map;
1588
1589 for (p = 0; p <= ai->port_max; p++)
1590 {
1591 if (irq_masked & (1UL << p))
1592 {
1593 ahci_port_intr(ai, p);
1594 }
1595 }
1596
1597 /* clear interrupt condition on the adapter */
1598 writel(ai->mmio + HOST_IRQ_STAT, irq_stat);
1599 readl(ai->mmio + HOST_IRQ_STAT); /* flush */
1600 handled = 1;
1601 }
1602 }
1603
1604 if (handled)
1605 {
1606 /* Trigger state machine to process next IORBs, if any. Due to excessive
1607 * IORB requeue operations (e.g. when processing large unaligned reads or
1608 * writes), we may be stacking interrupts on top of each other. If we
1609 * detect this, we'll pass this on to the engine context hook.
1610 */
1611 #if 0
1612 if ((u32)&irq_stat < 0xf000)
1613 {
1614 DPRINTF(0,__func__": IRQ stack running low; arming engine context hook\n");
1615 /* Rousseau:
1616 * A context hook cannot be re-armed before it has completed.
1617 * (?:\IBMDDK\DOCS\PDDREF.INF->Device Helper (DevHlp) Services)->ArmCtxHook)
1618 * Also, it is executed at task-time, thus in the context of some
1619 * application thread. Stacked interrupts with a stack below the
1620 * threshold specified above, (0xf000), will repeatly try to arm the
1621 * context hook, but since we are in an interrupted interrupt handler,
1622 * it's highly unlikely the hook has completed.
1623 * So, possibly only the first arming is succesful and subsequent armings
1624 * will fail because no task-time thread has run between the stacked
1625 * interrupts. One hint would be that if the dispatching truely worked,
1626 * excessive stacked interrupts in VBox would not be a problem.
1627 * This needs some more investigation.
1628 */
1629 KernArmHook(engine_ctxhook_h, 0, 0);
1630 }
1631 else
1632 #endif
1633 {
1634 spin_lock(drv_lock);
1635 trigger_engine();
1636 spin_unlock(drv_lock);
1637 }
1638 DevCli();
1639 Dev32Help_EOI(irq);
1640 return(1); /* handled */
1641 }
1642
1643 return(0); /* not handled */
1644}
1645
1646/******************************************************************************
1647 * AHCI port-level interrupt handler. As described above, processor interrupts
1648 * are enabled on entry thus we have to protect shared resources with a
1649 * spinlock.
1650 */
1651void ahci_port_intr(AD_INFO *ai, int p)
1652{
1653 IORB_QUEUE done_queue;
1654 IORBH FAR16DATA *vIorb;
1655 IORBH FAR16DATA *vNext = FAR16NULL;
1656 u8 *port_mmio = port_base(ai, p);
1657 u32 irq_stat;
1658 u32 active_cmds;
1659 u32 done_mask;
1660
1661 /* get interrupt status and clear it right away */
1662 irq_stat = readl(port_mmio + PORT_IRQ_STAT);
1663 writel(port_mmio + PORT_IRQ_STAT, irq_stat);
1664 readl(port_mmio + PORT_IRQ_STAT); /* flush */
1665
1666 memset(&done_queue, 0x00, sizeof(done_queue));
1667
1668 if (irq_stat & PORT_IRQ_ERROR)
1669 {
1670 /* this is an error interrupt;
1671 * disable port interrupts to avoid IRQ storm until error condition
1672 * has been cleared by the restart handler
1673 */
1674 writel(port_mmio + PORT_IRQ_MASK, 0);
1675 ahci_error_intr(ai, p, irq_stat);
1676 return;
1677 }
1678
1679 spin_lock(drv_lock);
1680
1681 /* Find out which command slots have completed. Since error recovery for
1682 * NCQ commands interfers with non-NCQ commands, the upper layers will
1683 * make sure there's never a mixture of NCQ and non-NCQ commands active
1684 * on any port at any given time. This makes it easier to find out which
1685 * commands have completed, too.
1686 */
1687 if (ai->ports[p].ncq_cmds != 0)
1688 {
1689 active_cmds = readl(port_mmio + PORT_SCR_ACT);
1690 done_mask = ai->ports[p].ncq_cmds ^ active_cmds;
1691 DPRINTF(7,"[ncq_cmds]: active_cmds=0x%08x done_mask=0x%08x\n", active_cmds, done_mask);
1692 }
1693 else
1694 {
1695 active_cmds = readl(port_mmio + PORT_CMD_ISSUE);
1696 done_mask = ai->ports[p].reg_cmds ^ active_cmds;
1697 DPRINTF(7,"[reg_cmds]: active_cmds=0x%08x done_mask=0x%08x\n", active_cmds, done_mask);
1698 }
1699
1700 /* Find the IORBs related to the completed commands and complete them.
1701 *
1702 * NOTES: The spinlock must not be released while in this loop to prevent
1703 * race conditions with timeout handlers or other threads in SMP
1704 * systems.
1705 *
1706 * Since we hold the spinlock when IORBs complete, we can't call the
1707 * IORB notification routine right away because this routine might
1708 * schedule another IORB which could cause a deadlock. Thus, we'll
1709 * add all IORBs to be completed to a temporary queue which will be
1710 * processed after releasing the spinlock.
1711 */
1712 for (vIorb = ai->ports[p].iorb_queue.vRoot; vIorb != FAR16NULL; vIorb = vNext)
1713 {
1714 IORBH *pIorb = Far16ToFlat(vIorb);
1715 ADD_WORKSPACE *aws = (ADD_WORKSPACE *) &pIorb->ADDWorkSpace;
1716
1717 vNext = pIorb->f16NxtIORB;
1718 if (aws->queued_hw && (done_mask & (1UL << aws->cmd_slot)))
1719 {
1720 /* this hardware command has completed */
1721 ai->ports[p].ncq_cmds &= ~(1UL << aws->cmd_slot);
1722 ai->ports[p].reg_cmds &= ~(1UL << aws->cmd_slot);
1723
1724 /* call post-processing function, if any */
1725 if (aws->ppfunc != NULL) aws->ppfunc(vIorb, pIorb);
1726 else aws->complete = 1;
1727
1728 if (aws->complete)
1729 {
1730 /* this IORB is complete; move IORB to our temporary done queue */
1731 iorb_queue_del(&ai->ports[p].iorb_queue, vIorb);
1732 iorb_queue_add(&done_queue, vIorb, pIorb);
1733 aws_free(add_workspace(pIorb));
1734 }
1735 }
1736 }
1737
1738 spin_unlock(drv_lock);
1739
1740 /* complete all IORBs in the done queue */
1741 for (vIorb = done_queue.vRoot; vIorb != FAR16NULL; vIorb = vNext)
1742 {
1743 IORBH *pIorb = Far16ToFlat(vIorb);
1744
1745 vNext = pIorb->f16NxtIORB;
1746
1747 iorb_complete(vIorb, pIorb);
1748 }
1749}
1750
1751/******************************************************************************
1752 * AHCI error interrupt handler. Errors include interface errors and device
1753 * errors (usually triggered by the error bit in the AHCI task file register).
1754 *
1755 * Since this involves long-running operations such as restarting or even
1756 * resetting a port, this function is invoked at task time via a context
1757 * hook.
1758 *
1759 * NOTE: AHCI controllers stop all processing when encountering an error
1760 * condition in order to give the driver time to find out what exactly
1761 * went wrong. This means no new commands will be processed until we
1762 * clear the error register and restore the "commands issued" register.
1763 */
1764void ahci_error_intr(AD_INFO *ai, int p, u32 irq_stat)
1765{
1766 int reset_port = 0;
1767
1768 /* Handle adapter and interface errors. Those typically require a port
1769 * reset, or worse.
1770 */
1771 ai->ports[p].error_count++;
1772
1773 if (irq_stat & PORT_IRQ_UNK_FIS)
1774 {
1775 #ifdef DEBUG
1776 u32 *unk = (u32 *) (port_dma_base(ai, p)->rx_fis + RX_FIS_UNK);
1777 DPRINTF(0,"warning: unknown FIS %08lx %08lx %08lx %08lx\n", unk[0], unk[1], unk[2], unk[3]);
1778 #endif
1779 reset_port = 1;
1780 }
1781 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR))
1782 {
1783 dprintf(0,"warning: host bus [data] error for port #%d\n", p);
1784 reset_port = 1;
1785 }
1786 if (irq_stat & PORT_IRQ_IF_ERR && !(ai->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR))
1787 {
1788 dprintf(0,"warning: interface fatal error for port #%d\n", p);
1789 reset_port = 1;
1790 }
1791 if (reset_port)
1792 {
1793 /* need to reset the port; leave this to the reset context hook */
1794
1795 ports_to_reset[ad_no(ai)] |= 1UL << p;
1796 KernArmHook(reset_ctxhook_h, 0, 0);
1797
1798 /* no point analyzing device errors after a reset... */
1799 return;
1800 }
1801
1802 #ifdef DEBUG
1803 dprintf(0,"port #%d interrupt error status: 0x%08x; restarting port\n", p, irq_stat);
1804 #else
1805 if (!ai->ports[p].devs[0].atapi || D32g_DbgLevel)
1806 {
1807 dprintf(0,"port #%d interrupt error status: 0x%08x; restarting port\n", p, irq_stat);
1808 }
1809 #endif
1810
1811 /* Handle device-specific errors. Those errors typically involve restarting
1812 * the corresponding port to resume operations which can take some time,
1813 * thus we need to offload this functionality to the restart context hook.
1814 */
1815 ports_to_restart[ad_no(ai)] |= 1UL << p;
1816 KernArmHook(restart_ctxhook_h, 0, 0);
1817}
1818
1819/******************************************************************************
1820 * Get device or media geometry. Device and media geometry are expected to be
1821 * the same for non-removable devices.
1822 */
1823void ahci_get_geometry(IORBH FAR16DATA *vIorb, IORBH *pIorb)
1824{
1825 DPRINTF(7,"ahci_get_geometry(%d.%d.%d)\n", iorb_unit_adapter(pIorb),
1826 iorb_unit_port(pIorb), iorb_unit_device(pIorb));
1827
1828 ahci_exec_iorb(vIorb, pIorb, 0, cmd_func(pIorb, get_geometry));
1829}
1830
1831/******************************************************************************
1832 * Test whether unit is ready.
1833 */
1834void ahci_unit_ready(IORBH FAR16DATA *vIorb, IORBH *pIorb)
1835{
1836 DPRINTF(7,"ahci_unit_ready(%d.%d.%d)\n", iorb_unit_adapter(pIorb),
1837 iorb_unit_port(pIorb), iorb_unit_device(pIorb));
1838
1839 ahci_exec_iorb(vIorb, pIorb, 0, cmd_func(pIorb, unit_ready));
1840}
1841
1842/******************************************************************************
1843 * Read sectors from AHCI device.
1844 */
1845void ahci_read(IORBH FAR16DATA *vIorb, IORBH *pIorb)
1846{
1847 DPRINTF(7,"ahci_read(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(vIorb),
1848 iorb_unit_port(pIorb), iorb_unit_device(pIorb),
1849 ((IORB_EXECUTEIO *) pIorb)->RBA,
1850 ((IORB_EXECUTEIO *) pIorb)->BlockCount);
1851
1852 ahci_exec_iorb(vIorb, pIorb, 1, cmd_func(pIorb, read));
1853}
1854
1855/******************************************************************************
1856 * Verify readability of sectors on AHCI device.
1857 */
1858void ahci_verify(IORBH FAR16DATA *vIorb, IORBH *pIorb)
1859{
1860 DPRINTF(7,"ahci_verify(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(pIorb),
1861 iorb_unit_port(pIorb), iorb_unit_device(pIorb),
1862 ((IORB_EXECUTEIO *)pIorb)->RBA,
1863 ((IORB_EXECUTEIO *)pIorb)->BlockCount);
1864
1865 ahci_exec_iorb(vIorb, pIorb, 0, cmd_func(pIorb, verify));
1866}
1867
1868/******************************************************************************
1869 * Write sectors to AHCI device.
1870 */
1871void ahci_write(IORBH FAR16DATA *vIorb, IORBH *pIorb)
1872{
1873 DPRINTF(7,"ahci_write(%d.%d.%d, %d, %d)\n", iorb_unit_adapter(pIorb),
1874 iorb_unit_port(pIorb), iorb_unit_device(pIorb),
1875 ((IORB_EXECUTEIO *)pIorb)->RBA,
1876 ((IORB_EXECUTEIO *)pIorb)->BlockCount);
1877
1878 ahci_exec_iorb(vIorb, pIorb, 1, cmd_func(pIorb, write));
1879}
1880
1881/******************************************************************************
1882 * Execute SCSI (ATAPI) command.
1883 */
1884void ahci_execute_cdb(IORBH FAR16DATA *vIorb, IORBH *pIorb)
1885{
1886 int a = iorb_unit_adapter(pIorb);
1887 int p = iorb_unit_port(pIorb);
1888 int d = iorb_unit_device(pIorb);
1889
1890 DHEXDUMP(5,Far16ToFlat(((IORB_ADAPTER_PASSTHRU *)pIorb)->f16ControllerCmd),
1891 ((IORB_ADAPTER_PASSTHRU *)pIorb)->ControllerCmdLen,
1892 "ahci_execute_cdb(%d.%d.%d): ", a, p, d);
1893
1894 if (ad_infos[a].ports[p].devs[d].atapi)
1895 {
1896 ahci_exec_iorb(vIorb, pIorb, 0, atapi_execute_cdb);
1897 }
1898 else
1899 {
1900 iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
1901 iorb_done(vIorb, pIorb);
1902 }
1903}
1904
1905/******************************************************************************
1906 * Execute ATA command. Please note that this is allowed for both ATA and
1907 * ATAPI devices because ATAPI devices will process some ATA commands as well.
1908 */
1909void ahci_execute_ata(IORBH FAR16DATA *vIorb, IORBH *pIorb)
1910{
1911 #ifdef DEBUG
1912 int a = iorb_unit_adapter(pIorb);
1913 int p = iorb_unit_port(pIorb);
1914 int d = iorb_unit_device(pIorb);
1915
1916 DHEXDUMP(5,Far16ToFlat(((IORB_ADAPTER_PASSTHRU *)pIorb)->f16ControllerCmd),
1917 ((IORB_ADAPTER_PASSTHRU *)pIorb)->ControllerCmdLen,
1918 "ahci_execute_ata(%d.%d.%d): ", a, p, d);
1919 #endif
1920
1921 ahci_exec_iorb(vIorb, pIorb, 0, ata_execute_ata);
1922}
1923
Note: See TracBrowser for help on using the repository browser.