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

Last change on this file since 188 was 188, checked in by David Azarewicz, 8 years ago

Fixed interrupt problem.

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