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

Last change on this file since 205 was 205, checked in by David Azarewicz, 5 years ago

Fixed ADD RM id.

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