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

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