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

Last change on this file since 102 was 102, checked in by Markus Thielen, 14 years ago

avoid possible IRQ storm during error handling

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