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

Last change on this file since 54 was 53, checked in by markus, 15 years ago

version with lba mapping (read) test

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