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

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

removed unused local vars

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