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

Last change on this file since 72 was 72, checked in by chris, 15 years ago
  • cosmetic changes
File size: 53.3 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 rc;
411 int p;
412
413 spin_lock(drv_lock);
414 id_buf = malloc(ATA_ID_WORDS * sizeof(u16));
415 spin_unlock(drv_lock);
416 if (id_buf == NULL) {
417 return(-1);
418 }
419
420 if (ai->bios_config[0] == 0) {
421 /* first call */
422 ahci_save_bios_config(ai);
423 }
424
425 if (ahci_enable_ahci(ai)) {
426 goto exit_port_scan;
427 }
428
429 /* perform port scan */
430 dprintf("scanning ports on adapter #%d\n", ad_no(ai));
431 for (p = 0; p < AHCI_MAX_PORTS; p++) {
432 if (ai->port_map & (1UL << p)) {
433
434 if (!init_complete) {
435 if ((pc = ahci_save_port_config(ai, p)) == NULL) {
436 goto exit_port_scan;
437 }
438 }
439
440 /* start/reset port; if no device is attached, this is expected to fail */
441 if (init_reset) {
442 ddprintf("init-resetting port #%d\n", p);
443 rc = ahci_reset_port(ai, p, 0);
444 } else {
445 ddprintf("(re)starting port #%d\n", p);
446 ahci_stop_port(ai, p);
447 rc = ahci_start_port(ai, p, 0);
448 }
449 if (rc) {
450 /* no device attached to this port */
451 ai->port_map &= ~(1UL << p);
452 goto restore_port_config;
453 }
454
455 /* this port has a device attached and is ready to accept commands */
456 ddprintf("port #%d seems to be attached to a device; probing...\n", p);
457 rc = ahci_exec_polled_cmd(ai, p, 0, 500, ATA_CMD_ID_ATA,
458 AP_VADDR, (void _far *) id_buf, 512,
459 AP_END);
460 if (rc != 0 || id_buf[ATA_ID_CONFIG] & (1U << 15)) {
461 /* this might be an ATAPI device; run IDENTIFY_PACKET_DEVICE */
462 rc = ahci_exec_polled_cmd(ai, p, 0, 500, ATA_CMD_ID_ATAPI,
463 AP_VADDR, (void _far *) id_buf, 512,
464 AP_END);
465 }
466
467 if (rc == 0) {
468 /* we have a valid IDENTIFY or IDENTIFY_PACKET response */
469 ddphex(id_buf, 512, "ATA_IDENTIFY(_PACKET) results:\n");
470 ahci_setup_device(ai, p, 0, id_buf);
471 } else {
472 /* no device attached to this port */
473 ai->port_map &= ~(1UL << p);
474 }
475
476 restore_port_config:
477 if (pc != NULL) {
478 ahci_restore_port_config(ai, p, pc);
479 }
480 }
481 }
482
483exit_port_scan:
484 if (!init_complete) {
485 ahci_restore_bios_config(ai);
486 }
487 spin_lock(drv_lock);
488 free(id_buf);
489 spin_unlock(drv_lock);
490 return(0);
491}
492
493/******************************************************************************
494 * Complete initialization of adapter. This includes restarting all active
495 * ports and initializing interrupt processing. This is called when receiving
496 * the IOCM_COMPLETE_INIT request.
497 */
498int ahci_complete_init(AD_INFO *ai)
499{
500 int rc;
501 int p;
502 int i;
503
504 dprintf("completing initialization of adapter #%d\n", ad_no(ai));
505
506 /* register IRQ handlers; each IRQ level is registered only once */
507 for (i = 0; i < irq_map_cnt; i++) {
508 if (irq_map[i] == ai->irq) {
509 /* we already have this IRQ registered */
510 break;
511 }
512 }
513
514 if (i >= irq_map_cnt) {
515 dprintf("registering interrupt #%d\n", ai->irq);
516
517 if (DevHelp_SetIRQ(mk_NPFN(irq_handlers[irq_map_cnt]), ai->irq, 1) != 0) {
518 dprintf("failed to register shared interrupt\n");
519
520 if (DevHelp_SetIRQ(mk_NPFN(irq_handlers[irq_map_cnt]), ai->irq, 0) != 0) {
521 dprintf("failed to register exclusive interrupt\n");
522 return(-1);
523 }
524 }
525 irq_map[irq_map_cnt++] = ai->irq;
526 }
527
528 /* enable AHCI mode */
529 if ((rc = ahci_enable_ahci(ai)) != 0) {
530 return(rc);
531 }
532
533 /* Start all ports. The main purpose is to set the command list and FIS
534 * receive area addresses properly and to enable port-level interrupts; we
535 * don't really care about the return status because we'll find out soon
536 * enough if a previously detected device has problems.
537 */
538 for (p = 0; p < AHCI_MAX_PORTS; p++) {
539 if (ai->port_map & (1UL << p)) {
540 dprintf("restarting port #%d\n", p);
541 ahci_stop_port(ai, p);
542 ahci_start_port(ai, p, 1);
543 }
544 }
545
546 /* clear pending interrupt status */
547 writel(ai->mmio + HOST_IRQ_STAT, readl(ai->mmio + HOST_IRQ_STAT));
548 readl(ai->mmio + HOST_IRQ_STAT); /* flush */
549
550 /* enable adapter-level interrupts */
551 writel(ai->mmio + HOST_CTL, HOST_IRQ_EN);
552 readl(ai->mmio + HOST_CTL); /* flush */
553
554 /* enable interrupts on PCI-level (PCI 2.3 added a feature to disable ints) */
555 /* pci_enable_int(ai->bus, ai->dev_func); */
556
557 return(0);
558}
559
560/******************************************************************************
561 * Reset specified port. This function is typically called during adapter
562 * initialization and first gets the port into a defined status, then resets
563 * the port by sending a COMRESET signal.
564 *
565 * This function is also the location of the link speed initialization (link
566 * needs to be restablished after changing link speed, anyway).
567 *
568 * NOTE: This function uses a busy loop to wait for DMA engines to stop and
569 * the COMRESET to complete. It should only be called at task time
570 * during initialization or in a context hook.
571 */
572int ahci_reset_port(AD_INFO *ai, int p, int ei)
573{
574 u8 _far *port_mmio = port_base(ai, p);
575 u32 tmp;
576 int timeout = 5000;
577
578 dprintf("resetting port %d.%d\n", ad_no(ai), p);
579
580 /* stop port engines (we don't care whether there is an error doing so) */
581 ahci_stop_port(ai, p);
582
583 /* clear SError */
584 tmp = readl(port_mmio + PORT_SCR_ERR);
585 ddprintf(" PORT_SCR_ERR = 0x%lx\n", tmp);
586 writel(port_mmio + PORT_SCR_ERR, tmp);
587
588 /* clear pending port IRQs */
589 tmp = readl(port_mmio + PORT_IRQ_STAT);
590 ddprintf("PORT_IRQ_STAT was 0x%lx\n", tmp);
591 if (tmp) {
592 writel(port_mmio + PORT_IRQ_STAT, tmp);
593 }
594 ddprintf(" PORT_IRQ_STAT = 0x%lx\n", tmp);
595 ddprintf(" PORT_IRQ_MASK = 0x%lx\n", readl(port_mmio + PORT_IRQ_MASK));
596 ddprintf(" HOST_IRQ_STAT = 0x%lx\n", readl(ai->mmio + HOST_IRQ_STAT));
597 writel(ai->mmio + HOST_IRQ_STAT, 1UL << p);
598
599 /* set link speed */
600 tmp = readl(port_mmio + PORT_SCR_CTL) & ~0x000000f0UL;
601 writel(port_mmio + PORT_SCR_CTL, tmp | (link_speed[ad_no(ai)][p] << 4));
602
603 /* issue COMRESET on the port */
604 tmp = readl(port_mmio + PORT_SCR_CTL) & ~0x0000000fUL;
605 writel(port_mmio + PORT_SCR_CTL, tmp | 1);
606 readl(port_mmio + PORT_SCR_CTL); /* flush */
607
608 /* spec says "leave reset bit on for at least 1ms"; make it 2ms */
609 mdelay(2);
610
611 writel(port_mmio + PORT_SCR_CTL, tmp);
612 readl(port_mmio + PORT_SCR_CTL); /* flush */
613
614 /* wait for communication to be re-established after port reset */
615 while (((tmp = readl(port_mmio + PORT_SCR_STAT) & 3)) != 3) {
616 mdelay(10);
617 timeout -= 10;
618 if (timeout <= 0) {
619 dprintf("no device present after resetting port #%d "
620 "(PORT_SCR_STAT = 0x%lx)\n", p, tmp);
621 return(-1);
622 }
623 }
624
625 /* clear SError again (recommended by AHCI spec) */
626 tmp = readl(port_mmio + PORT_SCR_ERR);
627 writel(port_mmio + PORT_SCR_ERR, tmp);
628
629 /* start port so we can receive the COMRESET FIS */
630 ahci_start_port(ai, p, ei);
631
632 /* wait for device to be ready ((PxTFD & (BSY | DRQ | ERR)) == 0) */
633 while (((tmp = readl(port_mmio + PORT_TFDATA)) & 0x89) != 0) {
634 mdelay(10);
635 timeout -= 10;
636 if (timeout <= 0) {
637 dprintf("device not ready on port #%d "
638 "(PORT_TFDATA = 0x%lx)\n", p, tmp);
639 ahci_stop_port(ai, p);
640 return(-1);
641 }
642 }
643 ddprintf(" PORT_TFDATA = 0x%lx\n", readl(port_mmio + PORT_TFDATA));
644
645 return(0);
646}
647
648/******************************************************************************
649 * Start specified port.
650 */
651int ahci_start_port(AD_INFO *ai, int p, int ei)
652{
653 u8 _far *port_mmio = port_base(ai, p);
654 u32 status;
655
656 /* check whether device presence is detected and link established */
657 status = readl(port_mmio + PORT_SCR_STAT);
658 ddprintf(" PORT_SCR_STAT = 0x%lx\n", status);
659 if ((status & 0xf) != 3) {
660 return(-1);
661 }
662
663 /* clear SError, if any */
664 status = readl(port_mmio + PORT_SCR_ERR);
665 ddprintf(" PORT_SCR_ERR = 0x%lx\n", status);
666 writel(port_mmio + PORT_SCR_ERR, status);
667
668 /* enable FIS reception */
669 ahci_start_fis_rx(ai, p);
670
671 /* enable command engine */
672 ahci_start_engine(ai, p);
673
674 if (ei) {
675 /* clear any pending interrupts on this port */
676 if ((status = readl(port_mmio + PORT_IRQ_STAT)) != 0) {
677 writel(port_mmio + PORT_IRQ_STAT, status);
678 }
679
680 /* enable port interrupts */
681 writel(port_mmio + PORT_IRQ_MASK, PORT_IRQ_TF_ERR |
682 PORT_IRQ_HBUS_ERR |
683 PORT_IRQ_HBUS_DATA_ERR |
684 PORT_IRQ_IF_ERR |
685 PORT_IRQ_OVERFLOW |
686 PORT_IRQ_BAD_PMP |
687 PORT_IRQ_UNK_FIS |
688 PORT_IRQ_SDB_FIS |
689 PORT_IRQ_D2H_REG_FIS);
690 } else {
691 writel(port_mmio + PORT_IRQ_MASK, 0);
692 }
693 readl(port_mmio + PORT_IRQ_MASK); /* flush */
694
695 return(0);
696}
697
698/******************************************************************************
699 * Start port FIS reception. Copied from Linux AHCI driver and adopted to
700 * OS2AHCI.
701 */
702void ahci_start_fis_rx(AD_INFO *ai, int p)
703{
704 u8 _far *port_mmio = port_base(ai, p);
705 u32 port_dma = port_dma_base_phys(ai, p);
706 u32 tmp;
707
708 /* set comand header and FIS address registers */
709 writel(port_mmio + PORT_LST_ADDR, port_dma + offsetof(AHCI_PORT_DMA, cmd_hdr));
710 writel(port_mmio + PORT_LST_ADDR_HI, 0);
711 writel(port_mmio + PORT_FIS_ADDR, port_dma + offsetof(AHCI_PORT_DMA, rx_fis));
712 writel(port_mmio + PORT_FIS_ADDR_HI, 0);
713
714 /* enable FIS reception */
715 tmp = readl(port_mmio + PORT_CMD);
716 tmp |= PORT_CMD_FIS_RX;
717 writel(port_mmio + PORT_CMD, tmp);
718
719 /* flush */
720 readl(port_mmio + PORT_CMD);
721}
722
723/******************************************************************************
724 * Start port HW engine. Copied from Linux AHCI driver and adopted to OS2AHCI.
725 */
726void ahci_start_engine(AD_INFO *ai, int p)
727{
728 u8 _far *port_mmio = port_base(ai, p);
729 u32 tmp;
730
731 /* start DMA */
732 tmp = readl(port_mmio + PORT_CMD);
733 tmp |= PORT_CMD_START;
734 writel(port_mmio + PORT_CMD, tmp);
735 readl(port_mmio + PORT_CMD); /* flush */
736}
737
738/******************************************************************************
739 * Stop specified port
740 */
741int ahci_stop_port(AD_INFO *ai, int p)
742{
743 u8 _far *port_mmio = port_base(ai, p);
744 int rc;
745
746 /* disable port interrupts */
747 writel(port_mmio + PORT_IRQ_MASK, PORT_IRQ_TF_ERR);
748
749 /* disable FIS reception */
750 if ((rc = ahci_stop_fis_rx(ai, p)) != 0) {
751 dprintf("error: failed to stop FIS receive (%d)\n", rc);
752 return(rc);
753 }
754
755 /* disable command engine */
756 if ((rc = ahci_stop_engine(ai, p)) != 0) {
757 dprintf("error: failed to stop port HW engine (%d)\n", rc);
758 return(rc);
759 }
760
761 /* reset PxSACT register (tagged command queues, not reset by COMRESET) */
762 writel(port_mmio + PORT_SCR_ACT, 0);
763 readl(port_mmio + PORT_SCR_ACT); /* flush */
764
765 return(0);
766}
767
768/******************************************************************************
769 * Stop port FIS reception. Copied from Linux AHCI driver and adopted to
770 * OS2AHCI.
771 *
772 * NOTE: This function uses a busy loop to wait for the DMA engine to stop. It
773 * should only be called at task time during initialization or in a
774 * context hook (e.g. when resetting a port).
775 */
776int ahci_stop_fis_rx(AD_INFO *ai, int p)
777{
778 u8 _far *port_mmio = port_base(ai, p);
779 int timeout = 1000;
780 u32 tmp;
781
782 /* disable FIS reception */
783 tmp = readl(port_mmio + PORT_CMD);
784 tmp &= ~PORT_CMD_FIS_RX;
785 writel(port_mmio + PORT_CMD, tmp);
786
787 /* wait for completion, spec says 500ms, give it 1000 */
788 while (timeout > 0 && (readl(port_mmio + PORT_CMD) & PORT_CMD_FIS_ON)) {
789 mdelay(10);
790 timeout -= 10;
791 }
792
793 return((timeout <= 0) ? -1 : 0);
794}
795
796/******************************************************************************
797 * Stop port HW engine. Copied from Linux AHCI driver and adopted to OS2AHCI.
798 *
799 * NOTE: This function uses a busy loop to wait for the DMA engine to stop. It
800 * should only be called at task time during initialization or in a
801 * context hook (e.g. when resetting a port).
802 */
803int ahci_stop_engine(AD_INFO *ai, int p)
804{
805 u8 _far *port_mmio = port_base(ai, p);
806 int timeout = 500;
807 u32 tmp;
808
809 tmp = readl(port_mmio + PORT_CMD);
810
811 /* check if the port is already stopped */
812 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0) {
813 return 0;
814 }
815
816 /* set port to idle */
817 tmp &= ~PORT_CMD_START;
818 writel(port_mmio + PORT_CMD, tmp);
819
820 /* wait for engine to stop. This could be as long as 500 msec */
821 while (timeout > 0 && (readl(port_mmio + PORT_CMD) & PORT_CMD_LIST_ON)) {
822 mdelay(10);
823 timeout -= 10;
824 }
825
826 return((timeout <= 0) ? -1 : 0);
827}
828
829/******************************************************************************
830 * Execute AHCI command for given IORB. This includes all steps typically
831 * required by any of the ahci_*() IORB processing functions.
832 *
833 * NOTE: In order to prevent race conditions with port restart and reset
834 * handlers, we either need to keep the spinlock during the whole
835 * operation or set the adapter's busy flag. Since the expectation
836 * is that command preparation will be quick (it certainly doesn't
837 * involve delays), we're going with the spinlock for the time being.
838 */
839void ahci_exec_iorb(IORBH _far *iorb, int ncq_capable,
840 int (*func)(IORBH _far *, int))
841{
842 volatile u32 *cmds;
843 ADD_WORKSPACE _far *aws = add_workspace(iorb);
844 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
845 P_INFO *port = ai->ports + iorb_unit_port(iorb);
846 ULONG timeout = (iorb->Timeout > 0) ? iorb->Timeout : DEFAULT_TIMEOUT;
847 u8 _far *port_mmio = port_base(ai, iorb_unit_port(iorb));
848 u16 cmd_max = ai->cmd_max;
849 int i;
850
851 /* Enable AHCI mode; apparently, the AHCI mode may end up becoming
852 * disabled, either during the boot sequence (by the BIOS) or by
853 * something else. The Linux AHCI drivers have this call in the
854 * command processing chain, and apparently for a good reason because
855 * without this, commands won't be executed.
856 */
857 ahci_enable_ahci(ai);
858
859 /* determine whether this will be an NCQ request */
860 aws->is_ncq = 0;
861 if (ncq_capable && port->devs[iorb_unit_device(iorb)].ncq_max > 1 &&
862 (ai->cap & HOST_CAP_NCQ) && !aws->no_ncq && init_complete) {
863
864 /* We can make this an NCQ request; limit command slots to the maximum
865 * NCQ tag number reported by the device - 1. Why "minus one"? I seem to
866 * recall an issue related to using all 32 tag numbers but can't quite
867 * pinpoint it right now. One less won't make much of a difference...
868 */
869 aws->is_ncq = 1;
870 if ((cmd_max = port->devs[iorb_unit_device(iorb)].ncq_max - 1) > ai->cmd_max) {
871 cmd_max = ai->cmd_max;
872 }
873 ddprintf("NCQ command; cmd_max = %d->%d\n", (u16) ai->cmd_max, cmd_max);
874 }
875
876 /* make sure adapter is available */
877 spin_lock(drv_lock);
878 if (!ai->busy) {
879
880 if (!init_complete) {
881 ai->busy = 1;
882 spin_unlock(drv_lock);
883 ahci_exec_polled_iorb(iorb, func, timeout);
884 ai->busy = 0;
885 return;
886 }
887
888 /* make sure we don't mix NCQ and regular commands */
889 if (aws->is_ncq && port->reg_cmds == 0 || !aws->is_ncq && port->ncq_cmds == 0) {
890
891 /* Find next available command slot. We use a simple round-robin
892 * algorithm for this to prevent commands with higher slot indexes
893 * from stalling when new commands are coming in frequently.
894 */
895 cmds = (aws->is_ncq) ? &port->ncq_cmds : &port->reg_cmds;
896 for (i = 0; i <= cmd_max; i++) {
897 if (++(port->cmd_slot) > cmd_max) {
898 port->cmd_slot = 0;
899 }
900 if ((*cmds & (1UL << port->cmd_slot)) == 0) {
901 break;
902 }
903 }
904
905 if ((*cmds & (1UL << port->cmd_slot)) == 0) {
906 /* prepare command */
907 if (func(iorb, port->cmd_slot)) {
908 /* Command preparation failed, or no HW command required; IORB
909 * will already have the error code if there was an error.
910 */
911 spin_unlock(drv_lock);
912 iorb_done(iorb);
913 return;
914 }
915
916 /* start timer for this IORB */
917 ADD_StartTimerMS(&aws->timer, timeout, (PFN) timeout_callback, iorb, 0);
918
919 /* update IORB */
920 aws->queued_hw = 1;
921 aws->cmd_slot = port->cmd_slot;
922
923 /* issue command to hardware */
924 ddprintf("issuing command on slot %d\n", port->cmd_slot);
925 *cmds |= (1UL << port->cmd_slot);
926 if (aws->is_ncq) {
927 writel(port_mmio + PORT_SCR_ACT, (1UL << port->cmd_slot));
928 readl(port_mmio + PORT_SCR_ACT); /* flush */
929 }
930 writel(port_mmio + PORT_CMD_ISSUE, (1UL << port->cmd_slot));
931 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
932
933 spin_unlock(drv_lock);
934 return;
935 }
936 }
937 }
938
939 /* requeue this IORB; it will be picked up again in trigger_engine() */
940 aws->processing = 0;
941 spin_unlock(drv_lock);
942}
943
944/******************************************************************************
945 * Execute polled IORB command. This function is called by ahci_exec_iorb()
946 * when the initialization has not yet completed. The reasons for polling until
947 * initialization has completed are:
948 *
949 * - We need to restore the BIOS configuration after we're done with this
950 * command because someone might still call int 13h routines; sending
951 * asynchronous commands and waiting for interrupts to indicate completion
952 * won't work in such a scenario.
953 * - Our context hooks won't work while the device managers are initializing
954 * (they can't yield at init time).
955 * - The device managers typically poll for command completion during
956 * initialization so it won't make much of a difference, anyway.
957 *
958 * NOTE: This function must be called with the adapter-level busy flag set but
959 * without the driver-level spinlock held.
960 */
961void ahci_exec_polled_iorb(IORBH _far *iorb, int (*func)(IORBH _far *, int),
962 ULONG timeout)
963{
964 AHCI_PORT_CFG *pc = NULL;
965 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
966 int p = iorb_unit_port(iorb);
967 u8 _far *port_mmio = port_base(ai, p);
968
969 /* enable AHCI mode */
970 if (ahci_enable_ahci(ai) != 0) {
971 iorb_seterr(iorb, IOERR_ADAPTER_NONSPECIFIC);
972 goto restore_bios_config;
973 }
974
975 /* check whether command slot 0 is available */
976 if ((readl(port_mmio + PORT_CMD_ISSUE) & 1) != 0) {
977 iorb_seterr(iorb, IOERR_DEVICE_BUSY);
978 goto restore_bios_config;
979 }
980
981 /* save port configuration */
982 if ((pc = ahci_save_port_config(ai, p)) == NULL) {
983 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
984 goto restore_bios_config;
985 }
986
987 /* restart port (includes the necessary port configuration) */
988 if (ahci_stop_port(ai, p) || ahci_start_port(ai, p, 0)) {
989 iorb_seterr(iorb, IOERR_ADAPTER_NONSPECIFIC);
990 goto restore_bios_config;
991 }
992
993 /* prepare command */
994 if (func(iorb, 0) == 0) {
995 /* successfully prepared cmd; issue cmd and wait for completion */
996 ddprintf("executing polled cmd...");
997 writel(port_mmio + PORT_CMD_ISSUE, 1);
998 timeout /= 10;
999 while (timeout > 0 && (readl(port_mmio + PORT_CMD_ISSUE) & 1)) {
1000 mdelay(10);
1001 timeout--;
1002 }
1003 ddprintf(" done (time left = %ld)\n", timeout * 10);
1004
1005 if (timeout == 0) {
1006 dprintf("timeout for IORB %Fp\n", iorb);
1007 iorb_seterr(iorb, IOERR_ADAPTER_TIMEOUT);
1008
1009 } else if (readl(port_mmio + PORT_SCR_ERR) != 0 ||
1010 readl(port_mmio + PORT_TFDATA) & 0x89) {
1011 dprintf("polled cmd error for IORB %Fp\n", iorb);
1012 iorb_seterr(iorb, IOERR_DEVICE_NONSPECIFIC);
1013 ahci_reset_port(ai, iorb_unit_port(iorb), 0);
1014
1015 } else {
1016 /* successfully executed command */
1017 if (add_workspace(iorb)->ppfunc != NULL) {
1018 add_workspace(iorb)->ppfunc(iorb);
1019 } else {
1020 add_workspace(iorb)->complete = 1;
1021 }
1022 }
1023 }
1024
1025restore_bios_config:
1026 /* restore BIOS configuration */
1027 if (pc != NULL) {
1028 ahci_restore_port_config(ai, p, pc);
1029 }
1030 ahci_restore_bios_config(ai);
1031
1032 if (add_workspace(iorb)->complete | (iorb->Status | IORB_ERROR)) {
1033 iorb_done(iorb);
1034 }
1035 return;
1036}
1037
1038/******************************************************************************
1039 * Execute polled ATA/ATAPI command. This function will block until the command
1040 * has completed or the timeout has expired, thus it should only be used during
1041 * initialization. Furthermore, it will always use command slot zero.
1042 *
1043 * The difference to ahci_exec_polled_iorb() is that this function executes
1044 * arbitrary ATA/ATAPI commands outside the context of an IORB. It's typically
1045 * used when scanning for devices during initialization.
1046 */
1047int ahci_exec_polled_cmd(AD_INFO *ai, int p, int d, int timeout, int cmd, ...)
1048{
1049 va_list va;
1050 u8 _far *port_mmio = port_base(ai, p);
1051 u32 tmp;
1052 int rc;
1053
1054 /* verify that command slot 0 is idle */
1055 if (readl(port_mmio + PORT_CMD_ISSUE) & 1) {
1056 ddprintf("port %d slot 0 is not idle; not executing polled cmd\n", p);
1057 return(-1);
1058 }
1059
1060 /* fill in command slot 0 */
1061 va_start(va, cmd);
1062 if ((rc = v_ata_cmd(ai, p, d, 0, cmd, va)) != 0) {
1063 return(rc);
1064 }
1065
1066 /* start command execution for slot 0 */
1067 ddprintf("executing polled cmd...");
1068 writel(port_mmio + PORT_CMD_ISSUE, 1);
1069
1070 /* wait until command has completed */
1071 while (timeout > 0 && (readl(port_mmio + PORT_CMD_ISSUE) & 1)) {
1072 mdelay(10);
1073 timeout -= 10;
1074 }
1075 ddprintf(" done (time left = %d)\n", timeout);
1076
1077 /* check error condition */
1078 if ((tmp = readl(port_mmio + PORT_SCR_ERR)) != 0) {
1079 dprintf("SERR = 0x%08lx\n", tmp);
1080 timeout = 0;
1081 }
1082 if (((tmp = readl(port_mmio + PORT_TFDATA)) & 0x89) != 0) {
1083 dprintf("TFDATA = 0x%08lx\n", tmp);
1084 timeout = 0;
1085 }
1086
1087 if (timeout <= 0) {
1088 ahci_reset_port(ai, p, 0);
1089 return(-1);
1090 }
1091 return(0);
1092}
1093
1094/******************************************************************************
1095 * Flush write cache of the specified device. Since there's no equivalent IORB
1096 * command, we'll execute this command directly using polling. Otherwise, we
1097 * would have to create a fake IORB, add it to the port's IORB queue, ...
1098 *
1099 * Besides, this function is only called when shutting down and the code there
1100 * would have to wait for the flush cache command to complete as well, using
1101 * polling just the same...
1102 */
1103int ahci_flush_cache(AD_INFO *ai, int p, int d)
1104{
1105 if (!ai->ports[p].devs[d].atapi) {
1106 dprintf("flushing cache on %d.%d.%d\n", ad_no(ai), p, d);
1107 return(ahci_exec_polled_cmd(ai, p, d, 30000,
1108 ai->ports[p].devs[d].lba48 ? ATA_CMD_FLUSH_EXT
1109 : ATA_CMD_FLUSH,
1110 AP_END));
1111 }
1112}
1113
1114/******************************************************************************
1115 * set device into IDLE mode (spin down); this was used during
1116 * debugging/testing and is still there since it does not hurt...
1117 * If 'idle' is != 0, the idle timeout is set to 5 seconds, otherwise it
1118 * is turned off.
1119 */
1120int ahci_set_dev_idle(AD_INFO *ai, int p, int d, int idle)
1121{
1122 ddprintf("sending IDLE=%d command to port %d\n", idle, p);
1123 return ahci_exec_polled_cmd(ai, p, d, 500, ATA_CMD_IDLE, AP_COUNT,
1124 idle ? 1 : 0, AP_END);
1125}
1126
1127/******************************************************************************
1128 * AHCI top-level hardware interrupt handler. This handler finds the adapters
1129 * and ports which have issued the interrupt and calls the corresponding
1130 * port interrupt handler.
1131 *
1132 * On entry, OS/2 will have processor interrupts enabled because we're using
1133 * shared IRQs but we won't be preempted by another interrupt on the same
1134 * IRQ level until we indicated EOI. We'll keep it this way, only requesting
1135 * the driver-level spinlock when actually changing the driver state (IORB
1136 * queues, ...)
1137 */
1138int ahci_intr(u16 irq)
1139{
1140 u32 irq_stat;
1141 int handled = 0;
1142 int a;
1143 int p;
1144
1145 /* find adapter(s) with pending interrupts */
1146 for (a = 0; a < ad_info_cnt; a++) {
1147 AD_INFO *ai = ad_infos + a;
1148
1149 if (ai->irq == irq && (irq_stat = readl(ai->mmio + HOST_IRQ_STAT)) != 0) {
1150 /* this adapter has interrupts pending */
1151 u32 irq_masked = irq_stat & ai->port_map;
1152
1153 for (p = 0; p <= ai->port_max; p++) {
1154 if (irq_masked & (1UL << p)) {
1155 ahci_port_intr(ai, p);
1156 }
1157 }
1158
1159 /* clear interrupt condition on the adapter */
1160 writel(ai->mmio + HOST_IRQ_STAT, irq_stat);
1161 readl(ai->mmio + HOST_IRQ_STAT); /* flush */
1162 handled = 1;
1163 }
1164 }
1165
1166 if (handled) {
1167 /* trigger state machine to process next IORBs, if any */
1168 spin_lock(drv_lock);
1169 trigger_engine();
1170 spin_unlock(drv_lock);
1171
1172 /* complete the interrupt */
1173 DevHelp_EOI(irq);
1174 return(0);
1175 } else {
1176 return(1);
1177 }
1178}
1179
1180/******************************************************************************
1181 * AHCI port-level interrupt handler. As described above, processor interrupts
1182 * are enabled on entry thus we have to protect shared resources with a
1183 * spinlock.
1184 */
1185void ahci_port_intr(AD_INFO *ai, int p)
1186{
1187 IORB_QUEUE done_queue;
1188 IORBH _far *iorb;
1189 IORBH _far *next = NULL;
1190 u8 _far *port_mmio = port_base(ai, p);
1191 u32 irq_stat;
1192 u32 active_cmds;
1193 u32 done_mask;
1194
1195 ddprintf("port interrupt for adapter #%d, port #%d\n", ad_no(ai), p);
1196 memset(&done_queue, 0x00, sizeof(done_queue));
1197
1198 /* get interrupt status and clear it right away */
1199 irq_stat = readl(port_mmio + PORT_IRQ_STAT);
1200 writel(port_mmio + PORT_IRQ_STAT, irq_stat);
1201 readl(port_mmio + PORT_IRQ_STAT); /* flush */
1202
1203 if (irq_stat & PORT_IRQ_ERROR) {
1204 /* this is an error interrupt */
1205 ahci_error_intr(ai, p, irq_stat);
1206 return;
1207 }
1208
1209 spin_lock(drv_lock);
1210
1211 /* Find out which command slots have completed. Since error recovery for
1212 * NCQ commands interfers with non-NCQ commands, the upper layers will
1213 * make sure there's never a mixture of NCQ and non-NCQ commands active
1214 * on any port at any given time. This makes it easier to find out which
1215 * commands have completed, too.
1216 */
1217 if (ai->ports[p].ncq_cmds != 0) {
1218 active_cmds = readl(port_mmio + PORT_SCR_ACT);
1219 done_mask = ai->ports[p].ncq_cmds ^ active_cmds;
1220 ddprintf("[ncq_cmds]: active_cmds = 0x%08lx, done_mask = 0x%08lx\n",
1221 active_cmds, done_mask);
1222 } else {
1223 active_cmds = readl(port_mmio + PORT_CMD_ISSUE);
1224 done_mask = ai->ports[p].reg_cmds ^ active_cmds;
1225 ddprintf("[reg_cmds]: active_cmds = 0x%08lx, done_mask = 0x%08lx\n",
1226 active_cmds, done_mask);
1227 }
1228
1229 /* Find the IORBs related to the completed commands and complete them.
1230 *
1231 * NOTES: The spinlock must not be released while in this loop to prevent
1232 * race conditions with timeout handlers or other threads in SMP
1233 * systems.
1234 *
1235 * Since we hold the spinlock when IORBs complete, we can't call the
1236 * IORB notification routine right away because this routine might
1237 * schedule another IORB which could cause a deadlock. Thus, we'll
1238 * add all IORBs to be completed to a temporary queue which will be
1239 * processed after releasing the spinlock.
1240 */
1241 for (iorb = ai->ports[p].iorb_queue.root; iorb != NULL; iorb = next) {
1242 ADD_WORKSPACE _far *aws = (ADD_WORKSPACE _far *) &iorb->ADDWorkSpace;
1243 next = iorb->pNxtIORB;
1244 if (aws->queued_hw && (done_mask & (1UL << aws->cmd_slot))) {
1245 /* this command has completed */
1246 if (aws->ppfunc != NULL) {
1247 aws->ppfunc(iorb);
1248 } else {
1249 aws->complete = 1;
1250 }
1251
1252 if (aws->complete) {
1253 /* this IORB is complete; move IORB to our temporary done queue */
1254 iorb_queue_del(&ai->ports[p].iorb_queue, iorb);
1255 iorb_queue_add(&done_queue, iorb);
1256 }
1257
1258 /* clear corresponding bit in issued command bitmaps */
1259 ai->ports[p].ncq_cmds &= ~(1UL << aws->cmd_slot);
1260 ai->ports[p].reg_cmds &= ~(1UL << aws->cmd_slot);
1261 }
1262 }
1263
1264 spin_unlock(drv_lock);
1265
1266 /* call notification routines for all IORBs in the done queue */
1267 for (iorb = done_queue.root; iorb != NULL; iorb = next) {
1268 next = iorb->pNxtIORB;
1269 iorb->Status = IORB_DONE;
1270 aws_free(add_workspace(iorb));
1271 if (iorb->RequestControl & IORB_ASYNC_POST) {
1272 iorb->NotifyAddress(iorb);
1273 }
1274 }
1275}
1276
1277/******************************************************************************
1278 * AHCI error interrupt handler. Errors include interface errors and device
1279 * errors (usually triggered by the error bit in the AHCI task file register).
1280 *
1281 * Since this involves long-running operations such as restarting or even
1282 * resetting a port, this function is invoked at task time via a context
1283 * hook.
1284 *
1285 * NOTE: AHCI controllers stop all processing when encountering an error
1286 * condition in order to give the driver time to find out what exactly
1287 * went wrong. This means no new commands will be processed until we
1288 * clear the error register and restore the "commands issued" register.
1289 */
1290void ahci_error_intr(AD_INFO *ai, int p, u32 irq_stat)
1291{
1292 int reset_port = 0;
1293
1294 /* Handle adapter and interface errors. Those typically require a port
1295 * reset, or worse.
1296 */
1297 if (irq_stat & PORT_IRQ_UNK_FIS) {
1298 u32 _far *unk = (u32 _far *) (port_dma_base(ai, p)->rx_fis + RX_FIS_UNK);
1299 dprintf("warning: unknown FIS %08lx %08lx %08lx %08lx\n",
1300 unk[0], unk[1], unk[2], unk[3]);
1301 reset_port = 1;
1302 }
1303 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1304 dprintf("warning: host bus [data] error for port #%d\n", p);
1305 reset_port = 1;
1306 }
1307 if (irq_stat & PORT_IRQ_IF_ERR && !(ai->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)) {
1308 dprintf("warning: interface fatal error for port #%d\n", p);
1309 reset_port = 1;
1310 }
1311 if (reset_port) {
1312 /* need to reset the port; leave this to the reset context hook */
1313 ports_to_reset[ad_no(ai)] |= 1UL << p;
1314 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
1315
1316 /* no point analyzing device errors after a reset... */
1317 return;
1318 }
1319
1320 /* Handle device-specific errors. Those errors typically involve restarting
1321 * the corresponding port to resume operations which can take some time,
1322 * thus we need to offload this functionality to the restart context hook.
1323 */
1324 if (irq_stat & PORT_IRQ_TF_ERR) {
1325 ports_to_restart[ad_no(ai)] |= 1UL << p;
1326 DevHelp_ArmCtxHook(0, restart_ctxhook_h);
1327 }
1328}
1329
1330/******************************************************************************
1331 * Get device or media geometry. Device and media geometry are expected to be
1332 * the same for non-removable devices.
1333 */
1334void ahci_get_geometry(IORBH _far *iorb)
1335{
1336 dprintf("ahci_get_geometry(%d.%d.%d)\n", (int) iorb_unit_adapter(iorb),
1337 (int) iorb_unit_port(iorb), (int) iorb_unit_device(iorb));
1338
1339 ahci_exec_iorb(iorb, 0, cmd_func(iorb, get_geometry));
1340}
1341
1342/******************************************************************************
1343 * Test whether unit is ready.
1344 */
1345void ahci_unit_ready(IORBH _far *iorb)
1346{
1347 dprintf("ahci_unit_ready(%d.%d.%d)\n", (int) iorb_unit_adapter(iorb),
1348 (int) iorb_unit_port(iorb), (int) iorb_unit_device(iorb));
1349
1350 ahci_exec_iorb(iorb, 0, cmd_func(iorb, unit_ready));
1351}
1352
1353/******************************************************************************
1354 * Read sectors from AHCI device.
1355 */
1356void ahci_read(IORBH _far *iorb)
1357{
1358 dprintf("ahci_read(%d.%d.%d, %ld, %ld)\n", (int) iorb_unit_adapter(iorb),
1359 (int) iorb_unit_port(iorb), (int) iorb_unit_device(iorb),
1360 (long) ((IORB_EXECUTEIO _far *) iorb)->RBA,
1361 (long) ((IORB_EXECUTEIO _far *) iorb)->BlockCount);
1362
1363 ahci_exec_iorb(iorb, 1, cmd_func(iorb, read));
1364}
1365
1366/******************************************************************************
1367 * Verify readability of sectors on AHCI device.
1368 */
1369void ahci_verify(IORBH _far *iorb)
1370{
1371 dprintf("ahci_verify(%d.%d.%d, %ld, %ld)\n", (int) iorb_unit_adapter(iorb),
1372 (int) iorb_unit_port(iorb), (int) iorb_unit_device(iorb),
1373 (long) ((IORB_EXECUTEIO _far *) iorb)->RBA,
1374 (long) ((IORB_EXECUTEIO _far *) iorb)->BlockCount);
1375
1376 ahci_exec_iorb(iorb, 0, cmd_func(iorb, verify));
1377}
1378
1379/******************************************************************************
1380 * Write sectors to AHCI device.
1381 */
1382void ahci_write(IORBH _far *iorb)
1383{
1384 dprintf("ahci_write(%d.%d.%d, %ld, %ld)\n", (int) iorb_unit_adapter(iorb),
1385 (int) iorb_unit_port(iorb), (int) iorb_unit_device(iorb),
1386 (long) ((IORB_EXECUTEIO _far *) iorb)->RBA,
1387 (long) ((IORB_EXECUTEIO _far *) iorb)->BlockCount);
1388
1389 ahci_exec_iorb(iorb, 1, cmd_func(iorb, write));
1390}
1391
1392/******************************************************************************
1393 * Execute SCSI (ATAPI) command.
1394 */
1395void ahci_execute_cdb(IORBH _far *iorb)
1396{
1397 int a = iorb_unit_adapter(iorb);
1398 int p = iorb_unit_port(iorb);
1399 int d = iorb_unit_device(iorb);
1400
1401 if (ad_infos[a].ports[p].devs[d].atapi) {
1402 ahci_exec_iorb(iorb, 0, atapi_execute_cdb);
1403 } else {
1404 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1405 iorb_done(iorb);
1406 }
1407}
1408
1409/******************************************************************************
1410 * Execute ATA command.
1411 */
1412void ahci_execute_ata(IORBH _far *iorb)
1413{
1414 int a = iorb_unit_adapter(iorb);
1415 int p = iorb_unit_port(iorb);
1416 int d = iorb_unit_device(iorb);
1417
1418 dphex(((IORB_ADAPTER_PASSTHRU _far *) iorb)->pControllerCmd,
1419 ((IORB_ADAPTER_PASSTHRU _far *) iorb)->ControllerCmdLen,
1420 "ahci_execute_cdb(%d.%d.%d)", a, p, d);
1421
1422 if (ad_infos[a].ports[p].devs[d].atapi) {
1423 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1424 iorb_done(iorb);
1425 } else {
1426 ahci_exec_iorb(iorb, 0, ata_execute_ata);
1427 }
1428}
1429
1430/******************************************************************************
1431 * Set up device attached to the specified port based on ATA_IDENTFY_DEVICE or
1432 * ATA_IDENTFY_PACKET_DEVICE data.
1433 *
1434 * NOTE: Port multipliers are not supported, yet, thus the device number is
1435 * expected to be 0 for the time being.
1436 */
1437static void ahci_setup_device(AD_INFO *ai, int p, int d, u16 *id_buf)
1438{
1439 DEVICESTRUCT ds;
1440 ADJUNCT adj;
1441 HDEVICE dh;
1442 char dev_name[RM_MAX_PREFIX_LEN+ATA_ID_PROD_LEN+1];
1443 static u8 total_dev_cnt;
1444
1445 if (ai->port_max < p) {
1446 ai->port_max = p;
1447 }
1448 if (ai->ports[p].dev_max < d) {
1449 ai->ports[p].dev_max = d;
1450 }
1451 memset(ai->ports[p].devs + d, 0x00, sizeof(*ai->ports[p].devs));
1452
1453 /* set generic device information (assuming an ATA disk device for now) */
1454 ai->ports[p].devs[d].present = 1;
1455 ai->ports[p].devs[d].removable = (id_buf[ATA_ID_CONFIG] & 0x0080U) != 0;
1456 ai->ports[p].devs[d].dev_type = UIB_TYPE_DISK;
1457
1458 if (id_buf[ATA_ID_CONFIG] & 0x8000U) {
1459 /* this is an ATAPI device; augment device information */
1460 ai->ports[p].devs[d].atapi = 1;
1461 ai->ports[p].devs[d].atapi_16 = (id_buf[ATA_ID_CONFIG] & 0x0001U) != 0;
1462 ai->ports[p].devs[d].dev_type = (id_buf[ATA_ID_CONFIG] & 0x1f00U) >> 8;
1463
1464 } else {
1465 /* complete ATA-specific device information */
1466 if (disable_ncq[ad_no(ai)][p]) {
1467 /* MT: set ncq_max to 1 if NCQ is disabled for this port */
1468 ai->ports[p].devs[d].ncq_max = 1;
1469 dprintf("NCQ off for a:%d p:%d\n", (int) ad_no(ai), p);
1470 } else {
1471 ai->ports[p].devs[d].ncq_max = max(id_buf[ATA_ID_QUEUE_DEPTH] & 0x001fU, 1);
1472 dprintf("NCQ max=%d for a:%d p:%d\n", ai->ports[p].devs[d].ncq_max, (int) ad_no(ai), p);
1473 }
1474
1475 if (id_buf[ATA_ID_CFS_ENABLE_2] & 0x0400U) {
1476 ai->ports[p].devs[d].lba48 = 1;
1477 }
1478 }
1479
1480 dprintf("found device %d.%d.%d: removable = %d, dev_type = %d, atapi = %d\n",
1481 ad_no(ai), p, d,
1482 ai->ports[p].devs[d].removable,
1483 ai->ports[p].devs[d].dev_type,
1484 ai->ports[p].devs[d].atapi);
1485
1486 /* add device to resource manager; we don't really care about errors here */
1487 memset(&ds, 0x00, sizeof(ds));
1488 memset(&adj, 0x00, sizeof(adj));
1489
1490 adj.pNextAdj = NULL;
1491 adj.AdjLength = sizeof(adj);
1492 adj.AdjType = ADJ_ADD_UNIT;
1493 adj.Add_Unit.ADDHandle = rm_drvh;
1494 adj.Add_Unit.UnitHandle = (USHORT) total_dev_cnt;
1495
1496 /* create Resource Manager device key string;
1497 * we distinguish only HDs and CD drives for now
1498 */
1499 if (ai->ports[p].devs[d].removable) {
1500 sprintf(dev_name, RM_CD_PREFIX "%s", p, d, ata_dev_name(id_buf));
1501 } else {
1502 sprintf(dev_name, RM_HD_PREFIX "%s", p, d, ata_dev_name(id_buf));
1503 }
1504
1505 ds.DevDescriptName = dev_name;
1506 ds.DevFlags = (ai->ports[p].devs[d].removable) ? DS_REMOVEABLE_MEDIA
1507 : DS_FIXED_LOGICALNAME;
1508 ds.DevType = ai->ports[p].devs[d].dev_type;
1509 ds.pAdjunctList = &adj;
1510
1511 RMCreateDevice(rm_drvh, &dh, &ds, ai->rm_adh, NULL);
1512
1513 total_dev_cnt++;
1514
1515 /* try to detect virtualbox environment to enable a hack for IRQ routing */
1516 if (ai == ad_infos && p == 7 &&
1517 ai->pci->vendor == 0x8086 && ai->pci->device == 0x2829 &&
1518 !memcmp(ata_dev_name(id_buf), "VBOX HARDDISK", 13)) {
1519 /* running inside virtualbox */
1520 pci_hack_virtualbox();
1521 }
1522}
1523
1524
Note: See TracBrowser for help on using the repository browser.