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

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

added evaluation version message; changed boot logo to contain adapter ID strings

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