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

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

removed NCQ flag array dump

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