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

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

Fixed up timer functions

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