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