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

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

working on interrupts, bug not found so far

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