source: trunk/src/os2ahci.c@ 4

Last change on this file since 4 was 4, checked in by root, 15 years ago

initial checkin of CM's code

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