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

Last change on this file since 169 was 169, checked in by David Azarewicz, 12 years ago

Added a check for bad geometries reported by the BIOS and fix them.

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