source: trunk/src/os2ahci/os2ahci.c@ 75

Last change on this file since 75 was 75, checked in by chris, 15 years ago

atapi.c:

  • Extended workaround for incorrect direction bits to "mode sense" commands; now there's no bus lockup and subsequent port reset during boot anymore.

ahci.c:

  • Fixed timeout handling -- we use milliseconds internally while the IORB uses seconds
  • Replaced IORB completion code with a macro to improve IORB debug logging

ctxhook.c:

  • Fixed an issue which would fail to clear a port command bit if the IORB in question was not the one that caused the port engine to stop. As a result, a subsequent sanity check caused an unecessary port reset. The result was that ejecting a CD via WPS' drive object would cause the next command to fail, a reset to occur and the tray to close again right away
  • Replaced IORB completion code with a macro to improve IORB debug logging
  • added spinlocks around aws_free()

os2ahci.c

  • Replaced IORB completion code with a macro to improve IORB debug logging
  • Improved IORB debug logging to make it easier to spot the lifetime of an IORB without having to read between the lines

os2ahci.h

  • Added macro to complete an IORB including the corresponding debug log
File size: 36.9 KB
Line 
1/******************************************************************************
2 * os2ahci.c - main file for os2ahci driver
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 "bldday.h"
24
25/* -------------------------- macros and constants ------------------------- */
26
27/* parse integer command line parameter */
28#define drv_parm_int(s, value, type, radix) \
29 { \
30 char _far *_ep; \
31 if ((s)[1] != ':') { \
32 cprintf("missing colon (:) after /%c\n", *(s)); \
33 goto init_fail; \
34 } \
35 value = (type) strtol((s) + 2, \
36 (const char _far* _far*) &_ep, \
37 radix); \
38 s = _ep; \
39 }
40
41/* ------------------------ typedefs and structures ------------------------ */
42
43/* -------------------------- function prototypes -------------------------- */
44
45void _cdecl small_code_ (void);
46
47/* ------------------------ global/static variables ------------------------ */
48
49int debug = 0; /* if > 0, print debug messages to COM1 */
50int thorough_scan; /* if != 0, perform thorough PCI scan */
51int init_reset; /* if != 0, reset ports during init */
52
53PFN Device_Help = 0; /* pointer to device helper entry point */
54ULONG RMFlags = 0; /* required by resource manager library */
55PFN RM_Help0 = NULL; /* required by resource manager library */
56PFN RM_Help3 = NULL; /* required by resource manager library */
57HDRIVER rm_drvh; /* resource manager driver handle */
58char rm_drvname[80]; /* driver name as returned by RM */
59USHORT add_handle; /* driver handle (RegisterDeviceClass) */
60UCHAR timer_pool[TIMER_POOL_SIZE]; /* timer pool */
61
62/* resource manager driver information structure */
63DRIVERSTRUCT rm_drvinfo = {
64 "OS2AHCI", /* driver name */
65 "AHCI SATA Driver", /* driver description */
66 "GNU", /* vendor name */
67 CMVERSION_MAJOR, /* RM interface version major */
68 CMVERSION_MINOR, /* RM interface version minor */
69 BLD_YEAR, BLD_MONTH, BLD_DAY, /* date */
70 0, /* driver flags */
71 DRT_ADDDM, /* driver type */
72 DRS_ADD, /* driver sub type */
73 NULL /* driver callback */
74};
75
76ULONG drv_lock; /* driver-level spinlock */
77IORB_QUEUE driver_queue; /* driver-level IORB queue */
78AD_INFO ad_infos[MAX_AD]; /* adapter information list */
79int ad_info_cnt; /* number of entries in ad_infos[] */
80int init_complete; /* if != 0, initialization has completed */
81
82/* apapter/port-specific options saved when parsing the command line */
83u8 link_speed[MAX_AD][AHCI_MAX_PORTS];
84u8 disable_ncq[MAX_AD][AHCI_MAX_PORTS];
85
86static char init_msg[] = "OS2AHCI driver version %d.%02d\n";
87static char exit_msg[] = "OS2AHCI driver *not* installed\n";
88static char eval_msg[] = ANSI_CLR_RED ANSI_CLR_BRIGHT "Evaluation version "
89 "- not licensed for production use.\n" ANSI_RESET;
90
91
92/* ----------------------------- start of code ----------------------------- */
93
94/******************************************************************************
95 * OS/2 device driver main strategy function. This function is only used
96 * for initialization purposes; all other calls go directly to the adapter
97 * device driver's strategy function.
98 */
99USHORT _cdecl c_strat(RPH _far *req)
100{
101 u16 rc;
102
103 switch (req->Cmd) {
104
105 case CMDInitBase:
106 rc = init_drv((RPINITIN _far *) req);
107 break;
108
109 case CMDShutdown:
110 rc = exit_drv(((RPSAVERESTORE _far *) req)->FuncCode);
111 break;
112
113 default:
114 rc = STDON | STATUS_ERR_UNKCMD;
115 break;
116 }
117
118 return(rc);
119}
120
121/******************************************************************************
122 * Intialize the os2ahci driver. This includes command line parsing, scanning
123 * the PCI bus for supported AHCI adapters, etc.
124 */
125USHORT init_drv(RPINITIN _far *req)
126{
127 RPINITOUT _far *rsp = (RPINITOUT _far *) req;
128 DDD_PARM_LIST _far *ddd_pl = (DDD_PARM_LIST _far *) req->InitArgs;
129 APIRET rmrc;
130 char _far *cmd_line;
131 char _far *s;
132 int adapter_index = -1;
133 int port_index = -1;
134 u16 vendor;
135 u16 device;
136
137 /* set device helper entry point */
138 Device_Help = req->DevHlpEP;
139
140 /* create driver-level spinlock */
141 DevHelp_CreateSpinLock(&drv_lock);
142
143 if (debug) {
144 /* initialize debug interface (COM1) */
145 init_com1();
146 }
147
148 /* print initialization message */
149 cprintf(init_msg, VERSION / 100, VERSION % 100);
150 cprintf(eval_msg);
151
152 /* register driver with resource manager */
153 if ((rmrc = RMCreateDriver(&rm_drvinfo, &rm_drvh)) != RMRC_SUCCESS) {
154 cprintf("failed to register driver with resource manager (rc = %d)\n", rmrc);
155 goto init_fail;
156 }
157
158 /* parse command line parameters */
159 cmd_line = (char _far *) ((u32) ddd_pl & 0xffff0000l) + ddd_pl->cmd_line_args;
160
161 for (s = cmd_line; *s != 0; s++) {
162 if (*s == '/' && s[1] != '\0') {
163 s++;
164 switch(tolower(*s)) {
165
166 case 'c':
167 /* set COM port base address for debug messages */
168 drv_parm_int(s, com_base, u16, 16);
169 break;
170
171 case 'd':
172 /* increase debug level */
173 debug++;
174 break;
175
176 case 'i':
177 /* add specfied PCI ID as a supported generic AHCI adapter */
178 drv_parm_int(s, vendor, u16, 16);
179 drv_parm_int(s, device, u16, 16);
180 if (add_pci_id(vendor, device)) {
181 cprintf("failed to add PCI ID %04x:%04x\n", vendor, device);
182 goto init_fail;
183 }
184 thorough_scan = 1;
185 break;
186
187 case 't':
188 /* perform thorough PCI scan (i.e. look for individual supported PCI IDs) */
189 thorough_scan = 1;
190 break;
191
192 case 'r':
193 /* reset ports during initialization */
194 init_reset = 1;
195 break;
196
197 case 'a':
198 /* set adapter index for adapter and port-related options */
199 drv_parm_int(s, adapter_index, int, 10);
200 if (adapter_index < 0 || adapter_index >= MAX_AD) {
201 cprintf("invalid adapter index (%d)\n", adapter_index);
202 goto init_fail;
203 }
204 break;
205
206 case 'p':
207 /* set port index for port-related options */
208 drv_parm_int(s, port_index, int, 10);
209 if (port_index < 0 || port_index >= AHCI_MAX_PORTS) {
210 cprintf("invalid port index (%d)\n", port_index);
211 goto init_fail;
212 }
213 break;
214
215 case 's':
216 /* set link speed of current port on current adapter */
217 drv_parm_int(s, link_speed[adapter_index][port_index], u8, 10);
218 init_reset = 1;
219 break;
220
221 case 'n':
222 /* disable NCQ */
223 if (adapter_index == -1) {
224 /* disable NCQ on all adapters and ports */
225 memset(disable_ncq, 0x01, sizeof(disable_ncq));
226 } else if (port_index == -1) {
227 /* disable NCQ on all ports of this adapter */
228 memset(disable_ncq[adapter_index], 0x01, sizeof(*disable_ncq));
229 } else {
230 /* disable NCQ for this adapter and port */
231 disable_ncq[adapter_index][port_index] = 1;
232 }
233 break;
234
235 default:
236 cprintf("invalid option: /%c\n", *s);
237 goto init_fail;
238 }
239 }
240 }
241
242 /* scan PCI bus for supported devices */
243 scan_pci_bus();
244
245 if (ad_info_cnt > 0) {
246 /* initialization succeeded and we found at least one AHCI adapter */
247 ADD_InitTimer(timer_pool, sizeof(timer_pool));
248 mdelay_cal();
249
250 if (DevHelp_RegisterDeviceClass("OS2AHCI", (PFN) add_entry, 0, 1,
251 &add_handle)) {
252 cprintf("error: couldn't register device class\n");
253 goto init_fail;
254 }
255
256 /* allocate context hooks */
257 if (DevHelp_AllocateCtxHook(mk_NPFN(restart_hook), &restart_ctxhook_h) != 0 ||
258 DevHelp_AllocateCtxHook(mk_NPFN(reset_hook), &reset_ctxhook_h) != 0 ||
259 DevHelp_AllocateCtxHook(mk_NPFN(engine_hook), &engine_ctxhook_h)) {
260 cprintf("failed to allocate task-time context hooks\n");
261 goto init_fail;
262 }
263
264 rsp->CodeEnd = (u16) end_of_code;
265 rsp->DataEnd = (u16) &end_of_data;
266 return(STDON);
267
268 } else {
269 /* no adapters found */
270 cprintf(" No adapters found.\n");
271 }
272
273init_fail:
274 /* initialization failed; set segment sizes to 0 and return error */
275 rsp->CodeEnd = 0;
276 rsp->DataEnd = 0;
277
278 /* free context hooks */
279 if (engine_ctxhook_h != 0) DevHelp_FreeCtxHook(engine_ctxhook_h);
280 if (reset_ctxhook_h != 0) DevHelp_FreeCtxHook(reset_ctxhook_h);
281 if (restart_ctxhook_h != 0) DevHelp_FreeCtxHook(restart_ctxhook_h);
282
283 if (rm_drvh != 0) {
284 /* remove driver from resource manager */
285 RMDestroyDriver(rm_drvh);
286 }
287
288 cprintf(exit_msg);
289 return(STDON | ERROR_I24_QUIET_INIT_FAIL);
290}
291
292/******************************************************************************
293 * Device driver exit handler. This handler is called when OS/2 shuts down and
294 * flushes the write caches of all attached devices.
295 *
296 * NOTE: Errors are ignored because there's no way we could stop the shutdown
297 * or do something about the error, unless retrying endlessly is
298 * considered an option.
299 */
300USHORT exit_drv(int func)
301{
302 int a;
303 int p;
304 int d;
305
306 dprintf("exit_drv(%d) called\n", func);
307
308 if (func == 0) {
309 /* we're only interested in the second phase of the shutdown */
310 return(STDON);
311 }
312
313 for (a = 0; a < ad_info_cnt; a++) {
314 AD_INFO *ai = ad_infos + a;
315
316 for (p = 0; p <= ai->port_max; p++) {
317 for (d = 0; d <= ai->ports[p].dev_max; d++) {
318 if (ai->ports[p].devs[d].present) {
319 ahci_flush_cache(ai, p, d);
320 }
321 }
322 }
323 }
324
325 return(STDON);
326}
327
328/******************************************************************************
329 * ADD entry point. This is the main entry point for all ADD requests. Due to
330 * the asynchronous nature of ADD drivers, this function primarily queues the
331 * IORB(s) to the corresponding adapter or port queues, then triggers the
332 * state machine to initiate processing queued IORBs.
333 *
334 * NOTE: In order to prevent race conditions or engine stalls, certain rules
335 * around locking, unlocking and IORB handling in general have been
336 * established. Refer to the comments in "trigger_engine()" for
337 * details.
338 */
339void _cdecl _far _loadds add_entry(IORBH _far *first_iorb)
340{
341 IORBH _far *iorb;
342 IORBH _far *next = NULL;
343
344 spin_lock(drv_lock);
345
346 for (iorb = first_iorb; iorb != NULL; iorb = next) {
347 /* Queue this IORB. Queues primarily exist on port level but there are
348 * some requests which affect the whole driver, most notably
349 * IOCC_CONFIGURATION. In either case, adding the IORB to the driver or
350 * port queue will change the links, thus we need to save the original
351 * link in 'next'.
352 */
353 next = (iorb->RequestControl | IORB_CHAIN) ? iorb->pNxtIORB : 0;
354
355 iorb->Status = 0;
356 iorb->ErrorCode = 0;
357 memset(&iorb->ADDWorkSpace, 0x00, sizeof(ADD_WORKSPACE));
358
359 if (iorb_driver_level(iorb)) {
360 /* adapter-level IORB */
361 iorb->UnitHandle = 0;
362 iorb_queue_add(&driver_queue, iorb);
363
364 } else {
365 /* port-level IORB */
366 int a = iorb_unit_adapter(iorb);
367 int p = iorb_unit_port(iorb);
368 int d = iorb_unit_device(iorb);
369
370 if (a >= ad_info_cnt ||
371 p > ad_infos[a].port_max ||
372 d > ad_infos[a].ports[p].dev_max ||
373 (ad_infos[a].port_map & (1UL << p)) == 0) {
374
375 /* unit handle outside of the allowed range */
376 dprintf("warning: IORB for %d.%d.%d out of range\n", a, p, d);
377 iorb->Status = IORB_ERROR;
378 iorb->ErrorCode = IOERR_CMD_SYNTAX;
379 complete_iorb(iorb);
380 continue;
381 }
382
383 iorb_queue_add(&ad_infos[a].ports[p].iorb_queue, iorb);
384 }
385 }
386
387 /* trigger state machine */
388 trigger_engine();
389
390 spin_unlock(drv_lock);
391}
392
393/******************************************************************************
394 * Trigger IORB queue engine. This is a wrapper function for trigger_engine_1()
395 * which will try to get all IORBs sent on their way a couple of times. If
396 * there are still IORBs ready for processing after this, this function will
397 * hand off to a context hook which will continue to trigger the engine until
398 * all IORBs have been sent.
399 */
400void trigger_engine(void)
401{
402 int i;
403
404 for (i = 0; i < 3; i++) {
405 if (trigger_engine_1() == 0) {
406 /* done -- all IORBs have been sent on their way */
407 return;
408 }
409 }
410
411 /* Something keeps bouncing; hand off to the engine context hook which will
412 * keep trying in the background.
413 */
414 DevHelp_ArmCtxHook(0, engine_ctxhook_h);
415}
416
417/******************************************************************************
418 * Trigger IORB queue engine in order to send commands in the driver/port IORB
419 * queues to the AHCI hardware. This function will return the number of IORBs
420 * sent. Keep in mind that IORBs might "bounce" if the adapter/port is not in
421 * a state to accept the command, thus it might take quite a few calls to get
422 * all IORBs on their way. This is why there's a wrapper function which tries
423 * it a few times, then hands off to a context hook which will keep trying in
424 * the background.
425 *
426 * IORBs might complete before send_iorb() has returned, at any time during
427 * interrupt processing or on another CPU on SMP systems. IORB completion
428 * means modifications to the corresponding IORB queue (the completed IORB
429 * is removed from the queue) thus we need to protect the IORB queues from
430 * race conditions. The safest approach short of keeping the driver-level
431 * spinlock aquired permanently is to keep it throughout this function and
432 * release it temporarily in send_iorb().
433 *
434 * This implies that the handler functions are fully responsible for aquiring
435 * the driver-level spinlock when they need it, and for releasing it again.
436 *
437 * As a rule of thumb, get the driver-level spinlock whenever accessing
438 * volatile variables (IORB queues, values in ad_info[], ...).
439 *
440 * Additional Notes:
441 *
442 * - This function is expected to be called with the spinlock aquired
443 *
444 * - Adapters can be flagged as 'busy' which means no new IORBs are sent (they
445 * just remain in the queue). This can be used to release the driver-level
446 * spinlock while making sure no new IORBs are going to hit the hardware.
447 * In order to prevent engine stalls, all handlers using this functionality
448 * need to invoke trigger_engine() after resetting the busy flag.
449 *
450 * - Driver-level IORBs are not synchronized by adapter-level 'busy' flags.
451 * However, the driver-level queue is worked "one entry at a time" which
452 * means that no new IORBs will be queued on the driver-level queue until
453 * the head element has completed processing. This means that driver-
454 * level IORB handlers don't need to protect against each other. But they
455 * they do need to keep in mind interference with port-level IORBs:
456 *
457 * - Driver-level IORB handlers must obtain the spinlock and/or flag all
458 * adapters as 'busy' which are affected by the driver-level IORB
459 *
460 * - Driver-level IORB handlers must not access the hardware of a
461 * particular adapter if it's flagged as 'busy'
462 */
463int trigger_engine_1(void)
464{
465 IORBH _far *iorb;
466 IORBH _far *next;
467 int iorbs_sent = 0;
468 int a;
469 int p;
470
471 iorbs_sent = 0;
472
473 /* process driver-level IORBs */
474 if ((iorb = driver_queue.root) != NULL && !add_workspace(iorb)->processing) {
475 send_iorb(iorb);
476 iorbs_sent++;
477 }
478
479 /* process port-level IORBs */
480 for (a = 0; a < ad_info_cnt; a++) {
481 AD_INFO *ai = ad_infos + a;
482 if (ai->busy) {
483 /* adapter is busy; don't process any IORBs */
484 continue;
485 }
486 for (p = 0; p <= ai->port_max; p++) {
487 /* send all queued IORBs on this port */
488 next = NULL;
489 for (iorb = ai->ports[p].iorb_queue.root; iorb != NULL; iorb = next) {
490 next = iorb->pNxtIORB;
491 if (!add_workspace(iorb)->processing) {
492 send_iorb(iorb);
493 iorbs_sent++;
494 }
495 }
496 }
497 }
498
499 return(iorbs_sent);
500}
501
502/******************************************************************************
503 * Send a single IORB to the corresponding AHCI adapter/port. This is just a
504 * switch board for calling the corresponding iocc_*() handler function.
505 *
506 * NOTE: This function is expected to be called with the driver-level spinlock
507 * aquired. It will release it before calling any of the handler
508 * functions and re-aquire it when done.
509 */
510void send_iorb(IORBH _far *iorb)
511{
512 /* Mark IORB as "processing" before doing anything else. Once the IORB is
513 * marked as "processing", we can release the spinlock because subsequent
514 * invocations of trigger_engine() (e.g. at interrupt time) will ignore this
515 * IORB.
516 */
517 add_workspace(iorb)->processing = 1;
518 spin_unlock(drv_lock);
519
520 switch (iorb->CommandCode) {
521
522 case IOCC_CONFIGURATION:
523 iocc_configuration(iorb);
524 break;
525
526 case IOCC_DEVICE_CONTROL:
527 iocc_device_control(iorb);
528 break;
529
530 case IOCC_UNIT_CONTROL:
531 iocc_unit_control(iorb);
532 break;
533
534 case IOCC_GEOMETRY:
535 iocc_geometry(iorb);
536 break;
537
538 case IOCC_EXECUTE_IO:
539 iocc_execute_io(iorb);
540 break;
541
542 case IOCC_UNIT_STATUS:
543 iocc_unit_status(iorb);
544 break;
545
546 case IOCC_ADAPTER_PASSTHRU:
547 iocc_adapter_passthru(iorb);
548 break;
549
550 default:
551 /* unsupported call */
552 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
553 iorb_done(iorb);
554 break;
555 }
556
557 /* re-aquire spinlock before returning to trigger_engine() */
558 spin_lock(drv_lock);
559}
560
561/******************************************************************************
562 * Handle IOCC_CONFIGURATION requests.
563 */
564void iocc_configuration(IORBH _far *iorb)
565{
566 int a;
567
568 switch (iorb->CommandModifier) {
569
570 case IOCM_COMPLETE_INIT:
571 /* Complete initialization. From now on, we won't have to restore the BIOS
572 * configuration after each command and we're fully operational (i.e. will
573 * use interrupts, timers and context hooks instead of polling).
574 */
575 if (!init_complete) {
576 dprintf("leaving initialization mode\n");
577 spin_lock(drv_lock);
578 for (a = 0; a < ad_info_cnt; a++) {
579 ahci_complete_init(ad_infos + a);
580 }
581 init_complete = 1;
582 spin_unlock(drv_lock);
583 }
584 iorb_done(iorb);
585 break;
586
587 case IOCM_GET_DEVICE_TABLE:
588 /* construct a device table */
589 iocm_device_table(iorb);
590 break;
591
592 default:
593 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
594 iorb_done(iorb);
595 break;
596 }
597}
598
599/******************************************************************************
600 * Handle IOCC_DEVICE_CONTROL requests.
601 */
602void iocc_device_control(IORBH _far *iorb)
603{
604 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
605 IORBH _far *ptr;
606 IORBH _far *next = NULL;
607 int p = iorb_unit_port(iorb);
608 int d = iorb_unit_device(iorb);
609
610 switch (iorb->CommandModifier) {
611
612 case IOCM_ABORT:
613 /* abort all pending commands on specified port and device */
614 spin_lock(drv_lock);
615 for (ptr = ai->ports[p].iorb_queue.root; ptr != NULL; ptr = next) {
616 next = ptr->pNxtIORB;
617 /* move all matching IORBs to the abort queue */
618 if (ptr != iorb && iorb_unit_device(ptr) == d) {
619 iorb_queue_del(&ai->ports[p].iorb_queue, ptr);
620 iorb_queue_add(&abort_queue, ptr);
621 ptr->ErrorCode = IOERR_CMD_ABORTED;
622 }
623 }
624 spin_unlock(drv_lock);
625
626 /* trigger reset context hook which will finish the abort processing */
627 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
628 break;
629
630 case IOCM_SUSPEND:
631 case IOCM_RESUME:
632 case IOCM_GET_QUEUE_STATUS:
633 /* Suspend/resume operations allow access to the hardware for other
634 * entities such as IBMIDECD.FLT. Since os2ahci implements both ATA
635 * and ATAPI in the same driver, this won't be required.
636 */
637 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
638 break;
639
640 case IOCM_LOCK_MEDIA:
641 case IOCM_UNLOCK_MEDIA:
642 case IOCM_EJECT_MEDIA:
643 /* unit control commands to lock, unlock and eject media */
644 /* will be supported later... */
645 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
646 break;
647
648 default:
649 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
650 break;
651 }
652
653 iorb_done(iorb);
654}
655
656/******************************************************************************
657 * Handle IOCC_UNIT_CONTROL requests.
658 */
659void iocc_unit_control(IORBH _far *iorb)
660{
661 IORB_UNIT_CONTROL _far *iorb_uc = (IORB_UNIT_CONTROL _far *) iorb;
662 int a = iorb_unit_adapter(iorb);
663 int p = iorb_unit_port(iorb);
664 int d = iorb_unit_device(iorb);
665
666 spin_lock(drv_lock);
667 switch (iorb->CommandModifier) {
668
669 case IOCM_ALLOCATE_UNIT:
670 /* allocate unit for exclusive access */
671 if (ad_infos[a].ports[p].devs[d].allocated) {
672 iorb_seterr(iorb, IOERR_UNIT_ALLOCATED);
673 } else {
674 ad_infos[a].ports[p].devs[d].allocated = 1;
675 }
676 break;
677
678 case IOCM_DEALLOCATE_UNIT:
679 /* deallocate exclusive access to unit */
680 if (!ad_infos[a].ports[p].devs[d].allocated) {
681 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
682 } else {
683 ad_infos[a].ports[p].devs[d].allocated = 0;
684 }
685 break;
686
687 case IOCM_CHANGE_UNITINFO:
688 /* Change unit (device) information. One reason for this IOCM is the
689 * interface for filter device drivers: a filter device driver can
690 * either change existing UNITINFOs or permanently allocate units
691 * and fabricate new [logical] units; the former is the reason why we
692 * must store the pointer to the updated UNITNIFO for subsequent
693 * IOCC_CONFIGURATION/IOCM_GET_DEVICE_TABLE calls.
694 */
695 if (!ad_infos[a].ports[p].devs[d].allocated) {
696 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
697 break;
698 }
699 ad_infos[a].ports[p].devs[d].unit_info = iorb_uc->pUnitInfo;
700 break;
701
702 default:
703 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
704 break;
705 }
706
707 spin_unlock(drv_lock);
708 iorb_done(iorb);
709}
710
711/******************************************************************************
712 * Scan all ports for AHCI devices and construct a DASD device table.
713 *
714 * NOTE: This function may be called multiple times. Only the first invocation
715 * will actually scan for devices; all subsequent calls will merely
716 * return the results of the initial scan, potentially augmented by
717 * modified unit infos after IOCC_CONFIGURATION/IOCM_CHANGE_UNITINFO
718 * requests.
719 */
720void iocm_device_table(IORBH _far *iorb)
721{
722 IORB_CONFIGURATION _far *iorb_conf;
723 DEVICETABLE _far *dt;
724 char _far *pos;
725 int rc;
726 int a;
727 int p;
728 int d;
729
730 iorb_conf = (IORB_CONFIGURATION _far *) iorb;
731 dt = iorb_conf->pDeviceTable;
732
733 spin_lock(drv_lock);
734
735 /* initialize device table header */
736 dt->ADDLevelMajor = ADD_LEVEL_MAJOR;
737 dt->ADDLevelMinor = ADD_LEVEL_MINOR;
738 dt->ADDHandle = add_handle;
739 dt->TotalAdapters = ad_info_cnt;
740
741 /* Initial position of dynamic portion of device table (i.e. behind the
742 * array of ADAPTERINFO pointers, pAdapter, in the device table)
743 */
744 pos = (char _far *) (dt->pAdapter + ad_info_cnt);
745
746 for (a = 0; a < ad_info_cnt; a++) {
747 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) pos;
748 AD_INFO *ad_info = ad_infos + a;
749 int units = 0;
750
751 /* sanity check for sufficient space in device table */
752 if ((u32) (ptr + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
753 dprintf("error: device table provided by DASD too small\n");
754 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
755 goto iocm_device_table_done;
756 }
757
758 /* set ADAPTERINFO offset in device table */
759 dt->pAdapter[a] = (ADAPTERINFO _near *) ((u32) ptr & 0xffff);
760
761 /* fill in adapter information structure in device table */
762 memset(ptr, 0x00, sizeof(*ptr));
763 sprintf(ptr->AdapterName, "AHCI_%d", a);
764 ptr->AdapterDevBus = AI_DEVBUS_ST506 | AI_DEVBUS_32BIT;
765 ptr->AdapterIOAccess = AI_IOACCESS_BUS_MASTER;
766 ptr->AdapterHostBus = AI_HOSTBUS_OTHER | AI_BUSWIDTH_32BIT;
767 ptr->AdapterFlags = AF_16M | AF_HW_SCATGAT;
768
769 /* AHCI limits S/G elements to 22 bits, thus we'll report only half of
770 * our S/G list buffers to reduce complexity. The command preparation code
771 * will always try to map as many S/G elements as possible so the physical
772 * S/G list capacity is not really wasted except in rare conditions where
773 * we need to split commands with long S/G lists without any suitable split
774 * points except those at the reported MaxHWSGList.
775 */
776 ptr->MaxHWSGList = AHCI_MAX_SG / 2;
777
778 if (!ad_info->port_scan_done) {
779 /* First call; need to scan AHCI hardware for devices. Since this might
780 * be a lengthy operation, especially when init_reset is set, we'll mark
781 * the adapter as busy (new IORBs will only be queued but not executed)
782 * and release the spinlock while scanning the ports so interrupts will
783 * be processed.
784 */
785 if (ad_info->busy) {
786 dprintf("error: port scan requested while adapter was busy\n");
787 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
788 goto iocm_device_table_done;
789 }
790 ad_info->busy = 1;
791 spin_unlock(drv_lock);
792 rc = ahci_scan_ports(ad_info);
793 spin_lock(drv_lock);
794 ad_info->busy = 0;
795
796 if (rc != 0) {
797 dprintf("error: port scan failed on adapter #%d\n", a);
798 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
799 goto iocm_device_table_done;
800 }
801 ad_info->port_scan_done = 1;
802 }
803
804 /* insert devices (units) into the device table */
805 for (p = 0; p <= ad_info->port_max; p++) {
806 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
807 if (ad_info->ports[p].devs[d].present) {
808 UNITINFO _far *ui = ptr->UnitInfo + units;
809
810 /* sanity check for sufficient space in device table */
811 if ((u32) (ui + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
812 dprintf("error: device table provided by DASD too small\n");
813 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
814 goto iocm_device_table_done;
815 }
816
817 if (ad_info->ports[p].devs[d].unit_info == NULL) {
818 /* provide initial information about this device (unit) */
819 memset(ui, 0x00, sizeof(*ui));
820 ui->AdapterIndex = a;
821 ui->UnitIndex = units;
822 ui->UnitHandle = iorb_unit(a, p, d);
823 ui->UnitType = ad_info->ports[p].devs[d].dev_type;
824 ui->QueuingCount = ad_info->ports[p].devs[d].ncq_max;;
825 if (ad_info->ports[p].devs[d].removable) {
826 ui->UnitFlags |= UF_REMOVABLE;
827 }
828 } else {
829 /* copy updated device (unit) information (IOCM_CHANGE_UNITINFO) */
830 memcpy(ui, ad_info->ports[p].devs[d].unit_info, sizeof(*ui));
831 }
832 units++;
833 }
834 }
835 }
836
837 /* set total device (unit) count for this adapter */
838 ptr->AdapterUnits = units;
839
840 /* calculate offset for next adapter */
841 pos = (char _far *) (ptr->UnitInfo + units);
842 }
843
844iocm_device_table_done:
845 spin_unlock(drv_lock);
846 iorb_done(iorb);
847}
848
849/******************************************************************************
850 * Handle IOCC_GEOMETRY requests.
851 */
852void iocc_geometry(IORBH _far *iorb)
853{
854 switch (iorb->CommandModifier) {
855
856 case IOCM_GET_MEDIA_GEOMETRY:
857 case IOCM_GET_DEVICE_GEOMETRY:
858 add_workspace(iorb)->idempotent = 1;
859 ahci_get_geometry(iorb);
860 break;
861
862 default:
863 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
864 iorb_done(iorb);
865 }
866}
867
868/******************************************************************************
869 * Handle IOCC_EXECUTE_IO requests.
870 */
871void iocc_execute_io(IORBH _far *iorb)
872{
873 switch (iorb->CommandModifier) {
874
875 case IOCM_READ:
876 add_workspace(iorb)->idempotent = 1;
877 ahci_read(iorb);
878 break;
879
880 case IOCM_READ_VERIFY:
881 add_workspace(iorb)->idempotent = 1;
882 ahci_verify(iorb);
883 break;
884
885 case IOCM_WRITE:
886 add_workspace(iorb)->idempotent = 1;
887 ahci_write(iorb);
888 break;
889
890 case IOCM_WRITE_VERIFY:
891 add_workspace(iorb)->idempotent = 1;
892 ahci_write(iorb);
893 break;
894
895 default:
896 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
897 iorb_done(iorb);
898 }
899}
900
901/******************************************************************************
902 * Handle IOCC_UNIT_STATUS requests.
903 */
904void iocc_unit_status(IORBH _far *iorb)
905{
906 switch (iorb->CommandModifier) {
907
908 case IOCM_GET_UNIT_STATUS:
909 add_workspace(iorb)->idempotent = 1;
910 ahci_unit_ready(iorb);
911 break;
912
913 default:
914 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
915 iorb_done(iorb);
916 }
917}
918
919/******************************************************************************
920 * Handle IOCC_ADAPTER_PASSTHROUGH requests.
921 */
922void iocc_adapter_passthru(IORBH _far *iorb)
923{
924 switch (iorb->CommandModifier) {
925
926 case IOCM_EXECUTE_CDB:
927 add_workspace(iorb)->idempotent = 0;
928 ahci_execute_cdb(iorb);
929 break;
930
931 case IOCM_EXECUTE_ATA:
932 add_workspace(iorb)->idempotent = 0;
933 ahci_execute_ata(iorb);
934 break;
935
936 default:
937 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
938 iorb_done(iorb);
939 }
940}
941
942/******************************************************************************
943 * Add an IORB to the specified queue.
944 */
945void iorb_queue_add(IORB_QUEUE _far *queue, IORBH _far *iorb)
946{
947 if (iorb_priority(iorb) {
948 /* priority IORB; insert at first position */
949 iorb->pNxtIORB = queue->root;
950 queue->root = iorb;
951
952 } else {
953 /* append IORB to end of queue */
954 iorb->pNxtIORB = NULL;
955
956 if (queue->root == NULL) {
957 queue->root = iorb;
958 } else {
959 queue->tail->pNxtIORB = iorb;
960 }
961 queue->tail = iorb;
962 }
963
964 if (debug) {
965 /* determine queue type (local, driver, abort or port) and minimum debug
966 * level; otherwise, queue debug prints can become really confusing.
967 */
968 char *queue_type;
969 int min_debug = 1;
970
971 if ((u32) queue >> 16 == (u32) (void _far *) &queue >> 16) {
972 /* this queue is on the stack */
973 queue_type = "local";
974 min_debug = 2;
975
976 } else if (queue == &driver_queue) {
977 queue_type = "driver";
978
979 } else if (queue == &abort_queue) {
980 queue_type = "abort";
981 min_debug = 2;
982
983 } else {
984 queue_type = "port";
985 }
986
987 if (debug >= min_debug) {
988 printf("IORB %Fp queued (cmd = %d/%d, queue = %Fp [%s], timeout = %ld)\n",
989 iorb, iorb->CommandCode, iorb->CommandModifier, queue, queue_type,
990 iorb->Timeout);
991 }
992 }
993}
994
995/******************************************************************************
996 * Remove an IORB from the specified queue.
997 */
998int iorb_queue_del(IORB_QUEUE _far *queue, IORBH _far *iorb)
999{
1000 IORBH _far *_iorb;
1001 IORBH _far *_prev = NULL;
1002 int found = 0;
1003
1004 for (_iorb = queue->root; _iorb != NULL; _iorb = _iorb->pNxtIORB) {
1005 if (_iorb == iorb) {
1006 /* found the IORB to be removed */
1007 if (_prev != NULL) {
1008 _prev->pNxtIORB = _iorb->pNxtIORB;
1009 } else {
1010 queue->root = _iorb->pNxtIORB;
1011 }
1012 if (_iorb == queue->tail) {
1013 queue->tail = _prev;
1014 }
1015 found = 1;
1016 break;
1017 }
1018 _prev = _iorb;
1019 }
1020
1021 if (found) {
1022 ddprintf("IORB %Fp removed (queue = %Fp)\n", iorb, queue);
1023 } else {
1024 dprintf("IORB %Fp not found in queue %Fp\n", iorb, queue);
1025 }
1026
1027 return(!found);
1028}
1029
1030/******************************************************************************
1031 * Set the error code in the specified IORB
1032 *
1033 * NOTE: This function does *not* call iorb_done(). It merely sets the IORB
1034 * status to the specified error code.
1035 */
1036void iorb_seterr(IORBH _far *iorb, USHORT error_code)
1037{
1038 iorb->ErrorCode = error_code;
1039 iorb->Status = IORB_ERROR;
1040}
1041
1042/******************************************************************************
1043 * Mark the specified IORB as done and notify the asynchronous post function,
1044 * if any. The IORB is also removed from the corresponding IORB queue.
1045 *
1046 * NOTES: This function does not clear the Status field; it merely adds the
1047 * IORB_DONE flag.
1048 *
1049 * This function is expected to be called *without* the corresponding
1050 * driver-level drv_lock aquired. It will aquire the spinlock before
1051 * updating the IORB queue and release it before notifying the upstream
1052 * code in order to prevent deadlocks.
1053 *
1054 * Due to this logic, this function is only good for simple task-time
1055 * completions. Functions working on lists of IORBs (such as interrupt
1056 * handlers or context hooks) should implement their own logic. See
1057 * abort_ctxhook() for an example.
1058 */
1059void iorb_done(IORBH _far *iorb)
1060{
1061 int a = iorb_unit_adapter(iorb);
1062 int p = iorb_unit_port(iorb);
1063
1064 /* remove IORB from corresponding queue */
1065 spin_lock(drv_lock);
1066 if (iorb_driver_level(iorb)) {
1067 iorb_queue_del(&driver_queue, iorb);
1068 } else {
1069 iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb);
1070 }
1071 aws_free(add_workspace(iorb));
1072 spin_unlock(drv_lock);
1073
1074 complete_iorb(iorb);
1075}
1076
1077/******************************************************************************
1078 * Requeue the specified IORB such that it will be sent downstream for
1079 * processing again. This includes freeing all resources currently allocated
1080 * (timer, buffer, ...) and resetting the flags to 0.
1081 *
1082 * The following flags are preserved:
1083 * - no_ncq
1084 */
1085void iorb_requeue(IORBH _far *iorb)
1086{
1087 ADD_WORKSPACE _far *aws = add_workspace(iorb);
1088 u16 no_ncq = aws->no_ncq;
1089
1090 aws_free(aws);
1091 memset(aws, 0x00, sizeof(*aws));
1092 aws->no_ncq = no_ncq;
1093}
1094
1095/******************************************************************************
1096 * Timeout handler for I/O commands. Since timeout handling can involve
1097 * lengthy operations like port resets, the main code is located in a
1098 * separate function which is invoked via a context hook.
1099 */
1100void _cdecl _far timeout_callback(ULONG timer_handle, ULONG p1,
1101 ULONG p2)
1102{
1103 IORBH _far *iorb = (IORBH _far *) p1;
1104 int a = iorb_unit_adapter(iorb);
1105 int p = iorb_unit_port(iorb);
1106
1107 ADD_CancelTimer(timer_handle);
1108 dprintf("timeout for IORB %Fp\n", iorb);
1109
1110 /* Move the timed-out IORB to the abort queue. Since it's possible that the
1111 * IORB has completed after the timeout has expired but before we got to
1112 * this line of code, we'll check the return code of iorb_queue_del(): If it
1113 * returns an error, the IORB must have completed a few microseconds ago and
1114 * there is no timeout.
1115 */
1116 spin_lock(drv_lock);
1117 if (iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb) == 0) {
1118 iorb_queue_add(&abort_queue, iorb);
1119 iorb->ErrorCode = IOERR_ADAPTER_TIMEOUT;
1120 }
1121 spin_unlock(drv_lock);
1122
1123 /* Trigger abort processing function. We don't really care whether this
1124 * succeeds because the only reason why it would fail should be multiple
1125 * calls to DevHelp_ArmCtxHook() before the context hook had a chance to
1126 * start executing, which leaves two scenarios:
1127 *
1128 * - We succeded in arming the context hook. Fine.
1129 *
1130 * - We armed the context hook a second time before it had a chance to
1131 * start executing. In this case, the already scheduled context hook
1132 * will process our IORB as well.
1133 */
1134 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
1135}
1136
1137/******************************************************************************
1138 * small_code_ - this dummy func resolves the undefined reference linker
1139 * error that occurrs when linking WATCOM objects with DDK's link.exe
1140 */
1141void _cdecl small_code_(void)
1142{
1143}
Note: See TracBrowser for help on using the repository browser.