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

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

latest NCQ changes from Christian

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