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

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

Debugging changes

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