source: trunk/src/os2ahci/ahci.c

Last change on this file was 211, checked in by David Azarewicz, 2 years ago

Added workaround to help with VirtualBox issues.
Improved diagnostic messages.
Changed how timeouts are reset and how ctx hooks are triggered.
Added quirk for devices with issues executing some standard commands.
Changed to make /N the default.

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