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

Last change on this file since 191 was 191, checked in by David Azarewicz, 8 years ago

Misc fixes. NULL pointer fixes.

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