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

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

Moved dma buffer pointers.

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