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

Last change on this file since 108 was 108, checked in by Markus Thielen, 14 years ago

fix for #1

File size: 45.7 KB
Line 
1/******************************************************************************
2 * os2ahci.c - main file for os2ahci driver
3 *
4 * Copyright (c) 2011 thi.guten Software Development
5 * Copyright (c) 2011 Mensys B.V.
6 *
7 * Authors: Christian Mueller, Markus Thielen
8 *
9 * Parts copied from/inspired by the Linux AHCI driver;
10 * those parts are (c) Linux AHCI/ATA maintainers
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#include "os2ahci.h"
28#include "bldday.h"
29
30#include "ioctl.h"
31
32/* -------------------------- macros and constants ------------------------- */
33
34/* parse integer command line parameter */
35#define drv_parm_int(s, value, type, radix) \
36 { \
37 char _far *_ep; \
38 if ((s)[1] != ':') { \
39 cprintf("missing colon (:) after /%c\n", *(s)); \
40 goto init_fail; \
41 } \
42 value = (type) strtol((s) + 2, \
43 (const char _far* _far*) &_ep, \
44 radix); \
45 s = _ep; \
46 }
47
48/* set two-dimensional array of port options */
49#define set_port_option(opt, val) \
50 if (adapter_index == -1) { \
51 /* set option for all adapters and ports */ \
52 memset(opt, val, sizeof(opt)); \
53 } else if (port_index == -1) { \
54 /* set option for all ports on current adapter */ \
55 memset(opt[adapter_index], val, sizeof(*opt)); \
56 } else { \
57 /* set option for specific port */ \
58 opt[adapter_index][port_index] = val; \
59 }
60
61/* ------------------------ typedefs and structures ------------------------ */
62
63/* -------------------------- function prototypes -------------------------- */
64
65void _cdecl small_code_ (void);
66
67static int add_unit_info (IORB_CONFIGURATION _far *iorb_conf, int dt_ai,
68 int a, int p, int d, int scsi_id);
69
70/* ------------------------ global/static variables ------------------------ */
71
72int debug = 0; /* if > 0, print debug messages to COM1 */
73int thorough_scan = 1; /* if != 0, perform thorough PCI scan */
74int init_reset; /* if != 0, reset ports during init */
75int verbosity; /* if > 0, show some info during boot */
76
77PFN Device_Help = 0; /* pointer to device helper entry point */
78ULONG RMFlags = 0; /* required by resource manager library */
79PFN RM_Help0 = NULL; /* required by resource manager library */
80PFN RM_Help3 = NULL; /* required by resource manager library */
81HDRIVER rm_drvh; /* resource manager driver handle */
82char rm_drvname[80]; /* driver name as returned by RM */
83USHORT add_handle; /* driver handle (RegisterDeviceClass) */
84UCHAR timer_pool[TIMER_POOL_SIZE]; /* timer pool */
85
86/* resource manager driver information structure */
87DRIVERSTRUCT rm_drvinfo = {
88 "OS2AHCI", /* driver name */
89 "AHCI SATA Driver", /* driver description */
90 "GNU", /* vendor name */
91 CMVERSION_MAJOR, /* RM interface version major */
92 CMVERSION_MINOR, /* RM interface version minor */
93 BLD_YEAR, BLD_MONTH, BLD_DAY, /* date */
94 0, /* driver flags */
95 DRT_ADDDM, /* driver type */
96 DRS_ADD, /* driver sub type */
97 NULL /* driver callback */
98};
99
100ULONG drv_lock; /* driver-level spinlock */
101IORB_QUEUE driver_queue; /* driver-level IORB queue */
102AD_INFO ad_infos[MAX_AD]; /* adapter information list */
103int ad_info_cnt; /* number of entries in ad_infos[] */
104u16 ad_ignore; /* bitmap with adapter indexes to ignore */
105int init_complete; /* if != 0, initialization has completed */
106
107/* apapter/port-specific options saved when parsing the command line */
108u8 emulate_scsi[MAX_AD][AHCI_MAX_PORTS];
109u8 enable_ncq[MAX_AD][AHCI_MAX_PORTS];
110u8 link_speed[MAX_AD][AHCI_MAX_PORTS];
111u8 link_power[MAX_AD][AHCI_MAX_PORTS];
112u8 track_size[MAX_AD][AHCI_MAX_PORTS];
113
114static char init_msg[] = "OS2AHCI driver version %d.%02d\n";
115static char exit_msg[] = "OS2AHCI driver *not* installed\n";
116
117/* ----------------------------- start of code ----------------------------- */
118
119/******************************************************************************
120 * OS/2 device driver main strategy function. This function is only used
121 * for initialization purposes; all other calls go directly to the adapter
122 * device driver's strategy function.
123 */
124USHORT _cdecl c_strat(RPH _far *req)
125{
126 u16 rc;
127
128 switch (req->Cmd) {
129
130 case CMDInitBase:
131 rc = init_drv((RPINITIN _far *) req);
132 break;
133
134 case CMDShutdown:
135 rc = exit_drv(((RPSAVERESTORE _far *) req)->FuncCode);
136 break;
137
138 case CMDGenIOCTL:
139 rc = gen_ioctl((RP_GENIOCTL _far *) req);
140 break;
141
142 default:
143 rc = STDON | STATUS_ERR_UNKCMD;
144 break;
145 }
146
147 return(rc);
148}
149
150/******************************************************************************
151 * Intialize the os2ahci driver. This includes command line parsing, scanning
152 * the PCI bus for supported AHCI adapters, etc.
153 */
154USHORT init_drv(RPINITIN _far *req)
155{
156 RPINITOUT _far *rsp = (RPINITOUT _far *) req;
157 DDD_PARM_LIST _far *ddd_pl = (DDD_PARM_LIST _far *) req->InitArgs;
158 APIRET rmrc;
159 char _far *cmd_line;
160 char _far *s;
161 int adapter_index = -1;
162 int port_index = -1;
163 int invert_option;
164 int optval;
165 u16 vendor;
166 u16 device;
167
168 /* set device helper entry point */
169 Device_Help = req->DevHlpEP;
170
171 /* create driver-level spinlock */
172 DevHelp_CreateSpinLock(&drv_lock);
173
174 /* initialize libc code */
175 init_libc();
176
177 /* print initialization message */
178 cprintf(init_msg, VERSION / 100, VERSION % 100);
179
180#ifdef ECS_BUILD
181 cprintf("This driver is licensed for use only in conjunction with eComStation.");
182#endif
183
184 /* register driver with resource manager */
185 if ((rmrc = RMCreateDriver(&rm_drvinfo, &rm_drvh)) != RMRC_SUCCESS) {
186 cprintf("failed to register driver with resource manager (rc = %d)\n", rmrc);
187 goto init_fail;
188 }
189
190 /* parse command line parameters */
191 cmd_line = (char _far *) ((u32) ddd_pl & 0xffff0000l) + ddd_pl->cmd_line_args;
192
193 for (s = cmd_line; *s != 0; s++) {
194 if (*s == '/') {
195 if ((invert_option = (s[1] == '!')) != 0) {
196 s++;
197 }
198 s++;
199 switch (tolower(*s)) {
200
201 case '\0':
202 /* end of command line; can only happen if command line is incorrect */
203 cprintf("incomplete command line option\n");
204 goto init_fail;
205
206 case 'c':
207 /* set COM port base address for debug messages */
208 drv_parm_int(s, com_base, u16, 16);
209 break;
210
211 case 'd':
212 /* increase debug level */
213 if (debug++ == 0) {
214 init_com();
215 }
216 break;
217
218 case 'g':
219 /* add specfied PCI ID as a supported generic AHCI adapter */
220 drv_parm_int(s, vendor, u16, 16);
221 drv_parm_int(s, device, u16, 16);
222 if (add_pci_id(vendor, device)) {
223 cprintf("failed to add PCI ID %04x:%04x\n", vendor, device);
224 goto init_fail;
225 }
226 thorough_scan = 1;
227 break;
228
229 case 't':
230 /* perform thorough PCI scan (i.e. look for individual supported PCI IDs) */
231 thorough_scan = !invert_option;
232 break;
233
234 case 'r':
235 /* reset ports during initialization */
236 init_reset = 1;
237 break;
238
239 case 'a':
240 /* set adapter index for adapter and port-related options */
241 drv_parm_int(s, adapter_index, int, 10);
242 if (adapter_index < 0 || adapter_index >= MAX_AD) {
243 cprintf("invalid adapter index (%d)\n", adapter_index);
244 goto init_fail;
245 }
246 break;
247
248 case 'p':
249 /* set port index for port-related options */
250 drv_parm_int(s, port_index, int, 10);
251 if (port_index < 0 || port_index >= AHCI_MAX_PORTS) {
252 cprintf("invalid port index (%d)\n", port_index);
253 goto init_fail;
254 }
255 break;
256
257 case 'i':
258 /* ignore current adapter index */
259 if (adapter_index >= 0) {
260 ad_ignore |= 1U << adapter_index;
261 }
262 break;
263
264 case 's':
265 /* enable SCSI emulation for ATAPI devices */
266 set_port_option(emulate_scsi, !invert_option);
267 break;
268
269 case 'n':
270 /* enable NCQ */
271 set_port_option(enable_ncq, !invert_option);
272 break;
273
274 case 'l':
275 /* set link speed or power savings */
276 s++;
277 switch (tolower(*s)) {
278 case 's':
279 /* set link speed */
280 drv_parm_int(s, optval, int, 10);
281 set_port_option(link_speed, optval);
282 break;
283 case 'p':
284 /* set power management */
285 drv_parm_int(s, optval, int, 10);
286 set_port_option(link_power, optval);
287 break;
288 default:
289 cprintf("invalid link parameter (%c)\n", *s);
290 goto init_fail;
291 }
292 /* need to reset the port in order to establish link settings */
293 init_reset = 1;
294 break;
295
296 case '4':
297 /* enable 4K sector geometry enhancement (track size = 56) */
298 if (!invert_option) {
299 set_port_option(track_size, 56);
300 }
301 break;
302
303 case 'v':
304 /* be verbose during boot */
305 verbosity++;
306 break;
307
308 default:
309 cprintf("invalid option: /%c\n", *s);
310 goto init_fail;
311 }
312 }
313 }
314
315 /* scan PCI bus for supported devices */
316 scan_pci_bus();
317
318 if (ad_info_cnt > 0) {
319 /* initialization succeeded and we found at least one AHCI adapter */
320 ADD_InitTimer(timer_pool, sizeof(timer_pool));
321 mdelay_cal();
322
323 if (DevHelp_RegisterDeviceClass("OS2AHCI", (PFN) add_entry, 0, 1,
324 &add_handle)) {
325 cprintf("error: couldn't register device class\n");
326 goto init_fail;
327 }
328
329 /* allocate context hooks */
330 if (DevHelp_AllocateCtxHook(mk_NPFN(restart_hook), &restart_ctxhook_h) != 0 ||
331 DevHelp_AllocateCtxHook(mk_NPFN(reset_hook), &reset_ctxhook_h) != 0 ||
332 DevHelp_AllocateCtxHook(mk_NPFN(engine_hook), &engine_ctxhook_h)) {
333 cprintf("failed to allocate task-time context hooks\n");
334 goto init_fail;
335 }
336
337 rsp->CodeEnd = (u16) end_of_code;
338 rsp->DataEnd = (u16) &end_of_data;
339 return(STDON);
340
341 } else {
342 /* no adapters found */
343 cprintf(" No adapters found.\n");
344 }
345
346init_fail:
347 /* initialization failed; set segment sizes to 0 and return error */
348 rsp->CodeEnd = 0;
349 rsp->DataEnd = 0;
350
351 /* free context hooks */
352 if (engine_ctxhook_h != 0) DevHelp_FreeCtxHook(engine_ctxhook_h);
353 if (reset_ctxhook_h != 0) DevHelp_FreeCtxHook(reset_ctxhook_h);
354 if (restart_ctxhook_h != 0) DevHelp_FreeCtxHook(restart_ctxhook_h);
355
356 if (rm_drvh != 0) {
357 /* remove driver from resource manager */
358 RMDestroyDriver(rm_drvh);
359 }
360
361 cprintf(exit_msg);
362 return(STDON | ERROR_I24_QUIET_INIT_FAIL);
363}
364
365/******************************************************************************
366 * Generic IOCTL via character device driver. IOCTLs are used to control the
367 * driver operation and to execute native ATA and ATAPI (SCSI) commands from
368 * ring 3 applications.
369 */
370USHORT gen_ioctl(RP_GENIOCTL _far *ioctl)
371{
372 switch (ioctl->Category) {
373
374 case OS2AHCI_IOCTL_CATEGORY:
375
376 switch (ioctl->Function) {
377
378 case OS2AHCI_IOCTL_GET_DEVLIST:
379 return(ioctl_get_devlist(ioctl));
380
381 case OS2AHCI_IOCTL_PASSTHROUGH:
382 return(ioctl_passthrough(ioctl));
383
384 }
385 }
386 return(STDON | STATUS_ERR_UNKCMD);
387}
388
389/******************************************************************************
390 * Device driver exit handler. This handler is called when OS/2 shuts down and
391 * flushes the write caches of all attached devices. Since this is effectively
392 * the same we do when suspending, we'll call out to the corresponding APM
393 * function.
394 *
395 * NOTE: Errors are ignored because there's no way we could stop the shutdown
396 * or do something about the error, unless retrying endlessly is
397 * considered an option.
398 */
399USHORT exit_drv(int func)
400{
401 dprintf("exit_drv(%d) called\n", func);
402
403 if (func == 0) {
404 /* we're only interested in the second phase of the shutdown */
405 return(STDON);
406 }
407
408 apm_suspend();
409 return(STDON);
410}
411
412/******************************************************************************
413 * ADD entry point. This is the main entry point for all ADD requests. Due to
414 * the asynchronous nature of ADD drivers, this function primarily queues the
415 * IORB(s) to the corresponding adapter or port queues, then triggers the
416 * state machine to initiate processing queued IORBs.
417 *
418 * NOTE: In order to prevent race conditions or engine stalls, certain rules
419 * around locking, unlocking and IORB handling in general have been
420 * established. Refer to the comments in "trigger_engine()" for
421 * details.
422 */
423void _cdecl _far _loadds add_entry(IORBH _far *first_iorb)
424{
425 IORBH _far *iorb;
426 IORBH _far *next = NULL;
427
428 spin_lock(drv_lock);
429
430 for (iorb = first_iorb; iorb != NULL; iorb = next) {
431 /* Queue this IORB. Queues primarily exist on port level but there are
432 * some requests which affect the whole driver, most notably
433 * IOCC_CONFIGURATION. In either case, adding the IORB to the driver or
434 * port queue will change the links, thus we need to save the original
435 * link in 'next'.
436 */
437 next = (iorb->RequestControl | IORB_CHAIN) ? iorb->pNxtIORB : 0;
438
439 iorb->Status = 0;
440 iorb->ErrorCode = 0;
441 memset(&iorb->ADDWorkSpace, 0x00, sizeof(ADD_WORKSPACE));
442
443 if (iorb_driver_level(iorb)) {
444 /* driver-level IORB */
445 iorb->UnitHandle = 0;
446 iorb_queue_add(&driver_queue, iorb);
447
448 } else {
449 /* port-level IORB */
450 int a = iorb_unit_adapter(iorb);
451 int p = iorb_unit_port(iorb);
452 int d = iorb_unit_device(iorb);
453
454 if (a >= ad_info_cnt ||
455 p > ad_infos[a].port_max ||
456 d > ad_infos[a].ports[p].dev_max ||
457 (ad_infos[a].port_map & (1UL << p)) == 0) {
458
459 /* unit handle outside of the allowed range */
460 dprintf("warning: IORB for %d.%d.%d out of range\n", a, p, d);
461 iorb->Status = IORB_ERROR;
462 iorb->ErrorCode = IOERR_CMD_SYNTAX;
463 iorb_complete(iorb);
464 continue;
465 }
466
467 iorb_queue_add(&ad_infos[a].ports[p].iorb_queue, iorb);
468 }
469 }
470
471 /* trigger state machine */
472 trigger_engine();
473
474 spin_unlock(drv_lock);
475}
476
477/******************************************************************************
478 * Trigger IORB queue engine. This is a wrapper function for trigger_engine_1()
479 * which will try to get all IORBs sent on their way a couple of times. If
480 * there are still IORBs ready for processing after this, this function will
481 * hand off to a context hook which will continue to trigger the engine until
482 * all IORBs have been sent.
483 */
484void trigger_engine(void)
485{
486 int i;
487
488 for (i = 0; i < 3; i++) {
489 if (trigger_engine_1() == 0) {
490 /* done -- all IORBs have been sent on their way */
491 return;
492 }
493 }
494
495 /* Something keeps bouncing; hand off to the engine context hook which will
496 * keep trying in the background.
497 */
498 DevHelp_ArmCtxHook(0, engine_ctxhook_h);
499}
500
501/******************************************************************************
502 * Trigger IORB queue engine in order to send commands in the driver/port IORB
503 * queues to the AHCI hardware. This function will return the number of IORBs
504 * sent. Keep in mind that IORBs might "bounce" if the adapter/port is not in
505 * a state to accept the command, thus it might take quite a few calls to get
506 * all IORBs on their way. This is why there's a wrapper function which tries
507 * it a few times, then hands off to a context hook which will keep trying in
508 * the background.
509 *
510 * IORBs might complete before send_iorb() has returned, at any time during
511 * interrupt processing or on another CPU on SMP systems. IORB completion
512 * means modifications to the corresponding IORB queue (the completed IORB
513 * is removed from the queue) thus we need to protect the IORB queues from
514 * race conditions. The safest approach short of keeping the driver-level
515 * spinlock aquired permanently is to keep it throughout this function and
516 * release it temporarily in send_iorb().
517 *
518 * This implies that the handler functions are fully responsible for aquiring
519 * the driver-level spinlock when they need it, and for releasing it again.
520 *
521 * As a rule of thumb, get the driver-level spinlock whenever accessing
522 * volatile variables (IORB queues, values in ad_info[], ...).
523 *
524 * Additional Notes:
525 *
526 * - This function is expected to be called with the spinlock aquired
527 *
528 * - Adapters can be flagged as 'busy' which means no new IORBs are sent (they
529 * just remain in the queue). This can be used to release the driver-level
530 * spinlock while making sure no new IORBs are going to hit the hardware.
531 * In order to prevent engine stalls, all handlers using this functionality
532 * need to invoke trigger_engine() after resetting the busy flag.
533 *
534 * - Driver-level IORBs are not synchronized by adapter-level 'busy' flags.
535 * However, the driver-level queue is worked "one entry at a time" which
536 * means that no new IORBs will be queued on the driver-level queue until
537 * the head element has completed processing. This means that driver-
538 * level IORB handlers don't need to protect against each other. But they
539 * they do need to keep in mind interference with port-level IORBs:
540 *
541 * - Driver-level IORB handlers must obtain the spinlock and/or flag all
542 * adapters as 'busy' which are affected by the driver-level IORB
543 *
544 * - Driver-level IORB handlers must not access the hardware of a
545 * particular adapter if it's flagged as 'busy' by another IORB.
546 */
547int trigger_engine_1(void)
548{
549 IORBH _far *iorb;
550 IORBH _far *next;
551 int iorbs_sent = 0;
552 int a;
553 int p;
554
555 iorbs_sent = 0;
556
557 /* process driver-level IORBs */
558 if ((iorb = driver_queue.root) != NULL && !add_workspace(iorb)->processing) {
559 send_iorb(iorb);
560 iorbs_sent++;
561 }
562
563 /* process port-level IORBs */
564 for (a = 0; a < ad_info_cnt; a++) {
565 AD_INFO *ai = ad_infos + a;
566 if (ai->busy) {
567 /* adapter is busy; don't process any IORBs */
568 continue;
569 }
570 for (p = 0; p <= ai->port_max; p++) {
571 /* send all queued IORBs on this port */
572 next = NULL;
573 for (iorb = ai->ports[p].iorb_queue.root; iorb != NULL; iorb = next) {
574 next = iorb->pNxtIORB;
575 if (!add_workspace(iorb)->processing) {
576 send_iorb(iorb);
577 iorbs_sent++;
578 }
579 }
580 }
581 }
582
583 return(iorbs_sent);
584}
585
586/******************************************************************************
587 * Send a single IORB to the corresponding AHCI adapter/port. This is just a
588 * switch board for calling the corresponding iocc_*() handler function.
589 *
590 * NOTE: This function is expected to be called with the driver-level spinlock
591 * aquired. It will release it before calling any of the handler
592 * functions and re-aquire it when done.
593 */
594void send_iorb(IORBH _far *iorb)
595{
596 /* Mark IORB as "processing" before doing anything else. Once the IORB is
597 * marked as "processing", we can release the spinlock because subsequent
598 * invocations of trigger_engine() (e.g. at interrupt time) will ignore this
599 * IORB.
600 */
601 add_workspace(iorb)->processing = 1;
602 spin_unlock(drv_lock);
603
604 switch (iorb->CommandCode) {
605
606 case IOCC_CONFIGURATION:
607 iocc_configuration(iorb);
608 break;
609
610 case IOCC_DEVICE_CONTROL:
611 iocc_device_control(iorb);
612 break;
613
614 case IOCC_UNIT_CONTROL:
615 iocc_unit_control(iorb);
616 break;
617
618 case IOCC_GEOMETRY:
619 iocc_geometry(iorb);
620 break;
621
622 case IOCC_EXECUTE_IO:
623 iocc_execute_io(iorb);
624 break;
625
626 case IOCC_UNIT_STATUS:
627 iocc_unit_status(iorb);
628 break;
629
630 case IOCC_ADAPTER_PASSTHRU:
631 iocc_adapter_passthru(iorb);
632 break;
633
634 default:
635 /* unsupported call */
636 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
637 iorb_done(iorb);
638 break;
639 }
640
641 /* re-aquire spinlock before returning to trigger_engine() */
642 spin_lock(drv_lock);
643}
644
645/******************************************************************************
646 * Handle IOCC_CONFIGURATION requests.
647 */
648void iocc_configuration(IORBH _far *iorb)
649{
650 int a;
651
652 switch (iorb->CommandModifier) {
653
654 case IOCM_COMPLETE_INIT:
655 /* Complete initialization. From now on, we won't have to restore the BIOS
656 * configuration after each command and we're fully operational (i.e. will
657 * use interrupts, timers and context hooks instead of polling).
658 */
659 if (!init_complete) {
660 dprintf("leaving initialization mode\n");
661 for (a = 0; a < ad_info_cnt; a++) {
662 lock_adapter(ad_infos + a);
663 ahci_complete_init(ad_infos + a);
664 }
665 init_complete = 1;
666
667 /* release all adapters */
668 for (a = 0; a < ad_info_cnt; a++) {
669 unlock_adapter(ad_infos + a);
670 }
671
672 /* register APM hook */
673 apm_init();
674 }
675 iorb_done(iorb);
676 break;
677
678 case IOCM_GET_DEVICE_TABLE:
679 /* construct a device table */
680 iocm_device_table(iorb);
681 break;
682
683 default:
684 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
685 iorb_done(iorb);
686 break;
687 }
688}
689
690/******************************************************************************
691 * Handle IOCC_DEVICE_CONTROL requests.
692 */
693void iocc_device_control(IORBH _far *iorb)
694{
695 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
696 IORBH _far *ptr;
697 IORBH _far *next = NULL;
698 int p = iorb_unit_port(iorb);
699 int d = iorb_unit_device(iorb);
700
701 switch (iorb->CommandModifier) {
702
703 case IOCM_ABORT:
704 /* abort all pending commands on specified port and device */
705 spin_lock(drv_lock);
706 for (ptr = ai->ports[p].iorb_queue.root; ptr != NULL; ptr = next) {
707 next = ptr->pNxtIORB;
708 /* move all matching IORBs to the abort queue */
709 if (ptr != iorb && iorb_unit_device(ptr) == d) {
710 iorb_queue_del(&ai->ports[p].iorb_queue, ptr);
711 iorb_queue_add(&abort_queue, ptr);
712 ptr->ErrorCode = IOERR_CMD_ABORTED;
713 }
714 }
715 spin_unlock(drv_lock);
716
717 /* trigger reset context hook which will finish the abort processing */
718 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
719 break;
720
721 case IOCM_SUSPEND:
722 case IOCM_RESUME:
723 case IOCM_GET_QUEUE_STATUS:
724 /* Suspend/resume operations allow access to the hardware for other
725 * entities such as IBMIDECD.FLT. Since os2ahci implements both ATA
726 * and ATAPI in the same driver, this won't be required.
727 */
728 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
729 break;
730
731 case IOCM_LOCK_MEDIA:
732 case IOCM_UNLOCK_MEDIA:
733 case IOCM_EJECT_MEDIA:
734 /* unit control commands to lock, unlock and eject media */
735 /* will be supported later... */
736 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
737 break;
738
739 default:
740 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
741 break;
742 }
743
744 iorb_done(iorb);
745}
746
747/******************************************************************************
748 * Handle IOCC_UNIT_CONTROL requests.
749 */
750void iocc_unit_control(IORBH _far *iorb)
751{
752 IORB_UNIT_CONTROL _far *iorb_uc = (IORB_UNIT_CONTROL _far *) iorb;
753 int a = iorb_unit_adapter(iorb);
754 int p = iorb_unit_port(iorb);
755 int d = iorb_unit_device(iorb);
756
757 spin_lock(drv_lock);
758 switch (iorb->CommandModifier) {
759
760 case IOCM_ALLOCATE_UNIT:
761 /* allocate unit for exclusive access */
762 if (ad_infos[a].ports[p].devs[d].allocated) {
763 iorb_seterr(iorb, IOERR_UNIT_ALLOCATED);
764 } else {
765 ad_infos[a].ports[p].devs[d].allocated = 1;
766 }
767 break;
768
769 case IOCM_DEALLOCATE_UNIT:
770 /* deallocate exclusive access to unit */
771 if (!ad_infos[a].ports[p].devs[d].allocated) {
772 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
773 } else {
774 ad_infos[a].ports[p].devs[d].allocated = 0;
775 }
776 break;
777
778 case IOCM_CHANGE_UNITINFO:
779 /* Change unit (device) information. One reason for this IOCM is the
780 * interface for filter device drivers: a filter device driver can
781 * either change existing UNITINFOs or permanently allocate units
782 * and fabricate new [logical] units; the former is the reason why we
783 * must store the pointer to the updated UNITNIFO for subsequent
784 * IOCC_CONFIGURATION/IOCM_GET_DEVICE_TABLE calls.
785 */
786 if (!ad_infos[a].ports[p].devs[d].allocated) {
787 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
788 break;
789 }
790 ad_infos[a].ports[p].devs[d].unit_info = iorb_uc->pUnitInfo;
791 break;
792
793 default:
794 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
795 break;
796 }
797
798 spin_unlock(drv_lock);
799 iorb_done(iorb);
800}
801
802/******************************************************************************
803 * Scan all ports for AHCI devices and construct a DASD device table.
804 *
805 * NOTES: This function may be called multiple times. Only the first
806 * invocation will actually scan for devices; all subsequent calls will
807 * merely return the results of the initial scan, potentially augmented
808 * by modified unit infos after IOCC_CONFIGURATION/IOCM_CHANGE_UNITINFO
809 * requests.
810 *
811 * In order to support applications that can't deal with ATAPI devices
812 * (i.e. need a SCSI adapter) os2ahci will optionally report ATAPI
813 * dvices as SCSI devices. The corresponding SCSI adapter doesn't
814 * really exist and is only reported here for the IOCM_GET_DEVICETABLE
815 * request. The units attached to this adapter will use the real HW
816 * unit IDs, thus we'll never receive a command specific to the
817 * emulated SCSI adapter and won't need to set up any sort of entity
818 * for it; the only purpose of the emulated SCSI adapter is to pass the
819 * bus type "AI_DEVBUS_SCSI_2" upstream, and the emulated units, of
820 * course. The emulated SCSI target IDs are allocated as follows:
821 *
822 * 0 the virtual adapter
823 * 1..n emulated devices; SCSI target ID increments sequentially
824 */
825void iocm_device_table(IORBH _far *iorb)
826{
827 IORB_CONFIGURATION _far *iorb_conf;
828 DEVICETABLE _far *dt;
829 char _far *pos;
830 int scsi_units = 0;
831 int scsi_id = 1;
832 int rc;
833 int dta;
834 int a;
835 int p;
836 int d;
837
838 iorb_conf = (IORB_CONFIGURATION _far *) iorb;
839 dt = iorb_conf->pDeviceTable;
840
841 spin_lock(drv_lock);
842
843 /* initialize device table header */
844 dt->ADDLevelMajor = ADD_LEVEL_MAJOR;
845 dt->ADDLevelMinor = ADD_LEVEL_MINOR;
846 dt->ADDHandle = add_handle;
847 dt->TotalAdapters = ad_info_cnt + 1;
848
849 /* set start of adapter and device information tables */
850 pos = (char _far *) (dt->pAdapter + dt->TotalAdapters);
851
852 /* go through all adapters, including the virtual SCSI adapter */
853 for (dta = 0; dta < dt->TotalAdapters; dta++) {
854 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) pos;
855
856 /* sanity check for sufficient space in device table */
857 if ((u32) (ptr + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
858 dprintf("error: device table provided by DASD too small\n");
859 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
860 goto iocm_device_table_done;
861 }
862
863 dt->pAdapter[dta] = (ADAPTERINFO _near *) ((u32) ptr & 0xffff);
864 memset(ptr, 0x00, sizeof(*ptr));
865
866 ptr->AdapterIOAccess = AI_IOACCESS_BUS_MASTER;
867 ptr->AdapterHostBus = AI_HOSTBUS_OTHER | AI_BUSWIDTH_32BIT;
868 ptr->AdapterFlags = AF_16M | AF_HW_SCATGAT;
869 ptr->MaxHWSGList = AHCI_MAX_SG / 2; /* AHCI S/G elements are 22 bits */
870
871 if (dta < ad_info_cnt) {
872 /* this is a physical AHCI adapter */
873 AD_INFO *ad_info = ad_infos + dta;
874
875 ptr->AdapterDevBus = AI_DEVBUS_ST506 | AI_DEVBUS_32BIT;
876 sprintf(ptr->AdapterName, "AHCI_%d", dta);
877
878 if (!ad_info->port_scan_done) {
879 /* first call; need to scan AHCI hardware for devices */
880 if (ad_info->busy) {
881 dprintf("error: port scan requested while adapter was busy\n");
882 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
883 goto iocm_device_table_done;
884 }
885 ad_info->busy = 1;
886 spin_unlock(drv_lock);
887 rc = ahci_scan_ports(ad_info);
888 spin_lock(drv_lock);
889 ad_info->busy = 0;
890
891 if (rc != 0) {
892 dprintf("error: port scan failed on adapter #%d\n", dta);
893 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
894 goto iocm_device_table_done;
895 }
896 ad_info->port_scan_done = 1;
897 }
898
899 /* insert physical (i.e. AHCI) devices into the device table */
900 for (p = 0; p <= ad_info->port_max; p++) {
901 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
902 if (ad_info->ports[p].devs[d].present) {
903 if (ad_info->ports[p].devs[d].atapi && emulate_scsi[dta][p]) {
904 /* only report this unit as SCSI unit */
905 scsi_units++;
906 continue;
907 }
908 if (add_unit_info(iorb_conf, dta, dta, p, d, 0)) {
909 goto iocm_device_table_done;
910 }
911 }
912 }
913 }
914
915 } else {
916 /* this is the virtual SCSI adapter */
917 if (scsi_units == 0) {
918 /* not a single unit to be emulated via SCSI */
919 dt->TotalAdapters--;
920 break;
921 }
922
923 /* set adapter name and bus type to mimic a SCSI controller */
924 ptr->AdapterDevBus = AI_DEVBUS_SCSI_2 | AI_DEVBUS_16BIT;
925 sprintf(ptr->AdapterName, "AHCI_SCSI_0");
926
927 /* add all ATAPI units to be emulated by this virtual adaper */
928 for (a = 0; a < ad_info_cnt; a++) {
929 AD_INFO *ad_info = ad_infos + a;
930
931 for (p = 0; p <= ad_info->port_max; p++) {
932 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
933 if (ad_info->ports[p].devs[d].present &&
934 ad_info->ports[p].devs[d].atapi &&
935 emulate_scsi[a][p]) {
936 if (add_unit_info(iorb_conf, dta, a, p, d, scsi_id++)) {
937 goto iocm_device_table_done;
938 }
939 }
940 }
941 }
942 }
943 }
944
945 /* calculate offset for next adapter */
946 pos = (char _far *) (ptr->UnitInfo + ptr->AdapterUnits);
947 }
948
949iocm_device_table_done:
950 spin_unlock(drv_lock);
951 iorb_done(iorb);
952}
953
954/******************************************************************************
955 * Handle IOCC_GEOMETRY requests.
956 */
957void iocc_geometry(IORBH _far *iorb)
958{
959 switch (iorb->CommandModifier) {
960
961 case IOCM_GET_MEDIA_GEOMETRY:
962 case IOCM_GET_DEVICE_GEOMETRY:
963 add_workspace(iorb)->idempotent = 1;
964 ahci_get_geometry(iorb);
965 break;
966
967 default:
968 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
969 iorb_done(iorb);
970 }
971}
972
973/******************************************************************************
974 * Handle IOCC_EXECUTE_IO requests.
975 */
976void iocc_execute_io(IORBH _far *iorb)
977{
978 switch (iorb->CommandModifier) {
979
980 case IOCM_READ:
981 add_workspace(iorb)->idempotent = 1;
982 ahci_read(iorb);
983 break;
984
985 case IOCM_READ_VERIFY:
986 add_workspace(iorb)->idempotent = 1;
987 ahci_verify(iorb);
988 break;
989
990 case IOCM_WRITE:
991 add_workspace(iorb)->idempotent = 1;
992 ahci_write(iorb);
993 break;
994
995 case IOCM_WRITE_VERIFY:
996 add_workspace(iorb)->idempotent = 1;
997 ahci_write(iorb);
998 break;
999
1000 default:
1001 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1002 iorb_done(iorb);
1003 }
1004}
1005
1006/******************************************************************************
1007 * Handle IOCC_UNIT_STATUS requests.
1008 */
1009void iocc_unit_status(IORBH _far *iorb)
1010{
1011 switch (iorb->CommandModifier) {
1012
1013 case IOCM_GET_UNIT_STATUS:
1014 add_workspace(iorb)->idempotent = 1;
1015 ahci_unit_ready(iorb);
1016 break;
1017
1018 default:
1019 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1020 iorb_done(iorb);
1021 }
1022}
1023
1024/******************************************************************************
1025 * Handle IOCC_ADAPTER_PASSTHROUGH requests.
1026 */
1027void iocc_adapter_passthru(IORBH _far *iorb)
1028{
1029 switch (iorb->CommandModifier) {
1030
1031 case IOCM_EXECUTE_CDB:
1032 add_workspace(iorb)->idempotent = 0;
1033 ahci_execute_cdb(iorb);
1034 break;
1035
1036 case IOCM_EXECUTE_ATA:
1037 add_workspace(iorb)->idempotent = 0;
1038 ahci_execute_ata(iorb);
1039 break;
1040
1041 default:
1042 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1043 iorb_done(iorb);
1044 }
1045}
1046
1047/******************************************************************************
1048 * Add an IORB to the specified queue. This function must be called with the
1049 * adapter-level spinlock aquired.
1050 */
1051void iorb_queue_add(IORB_QUEUE _far *queue, IORBH _far *iorb)
1052{
1053 if (iorb_priority(iorb) {
1054 /* priority IORB; insert at first position */
1055 iorb->pNxtIORB = queue->root;
1056 queue->root = iorb;
1057
1058 } else {
1059 /* append IORB to end of queue */
1060 iorb->pNxtIORB = NULL;
1061
1062 if (queue->root == NULL) {
1063 queue->root = iorb;
1064 } else {
1065 queue->tail->pNxtIORB = iorb;
1066 }
1067 queue->tail = iorb;
1068 }
1069
1070 if (debug) {
1071 /* determine queue type (local, driver, abort or port) and minimum debug
1072 * level; otherwise, queue debug prints can become really confusing.
1073 */
1074 char *queue_type;
1075 int min_debug = 1;
1076
1077 if ((u32) queue >> 16 == (u32) (void _far *) &queue >> 16) {
1078 /* this queue is on the stack */
1079 queue_type = "local";
1080 min_debug = 2;
1081
1082 } else if (queue == &driver_queue) {
1083 queue_type = "driver";
1084
1085 } else if (queue == &abort_queue) {
1086 queue_type = "abort";
1087 min_debug = 2;
1088
1089 } else {
1090 queue_type = "port";
1091 }
1092
1093 if (debug >= min_debug) {
1094 printf("IORB %Fp queued (cmd = %d/%d, queue = %Fp [%s], timeout = %ld)\n",
1095 iorb, iorb->CommandCode, iorb->CommandModifier, queue, queue_type,
1096 iorb->Timeout);
1097 }
1098 }
1099}
1100
1101/******************************************************************************
1102 * Remove an IORB from the specified queue. This function must be called with
1103 * the adapter-level spinlock aquired.
1104 */
1105int iorb_queue_del(IORB_QUEUE _far *queue, IORBH _far *iorb)
1106{
1107 IORBH _far *_iorb;
1108 IORBH _far *_prev = NULL;
1109 int found = 0;
1110
1111 for (_iorb = queue->root; _iorb != NULL; _iorb = _iorb->pNxtIORB) {
1112 if (_iorb == iorb) {
1113 /* found the IORB to be removed */
1114 if (_prev != NULL) {
1115 _prev->pNxtIORB = _iorb->pNxtIORB;
1116 } else {
1117 queue->root = _iorb->pNxtIORB;
1118 }
1119 if (_iorb == queue->tail) {
1120 queue->tail = _prev;
1121 }
1122 found = 1;
1123 break;
1124 }
1125 _prev = _iorb;
1126 }
1127
1128 if (found) {
1129 ddprintf("IORB %Fp removed (queue = %Fp)\n", iorb, queue);
1130 } else {
1131 dprintf("IORB %Fp not found in queue %Fp\n", iorb, queue);
1132 }
1133
1134 return(!found);
1135}
1136
1137/******************************************************************************
1138 * Set the error code in the specified IORB
1139 *
1140 * NOTE: This function does *not* call iorb_done(). It merely sets the IORB
1141 * status to the specified error code.
1142 */
1143void iorb_seterr(IORBH _far *iorb, USHORT error_code)
1144{
1145 iorb->ErrorCode = error_code;
1146 iorb->Status |= IORB_ERROR;
1147}
1148
1149/******************************************************************************
1150 * Mark the specified IORB as done and notify the asynchronous post function,
1151 * if any. The IORB is also removed from the corresponding IORB queue.
1152 *
1153 * NOTES: This function does not clear the Status field; it merely adds the
1154 * IORB_DONE flag.
1155 *
1156 * This function is expected to be called *without* the corresponding
1157 * driver-level drv_lock aquired. It will aquire the spinlock before
1158 * updating the IORB queue and release it before notifying the upstream
1159 * code in order to prevent deadlocks.
1160 *
1161 * Due to this logic, this function is only good for simple task-time
1162 * completions. Functions working on lists of IORBs (such as interrupt
1163 * handlers or context hooks) should call iorb_complete() directly and
1164 * implement their own logic for removing the IORB from the port queue.
1165 * See abort_ctxhook() for an example.
1166 */
1167void iorb_done(IORBH _far *iorb)
1168{
1169 int a = iorb_unit_adapter(iorb);
1170 int p = iorb_unit_port(iorb);
1171
1172 /* remove IORB from corresponding queue */
1173 spin_lock(drv_lock);
1174 if (iorb_driver_level(iorb)) {
1175 iorb_queue_del(&driver_queue, iorb);
1176 } else {
1177 iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb);
1178 }
1179 aws_free(add_workspace(iorb));
1180 spin_unlock(drv_lock);
1181
1182 iorb_complete(iorb);
1183}
1184
1185/******************************************************************************
1186 * Complete an IORB. This should be called without the adapter-level spinlock
1187 * to allow the IORB completion routine to perform whatever processing it
1188 * requires. This implies that the IORB should no longer be in any global
1189 * queue because the IORB completion routine may well reuse the IORB and send
1190 * the next request to us before even returning from this function.
1191 */
1192void iorb_complete(IORBH _far *iorb)
1193{
1194 iorb->Status |= IORB_DONE;
1195
1196 dprintf("IORB %Fp complete (status = 0x%04x, error = 0x%04x)\n",
1197 iorb, iorb->Status, iorb->ErrorCode);
1198
1199 if (iorb->RequestControl & IORB_ASYNC_POST) {
1200 iorb->NotifyAddress(iorb);
1201 }
1202}
1203
1204/******************************************************************************
1205 * Requeue the specified IORB such that it will be sent downstream for
1206 * processing again. This includes freeing all resources currently allocated
1207 * (timer, buffer, ...) and resetting the flags to 0. The driver-level
1208 * spinlock must be aquired when calling this function.
1209 *
1210 * The following flags are preserved:
1211 * - no_ncq
1212 */
1213void iorb_requeue(IORBH _far *iorb)
1214{
1215 ADD_WORKSPACE _far *aws = add_workspace(iorb);
1216 u16 no_ncq = aws->no_ncq;
1217
1218 aws_free(aws);
1219 memset(aws, 0x00, sizeof(*aws));
1220 aws->no_ncq = no_ncq;
1221}
1222
1223/******************************************************************************
1224 * Free resources in ADD workspace (timer, buffer, ...). This function should
1225 * be called with the spinlock held to prevent race conditions.
1226 */
1227void aws_free(ADD_WORKSPACE _far *aws)
1228{
1229 if (aws->timer != 0) {
1230 ADD_CancelTimer(aws->timer);
1231 aws->timer = 0;
1232 }
1233
1234 if (aws->buf != NULL) {
1235 free(aws->buf);
1236 aws->buf = NULL;
1237 }
1238}
1239
1240/******************************************************************************
1241 * Lock the adapter, waiting for availability if necessary. This is expected
1242 * to be called at task/request time without the driver-level spinlock
1243 * aquired. Don't call at interrupt time.
1244 */
1245void lock_adapter(AD_INFO *ai)
1246{
1247 spin_lock(drv_lock);
1248 while (ai->busy) {
1249 spin_unlock(drv_lock);
1250 msleep(250);
1251 spin_lock(drv_lock);
1252 }
1253 ai->busy = 1;
1254 spin_unlock(drv_lock);
1255}
1256
1257/******************************************************************************
1258 * Unlock adapter (i.e. reset busy flag)
1259 */
1260void unlock_adapter(AD_INFO *ai)
1261{
1262 ai->busy = 0;
1263}
1264
1265/******************************************************************************
1266 * Timeout handler for I/O commands. Since timeout handling can involve
1267 * lengthy operations like port resets, the main code is located in a
1268 * separate function which is invoked via a context hook.
1269 */
1270void _cdecl _far timeout_callback(ULONG timer_handle, ULONG p1,
1271 ULONG p2)
1272{
1273 IORBH _far *iorb = (IORBH _far *) p1;
1274 int a = iorb_unit_adapter(iorb);
1275 int p = iorb_unit_port(iorb);
1276
1277 ADD_CancelTimer(timer_handle);
1278 dprintf("timeout for IORB %Fp\n", iorb);
1279
1280 /* Move the timed-out IORB to the abort queue. Since it's possible that the
1281 * IORB has completed after the timeout has expired but before we got to
1282 * this line of code, we'll check the return code of iorb_queue_del(): If it
1283 * returns an error, the IORB must have completed a few microseconds ago and
1284 * there is no timeout.
1285 */
1286 spin_lock(drv_lock);
1287 if (iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb) == 0) {
1288 iorb_queue_add(&abort_queue, iorb);
1289 iorb->ErrorCode = IOERR_ADAPTER_TIMEOUT;
1290 }
1291 spin_unlock(drv_lock);
1292
1293 /* Trigger abort processing function. We don't really care whether this
1294 * succeeds because the only reason why it would fail should be multiple
1295 * calls to DevHelp_ArmCtxHook() before the context hook had a chance to
1296 * start executing, which leaves two scenarios:
1297 *
1298 * - We succeded in arming the context hook. Fine.
1299 *
1300 * - We armed the context hook a second time before it had a chance to
1301 * start executing. In this case, the already scheduled context hook
1302 * will process our IORB as well.
1303 */
1304 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
1305
1306 /* Set up a watchdog timer which calls the context hook manually in case
1307 * some kernel thread is looping around the IORB_COMPLETE status bit
1308 * without yielding the CPU (kernel threads don't preempt). This shouldn't
1309 * happen per design because kernel threads are supposed to yield but it
1310 * does in the early boot phase.
1311 */
1312 ADD_StartTimerMS(&th_reset_watchdog, 5000, (PFN) reset_watchdog, 0, 0);
1313}
1314
1315/******************************************************************************
1316 * Reset handler watchdog. If a timeout occurs, a context hook is armed which
1317 * will execute as soon as a kernel thread yields the CPU. However, some
1318 * kernel components won't yield the CPU during the early boot phase and the
1319 * only way to kick some sense into those components is to run the context
1320 * hook right inside this timer callback. Not exactly pretty, especially
1321 * considering the fact that context hooks were implemented to prevent running
1322 * lengthy operations like a port reset at interrupt time, but without this
1323 * watchdog mechanism we run the risk of getting completely stalled by device
1324 * problems during the early boot phase.
1325 */
1326void _cdecl _far reset_watchdog(ULONG timer_handle, ULONG p1,
1327 ULONG p2)
1328{
1329 /* reset watchdog timer */
1330 ADD_CancelTimer(timer_handle);
1331 dprintf("reset watchdog invoked\n");
1332
1333 /* call context hook manually */
1334 reset_ctxhook(0);
1335}
1336
1337/******************************************************************************
1338 * small_code_ - this dummy func resolves the undefined reference linker
1339 * error that occurrs when linking WATCOM objects with DDK's link.exe
1340 */
1341void _cdecl small_code_(void)
1342{
1343}
1344
1345/******************************************************************************
1346 * Add unit info to ADAPTERINFO array (IOCC_GET_DEVICE_TABLE requests). The
1347 * adapter info array in the device table, dt->pAdapter[], is expected to be
1348 * initialized for the specified index (dt_ai).
1349 *
1350 * Please note that the device table adapter index, dta, is not always equal
1351 * to the physical adapter index, a: if SCSI emulation has been activated, the
1352 * last reported adapter is a virtual SCSI adapter and the physical adapter
1353 * indexes for those units are, of course, different from the device table
1354 * index of the virtual SCSI adapter.
1355 */
1356static int add_unit_info(IORB_CONFIGURATION _far *iorb_conf, int dta,
1357 int a, int p, int d, int scsi_id)
1358{
1359 DEVICETABLE _far *dt = iorb_conf->pDeviceTable;
1360 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) (((u32) dt & 0xffff0000U) +
1361 (u16) dt->pAdapter[dta]);
1362 UNITINFO _far *ui = ptr->UnitInfo + ptr->AdapterUnits;
1363 AD_INFO *ai = ad_infos + a;
1364
1365 if ((u32) (ui + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
1366 dprintf("error: device table provided by DASD too small\n");
1367 iorb_seterr(&iorb_conf->iorbh, IOERR_CMD_SW_RESOURCE);
1368 return(-1);
1369 }
1370
1371 if (ai->ports[p].devs[d].unit_info == NULL) {
1372 /* provide original information about this device (unit) */
1373 memset(ui, 0x00, sizeof(*ui));
1374 ui->AdapterIndex = dta; /* device table adapter index */
1375 ui->UnitHandle = iorb_unit(a, p, d); /* physical adapter index */
1376 ui->UnitIndex = ptr->AdapterUnits;
1377 ui->UnitType = ai->ports[p].devs[d].dev_type;
1378 ui->QueuingCount = ai->ports[p].devs[d].ncq_max;;
1379 if (ai->ports[p].devs[d].removable) {
1380 ui->UnitFlags |= UF_REMOVABLE;
1381 }
1382 if (scsi_id > 0) {
1383 /* set fake SCSI ID for this unit */
1384 ui->UnitSCSITargetID = scsi_id;
1385 }
1386 } else {
1387 /* copy updated device (unit) information (IOCM_CHANGE_UNITINFO) */
1388 memcpy(ui, ai->ports[p].devs[d].unit_info, sizeof(*ui));
1389 }
1390
1391 ptr->AdapterUnits++;
1392 return(0);
1393}
1394
Note: See TracBrowser for help on using the repository browser.