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

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

Debug output updates

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