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

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

added support for os2trace (buggy; messages get swallowed); reverted last change that ignored unknown command line arguments

File size: 46.0 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("unknown 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 *
484 * NOTE: While initialization has not completed (or during APM suspend/resume
485 * operations), this function will loop indefinitely because we can't
486 * rely on interrupt handlers or context hooks and complex IORBs
487 * requiring multiple requeues would eventually hang and time out if
488 * we stopped triggering here.
489 */
490void trigger_engine(void)
491{
492 int i;
493
494 for (i = 0; i < 3 || !init_complete; i++) {
495 if (trigger_engine_1() == 0) {
496 /* done -- all IORBs have been sent on their way */
497 return;
498 }
499 }
500
501 /* Something keeps bouncing; hand off to the engine context hook which will
502 * keep trying in the background.
503 */
504 DevHelp_ArmCtxHook(0, engine_ctxhook_h);
505}
506
507/******************************************************************************
508 * Trigger IORB queue engine in order to send commands in the driver/port IORB
509 * queues to the AHCI hardware. This function will return the number of IORBs
510 * sent. Keep in mind that IORBs might "bounce" if the adapter/port is not in
511 * a state to accept the command, thus it might take quite a few calls to get
512 * all IORBs on their way. This is why there's a wrapper function which tries
513 * it a few times, then hands off to a context hook which will keep trying in
514 * the background.
515 *
516 * IORBs might complete before send_iorb() has returned, at any time during
517 * interrupt processing or on another CPU on SMP systems. IORB completion
518 * means modifications to the corresponding IORB queue (the completed IORB
519 * is removed from the queue) thus we need to protect the IORB queues from
520 * race conditions. The safest approach short of keeping the driver-level
521 * spinlock aquired permanently is to keep it throughout this function and
522 * release it temporarily in send_iorb().
523 *
524 * This implies that the handler functions are fully responsible for aquiring
525 * the driver-level spinlock when they need it, and for releasing it again.
526 *
527 * As a rule of thumb, get the driver-level spinlock whenever accessing
528 * volatile variables (IORB queues, values in ad_info[], ...).
529 *
530 * Additional Notes:
531 *
532 * - This function is expected to be called with the spinlock aquired
533 *
534 * - Adapters can be flagged as 'busy' which means no new IORBs are sent (they
535 * just remain in the queue). This can be used to release the driver-level
536 * spinlock while making sure no new IORBs are going to hit the hardware.
537 * In order to prevent engine stalls, all handlers using this functionality
538 * need to invoke trigger_engine() after resetting the busy flag.
539 *
540 * - Driver-level IORBs are not synchronized by adapter-level 'busy' flags.
541 * However, the driver-level queue is worked "one entry at a time" which
542 * means that no new IORBs will be queued on the driver-level queue until
543 * the head element has completed processing. This means that driver-
544 * level IORB handlers don't need to protect against each other. But they
545 * they do need to keep in mind interference with port-level IORBs:
546 *
547 * - Driver-level IORB handlers must obtain the spinlock and/or flag all
548 * adapters as 'busy' which are affected by the driver-level IORB
549 *
550 * - Driver-level IORB handlers must not access the hardware of a
551 * particular adapter if it's flagged as 'busy' by another IORB.
552 */
553int trigger_engine_1(void)
554{
555 IORBH _far *iorb;
556 IORBH _far *next;
557 int iorbs_sent = 0;
558 int a;
559 int p;
560
561 iorbs_sent = 0;
562
563 /* process driver-level IORBs */
564 if ((iorb = driver_queue.root) != NULL && !add_workspace(iorb)->processing) {
565 send_iorb(iorb);
566 iorbs_sent++;
567 }
568
569 /* process port-level IORBs */
570 for (a = 0; a < ad_info_cnt; a++) {
571 AD_INFO *ai = ad_infos + a;
572 if (ai->busy) {
573 /* adapter is busy; don't process any IORBs */
574 continue;
575 }
576 for (p = 0; p <= ai->port_max; p++) {
577 /* send all queued IORBs on this port */
578 next = NULL;
579 for (iorb = ai->ports[p].iorb_queue.root; iorb != NULL; iorb = next) {
580 next = iorb->pNxtIORB;
581 if (!add_workspace(iorb)->processing) {
582 send_iorb(iorb);
583 iorbs_sent++;
584 }
585 }
586 }
587 }
588
589 return(iorbs_sent);
590}
591
592/******************************************************************************
593 * Send a single IORB to the corresponding AHCI adapter/port. This is just a
594 * switch board for calling the corresponding iocc_*() handler function.
595 *
596 * NOTE: This function is expected to be called with the driver-level spinlock
597 * aquired. It will release it before calling any of the handler
598 * functions and re-aquire it when done.
599 */
600void send_iorb(IORBH _far *iorb)
601{
602 /* Mark IORB as "processing" before doing anything else. Once the IORB is
603 * marked as "processing", we can release the spinlock because subsequent
604 * invocations of trigger_engine() (e.g. at interrupt time) will ignore this
605 * IORB.
606 */
607 add_workspace(iorb)->processing = 1;
608 spin_unlock(drv_lock);
609
610 switch (iorb->CommandCode) {
611
612 case IOCC_CONFIGURATION:
613 iocc_configuration(iorb);
614 break;
615
616 case IOCC_DEVICE_CONTROL:
617 iocc_device_control(iorb);
618 break;
619
620 case IOCC_UNIT_CONTROL:
621 iocc_unit_control(iorb);
622 break;
623
624 case IOCC_GEOMETRY:
625 iocc_geometry(iorb);
626 break;
627
628 case IOCC_EXECUTE_IO:
629 iocc_execute_io(iorb);
630 break;
631
632 case IOCC_UNIT_STATUS:
633 iocc_unit_status(iorb);
634 break;
635
636 case IOCC_ADAPTER_PASSTHRU:
637 iocc_adapter_passthru(iorb);
638 break;
639
640 default:
641 /* unsupported call */
642 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
643 iorb_done(iorb);
644 break;
645 }
646
647 /* re-aquire spinlock before returning to trigger_engine() */
648 spin_lock(drv_lock);
649}
650
651/******************************************************************************
652 * Handle IOCC_CONFIGURATION requests.
653 */
654void iocc_configuration(IORBH _far *iorb)
655{
656 int a;
657
658 switch (iorb->CommandModifier) {
659
660 case IOCM_COMPLETE_INIT:
661 /* Complete initialization. From now on, we won't have to restore the BIOS
662 * configuration after each command and we're fully operational (i.e. will
663 * use interrupts, timers and context hooks instead of polling).
664 */
665 if (!init_complete) {
666 dprintf("leaving initialization mode\n");
667 for (a = 0; a < ad_info_cnt; a++) {
668 lock_adapter(ad_infos + a);
669 ahci_complete_init(ad_infos + a);
670 }
671 init_complete = 1;
672
673 /* release all adapters */
674 for (a = 0; a < ad_info_cnt; a++) {
675 unlock_adapter(ad_infos + a);
676 }
677
678 /* register APM hook */
679 apm_init();
680 }
681 iorb_done(iorb);
682 break;
683
684 case IOCM_GET_DEVICE_TABLE:
685 /* construct a device table */
686 iocm_device_table(iorb);
687 break;
688
689 default:
690 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
691 iorb_done(iorb);
692 break;
693 }
694}
695
696/******************************************************************************
697 * Handle IOCC_DEVICE_CONTROL requests.
698 */
699void iocc_device_control(IORBH _far *iorb)
700{
701 AD_INFO *ai = ad_infos + iorb_unit_adapter(iorb);
702 IORBH _far *ptr;
703 IORBH _far *next = NULL;
704 int p = iorb_unit_port(iorb);
705 int d = iorb_unit_device(iorb);
706
707 switch (iorb->CommandModifier) {
708
709 case IOCM_ABORT:
710 /* abort all pending commands on specified port and device */
711 spin_lock(drv_lock);
712 for (ptr = ai->ports[p].iorb_queue.root; ptr != NULL; ptr = next) {
713 next = ptr->pNxtIORB;
714 /* move all matching IORBs to the abort queue */
715 if (ptr != iorb && iorb_unit_device(ptr) == d) {
716 iorb_queue_del(&ai->ports[p].iorb_queue, ptr);
717 iorb_queue_add(&abort_queue, ptr);
718 ptr->ErrorCode = IOERR_CMD_ABORTED;
719 }
720 }
721 spin_unlock(drv_lock);
722
723 /* trigger reset context hook which will finish the abort processing */
724 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
725 break;
726
727 case IOCM_SUSPEND:
728 case IOCM_RESUME:
729 case IOCM_GET_QUEUE_STATUS:
730 /* Suspend/resume operations allow access to the hardware for other
731 * entities such as IBMIDECD.FLT. Since os2ahci implements both ATA
732 * and ATAPI in the same driver, this won't be required.
733 */
734 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
735 break;
736
737 case IOCM_LOCK_MEDIA:
738 case IOCM_UNLOCK_MEDIA:
739 case IOCM_EJECT_MEDIA:
740 /* unit control commands to lock, unlock and eject media */
741 /* will be supported later... */
742 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
743 break;
744
745 default:
746 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
747 break;
748 }
749
750 iorb_done(iorb);
751}
752
753/******************************************************************************
754 * Handle IOCC_UNIT_CONTROL requests.
755 */
756void iocc_unit_control(IORBH _far *iorb)
757{
758 IORB_UNIT_CONTROL _far *iorb_uc = (IORB_UNIT_CONTROL _far *) iorb;
759 int a = iorb_unit_adapter(iorb);
760 int p = iorb_unit_port(iorb);
761 int d = iorb_unit_device(iorb);
762
763 spin_lock(drv_lock);
764 switch (iorb->CommandModifier) {
765
766 case IOCM_ALLOCATE_UNIT:
767 /* allocate unit for exclusive access */
768 if (ad_infos[a].ports[p].devs[d].allocated) {
769 iorb_seterr(iorb, IOERR_UNIT_ALLOCATED);
770 } else {
771 ad_infos[a].ports[p].devs[d].allocated = 1;
772 }
773 break;
774
775 case IOCM_DEALLOCATE_UNIT:
776 /* deallocate exclusive access to unit */
777 if (!ad_infos[a].ports[p].devs[d].allocated) {
778 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
779 } else {
780 ad_infos[a].ports[p].devs[d].allocated = 0;
781 }
782 break;
783
784 case IOCM_CHANGE_UNITINFO:
785 /* Change unit (device) information. One reason for this IOCM is the
786 * interface for filter device drivers: a filter device driver can
787 * either change existing UNITINFOs or permanently allocate units
788 * and fabricate new [logical] units; the former is the reason why we
789 * must store the pointer to the updated UNITNIFO for subsequent
790 * IOCC_CONFIGURATION/IOCM_GET_DEVICE_TABLE calls.
791 */
792 if (!ad_infos[a].ports[p].devs[d].allocated) {
793 iorb_seterr(iorb, IOERR_UNIT_NOT_ALLOCATED);
794 break;
795 }
796 ad_infos[a].ports[p].devs[d].unit_info = iorb_uc->pUnitInfo;
797 break;
798
799 default:
800 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
801 break;
802 }
803
804 spin_unlock(drv_lock);
805 iorb_done(iorb);
806}
807
808/******************************************************************************
809 * Scan all ports for AHCI devices and construct a DASD device table.
810 *
811 * NOTES: This function may be called multiple times. Only the first
812 * invocation will actually scan for devices; all subsequent calls will
813 * merely return the results of the initial scan, potentially augmented
814 * by modified unit infos after IOCC_CONFIGURATION/IOCM_CHANGE_UNITINFO
815 * requests.
816 *
817 * In order to support applications that can't deal with ATAPI devices
818 * (i.e. need a SCSI adapter) os2ahci will optionally report ATAPI
819 * dvices as SCSI devices. The corresponding SCSI adapter doesn't
820 * really exist and is only reported here for the IOCM_GET_DEVICETABLE
821 * request. The units attached to this adapter will use the real HW
822 * unit IDs, thus we'll never receive a command specific to the
823 * emulated SCSI adapter and won't need to set up any sort of entity
824 * for it; the only purpose of the emulated SCSI adapter is to pass the
825 * bus type "AI_DEVBUS_SCSI_2" upstream, and the emulated units, of
826 * course. The emulated SCSI target IDs are allocated as follows:
827 *
828 * 0 the virtual adapter
829 * 1..n emulated devices; SCSI target ID increments sequentially
830 */
831void iocm_device_table(IORBH _far *iorb)
832{
833 IORB_CONFIGURATION _far *iorb_conf;
834 DEVICETABLE _far *dt;
835 char _far *pos;
836 int scsi_units = 0;
837 int scsi_id = 1;
838 int rc;
839 int dta;
840 int a;
841 int p;
842 int d;
843
844 iorb_conf = (IORB_CONFIGURATION _far *) iorb;
845 dt = iorb_conf->pDeviceTable;
846
847 spin_lock(drv_lock);
848
849 /* initialize device table header */
850 dt->ADDLevelMajor = ADD_LEVEL_MAJOR;
851 dt->ADDLevelMinor = ADD_LEVEL_MINOR;
852 dt->ADDHandle = add_handle;
853 dt->TotalAdapters = ad_info_cnt + 1;
854
855 /* set start of adapter and device information tables */
856 pos = (char _far *) (dt->pAdapter + dt->TotalAdapters);
857
858 /* go through all adapters, including the virtual SCSI adapter */
859 for (dta = 0; dta < dt->TotalAdapters; dta++) {
860 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) pos;
861
862 /* sanity check for sufficient space in device table */
863 if ((u32) (ptr + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
864 dprintf("error: device table provided by DASD too small\n");
865 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
866 goto iocm_device_table_done;
867 }
868
869 dt->pAdapter[dta] = (ADAPTERINFO _near *) ((u32) ptr & 0xffff);
870 memset(ptr, 0x00, sizeof(*ptr));
871
872 ptr->AdapterIOAccess = AI_IOACCESS_BUS_MASTER;
873 ptr->AdapterHostBus = AI_HOSTBUS_OTHER | AI_BUSWIDTH_32BIT;
874 ptr->AdapterFlags = AF_16M | AF_HW_SCATGAT;
875 ptr->MaxHWSGList = AHCI_MAX_SG / 2; /* AHCI S/G elements are 22 bits */
876
877 if (dta < ad_info_cnt) {
878 /* this is a physical AHCI adapter */
879 AD_INFO *ad_info = ad_infos + dta;
880
881 ptr->AdapterDevBus = AI_DEVBUS_ST506 | AI_DEVBUS_32BIT;
882 sprintf(ptr->AdapterName, "AHCI_%d", dta);
883
884 if (!ad_info->port_scan_done) {
885 /* first call; need to scan AHCI hardware for devices */
886 if (ad_info->busy) {
887 dprintf("error: port scan requested while adapter was busy\n");
888 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
889 goto iocm_device_table_done;
890 }
891 ad_info->busy = 1;
892 spin_unlock(drv_lock);
893 rc = ahci_scan_ports(ad_info);
894 spin_lock(drv_lock);
895 ad_info->busy = 0;
896
897 if (rc != 0) {
898 dprintf("error: port scan failed on adapter #%d\n", dta);
899 iorb_seterr(iorb, IOERR_CMD_SW_RESOURCE);
900 goto iocm_device_table_done;
901 }
902 ad_info->port_scan_done = 1;
903 }
904
905 /* insert physical (i.e. AHCI) devices into the device table */
906 for (p = 0; p <= ad_info->port_max; p++) {
907 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
908 if (ad_info->ports[p].devs[d].present) {
909 if (ad_info->ports[p].devs[d].atapi && emulate_scsi[dta][p]) {
910 /* only report this unit as SCSI unit */
911 scsi_units++;
912 continue;
913 }
914 if (add_unit_info(iorb_conf, dta, dta, p, d, 0)) {
915 goto iocm_device_table_done;
916 }
917 }
918 }
919 }
920
921 } else {
922 /* this is the virtual SCSI adapter */
923 if (scsi_units == 0) {
924 /* not a single unit to be emulated via SCSI */
925 dt->TotalAdapters--;
926 break;
927 }
928
929 /* set adapter name and bus type to mimic a SCSI controller */
930 ptr->AdapterDevBus = AI_DEVBUS_SCSI_2 | AI_DEVBUS_16BIT;
931 sprintf(ptr->AdapterName, "AHCI_SCSI_0");
932
933 /* add all ATAPI units to be emulated by this virtual adaper */
934 for (a = 0; a < ad_info_cnt; a++) {
935 AD_INFO *ad_info = ad_infos + a;
936
937 for (p = 0; p <= ad_info->port_max; p++) {
938 for (d = 0; d <= ad_info->ports[p].dev_max; d++) {
939 if (ad_info->ports[p].devs[d].present &&
940 ad_info->ports[p].devs[d].atapi &&
941 emulate_scsi[a][p]) {
942 if (add_unit_info(iorb_conf, dta, a, p, d, scsi_id++)) {
943 goto iocm_device_table_done;
944 }
945 }
946 }
947 }
948 }
949 }
950
951 /* calculate offset for next adapter */
952 pos = (char _far *) (ptr->UnitInfo + ptr->AdapterUnits);
953 }
954
955iocm_device_table_done:
956 spin_unlock(drv_lock);
957 iorb_done(iorb);
958}
959
960/******************************************************************************
961 * Handle IOCC_GEOMETRY requests.
962 */
963void iocc_geometry(IORBH _far *iorb)
964{
965 switch (iorb->CommandModifier) {
966
967 case IOCM_GET_MEDIA_GEOMETRY:
968 case IOCM_GET_DEVICE_GEOMETRY:
969 add_workspace(iorb)->idempotent = 1;
970 ahci_get_geometry(iorb);
971 break;
972
973 default:
974 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
975 iorb_done(iorb);
976 }
977}
978
979/******************************************************************************
980 * Handle IOCC_EXECUTE_IO requests.
981 */
982void iocc_execute_io(IORBH _far *iorb)
983{
984 switch (iorb->CommandModifier) {
985
986 case IOCM_READ:
987 add_workspace(iorb)->idempotent = 1;
988 ahci_read(iorb);
989 break;
990
991 case IOCM_READ_VERIFY:
992 add_workspace(iorb)->idempotent = 1;
993 ahci_verify(iorb);
994 break;
995
996 case IOCM_WRITE:
997 add_workspace(iorb)->idempotent = 1;
998 ahci_write(iorb);
999 break;
1000
1001 case IOCM_WRITE_VERIFY:
1002 add_workspace(iorb)->idempotent = 1;
1003 ahci_write(iorb);
1004 break;
1005
1006 default:
1007 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1008 iorb_done(iorb);
1009 }
1010}
1011
1012/******************************************************************************
1013 * Handle IOCC_UNIT_STATUS requests.
1014 */
1015void iocc_unit_status(IORBH _far *iorb)
1016{
1017 switch (iorb->CommandModifier) {
1018
1019 case IOCM_GET_UNIT_STATUS:
1020 add_workspace(iorb)->idempotent = 1;
1021 ahci_unit_ready(iorb);
1022 break;
1023
1024 default:
1025 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1026 iorb_done(iorb);
1027 }
1028}
1029
1030/******************************************************************************
1031 * Handle IOCC_ADAPTER_PASSTHROUGH requests.
1032 */
1033void iocc_adapter_passthru(IORBH _far *iorb)
1034{
1035 switch (iorb->CommandModifier) {
1036
1037 case IOCM_EXECUTE_CDB:
1038 add_workspace(iorb)->idempotent = 0;
1039 ahci_execute_cdb(iorb);
1040 break;
1041
1042 case IOCM_EXECUTE_ATA:
1043 add_workspace(iorb)->idempotent = 0;
1044 ahci_execute_ata(iorb);
1045 break;
1046
1047 default:
1048 iorb_seterr(iorb, IOERR_CMD_NOT_SUPPORTED);
1049 iorb_done(iorb);
1050 }
1051}
1052
1053/******************************************************************************
1054 * Add an IORB to the specified queue. This function must be called with the
1055 * adapter-level spinlock aquired.
1056 */
1057void iorb_queue_add(IORB_QUEUE _far *queue, IORBH _far *iorb)
1058{
1059 if (iorb_priority(iorb) {
1060 /* priority IORB; insert at first position */
1061 iorb->pNxtIORB = queue->root;
1062 queue->root = iorb;
1063
1064 } else {
1065 /* append IORB to end of queue */
1066 iorb->pNxtIORB = NULL;
1067
1068 if (queue->root == NULL) {
1069 queue->root = iorb;
1070 } else {
1071 queue->tail->pNxtIORB = iorb;
1072 }
1073 queue->tail = iorb;
1074 }
1075
1076 if (debug) {
1077 /* determine queue type (local, driver, abort or port) and minimum debug
1078 * level; otherwise, queue debug prints can become really confusing.
1079 */
1080 char *queue_type;
1081 int min_debug = 1;
1082
1083 if ((u32) queue >> 16 == (u32) (void _far *) &queue >> 16) {
1084 /* this queue is on the stack */
1085 queue_type = "local";
1086 min_debug = 2;
1087
1088 } else if (queue == &driver_queue) {
1089 queue_type = "driver";
1090
1091 } else if (queue == &abort_queue) {
1092 queue_type = "abort";
1093 min_debug = 2;
1094
1095 } else {
1096 queue_type = "port";
1097 }
1098
1099 if (debug >= min_debug) {
1100 printf("IORB %Fp queued (cmd = %d/%d, queue = %Fp [%s], timeout = %ld)\n",
1101 iorb, iorb->CommandCode, iorb->CommandModifier, queue, queue_type,
1102 iorb->Timeout);
1103 }
1104 }
1105}
1106
1107/******************************************************************************
1108 * Remove an IORB from the specified queue. This function must be called with
1109 * the adapter-level spinlock aquired.
1110 */
1111int iorb_queue_del(IORB_QUEUE _far *queue, IORBH _far *iorb)
1112{
1113 IORBH _far *_iorb;
1114 IORBH _far *_prev = NULL;
1115 int found = 0;
1116
1117 for (_iorb = queue->root; _iorb != NULL; _iorb = _iorb->pNxtIORB) {
1118 if (_iorb == iorb) {
1119 /* found the IORB to be removed */
1120 if (_prev != NULL) {
1121 _prev->pNxtIORB = _iorb->pNxtIORB;
1122 } else {
1123 queue->root = _iorb->pNxtIORB;
1124 }
1125 if (_iorb == queue->tail) {
1126 queue->tail = _prev;
1127 }
1128 found = 1;
1129 break;
1130 }
1131 _prev = _iorb;
1132 }
1133
1134 if (found) {
1135 ddprintf("IORB %Fp removed (queue = %Fp)\n", iorb, queue);
1136 } else {
1137 dprintf("IORB %Fp not found in queue %Fp\n", iorb, queue);
1138 }
1139
1140 return(!found);
1141}
1142
1143/******************************************************************************
1144 * Set the error code in the specified IORB
1145 *
1146 * NOTE: This function does *not* call iorb_done(). It merely sets the IORB
1147 * status to the specified error code.
1148 */
1149void iorb_seterr(IORBH _far *iorb, USHORT error_code)
1150{
1151 iorb->ErrorCode = error_code;
1152 iorb->Status |= IORB_ERROR;
1153}
1154
1155/******************************************************************************
1156 * Mark the specified IORB as done and notify the asynchronous post function,
1157 * if any. The IORB is also removed from the corresponding IORB queue.
1158 *
1159 * NOTES: This function does not clear the Status field; it merely adds the
1160 * IORB_DONE flag.
1161 *
1162 * This function is expected to be called *without* the corresponding
1163 * driver-level drv_lock aquired. It will aquire the spinlock before
1164 * updating the IORB queue and release it before notifying the upstream
1165 * code in order to prevent deadlocks.
1166 *
1167 * Due to this logic, this function is only good for simple task-time
1168 * completions. Functions working on lists of IORBs (such as interrupt
1169 * handlers or context hooks) should call iorb_complete() directly and
1170 * implement their own logic for removing the IORB from the port queue.
1171 * See abort_ctxhook() for an example.
1172 */
1173void iorb_done(IORBH _far *iorb)
1174{
1175 int a = iorb_unit_adapter(iorb);
1176 int p = iorb_unit_port(iorb);
1177
1178 /* remove IORB from corresponding queue */
1179 spin_lock(drv_lock);
1180 if (iorb_driver_level(iorb)) {
1181 iorb_queue_del(&driver_queue, iorb);
1182 } else {
1183 iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb);
1184 }
1185 aws_free(add_workspace(iorb));
1186 spin_unlock(drv_lock);
1187
1188 iorb_complete(iorb);
1189}
1190
1191/******************************************************************************
1192 * Complete an IORB. This should be called without the adapter-level spinlock
1193 * to allow the IORB completion routine to perform whatever processing it
1194 * requires. This implies that the IORB should no longer be in any global
1195 * queue because the IORB completion routine may well reuse the IORB and send
1196 * the next request to us before even returning from this function.
1197 */
1198void iorb_complete(IORBH _far *iorb)
1199{
1200 iorb->Status |= IORB_DONE;
1201
1202 dprintf("IORB %Fp complete (status = 0x%04x, error = 0x%04x)\n",
1203 iorb, iorb->Status, iorb->ErrorCode);
1204
1205 if (iorb->RequestControl & IORB_ASYNC_POST) {
1206 iorb->NotifyAddress(iorb);
1207 }
1208}
1209
1210/******************************************************************************
1211 * Requeue the specified IORB such that it will be sent downstream for
1212 * processing again. This includes freeing all resources currently allocated
1213 * (timer, buffer, ...) and resetting the flags to 0. The driver-level
1214 * spinlock must be aquired when calling this function.
1215 *
1216 * The following flags are preserved:
1217 * - no_ncq
1218 */
1219void iorb_requeue(IORBH _far *iorb)
1220{
1221 ADD_WORKSPACE _far *aws = add_workspace(iorb);
1222 u16 no_ncq = aws->no_ncq;
1223
1224 aws_free(aws);
1225 memset(aws, 0x00, sizeof(*aws));
1226 aws->no_ncq = no_ncq;
1227}
1228
1229/******************************************************************************
1230 * Free resources in ADD workspace (timer, buffer, ...). This function should
1231 * be called with the spinlock held to prevent race conditions.
1232 */
1233void aws_free(ADD_WORKSPACE _far *aws)
1234{
1235 if (aws->timer != 0) {
1236 ADD_CancelTimer(aws->timer);
1237 aws->timer = 0;
1238 }
1239
1240 if (aws->buf != NULL) {
1241 free(aws->buf);
1242 aws->buf = NULL;
1243 }
1244}
1245
1246/******************************************************************************
1247 * Lock the adapter, waiting for availability if necessary. This is expected
1248 * to be called at task/request time without the driver-level spinlock
1249 * aquired. Don't call at interrupt time.
1250 */
1251void lock_adapter(AD_INFO *ai)
1252{
1253 spin_lock(drv_lock);
1254 while (ai->busy) {
1255 spin_unlock(drv_lock);
1256 msleep(250);
1257 spin_lock(drv_lock);
1258 }
1259 ai->busy = 1;
1260 spin_unlock(drv_lock);
1261}
1262
1263/******************************************************************************
1264 * Unlock adapter (i.e. reset busy flag)
1265 */
1266void unlock_adapter(AD_INFO *ai)
1267{
1268 ai->busy = 0;
1269}
1270
1271/******************************************************************************
1272 * Timeout handler for I/O commands. Since timeout handling can involve
1273 * lengthy operations like port resets, the main code is located in a
1274 * separate function which is invoked via a context hook.
1275 */
1276void _cdecl _far timeout_callback(ULONG timer_handle, ULONG p1,
1277 ULONG p2)
1278{
1279 IORBH _far *iorb = (IORBH _far *) p1;
1280 int a = iorb_unit_adapter(iorb);
1281 int p = iorb_unit_port(iorb);
1282
1283 ADD_CancelTimer(timer_handle);
1284 dprintf("timeout for IORB %Fp\n", iorb);
1285
1286 /* Move the timed-out IORB to the abort queue. Since it's possible that the
1287 * IORB has completed after the timeout has expired but before we got to
1288 * this line of code, we'll check the return code of iorb_queue_del(): If it
1289 * returns an error, the IORB must have completed a few microseconds ago and
1290 * there is no timeout.
1291 */
1292 spin_lock(drv_lock);
1293 if (iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, iorb) == 0) {
1294 iorb_queue_add(&abort_queue, iorb);
1295 iorb->ErrorCode = IOERR_ADAPTER_TIMEOUT;
1296 }
1297 spin_unlock(drv_lock);
1298
1299 /* Trigger abort processing function. We don't really care whether this
1300 * succeeds because the only reason why it would fail should be multiple
1301 * calls to DevHelp_ArmCtxHook() before the context hook had a chance to
1302 * start executing, which leaves two scenarios:
1303 *
1304 * - We succeded in arming the context hook. Fine.
1305 *
1306 * - We armed the context hook a second time before it had a chance to
1307 * start executing. In this case, the already scheduled context hook
1308 * will process our IORB as well.
1309 */
1310 DevHelp_ArmCtxHook(0, reset_ctxhook_h);
1311
1312 /* Set up a watchdog timer which calls the context hook manually in case
1313 * some kernel thread is looping around the IORB_COMPLETE status bit
1314 * without yielding the CPU (kernel threads don't preempt). This shouldn't
1315 * happen per design because kernel threads are supposed to yield but it
1316 * does in the early boot phase.
1317 */
1318 ADD_StartTimerMS(&th_reset_watchdog, 5000, (PFN) reset_watchdog, 0, 0);
1319}
1320
1321/******************************************************************************
1322 * Reset handler watchdog. If a timeout occurs, a context hook is armed which
1323 * will execute as soon as a kernel thread yields the CPU. However, some
1324 * kernel components won't yield the CPU during the early boot phase and the
1325 * only way to kick some sense into those components is to run the context
1326 * hook right inside this timer callback. Not exactly pretty, especially
1327 * considering the fact that context hooks were implemented to prevent running
1328 * lengthy operations like a port reset at interrupt time, but without this
1329 * watchdog mechanism we run the risk of getting completely stalled by device
1330 * problems during the early boot phase.
1331 */
1332void _cdecl _far reset_watchdog(ULONG timer_handle, ULONG p1,
1333 ULONG p2)
1334{
1335 /* reset watchdog timer */
1336 ADD_CancelTimer(timer_handle);
1337 dprintf("reset watchdog invoked\n");
1338
1339 /* call context hook manually */
1340 reset_ctxhook(0);
1341}
1342
1343/******************************************************************************
1344 * small_code_ - this dummy func resolves the undefined reference linker
1345 * error that occurrs when linking WATCOM objects with DDK's link.exe
1346 */
1347void _cdecl small_code_(void)
1348{
1349}
1350
1351/******************************************************************************
1352 * Add unit info to ADAPTERINFO array (IOCC_GET_DEVICE_TABLE requests). The
1353 * adapter info array in the device table, dt->pAdapter[], is expected to be
1354 * initialized for the specified index (dt_ai).
1355 *
1356 * Please note that the device table adapter index, dta, is not always equal
1357 * to the physical adapter index, a: if SCSI emulation has been activated, the
1358 * last reported adapter is a virtual SCSI adapter and the physical adapter
1359 * indexes for those units are, of course, different from the device table
1360 * index of the virtual SCSI adapter.
1361 */
1362static int add_unit_info(IORB_CONFIGURATION _far *iorb_conf, int dta,
1363 int a, int p, int d, int scsi_id)
1364{
1365 DEVICETABLE _far *dt = iorb_conf->pDeviceTable;
1366 ADAPTERINFO _far *ptr = (ADAPTERINFO _far *) (((u32) dt & 0xffff0000U) +
1367 (u16) dt->pAdapter[dta]);
1368 UNITINFO _far *ui = ptr->UnitInfo + ptr->AdapterUnits;
1369 AD_INFO *ai = ad_infos + a;
1370
1371 if ((u32) (ui + 1) - (u32) dt > iorb_conf->DeviceTableLen) {
1372 dprintf("error: device table provided by DASD too small\n");
1373 iorb_seterr(&iorb_conf->iorbh, IOERR_CMD_SW_RESOURCE);
1374 return(-1);
1375 }
1376
1377 if (ai->ports[p].devs[d].unit_info == NULL) {
1378 /* provide original information about this device (unit) */
1379 memset(ui, 0x00, sizeof(*ui));
1380 ui->AdapterIndex = dta; /* device table adapter index */
1381 ui->UnitHandle = iorb_unit(a, p, d); /* physical adapter index */
1382 ui->UnitIndex = ptr->AdapterUnits;
1383 ui->UnitType = ai->ports[p].devs[d].dev_type;
1384 ui->QueuingCount = ai->ports[p].devs[d].ncq_max;;
1385 if (ai->ports[p].devs[d].removable) {
1386 ui->UnitFlags |= UF_REMOVABLE;
1387 }
1388 if (scsi_id > 0) {
1389 /* set fake SCSI ID for this unit */
1390 ui->UnitSCSITargetID = scsi_id;
1391 }
1392 } else {
1393 /* copy updated device (unit) information (IOCM_CHANGE_UNITINFO) */
1394 memcpy(ui, ai->ports[p].devs[d].unit_info, sizeof(*ui));
1395 }
1396
1397 ptr->AdapterUnits++;
1398 return(0);
1399}
1400
Note: See TracBrowser for help on using the repository browser.