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

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

Fixed IOCtl pass-thru interface.
Cosmetic changes to user interface.
Removed old IBM smartctl exe from distribution.

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