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

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

fixed trap dump kernel exit, some work on suspend/resume routines

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