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

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