| 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 | * Copyright (c) 2013-2016 David Azarewicz
|
|---|
| 7 | *
|
|---|
| 8 | * Authors: Christian Mueller, Markus Thielen
|
|---|
| 9 | *
|
|---|
| 10 | * Parts copied from/inspired by the Linux AHCI driver;
|
|---|
| 11 | * those parts are (c) Linux AHCI/ATA maintainers
|
|---|
| 12 | *
|
|---|
| 13 | * This program is free software; you can redistribute it and/or modify
|
|---|
| 14 | * it under the terms of the GNU General Public License as published by
|
|---|
| 15 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 16 | * (at your option) any later version.
|
|---|
| 17 | *
|
|---|
| 18 | * This program is distributed in the hope that it will be useful,
|
|---|
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 21 | * GNU General Public License for more details.
|
|---|
| 22 | *
|
|---|
| 23 | * You should have received a copy of the GNU General Public License
|
|---|
| 24 | * along with this program; if not, write to the Free Software
|
|---|
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | #include "os2ahci.h"
|
|---|
| 29 | #include "ioctl.h"
|
|---|
| 30 | #include "version.h"
|
|---|
| 31 | #include "devhdr.h"
|
|---|
| 32 |
|
|---|
| 33 | /* -------------------------- macros and constants ------------------------- */
|
|---|
| 34 |
|
|---|
| 35 | /* set two-dimensional array of port options */
|
|---|
| 36 | #define set_port_option(opt, val) \
|
|---|
| 37 | if (adapter_index == -1) { \
|
|---|
| 38 | /* set option for all adapters and ports */ \
|
|---|
| 39 | memset(opt, val, sizeof(opt)); \
|
|---|
| 40 | } else if (port_index == -1) { \
|
|---|
| 41 | /* set option for all ports on current adapter */ \
|
|---|
| 42 | memset(opt[adapter_index], val, sizeof(*opt)); \
|
|---|
| 43 | } else { \
|
|---|
| 44 | /* set option for specific port */ \
|
|---|
| 45 | opt[adapter_index][port_index] = val; \
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | #define FLAG_KRNL_EXIT_ADD 0x1000
|
|---|
| 49 | #define FLAG_KRNL_EXIT_REMOVE 0x2000
|
|---|
| 50 |
|
|---|
| 51 | #define TYPE_KRNL_EXIT_NMI 0x0000 /* non masked interrupts */
|
|---|
| 52 | #define TYPE_KRNL_EXIT_SFF 0x0001 /* system fatal faults */
|
|---|
| 53 | #define TYPE_KRNL_EXIT_PROCDUMP 0x0002
|
|---|
| 54 | #define TYPE_KRNL_EXIT_DYN 0x0003
|
|---|
| 55 | #define TYPE_KRNL_EXIT_INT13 0x0004 /* enable int13 IO */
|
|---|
| 56 |
|
|---|
| 57 | /* ------------------------ typedefs and structures ------------------------ */
|
|---|
| 58 |
|
|---|
| 59 | /* -------------------------- function prototypes -------------------------- */
|
|---|
| 60 |
|
|---|
| 61 | extern int SetPsdPutc(void);
|
|---|
| 62 | static int add_unit_info(IORB_CONFIGURATION *iorb_conf, int dt_ai, int a, int p, int d, int scsi_id);
|
|---|
| 63 |
|
|---|
| 64 | /* ------------------------ global/static variables ------------------------ */
|
|---|
| 65 | int thorough_scan = 1; /* if != 0, perform thorough PCI scan */
|
|---|
| 66 | int init_reset = 1; /* if != 0, reset ports during init */
|
|---|
| 67 | int force_write_cache; /* if != 0, force write cache */
|
|---|
| 68 | int verbosity = 0; /* default is quiet. 1=show sign on banner, >1=show adapter info during boot */
|
|---|
| 69 | int use_lvm_info = 1;
|
|---|
| 70 | long com_baud = 0;
|
|---|
| 71 |
|
|---|
| 72 | HDRIVER rm_drvh; /* resource manager driver handle */
|
|---|
| 73 | USHORT add_handle; /* driver handle (RegisterDeviceClass) */
|
|---|
| 74 | char drv_name[] = "OS2AHCI"; /* driver name as string */
|
|---|
| 75 |
|
|---|
| 76 | /* resource manager driver information structure */
|
|---|
| 77 | static DRIVERSTRUCT rm_drvinfo =
|
|---|
| 78 | {
|
|---|
| 79 | NULL, /* We cannot do Flat to Far16 conversion at compile time */
|
|---|
| 80 | NULL, /* so we put NULLs in all the Far16 fields and then fill */
|
|---|
| 81 | NULL, /* them in at run time */
|
|---|
| 82 | DMAJOR,
|
|---|
| 83 | DMINOR,
|
|---|
| 84 | BLD_YEAR, BLD_MONTH, BLD_DAY,
|
|---|
| 85 | 0,
|
|---|
| 86 | DRT_ADDDM,
|
|---|
| 87 | DRS_ADD,
|
|---|
| 88 | NULL
|
|---|
| 89 | };
|
|---|
| 90 |
|
|---|
| 91 | SpinLock_t drv_lock; /* driver-level spinlock */
|
|---|
| 92 | IORB_QUEUE driver_queue; /* driver-level IORB queue */
|
|---|
| 93 | AD_INFO ad_infos[MAX_AD]; /* adapter information list */
|
|---|
| 94 | int ad_info_cnt; /* number of entries in ad_infos[] */
|
|---|
| 95 | u16 ad_ignore; /* bitmap with adapter indexes to ignore */
|
|---|
| 96 | int init_complete; /* if != 0, initialization has completed */
|
|---|
| 97 | int suspended;
|
|---|
| 98 | int resume_sleep_flag;
|
|---|
| 99 |
|
|---|
| 100 | /* apapter/port-specific options saved when parsing the command line */
|
|---|
| 101 | u8 emulate_scsi[MAX_AD][AHCI_MAX_PORTS];
|
|---|
| 102 | u8 enable_ncq[MAX_AD][AHCI_MAX_PORTS];
|
|---|
| 103 | u8 link_speed[MAX_AD][AHCI_MAX_PORTS];
|
|---|
| 104 | u8 link_power[MAX_AD][AHCI_MAX_PORTS];
|
|---|
| 105 | u8 track_size[MAX_AD][AHCI_MAX_PORTS];
|
|---|
| 106 | u8 port_ignore[MAX_AD][AHCI_MAX_PORTS];
|
|---|
| 107 |
|
|---|
| 108 | char BldLevel[] = BLDLEVEL;
|
|---|
| 109 |
|
|---|
| 110 | /* ----------------------------- start of code ----------------------------- */
|
|---|
| 111 |
|
|---|
| 112 | /******************************************************************************
|
|---|
| 113 | * OS/2 device driver main strategy function.
|
|---|
| 114 | *
|
|---|
| 115 | * NOTE: this is also used as the IDC entry point. We expect an IOCTL request
|
|---|
| 116 | * packet for IDC calls, so they can be handled by gen_ioctl.
|
|---|
| 117 | */
|
|---|
| 118 | void StrategyHandler(REQPACKET *prp)
|
|---|
| 119 | {
|
|---|
| 120 | u16 rc;
|
|---|
| 121 |
|
|---|
| 122 | switch (prp->bCommand)
|
|---|
| 123 | {
|
|---|
| 124 | case STRATEGY_BASEDEVINIT:
|
|---|
| 125 | rc = init_drv(prp);
|
|---|
| 126 | break;
|
|---|
| 127 |
|
|---|
| 128 | case STRATEGY_SHUTDOWN:
|
|---|
| 129 | rc = exit_drv(prp->save_restore.Function);
|
|---|
| 130 | break;
|
|---|
| 131 |
|
|---|
| 132 | case STRATEGY_GENIOCTL:
|
|---|
| 133 | rc = gen_ioctl(prp);
|
|---|
| 134 | break;
|
|---|
| 135 |
|
|---|
| 136 | case STRATEGY_OPEN:
|
|---|
| 137 | build_user_info();
|
|---|
| 138 | rc = RPDONE;
|
|---|
| 139 | break;
|
|---|
| 140 |
|
|---|
| 141 | case STRATEGY_READ:
|
|---|
| 142 | rc = char_dev_input(prp);
|
|---|
| 143 | break;
|
|---|
| 144 |
|
|---|
| 145 | case STRATEGY_SAVERESTORE:
|
|---|
| 146 | rc = sr_drv(prp->save_restore.Function);
|
|---|
| 147 | break;
|
|---|
| 148 |
|
|---|
| 149 | case STRATEGY_INITCOMPLETE:
|
|---|
| 150 | case STRATEGY_CLOSE:
|
|---|
| 151 | case STRATEGY_INPUTSTATUS:
|
|---|
| 152 | case STRATEGY_FLUSHINPUT:
|
|---|
| 153 | /* noop */
|
|---|
| 154 | rc = RPDONE;
|
|---|
| 155 | break;
|
|---|
| 156 |
|
|---|
| 157 | default:
|
|---|
| 158 | rc = RPDONE | RPERR_BADCOMMAND;
|
|---|
| 159 | break;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | prp->usStatus = rc;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | void IdcHandler(REQPACKET *prp)
|
|---|
| 166 | {
|
|---|
| 167 | StrategyHandler(prp);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | /******************************************************************************
|
|---|
| 171 | * Intialize the os2ahci driver. This includes command line parsing, scanning
|
|---|
| 172 | * the PCI bus for supported AHCI adapters, etc.
|
|---|
| 173 | */
|
|---|
| 174 | USHORT init_drv(REQPACKET *req)
|
|---|
| 175 | {
|
|---|
| 176 | static int init_drv_called;
|
|---|
| 177 | static int init_drv_failed;
|
|---|
| 178 | APIRET rmrc;
|
|---|
| 179 | const char *pszCmdLine, *cmd_line;
|
|---|
| 180 | int adapter_index = -1;
|
|---|
| 181 | int port_index = -1;
|
|---|
| 182 | int iInvertOption;
|
|---|
| 183 | int iStatus;
|
|---|
| 184 |
|
|---|
| 185 | if (init_drv_called)
|
|---|
| 186 | {
|
|---|
| 187 | /* This is the init call for the second (IBMS506$) character
|
|---|
| 188 | * device driver. If the main driver failed initialization, fail this
|
|---|
| 189 | * one as well.
|
|---|
| 190 | */
|
|---|
| 191 | return(RPDONE | ((init_drv_failed) ? RPERR_INITFAIL : 0));
|
|---|
| 192 | }
|
|---|
| 193 | D32g_DbgLevel = 0;
|
|---|
| 194 | init_drv_called = 1;
|
|---|
| 195 | suspended = 0;
|
|---|
| 196 | resume_sleep_flag = 0;
|
|---|
| 197 | memset(ad_infos, 0, sizeof(ad_infos));
|
|---|
| 198 | memset(emulate_scsi, 1, sizeof(emulate_scsi)); /* set default enabled */
|
|---|
| 199 | UtSetDriverName("OS2AHCI$");
|
|---|
| 200 | Header.ulCaps |= DEV_ADAPTER_DD; /* DAZ This flag is not really needed. */
|
|---|
| 201 |
|
|---|
| 202 | /* create driver-level spinlock */
|
|---|
| 203 | KernAllocSpinLock(&drv_lock);
|
|---|
| 204 |
|
|---|
| 205 | /* register driver with resource manager */
|
|---|
| 206 | rm_drvinfo.DrvrName = drv_name;
|
|---|
| 207 | rm_drvinfo.DrvrDescript = "AHCI SATA Driver";
|
|---|
| 208 | rm_drvinfo.VendorName = DVENDOR;
|
|---|
| 209 | if ((rmrc = RMCreateDriver(&rm_drvinfo, &rm_drvh)) != RMRC_SUCCESS)
|
|---|
| 210 | {
|
|---|
| 211 | iprintf("%s: failed to register driver with resource manager (rc = %d)", drv_name, rmrc);
|
|---|
| 212 | goto init_fail;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | pszCmdLine = cmd_line = req->init_in.szArgs;
|
|---|
| 216 | iStatus = 0;
|
|---|
| 217 | while (*pszCmdLine)
|
|---|
| 218 | {
|
|---|
| 219 | if (*pszCmdLine++ != '/') continue; /* Ignore anything that doesn't start with '/' */
|
|---|
| 220 | /* pszCmdLine now points to first char of argument */
|
|---|
| 221 |
|
|---|
| 222 | if ((iInvertOption = (*pszCmdLine == '!')) != 0) pszCmdLine++;
|
|---|
| 223 |
|
|---|
| 224 | if (ArgCmp(pszCmdLine, "B:"))
|
|---|
| 225 | {
|
|---|
| 226 | pszCmdLine += 2;
|
|---|
| 227 | com_baud = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 228 | continue;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | if (ArgCmp(pszCmdLine, "C:"))
|
|---|
| 232 | {
|
|---|
| 233 | pszCmdLine += 2;
|
|---|
| 234 | /* set COM port base address for debug messages */
|
|---|
| 235 | D32g_ComBase = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 236 | if (D32g_ComBase == 1) D32g_ComBase = 0x3f8;
|
|---|
| 237 | if (D32g_ComBase == 2) D32g_ComBase = 0x2f8;
|
|---|
| 238 | continue;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | if (ArgCmp(pszCmdLine, "D"))
|
|---|
| 242 | {
|
|---|
| 243 | pszCmdLine++;
|
|---|
| 244 | if (*pszCmdLine == ':')
|
|---|
| 245 | {
|
|---|
| 246 | pszCmdLine++;
|
|---|
| 247 | D32g_DbgLevel = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 248 | }
|
|---|
| 249 | else D32g_DbgLevel++; /* increase debug level */
|
|---|
| 250 | continue;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | if (ArgCmp(pszCmdLine, "G:"))
|
|---|
| 254 | {
|
|---|
| 255 | u16 usVendor;
|
|---|
| 256 | u16 usDevice;
|
|---|
| 257 |
|
|---|
| 258 | pszCmdLine += 2;
|
|---|
| 259 | /* add specfied PCI ID as a supported generic AHCI adapter */
|
|---|
| 260 | usVendor = strtol(pszCmdLine, &pszCmdLine, 16);
|
|---|
| 261 | if (*pszCmdLine != ':') break;
|
|---|
| 262 | pszCmdLine++;
|
|---|
| 263 | usDevice = strtol(pszCmdLine, &pszCmdLine, 16);
|
|---|
| 264 | if (add_pci_id(usVendor, usDevice))
|
|---|
| 265 | {
|
|---|
| 266 | iprintf("%s: failed to add PCI ID %04x:%04x", drv_name, usVendor, usDevice);
|
|---|
| 267 | iStatus = 1;
|
|---|
| 268 | }
|
|---|
| 269 | thorough_scan = 1;
|
|---|
| 270 | continue;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | if (ArgCmp(pszCmdLine, "T"))
|
|---|
| 274 | {
|
|---|
| 275 | pszCmdLine++;
|
|---|
| 276 | /* perform thorough PCI scan (i.e. look for individual supported PCI IDs) */
|
|---|
| 277 | thorough_scan = !iInvertOption;
|
|---|
| 278 | continue;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | if (ArgCmp(pszCmdLine, "R"))
|
|---|
| 282 | {
|
|---|
| 283 | pszCmdLine++;
|
|---|
| 284 | /* reset ports during initialization */
|
|---|
| 285 | init_reset = !iInvertOption;
|
|---|
| 286 | continue;
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | if (ArgCmp(pszCmdLine, "F"))
|
|---|
| 290 | {
|
|---|
| 291 | pszCmdLine++;
|
|---|
| 292 | /* force write cache regardless of IORB flags */
|
|---|
| 293 | force_write_cache = 1;
|
|---|
| 294 | continue;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | if (ArgCmp(pszCmdLine, "A:"))
|
|---|
| 298 | {
|
|---|
| 299 | pszCmdLine += 2;
|
|---|
| 300 | /* set adapter index for adapter and port-related options */
|
|---|
| 301 | adapter_index = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 302 | if (adapter_index < 0 || adapter_index >= MAX_AD)
|
|---|
| 303 | {
|
|---|
| 304 | iprintf("%s: invalid adapter index (%d)", drv_name, adapter_index);
|
|---|
| 305 | iStatus = 1;
|
|---|
| 306 | }
|
|---|
| 307 | continue;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | if (ArgCmp(pszCmdLine, "P:"))
|
|---|
| 311 | {
|
|---|
| 312 | pszCmdLine += 2;
|
|---|
| 313 | /* set port index for port-related options */
|
|---|
| 314 | port_index = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 315 | if (port_index < 0 || port_index >= AHCI_MAX_PORTS)
|
|---|
| 316 | {
|
|---|
| 317 | iprintf("%s: invalid port index (%d)", drv_name, port_index);
|
|---|
| 318 | iStatus = 1;
|
|---|
| 319 | }
|
|---|
| 320 | continue;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | if (ArgCmp(pszCmdLine, "I"))
|
|---|
| 324 | {
|
|---|
| 325 | pszCmdLine++;
|
|---|
| 326 | /* ignore current adapter index */
|
|---|
| 327 | if (adapter_index >= 0)
|
|---|
| 328 | {
|
|---|
| 329 | if (port_index >= 0) port_ignore[adapter_index][port_index] = !iInvertOption;
|
|---|
| 330 | else ad_ignore |= 1U << adapter_index;
|
|---|
| 331 | }
|
|---|
| 332 | continue;
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | if (ArgCmp(pszCmdLine, "S"))
|
|---|
| 336 | {
|
|---|
| 337 | pszCmdLine++;
|
|---|
| 338 | /* enable SCSI emulation for ATAPI devices */
|
|---|
| 339 | set_port_option(emulate_scsi, !iInvertOption);
|
|---|
| 340 | continue;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | if (ArgCmp(pszCmdLine, "N"))
|
|---|
| 344 | {
|
|---|
| 345 | pszCmdLine++;
|
|---|
| 346 | /* enable NCQ */
|
|---|
| 347 | set_port_option(enable_ncq, !iInvertOption);
|
|---|
| 348 | continue;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | if (ArgCmp(pszCmdLine, "LS:"))
|
|---|
| 352 | {
|
|---|
| 353 | int optval;
|
|---|
| 354 |
|
|---|
| 355 | pszCmdLine += 3;
|
|---|
| 356 | /* set link speed */
|
|---|
| 357 | optval = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 358 | set_port_option(link_speed, optval);
|
|---|
| 359 | /* need to reset the port in order to establish link settings */
|
|---|
| 360 | init_reset = 1;
|
|---|
| 361 | continue;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | if (ArgCmp(pszCmdLine, "LP:"))
|
|---|
| 365 | {
|
|---|
| 366 | int optval;
|
|---|
| 367 |
|
|---|
| 368 | pszCmdLine += 3;
|
|---|
| 369 | /* set power management */
|
|---|
| 370 | optval = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 371 | set_port_option(link_power, optval);
|
|---|
| 372 | /* need to reset the port in order to establish link settings */
|
|---|
| 373 | init_reset = 1;
|
|---|
| 374 | continue;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | if (ArgCmp(pszCmdLine, "4"))
|
|---|
| 378 | {
|
|---|
| 379 | pszCmdLine++;
|
|---|
| 380 | /* enable 4K sector geometry enhancement (track size = 56) */
|
|---|
| 381 | if (!iInvertOption) set_port_option(track_size, 56);
|
|---|
| 382 | continue;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | if (ArgCmp(pszCmdLine, "Z"))
|
|---|
| 386 | {
|
|---|
| 387 | pszCmdLine++;
|
|---|
| 388 | /* Specify to not use the LVM information. There is no reason why anyone would
|
|---|
| 389 | * want to do this, but previous versions of this driver did not have LVM capability,
|
|---|
| 390 | * so this switch is here temporarily just in case.
|
|---|
| 391 | */
|
|---|
| 392 | use_lvm_info = !iInvertOption;
|
|---|
| 393 | continue;
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | if (ArgCmp(pszCmdLine, "V"))
|
|---|
| 397 | {
|
|---|
| 398 | pszCmdLine++;
|
|---|
| 399 | if (*pszCmdLine == ':')
|
|---|
| 400 | {
|
|---|
| 401 | pszCmdLine++;
|
|---|
| 402 | verbosity = strtol(pszCmdLine, &pszCmdLine, 0);
|
|---|
| 403 | }
|
|---|
| 404 | else verbosity++; /* increase verbosity level */
|
|---|
| 405 | continue;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | if (ArgCmp(pszCmdLine, "W"))
|
|---|
| 409 | {
|
|---|
| 410 | pszCmdLine++;
|
|---|
| 411 | /* Specify to allow the trace buffer to wrap when full. */
|
|---|
| 412 | D32g_DbgBufWrap = !iInvertOption;
|
|---|
| 413 | continue;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | iprintf("Unrecognized switch: %s", pszCmdLine-1);
|
|---|
| 417 | iStatus = 1; /* unrecognized argument */
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | if (iStatus) goto init_fail;
|
|---|
| 421 |
|
|---|
| 422 | if (com_baud) InitComPort(com_baud);
|
|---|
| 423 |
|
|---|
| 424 | dprintf(0,"BldLevel: %s\n", BldLevel);
|
|---|
| 425 | dprintf(0,"CmdLine: %s\n", cmd_line);
|
|---|
| 426 | /*
|
|---|
| 427 | if (sizeof(ADD_WORKSPACE) > ADD_WORKSPACE_SIZE)
|
|---|
| 428 | {
|
|---|
| 429 | dprintf(0,"ADD_WORKSPACE size is too big! %d>16\n", sizeof(ADD_WORKSPACE));
|
|---|
| 430 | goto init_fail;
|
|---|
| 431 | }
|
|---|
| 432 | */
|
|---|
| 433 |
|
|---|
| 434 | /* print initialization message */
|
|---|
| 435 | ciprintf("%s driver version %d.%02d", drv_name, DMAJOR, DMINOR);
|
|---|
| 436 |
|
|---|
| 437 | #ifdef TESTVER
|
|---|
| 438 | #include "testver.c"
|
|---|
| 439 | #endif
|
|---|
| 440 |
|
|---|
| 441 | /* scan PCI bus for supported devices */
|
|---|
| 442 | scan_pci_bus();
|
|---|
| 443 |
|
|---|
| 444 | if (ad_info_cnt > 0)
|
|---|
| 445 | {
|
|---|
| 446 | /* initialization succeeded and we found at least one AHCI adapter */
|
|---|
| 447 |
|
|---|
| 448 | if (Dev32Help_RegisterDeviceClass(drv_name, add_entry, 0, 1, &add_handle))
|
|---|
| 449 | {
|
|---|
| 450 | iprintf("%s: couldn't register device class", drv_name);
|
|---|
| 451 | goto init_fail;
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | Timer_InitTimer(TIMER_COUNT);
|
|---|
| 455 |
|
|---|
| 456 | /* allocate context hooks */
|
|---|
| 457 | KernAllocateContextHook(restart_ctxhook, 0, &restart_ctxhook_h);
|
|---|
| 458 | KernAllocateContextHook(reset_ctxhook, 0, &reset_ctxhook_h);
|
|---|
| 459 | KernAllocateContextHook(engine_ctxhook, 0, &engine_ctxhook_h);
|
|---|
| 460 |
|
|---|
| 461 | /* register kernel exit routine for trap dumps */
|
|---|
| 462 | Dev32Help_RegisterKrnlExit(shutdown_driver, FLAG_KRNL_EXIT_ADD, TYPE_KRNL_EXIT_INT13);
|
|---|
| 463 |
|
|---|
| 464 | return(RPDONE);
|
|---|
| 465 | }
|
|---|
| 466 | else
|
|---|
| 467 | {
|
|---|
| 468 | /* no adapters found */
|
|---|
| 469 | ciprintf("%s: No adapters found.", drv_name);
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | init_fail:
|
|---|
| 473 | /* initialization failed; set segment sizes to 0 and return error */
|
|---|
| 474 | init_drv_failed = 1;
|
|---|
| 475 |
|
|---|
| 476 | if (rm_drvh != 0)
|
|---|
| 477 | {
|
|---|
| 478 | /* remove driver from resource manager */
|
|---|
| 479 | RMDestroyDriver(rm_drvh);
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | ciprintf("%s driver *not* installed", drv_name);
|
|---|
| 483 | return(RPDONE | RPERR_INITFAIL);
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | /******************************************************************************
|
|---|
| 487 | * Generic IOCTL via character device driver. IOCTLs are used to control the
|
|---|
| 488 | * driver operation and to execute native ATA and ATAPI (SCSI) commands from
|
|---|
| 489 | * ring 3 applications. On top of that, some predefined IOCTLs (e.g. SMART
|
|---|
| 490 | * commands for ATA disks) are implemented here.
|
|---|
| 491 | */
|
|---|
| 492 | USHORT gen_ioctl(REQPACKET *ioctl)
|
|---|
| 493 | {
|
|---|
| 494 | DPRINTF(2,"IOCTL 0x%x/0x%x\n", ioctl->ioctl.bCategory, ioctl->ioctl.bFunction);
|
|---|
| 495 |
|
|---|
| 496 | switch (ioctl->ioctl.bCategory)
|
|---|
| 497 | {
|
|---|
| 498 | case OS2AHCI_IOCTL_CATEGORY:
|
|---|
| 499 | switch (ioctl->ioctl.bFunction)
|
|---|
| 500 | {
|
|---|
| 501 | case OS2AHCI_IOCTL_GET_DEVLIST:
|
|---|
| 502 | return(ioctl_get_devlist(ioctl));
|
|---|
| 503 |
|
|---|
| 504 | case OS2AHCI_IOCTL_PASSTHROUGH:
|
|---|
| 505 | return(ioctl_passthrough(ioctl));
|
|---|
| 506 | }
|
|---|
| 507 | break;
|
|---|
| 508 |
|
|---|
| 509 | case DSKSP_CAT_GENERIC:
|
|---|
| 510 | return(ioctl_gen_dsk(ioctl));
|
|---|
| 511 |
|
|---|
| 512 | case DSKSP_CAT_SMART:
|
|---|
| 513 | return(ioctl_smart(ioctl));
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | return(RPDONE | RPERR_BADCOMMAND);
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | /******************************************************************************
|
|---|
| 520 | * Read from character device. If tracing is on (internal ring buffer trace),
|
|---|
| 521 | * we return data from the trace buffer; if not, we might return a device
|
|---|
| 522 | * dump similar to IBM1S506.ADD/DANIS506.ADD (TODO).
|
|---|
| 523 | */
|
|---|
| 524 | USHORT char_dev_input(REQPACKET *pPacket)
|
|---|
| 525 | {
|
|---|
| 526 | void *LinAdr;
|
|---|
| 527 |
|
|---|
| 528 | if (Dev32Help_PhysToLin(pPacket->io.ulAddress, pPacket->io.usCount, &LinAdr))
|
|---|
| 529 | {
|
|---|
| 530 | pPacket->io.usCount = 0;
|
|---|
| 531 | return RPDONE | RPERR_GENERAL;
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | pPacket->io.usCount = dCopyToUser(LinAdr, pPacket->io.usCount);
|
|---|
| 535 |
|
|---|
| 536 | return RPDONE;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | /******************************************************************************
|
|---|
| 540 | * Device driver exit handler. This handler is called when OS/2 shuts down and
|
|---|
| 541 | * flushes the write caches of all attached devices. Since this is effectively
|
|---|
| 542 | * the same we do when suspending, we'll call out to the corresponding suspend
|
|---|
| 543 | * function.
|
|---|
| 544 | *
|
|---|
| 545 | * NOTE: Errors are ignored because there's no way we could stop the shutdown
|
|---|
| 546 | * or do something about the error, unless retrying endlessly is
|
|---|
| 547 | * considered an option.
|
|---|
| 548 | */
|
|---|
| 549 | USHORT exit_drv(int func)
|
|---|
| 550 | {
|
|---|
| 551 | DPRINTF(2,"exit_drv(%d) called\n", func);
|
|---|
| 552 |
|
|---|
| 553 | if (func == 0)
|
|---|
| 554 | {
|
|---|
| 555 | /* we're only interested in the second phase of the shutdown */
|
|---|
| 556 | return(RPDONE);
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | suspend();
|
|---|
| 560 | return(RPDONE);
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | /******************************************************************************
|
|---|
| 564 | * Device driver suspend/resume handler. This handler is called when ACPI is
|
|---|
| 565 | * executing a suspend or resume.
|
|---|
| 566 | */
|
|---|
| 567 | USHORT sr_drv(int func)
|
|---|
| 568 | {
|
|---|
| 569 | DPRINTF(2,"sr_drv(%d) called\n", func);
|
|---|
| 570 |
|
|---|
| 571 | if (func) resume();
|
|---|
| 572 | else suspend();
|
|---|
| 573 |
|
|---|
| 574 | return(RPDONE);
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | /******************************************************************************
|
|---|
| 578 | * ADD entry point. This is the main entry point for all ADD requests. Due to
|
|---|
| 579 | * the asynchronous nature of ADD drivers, this function primarily queues the
|
|---|
| 580 | * IORB(s) to the corresponding adapter or port queues, then triggers the
|
|---|
| 581 | * state machine to initiate processing queued IORBs.
|
|---|
| 582 | *
|
|---|
| 583 | * NOTE: In order to prevent race conditions or engine stalls, certain rules
|
|---|
| 584 | * around locking, unlocking and IORB handling in general have been
|
|---|
| 585 | * established. Refer to the comments in "trigger_engine()" for
|
|---|
| 586 | * details.
|
|---|
| 587 | */
|
|---|
| 588 | void add_entry(IORBH FAR16DATA *vFirstIorb)
|
|---|
| 589 | {
|
|---|
| 590 | IORBH FAR16DATA *vIorb;
|
|---|
| 591 | IORBH FAR16DATA *vNext = FAR16NULL;
|
|---|
| 592 |
|
|---|
| 593 | spin_lock(drv_lock);
|
|---|
| 594 |
|
|---|
| 595 | for (vIorb=vFirstIorb; vIorb!=FAR16NULL; vIorb=vNext)
|
|---|
| 596 | {
|
|---|
| 597 | IORBH *pIorb = Far16ToFlat(vIorb);
|
|---|
| 598 |
|
|---|
| 599 | /* Queue this IORB. Queues primarily exist on port level but there are
|
|---|
| 600 | * some requests which affect the whole driver, most notably
|
|---|
| 601 | * IOCC_CONFIGURATION. In either case, adding the IORB to the driver or
|
|---|
| 602 | * port queue will change the links, thus we need to save the original
|
|---|
| 603 | * link in 'vNext'.
|
|---|
| 604 | */
|
|---|
| 605 | if (pIorb->RequestControl & IORB_CHAIN) vNext = pIorb->pNxtIORB;
|
|---|
| 606 | else vNext = (IORBH FAR16DATA *)0;
|
|---|
| 607 |
|
|---|
| 608 | pIorb->Status = 0;
|
|---|
| 609 | pIorb->ErrorCode = 0;
|
|---|
| 610 | memset(&pIorb->ADDWorkSpace, 0x00, sizeof(ADD_WORKSPACE));
|
|---|
| 611 |
|
|---|
| 612 | #ifdef DEBUG
|
|---|
| 613 | DumpIorb(pIorb); /* DAZ TESTING */
|
|---|
| 614 | #endif
|
|---|
| 615 |
|
|---|
| 616 | if (iorb_driver_level(pIorb))
|
|---|
| 617 | {
|
|---|
| 618 | /* driver-level IORB */
|
|---|
| 619 | pIorb->UnitHandle = 0;
|
|---|
| 620 | iorb_queue_add(&driver_queue, vIorb, pIorb);
|
|---|
| 621 | }
|
|---|
| 622 | else
|
|---|
| 623 | {
|
|---|
| 624 | /* port-level IORB */
|
|---|
| 625 | int a = iorb_unit_adapter(pIorb);
|
|---|
| 626 | int p = iorb_unit_port(pIorb);
|
|---|
| 627 | int d = iorb_unit_device(pIorb);
|
|---|
| 628 |
|
|---|
| 629 | if (a >= ad_info_cnt ||
|
|---|
| 630 | p > ad_infos[a].port_max ||
|
|---|
| 631 | d > ad_infos[a].ports[p].dev_max ||
|
|---|
| 632 | (ad_infos[a].port_map & (1UL << p)) == 0)
|
|---|
| 633 | {
|
|---|
| 634 | /* unit handle outside of the allowed range */
|
|---|
| 635 | dprintf(0,"warning: IORB for %d.%d.%d out of range\n", a, p, d);
|
|---|
| 636 | pIorb->Status = IORB_ERROR;
|
|---|
| 637 | pIorb->ErrorCode = IOERR_CMD_SYNTAX;
|
|---|
| 638 | iorb_complete(vIorb, pIorb);
|
|---|
| 639 | continue;
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | iorb_queue_add(&ad_infos[a].ports[p].iorb_queue, vIorb, pIorb);
|
|---|
| 643 | }
|
|---|
| 644 | }
|
|---|
| 645 |
|
|---|
| 646 | /* trigger state machine */
|
|---|
| 647 | trigger_engine();
|
|---|
| 648 |
|
|---|
| 649 | spin_unlock(drv_lock);
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 | /******************************************************************************
|
|---|
| 653 | * Trigger IORB queue engine. This is a wrapper function for trigger_engine_1()
|
|---|
| 654 | * which will try to get all IORBs sent on their way a couple of times. If
|
|---|
| 655 | * there are still IORBs ready for processing after this, this function will
|
|---|
| 656 | * hand off to a context hook which will continue to trigger the engine until
|
|---|
| 657 | * all IORBs have been sent.
|
|---|
| 658 | *
|
|---|
| 659 | * NOTE: While initialization has not completed (or during suspend/resume
|
|---|
| 660 | * operations), this function will loop indefinitely because we can't
|
|---|
| 661 | * rely on interrupt handlers or context hooks and complex IORBs
|
|---|
| 662 | * requiring multiple requeues would eventually hang and time out if
|
|---|
| 663 | * we stopped triggering here.
|
|---|
| 664 | */
|
|---|
| 665 | void trigger_engine(void)
|
|---|
| 666 | {
|
|---|
| 667 | int i;
|
|---|
| 668 |
|
|---|
| 669 | for (i = 0; i < 3 || !init_complete; i++)
|
|---|
| 670 | {
|
|---|
| 671 | if (trigger_engine_1() == 0)
|
|---|
| 672 | {
|
|---|
| 673 | /* done -- all IORBs have been sent on their way */
|
|---|
| 674 | return;
|
|---|
| 675 | }
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | /* Something keeps bouncing; hand off to the engine context hook which will
|
|---|
| 679 | * keep trying in the background.
|
|---|
| 680 | */
|
|---|
| 681 | KernArmHook(engine_ctxhook_h, 0, 0);
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 | /******************************************************************************
|
|---|
| 685 | * Trigger IORB queue engine in order to send commands in the driver/port IORB
|
|---|
| 686 | * queues to the AHCI hardware. This function will return the number of IORBs
|
|---|
| 687 | * sent. Keep in mind that IORBs might "bounce" if the adapter/port is not in
|
|---|
| 688 | * a state to accept the command, thus it might take quite a few calls to get
|
|---|
| 689 | * all IORBs on their way. This is why there's a wrapper function which tries
|
|---|
| 690 | * it a few times, then hands off to a context hook which will keep trying in
|
|---|
| 691 | * the background.
|
|---|
| 692 | *
|
|---|
| 693 | * IORBs might complete before send_iorb() has returned, at any time during
|
|---|
| 694 | * interrupt processing or on another CPU on SMP systems. IORB completion
|
|---|
| 695 | * means modifications to the corresponding IORB queue (the completed IORB
|
|---|
| 696 | * is removed from the queue) thus we need to protect the IORB queues from
|
|---|
| 697 | * race conditions. The safest approach short of keeping the driver-level
|
|---|
| 698 | * spinlock aquired permanently is to keep it throughout this function and
|
|---|
| 699 | * release it temporarily in send_iorb().
|
|---|
| 700 | *
|
|---|
| 701 | * This implies that the handler functions are fully responsible for aquiring
|
|---|
| 702 | * the driver-level spinlock when they need it, and for releasing it again.
|
|---|
| 703 | *
|
|---|
| 704 | * As a rule of thumb, get the driver-level spinlock whenever accessing
|
|---|
| 705 | * volatile variables (IORB queues, values in ad_info[], ...).
|
|---|
| 706 | *
|
|---|
| 707 | * Additional Notes:
|
|---|
| 708 | *
|
|---|
| 709 | * - This function is expected to be called with the spinlock aquired
|
|---|
| 710 | *
|
|---|
| 711 | * - Adapters can be flagged as 'busy' which means no new IORBs are sent (they
|
|---|
| 712 | * just remain in the queue). This can be used to release the driver-level
|
|---|
| 713 | * spinlock while making sure no new IORBs are going to hit the hardware.
|
|---|
| 714 | * In order to prevent engine stalls, all handlers using this functionality
|
|---|
| 715 | * need to invoke trigger_engine() after resetting the busy flag.
|
|---|
| 716 | *
|
|---|
| 717 | * - Driver-level IORBs are not synchronized by adapter-level 'busy' flags.
|
|---|
| 718 | * However, the driver-level queue is worked "one entry at a time" which
|
|---|
| 719 | * means that no new IORBs will be queued on the driver-level queue until
|
|---|
| 720 | * the head element has completed processing. This means that driver-
|
|---|
| 721 | * level IORB handlers don't need to protect against each other. But they
|
|---|
| 722 | * they do need to keep in mind interference with port-level IORBs:
|
|---|
| 723 | *
|
|---|
| 724 | * - Driver-level IORB handlers must obtain the spinlock and/or flag all
|
|---|
| 725 | * adapters as 'busy' which are affected by the driver-level IORB
|
|---|
| 726 | *
|
|---|
| 727 | * - Driver-level IORB handlers must not access the hardware of a
|
|---|
| 728 | * particular adapter if it's flagged as 'busy' by another IORB.
|
|---|
| 729 | */
|
|---|
| 730 | int trigger_engine_1(void)
|
|---|
| 731 | {
|
|---|
| 732 | IORBH FAR16DATA *vIorb;
|
|---|
| 733 | IORBH *pIorb;
|
|---|
| 734 | IORBH FAR16DATA *vNext;
|
|---|
| 735 | int iorbs_sent = 0;
|
|---|
| 736 | int a;
|
|---|
| 737 | int p;
|
|---|
| 738 |
|
|---|
| 739 | iorbs_sent = 0;
|
|---|
| 740 |
|
|---|
| 741 | /* process driver-level IORBs */
|
|---|
| 742 | if ((vIorb = driver_queue.vRoot) != FAR16NULL)
|
|---|
| 743 | {
|
|---|
| 744 | pIorb = Far16ToFlat(vIorb);
|
|---|
| 745 |
|
|---|
| 746 | if (!add_workspace(pIorb)->processing)
|
|---|
| 747 | {
|
|---|
| 748 | send_iorb(vIorb, pIorb);
|
|---|
| 749 | iorbs_sent++;
|
|---|
| 750 | }
|
|---|
| 751 | }
|
|---|
| 752 |
|
|---|
| 753 | /* process port-level IORBs */
|
|---|
| 754 | for (a = 0; a < ad_info_cnt; a++)
|
|---|
| 755 | {
|
|---|
| 756 | AD_INFO *ai = ad_infos + a;
|
|---|
| 757 | if (ai->busy)
|
|---|
| 758 | {
|
|---|
| 759 | /* adapter is busy; don't process any IORBs */
|
|---|
| 760 | continue;
|
|---|
| 761 | }
|
|---|
| 762 | for (p = 0; p <= ai->port_max; p++)
|
|---|
| 763 | {
|
|---|
| 764 | /* send all queued IORBs on this port */
|
|---|
| 765 | vNext = FAR16NULL;
|
|---|
| 766 | for (vIorb = ai->ports[p].iorb_queue.vRoot; vIorb != FAR16NULL; vIorb = vNext)
|
|---|
| 767 | {
|
|---|
| 768 | pIorb = Far16ToFlat(vIorb);
|
|---|
| 769 |
|
|---|
| 770 | vNext = pIorb->pNxtIORB;
|
|---|
| 771 | if (!add_workspace(pIorb)->processing)
|
|---|
| 772 | {
|
|---|
| 773 | send_iorb(vIorb, pIorb);
|
|---|
| 774 | iorbs_sent++;
|
|---|
| 775 | }
|
|---|
| 776 | }
|
|---|
| 777 | }
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | return(iorbs_sent);
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | /******************************************************************************
|
|---|
| 784 | * Send a single IORB to the corresponding AHCI adapter/port. This is just a
|
|---|
| 785 | * switch board for calling the corresponding iocc_*() handler function.
|
|---|
| 786 | *
|
|---|
| 787 | * NOTE: This function is expected to be called with the driver-level spinlock
|
|---|
| 788 | * aquired. It will release it before calling any of the handler
|
|---|
| 789 | * functions and re-aquire it when done.
|
|---|
| 790 | */
|
|---|
| 791 | void send_iorb(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 792 | {
|
|---|
| 793 | /* Mark IORB as "processing" before doing anything else. Once the IORB is
|
|---|
| 794 | * marked as "processing", we can release the spinlock because subsequent
|
|---|
| 795 | * invocations of trigger_engine() (e.g. at interrupt time) will ignore this
|
|---|
| 796 | * IORB.
|
|---|
| 797 | */
|
|---|
| 798 | add_workspace(pIorb)->processing = 1;
|
|---|
| 799 | spin_unlock(drv_lock);
|
|---|
| 800 |
|
|---|
| 801 | switch (pIorb->CommandCode)
|
|---|
| 802 | {
|
|---|
| 803 | case IOCC_CONFIGURATION:
|
|---|
| 804 | iocc_configuration(vIorb, pIorb);
|
|---|
| 805 | break;
|
|---|
| 806 |
|
|---|
| 807 | case IOCC_DEVICE_CONTROL:
|
|---|
| 808 | iocc_device_control(vIorb, pIorb);
|
|---|
| 809 | break;
|
|---|
| 810 |
|
|---|
| 811 | case IOCC_UNIT_CONTROL:
|
|---|
| 812 | iocc_unit_control(vIorb, pIorb);
|
|---|
| 813 | break;
|
|---|
| 814 |
|
|---|
| 815 | case IOCC_GEOMETRY:
|
|---|
| 816 | iocc_geometry(vIorb, pIorb);
|
|---|
| 817 | break;
|
|---|
| 818 |
|
|---|
| 819 | case IOCC_EXECUTE_IO:
|
|---|
| 820 | iocc_execute_io(vIorb, pIorb);
|
|---|
| 821 | break;
|
|---|
| 822 |
|
|---|
| 823 | case IOCC_UNIT_STATUS:
|
|---|
| 824 | iocc_unit_status(vIorb, pIorb);
|
|---|
| 825 | break;
|
|---|
| 826 |
|
|---|
| 827 | case IOCC_ADAPTER_PASSTHRU:
|
|---|
| 828 | iocc_adapter_passthru(vIorb, pIorb);
|
|---|
| 829 | break;
|
|---|
| 830 |
|
|---|
| 831 | default:
|
|---|
| 832 | /* unsupported call */
|
|---|
| 833 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 834 | iorb_done(vIorb, pIorb);
|
|---|
| 835 | break;
|
|---|
| 836 | }
|
|---|
| 837 |
|
|---|
| 838 | /* re-aquire spinlock before returning to trigger_engine() */
|
|---|
| 839 | spin_lock(drv_lock);
|
|---|
| 840 | }
|
|---|
| 841 |
|
|---|
| 842 | /******************************************************************************
|
|---|
| 843 | * Handle IOCC_CONFIGURATION requests.
|
|---|
| 844 | */
|
|---|
| 845 | void iocc_configuration(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 846 | {
|
|---|
| 847 | int a;
|
|---|
| 848 |
|
|---|
| 849 | switch (pIorb->CommandModifier)
|
|---|
| 850 | {
|
|---|
| 851 |
|
|---|
| 852 | case IOCM_COMPLETE_INIT:
|
|---|
| 853 | /* Complete initialization. From now on, we won't have to restore the BIOS
|
|---|
| 854 | * configuration after each command and we're fully operational (i.e. will
|
|---|
| 855 | * use interrupts, timers and context hooks instead of polling).
|
|---|
| 856 | */
|
|---|
| 857 | if (!init_complete)
|
|---|
| 858 | {
|
|---|
| 859 | DPRINTF(1,"leaving initialization mode\n");
|
|---|
| 860 | for (a = 0; a < ad_info_cnt; a++)
|
|---|
| 861 | {
|
|---|
| 862 | lock_adapter(ad_infos + a);
|
|---|
| 863 | ahci_complete_init(ad_infos + a);
|
|---|
| 864 | }
|
|---|
| 865 | init_complete = 1;
|
|---|
| 866 |
|
|---|
| 867 | /* release all adapters */
|
|---|
| 868 | for (a = 0; a < ad_info_cnt; a++)
|
|---|
| 869 | {
|
|---|
| 870 | unlock_adapter(ad_infos + a);
|
|---|
| 871 | }
|
|---|
| 872 | DPRINTF(1,"leaving initialization mode 2\n");
|
|---|
| 873 |
|
|---|
| 874 | #ifdef LEGACY_APM
|
|---|
| 875 | /* register APM hook */
|
|---|
| 876 | apm_init();
|
|---|
| 877 | #endif
|
|---|
| 878 | }
|
|---|
| 879 | iorb_done(vIorb, pIorb);
|
|---|
| 880 | break;
|
|---|
| 881 |
|
|---|
| 882 | case IOCM_GET_DEVICE_TABLE:
|
|---|
| 883 | /* construct a device table */
|
|---|
| 884 | iocm_device_table(vIorb, pIorb);
|
|---|
| 885 | break;
|
|---|
| 886 |
|
|---|
| 887 | default:
|
|---|
| 888 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 889 | iorb_done(vIorb, pIorb);
|
|---|
| 890 | break;
|
|---|
| 891 | }
|
|---|
| 892 | }
|
|---|
| 893 |
|
|---|
| 894 | /******************************************************************************
|
|---|
| 895 | * Handle IOCC_DEVICE_CONTROL requests.
|
|---|
| 896 | */
|
|---|
| 897 | void iocc_device_control(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 898 | {
|
|---|
| 899 | AD_INFO *ai = ad_infos + iorb_unit_adapter(pIorb);
|
|---|
| 900 | IORBH FAR16DATA *vPtr;
|
|---|
| 901 | IORBH FAR16DATA *vNext = FAR16NULL;
|
|---|
| 902 | int p = iorb_unit_port(pIorb);
|
|---|
| 903 | int d = iorb_unit_device(pIorb);
|
|---|
| 904 |
|
|---|
| 905 | switch (pIorb->CommandModifier)
|
|---|
| 906 | {
|
|---|
| 907 | case IOCM_ABORT:
|
|---|
| 908 | /* abort all pending commands on specified port and device */
|
|---|
| 909 | spin_lock(drv_lock);
|
|---|
| 910 | for (vPtr = ai->ports[p].iorb_queue.vRoot; vPtr != FAR16NULL; vPtr = vNext)
|
|---|
| 911 | {
|
|---|
| 912 | IORBH *pPtr = Far16ToFlat(vPtr);
|
|---|
| 913 |
|
|---|
| 914 | vNext = pPtr->pNxtIORB;
|
|---|
| 915 | /* move all matching IORBs to the abort queue */
|
|---|
| 916 | if (vPtr != vIorb && iorb_unit_device(pPtr) == d)
|
|---|
| 917 | {
|
|---|
| 918 | iorb_queue_del(&ai->ports[p].iorb_queue, vPtr);
|
|---|
| 919 | iorb_queue_add(&abort_queue, vPtr, pPtr);
|
|---|
| 920 | pPtr->ErrorCode = IOERR_CMD_ABORTED;
|
|---|
| 921 | }
|
|---|
| 922 | }
|
|---|
| 923 | spin_unlock(drv_lock);
|
|---|
| 924 |
|
|---|
| 925 | /* trigger reset context hook which will finish the abort processing */
|
|---|
| 926 | KernArmHook(reset_ctxhook_h, 0, 0);
|
|---|
| 927 | break;
|
|---|
| 928 |
|
|---|
| 929 | case IOCM_SUSPEND:
|
|---|
| 930 | case IOCM_RESUME:
|
|---|
| 931 | case IOCM_GET_QUEUE_STATUS:
|
|---|
| 932 | /* Suspend/resume operations allow access to the hardware for other
|
|---|
| 933 | * entities such as IBMIDECD.FLT. Since os2ahci implements both ATA
|
|---|
| 934 | * and ATAPI in the same driver, this won't be required.
|
|---|
| 935 | */
|
|---|
| 936 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 937 | break;
|
|---|
| 938 |
|
|---|
| 939 | case IOCM_LOCK_MEDIA:
|
|---|
| 940 | case IOCM_UNLOCK_MEDIA:
|
|---|
| 941 | case IOCM_EJECT_MEDIA:
|
|---|
| 942 | /* unit control commands to lock, unlock and eject media */
|
|---|
| 943 | /* will be supported later... */
|
|---|
| 944 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 945 | break;
|
|---|
| 946 |
|
|---|
| 947 | default:
|
|---|
| 948 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 949 | break;
|
|---|
| 950 | }
|
|---|
| 951 |
|
|---|
| 952 | iorb_done(vIorb, pIorb);
|
|---|
| 953 | }
|
|---|
| 954 |
|
|---|
| 955 | /******************************************************************************
|
|---|
| 956 | * Handle IOCC_UNIT_CONTROL requests.
|
|---|
| 957 | */
|
|---|
| 958 | void iocc_unit_control(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 959 | {
|
|---|
| 960 | IORB_UNIT_CONTROL *pIorb_uc = (IORB_UNIT_CONTROL *)pIorb;
|
|---|
| 961 | int a = iorb_unit_adapter(pIorb);
|
|---|
| 962 | int p = iorb_unit_port(pIorb);
|
|---|
| 963 | int d = iorb_unit_device(pIorb);
|
|---|
| 964 |
|
|---|
| 965 | spin_lock(drv_lock);
|
|---|
| 966 | switch (pIorb->CommandModifier)
|
|---|
| 967 | {
|
|---|
| 968 | case IOCM_ALLOCATE_UNIT:
|
|---|
| 969 | /* allocate unit for exclusive access */
|
|---|
| 970 | if (ad_infos[a].ports[p].devs[d].allocated)
|
|---|
| 971 | {
|
|---|
| 972 | iorb_seterr(pIorb, IOERR_UNIT_ALLOCATED);
|
|---|
| 973 | }
|
|---|
| 974 | else
|
|---|
| 975 | {
|
|---|
| 976 | ad_infos[a].ports[p].devs[d].allocated = 1;
|
|---|
| 977 | }
|
|---|
| 978 | break;
|
|---|
| 979 |
|
|---|
| 980 | case IOCM_DEALLOCATE_UNIT:
|
|---|
| 981 | /* deallocate exclusive access to unit */
|
|---|
| 982 | if (!ad_infos[a].ports[p].devs[d].allocated)
|
|---|
| 983 | {
|
|---|
| 984 | iorb_seterr(pIorb, IOERR_UNIT_NOT_ALLOCATED);
|
|---|
| 985 | }
|
|---|
| 986 | else
|
|---|
| 987 | {
|
|---|
| 988 | ad_infos[a].ports[p].devs[d].allocated = 0;
|
|---|
| 989 | }
|
|---|
| 990 | break;
|
|---|
| 991 |
|
|---|
| 992 | case IOCM_CHANGE_UNITINFO:
|
|---|
| 993 | /* Change unit (device) information. One reason for this IOCM is the
|
|---|
| 994 | * interface for filter device drivers: a filter device driver can
|
|---|
| 995 | * either change existing UNITINFOs or permanently allocate units
|
|---|
| 996 | * and fabricate new [logical] units; the former is the reason why we
|
|---|
| 997 | * must store the pointer to the updated UNITNIFO for subsequent
|
|---|
| 998 | * IOCC_CONFIGURATION/IOCM_GET_DEVICE_TABLE calls.
|
|---|
| 999 | */
|
|---|
| 1000 | if (!ad_infos[a].ports[p].devs[d].allocated)
|
|---|
| 1001 | {
|
|---|
| 1002 | iorb_seterr(pIorb, IOERR_UNIT_NOT_ALLOCATED);
|
|---|
| 1003 | break;
|
|---|
| 1004 | }
|
|---|
| 1005 | ad_infos[a].ports[p].devs[d].unit_info = pIorb_uc->pUnitInfo;
|
|---|
| 1006 | break;
|
|---|
| 1007 |
|
|---|
| 1008 | default:
|
|---|
| 1009 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 1010 | break;
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | spin_unlock(drv_lock);
|
|---|
| 1014 | iorb_done(vIorb, pIorb);
|
|---|
| 1015 | }
|
|---|
| 1016 |
|
|---|
| 1017 | /******************************************************************************
|
|---|
| 1018 | * Scan all ports for AHCI devices and construct a DASD device table.
|
|---|
| 1019 | *
|
|---|
| 1020 | * NOTES: This function may be called multiple times. Only the first
|
|---|
| 1021 | * invocation will actually scan for devices; all subsequent calls will
|
|---|
| 1022 | * merely return the results of the initial scan, potentially augmented
|
|---|
| 1023 | * by modified unit infos after IOCC_CONFIGURATION/IOCM_CHANGE_UNITINFO
|
|---|
| 1024 | * requests.
|
|---|
| 1025 | *
|
|---|
| 1026 | * In order to support applications that can't deal with ATAPI devices
|
|---|
| 1027 | * (i.e. need a SCSI adapter) os2ahci will optionally report ATAPI
|
|---|
| 1028 | * dvices as SCSI devices. The corresponding SCSI adapter doesn't
|
|---|
| 1029 | * really exist and is only reported here for the IOCM_GET_DEVICETABLE
|
|---|
| 1030 | * request. The units attached to this adapter will use the real HW
|
|---|
| 1031 | * unit IDs, thus we'll never receive a command specific to the
|
|---|
| 1032 | * emulated SCSI adapter and won't need to set up any sort of entity
|
|---|
| 1033 | * for it; the only purpose of the emulated SCSI adapter is to pass the
|
|---|
| 1034 | * bus type "AI_DEVBUS_SCSI_2" upstream, and the emulated units, of
|
|---|
| 1035 | * course. The emulated SCSI target IDs are allocated as follows:
|
|---|
| 1036 | *
|
|---|
| 1037 | * 0 the virtual adapter
|
|---|
| 1038 | * 1..n emulated devices; SCSI target ID increments sequentially
|
|---|
| 1039 | */
|
|---|
| 1040 | void iocm_device_table(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1041 | {
|
|---|
| 1042 | IORB_CONFIGURATION *pIorb_conf;
|
|---|
| 1043 | DEVICETABLE FAR16DATA *vDt;
|
|---|
| 1044 | DEVICETABLE *pDt;
|
|---|
| 1045 | char *pPos;
|
|---|
| 1046 | int scsi_units = 0;
|
|---|
| 1047 | int scsi_id = 1;
|
|---|
| 1048 | int rc;
|
|---|
| 1049 | int dta;
|
|---|
| 1050 | int a;
|
|---|
| 1051 | int p;
|
|---|
| 1052 | int d;
|
|---|
| 1053 |
|
|---|
| 1054 | pIorb_conf = (IORB_CONFIGURATION *)pIorb;
|
|---|
| 1055 | vDt = pIorb_conf->pDeviceTable;
|
|---|
| 1056 | pDt = Far16ToFlat(vDt);
|
|---|
| 1057 |
|
|---|
| 1058 | spin_lock(drv_lock);
|
|---|
| 1059 |
|
|---|
| 1060 | /* initialize device table header */
|
|---|
| 1061 | pDt->ADDLevelMajor = ADD_LEVEL_MAJOR;
|
|---|
| 1062 | pDt->ADDLevelMinor = ADD_LEVEL_MINOR;
|
|---|
| 1063 | pDt->ADDHandle = add_handle;
|
|---|
| 1064 | pDt->TotalAdapters = ad_info_cnt + 1;
|
|---|
| 1065 |
|
|---|
| 1066 | /* set start of adapter and device information tables */
|
|---|
| 1067 | pPos = (char*)&pDt->pAdapter[pDt->TotalAdapters];
|
|---|
| 1068 |
|
|---|
| 1069 | /* go through all adapters, including the virtual SCSI adapter */
|
|---|
| 1070 | for (dta = 0; dta < pDt->TotalAdapters; dta++)
|
|---|
| 1071 | {
|
|---|
| 1072 | ADAPTERINFO *pPtr = (ADAPTERINFO *)pPos;
|
|---|
| 1073 |
|
|---|
| 1074 | /* sanity check for sufficient space in device table */
|
|---|
| 1075 | if ((u32)(pPtr + 1) - (u32)pDt > pIorb_conf->DeviceTableLen)
|
|---|
| 1076 | {
|
|---|
| 1077 | dprintf(0,"error: device table provided by DASD too small\n");
|
|---|
| 1078 | iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
|
|---|
| 1079 | goto iocm_device_table_done;
|
|---|
| 1080 | }
|
|---|
| 1081 |
|
|---|
| 1082 | pDt->pAdapter[dta] = MakeNear16PtrFromDiff(pIorb_conf->pDeviceTable, pDt, pPtr);
|
|---|
| 1083 |
|
|---|
| 1084 | //DPRINTF(2,"iocm_device_table: ptr=%x dta=%x pAdapter[dta]=%x pDeviceTable=%x\n",
|
|---|
| 1085 | // ptr, dta, dt->pAdapter[dta], iorb_conf->pDeviceTable);
|
|---|
| 1086 | memset(pPtr, 0x00, sizeof(*pPtr));
|
|---|
| 1087 |
|
|---|
| 1088 | pPtr->AdapterIOAccess = AI_IOACCESS_BUS_MASTER;
|
|---|
| 1089 | pPtr->AdapterHostBus = AI_HOSTBUS_OTHER | AI_BUSWIDTH_32BIT;
|
|---|
| 1090 | pPtr->AdapterFlags = AF_16M | AF_HW_SCATGAT;
|
|---|
| 1091 | pPtr->MaxHWSGList = AHCI_MAX_SG / 2; /* AHCI S/G elements are 22 bits */
|
|---|
| 1092 |
|
|---|
| 1093 | if (dta < ad_info_cnt)
|
|---|
| 1094 | {
|
|---|
| 1095 | /* this is a physical AHCI adapter */
|
|---|
| 1096 | AD_INFO *ad_info = ad_infos + dta;
|
|---|
| 1097 |
|
|---|
| 1098 | pPtr->AdapterDevBus = AI_DEVBUS_ST506 | AI_DEVBUS_32BIT;
|
|---|
| 1099 | snprintf(pPtr->AdapterName, sizeof(pPtr->AdapterName), "AHCI_%d", dta);
|
|---|
| 1100 |
|
|---|
| 1101 | if (!ad_info->port_scan_done)
|
|---|
| 1102 | {
|
|---|
| 1103 | /* first call; need to scan AHCI hardware for devices */
|
|---|
| 1104 | if (ad_info->busy)
|
|---|
| 1105 | {
|
|---|
| 1106 | dprintf(0,"error: port scan requested while adapter was busy\n");
|
|---|
| 1107 | iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
|
|---|
| 1108 | goto iocm_device_table_done;
|
|---|
| 1109 | }
|
|---|
| 1110 | ad_info->busy = 1;
|
|---|
| 1111 | spin_unlock(drv_lock);
|
|---|
| 1112 | rc = ahci_scan_ports(ad_info);
|
|---|
| 1113 | spin_lock(drv_lock);
|
|---|
| 1114 | ad_info->busy = 0;
|
|---|
| 1115 |
|
|---|
| 1116 | if (rc != 0)
|
|---|
| 1117 | {
|
|---|
| 1118 | dprintf(0,"error: port scan failed on adapter #%d\n", dta);
|
|---|
| 1119 | iorb_seterr(pIorb, IOERR_CMD_SW_RESOURCE);
|
|---|
| 1120 | goto iocm_device_table_done;
|
|---|
| 1121 | }
|
|---|
| 1122 | ad_info->port_scan_done = 1;
|
|---|
| 1123 | }
|
|---|
| 1124 |
|
|---|
| 1125 | /* insert physical (i.e. AHCI) devices into the device table */
|
|---|
| 1126 | for (p = 0; p <= ad_info->port_max; p++)
|
|---|
| 1127 | {
|
|---|
| 1128 | for (d = 0; d <= ad_info->ports[p].dev_max; d++)
|
|---|
| 1129 | {
|
|---|
| 1130 | if (ad_info->ports[p].devs[d].present)
|
|---|
| 1131 | {
|
|---|
| 1132 | if (ad_info->ports[p].devs[d].atapi && emulate_scsi[dta][p])
|
|---|
| 1133 | {
|
|---|
| 1134 | /* report this unit as SCSI unit */
|
|---|
| 1135 | scsi_units++;
|
|---|
| 1136 | //continue;
|
|---|
| 1137 | }
|
|---|
| 1138 | if (add_unit_info(pIorb_conf, dta, dta, p, d, 0))
|
|---|
| 1139 | {
|
|---|
| 1140 | goto iocm_device_table_done;
|
|---|
| 1141 | }
|
|---|
| 1142 | }
|
|---|
| 1143 | }
|
|---|
| 1144 | }
|
|---|
| 1145 | }
|
|---|
| 1146 | else
|
|---|
| 1147 | {
|
|---|
| 1148 | /* this is the virtual SCSI adapter */
|
|---|
| 1149 | if (scsi_units == 0)
|
|---|
| 1150 | {
|
|---|
| 1151 | /* not a single unit to be emulated via SCSI */
|
|---|
| 1152 | pDt->TotalAdapters--;
|
|---|
| 1153 | break;
|
|---|
| 1154 | }
|
|---|
| 1155 |
|
|---|
| 1156 | /* set adapter name and bus type to mimic a SCSI controller */
|
|---|
| 1157 | pPtr->AdapterDevBus = AI_DEVBUS_SCSI_2 | AI_DEVBUS_16BIT;
|
|---|
| 1158 | snprintf(pPtr->AdapterName, sizeof(pPtr->AdapterName), "AHCI_SCSI_0");
|
|---|
| 1159 |
|
|---|
| 1160 | /* add all ATAPI units to be emulated by this virtual adaper */
|
|---|
| 1161 | for (a = 0; a < ad_info_cnt; a++)
|
|---|
| 1162 | {
|
|---|
| 1163 | AD_INFO *ad_info = ad_infos + a;
|
|---|
| 1164 |
|
|---|
| 1165 | for (p = 0; p <= ad_info->port_max; p++)
|
|---|
| 1166 | {
|
|---|
| 1167 | for (d = 0; d <= ad_info->ports[p].dev_max; d++)
|
|---|
| 1168 | {
|
|---|
| 1169 | if (ad_info->ports[p].devs[d].present && ad_info->ports[p].devs[d].atapi && emulate_scsi[a][p])
|
|---|
| 1170 | {
|
|---|
| 1171 | if (add_unit_info(pIorb_conf, dta, a, p, d, scsi_id++))
|
|---|
| 1172 | {
|
|---|
| 1173 | goto iocm_device_table_done;
|
|---|
| 1174 | }
|
|---|
| 1175 | }
|
|---|
| 1176 | }
|
|---|
| 1177 | }
|
|---|
| 1178 | }
|
|---|
| 1179 | }
|
|---|
| 1180 |
|
|---|
| 1181 | /* calculate offset for next adapter */
|
|---|
| 1182 | pPos = (char *)(pPtr->UnitInfo + pPtr->AdapterUnits);
|
|---|
| 1183 | }
|
|---|
| 1184 |
|
|---|
| 1185 | iocm_device_table_done:
|
|---|
| 1186 | spin_unlock(drv_lock);
|
|---|
| 1187 | iorb_done(vIorb, pIorb);
|
|---|
| 1188 | }
|
|---|
| 1189 |
|
|---|
| 1190 | /******************************************************************************
|
|---|
| 1191 | * Handle IOCC_GEOMETRY requests.
|
|---|
| 1192 | */
|
|---|
| 1193 | void iocc_geometry(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1194 | {
|
|---|
| 1195 | switch (pIorb->CommandModifier)
|
|---|
| 1196 | {
|
|---|
| 1197 | case IOCM_GET_MEDIA_GEOMETRY:
|
|---|
| 1198 | case IOCM_GET_DEVICE_GEOMETRY:
|
|---|
| 1199 | add_workspace(pIorb)->idempotent = 1;
|
|---|
| 1200 | ahci_get_geometry(vIorb, pIorb);
|
|---|
| 1201 | break;
|
|---|
| 1202 |
|
|---|
| 1203 | default:
|
|---|
| 1204 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 1205 | iorb_done(vIorb, pIorb);
|
|---|
| 1206 | }
|
|---|
| 1207 | }
|
|---|
| 1208 |
|
|---|
| 1209 | /******************************************************************************
|
|---|
| 1210 | * Handle IOCC_EXECUTE_IO requests.
|
|---|
| 1211 | */
|
|---|
| 1212 | void iocc_execute_io(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1213 | {
|
|---|
| 1214 | switch (pIorb->CommandModifier)
|
|---|
| 1215 | {
|
|---|
| 1216 | case IOCM_READ:
|
|---|
| 1217 | add_workspace(pIorb)->idempotent = 1;
|
|---|
| 1218 | ahci_read(vIorb, pIorb);
|
|---|
| 1219 | break;
|
|---|
| 1220 |
|
|---|
| 1221 | case IOCM_READ_VERIFY:
|
|---|
| 1222 | add_workspace(pIorb)->idempotent = 1;
|
|---|
| 1223 | ahci_verify(vIorb, pIorb);
|
|---|
| 1224 | break;
|
|---|
| 1225 |
|
|---|
| 1226 | case IOCM_WRITE:
|
|---|
| 1227 | add_workspace(pIorb)->idempotent = 1;
|
|---|
| 1228 | ahci_write(vIorb, pIorb);
|
|---|
| 1229 | break;
|
|---|
| 1230 |
|
|---|
| 1231 | case IOCM_WRITE_VERIFY:
|
|---|
| 1232 | add_workspace(pIorb)->idempotent = 1;
|
|---|
| 1233 | ahci_write(vIorb, pIorb);
|
|---|
| 1234 | break;
|
|---|
| 1235 |
|
|---|
| 1236 | default:
|
|---|
| 1237 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 1238 | iorb_done(vIorb, pIorb);
|
|---|
| 1239 | }
|
|---|
| 1240 | }
|
|---|
| 1241 |
|
|---|
| 1242 | /******************************************************************************
|
|---|
| 1243 | * Handle IOCC_UNIT_STATUS requests.
|
|---|
| 1244 | */
|
|---|
| 1245 | void iocc_unit_status(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1246 | {
|
|---|
| 1247 | switch (pIorb->CommandModifier)
|
|---|
| 1248 | {
|
|---|
| 1249 | case IOCM_GET_UNIT_STATUS:
|
|---|
| 1250 | add_workspace(pIorb)->idempotent = 1;
|
|---|
| 1251 | ahci_unit_ready(vIorb, pIorb);
|
|---|
| 1252 | break;
|
|---|
| 1253 |
|
|---|
| 1254 | default:
|
|---|
| 1255 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 1256 | iorb_done(vIorb, pIorb);
|
|---|
| 1257 | }
|
|---|
| 1258 | }
|
|---|
| 1259 |
|
|---|
| 1260 | /******************************************************************************
|
|---|
| 1261 | * Handle IOCC_ADAPTER_PASSTHROUGH requests.
|
|---|
| 1262 | */
|
|---|
| 1263 | void iocc_adapter_passthru(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1264 | {
|
|---|
| 1265 | switch (pIorb->CommandModifier)
|
|---|
| 1266 | {
|
|---|
| 1267 | case IOCM_EXECUTE_CDB:
|
|---|
| 1268 | add_workspace(pIorb)->idempotent = 0;
|
|---|
| 1269 | ahci_execute_cdb(vIorb, pIorb);
|
|---|
| 1270 | break;
|
|---|
| 1271 |
|
|---|
| 1272 | case IOCM_EXECUTE_ATA:
|
|---|
| 1273 | add_workspace(pIorb)->idempotent = 0;
|
|---|
| 1274 | ahci_execute_ata(vIorb, pIorb);
|
|---|
| 1275 | break;
|
|---|
| 1276 |
|
|---|
| 1277 | default:
|
|---|
| 1278 | iorb_seterr(pIorb, IOERR_CMD_NOT_SUPPORTED);
|
|---|
| 1279 | iorb_done(vIorb, pIorb);
|
|---|
| 1280 | }
|
|---|
| 1281 | }
|
|---|
| 1282 |
|
|---|
| 1283 | /******************************************************************************
|
|---|
| 1284 | * Add an IORB to the specified queue. This function must be called with the
|
|---|
| 1285 | * adapter-level spinlock aquired.
|
|---|
| 1286 | */
|
|---|
| 1287 | void iorb_queue_add(IORB_QUEUE *queue, IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1288 | {
|
|---|
| 1289 | if (iorb_priority(pIorb)
|
|---|
| 1290 | {
|
|---|
| 1291 | /* priority IORB; insert at first position */
|
|---|
| 1292 | pIorb->pNxtIORB = queue->vRoot;
|
|---|
| 1293 | queue->vRoot = vIorb;
|
|---|
| 1294 | }
|
|---|
| 1295 | else
|
|---|
| 1296 | {
|
|---|
| 1297 | /* append IORB to end of queue */
|
|---|
| 1298 | pIorb->pNxtIORB = FAR16NULL;
|
|---|
| 1299 |
|
|---|
| 1300 | if (queue->vRoot == FAR16NULL)
|
|---|
| 1301 | {
|
|---|
| 1302 | queue->vRoot = vIorb;
|
|---|
| 1303 | }
|
|---|
| 1304 | else
|
|---|
| 1305 | {
|
|---|
| 1306 | ((IORBH *)Far16ToFlat(queue->vTail))->pNxtIORB = vIorb;
|
|---|
| 1307 | }
|
|---|
| 1308 | queue->vTail = vIorb;
|
|---|
| 1309 | }
|
|---|
| 1310 |
|
|---|
| 1311 | #ifdef DEBUG
|
|---|
| 1312 | if (D32g_DbgLevel)
|
|---|
| 1313 | {
|
|---|
| 1314 | /* determine queue type (local, driver, abort or port) and minimum debug
|
|---|
| 1315 | * level; otherwise, queue debug prints can become really confusing.
|
|---|
| 1316 | */
|
|---|
| 1317 | char *queue_type;
|
|---|
| 1318 | int min_debug = 7;
|
|---|
| 1319 |
|
|---|
| 1320 | if ((u32)queue >> 16 == (u32)&queue >> 16) /* DAZ this is bogus */
|
|---|
| 1321 | {
|
|---|
| 1322 | /* this queue is on the stack */
|
|---|
| 1323 | queue_type = "local";
|
|---|
| 1324 | min_debug = 8;
|
|---|
| 1325 | }
|
|---|
| 1326 | else if (queue == &driver_queue)
|
|---|
| 1327 | {
|
|---|
| 1328 | queue_type = "driver";
|
|---|
| 1329 | }
|
|---|
| 1330 | else if (queue == &abort_queue)
|
|---|
| 1331 | {
|
|---|
| 1332 | queue_type = "abort";
|
|---|
| 1333 | min_debug = 8;
|
|---|
| 1334 | }
|
|---|
| 1335 | else
|
|---|
| 1336 | {
|
|---|
| 1337 | queue_type = "port";
|
|---|
| 1338 | }
|
|---|
| 1339 |
|
|---|
| 1340 | DPRINTF(min_debug,"IORB %x queued (cmd=%d/%d queue=%x [%s], timeout=%d)\n",
|
|---|
| 1341 | vIorb, pIorb->CommandCode, pIorb->CommandModifier, queue, queue_type,
|
|---|
| 1342 | pIorb->Timeout);
|
|---|
| 1343 | }
|
|---|
| 1344 | #endif
|
|---|
| 1345 | }
|
|---|
| 1346 |
|
|---|
| 1347 | /******************************************************************************
|
|---|
| 1348 | * Remove an IORB from the specified queue. This function must be called with
|
|---|
| 1349 | * the adapter-level spinlock aquired.
|
|---|
| 1350 | */
|
|---|
| 1351 | int iorb_queue_del(IORB_QUEUE *queue, IORBH FAR16DATA *vIorb)
|
|---|
| 1352 | {
|
|---|
| 1353 | IORBH FAR16DATA *_vIorb;
|
|---|
| 1354 | IORBH FAR16DATA *_vPrev = FAR16NULL;
|
|---|
| 1355 | int found = 0;
|
|---|
| 1356 |
|
|---|
| 1357 | for (_vIorb = queue->vRoot; _vIorb != FAR16NULL; )
|
|---|
| 1358 | {
|
|---|
| 1359 | IORBH *_pIorb = Far16ToFlat(_vIorb);
|
|---|
| 1360 | if (_vIorb == vIorb)
|
|---|
| 1361 | {
|
|---|
| 1362 | /* found the IORB to be removed */
|
|---|
| 1363 | if (_vPrev != FAR16NULL)
|
|---|
| 1364 | {
|
|---|
| 1365 | ((IORBH*)Far16ToFlat(_vPrev))->pNxtIORB = _pIorb->pNxtIORB;
|
|---|
| 1366 | }
|
|---|
| 1367 | else
|
|---|
| 1368 | {
|
|---|
| 1369 | queue->vRoot = _pIorb->pNxtIORB;
|
|---|
| 1370 | }
|
|---|
| 1371 | if (_vIorb == queue->vTail)
|
|---|
| 1372 | {
|
|---|
| 1373 | queue->vTail = _vPrev;
|
|---|
| 1374 | }
|
|---|
| 1375 | found = 1;
|
|---|
| 1376 | break;
|
|---|
| 1377 | }
|
|---|
| 1378 | _vPrev = _vIorb;
|
|---|
| 1379 | _vIorb = _pIorb->pNxtIORB;
|
|---|
| 1380 | }
|
|---|
| 1381 |
|
|---|
| 1382 | #ifdef DEBUG
|
|---|
| 1383 | if (found)
|
|---|
| 1384 | {
|
|---|
| 1385 | DPRINTF(8,"IORB %x removed (queue = %x)\n", vIorb, queue);
|
|---|
| 1386 | }
|
|---|
| 1387 | else
|
|---|
| 1388 | {
|
|---|
| 1389 | DPRINTF(2,"IORB %x not found in queue %x\n", vIorb, queue);
|
|---|
| 1390 | }
|
|---|
| 1391 | #endif
|
|---|
| 1392 |
|
|---|
| 1393 | return(!found);
|
|---|
| 1394 | }
|
|---|
| 1395 |
|
|---|
| 1396 | /******************************************************************************
|
|---|
| 1397 | * Set the error code in the specified IORB
|
|---|
| 1398 | *
|
|---|
| 1399 | * NOTE: This function does *not* call iorb_done(). It merely sets the IORB
|
|---|
| 1400 | * status to the specified error code.
|
|---|
| 1401 | */
|
|---|
| 1402 | void iorb_seterr(IORBH *pIorb, USHORT error_code)
|
|---|
| 1403 | {
|
|---|
| 1404 | pIorb->ErrorCode = error_code;
|
|---|
| 1405 | pIorb->Status |= IORB_ERROR;
|
|---|
| 1406 | }
|
|---|
| 1407 |
|
|---|
| 1408 | /******************************************************************************
|
|---|
| 1409 | * Mark the specified IORB as done and notify the asynchronous post function,
|
|---|
| 1410 | * if any. The IORB is also removed from the corresponding IORB queue.
|
|---|
| 1411 | *
|
|---|
| 1412 | * NOTES: This function does not clear the Status field; it merely adds the
|
|---|
| 1413 | * IORB_DONE flag.
|
|---|
| 1414 | *
|
|---|
| 1415 | * This function is expected to be called *without* the corresponding
|
|---|
| 1416 | * driver-level drv_lock aquired. It will aquire the spinlock before
|
|---|
| 1417 | * updating the IORB queue and release it before notifying the upstream
|
|---|
| 1418 | * code in order to prevent deadlocks.
|
|---|
| 1419 | *
|
|---|
| 1420 | * Due to this logic, this function is only good for simple task-time
|
|---|
| 1421 | * completions. Functions working on lists of IORBs (such as interrupt
|
|---|
| 1422 | * handlers or context hooks) should call iorb_complete() directly and
|
|---|
| 1423 | * implement their own logic for removing the IORB from the port queue.
|
|---|
| 1424 | * See abort_ctxhook() for an example.
|
|---|
| 1425 | */
|
|---|
| 1426 | void iorb_done(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1427 | {
|
|---|
| 1428 | int a = iorb_unit_adapter(pIorb);
|
|---|
| 1429 | int p = iorb_unit_port(pIorb);
|
|---|
| 1430 |
|
|---|
| 1431 | /* remove IORB from corresponding queue */
|
|---|
| 1432 | spin_lock(drv_lock);
|
|---|
| 1433 | if (iorb_driver_level(pIorb))
|
|---|
| 1434 | {
|
|---|
| 1435 | iorb_queue_del(&driver_queue, vIorb);
|
|---|
| 1436 | }
|
|---|
| 1437 | else
|
|---|
| 1438 | {
|
|---|
| 1439 | iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, vIorb);
|
|---|
| 1440 | }
|
|---|
| 1441 | aws_free(add_workspace(pIorb));
|
|---|
| 1442 | spin_unlock(drv_lock);
|
|---|
| 1443 |
|
|---|
| 1444 | iorb_complete(vIorb, pIorb);
|
|---|
| 1445 | }
|
|---|
| 1446 |
|
|---|
| 1447 | /******************************************************************************
|
|---|
| 1448 | * Complete an IORB. This should be called without the adapter-level spinlock
|
|---|
| 1449 | * to allow the IORB completion routine to perform whatever processing it
|
|---|
| 1450 | * requires. This implies that the IORB should no longer be in any global
|
|---|
| 1451 | * queue because the IORB completion routine may well reuse the IORB and send
|
|---|
| 1452 | * the next request to us before even returning from this function.
|
|---|
| 1453 | */
|
|---|
| 1454 | void iorb_complete(IORBH FAR16DATA *vIorb, IORBH *pIorb)
|
|---|
| 1455 | {
|
|---|
| 1456 | pIorb->Status |= IORB_DONE;
|
|---|
| 1457 |
|
|---|
| 1458 | DPRINTF(7,"IORB %x complete status=0x%04x error=0x%04x\n",
|
|---|
| 1459 | vIorb, pIorb->Status, pIorb->ErrorCode);
|
|---|
| 1460 |
|
|---|
| 1461 | if (pIorb->RequestControl & IORB_ASYNC_POST)
|
|---|
| 1462 | {
|
|---|
| 1463 | Dev32Help_CallFar16((PFNFAR16)pIorb->NotifyAddress, vIorb);
|
|---|
| 1464 | }
|
|---|
| 1465 | }
|
|---|
| 1466 |
|
|---|
| 1467 | /******************************************************************************
|
|---|
| 1468 | * Requeue the specified IORB such that it will be sent downstream for
|
|---|
| 1469 | * processing again. This includes freeing all resources currently allocated
|
|---|
| 1470 | * (timer, buffer, ...) and resetting the flags to 0. The driver-level
|
|---|
| 1471 | * spinlock must be aquired when calling this function.
|
|---|
| 1472 | *
|
|---|
| 1473 | * The following flags are preserved:
|
|---|
| 1474 | * - no_ncq
|
|---|
| 1475 | */
|
|---|
| 1476 | void iorb_requeue(IORBH *pIorb)
|
|---|
| 1477 | {
|
|---|
| 1478 | ADD_WORKSPACE *aws = add_workspace(pIorb);
|
|---|
| 1479 | u16 no_ncq = aws->no_ncq;
|
|---|
| 1480 | u16 unaligned = aws->unaligned;
|
|---|
| 1481 | u16 retries = aws->retries;
|
|---|
| 1482 |
|
|---|
| 1483 | aws_free(aws);
|
|---|
| 1484 | memset(aws, 0x00, sizeof(*aws));
|
|---|
| 1485 |
|
|---|
| 1486 | aws->no_ncq = no_ncq;
|
|---|
| 1487 | aws->unaligned = unaligned;
|
|---|
| 1488 | aws->retries = retries;
|
|---|
| 1489 | }
|
|---|
| 1490 |
|
|---|
| 1491 | /******************************************************************************
|
|---|
| 1492 | * Free resources in ADD workspace (timer, buffer, ...). This function should
|
|---|
| 1493 | * be called with the spinlock held to prevent race conditions.
|
|---|
| 1494 | */
|
|---|
| 1495 | void aws_free(ADD_WORKSPACE *aws)
|
|---|
| 1496 | {
|
|---|
| 1497 | if (aws->timer != 0)
|
|---|
| 1498 | {
|
|---|
| 1499 | Timer_CancelTimer(aws->timer);
|
|---|
| 1500 | aws->timer = 0;
|
|---|
| 1501 | }
|
|---|
| 1502 |
|
|---|
| 1503 | if (aws->buf != NULL)
|
|---|
| 1504 | {
|
|---|
| 1505 | MemFree(aws->buf);
|
|---|
| 1506 | aws->buf = NULL;
|
|---|
| 1507 | }
|
|---|
| 1508 | }
|
|---|
| 1509 |
|
|---|
| 1510 | /******************************************************************************
|
|---|
| 1511 | * Lock the adapter, waiting for availability if necessary. This is expected
|
|---|
| 1512 | * to be called at task/request time without the driver-level spinlock
|
|---|
| 1513 | * aquired. Don't call at interrupt time.
|
|---|
| 1514 | */
|
|---|
| 1515 | void lock_adapter(AD_INFO *ai)
|
|---|
| 1516 | {
|
|---|
| 1517 | TIMER Timer;
|
|---|
| 1518 |
|
|---|
| 1519 | spin_lock(drv_lock);
|
|---|
| 1520 | while (ai->busy)
|
|---|
| 1521 | {
|
|---|
| 1522 | spin_unlock(drv_lock);
|
|---|
| 1523 | TimerInit(&Timer, 250);
|
|---|
| 1524 | while (!TimerCheckAndBlock(&Timer));
|
|---|
| 1525 | spin_lock(drv_lock);
|
|---|
| 1526 | }
|
|---|
| 1527 | ai->busy = 1;
|
|---|
| 1528 | spin_unlock(drv_lock);
|
|---|
| 1529 | }
|
|---|
| 1530 |
|
|---|
| 1531 | /******************************************************************************
|
|---|
| 1532 | * Unlock adapter (i.e. reset busy flag)
|
|---|
| 1533 | */
|
|---|
| 1534 | void unlock_adapter(AD_INFO *ai)
|
|---|
| 1535 | {
|
|---|
| 1536 | ai->busy = 0;
|
|---|
| 1537 | }
|
|---|
| 1538 |
|
|---|
| 1539 | /******************************************************************************
|
|---|
| 1540 | * Timeout handler for I/O commands. Since timeout handling can involve
|
|---|
| 1541 | * lengthy operations like port resets, the main code is located in a
|
|---|
| 1542 | * separate function which is invoked via a context hook.
|
|---|
| 1543 | */
|
|---|
| 1544 | void __syscall timeout_callback(ULONG timer_handle, ULONG p1)
|
|---|
| 1545 | {
|
|---|
| 1546 | IORBH FAR16DATA *vIorb = (IORBH FAR16DATA *)CastULONGToFar16(p1);
|
|---|
| 1547 | IORBH *pIorb = Far16ToFlat(vIorb);
|
|---|
| 1548 | int a = iorb_unit_adapter(pIorb);
|
|---|
| 1549 | int p = iorb_unit_port(pIorb);
|
|---|
| 1550 |
|
|---|
| 1551 | Timer_CancelTimer(timer_handle);
|
|---|
| 1552 | dprintf(0,"timeout for IORB %x\n", vIorb);
|
|---|
| 1553 |
|
|---|
| 1554 | /* Move the timed-out IORB to the abort queue. Since it's possible that the
|
|---|
| 1555 | * IORB has completed after the timeout has expired but before we got to
|
|---|
| 1556 | * this line of code, we'll check the return code of iorb_queue_del(): If it
|
|---|
| 1557 | * returns an error, the IORB must have completed a few microseconds ago and
|
|---|
| 1558 | * there is no timeout.
|
|---|
| 1559 | */
|
|---|
| 1560 | spin_lock(drv_lock);
|
|---|
| 1561 | if (iorb_queue_del(&ad_infos[a].ports[p].iorb_queue, vIorb) == 0)
|
|---|
| 1562 | {
|
|---|
| 1563 | iorb_queue_add(&abort_queue, vIorb, pIorb);
|
|---|
| 1564 | pIorb->ErrorCode = IOERR_ADAPTER_TIMEOUT;
|
|---|
| 1565 | }
|
|---|
| 1566 | spin_unlock(drv_lock);
|
|---|
| 1567 |
|
|---|
| 1568 | /* Trigger abort processing function. We don't really care whether this
|
|---|
| 1569 | * succeeds because the only reason why it would fail should be multiple
|
|---|
| 1570 | * calls to DevHelp_ArmCtxHook() before the context hook had a chance to
|
|---|
| 1571 | * start executing, which leaves two scenarios:
|
|---|
| 1572 | *
|
|---|
| 1573 | * - We succeded in arming the context hook. Fine.
|
|---|
| 1574 | *
|
|---|
| 1575 | * - We armed the context hook a second time before it had a chance to
|
|---|
| 1576 | * start executing. In this case, the already scheduled context hook
|
|---|
| 1577 | * will process our IORB as well.
|
|---|
| 1578 | */
|
|---|
| 1579 | KernArmHook(reset_ctxhook_h, 0, 0);
|
|---|
| 1580 |
|
|---|
| 1581 | /* Set up a watchdog timer which calls the context hook manually in case
|
|---|
| 1582 | * some kernel thread is looping around the IORB_COMPLETE status bit
|
|---|
| 1583 | * without yielding the CPU (kernel threads don't preempt). This shouldn't
|
|---|
| 1584 | * happen per design because kernel threads are supposed to yield but it
|
|---|
| 1585 | * does in the early boot phase.
|
|---|
| 1586 | */
|
|---|
| 1587 | Timer_StartTimerMS(&th_reset_watchdog, 5000, reset_watchdog, 0);
|
|---|
| 1588 | }
|
|---|
| 1589 |
|
|---|
| 1590 | /******************************************************************************
|
|---|
| 1591 | * Reset handler watchdog. If a timeout occurs, a context hook is armed which
|
|---|
| 1592 | * will execute as soon as a kernel thread yields the CPU. However, some
|
|---|
| 1593 | * kernel components won't yield the CPU during the early boot phase and the
|
|---|
| 1594 | * only way to kick some sense into those components is to run the context
|
|---|
| 1595 | * hook right inside this timer callback. Not exactly pretty, especially
|
|---|
| 1596 | * considering the fact that context hooks were implemented to prevent running
|
|---|
| 1597 | * lengthy operations like a port reset at interrupt time, but without this
|
|---|
| 1598 | * watchdog mechanism we run the risk of getting completely stalled by device
|
|---|
| 1599 | * problems during the early boot phase.
|
|---|
| 1600 | */
|
|---|
| 1601 | void __syscall reset_watchdog(ULONG timer_handle, ULONG p1)
|
|---|
| 1602 | {
|
|---|
| 1603 | /* reset watchdog timer */
|
|---|
| 1604 | Timer_CancelTimer(timer_handle);
|
|---|
| 1605 | dprintf(0,"reset watchdog invoked\n");
|
|---|
| 1606 |
|
|---|
| 1607 | /* call context hook manually */
|
|---|
| 1608 | reset_ctxhook(0);
|
|---|
| 1609 | }
|
|---|
| 1610 |
|
|---|
| 1611 | /******************************************************************************
|
|---|
| 1612 | * Add unit info to ADAPTERINFO array (IOCC_GET_DEVICE_TABLE requests). The
|
|---|
| 1613 | * adapter info array in the device table, dt->pAdapter[], is expected to be
|
|---|
| 1614 | * initialized for the specified index (dt_ai).
|
|---|
| 1615 | *
|
|---|
| 1616 | * Please note that the device table adapter index, dta, is not always equal
|
|---|
| 1617 | * to the physical adapter index, a: if SCSI emulation has been activated, the
|
|---|
| 1618 | * last reported adapter is a virtual SCSI adapter and the physical adapter
|
|---|
| 1619 | * indexes for those units are, of course, different from the device table
|
|---|
| 1620 | * index of the virtual SCSI adapter.
|
|---|
| 1621 | */
|
|---|
| 1622 | static int add_unit_info(IORB_CONFIGURATION *pIorb_conf, int dta,
|
|---|
| 1623 | int a, int p, int d, int scsi_id)
|
|---|
| 1624 | {
|
|---|
| 1625 | DEVICETABLE *pDt = Far16ToFlat(pIorb_conf->pDeviceTable);
|
|---|
| 1626 | ADAPTERINFO *pPtr;
|
|---|
| 1627 | UNITINFO *pUi;
|
|---|
| 1628 | AD_INFO *ai = ad_infos + a;
|
|---|
| 1629 |
|
|---|
| 1630 | pPtr = (ADAPTERINFO *)MakeFlatFromNear16(pIorb_conf->pDeviceTable, pDt->pAdapter[dta]);
|
|---|
| 1631 | //DPRINTF(2,"add_unit_info: ptr=%x dta=%x pAdapter[dta]=%x pDeviceTable=%x\n",
|
|---|
| 1632 | // ptr, dta, dt->pAdapter[dta], iorb_conf->pDeviceTable);
|
|---|
| 1633 |
|
|---|
| 1634 | pUi = &pPtr->UnitInfo[pPtr->AdapterUnits];
|
|---|
| 1635 |
|
|---|
| 1636 | if ((u32)(pUi + 1) - (u32)pDt > pIorb_conf->DeviceTableLen)
|
|---|
| 1637 | {
|
|---|
| 1638 | dprintf(0,"error: device table provided by DASD too small\n");
|
|---|
| 1639 | iorb_seterr(&pIorb_conf->iorbh, IOERR_CMD_SW_RESOURCE);
|
|---|
| 1640 | return(-1);
|
|---|
| 1641 | }
|
|---|
| 1642 |
|
|---|
| 1643 | if (ai->ports[p].devs[d].unit_info == NULL)
|
|---|
| 1644 | {
|
|---|
| 1645 | /* provide original information about this device (unit) */
|
|---|
| 1646 | memset(pUi, 0x00, sizeof(*pUi));
|
|---|
| 1647 | pUi->AdapterIndex = dta; /* device table adapter index */
|
|---|
| 1648 | pUi->UnitHandle = iorb_unit(a, p, d); /* physical adapter index */
|
|---|
| 1649 | pUi->UnitIndex = pPtr->AdapterUnits;
|
|---|
| 1650 | pUi->UnitType = ai->ports[p].devs[d].dev_type;
|
|---|
| 1651 | pUi->QueuingCount = ai->ports[p].devs[d].ncq_max;
|
|---|
| 1652 | if (ai->ports[p].devs[d].removable)
|
|---|
| 1653 | {
|
|---|
| 1654 | pUi->UnitFlags |= UF_REMOVABLE;
|
|---|
| 1655 | }
|
|---|
| 1656 | if (scsi_id > 0) {
|
|---|
| 1657 | /* set fake SCSI ID for this unit */
|
|---|
| 1658 | pUi->UnitSCSITargetID = scsi_id;
|
|---|
| 1659 | }
|
|---|
| 1660 | }
|
|---|
| 1661 | else
|
|---|
| 1662 | {
|
|---|
| 1663 | /* copy updated device (unit) information (IOCM_CHANGE_UNITINFO) */
|
|---|
| 1664 | memcpy(pUi, ai->ports[p].devs[d].unit_info, sizeof(*pUi));
|
|---|
| 1665 | }
|
|---|
| 1666 |
|
|---|
| 1667 | pPtr->AdapterUnits++;
|
|---|
| 1668 | return(0);
|
|---|
| 1669 | }
|
|---|
| 1670 |
|
|---|