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

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

for watcom build: use watcom calling convention as default, _cdecl where needed

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