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

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

debugging updates

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