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

Last change on this file since 178 was 178, checked in by David Azarewicz, 9 years ago

Major reorganization

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