Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Adaptec AIC7xxx device driver for Linux. |
| 3 | * |
| 4 | * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.c#235 $ |
| 5 | * |
| 6 | * Copyright (c) 1994 John Aycock |
| 7 | * The University of Calgary Department of Computer Science. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; see the file COPYING. If not, write to |
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | * Sources include the Adaptec 1740 driver (aha1740.c), the Ultrastor 24F |
| 24 | * driver (ultrastor.c), various Linux kernel source, the Adaptec EISA |
| 25 | * config file (!adp7771.cfg), the Adaptec AHA-2740A Series User's Guide, |
| 26 | * the Linux Kernel Hacker's Guide, Writing a SCSI Device Driver for Linux, |
| 27 | * the Adaptec 1542 driver (aha1542.c), the Adaptec EISA overlay file |
| 28 | * (adp7770.ovl), the Adaptec AHA-2740 Series Technical Reference Manual, |
| 29 | * the Adaptec AIC-7770 Data Book, the ANSI SCSI specification, the |
| 30 | * ANSI SCSI-2 specification (draft 10c), ... |
| 31 | * |
| 32 | * -------------------------------------------------------------------------- |
| 33 | * |
| 34 | * Modifications by Daniel M. Eischen (deischen@iworks.InterWorks.org): |
| 35 | * |
| 36 | * Substantially modified to include support for wide and twin bus |
| 37 | * adapters, DMAing of SCBs, tagged queueing, IRQ sharing, bug fixes, |
| 38 | * SCB paging, and other rework of the code. |
| 39 | * |
| 40 | * -------------------------------------------------------------------------- |
| 41 | * Copyright (c) 1994-2000 Justin T. Gibbs. |
| 42 | * Copyright (c) 2000-2001 Adaptec Inc. |
| 43 | * All rights reserved. |
| 44 | * |
| 45 | * Redistribution and use in source and binary forms, with or without |
| 46 | * modification, are permitted provided that the following conditions |
| 47 | * are met: |
| 48 | * 1. Redistributions of source code must retain the above copyright |
| 49 | * notice, this list of conditions, and the following disclaimer, |
| 50 | * without modification. |
| 51 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer |
| 52 | * substantially similar to the "NO WARRANTY" disclaimer below |
| 53 | * ("Disclaimer") and any redistribution must be conditioned upon |
| 54 | * including a substantially similar Disclaimer requirement for further |
| 55 | * binary redistribution. |
| 56 | * 3. Neither the names of the above-listed copyright holders nor the names |
| 57 | * of any contributors may be used to endorse or promote products derived |
| 58 | * from this software without specific prior written permission. |
| 59 | * |
| 60 | * Alternatively, this software may be distributed under the terms of the |
| 61 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 62 | * Software Foundation. |
| 63 | * |
| 64 | * NO WARRANTY |
| 65 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 66 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 67 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR |
| 68 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 69 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 70 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 71 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 72 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 73 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 74 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 75 | * POSSIBILITY OF SUCH DAMAGES. |
| 76 | * |
| 77 | *--------------------------------------------------------------------------- |
| 78 | * |
| 79 | * Thanks also go to (in alphabetical order) the following: |
| 80 | * |
| 81 | * Rory Bolt - Sequencer bug fixes |
| 82 | * Jay Estabrook - Initial DEC Alpha support |
| 83 | * Doug Ledford - Much needed abort/reset bug fixes |
| 84 | * Kai Makisara - DMAing of SCBs |
| 85 | * |
| 86 | * A Boot time option was also added for not resetting the scsi bus. |
| 87 | * |
| 88 | * Form: aic7xxx=extended |
| 89 | * aic7xxx=no_reset |
| 90 | * aic7xxx=verbose |
| 91 | * |
| 92 | * Daniel M. Eischen, deischen@iworks.InterWorks.org, 1/23/97 |
| 93 | * |
| 94 | * Id: aic7xxx.c,v 4.1 1997/06/12 08:23:42 deang Exp |
| 95 | */ |
| 96 | |
| 97 | /* |
| 98 | * Further driver modifications made by Doug Ledford <dledford@redhat.com> |
| 99 | * |
| 100 | * Copyright (c) 1997-1999 Doug Ledford |
| 101 | * |
| 102 | * These changes are released under the same licensing terms as the FreeBSD |
| 103 | * driver written by Justin Gibbs. Please see his Copyright notice above |
| 104 | * for the exact terms and conditions covering my changes as well as the |
| 105 | * warranty statement. |
| 106 | * |
| 107 | * Modifications made to the aic7xxx.c,v 4.1 driver from Dan Eischen include |
| 108 | * but are not limited to: |
| 109 | * |
| 110 | * 1: Import of the latest FreeBSD sequencer code for this driver |
| 111 | * 2: Modification of kernel code to accommodate different sequencer semantics |
| 112 | * 3: Extensive changes throughout kernel portion of driver to improve |
| 113 | * abort/reset processing and error hanndling |
| 114 | * 4: Other work contributed by various people on the Internet |
| 115 | * 5: Changes to printk information and verbosity selection code |
| 116 | * 6: General reliability related changes, especially in IRQ management |
| 117 | * 7: Modifications to the default probe/attach order for supported cards |
| 118 | * 8: SMP friendliness has been improved |
| 119 | * |
| 120 | */ |
| 121 | |
| 122 | #include "aic7xxx_osm.h" |
| 123 | #include "aic7xxx_inline.h" |
| 124 | #include <scsi/scsicam.h> |
| 92d161c | 2005-04-17 16:59:33 -0500 | [diff] [blame] | 125 | #include <scsi/scsi_transport.h> |
| 126 | #include <scsi/scsi_transport_spi.h> |
| 127 | |
| 128 | static struct scsi_transport_template *ahc_linux_transport_template = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * Include aiclib.c as part of our |
| 132 | * "module dependencies are hard" work around. |
| 133 | */ |
| 134 | #include "aiclib.c" |
| 135 | |
| 136 | #include <linux/init.h> /* __setup */ |
| 137 | |
| 138 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 139 | #include "sd.h" /* For geometry detection */ |
| 140 | #endif |
| 141 | |
| 142 | #include <linux/mm.h> /* For fetching system memory size */ |
| 143 | #include <linux/blkdev.h> /* For block_size() */ |
| 144 | #include <linux/delay.h> /* For ssleep/msleep */ |
| 145 | |
| 146 | /* |
| 147 | * Lock protecting manipulation of the ahc softc list. |
| 148 | */ |
| 149 | spinlock_t ahc_list_spinlock; |
| 150 | |
| 151 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 152 | /* For dynamic sglist size calculation. */ |
| 153 | u_int ahc_linux_nseg; |
| 154 | #endif |
| 155 | |
| 156 | /* |
| 157 | * Set this to the delay in seconds after SCSI bus reset. |
| 158 | * Note, we honor this only for the initial bus reset. |
| 159 | * The scsi error recovery code performs its own bus settle |
| 160 | * delay handling for error recovery actions. |
| 161 | */ |
| 162 | #ifdef CONFIG_AIC7XXX_RESET_DELAY_MS |
| 163 | #define AIC7XXX_RESET_DELAY CONFIG_AIC7XXX_RESET_DELAY_MS |
| 164 | #else |
| 165 | #define AIC7XXX_RESET_DELAY 5000 |
| 166 | #endif |
| 167 | |
| 168 | /* |
| 169 | * Control collection of SCSI transfer statistics for the /proc filesystem. |
| 170 | * |
| 171 | * NOTE: Do NOT enable this when running on kernels version 1.2.x and below. |
| 172 | * NOTE: This does affect performance since it has to maintain statistics. |
| 173 | */ |
| 174 | #ifdef CONFIG_AIC7XXX_PROC_STATS |
| 175 | #define AIC7XXX_PROC_STATS |
| 176 | #endif |
| 177 | |
| 178 | /* |
| 179 | * To change the default number of tagged transactions allowed per-device, |
| 180 | * add a line to the lilo.conf file like: |
| 181 | * append="aic7xxx=verbose,tag_info:{{32,32,32,32},{32,32,32,32}}" |
| 182 | * which will result in the first four devices on the first two |
| 183 | * controllers being set to a tagged queue depth of 32. |
| 184 | * |
| 185 | * The tag_commands is an array of 16 to allow for wide and twin adapters. |
| 186 | * Twin adapters will use indexes 0-7 for channel 0, and indexes 8-15 |
| 187 | * for channel 1. |
| 188 | */ |
| 189 | typedef struct { |
| 190 | uint8_t tag_commands[16]; /* Allow for wide/twin adapters. */ |
| 191 | } adapter_tag_info_t; |
| 192 | |
| 193 | /* |
| 194 | * Modify this as you see fit for your system. |
| 195 | * |
| 196 | * 0 tagged queuing disabled |
| 197 | * 1 <= n <= 253 n == max tags ever dispatched. |
| 198 | * |
| 199 | * The driver will throttle the number of commands dispatched to a |
| 200 | * device if it returns queue full. For devices with a fixed maximum |
| 201 | * queue depth, the driver will eventually determine this depth and |
| 202 | * lock it in (a console message is printed to indicate that a lock |
| 203 | * has occurred). On some devices, queue full is returned for a temporary |
| 204 | * resource shortage. These devices will return queue full at varying |
| 205 | * depths. The driver will throttle back when the queue fulls occur and |
| 206 | * attempt to slowly increase the depth over time as the device recovers |
| 207 | * from the resource shortage. |
| 208 | * |
| 209 | * In this example, the first line will disable tagged queueing for all |
| 210 | * the devices on the first probed aic7xxx adapter. |
| 211 | * |
| 212 | * The second line enables tagged queueing with 4 commands/LUN for IDs |
| 213 | * (0, 2-11, 13-15), disables tagged queueing for ID 12, and tells the |
| 214 | * driver to attempt to use up to 64 tags for ID 1. |
| 215 | * |
| 216 | * The third line is the same as the first line. |
| 217 | * |
| 218 | * The fourth line disables tagged queueing for devices 0 and 3. It |
| 219 | * enables tagged queueing for the other IDs, with 16 commands/LUN |
| 220 | * for IDs 1 and 4, 127 commands/LUN for ID 8, and 4 commands/LUN for |
| 221 | * IDs 2, 5-7, and 9-15. |
| 222 | */ |
| 223 | |
| 224 | /* |
| 225 | * NOTE: The below structure is for reference only, the actual structure |
| 226 | * to modify in order to change things is just below this comment block. |
| 227 | adapter_tag_info_t aic7xxx_tag_info[] = |
| 228 | { |
| 229 | {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, |
| 230 | {{4, 64, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 4, 4, 4}}, |
| 231 | {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, |
| 232 | {{0, 16, 4, 0, 16, 4, 4, 4, 127, 4, 4, 4, 4, 4, 4, 4}} |
| 233 | }; |
| 234 | */ |
| 235 | |
| 236 | #ifdef CONFIG_AIC7XXX_CMDS_PER_DEVICE |
| 237 | #define AIC7XXX_CMDS_PER_DEVICE CONFIG_AIC7XXX_CMDS_PER_DEVICE |
| 238 | #else |
| 239 | #define AIC7XXX_CMDS_PER_DEVICE AHC_MAX_QUEUE |
| 240 | #endif |
| 241 | |
| 242 | #define AIC7XXX_CONFIGED_TAG_COMMANDS { \ |
| 243 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \ |
| 244 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \ |
| 245 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \ |
| 246 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \ |
| 247 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \ |
| 248 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \ |
| 249 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE, \ |
| 250 | AIC7XXX_CMDS_PER_DEVICE, AIC7XXX_CMDS_PER_DEVICE \ |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * By default, use the number of commands specified by |
| 255 | * the users kernel configuration. |
| 256 | */ |
| 257 | static adapter_tag_info_t aic7xxx_tag_info[] = |
| 258 | { |
| 259 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 260 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 261 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 262 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 263 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 264 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 265 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 266 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 267 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 268 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 269 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 270 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 271 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 272 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 273 | {AIC7XXX_CONFIGED_TAG_COMMANDS}, |
| 274 | {AIC7XXX_CONFIGED_TAG_COMMANDS} |
| 275 | }; |
| 276 | |
| 277 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | * There should be a specific return value for this in scsi.h, but |
| 279 | * it seems that most drivers ignore it. |
| 280 | */ |
| 281 | #define DID_UNDERFLOW DID_ERROR |
| 282 | |
| 283 | void |
| 284 | ahc_print_path(struct ahc_softc *ahc, struct scb *scb) |
| 285 | { |
| 286 | printk("(scsi%d:%c:%d:%d): ", |
| 287 | ahc->platform_data->host->host_no, |
| 288 | scb != NULL ? SCB_GET_CHANNEL(ahc, scb) : 'X', |
| 289 | scb != NULL ? SCB_GET_TARGET(ahc, scb) : -1, |
| 290 | scb != NULL ? SCB_GET_LUN(scb) : -1); |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | * XXX - these options apply unilaterally to _all_ 274x/284x/294x |
| 295 | * cards in the system. This should be fixed. Exceptions to this |
| 296 | * rule are noted in the comments. |
| 297 | */ |
| 298 | |
| 299 | /* |
| 300 | * Skip the scsi bus reset. Non 0 make us skip the reset at startup. This |
| 301 | * has no effect on any later resets that might occur due to things like |
| 302 | * SCSI bus timeouts. |
| 303 | */ |
| 304 | static uint32_t aic7xxx_no_reset; |
| 305 | |
| 306 | /* |
| 307 | * Certain PCI motherboards will scan PCI devices from highest to lowest, |
| 308 | * others scan from lowest to highest, and they tend to do all kinds of |
| 309 | * strange things when they come into contact with PCI bridge chips. The |
| 310 | * net result of all this is that the PCI card that is actually used to boot |
| 311 | * the machine is very hard to detect. Most motherboards go from lowest |
| 312 | * PCI slot number to highest, and the first SCSI controller found is the |
| 313 | * one you boot from. The only exceptions to this are when a controller |
| 314 | * has its BIOS disabled. So, we by default sort all of our SCSI controllers |
| 315 | * from lowest PCI slot number to highest PCI slot number. We also force |
| 316 | * all controllers with their BIOS disabled to the end of the list. This |
| 317 | * works on *almost* all computers. Where it doesn't work, we have this |
| 318 | * option. Setting this option to non-0 will reverse the order of the sort |
| 319 | * to highest first, then lowest, but will still leave cards with their BIOS |
| 320 | * disabled at the very end. That should fix everyone up unless there are |
| 321 | * really strange cirumstances. |
| 322 | */ |
| 323 | static uint32_t aic7xxx_reverse_scan; |
| 324 | |
| 325 | /* |
| 326 | * Should we force EXTENDED translation on a controller. |
| 327 | * 0 == Use whatever is in the SEEPROM or default to off |
| 328 | * 1 == Use whatever is in the SEEPROM or default to on |
| 329 | */ |
| 330 | static uint32_t aic7xxx_extended; |
| 331 | |
| 332 | /* |
| 333 | * PCI bus parity checking of the Adaptec controllers. This is somewhat |
| 334 | * dubious at best. To my knowledge, this option has never actually |
| 335 | * solved a PCI parity problem, but on certain machines with broken PCI |
| 336 | * chipset configurations where stray PCI transactions with bad parity are |
| 337 | * the norm rather than the exception, the error messages can be overwelming. |
| 338 | * It's included in the driver for completeness. |
| 339 | * 0 = Shut off PCI parity check |
| 340 | * non-0 = reverse polarity pci parity checking |
| 341 | */ |
| 342 | static uint32_t aic7xxx_pci_parity = ~0; |
| 343 | |
| 344 | /* |
| 345 | * Certain newer motherboards have put new PCI based devices into the |
| 346 | * IO spaces that used to typically be occupied by VLB or EISA cards. |
| 347 | * This overlap can cause these newer motherboards to lock up when scanned |
| 348 | * for older EISA and VLB devices. Setting this option to non-0 will |
| 349 | * cause the driver to skip scanning for any VLB or EISA controllers and |
| 350 | * only support the PCI controllers. NOTE: this means that if the kernel |
| 351 | * os compiled with PCI support disabled, then setting this to non-0 |
| 352 | * would result in never finding any devices :) |
| 353 | */ |
| 354 | #ifndef CONFIG_AIC7XXX_PROBE_EISA_VL |
| 355 | uint32_t aic7xxx_probe_eisa_vl; |
| 356 | #else |
| 357 | uint32_t aic7xxx_probe_eisa_vl = ~0; |
| 358 | #endif |
| 359 | |
| 360 | /* |
| 361 | * There are lots of broken chipsets in the world. Some of them will |
| 362 | * violate the PCI spec when we issue byte sized memory writes to our |
| 363 | * controller. I/O mapped register access, if allowed by the given |
| 364 | * platform, will work in almost all cases. |
| 365 | */ |
| 366 | uint32_t aic7xxx_allow_memio = ~0; |
| 367 | |
| 368 | /* |
| 369 | * aic7xxx_detect() has been run, so register all device arrivals |
| 370 | * immediately with the system rather than deferring to the sorted |
| 371 | * attachment performed by aic7xxx_detect(). |
| 372 | */ |
| 373 | int aic7xxx_detect_complete; |
| 374 | |
| 375 | /* |
| 376 | * So that we can set how long each device is given as a selection timeout. |
| 377 | * The table of values goes like this: |
| 378 | * 0 - 256ms |
| 379 | * 1 - 128ms |
| 380 | * 2 - 64ms |
| 381 | * 3 - 32ms |
| 382 | * We default to 256ms because some older devices need a longer time |
| 383 | * to respond to initial selection. |
| 384 | */ |
| 385 | static uint32_t aic7xxx_seltime; |
| 386 | |
| 387 | /* |
| 388 | * Certain devices do not perform any aging on commands. Should the |
| 389 | * device be saturated by commands in one portion of the disk, it is |
| 390 | * possible for transactions on far away sectors to never be serviced. |
| 391 | * To handle these devices, we can periodically send an ordered tag to |
| 392 | * force all outstanding transactions to be serviced prior to a new |
| 393 | * transaction. |
| 394 | */ |
| 395 | uint32_t aic7xxx_periodic_otag; |
| 396 | |
| 397 | /* |
| 398 | * Module information and settable options. |
| 399 | */ |
| 400 | static char *aic7xxx = NULL; |
| 401 | |
| 402 | MODULE_AUTHOR("Maintainer: Justin T. Gibbs <gibbs@scsiguy.com>"); |
| 403 | MODULE_DESCRIPTION("Adaptec Aic77XX/78XX SCSI Host Bus Adapter driver"); |
| 404 | MODULE_LICENSE("Dual BSD/GPL"); |
| 405 | MODULE_VERSION(AIC7XXX_DRIVER_VERSION); |
| 406 | module_param(aic7xxx, charp, 0444); |
| 407 | MODULE_PARM_DESC(aic7xxx, |
| 408 | "period delimited, options string.\n" |
| 409 | " verbose Enable verbose/diagnostic logging\n" |
| 410 | " allow_memio Allow device registers to be memory mapped\n" |
| 411 | " debug Bitmask of debug values to enable\n" |
| 412 | " no_probe Toggle EISA/VLB controller probing\n" |
| 413 | " probe_eisa_vl Toggle EISA/VLB controller probing\n" |
| 414 | " no_reset Supress initial bus resets\n" |
| 415 | " extended Enable extended geometry on all controllers\n" |
| 416 | " periodic_otag Send an ordered tagged transaction\n" |
| 417 | " periodically to prevent tag starvation.\n" |
| 418 | " This may be required by some older disk\n" |
| 419 | " drives or RAID arrays.\n" |
| 420 | " reverse_scan Sort PCI devices highest Bus/Slot to lowest\n" |
| 421 | " tag_info:<tag_str> Set per-target tag depth\n" |
| 422 | " global_tag_depth:<int> Global tag depth for every target\n" |
| 423 | " on every bus\n" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | " seltime:<int> Selection Timeout\n" |
| 425 | " (0/256ms,1/128ms,2/64ms,3/32ms)\n" |
| 426 | "\n" |
| 427 | " Sample /etc/modprobe.conf line:\n" |
| 428 | " Toggle EISA/VLB probing\n" |
| 429 | " Set tag depth on Controller 1/Target 1 to 10 tags\n" |
| 430 | " Shorten the selection timeout to 128ms\n" |
| 431 | "\n" |
| 432 | " options aic7xxx 'aic7xxx=probe_eisa_vl.tag_info:{{}.{.10}}.seltime:1'\n" |
| 433 | ); |
| 434 | |
| 435 | static void ahc_linux_handle_scsi_status(struct ahc_softc *, |
| 436 | struct ahc_linux_device *, |
| 437 | struct scb *); |
| 438 | static void ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, |
| 439 | Scsi_Cmnd *cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | static void ahc_linux_sem_timeout(u_long arg); |
| 441 | static void ahc_linux_freeze_simq(struct ahc_softc *ahc); |
| 442 | static void ahc_linux_release_simq(u_long arg); |
| 443 | static void ahc_linux_dev_timed_unfreeze(u_long arg); |
| 444 | static int ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag); |
| 445 | static void ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc); |
| 446 | static void ahc_linux_size_nseg(void); |
| 447 | static void ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | static u_int ahc_linux_user_tagdepth(struct ahc_softc *ahc, |
| 449 | struct ahc_devinfo *devinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | static void ahc_linux_device_queue_depth(struct ahc_softc *ahc, |
| 451 | struct ahc_linux_device *dev); |
| 452 | static struct ahc_linux_target* ahc_linux_alloc_target(struct ahc_softc*, |
| 453 | u_int, u_int); |
| 454 | static void ahc_linux_free_target(struct ahc_softc*, |
| 455 | struct ahc_linux_target*); |
| 456 | static struct ahc_linux_device* ahc_linux_alloc_device(struct ahc_softc*, |
| 457 | struct ahc_linux_target*, |
| 458 | u_int); |
| 459 | static void ahc_linux_free_device(struct ahc_softc*, |
| 460 | struct ahc_linux_device*); |
| 461 | static void ahc_linux_run_device_queue(struct ahc_softc*, |
| 462 | struct ahc_linux_device*); |
| 463 | static void ahc_linux_setup_tag_info_global(char *p); |
| 464 | static aic_option_callback_t ahc_linux_setup_tag_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | static int aic7xxx_setup(char *s); |
| 466 | static int ahc_linux_next_unit(void); |
| 467 | static void ahc_runq_tasklet(unsigned long data); |
| 468 | static struct ahc_cmd *ahc_linux_run_complete_queue(struct ahc_softc *ahc); |
| 469 | |
| 470 | /********************************* Inlines ************************************/ |
| 471 | static __inline void ahc_schedule_runq(struct ahc_softc *ahc); |
| 472 | static __inline struct ahc_linux_device* |
| 473 | ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, |
| 474 | u_int target, u_int lun, int alloc); |
| 475 | static __inline void ahc_schedule_completeq(struct ahc_softc *ahc); |
| 476 | static __inline void ahc_linux_check_device_queue(struct ahc_softc *ahc, |
| 477 | struct ahc_linux_device *dev); |
| 478 | static __inline struct ahc_linux_device * |
| 479 | ahc_linux_next_device_to_run(struct ahc_softc *ahc); |
| 480 | static __inline void ahc_linux_run_device_queues(struct ahc_softc *ahc); |
| 481 | static __inline void ahc_linux_unmap_scb(struct ahc_softc*, struct scb*); |
| 482 | |
| 483 | static __inline int ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb, |
| 484 | struct ahc_dma_seg *sg, |
| 485 | dma_addr_t addr, bus_size_t len); |
| 486 | |
| 487 | static __inline void |
| 488 | ahc_schedule_completeq(struct ahc_softc *ahc) |
| 489 | { |
| 490 | if ((ahc->platform_data->flags & AHC_RUN_CMPLT_Q_TIMER) == 0) { |
| 491 | ahc->platform_data->flags |= AHC_RUN_CMPLT_Q_TIMER; |
| 492 | ahc->platform_data->completeq_timer.expires = jiffies; |
| 493 | add_timer(&ahc->platform_data->completeq_timer); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Must be called with our lock held. |
| 499 | */ |
| 500 | static __inline void |
| 501 | ahc_schedule_runq(struct ahc_softc *ahc) |
| 502 | { |
| 503 | tasklet_schedule(&ahc->platform_data->runq_tasklet); |
| 504 | } |
| 505 | |
| 506 | static __inline struct ahc_linux_device* |
| 507 | ahc_linux_get_device(struct ahc_softc *ahc, u_int channel, u_int target, |
| 508 | u_int lun, int alloc) |
| 509 | { |
| 510 | struct ahc_linux_target *targ; |
| 511 | struct ahc_linux_device *dev; |
| 512 | u_int target_offset; |
| 513 | |
| 514 | target_offset = target; |
| 515 | if (channel != 0) |
| 516 | target_offset += 8; |
| 517 | targ = ahc->platform_data->targets[target_offset]; |
| 518 | if (targ == NULL) { |
| 519 | if (alloc != 0) { |
| 520 | targ = ahc_linux_alloc_target(ahc, channel, target); |
| 521 | if (targ == NULL) |
| 522 | return (NULL); |
| 523 | } else |
| 524 | return (NULL); |
| 525 | } |
| 526 | dev = targ->devices[lun]; |
| 527 | if (dev == NULL && alloc != 0) |
| 528 | dev = ahc_linux_alloc_device(ahc, targ, lun); |
| 529 | return (dev); |
| 530 | } |
| 531 | |
| 532 | #define AHC_LINUX_MAX_RETURNED_ERRORS 4 |
| 533 | static struct ahc_cmd * |
| 534 | ahc_linux_run_complete_queue(struct ahc_softc *ahc) |
| 535 | { |
| 536 | struct ahc_cmd *acmd; |
| 537 | u_long done_flags; |
| 538 | int with_errors; |
| 539 | |
| 540 | with_errors = 0; |
| 541 | ahc_done_lock(ahc, &done_flags); |
| 542 | while ((acmd = TAILQ_FIRST(&ahc->platform_data->completeq)) != NULL) { |
| 543 | Scsi_Cmnd *cmd; |
| 544 | |
| 545 | if (with_errors > AHC_LINUX_MAX_RETURNED_ERRORS) { |
| 546 | /* |
| 547 | * Linux uses stack recursion to requeue |
| 548 | * commands that need to be retried. Avoid |
| 549 | * blowing out the stack by "spoon feeding" |
| 550 | * commands that completed with error back |
| 551 | * the operating system in case they are going |
| 552 | * to be retried. "ick" |
| 553 | */ |
| 554 | ahc_schedule_completeq(ahc); |
| 555 | break; |
| 556 | } |
| 557 | TAILQ_REMOVE(&ahc->platform_data->completeq, |
| 558 | acmd, acmd_links.tqe); |
| 559 | cmd = &acmd_scsi_cmd(acmd); |
| 560 | cmd->host_scribble = NULL; |
| 561 | if (ahc_cmd_get_transaction_status(cmd) != DID_OK |
| 562 | || (cmd->result & 0xFF) != SCSI_STATUS_OK) |
| 563 | with_errors++; |
| 564 | |
| 565 | cmd->scsi_done(cmd); |
| 566 | } |
| 567 | ahc_done_unlock(ahc, &done_flags); |
| 568 | return (acmd); |
| 569 | } |
| 570 | |
| 571 | static __inline void |
| 572 | ahc_linux_check_device_queue(struct ahc_softc *ahc, |
| 573 | struct ahc_linux_device *dev) |
| 574 | { |
| 575 | if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) != 0 |
| 576 | && dev->active == 0) { |
| 577 | dev->flags &= ~AHC_DEV_FREEZE_TIL_EMPTY; |
| 578 | dev->qfrozen--; |
| 579 | } |
| 580 | |
| 581 | if (TAILQ_FIRST(&dev->busyq) == NULL |
| 582 | || dev->openings == 0 || dev->qfrozen != 0) |
| 583 | return; |
| 584 | |
| 585 | ahc_linux_run_device_queue(ahc, dev); |
| 586 | } |
| 587 | |
| 588 | static __inline struct ahc_linux_device * |
| 589 | ahc_linux_next_device_to_run(struct ahc_softc *ahc) |
| 590 | { |
| 591 | |
| 592 | if ((ahc->flags & AHC_RESOURCE_SHORTAGE) != 0 |
| cb62402 | 2005-04-17 18:03:20 -0500 | [diff] [blame] | 593 | || (ahc->platform_data->qfrozen != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return (NULL); |
| 595 | return (TAILQ_FIRST(&ahc->platform_data->device_runq)); |
| 596 | } |
| 597 | |
| 598 | static __inline void |
| 599 | ahc_linux_run_device_queues(struct ahc_softc *ahc) |
| 600 | { |
| 601 | struct ahc_linux_device *dev; |
| 602 | |
| 603 | while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) { |
| 604 | TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links); |
| 605 | dev->flags &= ~AHC_DEV_ON_RUN_LIST; |
| 606 | ahc_linux_check_device_queue(ahc, dev); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | static __inline void |
| 611 | ahc_linux_unmap_scb(struct ahc_softc *ahc, struct scb *scb) |
| 612 | { |
| 613 | Scsi_Cmnd *cmd; |
| 614 | |
| 615 | cmd = scb->io_ctx; |
| 616 | ahc_sync_sglist(ahc, scb, BUS_DMASYNC_POSTWRITE); |
| 617 | if (cmd->use_sg != 0) { |
| 618 | struct scatterlist *sg; |
| 619 | |
| 620 | sg = (struct scatterlist *)cmd->request_buffer; |
| 621 | pci_unmap_sg(ahc->dev_softc, sg, cmd->use_sg, |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 622 | cmd->sc_data_direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } else if (cmd->request_bufflen != 0) { |
| 624 | pci_unmap_single(ahc->dev_softc, |
| 625 | scb->platform_data->buf_busaddr, |
| 626 | cmd->request_bufflen, |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 627 | cmd->sc_data_direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
| 631 | static __inline int |
| 632 | ahc_linux_map_seg(struct ahc_softc *ahc, struct scb *scb, |
| 633 | struct ahc_dma_seg *sg, dma_addr_t addr, bus_size_t len) |
| 634 | { |
| 635 | int consumed; |
| 636 | |
| 637 | if ((scb->sg_count + 1) > AHC_NSEG) |
| 638 | panic("Too few segs for dma mapping. " |
| 639 | "Increase AHC_NSEG\n"); |
| 640 | |
| 641 | consumed = 1; |
| 642 | sg->addr = ahc_htole32(addr & 0xFFFFFFFF); |
| 643 | scb->platform_data->xfer_len += len; |
| 644 | |
| 645 | if (sizeof(dma_addr_t) > 4 |
| 646 | && (ahc->flags & AHC_39BIT_ADDRESSING) != 0) |
| 647 | len |= (addr >> 8) & AHC_SG_HIGH_ADDR_MASK; |
| 648 | |
| 649 | sg->len = ahc_htole32(len); |
| 650 | return (consumed); |
| 651 | } |
| 652 | |
| 653 | /************************ Host template entry points *************************/ |
| 654 | static int ahc_linux_detect(Scsi_Host_Template *); |
| 655 | static int ahc_linux_queue(Scsi_Cmnd *, void (*)(Scsi_Cmnd *)); |
| 656 | static const char *ahc_linux_info(struct Scsi_Host *); |
| 657 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 658 | static int ahc_linux_slave_alloc(Scsi_Device *); |
| 659 | static int ahc_linux_slave_configure(Scsi_Device *); |
| 660 | static void ahc_linux_slave_destroy(Scsi_Device *); |
| 661 | #if defined(__i386__) |
| 662 | static int ahc_linux_biosparam(struct scsi_device*, |
| 663 | struct block_device*, |
| 664 | sector_t, int[]); |
| 665 | #endif |
| 666 | #else |
| 667 | static int ahc_linux_release(struct Scsi_Host *); |
| 668 | static void ahc_linux_select_queue_depth(struct Scsi_Host *host, |
| 669 | Scsi_Device *scsi_devs); |
| 670 | #if defined(__i386__) |
| 671 | static int ahc_linux_biosparam(Disk *, kdev_t, int[]); |
| 672 | #endif |
| 673 | #endif |
| 674 | static int ahc_linux_bus_reset(Scsi_Cmnd *); |
| 675 | static int ahc_linux_dev_reset(Scsi_Cmnd *); |
| 676 | static int ahc_linux_abort(Scsi_Cmnd *); |
| 677 | |
| 678 | /* |
| 679 | * Calculate a safe value for AHC_NSEG (as expressed through ahc_linux_nseg). |
| 680 | * |
| 681 | * In pre-2.5.X... |
| 682 | * The midlayer allocates an S/G array dynamically when a command is issued |
| 683 | * using SCSI malloc. This array, which is in an OS dependent format that |
| 684 | * must later be copied to our private S/G list, is sized to house just the |
| 685 | * number of segments needed for the current transfer. Since the code that |
| 686 | * sizes the SCSI malloc pool does not take into consideration fragmentation |
| 687 | * of the pool, executing transactions numbering just a fraction of our |
| 688 | * concurrent transaction limit with list lengths aproaching AHC_NSEG will |
| 689 | * quickly depleat the SCSI malloc pool of usable space. Unfortunately, the |
| 690 | * mid-layer does not properly handle this scsi malloc failures for the S/G |
| 691 | * array and the result can be a lockup of the I/O subsystem. We try to size |
| 692 | * our S/G list so that it satisfies our drivers allocation requirements in |
| 693 | * addition to avoiding fragmentation of the SCSI malloc pool. |
| 694 | */ |
| 695 | static void |
| 696 | ahc_linux_size_nseg(void) |
| 697 | { |
| 698 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 699 | u_int cur_size; |
| 700 | u_int best_size; |
| 701 | |
| 702 | /* |
| 703 | * The SCSI allocator rounds to the nearest 512 bytes |
| 704 | * an cannot allocate across a page boundary. Our algorithm |
| 705 | * is to start at 1K of scsi malloc space per-command and |
| 706 | * loop through all factors of the PAGE_SIZE and pick the best. |
| 707 | */ |
| 708 | best_size = 0; |
| 709 | for (cur_size = 1024; cur_size <= PAGE_SIZE; cur_size *= 2) { |
| 710 | u_int nseg; |
| 711 | |
| 712 | nseg = cur_size / sizeof(struct scatterlist); |
| 713 | if (nseg < AHC_LINUX_MIN_NSEG) |
| 714 | continue; |
| 715 | |
| 716 | if (best_size == 0) { |
| 717 | best_size = cur_size; |
| 718 | ahc_linux_nseg = nseg; |
| 719 | } else { |
| 720 | u_int best_rem; |
| 721 | u_int cur_rem; |
| 722 | |
| 723 | /* |
| 724 | * Compare the traits of the current "best_size" |
| 725 | * with the current size to determine if the |
| 726 | * current size is a better size. |
| 727 | */ |
| 728 | best_rem = best_size % sizeof(struct scatterlist); |
| 729 | cur_rem = cur_size % sizeof(struct scatterlist); |
| 730 | if (cur_rem < best_rem) { |
| 731 | best_size = cur_size; |
| 732 | ahc_linux_nseg = nseg; |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | #endif |
| 737 | } |
| 738 | |
| 739 | /* |
| 740 | * Try to detect an Adaptec 7XXX controller. |
| 741 | */ |
| 742 | static int |
| 743 | ahc_linux_detect(Scsi_Host_Template *template) |
| 744 | { |
| 745 | struct ahc_softc *ahc; |
| 746 | int found = 0; |
| 747 | |
| 748 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 749 | /* |
| 750 | * It is a bug that the upper layer takes |
| 751 | * this lock just prior to calling us. |
| 752 | */ |
| 753 | spin_unlock_irq(&io_request_lock); |
| 754 | #endif |
| 755 | |
| 756 | /* |
| 757 | * Sanity checking of Linux SCSI data structures so |
| 758 | * that some of our hacks^H^H^H^H^Hassumptions aren't |
| 759 | * violated. |
| 760 | */ |
| 761 | if (offsetof(struct ahc_cmd_internal, end) |
| 762 | > offsetof(struct scsi_cmnd, host_scribble)) { |
| 763 | printf("ahc_linux_detect: SCSI data structures changed.\n"); |
| 764 | printf("ahc_linux_detect: Unable to attach\n"); |
| 765 | return (0); |
| 766 | } |
| 767 | ahc_linux_size_nseg(); |
| 768 | /* |
| 769 | * If we've been passed any parameters, process them now. |
| 770 | */ |
| 771 | if (aic7xxx) |
| 772 | aic7xxx_setup(aic7xxx); |
| 773 | |
| 774 | template->proc_name = "aic7xxx"; |
| 775 | |
| 776 | /* |
| 777 | * Initialize our softc list lock prior to |
| 778 | * probing for any adapters. |
| 779 | */ |
| 780 | ahc_list_lockinit(); |
| 781 | |
| 782 | found = ahc_linux_pci_init(); |
| 783 | if (!ahc_linux_eisa_init()) |
| 784 | found++; |
| 785 | |
| 786 | /* |
| 787 | * Register with the SCSI layer all |
| 788 | * controllers we've found. |
| 789 | */ |
| 790 | TAILQ_FOREACH(ahc, &ahc_tailq, links) { |
| 791 | |
| 792 | if (ahc_linux_register_host(ahc, template) == 0) |
| 793 | found++; |
| 794 | } |
| 795 | |
| 796 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 797 | spin_lock_irq(&io_request_lock); |
| 798 | #endif |
| 799 | aic7xxx_detect_complete++; |
| 800 | |
| 801 | return (found); |
| 802 | } |
| 803 | |
| 804 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 805 | /* |
| 806 | * Free the passed in Scsi_Host memory structures prior to unloading the |
| 807 | * module. |
| 808 | */ |
| 809 | int |
| 810 | ahc_linux_release(struct Scsi_Host * host) |
| 811 | { |
| 812 | struct ahc_softc *ahc; |
| 813 | u_long l; |
| 814 | |
| 815 | ahc_list_lock(&l); |
| 816 | if (host != NULL) { |
| 817 | |
| 818 | /* |
| 819 | * We should be able to just perform |
| 820 | * the free directly, but check our |
| 821 | * list for extra sanity. |
| 822 | */ |
| 823 | ahc = ahc_find_softc(*(struct ahc_softc **)host->hostdata); |
| 824 | if (ahc != NULL) { |
| 825 | u_long s; |
| 826 | |
| 827 | ahc_lock(ahc, &s); |
| 828 | ahc_intr_enable(ahc, FALSE); |
| 829 | ahc_unlock(ahc, &s); |
| 830 | ahc_free(ahc); |
| 831 | } |
| 832 | } |
| 833 | ahc_list_unlock(&l); |
| 834 | return (0); |
| 835 | } |
| 836 | #endif |
| 837 | |
| 838 | /* |
| 839 | * Return a string describing the driver. |
| 840 | */ |
| 841 | static const char * |
| 842 | ahc_linux_info(struct Scsi_Host *host) |
| 843 | { |
| 844 | static char buffer[512]; |
| 845 | char ahc_info[256]; |
| 846 | char *bp; |
| 847 | struct ahc_softc *ahc; |
| 848 | |
| 849 | bp = &buffer[0]; |
| 850 | ahc = *(struct ahc_softc **)host->hostdata; |
| 851 | memset(bp, 0, sizeof(buffer)); |
| 852 | strcpy(bp, "Adaptec AIC7XXX EISA/VLB/PCI SCSI HBA DRIVER, Rev "); |
| 853 | strcat(bp, AIC7XXX_DRIVER_VERSION); |
| 854 | strcat(bp, "\n"); |
| 855 | strcat(bp, " <"); |
| 856 | strcat(bp, ahc->description); |
| 857 | strcat(bp, ">\n"); |
| 858 | strcat(bp, " "); |
| 859 | ahc_controller_info(ahc, ahc_info); |
| 860 | strcat(bp, ahc_info); |
| 861 | strcat(bp, "\n"); |
| 862 | |
| 863 | return (bp); |
| 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Queue an SCB to the controller. |
| 868 | */ |
| 869 | static int |
| 870 | ahc_linux_queue(Scsi_Cmnd * cmd, void (*scsi_done) (Scsi_Cmnd *)) |
| 871 | { |
| 872 | struct ahc_softc *ahc; |
| 873 | struct ahc_linux_device *dev; |
| 874 | u_long flags; |
| 875 | |
| 876 | ahc = *(struct ahc_softc **)cmd->device->host->hostdata; |
| 877 | |
| 878 | /* |
| 879 | * Save the callback on completion function. |
| 880 | */ |
| 881 | cmd->scsi_done = scsi_done; |
| 882 | |
| 883 | ahc_midlayer_entrypoint_lock(ahc, &flags); |
| 884 | |
| 885 | /* |
| 886 | * Close the race of a command that was in the process of |
| 887 | * being queued to us just as our simq was frozen. Let |
| 888 | * DV commands through so long as we are only frozen to |
| 889 | * perform DV. |
| 890 | */ |
| cb62402 | 2005-04-17 18:03:20 -0500 | [diff] [blame] | 891 | if (ahc->platform_data->qfrozen != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | |
| 893 | ahc_cmd_set_transaction_status(cmd, CAM_REQUEUE_REQ); |
| 894 | ahc_linux_queue_cmd_complete(ahc, cmd); |
| 895 | ahc_schedule_completeq(ahc); |
| 896 | ahc_midlayer_entrypoint_unlock(ahc, &flags); |
| 897 | return (0); |
| 898 | } |
| 899 | dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id, |
| 900 | cmd->device->lun, /*alloc*/TRUE); |
| 901 | if (dev == NULL) { |
| 902 | ahc_cmd_set_transaction_status(cmd, CAM_RESRC_UNAVAIL); |
| 903 | ahc_linux_queue_cmd_complete(ahc, cmd); |
| 904 | ahc_schedule_completeq(ahc); |
| 905 | ahc_midlayer_entrypoint_unlock(ahc, &flags); |
| 906 | printf("%s: aic7xxx_linux_queue - Unable to allocate device!\n", |
| 907 | ahc_name(ahc)); |
| 908 | return (0); |
| 909 | } |
| 910 | cmd->result = CAM_REQ_INPROG << 16; |
| 911 | TAILQ_INSERT_TAIL(&dev->busyq, (struct ahc_cmd *)cmd, acmd_links.tqe); |
| 912 | if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) { |
| 913 | TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links); |
| 914 | dev->flags |= AHC_DEV_ON_RUN_LIST; |
| 915 | ahc_linux_run_device_queues(ahc); |
| 916 | } |
| 917 | ahc_midlayer_entrypoint_unlock(ahc, &flags); |
| 918 | return (0); |
| 919 | } |
| 920 | |
| 921 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 922 | static int |
| 923 | ahc_linux_slave_alloc(Scsi_Device *device) |
| 924 | { |
| 925 | struct ahc_softc *ahc; |
| 926 | |
| 927 | ahc = *((struct ahc_softc **)device->host->hostdata); |
| 928 | if (bootverbose) |
| 929 | printf("%s: Slave Alloc %d\n", ahc_name(ahc), device->id); |
| 930 | return (0); |
| 931 | } |
| 932 | |
| 933 | static int |
| 934 | ahc_linux_slave_configure(Scsi_Device *device) |
| 935 | { |
| 936 | struct ahc_softc *ahc; |
| 937 | struct ahc_linux_device *dev; |
| 938 | u_long flags; |
| 939 | |
| 940 | ahc = *((struct ahc_softc **)device->host->hostdata); |
| 941 | if (bootverbose) |
| 942 | printf("%s: Slave Configure %d\n", ahc_name(ahc), device->id); |
| 943 | ahc_midlayer_entrypoint_lock(ahc, &flags); |
| 944 | /* |
| 945 | * Since Linux has attached to the device, configure |
| 946 | * it so we don't free and allocate the device |
| 947 | * structure on every command. |
| 948 | */ |
| 949 | dev = ahc_linux_get_device(ahc, device->channel, |
| 950 | device->id, device->lun, |
| 951 | /*alloc*/TRUE); |
| 952 | if (dev != NULL) { |
| 953 | dev->flags &= ~AHC_DEV_UNCONFIGURED; |
| 954 | dev->scsi_device = device; |
| 955 | ahc_linux_device_queue_depth(ahc, dev); |
| 956 | } |
| 957 | ahc_midlayer_entrypoint_unlock(ahc, &flags); |
| cb62402 | 2005-04-17 18:03:20 -0500 | [diff] [blame] | 958 | |
| 959 | /* Initial Domain Validation */ |
| 960 | if (!spi_initial_dv(device->sdev_target)) |
| 961 | spi_dv_device(device); |
| 962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | return (0); |
| 964 | } |
| 965 | |
| 966 | static void |
| 967 | ahc_linux_slave_destroy(Scsi_Device *device) |
| 968 | { |
| 969 | struct ahc_softc *ahc; |
| 970 | struct ahc_linux_device *dev; |
| 971 | u_long flags; |
| 972 | |
| 973 | ahc = *((struct ahc_softc **)device->host->hostdata); |
| 974 | if (bootverbose) |
| 975 | printf("%s: Slave Destroy %d\n", ahc_name(ahc), device->id); |
| 976 | ahc_midlayer_entrypoint_lock(ahc, &flags); |
| 977 | dev = ahc_linux_get_device(ahc, device->channel, |
| 978 | device->id, device->lun, |
| 979 | /*alloc*/FALSE); |
| 980 | /* |
| 981 | * Filter out "silly" deletions of real devices by only |
| 982 | * deleting devices that have had slave_configure() |
| 983 | * called on them. All other devices that have not |
| 984 | * been configured will automatically be deleted by |
| 985 | * the refcounting process. |
| 986 | */ |
| 987 | if (dev != NULL |
| 988 | && (dev->flags & AHC_DEV_SLAVE_CONFIGURED) != 0) { |
| 989 | dev->flags |= AHC_DEV_UNCONFIGURED; |
| 990 | if (TAILQ_EMPTY(&dev->busyq) |
| 991 | && dev->active == 0 |
| 992 | && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0) |
| 993 | ahc_linux_free_device(ahc, dev); |
| 994 | } |
| 995 | ahc_midlayer_entrypoint_unlock(ahc, &flags); |
| 996 | } |
| 997 | #else |
| 998 | /* |
| 999 | * Sets the queue depth for each SCSI device hanging |
| 1000 | * off the input host adapter. |
| 1001 | */ |
| 1002 | static void |
| 1003 | ahc_linux_select_queue_depth(struct Scsi_Host *host, Scsi_Device *scsi_devs) |
| 1004 | { |
| 1005 | Scsi_Device *device; |
| 1006 | Scsi_Device *ldev; |
| 1007 | struct ahc_softc *ahc; |
| 1008 | u_long flags; |
| 1009 | |
| 1010 | ahc = *((struct ahc_softc **)host->hostdata); |
| 1011 | ahc_lock(ahc, &flags); |
| 1012 | for (device = scsi_devs; device != NULL; device = device->next) { |
| 1013 | |
| 1014 | /* |
| 1015 | * Watch out for duplicate devices. This works around |
| 1016 | * some quirks in how the SCSI scanning code does its |
| 1017 | * device management. |
| 1018 | */ |
| 1019 | for (ldev = scsi_devs; ldev != device; ldev = ldev->next) { |
| 1020 | if (ldev->host == device->host |
| 1021 | && ldev->channel == device->channel |
| 1022 | && ldev->id == device->id |
| 1023 | && ldev->lun == device->lun) |
| 1024 | break; |
| 1025 | } |
| 1026 | /* Skip duplicate. */ |
| 1027 | if (ldev != device) |
| 1028 | continue; |
| 1029 | |
| 1030 | if (device->host == host) { |
| 1031 | struct ahc_linux_device *dev; |
| 1032 | |
| 1033 | /* |
| 1034 | * Since Linux has attached to the device, configure |
| 1035 | * it so we don't free and allocate the device |
| 1036 | * structure on every command. |
| 1037 | */ |
| 1038 | dev = ahc_linux_get_device(ahc, device->channel, |
| 1039 | device->id, device->lun, |
| 1040 | /*alloc*/TRUE); |
| 1041 | if (dev != NULL) { |
| 1042 | dev->flags &= ~AHC_DEV_UNCONFIGURED; |
| 1043 | dev->scsi_device = device; |
| 1044 | ahc_linux_device_queue_depth(ahc, dev); |
| 1045 | device->queue_depth = dev->openings |
| 1046 | + dev->active; |
| 1047 | if ((dev->flags & (AHC_DEV_Q_BASIC |
| 1048 | | AHC_DEV_Q_TAGGED)) == 0) { |
| 1049 | /* |
| 1050 | * We allow the OS to queue 2 untagged |
| 1051 | * transactions to us at any time even |
| 1052 | * though we can only execute them |
| 1053 | * serially on the controller/device. |
| 1054 | * This should remove some latency. |
| 1055 | */ |
| 1056 | device->queue_depth = 2; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | ahc_unlock(ahc, &flags); |
| 1062 | } |
| 1063 | #endif |
| 1064 | |
| 1065 | #if defined(__i386__) |
| 1066 | /* |
| 1067 | * Return the disk geometry for the given SCSI device. |
| 1068 | */ |
| 1069 | static int |
| 1070 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 1071 | ahc_linux_biosparam(struct scsi_device *sdev, struct block_device *bdev, |
| 1072 | sector_t capacity, int geom[]) |
| 1073 | { |
| 1074 | uint8_t *bh; |
| 1075 | #else |
| 1076 | ahc_linux_biosparam(Disk *disk, kdev_t dev, int geom[]) |
| 1077 | { |
| 1078 | struct scsi_device *sdev = disk->device; |
| 1079 | u_long capacity = disk->capacity; |
| 1080 | struct buffer_head *bh; |
| 1081 | #endif |
| 1082 | int heads; |
| 1083 | int sectors; |
| 1084 | int cylinders; |
| 1085 | int ret; |
| 1086 | int extended; |
| 1087 | struct ahc_softc *ahc; |
| 1088 | u_int channel; |
| 1089 | |
| 1090 | ahc = *((struct ahc_softc **)sdev->host->hostdata); |
| 1091 | channel = sdev->channel; |
| 1092 | |
| 1093 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 1094 | bh = scsi_bios_ptable(bdev); |
| 1095 | #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,17) |
| 1096 | bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, block_size(dev)); |
| 1097 | #else |
| 1098 | bh = bread(MKDEV(MAJOR(dev), MINOR(dev) & ~0xf), 0, 1024); |
| 1099 | #endif |
| 1100 | |
| 1101 | if (bh) { |
| 1102 | ret = scsi_partsize(bh, capacity, |
| 1103 | &geom[2], &geom[0], &geom[1]); |
| 1104 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 1105 | kfree(bh); |
| 1106 | #else |
| 1107 | brelse(bh); |
| 1108 | #endif |
| 1109 | if (ret != -1) |
| 1110 | return (ret); |
| 1111 | } |
| 1112 | heads = 64; |
| 1113 | sectors = 32; |
| 1114 | cylinders = aic_sector_div(capacity, heads, sectors); |
| 1115 | |
| 1116 | if (aic7xxx_extended != 0) |
| 1117 | extended = 1; |
| 1118 | else if (channel == 0) |
| 1119 | extended = (ahc->flags & AHC_EXTENDED_TRANS_A) != 0; |
| 1120 | else |
| 1121 | extended = (ahc->flags & AHC_EXTENDED_TRANS_B) != 0; |
| 1122 | if (extended && cylinders >= 1024) { |
| 1123 | heads = 255; |
| 1124 | sectors = 63; |
| 1125 | cylinders = aic_sector_div(capacity, heads, sectors); |
| 1126 | } |
| 1127 | geom[0] = heads; |
| 1128 | geom[1] = sectors; |
| 1129 | geom[2] = cylinders; |
| 1130 | return (0); |
| 1131 | } |
| 1132 | #endif |
| 1133 | |
| 1134 | /* |
| 1135 | * Abort the current SCSI command(s). |
| 1136 | */ |
| 1137 | static int |
| 1138 | ahc_linux_abort(Scsi_Cmnd *cmd) |
| 1139 | { |
| 1140 | int error; |
| 1141 | |
| 1142 | error = ahc_linux_queue_recovery_cmd(cmd, SCB_ABORT); |
| 1143 | if (error != 0) |
| 1144 | printf("aic7xxx_abort returns 0x%x\n", error); |
| 1145 | return (error); |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * Attempt to send a target reset message to the device that timed out. |
| 1150 | */ |
| 1151 | static int |
| 1152 | ahc_linux_dev_reset(Scsi_Cmnd *cmd) |
| 1153 | { |
| 1154 | int error; |
| 1155 | |
| 1156 | error = ahc_linux_queue_recovery_cmd(cmd, SCB_DEVICE_RESET); |
| 1157 | if (error != 0) |
| 1158 | printf("aic7xxx_dev_reset returns 0x%x\n", error); |
| 1159 | return (error); |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Reset the SCSI bus. |
| 1164 | */ |
| 1165 | static int |
| 1166 | ahc_linux_bus_reset(Scsi_Cmnd *cmd) |
| 1167 | { |
| 1168 | struct ahc_softc *ahc; |
| 1169 | u_long s; |
| 1170 | int found; |
| 1171 | |
| 1172 | ahc = *(struct ahc_softc **)cmd->device->host->hostdata; |
| 1173 | ahc_midlayer_entrypoint_lock(ahc, &s); |
| 1174 | found = ahc_reset_channel(ahc, cmd->device->channel + 'A', |
| 1175 | /*initiate reset*/TRUE); |
| 1176 | ahc_linux_run_complete_queue(ahc); |
| 1177 | ahc_midlayer_entrypoint_unlock(ahc, &s); |
| 1178 | |
| 1179 | if (bootverbose) |
| 1180 | printf("%s: SCSI bus reset delivered. " |
| 1181 | "%d SCBs aborted.\n", ahc_name(ahc), found); |
| 1182 | |
| 1183 | return SUCCESS; |
| 1184 | } |
| 1185 | |
| 1186 | Scsi_Host_Template aic7xxx_driver_template = { |
| 1187 | .module = THIS_MODULE, |
| 1188 | .name = "aic7xxx", |
| 1189 | .proc_info = ahc_linux_proc_info, |
| 1190 | .info = ahc_linux_info, |
| 1191 | .queuecommand = ahc_linux_queue, |
| 1192 | .eh_abort_handler = ahc_linux_abort, |
| 1193 | .eh_device_reset_handler = ahc_linux_dev_reset, |
| 1194 | .eh_bus_reset_handler = ahc_linux_bus_reset, |
| 1195 | #if defined(__i386__) |
| 1196 | .bios_param = ahc_linux_biosparam, |
| 1197 | #endif |
| 1198 | .can_queue = AHC_MAX_QUEUE, |
| 1199 | .this_id = -1, |
| 1200 | .cmd_per_lun = 2, |
| 1201 | .use_clustering = ENABLE_CLUSTERING, |
| 1202 | .slave_alloc = ahc_linux_slave_alloc, |
| 1203 | .slave_configure = ahc_linux_slave_configure, |
| 1204 | .slave_destroy = ahc_linux_slave_destroy, |
| 1205 | }; |
| 1206 | |
| 1207 | /**************************** Tasklet Handler *********************************/ |
| 1208 | |
| 1209 | /* |
| 1210 | * In 2.4.X and above, this routine is called from a tasklet, |
| 1211 | * so we must re-acquire our lock prior to executing this code. |
| 1212 | * In all prior kernels, ahc_schedule_runq() calls this routine |
| 1213 | * directly and ahc_schedule_runq() is called with our lock held. |
| 1214 | */ |
| 1215 | static void |
| 1216 | ahc_runq_tasklet(unsigned long data) |
| 1217 | { |
| 1218 | struct ahc_softc* ahc; |
| 1219 | struct ahc_linux_device *dev; |
| 1220 | u_long flags; |
| 1221 | |
| 1222 | ahc = (struct ahc_softc *)data; |
| 1223 | ahc_lock(ahc, &flags); |
| 1224 | while ((dev = ahc_linux_next_device_to_run(ahc)) != NULL) { |
| 1225 | |
| 1226 | TAILQ_REMOVE(&ahc->platform_data->device_runq, dev, links); |
| 1227 | dev->flags &= ~AHC_DEV_ON_RUN_LIST; |
| 1228 | ahc_linux_check_device_queue(ahc, dev); |
| 1229 | /* Yeild to our interrupt handler */ |
| 1230 | ahc_unlock(ahc, &flags); |
| 1231 | ahc_lock(ahc, &flags); |
| 1232 | } |
| 1233 | ahc_unlock(ahc, &flags); |
| 1234 | } |
| 1235 | |
| 1236 | /******************************** Macros **************************************/ |
| 1237 | #define BUILD_SCSIID(ahc, cmd) \ |
| 1238 | ((((cmd)->device->id << TID_SHIFT) & TID) \ |
| 1239 | | (((cmd)->device->channel == 0) ? (ahc)->our_id : (ahc)->our_id_b) \ |
| 1240 | | (((cmd)->device->channel == 0) ? 0 : TWIN_CHNLB)) |
| 1241 | |
| 1242 | /******************************** Bus DMA *************************************/ |
| 1243 | int |
| 1244 | ahc_dma_tag_create(struct ahc_softc *ahc, bus_dma_tag_t parent, |
| 1245 | bus_size_t alignment, bus_size_t boundary, |
| 1246 | dma_addr_t lowaddr, dma_addr_t highaddr, |
| 1247 | bus_dma_filter_t *filter, void *filterarg, |
| 1248 | bus_size_t maxsize, int nsegments, |
| 1249 | bus_size_t maxsegsz, int flags, bus_dma_tag_t *ret_tag) |
| 1250 | { |
| 1251 | bus_dma_tag_t dmat; |
| 1252 | |
| 1253 | dmat = malloc(sizeof(*dmat), M_DEVBUF, M_NOWAIT); |
| 1254 | if (dmat == NULL) |
| 1255 | return (ENOMEM); |
| 1256 | |
| 1257 | /* |
| 1258 | * Linux is very simplistic about DMA memory. For now don't |
| 1259 | * maintain all specification information. Once Linux supplies |
| 1260 | * better facilities for doing these operations, or the |
| 1261 | * needs of this particular driver change, we might need to do |
| 1262 | * more here. |
| 1263 | */ |
| 1264 | dmat->alignment = alignment; |
| 1265 | dmat->boundary = boundary; |
| 1266 | dmat->maxsize = maxsize; |
| 1267 | *ret_tag = dmat; |
| 1268 | return (0); |
| 1269 | } |
| 1270 | |
| 1271 | void |
| 1272 | ahc_dma_tag_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat) |
| 1273 | { |
| 1274 | free(dmat, M_DEVBUF); |
| 1275 | } |
| 1276 | |
| 1277 | int |
| 1278 | ahc_dmamem_alloc(struct ahc_softc *ahc, bus_dma_tag_t dmat, void** vaddr, |
| 1279 | int flags, bus_dmamap_t *mapp) |
| 1280 | { |
| 1281 | bus_dmamap_t map; |
| 1282 | |
| 1283 | map = malloc(sizeof(*map), M_DEVBUF, M_NOWAIT); |
| 1284 | if (map == NULL) |
| 1285 | return (ENOMEM); |
| 1286 | /* |
| 1287 | * Although we can dma data above 4GB, our |
| 1288 | * "consistent" memory is below 4GB for |
| 1289 | * space efficiency reasons (only need a 4byte |
| 1290 | * address). For this reason, we have to reset |
| 1291 | * our dma mask when doing allocations. |
| 1292 | */ |
| 1293 | if (ahc->dev_softc != NULL) |
| 1294 | if (pci_set_dma_mask(ahc->dev_softc, 0xFFFFFFFF)) { |
| 1295 | printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n"); |
| 1296 | kfree(map); |
| 1297 | return (ENODEV); |
| 1298 | } |
| 1299 | *vaddr = pci_alloc_consistent(ahc->dev_softc, |
| 1300 | dmat->maxsize, &map->bus_addr); |
| 1301 | if (ahc->dev_softc != NULL) |
| 1302 | if (pci_set_dma_mask(ahc->dev_softc, |
| 1303 | ahc->platform_data->hw_dma_mask)) { |
| 1304 | printk(KERN_WARNING "aic7xxx: No suitable DMA available.\n"); |
| 1305 | kfree(map); |
| 1306 | return (ENODEV); |
| 1307 | } |
| 1308 | if (*vaddr == NULL) |
| 1309 | return (ENOMEM); |
| 1310 | *mapp = map; |
| 1311 | return(0); |
| 1312 | } |
| 1313 | |
| 1314 | void |
| 1315 | ahc_dmamem_free(struct ahc_softc *ahc, bus_dma_tag_t dmat, |
| 1316 | void* vaddr, bus_dmamap_t map) |
| 1317 | { |
| 1318 | pci_free_consistent(ahc->dev_softc, dmat->maxsize, |
| 1319 | vaddr, map->bus_addr); |
| 1320 | } |
| 1321 | |
| 1322 | int |
| 1323 | ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map, |
| 1324 | void *buf, bus_size_t buflen, bus_dmamap_callback_t *cb, |
| 1325 | void *cb_arg, int flags) |
| 1326 | { |
| 1327 | /* |
| 1328 | * Assume for now that this will only be used during |
| 1329 | * initialization and not for per-transaction buffer mapping. |
| 1330 | */ |
| 1331 | bus_dma_segment_t stack_sg; |
| 1332 | |
| 1333 | stack_sg.ds_addr = map->bus_addr; |
| 1334 | stack_sg.ds_len = dmat->maxsize; |
| 1335 | cb(cb_arg, &stack_sg, /*nseg*/1, /*error*/0); |
| 1336 | return (0); |
| 1337 | } |
| 1338 | |
| 1339 | void |
| 1340 | ahc_dmamap_destroy(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map) |
| 1341 | { |
| 1342 | /* |
| 1343 | * The map may is NULL in our < 2.3.X implementation. |
| 1344 | * Now it's 2.6.5, but just in case... |
| 1345 | */ |
| 1346 | BUG_ON(map == NULL); |
| 1347 | free(map, M_DEVBUF); |
| 1348 | } |
| 1349 | |
| 1350 | int |
| 1351 | ahc_dmamap_unload(struct ahc_softc *ahc, bus_dma_tag_t dmat, bus_dmamap_t map) |
| 1352 | { |
| 1353 | /* Nothing to do */ |
| 1354 | return (0); |
| 1355 | } |
| 1356 | |
| 1357 | /********************* Platform Dependent Functions ***************************/ |
| 1358 | /* |
| 1359 | * Compare "left hand" softc with "right hand" softc, returning: |
| 1360 | * < 0 - lahc has a lower priority than rahc |
| 1361 | * 0 - Softcs are equal |
| 1362 | * > 0 - lahc has a higher priority than rahc |
| 1363 | */ |
| 1364 | int |
| 1365 | ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc) |
| 1366 | { |
| 1367 | int value; |
| 1368 | int rvalue; |
| 1369 | int lvalue; |
| 1370 | |
| 1371 | /* |
| 1372 | * Under Linux, cards are ordered as follows: |
| 1373 | * 1) VLB/EISA BIOS enabled devices sorted by BIOS address. |
| 1374 | * 2) PCI devices with BIOS enabled sorted by bus/slot/func. |
| 1375 | * 3) All remaining VLB/EISA devices sorted by ioport. |
| 1376 | * 4) All remaining PCI devices sorted by bus/slot/func. |
| 1377 | */ |
| 1378 | value = (lahc->flags & AHC_BIOS_ENABLED) |
| 1379 | - (rahc->flags & AHC_BIOS_ENABLED); |
| 1380 | if (value != 0) |
| 1381 | /* Controllers with BIOS enabled have a *higher* priority */ |
| 1382 | return (value); |
| 1383 | |
| 1384 | /* |
| 1385 | * Same BIOS setting, now sort based on bus type. |
| 1386 | * EISA and VL controllers sort together. EISA/VL |
| 1387 | * have higher priority than PCI. |
| 1388 | */ |
| 1389 | rvalue = (rahc->chip & AHC_BUS_MASK); |
| 1390 | if (rvalue == AHC_VL) |
| 1391 | rvalue = AHC_EISA; |
| 1392 | lvalue = (lahc->chip & AHC_BUS_MASK); |
| 1393 | if (lvalue == AHC_VL) |
| 1394 | lvalue = AHC_EISA; |
| 1395 | value = rvalue - lvalue; |
| 1396 | if (value != 0) |
| 1397 | return (value); |
| 1398 | |
| 1399 | /* Still equal. Sort by BIOS address, ioport, or bus/slot/func. */ |
| 1400 | switch (rvalue) { |
| 1401 | #ifdef CONFIG_PCI |
| 1402 | case AHC_PCI: |
| 1403 | { |
| 1404 | char primary_channel; |
| 1405 | |
| 1406 | if (aic7xxx_reverse_scan != 0) |
| 1407 | value = ahc_get_pci_bus(lahc->dev_softc) |
| 1408 | - ahc_get_pci_bus(rahc->dev_softc); |
| 1409 | else |
| 1410 | value = ahc_get_pci_bus(rahc->dev_softc) |
| 1411 | - ahc_get_pci_bus(lahc->dev_softc); |
| 1412 | if (value != 0) |
| 1413 | break; |
| 1414 | if (aic7xxx_reverse_scan != 0) |
| 1415 | value = ahc_get_pci_slot(lahc->dev_softc) |
| 1416 | - ahc_get_pci_slot(rahc->dev_softc); |
| 1417 | else |
| 1418 | value = ahc_get_pci_slot(rahc->dev_softc) |
| 1419 | - ahc_get_pci_slot(lahc->dev_softc); |
| 1420 | if (value != 0) |
| 1421 | break; |
| 1422 | /* |
| 1423 | * On multi-function devices, the user can choose |
| 1424 | * to have function 1 probed before function 0. |
| 1425 | * Give whichever channel is the primary channel |
| 1426 | * the highest priority. |
| 1427 | */ |
| 1428 | primary_channel = (lahc->flags & AHC_PRIMARY_CHANNEL) + 'A'; |
| 1429 | value = -1; |
| 1430 | if (lahc->channel == primary_channel) |
| 1431 | value = 1; |
| 1432 | break; |
| 1433 | } |
| 1434 | #endif |
| 1435 | case AHC_EISA: |
| 1436 | if ((rahc->flags & AHC_BIOS_ENABLED) != 0) { |
| 1437 | value = rahc->platform_data->bios_address |
| 1438 | - lahc->platform_data->bios_address; |
| 1439 | } else { |
| 1440 | value = rahc->bsh.ioport |
| 1441 | - lahc->bsh.ioport; |
| 1442 | } |
| 1443 | break; |
| 1444 | default: |
| 1445 | panic("ahc_softc_sort: invalid bus type"); |
| 1446 | } |
| 1447 | return (value); |
| 1448 | } |
| 1449 | |
| 1450 | static void |
| 1451 | ahc_linux_setup_tag_info_global(char *p) |
| 1452 | { |
| 1453 | int tags, i, j; |
| 1454 | |
| 1455 | tags = simple_strtoul(p + 1, NULL, 0) & 0xff; |
| 1456 | printf("Setting Global Tags= %d\n", tags); |
| 1457 | |
| 1458 | for (i = 0; i < NUM_ELEMENTS(aic7xxx_tag_info); i++) { |
| 1459 | for (j = 0; j < AHC_NUM_TARGETS; j++) { |
| 1460 | aic7xxx_tag_info[i].tag_commands[j] = tags; |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | static void |
| 1466 | ahc_linux_setup_tag_info(u_long arg, int instance, int targ, int32_t value) |
| 1467 | { |
| 1468 | |
| 1469 | if ((instance >= 0) && (targ >= 0) |
| 1470 | && (instance < NUM_ELEMENTS(aic7xxx_tag_info)) |
| 1471 | && (targ < AHC_NUM_TARGETS)) { |
| 1472 | aic7xxx_tag_info[instance].tag_commands[targ] = value & 0xff; |
| 1473 | if (bootverbose) |
| 1474 | printf("tag_info[%d:%d] = %d\n", instance, targ, value); |
| 1475 | } |
| 1476 | } |
| 1477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | /* |
| 1479 | * Handle Linux boot parameters. This routine allows for assigning a value |
| 1480 | * to a parameter with a ':' between the parameter and the value. |
| 1481 | * ie. aic7xxx=stpwlev:1,extended |
| 1482 | */ |
| 1483 | static int |
| 1484 | aic7xxx_setup(char *s) |
| 1485 | { |
| 1486 | int i, n; |
| 1487 | char *p; |
| 1488 | char *end; |
| 1489 | |
| 1490 | static struct { |
| 1491 | const char *name; |
| 1492 | uint32_t *flag; |
| 1493 | } options[] = { |
| 1494 | { "extended", &aic7xxx_extended }, |
| 1495 | { "no_reset", &aic7xxx_no_reset }, |
| 1496 | { "verbose", &aic7xxx_verbose }, |
| 1497 | { "allow_memio", &aic7xxx_allow_memio}, |
| 1498 | #ifdef AHC_DEBUG |
| 1499 | { "debug", &ahc_debug }, |
| 1500 | #endif |
| 1501 | { "reverse_scan", &aic7xxx_reverse_scan }, |
| 1502 | { "no_probe", &aic7xxx_probe_eisa_vl }, |
| 1503 | { "probe_eisa_vl", &aic7xxx_probe_eisa_vl }, |
| 1504 | { "periodic_otag", &aic7xxx_periodic_otag }, |
| 1505 | { "pci_parity", &aic7xxx_pci_parity }, |
| 1506 | { "seltime", &aic7xxx_seltime }, |
| 1507 | { "tag_info", NULL }, |
| 1508 | { "global_tag_depth", NULL }, |
| 1509 | { "dv", NULL } |
| 1510 | }; |
| 1511 | |
| 1512 | end = strchr(s, '\0'); |
| 1513 | |
| 1514 | /* |
| 1515 | * XXX ia64 gcc isn't smart enough to know that NUM_ELEMENTS |
| 1516 | * will never be 0 in this case. |
| 1517 | */ |
| 1518 | n = 0; |
| 1519 | |
| 1520 | while ((p = strsep(&s, ",.")) != NULL) { |
| 1521 | if (*p == '\0') |
| 1522 | continue; |
| 1523 | for (i = 0; i < NUM_ELEMENTS(options); i++) { |
| 1524 | |
| 1525 | n = strlen(options[i].name); |
| 1526 | if (strncmp(options[i].name, p, n) == 0) |
| 1527 | break; |
| 1528 | } |
| 1529 | if (i == NUM_ELEMENTS(options)) |
| 1530 | continue; |
| 1531 | |
| 1532 | if (strncmp(p, "global_tag_depth", n) == 0) { |
| 1533 | ahc_linux_setup_tag_info_global(p + n); |
| 1534 | } else if (strncmp(p, "tag_info", n) == 0) { |
| 1535 | s = aic_parse_brace_option("tag_info", p + n, end, |
| 1536 | 2, ahc_linux_setup_tag_info, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } else if (p[n] == ':') { |
| 1538 | *(options[i].flag) = simple_strtoul(p + n + 1, NULL, 0); |
| 1539 | } else if (strncmp(p, "verbose", n) == 0) { |
| 1540 | *(options[i].flag) = 1; |
| 1541 | } else { |
| 1542 | *(options[i].flag) ^= 0xFFFFFFFF; |
| 1543 | } |
| 1544 | } |
| 1545 | return 1; |
| 1546 | } |
| 1547 | |
| 1548 | __setup("aic7xxx=", aic7xxx_setup); |
| 1549 | |
| 1550 | uint32_t aic7xxx_verbose; |
| 1551 | |
| 1552 | int |
| 1553 | ahc_linux_register_host(struct ahc_softc *ahc, Scsi_Host_Template *template) |
| 1554 | { |
| 1555 | char buf[80]; |
| 1556 | struct Scsi_Host *host; |
| 1557 | char *new_name; |
| 1558 | u_long s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
| 1560 | template->name = ahc->description; |
| 1561 | host = scsi_host_alloc(template, sizeof(struct ahc_softc *)); |
| 1562 | if (host == NULL) |
| 1563 | return (ENOMEM); |
| 1564 | |
| 1565 | *((struct ahc_softc **)host->hostdata) = ahc; |
| 1566 | ahc_lock(ahc, &s); |
| 1567 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 1568 | scsi_assign_lock(host, &ahc->platform_data->spin_lock); |
| 1569 | #elif AHC_SCSI_HAS_HOST_LOCK != 0 |
| 1570 | host->lock = &ahc->platform_data->spin_lock; |
| 1571 | #endif |
| 1572 | ahc->platform_data->host = host; |
| 1573 | host->can_queue = AHC_MAX_QUEUE; |
| 1574 | host->cmd_per_lun = 2; |
| 1575 | /* XXX No way to communicate the ID for multiple channels */ |
| 1576 | host->this_id = ahc->our_id; |
| 1577 | host->irq = ahc->platform_data->irq; |
| 1578 | host->max_id = (ahc->features & AHC_WIDE) ? 16 : 8; |
| 1579 | host->max_lun = AHC_NUM_LUNS; |
| 1580 | host->max_channel = (ahc->features & AHC_TWIN) ? 1 : 0; |
| 1581 | host->sg_tablesize = AHC_NSEG; |
| 1582 | ahc_set_unit(ahc, ahc_linux_next_unit()); |
| 1583 | sprintf(buf, "scsi%d", host->host_no); |
| 1584 | new_name = malloc(strlen(buf) + 1, M_DEVBUF, M_NOWAIT); |
| 1585 | if (new_name != NULL) { |
| 1586 | strcpy(new_name, buf); |
| 1587 | ahc_set_name(ahc, new_name); |
| 1588 | } |
| 1589 | host->unique_id = ahc->unit; |
| 1590 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 1591 | scsi_set_pci_device(host, ahc->dev_softc); |
| 1592 | #endif |
| 1593 | ahc_linux_initialize_scsi_bus(ahc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | ahc_intr_enable(ahc, TRUE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | ahc_unlock(ahc, &s); |
| 1596 | |
| 92d161c | 2005-04-17 16:59:33 -0500 | [diff] [blame] | 1597 | host->transportt = ahc_linux_transport_template; |
| 1598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 1600 | scsi_add_host(host, (ahc->dev_softc ? &ahc->dev_softc->dev : NULL)); /* XXX handle failure */ |
| 1601 | scsi_scan_host(host); |
| 1602 | #endif |
| 1603 | return (0); |
| 1604 | } |
| 1605 | |
| 1606 | uint64_t |
| 1607 | ahc_linux_get_memsize(void) |
| 1608 | { |
| 1609 | struct sysinfo si; |
| 1610 | |
| 1611 | si_meminfo(&si); |
| 1612 | return ((uint64_t)si.totalram << PAGE_SHIFT); |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * Find the smallest available unit number to use |
| 1617 | * for a new device. We don't just use a static |
| 1618 | * count to handle the "repeated hot-(un)plug" |
| 1619 | * scenario. |
| 1620 | */ |
| 1621 | static int |
| 1622 | ahc_linux_next_unit(void) |
| 1623 | { |
| 1624 | struct ahc_softc *ahc; |
| 1625 | int unit; |
| 1626 | |
| 1627 | unit = 0; |
| 1628 | retry: |
| 1629 | TAILQ_FOREACH(ahc, &ahc_tailq, links) { |
| 1630 | if (ahc->unit == unit) { |
| 1631 | unit++; |
| 1632 | goto retry; |
| 1633 | } |
| 1634 | } |
| 1635 | return (unit); |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * Place the SCSI bus into a known state by either resetting it, |
| 1640 | * or forcing transfer negotiations on the next command to any |
| 1641 | * target. |
| 1642 | */ |
| 1643 | void |
| 1644 | ahc_linux_initialize_scsi_bus(struct ahc_softc *ahc) |
| 1645 | { |
| 1646 | int i; |
| 1647 | int numtarg; |
| 1648 | |
| 1649 | i = 0; |
| 1650 | numtarg = 0; |
| 1651 | |
| 1652 | if (aic7xxx_no_reset != 0) |
| 1653 | ahc->flags &= ~(AHC_RESET_BUS_A|AHC_RESET_BUS_B); |
| 1654 | |
| 1655 | if ((ahc->flags & AHC_RESET_BUS_A) != 0) |
| 1656 | ahc_reset_channel(ahc, 'A', /*initiate_reset*/TRUE); |
| 1657 | else |
| 1658 | numtarg = (ahc->features & AHC_WIDE) ? 16 : 8; |
| 1659 | |
| 1660 | if ((ahc->features & AHC_TWIN) != 0) { |
| 1661 | |
| 1662 | if ((ahc->flags & AHC_RESET_BUS_B) != 0) { |
| 1663 | ahc_reset_channel(ahc, 'B', /*initiate_reset*/TRUE); |
| 1664 | } else { |
| 1665 | if (numtarg == 0) |
| 1666 | i = 8; |
| 1667 | numtarg += 8; |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /* |
| 1672 | * Force negotiation to async for all targets that |
| 1673 | * will not see an initial bus reset. |
| 1674 | */ |
| 1675 | for (; i < numtarg; i++) { |
| 1676 | struct ahc_devinfo devinfo; |
| 1677 | struct ahc_initiator_tinfo *tinfo; |
| 1678 | struct ahc_tmode_tstate *tstate; |
| 1679 | u_int our_id; |
| 1680 | u_int target_id; |
| 1681 | char channel; |
| 1682 | |
| 1683 | channel = 'A'; |
| 1684 | our_id = ahc->our_id; |
| 1685 | target_id = i; |
| 1686 | if (i > 7 && (ahc->features & AHC_TWIN) != 0) { |
| 1687 | channel = 'B'; |
| 1688 | our_id = ahc->our_id_b; |
| 1689 | target_id = i % 8; |
| 1690 | } |
| 1691 | tinfo = ahc_fetch_transinfo(ahc, channel, our_id, |
| 1692 | target_id, &tstate); |
| 1693 | ahc_compile_devinfo(&devinfo, our_id, target_id, |
| 1694 | CAM_LUN_WILDCARD, channel, ROLE_INITIATOR); |
| 1695 | ahc_update_neg_request(ahc, &devinfo, tstate, |
| 1696 | tinfo, AHC_NEG_ALWAYS); |
| 1697 | } |
| 1698 | /* Give the bus some time to recover */ |
| 1699 | if ((ahc->flags & (AHC_RESET_BUS_A|AHC_RESET_BUS_B)) != 0) { |
| 1700 | ahc_linux_freeze_simq(ahc); |
| 1701 | init_timer(&ahc->platform_data->reset_timer); |
| 1702 | ahc->platform_data->reset_timer.data = (u_long)ahc; |
| 1703 | ahc->platform_data->reset_timer.expires = |
| 1704 | jiffies + (AIC7XXX_RESET_DELAY * HZ)/1000; |
| 1705 | ahc->platform_data->reset_timer.function = |
| 1706 | ahc_linux_release_simq; |
| 1707 | add_timer(&ahc->platform_data->reset_timer); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | int |
| 1712 | ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg) |
| 1713 | { |
| 1714 | |
| 1715 | ahc->platform_data = |
| 1716 | malloc(sizeof(struct ahc_platform_data), M_DEVBUF, M_NOWAIT); |
| 1717 | if (ahc->platform_data == NULL) |
| 1718 | return (ENOMEM); |
| 1719 | memset(ahc->platform_data, 0, sizeof(struct ahc_platform_data)); |
| 1720 | TAILQ_INIT(&ahc->platform_data->completeq); |
| 1721 | TAILQ_INIT(&ahc->platform_data->device_runq); |
| 1722 | ahc->platform_data->irq = AHC_LINUX_NOIRQ; |
| 1723 | ahc->platform_data->hw_dma_mask = 0xFFFFFFFF; |
| 1724 | ahc_lockinit(ahc); |
| 1725 | ahc_done_lockinit(ahc); |
| 1726 | init_timer(&ahc->platform_data->completeq_timer); |
| 1727 | ahc->platform_data->completeq_timer.data = (u_long)ahc; |
| 1728 | ahc->platform_data->completeq_timer.function = |
| 1729 | (ahc_linux_callback_t *)ahc_linux_thread_run_complete_queue; |
| 1730 | init_MUTEX_LOCKED(&ahc->platform_data->eh_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | tasklet_init(&ahc->platform_data->runq_tasklet, ahc_runq_tasklet, |
| 1732 | (unsigned long)ahc); |
| 1733 | ahc->seltime = (aic7xxx_seltime & 0x3) << 4; |
| 1734 | ahc->seltime_b = (aic7xxx_seltime & 0x3) << 4; |
| 1735 | if (aic7xxx_pci_parity == 0) |
| 1736 | ahc->flags |= AHC_DISABLE_PCI_PERR; |
| 1737 | |
| 1738 | return (0); |
| 1739 | } |
| 1740 | |
| 1741 | void |
| 1742 | ahc_platform_free(struct ahc_softc *ahc) |
| 1743 | { |
| 1744 | struct ahc_linux_target *targ; |
| 1745 | struct ahc_linux_device *dev; |
| 1746 | int i, j; |
| 1747 | |
| 1748 | if (ahc->platform_data != NULL) { |
| 1749 | del_timer_sync(&ahc->platform_data->completeq_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | tasklet_kill(&ahc->platform_data->runq_tasklet); |
| 1751 | if (ahc->platform_data->host != NULL) { |
| 1752 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 1753 | scsi_remove_host(ahc->platform_data->host); |
| 1754 | #endif |
| 1755 | scsi_host_put(ahc->platform_data->host); |
| 1756 | } |
| 1757 | |
| 1758 | /* destroy all of the device and target objects */ |
| 1759 | for (i = 0; i < AHC_NUM_TARGETS; i++) { |
| 1760 | targ = ahc->platform_data->targets[i]; |
| 1761 | if (targ != NULL) { |
| 1762 | /* Keep target around through the loop. */ |
| 1763 | targ->refcount++; |
| 1764 | for (j = 0; j < AHC_NUM_LUNS; j++) { |
| 1765 | |
| 1766 | if (targ->devices[j] == NULL) |
| 1767 | continue; |
| 1768 | dev = targ->devices[j]; |
| 1769 | ahc_linux_free_device(ahc, dev); |
| 1770 | } |
| 1771 | /* |
| 1772 | * Forcibly free the target now that |
| 1773 | * all devices are gone. |
| 1774 | */ |
| 1775 | ahc_linux_free_target(ahc, targ); |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | if (ahc->platform_data->irq != AHC_LINUX_NOIRQ) |
| 1780 | free_irq(ahc->platform_data->irq, ahc); |
| 1781 | if (ahc->tag == BUS_SPACE_PIO |
| 1782 | && ahc->bsh.ioport != 0) |
| 1783 | release_region(ahc->bsh.ioport, 256); |
| 1784 | if (ahc->tag == BUS_SPACE_MEMIO |
| 1785 | && ahc->bsh.maddr != NULL) { |
| 1786 | iounmap(ahc->bsh.maddr); |
| 1787 | release_mem_region(ahc->platform_data->mem_busaddr, |
| 1788 | 0x1000); |
| 1789 | } |
| 1790 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 1791 | /* |
| 1792 | * In 2.4 we detach from the scsi midlayer before the PCI |
| 1793 | * layer invokes our remove callback. No per-instance |
| 1794 | * detach is provided, so we must reach inside the PCI |
| 1795 | * subsystem's internals and detach our driver manually. |
| 1796 | */ |
| 1797 | if (ahc->dev_softc != NULL) |
| 1798 | ahc->dev_softc->driver = NULL; |
| 1799 | #endif |
| 1800 | free(ahc->platform_data, M_DEVBUF); |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | void |
| 1805 | ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb) |
| 1806 | { |
| 1807 | ahc_platform_abort_scbs(ahc, SCB_GET_TARGET(ahc, scb), |
| 1808 | SCB_GET_CHANNEL(ahc, scb), |
| 1809 | SCB_GET_LUN(scb), SCB_LIST_NULL, |
| 1810 | ROLE_UNKNOWN, CAM_REQUEUE_REQ); |
| 1811 | } |
| 1812 | |
| 1813 | void |
| 1814 | ahc_platform_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo, |
| 1815 | ahc_queue_alg alg) |
| 1816 | { |
| 1817 | struct ahc_linux_device *dev; |
| 1818 | int was_queuing; |
| 1819 | int now_queuing; |
| 1820 | |
| 1821 | dev = ahc_linux_get_device(ahc, devinfo->channel - 'A', |
| 1822 | devinfo->target, |
| 1823 | devinfo->lun, /*alloc*/FALSE); |
| 1824 | if (dev == NULL) |
| 1825 | return; |
| 1826 | was_queuing = dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED); |
| 1827 | switch (alg) { |
| 1828 | default: |
| 1829 | case AHC_QUEUE_NONE: |
| 1830 | now_queuing = 0; |
| 1831 | break; |
| 1832 | case AHC_QUEUE_BASIC: |
| 1833 | now_queuing = AHC_DEV_Q_BASIC; |
| 1834 | break; |
| 1835 | case AHC_QUEUE_TAGGED: |
| 1836 | now_queuing = AHC_DEV_Q_TAGGED; |
| 1837 | break; |
| 1838 | } |
| 1839 | if ((dev->flags & AHC_DEV_FREEZE_TIL_EMPTY) == 0 |
| 1840 | && (was_queuing != now_queuing) |
| 1841 | && (dev->active != 0)) { |
| 1842 | dev->flags |= AHC_DEV_FREEZE_TIL_EMPTY; |
| 1843 | dev->qfrozen++; |
| 1844 | } |
| 1845 | |
| 1846 | dev->flags &= ~(AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED|AHC_DEV_PERIODIC_OTAG); |
| 1847 | if (now_queuing) { |
| 1848 | u_int usertags; |
| 1849 | |
| 1850 | usertags = ahc_linux_user_tagdepth(ahc, devinfo); |
| 1851 | if (!was_queuing) { |
| 1852 | /* |
| 1853 | * Start out agressively and allow our |
| 1854 | * dynamic queue depth algorithm to take |
| 1855 | * care of the rest. |
| 1856 | */ |
| 1857 | dev->maxtags = usertags; |
| 1858 | dev->openings = dev->maxtags - dev->active; |
| 1859 | } |
| 1860 | if (dev->maxtags == 0) { |
| 1861 | /* |
| 1862 | * Queueing is disabled by the user. |
| 1863 | */ |
| 1864 | dev->openings = 1; |
| 1865 | } else if (alg == AHC_QUEUE_TAGGED) { |
| 1866 | dev->flags |= AHC_DEV_Q_TAGGED; |
| 1867 | if (aic7xxx_periodic_otag != 0) |
| 1868 | dev->flags |= AHC_DEV_PERIODIC_OTAG; |
| 1869 | } else |
| 1870 | dev->flags |= AHC_DEV_Q_BASIC; |
| 1871 | } else { |
| 1872 | /* We can only have one opening. */ |
| 1873 | dev->maxtags = 0; |
| 1874 | dev->openings = 1 - dev->active; |
| 1875 | } |
| 1876 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 1877 | if (dev->scsi_device != NULL) { |
| 1878 | switch ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED))) { |
| 1879 | case AHC_DEV_Q_BASIC: |
| 1880 | scsi_adjust_queue_depth(dev->scsi_device, |
| 1881 | MSG_SIMPLE_TASK, |
| 1882 | dev->openings + dev->active); |
| 1883 | break; |
| 1884 | case AHC_DEV_Q_TAGGED: |
| 1885 | scsi_adjust_queue_depth(dev->scsi_device, |
| 1886 | MSG_ORDERED_TASK, |
| 1887 | dev->openings + dev->active); |
| 1888 | break; |
| 1889 | default: |
| 1890 | /* |
| 1891 | * We allow the OS to queue 2 untagged transactions to |
| 1892 | * us at any time even though we can only execute them |
| 1893 | * serially on the controller/device. This should |
| 1894 | * remove some latency. |
| 1895 | */ |
| 1896 | scsi_adjust_queue_depth(dev->scsi_device, |
| 1897 | /*NON-TAGGED*/0, |
| 1898 | /*queue depth*/2); |
| 1899 | break; |
| 1900 | } |
| 1901 | } |
| 1902 | #endif |
| 1903 | } |
| 1904 | |
| 1905 | int |
| 1906 | ahc_platform_abort_scbs(struct ahc_softc *ahc, int target, char channel, |
| 1907 | int lun, u_int tag, role_t role, uint32_t status) |
| 1908 | { |
| 1909 | int chan; |
| 1910 | int maxchan; |
| 1911 | int targ; |
| 1912 | int maxtarg; |
| 1913 | int clun; |
| 1914 | int maxlun; |
| 1915 | int count; |
| 1916 | |
| 1917 | if (tag != SCB_LIST_NULL) |
| 1918 | return (0); |
| 1919 | |
| 1920 | chan = 0; |
| 1921 | if (channel != ALL_CHANNELS) { |
| 1922 | chan = channel - 'A'; |
| 1923 | maxchan = chan + 1; |
| 1924 | } else { |
| 1925 | maxchan = (ahc->features & AHC_TWIN) ? 2 : 1; |
| 1926 | } |
| 1927 | targ = 0; |
| 1928 | if (target != CAM_TARGET_WILDCARD) { |
| 1929 | targ = target; |
| 1930 | maxtarg = targ + 1; |
| 1931 | } else { |
| 1932 | maxtarg = (ahc->features & AHC_WIDE) ? 16 : 8; |
| 1933 | } |
| 1934 | clun = 0; |
| 1935 | if (lun != CAM_LUN_WILDCARD) { |
| 1936 | clun = lun; |
| 1937 | maxlun = clun + 1; |
| 1938 | } else { |
| 1939 | maxlun = AHC_NUM_LUNS; |
| 1940 | } |
| 1941 | |
| 1942 | count = 0; |
| 1943 | for (; chan < maxchan; chan++) { |
| 1944 | |
| 1945 | for (; targ < maxtarg; targ++) { |
| 1946 | |
| 1947 | for (; clun < maxlun; clun++) { |
| 1948 | struct ahc_linux_device *dev; |
| 1949 | struct ahc_busyq *busyq; |
| 1950 | struct ahc_cmd *acmd; |
| 1951 | |
| 1952 | dev = ahc_linux_get_device(ahc, chan, |
| 1953 | targ, clun, |
| 1954 | /*alloc*/FALSE); |
| 1955 | if (dev == NULL) |
| 1956 | continue; |
| 1957 | |
| 1958 | busyq = &dev->busyq; |
| 1959 | while ((acmd = TAILQ_FIRST(busyq)) != NULL) { |
| 1960 | Scsi_Cmnd *cmd; |
| 1961 | |
| 1962 | cmd = &acmd_scsi_cmd(acmd); |
| 1963 | TAILQ_REMOVE(busyq, acmd, |
| 1964 | acmd_links.tqe); |
| 1965 | count++; |
| 1966 | cmd->result = status << 16; |
| 1967 | ahc_linux_queue_cmd_complete(ahc, cmd); |
| 1968 | } |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | return (count); |
| 1974 | } |
| 1975 | |
| 1976 | static void |
| 1977 | ahc_linux_thread_run_complete_queue(struct ahc_softc *ahc) |
| 1978 | { |
| 1979 | u_long flags; |
| 1980 | |
| 1981 | ahc_lock(ahc, &flags); |
| 1982 | del_timer(&ahc->platform_data->completeq_timer); |
| 1983 | ahc->platform_data->flags &= ~AHC_RUN_CMPLT_Q_TIMER; |
| 1984 | ahc_linux_run_complete_queue(ahc); |
| 1985 | ahc_unlock(ahc, &flags); |
| 1986 | } |
| 1987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | static u_int |
| 1989 | ahc_linux_user_tagdepth(struct ahc_softc *ahc, struct ahc_devinfo *devinfo) |
| 1990 | { |
| 1991 | static int warned_user; |
| 1992 | u_int tags; |
| 1993 | |
| 1994 | tags = 0; |
| 1995 | if ((ahc->user_discenable & devinfo->target_mask) != 0) { |
| 1996 | if (ahc->unit >= NUM_ELEMENTS(aic7xxx_tag_info)) { |
| 1997 | if (warned_user == 0) { |
| 1998 | |
| 1999 | printf(KERN_WARNING |
| 2000 | "aic7xxx: WARNING: Insufficient tag_info instances\n" |
| 2001 | "aic7xxx: for installed controllers. Using defaults\n" |
| 2002 | "aic7xxx: Please update the aic7xxx_tag_info array in\n" |
| 2003 | "aic7xxx: the aic7xxx_osm..c source file.\n"); |
| 2004 | warned_user++; |
| 2005 | } |
| 2006 | tags = AHC_MAX_QUEUE; |
| 2007 | } else { |
| 2008 | adapter_tag_info_t *tag_info; |
| 2009 | |
| 2010 | tag_info = &aic7xxx_tag_info[ahc->unit]; |
| 2011 | tags = tag_info->tag_commands[devinfo->target_offset]; |
| 2012 | if (tags > AHC_MAX_QUEUE) |
| 2013 | tags = AHC_MAX_QUEUE; |
| 2014 | } |
| 2015 | } |
| 2016 | return (tags); |
| 2017 | } |
| 2018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | /* |
| 2020 | * Determines the queue depth for a given device. |
| 2021 | */ |
| 2022 | static void |
| 2023 | ahc_linux_device_queue_depth(struct ahc_softc *ahc, |
| 2024 | struct ahc_linux_device *dev) |
| 2025 | { |
| 2026 | struct ahc_devinfo devinfo; |
| 2027 | u_int tags; |
| 2028 | |
| 2029 | ahc_compile_devinfo(&devinfo, |
| 2030 | dev->target->channel == 0 |
| 2031 | ? ahc->our_id : ahc->our_id_b, |
| 2032 | dev->target->target, dev->lun, |
| 2033 | dev->target->channel == 0 ? 'A' : 'B', |
| 2034 | ROLE_INITIATOR); |
| 2035 | tags = ahc_linux_user_tagdepth(ahc, &devinfo); |
| 2036 | if (tags != 0 |
| 2037 | && dev->scsi_device != NULL |
| 2038 | && dev->scsi_device->tagged_supported != 0) { |
| 2039 | |
| 2040 | ahc_set_tags(ahc, &devinfo, AHC_QUEUE_TAGGED); |
| 2041 | ahc_print_devinfo(ahc, &devinfo); |
| 2042 | printf("Tagged Queuing enabled. Depth %d\n", tags); |
| 2043 | } else { |
| 2044 | ahc_set_tags(ahc, &devinfo, AHC_QUEUE_NONE); |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | static void |
| 2049 | ahc_linux_run_device_queue(struct ahc_softc *ahc, struct ahc_linux_device *dev) |
| 2050 | { |
| 2051 | struct ahc_cmd *acmd; |
| 2052 | struct scsi_cmnd *cmd; |
| 2053 | struct scb *scb; |
| 2054 | struct hardware_scb *hscb; |
| 2055 | struct ahc_initiator_tinfo *tinfo; |
| 2056 | struct ahc_tmode_tstate *tstate; |
| 2057 | uint16_t mask; |
| 2058 | |
| 2059 | if ((dev->flags & AHC_DEV_ON_RUN_LIST) != 0) |
| 2060 | panic("running device on run list"); |
| 2061 | |
| 2062 | while ((acmd = TAILQ_FIRST(&dev->busyq)) != NULL |
| 2063 | && dev->openings > 0 && dev->qfrozen == 0) { |
| 2064 | |
| 2065 | /* |
| 2066 | * Schedule us to run later. The only reason we are not |
| 2067 | * running is because the whole controller Q is frozen. |
| 2068 | */ |
| cb62402 | 2005-04-17 18:03:20 -0500 | [diff] [blame] | 2069 | if (ahc->platform_data->qfrozen != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, |
| 2071 | dev, links); |
| 2072 | dev->flags |= AHC_DEV_ON_RUN_LIST; |
| 2073 | return; |
| 2074 | } |
| 2075 | /* |
| 2076 | * Get an scb to use. |
| 2077 | */ |
| 2078 | if ((scb = ahc_get_scb(ahc)) == NULL) { |
| 2079 | TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, |
| 2080 | dev, links); |
| 2081 | dev->flags |= AHC_DEV_ON_RUN_LIST; |
| 2082 | ahc->flags |= AHC_RESOURCE_SHORTAGE; |
| 2083 | return; |
| 2084 | } |
| 2085 | TAILQ_REMOVE(&dev->busyq, acmd, acmd_links.tqe); |
| 2086 | cmd = &acmd_scsi_cmd(acmd); |
| 2087 | scb->io_ctx = cmd; |
| 2088 | scb->platform_data->dev = dev; |
| 2089 | hscb = scb->hscb; |
| 2090 | cmd->host_scribble = (char *)scb; |
| 2091 | |
| 2092 | /* |
| 2093 | * Fill out basics of the HSCB. |
| 2094 | */ |
| 2095 | hscb->control = 0; |
| 2096 | hscb->scsiid = BUILD_SCSIID(ahc, cmd); |
| 2097 | hscb->lun = cmd->device->lun; |
| 2098 | mask = SCB_GET_TARGET_MASK(ahc, scb); |
| 2099 | tinfo = ahc_fetch_transinfo(ahc, SCB_GET_CHANNEL(ahc, scb), |
| 2100 | SCB_GET_OUR_ID(scb), |
| 2101 | SCB_GET_TARGET(ahc, scb), &tstate); |
| 2102 | hscb->scsirate = tinfo->scsirate; |
| 2103 | hscb->scsioffset = tinfo->curr.offset; |
| 2104 | if ((tstate->ultraenb & mask) != 0) |
| 2105 | hscb->control |= ULTRAENB; |
| 2106 | |
| 2107 | if ((ahc->user_discenable & mask) != 0) |
| 2108 | hscb->control |= DISCENB; |
| 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | if ((tstate->auto_negotiate & mask) != 0) { |
| 2111 | scb->flags |= SCB_AUTO_NEGOTIATE; |
| 2112 | scb->hscb->control |= MK_MESSAGE; |
| 2113 | } |
| 2114 | |
| 2115 | if ((dev->flags & (AHC_DEV_Q_TAGGED|AHC_DEV_Q_BASIC)) != 0) { |
| 2116 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 2117 | int msg_bytes; |
| 2118 | uint8_t tag_msgs[2]; |
| 2119 | |
| 2120 | msg_bytes = scsi_populate_tag_msg(cmd, tag_msgs); |
| 2121 | if (msg_bytes && tag_msgs[0] != MSG_SIMPLE_TASK) { |
| 2122 | hscb->control |= tag_msgs[0]; |
| 2123 | if (tag_msgs[0] == MSG_ORDERED_TASK) |
| 2124 | dev->commands_since_idle_or_otag = 0; |
| 2125 | } else |
| 2126 | #endif |
| 2127 | if (dev->commands_since_idle_or_otag == AHC_OTAG_THRESH |
| 2128 | && (dev->flags & AHC_DEV_Q_TAGGED) != 0) { |
| 2129 | hscb->control |= MSG_ORDERED_TASK; |
| 2130 | dev->commands_since_idle_or_otag = 0; |
| 2131 | } else { |
| 2132 | hscb->control |= MSG_SIMPLE_TASK; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | hscb->cdb_len = cmd->cmd_len; |
| 2137 | if (hscb->cdb_len <= 12) { |
| 2138 | memcpy(hscb->shared_data.cdb, cmd->cmnd, hscb->cdb_len); |
| 2139 | } else { |
| 2140 | memcpy(hscb->cdb32, cmd->cmnd, hscb->cdb_len); |
| 2141 | scb->flags |= SCB_CDB32_PTR; |
| 2142 | } |
| 2143 | |
| 2144 | scb->platform_data->xfer_len = 0; |
| 2145 | ahc_set_residual(scb, 0); |
| 2146 | ahc_set_sense_residual(scb, 0); |
| 2147 | scb->sg_count = 0; |
| 2148 | if (cmd->use_sg != 0) { |
| 2149 | struct ahc_dma_seg *sg; |
| 2150 | struct scatterlist *cur_seg; |
| 2151 | struct scatterlist *end_seg; |
| 2152 | int nseg; |
| 2153 | |
| 2154 | cur_seg = (struct scatterlist *)cmd->request_buffer; |
| 2155 | nseg = pci_map_sg(ahc->dev_softc, cur_seg, cmd->use_sg, |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 2156 | cmd->sc_data_direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | end_seg = cur_seg + nseg; |
| 2158 | /* Copy the segments into the SG list. */ |
| 2159 | sg = scb->sg_list; |
| 2160 | /* |
| 2161 | * The sg_count may be larger than nseg if |
| 2162 | * a transfer crosses a 32bit page. |
| 2163 | */ |
| 2164 | while (cur_seg < end_seg) { |
| 2165 | dma_addr_t addr; |
| 2166 | bus_size_t len; |
| 2167 | int consumed; |
| 2168 | |
| 2169 | addr = sg_dma_address(cur_seg); |
| 2170 | len = sg_dma_len(cur_seg); |
| 2171 | consumed = ahc_linux_map_seg(ahc, scb, |
| 2172 | sg, addr, len); |
| 2173 | sg += consumed; |
| 2174 | scb->sg_count += consumed; |
| 2175 | cur_seg++; |
| 2176 | } |
| 2177 | sg--; |
| 2178 | sg->len |= ahc_htole32(AHC_DMA_LAST_SEG); |
| 2179 | |
| 2180 | /* |
| 2181 | * Reset the sg list pointer. |
| 2182 | */ |
| 2183 | scb->hscb->sgptr = |
| 2184 | ahc_htole32(scb->sg_list_phys | SG_FULL_RESID); |
| 2185 | |
| 2186 | /* |
| 2187 | * Copy the first SG into the "current" |
| 2188 | * data pointer area. |
| 2189 | */ |
| 2190 | scb->hscb->dataptr = scb->sg_list->addr; |
| 2191 | scb->hscb->datacnt = scb->sg_list->len; |
| 2192 | } else if (cmd->request_bufflen != 0) { |
| 2193 | struct ahc_dma_seg *sg; |
| 2194 | dma_addr_t addr; |
| 2195 | |
| 2196 | sg = scb->sg_list; |
| 2197 | addr = pci_map_single(ahc->dev_softc, |
| 2198 | cmd->request_buffer, |
| 2199 | cmd->request_bufflen, |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 2200 | cmd->sc_data_direction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | scb->platform_data->buf_busaddr = addr; |
| 2202 | scb->sg_count = ahc_linux_map_seg(ahc, scb, |
| 2203 | sg, addr, |
| 2204 | cmd->request_bufflen); |
| 2205 | sg->len |= ahc_htole32(AHC_DMA_LAST_SEG); |
| 2206 | |
| 2207 | /* |
| 2208 | * Reset the sg list pointer. |
| 2209 | */ |
| 2210 | scb->hscb->sgptr = |
| 2211 | ahc_htole32(scb->sg_list_phys | SG_FULL_RESID); |
| 2212 | |
| 2213 | /* |
| 2214 | * Copy the first SG into the "current" |
| 2215 | * data pointer area. |
| 2216 | */ |
| 2217 | scb->hscb->dataptr = sg->addr; |
| 2218 | scb->hscb->datacnt = sg->len; |
| 2219 | } else { |
| 2220 | scb->hscb->sgptr = ahc_htole32(SG_LIST_NULL); |
| 2221 | scb->hscb->dataptr = 0; |
| 2222 | scb->hscb->datacnt = 0; |
| 2223 | scb->sg_count = 0; |
| 2224 | } |
| 2225 | |
| 2226 | ahc_sync_sglist(ahc, scb, BUS_DMASYNC_PREWRITE); |
| 2227 | LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links); |
| 2228 | dev->openings--; |
| 2229 | dev->active++; |
| 2230 | dev->commands_issued++; |
| 2231 | if ((dev->flags & AHC_DEV_PERIODIC_OTAG) != 0) |
| 2232 | dev->commands_since_idle_or_otag++; |
| 2233 | |
| 2234 | /* |
| 2235 | * We only allow one untagged transaction |
| 2236 | * per target in the initiator role unless |
| 2237 | * we are storing a full busy target *lun* |
| 2238 | * table in SCB space. |
| 2239 | */ |
| 2240 | if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0 |
| 2241 | && (ahc->features & AHC_SCB_BTT) == 0) { |
| 2242 | struct scb_tailq *untagged_q; |
| 2243 | int target_offset; |
| 2244 | |
| 2245 | target_offset = SCB_GET_TARGET_OFFSET(ahc, scb); |
| 2246 | untagged_q = &(ahc->untagged_queues[target_offset]); |
| 2247 | TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe); |
| 2248 | scb->flags |= SCB_UNTAGGEDQ; |
| 2249 | if (TAILQ_FIRST(untagged_q) != scb) |
| 2250 | continue; |
| 2251 | } |
| 2252 | scb->flags |= SCB_ACTIVE; |
| 2253 | ahc_queue_scb(ahc, scb); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /* |
| 2258 | * SCSI controller interrupt handler. |
| 2259 | */ |
| 2260 | irqreturn_t |
| 2261 | ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs) |
| 2262 | { |
| 2263 | struct ahc_softc *ahc; |
| 2264 | u_long flags; |
| 2265 | int ours; |
| 2266 | |
| 2267 | ahc = (struct ahc_softc *) dev_id; |
| 2268 | ahc_lock(ahc, &flags); |
| 2269 | ours = ahc_intr(ahc); |
| 2270 | if (ahc_linux_next_device_to_run(ahc) != NULL) |
| 2271 | ahc_schedule_runq(ahc); |
| 2272 | ahc_linux_run_complete_queue(ahc); |
| 2273 | ahc_unlock(ahc, &flags); |
| 2274 | return IRQ_RETVAL(ours); |
| 2275 | } |
| 2276 | |
| 2277 | void |
| 2278 | ahc_platform_flushwork(struct ahc_softc *ahc) |
| 2279 | { |
| 2280 | |
| 2281 | while (ahc_linux_run_complete_queue(ahc) != NULL) |
| 2282 | ; |
| 2283 | } |
| 2284 | |
| 2285 | static struct ahc_linux_target* |
| 2286 | ahc_linux_alloc_target(struct ahc_softc *ahc, u_int channel, u_int target) |
| 2287 | { |
| 2288 | struct ahc_linux_target *targ; |
| 2289 | u_int target_offset; |
| 2290 | |
| 2291 | target_offset = target; |
| 2292 | if (channel != 0) |
| 2293 | target_offset += 8; |
| 2294 | |
| 2295 | targ = malloc(sizeof(*targ), M_DEVBUG, M_NOWAIT); |
| 2296 | if (targ == NULL) |
| 2297 | return (NULL); |
| 2298 | memset(targ, 0, sizeof(*targ)); |
| 2299 | targ->channel = channel; |
| 2300 | targ->target = target; |
| 2301 | targ->ahc = ahc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | ahc->platform_data->targets[target_offset] = targ; |
| 2303 | return (targ); |
| 2304 | } |
| 2305 | |
| 2306 | static void |
| 2307 | ahc_linux_free_target(struct ahc_softc *ahc, struct ahc_linux_target *targ) |
| 2308 | { |
| 2309 | struct ahc_devinfo devinfo; |
| 2310 | struct ahc_initiator_tinfo *tinfo; |
| 2311 | struct ahc_tmode_tstate *tstate; |
| 2312 | u_int our_id; |
| 2313 | u_int target_offset; |
| 2314 | char channel; |
| 2315 | |
| 2316 | /* |
| 2317 | * Force a negotiation to async/narrow on any |
| 2318 | * future command to this device unless a bus |
| 2319 | * reset occurs between now and that command. |
| 2320 | */ |
| 2321 | channel = 'A' + targ->channel; |
| 2322 | our_id = ahc->our_id; |
| 2323 | target_offset = targ->target; |
| 2324 | if (targ->channel != 0) { |
| 2325 | target_offset += 8; |
| 2326 | our_id = ahc->our_id_b; |
| 2327 | } |
| 2328 | tinfo = ahc_fetch_transinfo(ahc, channel, our_id, |
| 2329 | targ->target, &tstate); |
| 2330 | ahc_compile_devinfo(&devinfo, our_id, targ->target, CAM_LUN_WILDCARD, |
| 2331 | channel, ROLE_INITIATOR); |
| 2332 | ahc_set_syncrate(ahc, &devinfo, NULL, 0, 0, 0, |
| 2333 | AHC_TRANS_GOAL, /*paused*/FALSE); |
| 2334 | ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT, |
| 2335 | AHC_TRANS_GOAL, /*paused*/FALSE); |
| 2336 | ahc_update_neg_request(ahc, &devinfo, tstate, tinfo, AHC_NEG_ALWAYS); |
| 2337 | ahc->platform_data->targets[target_offset] = NULL; |
| 2338 | if (targ->inq_data != NULL) |
| 2339 | free(targ->inq_data, M_DEVBUF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | free(targ, M_DEVBUF); |
| 2341 | } |
| 2342 | |
| 2343 | static struct ahc_linux_device* |
| 2344 | ahc_linux_alloc_device(struct ahc_softc *ahc, |
| 2345 | struct ahc_linux_target *targ, u_int lun) |
| 2346 | { |
| 2347 | struct ahc_linux_device *dev; |
| 2348 | |
| 2349 | dev = malloc(sizeof(*dev), M_DEVBUG, M_NOWAIT); |
| 2350 | if (dev == NULL) |
| 2351 | return (NULL); |
| 2352 | memset(dev, 0, sizeof(*dev)); |
| 2353 | init_timer(&dev->timer); |
| 2354 | TAILQ_INIT(&dev->busyq); |
| 2355 | dev->flags = AHC_DEV_UNCONFIGURED; |
| 2356 | dev->lun = lun; |
| 2357 | dev->target = targ; |
| 2358 | |
| 2359 | /* |
| 2360 | * We start out life using untagged |
| 2361 | * transactions of which we allow one. |
| 2362 | */ |
| 2363 | dev->openings = 1; |
| 2364 | |
| 2365 | /* |
| 2366 | * Set maxtags to 0. This will be changed if we |
| 2367 | * later determine that we are dealing with |
| 2368 | * a tagged queuing capable device. |
| 2369 | */ |
| 2370 | dev->maxtags = 0; |
| 2371 | |
| 2372 | targ->refcount++; |
| 2373 | targ->devices[lun] = dev; |
| 2374 | return (dev); |
| 2375 | } |
| 2376 | |
| 2377 | static void |
| 2378 | __ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev) |
| 2379 | { |
| 2380 | struct ahc_linux_target *targ; |
| 2381 | |
| 2382 | targ = dev->target; |
| 2383 | targ->devices[dev->lun] = NULL; |
| 2384 | free(dev, M_DEVBUF); |
| 2385 | targ->refcount--; |
| cb62402 | 2005-04-17 18:03:20 -0500 | [diff] [blame] | 2386 | if (targ->refcount == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | ahc_linux_free_target(ahc, targ); |
| 2388 | } |
| 2389 | |
| 2390 | static void |
| 2391 | ahc_linux_free_device(struct ahc_softc *ahc, struct ahc_linux_device *dev) |
| 2392 | { |
| 2393 | del_timer_sync(&dev->timer); |
| 2394 | __ahc_linux_free_device(ahc, dev); |
| 2395 | } |
| 2396 | |
| 2397 | void |
| 2398 | ahc_send_async(struct ahc_softc *ahc, char channel, |
| 2399 | u_int target, u_int lun, ac_code code, void *arg) |
| 2400 | { |
| 2401 | switch (code) { |
| 2402 | case AC_TRANSFER_NEG: |
| 2403 | { |
| 2404 | char buf[80]; |
| 2405 | struct ahc_linux_target *targ; |
| 2406 | struct info_str info; |
| 2407 | struct ahc_initiator_tinfo *tinfo; |
| 2408 | struct ahc_tmode_tstate *tstate; |
| 2409 | int target_offset; |
| 2410 | |
| 2411 | info.buffer = buf; |
| 2412 | info.length = sizeof(buf); |
| 2413 | info.offset = 0; |
| 2414 | info.pos = 0; |
| 2415 | tinfo = ahc_fetch_transinfo(ahc, channel, |
| 2416 | channel == 'A' ? ahc->our_id |
| 2417 | : ahc->our_id_b, |
| 2418 | target, &tstate); |
| 2419 | |
| 2420 | /* |
| 2421 | * Don't bother reporting results while |
| 2422 | * negotiations are still pending. |
| 2423 | */ |
| 2424 | if (tinfo->curr.period != tinfo->goal.period |
| 2425 | || tinfo->curr.width != tinfo->goal.width |
| 2426 | || tinfo->curr.offset != tinfo->goal.offset |
| 2427 | || tinfo->curr.ppr_options != tinfo->goal.ppr_options) |
| 2428 | if (bootverbose == 0) |
| 2429 | break; |
| 2430 | |
| 2431 | /* |
| 2432 | * Don't bother reporting results that |
| 2433 | * are identical to those last reported. |
| 2434 | */ |
| 2435 | target_offset = target; |
| 2436 | if (channel == 'B') |
| 2437 | target_offset += 8; |
| 2438 | targ = ahc->platform_data->targets[target_offset]; |
| 2439 | if (targ == NULL) |
| 2440 | break; |
| 2441 | if (tinfo->curr.period == targ->last_tinfo.period |
| 2442 | && tinfo->curr.width == targ->last_tinfo.width |
| 2443 | && tinfo->curr.offset == targ->last_tinfo.offset |
| 2444 | && tinfo->curr.ppr_options == targ->last_tinfo.ppr_options) |
| 2445 | if (bootverbose == 0) |
| 2446 | break; |
| 2447 | |
| 2448 | targ->last_tinfo.period = tinfo->curr.period; |
| 2449 | targ->last_tinfo.width = tinfo->curr.width; |
| 2450 | targ->last_tinfo.offset = tinfo->curr.offset; |
| 2451 | targ->last_tinfo.ppr_options = tinfo->curr.ppr_options; |
| 2452 | |
| 2453 | printf("(%s:%c:", ahc_name(ahc), channel); |
| 2454 | if (target == CAM_TARGET_WILDCARD) |
| 2455 | printf("*): "); |
| 2456 | else |
| 2457 | printf("%d): ", target); |
| 2458 | ahc_format_transinfo(&info, &tinfo->curr); |
| 2459 | if (info.pos < info.length) |
| 2460 | *info.buffer = '\0'; |
| 2461 | else |
| 2462 | buf[info.length - 1] = '\0'; |
| 2463 | printf("%s", buf); |
| 2464 | break; |
| 2465 | } |
| 2466 | case AC_SENT_BDR: |
| 2467 | { |
| 2468 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 2469 | WARN_ON(lun != CAM_LUN_WILDCARD); |
| 2470 | scsi_report_device_reset(ahc->platform_data->host, |
| 2471 | channel - 'A', target); |
| 2472 | #else |
| 2473 | Scsi_Device *scsi_dev; |
| 2474 | |
| 2475 | /* |
| 2476 | * Find the SCSI device associated with this |
| 2477 | * request and indicate that a UA is expected. |
| 2478 | */ |
| 2479 | for (scsi_dev = ahc->platform_data->host->host_queue; |
| 2480 | scsi_dev != NULL; scsi_dev = scsi_dev->next) { |
| 2481 | if (channel - 'A' == scsi_dev->channel |
| 2482 | && target == scsi_dev->id |
| 2483 | && (lun == CAM_LUN_WILDCARD |
| 2484 | || lun == scsi_dev->lun)) { |
| 2485 | scsi_dev->was_reset = 1; |
| 2486 | scsi_dev->expecting_cc_ua = 1; |
| 2487 | } |
| 2488 | } |
| 2489 | #endif |
| 2490 | break; |
| 2491 | } |
| 2492 | case AC_BUS_RESET: |
| 2493 | if (ahc->platform_data->host != NULL) { |
| 2494 | scsi_report_bus_reset(ahc->platform_data->host, |
| 2495 | channel - 'A'); |
| 2496 | } |
| 2497 | break; |
| 2498 | default: |
| 2499 | panic("ahc_send_async: Unexpected async event"); |
| 2500 | } |
| 2501 | } |
| 2502 | |
| 2503 | /* |
| 2504 | * Calls the higher level scsi done function and frees the scb. |
| 2505 | */ |
| 2506 | void |
| 2507 | ahc_done(struct ahc_softc *ahc, struct scb *scb) |
| 2508 | { |
| 2509 | Scsi_Cmnd *cmd; |
| 2510 | struct ahc_linux_device *dev; |
| 2511 | |
| 2512 | LIST_REMOVE(scb, pending_links); |
| 2513 | if ((scb->flags & SCB_UNTAGGEDQ) != 0) { |
| 2514 | struct scb_tailq *untagged_q; |
| 2515 | int target_offset; |
| 2516 | |
| 2517 | target_offset = SCB_GET_TARGET_OFFSET(ahc, scb); |
| 2518 | untagged_q = &(ahc->untagged_queues[target_offset]); |
| 2519 | TAILQ_REMOVE(untagged_q, scb, links.tqe); |
| 2520 | ahc_run_untagged_queue(ahc, untagged_q); |
| 2521 | } |
| 2522 | |
| 2523 | if ((scb->flags & SCB_ACTIVE) == 0) { |
| 2524 | printf("SCB %d done'd twice\n", scb->hscb->tag); |
| 2525 | ahc_dump_card_state(ahc); |
| 2526 | panic("Stopping for safety"); |
| 2527 | } |
| 2528 | cmd = scb->io_ctx; |
| 2529 | dev = scb->platform_data->dev; |
| 2530 | dev->active--; |
| 2531 | dev->openings++; |
| 2532 | if ((cmd->result & (CAM_DEV_QFRZN << 16)) != 0) { |
| 2533 | cmd->result &= ~(CAM_DEV_QFRZN << 16); |
| 2534 | dev->qfrozen--; |
| 2535 | } |
| 2536 | ahc_linux_unmap_scb(ahc, scb); |
| 2537 | |
| 2538 | /* |
| 2539 | * Guard against stale sense data. |
| 2540 | * The Linux mid-layer assumes that sense |
| 2541 | * was retrieved anytime the first byte of |
| 2542 | * the sense buffer looks "sane". |
| 2543 | */ |
| 2544 | cmd->sense_buffer[0] = 0; |
| 2545 | if (ahc_get_transaction_status(scb) == CAM_REQ_INPROG) { |
| 2546 | uint32_t amount_xferred; |
| 2547 | |
| 2548 | amount_xferred = |
| 2549 | ahc_get_transfer_length(scb) - ahc_get_residual(scb); |
| 2550 | if ((scb->flags & SCB_TRANSMISSION_ERROR) != 0) { |
| 2551 | #ifdef AHC_DEBUG |
| 2552 | if ((ahc_debug & AHC_SHOW_MISC) != 0) { |
| 2553 | ahc_print_path(ahc, scb); |
| 2554 | printf("Set CAM_UNCOR_PARITY\n"); |
| 2555 | } |
| 2556 | #endif |
| 2557 | ahc_set_transaction_status(scb, CAM_UNCOR_PARITY); |
| 2558 | #ifdef AHC_REPORT_UNDERFLOWS |
| 2559 | /* |
| 2560 | * This code is disabled by default as some |
| 2561 | * clients of the SCSI system do not properly |
| 2562 | * initialize the underflow parameter. This |
| 2563 | * results in spurious termination of commands |
| 2564 | * that complete as expected (e.g. underflow is |
| 2565 | * allowed as command can return variable amounts |
| 2566 | * of data. |
| 2567 | */ |
| 2568 | } else if (amount_xferred < scb->io_ctx->underflow) { |
| 2569 | u_int i; |
| 2570 | |
| 2571 | ahc_print_path(ahc, scb); |
| 2572 | printf("CDB:"); |
| 2573 | for (i = 0; i < scb->io_ctx->cmd_len; i++) |
| 2574 | printf(" 0x%x", scb->io_ctx->cmnd[i]); |
| 2575 | printf("\n"); |
| 2576 | ahc_print_path(ahc, scb); |
| 2577 | printf("Saw underflow (%ld of %ld bytes). " |
| 2578 | "Treated as error\n", |
| 2579 | ahc_get_residual(scb), |
| 2580 | ahc_get_transfer_length(scb)); |
| 2581 | ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR); |
| 2582 | #endif |
| 2583 | } else { |
| 2584 | ahc_set_transaction_status(scb, CAM_REQ_CMP); |
| 2585 | } |
| 2586 | } else if (ahc_get_transaction_status(scb) == CAM_SCSI_STATUS_ERROR) { |
| 2587 | ahc_linux_handle_scsi_status(ahc, dev, scb); |
| 2588 | } else if (ahc_get_transaction_status(scb) == CAM_SEL_TIMEOUT) { |
| 2589 | dev->flags |= AHC_DEV_UNCONFIGURED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | |
| 2592 | if (dev->openings == 1 |
| 2593 | && ahc_get_transaction_status(scb) == CAM_REQ_CMP |
| 2594 | && ahc_get_scsi_status(scb) != SCSI_STATUS_QUEUE_FULL) |
| 2595 | dev->tag_success_count++; |
| 2596 | /* |
| 2597 | * Some devices deal with temporary internal resource |
| 2598 | * shortages by returning queue full. When the queue |
| 2599 | * full occurrs, we throttle back. Slowly try to get |
| 2600 | * back to our previous queue depth. |
| 2601 | */ |
| 2602 | if ((dev->openings + dev->active) < dev->maxtags |
| 2603 | && dev->tag_success_count > AHC_TAG_SUCCESS_INTERVAL) { |
| 2604 | dev->tag_success_count = 0; |
| 2605 | dev->openings++; |
| 2606 | } |
| 2607 | |
| 2608 | if (dev->active == 0) |
| 2609 | dev->commands_since_idle_or_otag = 0; |
| 2610 | |
| 2611 | if (TAILQ_EMPTY(&dev->busyq)) { |
| 2612 | if ((dev->flags & AHC_DEV_UNCONFIGURED) != 0 |
| 2613 | && dev->active == 0 |
| 2614 | && (dev->flags & AHC_DEV_TIMER_ACTIVE) == 0) |
| 2615 | ahc_linux_free_device(ahc, dev); |
| 2616 | } else if ((dev->flags & AHC_DEV_ON_RUN_LIST) == 0) { |
| 2617 | TAILQ_INSERT_TAIL(&ahc->platform_data->device_runq, dev, links); |
| 2618 | dev->flags |= AHC_DEV_ON_RUN_LIST; |
| 2619 | } |
| 2620 | |
| 2621 | if ((scb->flags & SCB_RECOVERY_SCB) != 0) { |
| 2622 | printf("Recovery SCB completes\n"); |
| 2623 | if (ahc_get_transaction_status(scb) == CAM_BDR_SENT |
| 2624 | || ahc_get_transaction_status(scb) == CAM_REQ_ABORTED) |
| 2625 | ahc_set_transaction_status(scb, CAM_CMD_TIMEOUT); |
| 2626 | if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) { |
| 2627 | ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE; |
| 2628 | up(&ahc->platform_data->eh_sem); |
| 2629 | } |
| 2630 | } |
| 2631 | |
| 2632 | ahc_free_scb(ahc, scb); |
| 2633 | ahc_linux_queue_cmd_complete(ahc, cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | static void |
| 2637 | ahc_linux_handle_scsi_status(struct ahc_softc *ahc, |
| 2638 | struct ahc_linux_device *dev, struct scb *scb) |
| 2639 | { |
| 2640 | struct ahc_devinfo devinfo; |
| 2641 | |
| 2642 | ahc_compile_devinfo(&devinfo, |
| 2643 | ahc->our_id, |
| 2644 | dev->target->target, dev->lun, |
| 2645 | dev->target->channel == 0 ? 'A' : 'B', |
| 2646 | ROLE_INITIATOR); |
| 2647 | |
| 2648 | /* |
| 2649 | * We don't currently trust the mid-layer to |
| 2650 | * properly deal with queue full or busy. So, |
| 2651 | * when one occurs, we tell the mid-layer to |
| 2652 | * unconditionally requeue the command to us |
| 2653 | * so that we can retry it ourselves. We also |
| 2654 | * implement our own throttling mechanism so |
| 2655 | * we don't clobber the device with too many |
| 2656 | * commands. |
| 2657 | */ |
| 2658 | switch (ahc_get_scsi_status(scb)) { |
| 2659 | default: |
| 2660 | break; |
| 2661 | case SCSI_STATUS_CHECK_COND: |
| 2662 | case SCSI_STATUS_CMD_TERMINATED: |
| 2663 | { |
| 2664 | Scsi_Cmnd *cmd; |
| 2665 | |
| 2666 | /* |
| 2667 | * Copy sense information to the OS's cmd |
| 2668 | * structure if it is available. |
| 2669 | */ |
| 2670 | cmd = scb->io_ctx; |
| 2671 | if (scb->flags & SCB_SENSE) { |
| 2672 | u_int sense_size; |
| 2673 | |
| 2674 | sense_size = MIN(sizeof(struct scsi_sense_data) |
| 2675 | - ahc_get_sense_residual(scb), |
| 2676 | sizeof(cmd->sense_buffer)); |
| 2677 | memcpy(cmd->sense_buffer, |
| 2678 | ahc_get_sense_buf(ahc, scb), sense_size); |
| 2679 | if (sense_size < sizeof(cmd->sense_buffer)) |
| 2680 | memset(&cmd->sense_buffer[sense_size], 0, |
| 2681 | sizeof(cmd->sense_buffer) - sense_size); |
| 2682 | cmd->result |= (DRIVER_SENSE << 24); |
| 2683 | #ifdef AHC_DEBUG |
| 2684 | if (ahc_debug & AHC_SHOW_SENSE) { |
| 2685 | int i; |
| 2686 | |
| 2687 | printf("Copied %d bytes of sense data:", |
| 2688 | sense_size); |
| 2689 | for (i = 0; i < sense_size; i++) { |
| 2690 | if ((i & 0xF) == 0) |
| 2691 | printf("\n"); |
| 2692 | printf("0x%x ", cmd->sense_buffer[i]); |
| 2693 | } |
| 2694 | printf("\n"); |
| 2695 | } |
| 2696 | #endif |
| 2697 | } |
| 2698 | break; |
| 2699 | } |
| 2700 | case SCSI_STATUS_QUEUE_FULL: |
| 2701 | { |
| 2702 | /* |
| 2703 | * By the time the core driver has returned this |
| 2704 | * command, all other commands that were queued |
| 2705 | * to us but not the device have been returned. |
| 2706 | * This ensures that dev->active is equal to |
| 2707 | * the number of commands actually queued to |
| 2708 | * the device. |
| 2709 | */ |
| 2710 | dev->tag_success_count = 0; |
| 2711 | if (dev->active != 0) { |
| 2712 | /* |
| 2713 | * Drop our opening count to the number |
| 2714 | * of commands currently outstanding. |
| 2715 | */ |
| 2716 | dev->openings = 0; |
| 2717 | /* |
| 2718 | ahc_print_path(ahc, scb); |
| 2719 | printf("Dropping tag count to %d\n", dev->active); |
| 2720 | */ |
| 2721 | if (dev->active == dev->tags_on_last_queuefull) { |
| 2722 | |
| 2723 | dev->last_queuefull_same_count++; |
| 2724 | /* |
| 2725 | * If we repeatedly see a queue full |
| 2726 | * at the same queue depth, this |
| 2727 | * device has a fixed number of tag |
| 2728 | * slots. Lock in this tag depth |
| 2729 | * so we stop seeing queue fulls from |
| 2730 | * this device. |
| 2731 | */ |
| 2732 | if (dev->last_queuefull_same_count |
| 2733 | == AHC_LOCK_TAGS_COUNT) { |
| 2734 | dev->maxtags = dev->active; |
| 2735 | ahc_print_path(ahc, scb); |
| 2736 | printf("Locking max tag count at %d\n", |
| 2737 | dev->active); |
| 2738 | } |
| 2739 | } else { |
| 2740 | dev->tags_on_last_queuefull = dev->active; |
| 2741 | dev->last_queuefull_same_count = 0; |
| 2742 | } |
| 2743 | ahc_set_transaction_status(scb, CAM_REQUEUE_REQ); |
| 2744 | ahc_set_scsi_status(scb, SCSI_STATUS_OK); |
| 2745 | ahc_platform_set_tags(ahc, &devinfo, |
| 2746 | (dev->flags & AHC_DEV_Q_BASIC) |
| 2747 | ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED); |
| 2748 | break; |
| 2749 | } |
| 2750 | /* |
| 2751 | * Drop down to a single opening, and treat this |
| 2752 | * as if the target returned BUSY SCSI status. |
| 2753 | */ |
| 2754 | dev->openings = 1; |
| 2755 | ahc_set_scsi_status(scb, SCSI_STATUS_BUSY); |
| 2756 | ahc_platform_set_tags(ahc, &devinfo, |
| 2757 | (dev->flags & AHC_DEV_Q_BASIC) |
| 2758 | ? AHC_QUEUE_BASIC : AHC_QUEUE_TAGGED); |
| 2759 | /* FALLTHROUGH */ |
| 2760 | } |
| 2761 | case SCSI_STATUS_BUSY: |
| 2762 | { |
| 2763 | /* |
| 2764 | * Set a short timer to defer sending commands for |
| 2765 | * a bit since Linux will not delay in this case. |
| 2766 | */ |
| 2767 | if ((dev->flags & AHC_DEV_TIMER_ACTIVE) != 0) { |
| 2768 | printf("%s:%c:%d: Device Timer still active during " |
| 2769 | "busy processing\n", ahc_name(ahc), |
| 2770 | dev->target->channel, dev->target->target); |
| 2771 | break; |
| 2772 | } |
| 2773 | dev->flags |= AHC_DEV_TIMER_ACTIVE; |
| 2774 | dev->qfrozen++; |
| 2775 | init_timer(&dev->timer); |
| 2776 | dev->timer.data = (u_long)dev; |
| 2777 | dev->timer.expires = jiffies + (HZ/2); |
| 2778 | dev->timer.function = ahc_linux_dev_timed_unfreeze; |
| 2779 | add_timer(&dev->timer); |
| 2780 | break; |
| 2781 | } |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | static void |
| 2786 | ahc_linux_queue_cmd_complete(struct ahc_softc *ahc, Scsi_Cmnd *cmd) |
| 2787 | { |
| 2788 | /* |
| 2789 | * Typically, the complete queue has very few entries |
| 2790 | * queued to it before the queue is emptied by |
| 2791 | * ahc_linux_run_complete_queue, so sorting the entries |
| 2792 | * by generation number should be inexpensive. |
| 2793 | * We perform the sort so that commands that complete |
| 2794 | * with an error are retuned in the order origionally |
| 2795 | * queued to the controller so that any subsequent retries |
| 2796 | * are performed in order. The underlying ahc routines do |
| 2797 | * not guarantee the order that aborted commands will be |
| 2798 | * returned to us. |
| 2799 | */ |
| 2800 | struct ahc_completeq *completeq; |
| 2801 | struct ahc_cmd *list_cmd; |
| 2802 | struct ahc_cmd *acmd; |
| 2803 | |
| 2804 | /* |
| 2805 | * Map CAM error codes into Linux Error codes. We |
| 2806 | * avoid the conversion so that the DV code has the |
| 2807 | * full error information available when making |
| 2808 | * state change decisions. |
| 2809 | */ |
| cb62402 | 2005-04-17 18:03:20 -0500 | [diff] [blame] | 2810 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | u_int new_status; |
| 2812 | |
| 2813 | switch (ahc_cmd_get_transaction_status(cmd)) { |
| 2814 | case CAM_REQ_INPROG: |
| 2815 | case CAM_REQ_CMP: |
| 2816 | case CAM_SCSI_STATUS_ERROR: |
| 2817 | new_status = DID_OK; |
| 2818 | break; |
| 2819 | case CAM_REQ_ABORTED: |
| 2820 | new_status = DID_ABORT; |
| 2821 | break; |
| 2822 | case CAM_BUSY: |
| 2823 | new_status = DID_BUS_BUSY; |
| 2824 | break; |
| 2825 | case CAM_REQ_INVALID: |
| 2826 | case CAM_PATH_INVALID: |
| 2827 | new_status = DID_BAD_TARGET; |
| 2828 | break; |
| 2829 | case CAM_SEL_TIMEOUT: |
| 2830 | new_status = DID_NO_CONNECT; |
| 2831 | break; |
| 2832 | case CAM_SCSI_BUS_RESET: |
| 2833 | case CAM_BDR_SENT: |
| 2834 | new_status = DID_RESET; |
| 2835 | break; |
| 2836 | case CAM_UNCOR_PARITY: |
| 2837 | new_status = DID_PARITY; |
| 2838 | break; |
| 2839 | case CAM_CMD_TIMEOUT: |
| 2840 | new_status = DID_TIME_OUT; |
| 2841 | break; |
| 2842 | case CAM_UA_ABORT: |
| 2843 | case CAM_REQ_CMP_ERR: |
| 2844 | case CAM_AUTOSENSE_FAIL: |
| 2845 | case CAM_NO_HBA: |
| 2846 | case CAM_DATA_RUN_ERR: |
| 2847 | case CAM_UNEXP_BUSFREE: |
| 2848 | case CAM_SEQUENCE_FAIL: |
| 2849 | case CAM_CCB_LEN_ERR: |
| 2850 | case CAM_PROVIDE_FAIL: |
| 2851 | case CAM_REQ_TERMIO: |
| 2852 | case CAM_UNREC_HBA_ERROR: |
| 2853 | case CAM_REQ_TOO_BIG: |
| 2854 | new_status = DID_ERROR; |
| 2855 | break; |
| 2856 | case CAM_REQUEUE_REQ: |
| 2857 | /* |
| 2858 | * If we want the request requeued, make sure there |
| 2859 | * are sufficent retries. In the old scsi error code, |
| 2860 | * we used to be able to specify a result code that |
| 2861 | * bypassed the retry count. Now we must use this |
| 2862 | * hack. We also "fake" a check condition with |
| 2863 | * a sense code of ABORTED COMMAND. This seems to |
| 2864 | * evoke a retry even if this command is being sent |
| 2865 | * via the eh thread. Ick! Ick! Ick! |
| 2866 | */ |
| 2867 | if (cmd->retries > 0) |
| 2868 | cmd->retries--; |
| 2869 | new_status = DID_OK; |
| 2870 | ahc_cmd_set_scsi_status(cmd, SCSI_STATUS_CHECK_COND); |
| 2871 | cmd->result |= (DRIVER_SENSE << 24); |
| 2872 | memset(cmd->sense_buffer, 0, |
| 2873 | sizeof(cmd->sense_buffer)); |
| 2874 | cmd->sense_buffer[0] = SSD_ERRCODE_VALID |
| 2875 | | SSD_CURRENT_ERROR; |
| 2876 | cmd->sense_buffer[2] = SSD_KEY_ABORTED_COMMAND; |
| 2877 | break; |
| 2878 | default: |
| 2879 | /* We should never get here */ |
| 2880 | new_status = DID_ERROR; |
| 2881 | break; |
| 2882 | } |
| 2883 | |
| 2884 | ahc_cmd_set_transaction_status(cmd, new_status); |
| 2885 | } |
| 2886 | |
| 2887 | completeq = &ahc->platform_data->completeq; |
| 2888 | list_cmd = TAILQ_FIRST(completeq); |
| 2889 | acmd = (struct ahc_cmd *)cmd; |
| 2890 | while (list_cmd != NULL |
| 2891 | && acmd_scsi_cmd(list_cmd).serial_number |
| 2892 | < acmd_scsi_cmd(acmd).serial_number) |
| 2893 | list_cmd = TAILQ_NEXT(list_cmd, acmd_links.tqe); |
| 2894 | if (list_cmd != NULL) |
| 2895 | TAILQ_INSERT_BEFORE(list_cmd, acmd, acmd_links.tqe); |
| 2896 | else |
| 2897 | TAILQ_INSERT_TAIL(completeq, acmd, acmd_links.tqe); |
| 2898 | } |
| 2899 | |
| 2900 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | ahc_linux_sem_timeout(u_long arg) |
| 2902 | { |
| 2903 | struct ahc_softc *ahc; |
| 2904 | u_long s; |
| 2905 | |
| 2906 | ahc = (struct ahc_softc *)arg; |
| 2907 | |
| 2908 | ahc_lock(ahc, &s); |
| 2909 | if ((ahc->platform_data->flags & AHC_UP_EH_SEMAPHORE) != 0) { |
| 2910 | ahc->platform_data->flags &= ~AHC_UP_EH_SEMAPHORE; |
| 2911 | up(&ahc->platform_data->eh_sem); |
| 2912 | } |
| 2913 | ahc_unlock(ahc, &s); |
| 2914 | } |
| 2915 | |
| 2916 | static void |
| 2917 | ahc_linux_freeze_simq(struct ahc_softc *ahc) |
| 2918 | { |
| 2919 | ahc->platform_data->qfrozen++; |
| 2920 | if (ahc->platform_data->qfrozen == 1) { |
| 2921 | scsi_block_requests(ahc->platform_data->host); |
| 2922 | |
| 2923 | /* XXX What about Twin channels? */ |
| 2924 | ahc_platform_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS, |
| 2925 | CAM_LUN_WILDCARD, SCB_LIST_NULL, |
| 2926 | ROLE_INITIATOR, CAM_REQUEUE_REQ); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | static void |
| 2931 | ahc_linux_release_simq(u_long arg) |
| 2932 | { |
| 2933 | struct ahc_softc *ahc; |
| 2934 | u_long s; |
| 2935 | int unblock_reqs; |
| 2936 | |
| 2937 | ahc = (struct ahc_softc *)arg; |
| 2938 | |
| 2939 | unblock_reqs = 0; |
| 2940 | ahc_lock(ahc, &s); |
| 2941 | if (ahc->platform_data->qfrozen > 0) |
| 2942 | ahc->platform_data->qfrozen--; |
| 2943 | if (ahc->platform_data->qfrozen == 0) |
| 2944 | unblock_reqs = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | ahc_schedule_runq(ahc); |
| 2946 | ahc_unlock(ahc, &s); |
| 2947 | /* |
| 2948 | * There is still a race here. The mid-layer |
| 2949 | * should keep its own freeze count and use |
| 2950 | * a bottom half handler to run the queues |
| 2951 | * so we can unblock with our own lock held. |
| 2952 | */ |
| 2953 | if (unblock_reqs) |
| 2954 | scsi_unblock_requests(ahc->platform_data->host); |
| 2955 | } |
| 2956 | |
| 2957 | static void |
| 2958 | ahc_linux_dev_timed_unfreeze(u_long arg) |
| 2959 | { |
| 2960 | struct ahc_linux_device *dev; |
| 2961 | struct ahc_softc *ahc; |
| 2962 | u_long s; |
| 2963 | |
| 2964 | dev = (struct ahc_linux_device *)arg; |
| 2965 | ahc = dev->target->ahc; |
| 2966 | ahc_lock(ahc, &s); |
| 2967 | dev->flags &= ~AHC_DEV_TIMER_ACTIVE; |
| 2968 | if (dev->qfrozen > 0) |
| 2969 | dev->qfrozen--; |
| 2970 | if (dev->qfrozen == 0 |
| 2971 | && (dev->flags & AHC_DEV_ON_RUN_LIST) == 0) |
| 2972 | ahc_linux_run_device_queue(ahc, dev); |
| 2973 | if (TAILQ_EMPTY(&dev->busyq) |
| 2974 | && dev->active == 0) |
| 2975 | __ahc_linux_free_device(ahc, dev); |
| 2976 | ahc_unlock(ahc, &s); |
| 2977 | } |
| 2978 | |
| 2979 | static int |
| 2980 | ahc_linux_queue_recovery_cmd(Scsi_Cmnd *cmd, scb_flag flag) |
| 2981 | { |
| 2982 | struct ahc_softc *ahc; |
| 2983 | struct ahc_cmd *acmd; |
| 2984 | struct ahc_cmd *list_acmd; |
| 2985 | struct ahc_linux_device *dev; |
| 2986 | struct scb *pending_scb; |
| 2987 | u_long s; |
| 2988 | u_int saved_scbptr; |
| 2989 | u_int active_scb_index; |
| 2990 | u_int last_phase; |
| 2991 | u_int saved_scsiid; |
| 2992 | u_int cdb_byte; |
| 2993 | int retval; |
| 2994 | int was_paused; |
| 2995 | int paused; |
| 2996 | int wait; |
| 2997 | int disconnected; |
| 2998 | |
| 2999 | pending_scb = NULL; |
| 3000 | paused = FALSE; |
| 3001 | wait = FALSE; |
| 3002 | ahc = *(struct ahc_softc **)cmd->device->host->hostdata; |
| 3003 | acmd = (struct ahc_cmd *)cmd; |
| 3004 | |
| 3005 | printf("%s:%d:%d:%d: Attempting to queue a%s message\n", |
| 3006 | ahc_name(ahc), cmd->device->channel, |
| 3007 | cmd->device->id, cmd->device->lun, |
| 3008 | flag == SCB_ABORT ? "n ABORT" : " TARGET RESET"); |
| 3009 | |
| 3010 | printf("CDB:"); |
| 3011 | for (cdb_byte = 0; cdb_byte < cmd->cmd_len; cdb_byte++) |
| 3012 | printf(" 0x%x", cmd->cmnd[cdb_byte]); |
| 3013 | printf("\n"); |
| 3014 | |
| 3015 | /* |
| 3016 | * In all versions of Linux, we have to work around |
| 3017 | * a major flaw in how the mid-layer is locked down |
| 3018 | * if we are to sleep successfully in our error handler |
| 3019 | * while allowing our interrupt handler to run. Since |
| 3020 | * the midlayer acquires either the io_request_lock or |
| 3021 | * our lock prior to calling us, we must use the |
| 3022 | * spin_unlock_irq() method for unlocking our lock. |
| 3023 | * This will force interrupts to be enabled on the |
| 3024 | * current CPU. Since the EH thread should not have |
| 3025 | * been running with CPU interrupts disabled other than |
| 3026 | * by acquiring either the io_request_lock or our own |
| 3027 | * lock, this *should* be safe. |
| 3028 | */ |
| 3029 | ahc_midlayer_entrypoint_lock(ahc, &s); |
| 3030 | |
| 3031 | /* |
| 3032 | * First determine if we currently own this command. |
| 3033 | * Start by searching the device queue. If not found |
| 3034 | * there, check the pending_scb list. If not found |
| 3035 | * at all, and the system wanted us to just abort the |
| 3036 | * command, return success. |
| 3037 | */ |
| 3038 | dev = ahc_linux_get_device(ahc, cmd->device->channel, cmd->device->id, |
| 3039 | cmd->device->lun, /*alloc*/FALSE); |
| 3040 | |
| 3041 | if (dev == NULL) { |
| 3042 | /* |
| 3043 | * No target device for this command exists, |
| 3044 | * so we must not still own the command. |
| 3045 | */ |
| 3046 | printf("%s:%d:%d:%d: Is not an active device\n", |
| 3047 | ahc_name(ahc), cmd->device->channel, cmd->device->id, |
| 3048 | cmd->device->lun); |
| 3049 | retval = SUCCESS; |
| 3050 | goto no_cmd; |
| 3051 | } |
| 3052 | |
| 3053 | TAILQ_FOREACH(list_acmd, &dev->busyq, acmd_links.tqe) { |
| 3054 | if (list_acmd == acmd) |
| 3055 | break; |
| 3056 | } |
| 3057 | |
| 3058 | if (list_acmd != NULL) { |
| 3059 | printf("%s:%d:%d:%d: Command found on device queue\n", |
| 3060 | ahc_name(ahc), cmd->device->channel, cmd->device->id, |
| 3061 | cmd->device->lun); |
| 3062 | if (flag == SCB_ABORT) { |
| 3063 | TAILQ_REMOVE(&dev->busyq, list_acmd, acmd_links.tqe); |
| 3064 | cmd->result = DID_ABORT << 16; |
| 3065 | ahc_linux_queue_cmd_complete(ahc, cmd); |
| 3066 | retval = SUCCESS; |
| 3067 | goto done; |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | if ((dev->flags & (AHC_DEV_Q_BASIC|AHC_DEV_Q_TAGGED)) == 0 |
| 3072 | && ahc_search_untagged_queues(ahc, cmd, cmd->device->id, |
| 3073 | cmd->device->channel + 'A', |
| 3074 | cmd->device->lun, |
| 3075 | CAM_REQ_ABORTED, SEARCH_COMPLETE) != 0) { |
| 3076 | printf("%s:%d:%d:%d: Command found on untagged queue\n", |
| 3077 | ahc_name(ahc), cmd->device->channel, cmd->device->id, |
| 3078 | cmd->device->lun); |
| 3079 | retval = SUCCESS; |
| 3080 | goto done; |
| 3081 | } |
| 3082 | |
| 3083 | /* |
| 3084 | * See if we can find a matching cmd in the pending list. |
| 3085 | */ |
| 3086 | LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) { |
| 3087 | if (pending_scb->io_ctx == cmd) |
| 3088 | break; |
| 3089 | } |
| 3090 | |
| 3091 | if (pending_scb == NULL && flag == SCB_DEVICE_RESET) { |
| 3092 | |
| 3093 | /* Any SCB for this device will do for a target reset */ |
| 3094 | LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) { |
| 3095 | if (ahc_match_scb(ahc, pending_scb, cmd->device->id, |
| 3096 | cmd->device->channel + 'A', |
| 3097 | CAM_LUN_WILDCARD, |
| 3098 | SCB_LIST_NULL, ROLE_INITIATOR) == 0) |
| 3099 | break; |
| 3100 | } |
| 3101 | } |
| 3102 | |
| 3103 | if (pending_scb == NULL) { |
| 3104 | printf("%s:%d:%d:%d: Command not found\n", |
| 3105 | ahc_name(ahc), cmd->device->channel, cmd->device->id, |
| 3106 | cmd->device->lun); |
| 3107 | goto no_cmd; |
| 3108 | } |
| 3109 | |
| 3110 | if ((pending_scb->flags & SCB_RECOVERY_SCB) != 0) { |
| 3111 | /* |
| 3112 | * We can't queue two recovery actions using the same SCB |
| 3113 | */ |
| 3114 | retval = FAILED; |
| 3115 | goto done; |
| 3116 | } |
| 3117 | |
| 3118 | /* |
| 3119 | * Ensure that the card doesn't do anything |
| 3120 | * behind our back and that we didn't "just" miss |
| 3121 | * an interrupt that would affect this cmd. |
| 3122 | */ |
| 3123 | was_paused = ahc_is_paused(ahc); |
| 3124 | ahc_pause_and_flushwork(ahc); |
| 3125 | paused = TRUE; |
| 3126 | |
| 3127 | if ((pending_scb->flags & SCB_ACTIVE) == 0) { |
| 3128 | printf("%s:%d:%d:%d: Command already completed\n", |
| 3129 | ahc_name(ahc), cmd->device->channel, cmd->device->id, |
| 3130 | cmd->device->lun); |
| 3131 | goto no_cmd; |
| 3132 | } |
| 3133 | |
| 3134 | printf("%s: At time of recovery, card was %spaused\n", |
| 3135 | ahc_name(ahc), was_paused ? "" : "not "); |
| 3136 | ahc_dump_card_state(ahc); |
| 3137 | |
| 3138 | disconnected = TRUE; |
| 3139 | if (flag == SCB_ABORT) { |
| 3140 | if (ahc_search_qinfifo(ahc, cmd->device->id, |
| 3141 | cmd->device->channel + 'A', |
| 3142 | cmd->device->lun, |
| 3143 | pending_scb->hscb->tag, |
| 3144 | ROLE_INITIATOR, CAM_REQ_ABORTED, |
| 3145 | SEARCH_COMPLETE) > 0) { |
| 3146 | printf("%s:%d:%d:%d: Cmd aborted from QINFIFO\n", |
| 3147 | ahc_name(ahc), cmd->device->channel, |
| 3148 | cmd->device->id, cmd->device->lun); |
| 3149 | retval = SUCCESS; |
| 3150 | goto done; |
| 3151 | } |
| 3152 | } else if (ahc_search_qinfifo(ahc, cmd->device->id, |
| 3153 | cmd->device->channel + 'A', |
| 3154 | cmd->device->lun, pending_scb->hscb->tag, |
| 3155 | ROLE_INITIATOR, /*status*/0, |
| 3156 | SEARCH_COUNT) > 0) { |
| 3157 | disconnected = FALSE; |
| 3158 | } |
| 3159 | |
| 3160 | if (disconnected && (ahc_inb(ahc, SEQ_FLAGS) & NOT_IDENTIFIED) == 0) { |
| 3161 | struct scb *bus_scb; |
| 3162 | |
| 3163 | bus_scb = ahc_lookup_scb(ahc, ahc_inb(ahc, SCB_TAG)); |
| 3164 | if (bus_scb == pending_scb) |
| 3165 | disconnected = FALSE; |
| 3166 | else if (flag != SCB_ABORT |
| 3167 | && ahc_inb(ahc, SAVED_SCSIID) == pending_scb->hscb->scsiid |
| 3168 | && ahc_inb(ahc, SAVED_LUN) == SCB_GET_LUN(pending_scb)) |
| 3169 | disconnected = FALSE; |
| 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | * At this point, pending_scb is the scb associated with the |
| 3174 | * passed in command. That command is currently active on the |
| 3175 | * bus, is in the disconnected state, or we're hoping to find |
| 3176 | * a command for the same target active on the bus to abuse to |
| 3177 | * send a BDR. Queue the appropriate message based on which of |
| 3178 | * these states we are in. |
| 3179 | */ |
| 3180 | last_phase = ahc_inb(ahc, LASTPHASE); |
| 3181 | saved_scbptr = ahc_inb(ahc, SCBPTR); |
| 3182 | active_scb_index = ahc_inb(ahc, SCB_TAG); |
| 3183 | saved_scsiid = ahc_inb(ahc, SAVED_SCSIID); |
| 3184 | if (last_phase != P_BUSFREE |
| 3185 | && (pending_scb->hscb->tag == active_scb_index |
| 3186 | || (flag == SCB_DEVICE_RESET |
| 3187 | && SCSIID_TARGET(ahc, saved_scsiid) == cmd->device->id))) { |
| 3188 | |
| 3189 | /* |
| 3190 | * We're active on the bus, so assert ATN |
| 3191 | * and hope that the target responds. |
| 3192 | */ |
| 3193 | pending_scb = ahc_lookup_scb(ahc, active_scb_index); |
| 3194 | pending_scb->flags |= SCB_RECOVERY_SCB|flag; |
| 3195 | ahc_outb(ahc, MSG_OUT, HOST_MSG); |
| 3196 | ahc_outb(ahc, SCSISIGO, last_phase|ATNO); |
| 3197 | printf("%s:%d:%d:%d: Device is active, asserting ATN\n", |
| 3198 | ahc_name(ahc), cmd->device->channel, cmd->device->id, |
| 3199 | cmd->device->lun); |
| 3200 | wait = TRUE; |
| 3201 | } else if (disconnected) { |
| 3202 | |
| 3203 | /* |
| 3204 | * Actually re-queue this SCB in an attempt |
| 3205 | * to select the device before it reconnects. |
| 3206 | * In either case (selection or reselection), |
| 3207 | * we will now issue the approprate message |
| 3208 | * to the timed-out device. |
| 3209 | * |
| 3210 | * Set the MK_MESSAGE control bit indicating |
| 3211 | * that we desire to send a message. We |
| 3212 | * also set the disconnected flag since |
| 3213 | * in the paging case there is no guarantee |
| 3214 | * that our SCB control byte matches the |
| 3215 | * version on the card. We don't want the |
| 3216 | * sequencer to abort the command thinking |
| 3217 | * an unsolicited reselection occurred. |
| 3218 | */ |
| 3219 | pending_scb->hscb->control |= MK_MESSAGE|DISCONNECTED; |
| 3220 | pending_scb->flags |= SCB_RECOVERY_SCB|flag; |
| 3221 | |
| 3222 | /* |
| 3223 | * Remove any cached copy of this SCB in the |
| 3224 | * disconnected list in preparation for the |
| 3225 | * queuing of our abort SCB. We use the |
| 3226 | * same element in the SCB, SCB_NEXT, for |
| 3227 | * both the qinfifo and the disconnected list. |
| 3228 | */ |
| 3229 | ahc_search_disc_list(ahc, cmd->device->id, |
| 3230 | cmd->device->channel + 'A', |
| 3231 | cmd->device->lun, pending_scb->hscb->tag, |
| 3232 | /*stop_on_first*/TRUE, |
| 3233 | /*remove*/TRUE, |
| 3234 | /*save_state*/FALSE); |
| 3235 | |
| 3236 | /* |
| 3237 | * In the non-paging case, the sequencer will |
| 3238 | * never re-reference the in-core SCB. |
| 3239 | * To make sure we are notified during |
| 3240 | * reslection, set the MK_MESSAGE flag in |
| 3241 | * the card's copy of the SCB. |
| 3242 | */ |
| 3243 | if ((ahc->flags & AHC_PAGESCBS) == 0) { |
| 3244 | ahc_outb(ahc, SCBPTR, pending_scb->hscb->tag); |
| 3245 | ahc_outb(ahc, SCB_CONTROL, |
| 3246 | ahc_inb(ahc, SCB_CONTROL)|MK_MESSAGE); |
| 3247 | } |
| 3248 | |
| 3249 | /* |
| 3250 | * Clear out any entries in the QINFIFO first |
| 3251 | * so we are the next SCB for this target |
| 3252 | * to run. |
| 3253 | */ |
| 3254 | ahc_search_qinfifo(ahc, cmd->device->id, |
| 3255 | cmd->device->channel + 'A', |
| 3256 | cmd->device->lun, SCB_LIST_NULL, |
| 3257 | ROLE_INITIATOR, CAM_REQUEUE_REQ, |
| 3258 | SEARCH_COMPLETE); |
| 3259 | ahc_qinfifo_requeue_tail(ahc, pending_scb); |
| 3260 | ahc_outb(ahc, SCBPTR, saved_scbptr); |
| 3261 | ahc_print_path(ahc, pending_scb); |
| 3262 | printf("Device is disconnected, re-queuing SCB\n"); |
| 3263 | wait = TRUE; |
| 3264 | } else { |
| 3265 | printf("%s:%d:%d:%d: Unable to deliver message\n", |
| 3266 | ahc_name(ahc), cmd->device->channel, cmd->device->id, |
| 3267 | cmd->device->lun); |
| 3268 | retval = FAILED; |
| 3269 | goto done; |
| 3270 | } |
| 3271 | |
| 3272 | no_cmd: |
| 3273 | /* |
| 3274 | * Our assumption is that if we don't have the command, no |
| 3275 | * recovery action was required, so we return success. Again, |
| 3276 | * the semantics of the mid-layer recovery engine are not |
| 3277 | * well defined, so this may change in time. |
| 3278 | */ |
| 3279 | retval = SUCCESS; |
| 3280 | done: |
| 3281 | if (paused) |
| 3282 | ahc_unpause(ahc); |
| 3283 | if (wait) { |
| 3284 | struct timer_list timer; |
| 3285 | int ret; |
| 3286 | |
| 3287 | ahc->platform_data->flags |= AHC_UP_EH_SEMAPHORE; |
| 3288 | spin_unlock_irq(&ahc->platform_data->spin_lock); |
| 3289 | init_timer(&timer); |
| 3290 | timer.data = (u_long)ahc; |
| 3291 | timer.expires = jiffies + (5 * HZ); |
| 3292 | timer.function = ahc_linux_sem_timeout; |
| 3293 | add_timer(&timer); |
| 3294 | printf("Recovery code sleeping\n"); |
| 3295 | down(&ahc->platform_data->eh_sem); |
| 3296 | printf("Recovery code awake\n"); |
| 3297 | ret = del_timer_sync(&timer); |
| 3298 | if (ret == 0) { |
| 3299 | printf("Timer Expired\n"); |
| 3300 | retval = FAILED; |
| 3301 | } |
| 3302 | spin_lock_irq(&ahc->platform_data->spin_lock); |
| 3303 | } |
| 3304 | ahc_schedule_runq(ahc); |
| 3305 | ahc_linux_run_complete_queue(ahc); |
| 3306 | ahc_midlayer_entrypoint_unlock(ahc, &s); |
| 3307 | return (retval); |
| 3308 | } |
| 3309 | |
| 3310 | void |
| 3311 | ahc_platform_dump_card_state(struct ahc_softc *ahc) |
| 3312 | { |
| 3313 | struct ahc_linux_device *dev; |
| 3314 | int channel; |
| 3315 | int maxchannel; |
| 3316 | int target; |
| 3317 | int maxtarget; |
| 3318 | int lun; |
| 3319 | int i; |
| 3320 | |
| 3321 | maxchannel = (ahc->features & AHC_TWIN) ? 1 : 0; |
| 3322 | maxtarget = (ahc->features & AHC_WIDE) ? 15 : 7; |
| 3323 | for (channel = 0; channel <= maxchannel; channel++) { |
| 3324 | |
| 3325 | for (target = 0; target <=maxtarget; target++) { |
| 3326 | |
| 3327 | for (lun = 0; lun < AHC_NUM_LUNS; lun++) { |
| 3328 | struct ahc_cmd *acmd; |
| 3329 | |
| 3330 | dev = ahc_linux_get_device(ahc, channel, target, |
| 3331 | lun, /*alloc*/FALSE); |
| 3332 | if (dev == NULL) |
| 3333 | continue; |
| 3334 | |
| 3335 | printf("DevQ(%d:%d:%d): ", |
| 3336 | channel, target, lun); |
| 3337 | i = 0; |
| 3338 | TAILQ_FOREACH(acmd, &dev->busyq, |
| 3339 | acmd_links.tqe) { |
| 3340 | if (i++ > AHC_SCB_MAX) |
| 3341 | break; |
| 3342 | } |
| 3343 | printf("%d waiting\n", i); |
| 3344 | } |
| 3345 | } |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | static void ahc_linux_exit(void); |
| 3350 | |
| 92d161c | 2005-04-17 16:59:33 -0500 | [diff] [blame] | 3351 | static void ahc_linux_get_period(struct scsi_target *starget) |
| 3352 | { |
| 3353 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3354 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3355 | struct ahc_tmode_tstate *tstate; |
| 3356 | struct ahc_initiator_tinfo *tinfo |
| 3357 | = ahc_fetch_transinfo(ahc, |
| 3358 | starget->channel + 'A', |
| 3359 | shost->this_id, starget->id, &tstate); |
| 3360 | spi_period(starget) = tinfo->curr.period; |
| 3361 | } |
| 3362 | |
| 3363 | static void ahc_linux_set_period(struct scsi_target *starget, int period) |
| 3364 | { |
| 3365 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3366 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3367 | struct ahc_tmode_tstate *tstate; |
| 3368 | struct ahc_initiator_tinfo *tinfo |
| 3369 | = ahc_fetch_transinfo(ahc, |
| 3370 | starget->channel + 'A', |
| 3371 | shost->this_id, starget->id, &tstate); |
| 3372 | struct ahc_devinfo devinfo; |
| 3373 | unsigned int ppr_options = tinfo->curr.ppr_options; |
| 3374 | unsigned long flags; |
| 3375 | unsigned long offset = tinfo->curr.offset; |
| 3376 | struct ahc_syncrate *syncrate; |
| 3377 | |
| 3378 | if (offset == 0) |
| 3379 | offset = MAX_OFFSET; |
| 3380 | |
| 3381 | ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0, |
| 3382 | starget->channel + 'A', ROLE_INITIATOR); |
| 3383 | syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT); |
| 3384 | ahc_lock(ahc, &flags); |
| 3385 | ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset, |
| 3386 | ppr_options, AHC_TRANS_GOAL, FALSE); |
| 3387 | ahc_unlock(ahc, &flags); |
| 3388 | } |
| 3389 | |
| 3390 | static void ahc_linux_get_offset(struct scsi_target *starget) |
| 3391 | { |
| 3392 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3393 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3394 | struct ahc_tmode_tstate *tstate; |
| 3395 | struct ahc_initiator_tinfo *tinfo |
| 3396 | = ahc_fetch_transinfo(ahc, |
| 3397 | starget->channel + 'A', |
| 3398 | shost->this_id, starget->id, &tstate); |
| 3399 | spi_offset(starget) = tinfo->curr.offset; |
| 3400 | } |
| 3401 | |
| 3402 | static void ahc_linux_set_offset(struct scsi_target *starget, int offset) |
| 3403 | { |
| 3404 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3405 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3406 | struct ahc_tmode_tstate *tstate; |
| 3407 | struct ahc_initiator_tinfo *tinfo |
| 3408 | = ahc_fetch_transinfo(ahc, |
| 3409 | starget->channel + 'A', |
| 3410 | shost->this_id, starget->id, &tstate); |
| 3411 | struct ahc_devinfo devinfo; |
| 3412 | unsigned int ppr_options = 0; |
| 3413 | unsigned int period = 0; |
| 3414 | unsigned long flags; |
| 3415 | struct ahc_syncrate *syncrate = NULL; |
| 3416 | |
| 3417 | ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0, |
| 3418 | starget->channel + 'A', ROLE_INITIATOR); |
| 3419 | if (offset != 0) { |
| 3420 | syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, AHC_SYNCRATE_DT); |
| 3421 | period = tinfo->curr.period; |
| 3422 | ppr_options = tinfo->curr.ppr_options; |
| 3423 | } |
| 3424 | ahc_lock(ahc, &flags); |
| 3425 | ahc_set_syncrate(ahc, &devinfo, syncrate, period, offset, |
| 3426 | ppr_options, AHC_TRANS_GOAL, FALSE); |
| 3427 | ahc_unlock(ahc, &flags); |
| 3428 | } |
| 3429 | |
| 3430 | static void ahc_linux_get_width(struct scsi_target *starget) |
| 3431 | { |
| 3432 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3433 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3434 | struct ahc_tmode_tstate *tstate; |
| 3435 | struct ahc_initiator_tinfo *tinfo |
| 3436 | = ahc_fetch_transinfo(ahc, |
| 3437 | starget->channel + 'A', |
| 3438 | shost->this_id, starget->id, &tstate); |
| 3439 | spi_width(starget) = tinfo->curr.width; |
| 3440 | } |
| 3441 | |
| 3442 | static void ahc_linux_set_width(struct scsi_target *starget, int width) |
| 3443 | { |
| 3444 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3445 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3446 | struct ahc_devinfo devinfo; |
| 3447 | unsigned long flags; |
| 3448 | |
| 3449 | ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0, |
| 3450 | starget->channel + 'A', ROLE_INITIATOR); |
| 3451 | ahc_lock(ahc, &flags); |
| 3452 | ahc_set_width(ahc, &devinfo, width, AHC_TRANS_GOAL, FALSE); |
| 3453 | ahc_unlock(ahc, &flags); |
| 3454 | } |
| 3455 | |
| 3456 | static void ahc_linux_get_dt(struct scsi_target *starget) |
| 3457 | { |
| 3458 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3459 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3460 | struct ahc_tmode_tstate *tstate; |
| 3461 | struct ahc_initiator_tinfo *tinfo |
| 3462 | = ahc_fetch_transinfo(ahc, |
| 3463 | starget->channel + 'A', |
| 3464 | shost->this_id, starget->id, &tstate); |
| 3465 | spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_DT_REQ; |
| 3466 | } |
| 3467 | |
| 3468 | static void ahc_linux_set_dt(struct scsi_target *starget, int dt) |
| 3469 | { |
| 3470 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3471 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3472 | struct ahc_tmode_tstate *tstate; |
| 3473 | struct ahc_initiator_tinfo *tinfo |
| 3474 | = ahc_fetch_transinfo(ahc, |
| 3475 | starget->channel + 'A', |
| 3476 | shost->this_id, starget->id, &tstate); |
| 3477 | struct ahc_devinfo devinfo; |
| 3478 | unsigned int ppr_options = tinfo->curr.ppr_options |
| 3479 | & ~MSG_EXT_PPR_DT_REQ; |
| 3480 | unsigned int period = tinfo->curr.period; |
| 3481 | unsigned long flags; |
| 3482 | struct ahc_syncrate *syncrate; |
| 3483 | |
| 3484 | ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0, |
| 3485 | starget->channel + 'A', ROLE_INITIATOR); |
| 3486 | syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, |
| 3487 | dt ? AHC_SYNCRATE_DT : AHC_SYNCRATE_ULTRA2); |
| 3488 | ahc_lock(ahc, &flags); |
| 3489 | ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset, |
| 3490 | ppr_options, AHC_TRANS_GOAL, FALSE); |
| 3491 | ahc_unlock(ahc, &flags); |
| 3492 | } |
| 3493 | |
| 3494 | static void ahc_linux_get_qas(struct scsi_target *starget) |
| 3495 | { |
| 3496 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3497 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3498 | struct ahc_tmode_tstate *tstate; |
| 3499 | struct ahc_initiator_tinfo *tinfo |
| 3500 | = ahc_fetch_transinfo(ahc, |
| 3501 | starget->channel + 'A', |
| 3502 | shost->this_id, starget->id, &tstate); |
| 3503 | spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_QAS_REQ; |
| 3504 | } |
| 3505 | |
| 3506 | static void ahc_linux_set_qas(struct scsi_target *starget, int qas) |
| 3507 | { |
| 3508 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3509 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3510 | struct ahc_tmode_tstate *tstate; |
| 3511 | struct ahc_initiator_tinfo *tinfo |
| 3512 | = ahc_fetch_transinfo(ahc, |
| 3513 | starget->channel + 'A', |
| 3514 | shost->this_id, starget->id, &tstate); |
| 3515 | struct ahc_devinfo devinfo; |
| 3516 | unsigned int ppr_options = tinfo->curr.ppr_options |
| 3517 | & ~MSG_EXT_PPR_QAS_REQ; |
| 3518 | unsigned int period = tinfo->curr.period; |
| 3519 | unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ; |
| 3520 | unsigned long flags; |
| 3521 | struct ahc_syncrate *syncrate; |
| 3522 | |
| 3523 | if (qas) |
| 3524 | ppr_options |= MSG_EXT_PPR_QAS_REQ; |
| 3525 | |
| 3526 | ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0, |
| 3527 | starget->channel + 'A', ROLE_INITIATOR); |
| 3528 | syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, |
| 3529 | dt ? AHC_SYNCRATE_DT : AHC_SYNCRATE_ULTRA2); |
| 3530 | ahc_lock(ahc, &flags); |
| 3531 | ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset, |
| 3532 | ppr_options, AHC_TRANS_GOAL, FALSE); |
| 3533 | ahc_unlock(ahc, &flags); |
| 3534 | } |
| 3535 | |
| 3536 | static void ahc_linux_get_iu(struct scsi_target *starget) |
| 3537 | { |
| 3538 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3539 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3540 | struct ahc_tmode_tstate *tstate; |
| 3541 | struct ahc_initiator_tinfo *tinfo |
| 3542 | = ahc_fetch_transinfo(ahc, |
| 3543 | starget->channel + 'A', |
| 3544 | shost->this_id, starget->id, &tstate); |
| 3545 | spi_dt(starget) = tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ; |
| 3546 | } |
| 3547 | |
| 3548 | static void ahc_linux_set_iu(struct scsi_target *starget, int iu) |
| 3549 | { |
| 3550 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 3551 | struct ahc_softc *ahc = *((struct ahc_softc **)shost->hostdata); |
| 3552 | struct ahc_tmode_tstate *tstate; |
| 3553 | struct ahc_initiator_tinfo *tinfo |
| 3554 | = ahc_fetch_transinfo(ahc, |
| 3555 | starget->channel + 'A', |
| 3556 | shost->this_id, starget->id, &tstate); |
| 3557 | struct ahc_devinfo devinfo; |
| 3558 | unsigned int ppr_options = tinfo->curr.ppr_options |
| 3559 | & ~MSG_EXT_PPR_IU_REQ; |
| 3560 | unsigned int period = tinfo->curr.period; |
| 3561 | unsigned int dt = ppr_options & MSG_EXT_PPR_DT_REQ; |
| 3562 | unsigned long flags; |
| 3563 | struct ahc_syncrate *syncrate; |
| 3564 | |
| 3565 | if (iu) |
| 3566 | ppr_options |= MSG_EXT_PPR_IU_REQ; |
| 3567 | |
| 3568 | ahc_compile_devinfo(&devinfo, shost->this_id, starget->id, 0, |
| 3569 | starget->channel + 'A', ROLE_INITIATOR); |
| 3570 | syncrate = ahc_find_syncrate(ahc, &period, &ppr_options, |
| 3571 | dt ? AHC_SYNCRATE_DT : AHC_SYNCRATE_ULTRA2); |
| 3572 | ahc_lock(ahc, &flags); |
| 3573 | ahc_set_syncrate(ahc, &devinfo, syncrate, period, tinfo->curr.offset, |
| 3574 | ppr_options, AHC_TRANS_GOAL, FALSE); |
| 3575 | ahc_unlock(ahc, &flags); |
| 3576 | } |
| 3577 | |
| 3578 | static struct spi_function_template ahc_linux_transport_functions = { |
| 3579 | .get_offset = ahc_linux_get_offset, |
| 3580 | .set_offset = ahc_linux_set_offset, |
| 3581 | .show_offset = 1, |
| 3582 | .get_period = ahc_linux_get_period, |
| 3583 | .set_period = ahc_linux_set_period, |
| 3584 | .show_period = 1, |
| 3585 | .get_width = ahc_linux_get_width, |
| 3586 | .set_width = ahc_linux_set_width, |
| 3587 | .show_width = 1, |
| 3588 | .get_dt = ahc_linux_get_dt, |
| 3589 | .set_dt = ahc_linux_set_dt, |
| 3590 | .show_dt = 1, |
| 3591 | .get_iu = ahc_linux_get_iu, |
| 3592 | .set_iu = ahc_linux_set_iu, |
| 3593 | .show_iu = 1, |
| 3594 | .get_qas = ahc_linux_get_qas, |
| 3595 | .set_qas = ahc_linux_set_qas, |
| 3596 | .show_qas = 1, |
| 3597 | }; |
| 3598 | |
| 3599 | |
| 3600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | static int __init |
| 3602 | ahc_linux_init(void) |
| 3603 | { |
| 3604 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) |
| 92d161c | 2005-04-17 16:59:33 -0500 | [diff] [blame] | 3605 | ahc_linux_transport_template = spi_attach_transport(&ahc_linux_transport_functions); |
| 3606 | if (!ahc_linux_transport_template) |
| 3607 | return -ENODEV; |
James Bottomley | 858eaca | 2005-04-21 07:35:45 -0700 | [diff] [blame^] | 3608 | if (ahc_linux_detect(&aic7xxx_driver_template)) |
| 3609 | return 0; |
| 92d161c | 2005-04-17 16:59:33 -0500 | [diff] [blame] | 3610 | spi_release_transport(ahc_linux_transport_template); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | ahc_linux_exit(); |
| 3612 | return -ENODEV; |
| 3613 | #else |
| 3614 | scsi_register_module(MODULE_SCSI_HA, &aic7xxx_driver_template); |
| 3615 | if (aic7xxx_driver_template.present == 0) { |
| 3616 | scsi_unregister_module(MODULE_SCSI_HA, |
| 3617 | &aic7xxx_driver_template); |
| 3618 | return (-ENODEV); |
| 3619 | } |
| 3620 | |
| 3621 | return (0); |
| 3622 | #endif |
| 3623 | } |
| 3624 | |
| 3625 | static void |
| 3626 | ahc_linux_exit(void) |
| 3627 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3628 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) |
| 3629 | /* |
| 3630 | * In 2.4 we have to unregister from the PCI core _after_ |
| 3631 | * unregistering from the scsi midlayer to avoid dangling |
| 3632 | * references. |
| 3633 | */ |
| 3634 | scsi_unregister_module(MODULE_SCSI_HA, &aic7xxx_driver_template); |
| 3635 | #endif |
| 3636 | ahc_linux_pci_exit(); |
| 3637 | ahc_linux_eisa_exit(); |
| 92d161c | 2005-04-17 16:59:33 -0500 | [diff] [blame] | 3638 | spi_release_transport(ahc_linux_transport_template); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | module_init(ahc_linux_init); |
| 3642 | module_exit(ahc_linux_exit); |