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

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