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

Last change on this file since 162 was 162, checked in by David Azarewicz, 12 years ago

driver info updates, misc cleanup, add comments
This is version 1.28

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