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

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

Enhanced debug output
Fixed a long time PCI ID bug.

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