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

Last change on this file since 69 was 69, checked in by markus, 15 years ago

fixed ATAPI packet commands; added cache flush in shutdown handler; allow write cache for NCQ commands

File size: 36.5 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 | IORB_DONE;
378 iorb->ErrorCode = IOERR_CMD_SYNTAX;
379 if (iorb->RequestControl & IORB_ASYNC_POST) {
380 iorb->NotifyAddress(iorb);
381 }
382 continue;
383 }
384
385 iorb_queue_add(&ad_infos[a].ports[p].iorb_queue, iorb);
386 }
387 }
388
389 /* trigger state machine */
390 trigger_engine();
391
392 spin_unlock(drv_lock);
393}
394
395/******************************************************************************
396 * Trigger IORB queue engine. This is a wrapper function for trigger_engine_1()
397 * which will try to get all IORBs sent on their way a couple of times. If
398 * there are still IORBs ready for processing after this, this function will
399 * hand off to a context hook which will continue to trigger the engine until
400 * all IORBs have been sent.
401 */
402void trigger_engine(void)
403{
404 int i;
405
406 for (i = 0; i < 3; i++) {
407 if (trigger_engine_1() == 0) {
408 /* done -- all IORBs have been sent on their way */
409 return;
410 }
411 }
412
413 /* Something keeps bouncing; hand off to the engine context hook which will
414 * keep trying in the background.
415 */
416 DevHelp_ArmCtxHook(0, engine_ctxhook_h);
417}
418
419/******************************************************************************
420 * Trigger IORB queue engine in order to send commands in the driver/port IORB
421 * queues to the AHCI hardware. This function will return the number of IORBs
422 * sent. Keep in mind that IORBs might "bounce" if the adapter/port is not in
423 * a state to accept the command, thus it might take quite a few calls to get
424 * all IORBs on their way. This is why there's a wrapper function which tries
425 * it a few times, then hands off to a context hook which will keep trying in
426 * the background.
427 *
428 * IORBs might complete before send_iorb() has returned, at any time during
429 * interrupt processing or on another CPU on SMP systems. IORB completion
430 * means modifications to the corresponding IORB queue (the completed IORB
431 * is removed from the queue) thus we need to protect the IORB queues from
432 * race conditions. The safest approach short of keeping the driver-level
433 * spinlock aquired permanently is to keep it throughout this function and
434 * release it temporarily in send_iorb().
435 *
436 * This implies that the handler functions are fully responsible for aquiring
437 * the driver-level spinlock when they need it, and for releasing it again.
438 *
439 * As a rule of thumb, get the driver-level spinlock whenever accessing
440 * volatile variables (IORB queues, values in ad_info[], ...).
441 *
442 * Additional Notes:
443 *
444 * - This function is expected to be called with the spinlock aquired
445 *
446 * - Adapters can be flagged as 'busy' which means no new IORBs are sent (they
447 * just remain in the queue). This can be used to release the driver-level
448 * spinlock while making sure no new IORBs are going to hit the hardware.
449 * In order to prevent engine stalls, all handlers using this functionality
450 * need to invoke trigger_engine() after resetting the busy flag.
451 *
452 * - Driver-level IORBs are not synchronized by adapter-level 'busy' flags.
453 * However, the driver-level queue is worked "one entry at a time" which
454 * means that no new IORBs will be queued on the driver-level queue until
455 * the head element has completed processing. This means that driver-
456 * level IORB handlers don't need to protect against each other. But they
457 * they do need to keep in mind interference with port-level IORBs:
458 *
459 * - Driver-level IORB handlers must obtain the spinlock and/or flag all
460 * adapters as 'busy' which are affected by the driver-level IORB
461 *
462 * - Driver-level IORB handlers must not access the hardware of a
463 * particular adapter if it's flagged as 'busy'
464 */
465int trigger_engine_1(void)
466{
467 IORBH _far *iorb;
468 IORBH _far *next;
469 int iorbs_sent = 0;
470 int a;
471 int p;
472
473 iorbs_sent = 0;
474
475 /* process driver-level IORBs */
476 if ((iorb = driver_queue.root) != NULL && !add_workspace(iorb)->processing) {
477 send_iorb(iorb);
478 iorbs_sent++;
479 }
480
481 /* process port-level IORBs */
482 for (a = 0; a < ad_info_cnt; a++) {
483 AD_INFO *ai = ad_infos + a;
484 if (ai->busy) {
485 /* adapter is busy; don't process any IORBs */
486 continue;
487 }
488 for (p = 0; p <= ai->port_max; p++) {
489 /* send all queued IORBs on this port */
490 next = NULL;
491 for (iorb = ai->ports[p].iorb_queue.root; iorb != NULL; iorb = next) {
492 next = iorb->pNxtIORB;
493 if (!add_workspace(iorb)->processing) {
494 send_iorb(iorb);
495 iorbs_sent++;
496 }
497 }
498 }
499 }
500
501 return(iorbs_sent);
502}
503
504/******************************************************************************
505 * Send a single IORB to the corresponding AHCI adapter/port. This is just a
506 * switch board for calling the corresponding iocc_*() handler function.
507 *
508 * NOTE: This function is expected to be called with the driver-level spinlock
509 * aquired. It will release it before calling any of the handler
510 * functions and re-aquire it when done.
511 */
512void send_iorb(IORBH _far *iorb)
513{
514 /* Mark IORB as "processing" before doing anything else. Once the IORB is
515 * marked as "processing", we can release the spinlock because subsequent
516 * invocations of trigger_engine() (e.g. at interrupt time) will ignore this
517 * IORB.
518 */
519 add_workspace(iorb)->processing = 1;
520 spin_unlock(drv_lock);
521
522 switch (iorb->CommandCode) {
523
524 case IOCC_CONFIGURATION:
525 iocc_configuration(iorb);
526 break;
527
528 case IOCC_DEVICE_CONTROL:
529 iocc_device_control(iorb);
530 break;
531
532 case IOCC_UNIT_CONTROL:
533 iocc_unit_control(iorb);
534 break;
535
536 case IOCC_GEOMETRY:
537 iocc_geometry(iorb);
538 break;
539
540 case IOCC_EXECUTE_IO:
541 iocc_execute_io(iorb);
542 break;
543
544 case IOCC_UNIT_STATUS:
545 iocc_unit_status(iorb);
546 break;
547
548 case IOCC_ADAPTER_PASSTHRU:
549 iocc_adapter_passthru(iorb);
550 break;
551
552 default:
553 /* unsupported call */
554 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
555 iorb_done(iorb);
556 break;
557 }
558
559 /* re-aquire spinlock before returning to trigger_engine() */
560 spin_lock(drv_lock);
561}
562
563/******************************************************************************
564 * Handle IOCC_CONFIGURATION requests.
565 */
566void iocc_configuration(IORBH _far *iorb)
567{
568 int a;
569
570 switch (iorb->CommandModifier) {
571
572 case IOCM_COMPLETE_INIT:
573 /* Complete initialization. From now on, we won't have to restore the BIOS
574 * configuration after each command and we're fully operational (i.e. will
575 * use interrupts, timers and context hooks instead of polling).
576 */
577 if (!init_complete) {
578 dprintf("leaving initialization mode\n");
579 spin_lock(drv_lock);
580 for (a = 0; a < ad_info_cnt; a++) {
581 ahci_complete_init(ad_infos + a);
582 }
583 init_complete = 1;
584 spin_unlock(drv_lock);
585 }
586 iorb_done(iorb);
587 break;
588
589 case IOCM_GET_DEVICE_TABLE:
590 /* construct a device table */
591 iocm_device_table(iorb);
592 break;
593
594 default:
595 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
596 iorb_done(iorb);
597 break;
598 }
599}
600
601/******************************************************************************
602 * Handle IOCC_DEVICE_CONTROL requests.
603 */
604void iocc_device_control(IORBH _far *iorb)
605{
606 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
607 IORBH _far *ptr;
608 IORBH _far *next = NULL;
609 int p = iorb_unit_port(iorb);
610 int d = iorb_unit_device(iorb);
611
612 switch (iorb->CommandModifier) {
613
614 case IOCM_ABORT:
615 /* abort all pending commands on specified port and device */
616 spin_lock(drv_lock);
617 for (ptr = ai->ports[p].iorb_queue.root; ptr != NULL; ptr = next) {
618 next = ptr->pNxtIORB;
619 /* move all matching IORBs to the abort queue */
620 if (ptr != iorb && iorb_unit_device(ptr) == d) {
621 iorb_queue_del(&ai->ports[p].iorb_queue, ptr);
622 iorb_queue_add(&abort_queue, ptr);
623 ptr->ErrorCode = IOERR_CMD_ABORTED;
624 }
625 }
626 spin_unlock(drv_lock);
627
628 /* trigger reset context hook which will finish the abort processing */
629 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
630 break;
631
632 case IOCM_SUSPEND:
633 case IOCM_RESUME:
634 case IOCM_GET_QUEUE_STATUS:
635 /* Suspend/resume operations allow access to the hardware for other
636 * entities such as IBMIDECD.FLT. Since os2ahci implements both ATA
637 * and ATAPI in the same driver, this won't be required.
638 */
639 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
640 break;
641
642 case IOCM_LOCK_MEDIA:
643 case IOCM_UNLOCK_MEDIA:
644 case IOCM_EJECT_MEDIA:
645 /* unit control commands to lock, unlock and eject media */
646 /* will be supported later... */
647 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
648 break;
649
650 default:
651 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
652 break;
653 }
654
655 iorb_done(iorb);
656}
657
658/******************************************************************************
659 * Handle IOCC_UNIT_CONTROL requests.
660 */
661void iocc_unit_control(IORBH _far *iorb)
662{
663 IORB_UNIT_CONTROL _far *iorb_uc = (IORB_UNIT_CONTROL _far *) iorb;
664 int a = iorb_unit_adapter(iorb);
665 int p = iorb_unit_port(iorb);
666 int d = iorb_unit_device(iorb);
667
668 spin_lock(drv_lock);
669 switch (iorb->CommandModifier) {
670
671 case IOCM_ALLOCATE_UNIT:
672 /* allocate unit for exclusive access */
673 if (ad_infos[a].ports[p].devs[d].allocated) {
674 iorb_seterr(iorb, IOERR_UNIT_ALLOCATED);
675 } else {
676 ad_infos[a].ports[p].devs[d].allocated = 1;
677 }
678 break;
679
680 case IOCM_DEALLOCATE_UNIT:
681 /* deallocate exclusive access to unit */
682 if (!ad_infos[a].ports[p].devs[d].allocated) {
683 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
684 } else {
685 ad_infos[a].ports[p].devs[d].allocated = 0;
686 }
687 break;
688
689 case IOCM_CHANGE_UNITINFO:
690 /* Change unit (device) information. One reason for this IOCM is the
691 * interface for filter device drivers: a filter device driver can
692 * either change existing UNITINFOs or permanently allocate units
693 * and fabricate new [logical] units; the former is the reason why we
694 * must store the pointer to the updated UNITNIFO for subsequent
695 * IOCC_CONFIGURATION/IOCM_GET_DEVICE_TABLE calls.
696 */
697 if (!ad_infos[a].ports[p].devs[d].allocated) {
698 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
699 break;
700 }
701 ad_infos[a].ports[p].devs[d].unit_info = iorb_uc->pUnitInfo;
702 break;
703
704 default:
705 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
706 break;
707 }
708
709 spin_unlock(drv_lock);
710 iorb_done(iorb);
711}
712
713/******************************************************************************
714 * Scan all ports for AHCI devices and construct a DASD device table.
715 *
716 * NOTE: This function may be called multiple times. Only the first invocation
717 * will actually scan for devices; all subsequent calls will merely
718 * return the results of the initial scan, potentially augmented by
719 * modified unit infos after IOCC_CONFIGURATION/IOCM_CHANGE_UNITINFO
720 * requests.
721 */
722void iocm_device_table(IORBH _far *iorb)
723{
724 IORB_CONFIGURATION _far *iorb_conf;
725 DEVICETABLE _far *dt;
726 char _far *pos;
727 int rc;
728 int a;
729 int p;
730 int d;
731
732 iorb_conf = (IORB_CONFIGURATION _far *) iorb;
733 dt = iorb_conf->pDeviceTable;
734
735 spin_lock(drv_lock);
736
737 /* initialize device table header */
738 dt->ADDLevelMajor = ADD_LEVEL_MAJOR;
739 dt->ADDLevelMinor = ADD_LEVEL_MINOR;
740 dt->ADDHandle = add_handle;
741 dt->TotalAdapters = ad_info_cnt;
742
743 /* Initial position of dynamic portion of device table (i.e. behind the
744 * array of ADAPTERINFO pointers, pAdapter, in the device table)
745 */
746 pos = (char _far *) (dt->pAdapter + ad_info_cnt);
747
748 for (a = 0; a < ad_info_cnt; a++) {
749 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) pos;
750 AD_INFO *ad_info = ad_infos + a;
751 int units = 0;
752
753 /* sanity check for sufficient space in device table */
754 if ((u32) (ptr + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
755 dprintf("error: device table provided by DASD too small\n");
756 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
757 goto iocm_device_table_done;
758 }
759
760 /* set ADAPTERINFO offset in device table */
761 dt->pAdapter[a] = (ADAPTERINFO _near *) ((u32) ptr & 0xffff);
762
763 /* fill in adapter information structure in device table */
764 memset(ptr, 0x00, sizeof(*ptr));
765 sprintf(ptr->AdapterName, "AHCI_%d", a);
766 ptr->AdapterDevBus = AI_DEVBUS_ST506 | AI_DEVBUS_32BIT;
767 ptr->AdapterIOAccess = AI_IOACCESS_BUS_MASTER;
768 ptr->AdapterHostBus = AI_HOSTBUS_OTHER | AI_BUSWIDTH_32BIT;
769 ptr->AdapterFlags = AF_16M | AF_HW_SCATGAT;
770
771 /* AHCI limits S/G elements to 22 bits, thus we'll report only half of
772 * our S/G list buffers to reduce complexity. The command preparation code
773 * will always try to map as many S/G elements as possible so the physical
774 * S/G list capacity is not really wasted except in rare conditions where
775 * we need to split commands with long S/G lists without any suitable split
776 * points except those at the reported MaxHWSGList.
777 */
778 ptr->MaxHWSGList = AHCI_MAX_SG / 2;
779
780 if (!ad_info->port_scan_done) {
781 /* First call; need to scan AHCI hardware for devices. Since this might
782 * be a lengthy operation, especially when init_reset is set, we'll mark
783 * the adapter as busy (new IORBs will only be queued but not executed)
784 * and release the spinlock while scanning the ports so interrupts will
785 * be processed.
786 */
787 if (ad_info->busy) {
788 dprintf("error: port scan requested while adapter was busy\n");
789 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
790 goto iocm_device_table_done;
791 }
792 ad_info->busy = 1;
793 spin_unlock(drv_lock);
794 rc = ahci_scan_ports(ad_info);
795 spin_lock(drv_lock);
796 ad_info->busy = 0;
797
798 if (rc != 0) {
799 dprintf("error: port scan failed on adapter #%d\n", a);
800 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
801 goto iocm_device_table_done;
802 }
803 ad_info->port_scan_done = 1;
804 }
805
806 /* insert devices (units) into the device table */
807 for (p = 0; p <= ad_info->port_max; p++) {
808 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
809 if (ad_info->ports[p].devs[d].present) {
810 UNITINFO _far *ui = ptr->UnitInfo + units;
811
812 /* sanity check for sufficient space in device table */
813 if ((u32) (ui + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
814 dprintf("error: device table provided by DASD too small\n");
815 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
816 goto iocm_device_table_done;
817 }
818
819 if (ad_info->ports[p].devs[d].unit_info == NULL) {
820 /* provide initial information about this device (unit) */
821 memset(ui, 0x00, sizeof(*ui));
822 ui->AdapterIndex = a;
823 ui->UnitIndex = units;
824 ui->UnitHandle = iorb_unit(a, p, d);
825 ui->UnitType = ad_info->ports[p].devs[d].dev_type;
826 ui->QueuingCount = ad_info->ports[p].devs[d].ncq_max;;
827 if (ad_info->ports[p].devs[d].removable) {
828 ui->UnitFlags |= UF_REMOVABLE;
829 }
830 } else {
831 /* copy updated device (unit) information (IOCM_CHANGE_UNITINFO) */
832 memcpy(ui, ad_info->ports[p].devs[d].unit_info, sizeof(*ui));
833 }
834 units++;
835 }
836 }
837 }
838
839 /* set total device (unit) count for this adapter */
840 ptr->AdapterUnits = units;
841
842 /* calculate offset for next adapter */
843 pos = (char _far *) (ptr->UnitInfo + units);
844 }
845
846iocm_device_table_done:
847 spin_unlock(drv_lock);
848 iorb_done(iorb);
849}
850
851/******************************************************************************
852 * Handle IOCC_GEOMETRY requests.
853 */
854void iocc_geometry(IORBH _far *iorb)
855{
856 switch (iorb->CommandModifier) {
857
858 case IOCM_GET_MEDIA_GEOMETRY:
859 case IOCM_GET_DEVICE_GEOMETRY:
860 add_workspace(iorb)->idempotent = 1;
861 ahci_get_geometry(iorb);
862 break;
863
864 default:
865 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
866 iorb_done(iorb);
867 }
868}
869
870/******************************************************************************
871 * Handle IOCC_EXECUTE_IO requests.
872 */
873void iocc_execute_io(IORBH _far *iorb)
874{
875 switch (iorb->CommandModifier) {
876
877 case IOCM_READ:
878 add_workspace(iorb)->idempotent = 1;
879 ahci_read(iorb);
880 break;
881
882 case IOCM_READ_VERIFY:
883 add_workspace(iorb)->idempotent = 1;
884 ahci_verify(iorb);
885 break;
886
887 case IOCM_WRITE:
888 add_workspace(iorb)->idempotent = 1;
889 ahci_write(iorb);
890 break;
891
892 case IOCM_WRITE_VERIFY:
893 add_workspace(iorb)->idempotent = 1;
894 ahci_write(iorb);
895 break;
896
897 default:
898 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
899 iorb_done(iorb);
900 }
901}
902
903/******************************************************************************
904 * Handle IOCC_UNIT_STATUS requests.
905 */
906void iocc_unit_status(IORBH _far *iorb)
907{
908 switch (iorb->CommandModifier) {
909
910 case IOCM_GET_UNIT_STATUS:
911 add_workspace(iorb)->idempotent = 1;
912 ahci_unit_ready(iorb);
913 break;
914
915 default:
916 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
917 iorb_done(iorb);
918 }
919}
920
921/******************************************************************************
922 * Handle IOCC_ADAPTER_PASSTHROUGH requests.
923 */
924void iocc_adapter_passthru(IORBH _far *iorb)
925{
926 switch (iorb->CommandModifier) {
927
928 case IOCM_EXECUTE_CDB:
929 add_workspace(iorb)->idempotent = 0;
930 ahci_execute_cdb(iorb);
931 break;
932
933 case IOCM_EXECUTE_ATA:
934 add_workspace(iorb)->idempotent = 0;
935 ahci_execute_ata(iorb);
936 break;
937
938 default:
939 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
940 iorb_done(iorb);
941 }
942}
943
944/******************************************************************************
945 * Add an IORB to the specified queue.
946 */
947void iorb_queue_add(IORB_QUEUE _far *queue, IORBH _far *iorb)
948{
949 if (iorb_priority(iorb) {
950 /* priority IORB; insert at first position */
951 iorb->pNxtIORB = queue->root;
952 queue->root = iorb;
953
954 } else {
955 /* append IORB to end of queue */
956 iorb->pNxtIORB = NULL;
957
958 if (queue->root == NULL) {
959 queue->root = iorb;
960 } else {
961 queue->tail->pNxtIORB = iorb;
962 }
963 queue->tail = iorb;
964 }
965
966 dprintf("IORB queued: %d/%d (queue = %Fp, IORB = %Fp)\n",
967 iorb->CommandCode, iorb->CommandModifier, queue, iorb);
968}
969
970/******************************************************************************
971 * Remove an IORB from the specified queue.
972 */
973int iorb_queue_del(IORB_QUEUE _far *queue, IORBH _far *iorb)
974{
975 IORBH _far *_iorb;
976 IORBH _far *_prev = NULL;
977 int found = 0;
978
979 for (_iorb = queue->root; _iorb != NULL; _iorb = _iorb->pNxtIORB) {
980 if (_iorb == iorb) {
981 /* found the IORB to be removed */
982 if (_prev != NULL) {
983 _prev->pNxtIORB = _iorb->pNxtIORB;
984 } else {
985 queue->root = _iorb->pNxtIORB;
986 }
987 if (_iorb == queue->tail) {
988 queue->tail = _prev;
989 }
990 found = 1;
991 break;
992 }
993 _prev = _iorb;
994 }
995
996 if (found) {
997 dprintf("IORB removed: %d/%d (queue = %Fp, IORB = %Fp) - %04x/%04x\n",
998 iorb->CommandCode, iorb->CommandModifier, queue, iorb,
999 iorb->Status, iorb->ErrorCode);
1000 } else {
1001 dprintf("IORB %Fp not found in queue %Fp\n", iorb, queue);
1002 }
1003
1004 return(!found);
1005}
1006
1007/******************************************************************************
1008 * Set the error code in the specified IORB
1009 *
1010 * NOTE: This function does *not* call iorb_done(). It merely sets the IORB
1011 * status to the specified error code.
1012 */
1013void iorb_seterr(IORBH _far *iorb, USHORT error_code)
1014{
1015 iorb->ErrorCode = error_code;
1016 iorb->Status = IORB_ERROR;
1017}
1018
1019/******************************************************************************
1020 * Mark the specified IORB as done and notify the asynchronous post function,
1021 * if any. The IORB is also removed from the corresponding IORB queue.
1022 *
1023 * NOTES: This function does not clear the Status field; it merely adds the
1024 * IORB_DONE flag.
1025 *
1026 * This function is expected to be called *without* the corresponding
1027 * driver-level drv_lock aquired. It will aquire the spinlock before
1028 * updating the IORB queue and release it before notifying the upstream
1029 * code in order to prevent deadlocks.
1030 *
1031 * Due to this logic, this function is only good for simple task-time
1032 * completions. Functions working on lists of IORBs (such as interrupt
1033 * handlers or context hooks) should implement their own logic. See
1034 * abort_ctxhook() for an example.
1035 */
1036void iorb_done(IORBH _far *iorb)
1037{
1038 int a = iorb_unit_adapter(iorb);
1039 int p = iorb_unit_port(iorb);
1040
1041 /* remove IORB from corresponding queue */
1042 spin_lock(drv_lock);
1043 if (iorb_driver_level(iorb)) {
1044 iorb_queue_del(&driver_queue, iorb);
1045 } else {
1046 iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb);
1047 }
1048 aws_free(add_workspace(iorb));
1049 spin_unlock(drv_lock);
1050
1051 /* notify caller, if requested */
1052 iorb->Status |= IORB_DONE;
1053 if (iorb->RequestControl & IORB_ASYNC_POST) {
1054 iorb->NotifyAddress(iorb);
1055 }
1056}
1057
1058/******************************************************************************
1059 * Requeue the specified IORB such that it will be sent downstream for
1060 * processing again. This includes freeing all resources currently allocated
1061 * (timer, buffer, ...) and resetting the flags to 0.
1062 *
1063 * The following flags are preserved:
1064 * - no_ncq
1065 */
1066void iorb_requeue(IORBH _far *iorb)
1067{
1068 ADD_WORKSPACE _far *aws = add_workspace(iorb);
1069 u16 no_ncq = aws->no_ncq;
1070
1071 aws_free(aws);
1072 memset(aws, 0x00, sizeof(*aws));
1073 aws->no_ncq = no_ncq;
1074}
1075
1076/******************************************************************************
1077 * Timeout handler for I/O commands. Since timeout handling can involve
1078 * lengthy operations like port resets, the main code is located in a
1079 * separate function which is invoked via a context hook.
1080 */
1081void _cdecl _far timeout_callback(ULONG timer_handle, ULONG p1,
1082 ULONG p2)
1083{
1084 IORBH _far *iorb = (IORBH _far *) p1;
1085 int a = iorb_unit_adapter(iorb);
1086 int p = iorb_unit_port(iorb);
1087
1088 ADD_CancelTimer(timer_handle);
1089 dprintf("timeout for IORB %Fp\n", iorb);
1090
1091 /* Move the timed-out IORB to the abort queue. Since it's possible that the
1092 * IORB has completed after the timeout has expired but before we got to
1093 * this line of code, we'll check the return code of iorb_queue_del(): If it
1094 * returns an error, the IORB must have completed a few microseconds ago and
1095 * there is no timeout.
1096 */
1097 spin_lock(drv_lock);
1098 if (iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb) == 0) {
1099 iorb_queue_add(&abort_queue, iorb);
1100 iorb->ErrorCode = IOERR_ADAPTER_TIMEOUT;
1101 }
1102 spin_unlock(drv_lock);
1103
1104 /* Trigger abort processing function. We don't really care whether this
1105 * succeeds because the only reason why it would fail should be multiple
1106 * calls to DevHelp_ArmCtxHook() before the context hook had a chance to
1107 * start executing, which leaves two scenarios:
1108 *
1109 * - We succeded in arming the context hook. Fine.
1110 *
1111 * - We armed the context hook a second time before it had a chance to
1112 * start executing. In this case, the already scheduled context hook
1113 * will process our IORB as well.
1114 */
1115 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
1116}
1117
1118/******************************************************************************
1119 * small_code_ - this dummy func resolves the undefined reference linker
1120 * error that occurrs when linking WATCOM objects with DDK's link.exe
1121 */
1122void _cdecl small_code_(void)
1123{
1124}
Note: See TracBrowser for help on using the repository browser.