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

Last change on this file since 80 was 80, checked in by chris, 14 years ago

Version 1.06
============

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