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