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

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

fixed readtest to run under OS/2 Ring3 above 2GB; commented out ADD read sector dump code

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