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

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

fixed NCQ disabling via switch

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