1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Adaptec AAC series RAID controller driver 4 * (c) Copyright 2001 Red Hat Inc. 5 * 6 * based on the old aacraid driver that is.. 7 * Adaptec aacraid device driver for Linux. 8 * 9 * Copyright (c) 2000-2010 Adaptec, Inc. 10 * 2010-2015 PMC-Sierra, Inc. (aacraid@pmc-sierra.com) 11 * 2016-2017 Microsemi Corp. (aacraid@microsemi.com) 12 * 13 * Module Name: 14 * commsup.c 15 * 16 * Abstract: Contain all routines that are required for FSA host/adapter 17 * communication. 18 */ 19 20 #include <linux/kernel.h> 21 #include <linux/init.h> 22 #include <linux/crash_dump.h> 23 #include <linux/types.h> 24 #include <linux/sched.h> 25 #include <linux/pci.h> 26 #include <linux/spinlock.h> 27 #include <linux/slab.h> 28 #include <linux/completion.h> 29 #include <linux/blkdev.h> 30 #include <linux/delay.h> 31 #include <linux/kthread.h> 32 #include <linux/interrupt.h> 33 #include <linux/bcd.h> 34 #include <scsi/scsi.h> 35 #include <scsi/scsi_host.h> 36 #include <scsi/scsi_device.h> 37 #include <scsi/scsi_cmnd.h> 38 39 #include "aacraid.h" 40 41 /** 42 * fib_map_alloc - allocate the fib objects 43 * @dev: Adapter to allocate for 44 * 45 * Allocate and map the shared PCI space for the FIB blocks used to 46 * talk to the Adaptec firmware. 47 */ 48 49 static int fib_map_alloc(struct aac_dev *dev) 50 { 51 dev->max_cmd_size = AAC_MAX_NATIVE_SIZE; 52 53 dprintk((KERN_INFO 54 "allocate hardware fibs dma_alloc_coherent(%p, %d * (%d + %d), %p)\n", 55 &dev->pdev->dev, dev->max_cmd_size, dev->scsi_host_ptr->can_queue, 56 AAC_NUM_MGT_FIB, &dev->hw_fib_pa)); 57 dev->hw_fib_va = dma_alloc_coherent(&dev->pdev->dev, 58 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)) 59 * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) + (ALIGN32 - 1), 60 &dev->hw_fib_pa, GFP_KERNEL); 61 if (dev->hw_fib_va == NULL) 62 return -ENOMEM; 63 return 0; 64 } 65 66 /** 67 * aac_fib_map_free - free the fib objects 68 * @dev: Adapter to free 69 * 70 * Free the PCI mappings and the memory allocated for FIB blocks 71 * on this adapter. 72 */ 73 74 void aac_fib_map_free(struct aac_dev *dev) 75 { 76 size_t alloc_size; 77 size_t fib_size; 78 int num_fibs; 79 80 if(!dev->hw_fib_va || !dev->max_cmd_size) 81 return; 82 83 num_fibs = dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB; 84 fib_size = dev->max_fib_size + sizeof(struct aac_fib_xporthdr); 85 alloc_size = fib_size * num_fibs + ALIGN32 - 1; 86 87 dma_free_coherent(&dev->pdev->dev, alloc_size, dev->hw_fib_va, 88 dev->hw_fib_pa); 89 90 dev->hw_fib_va = NULL; 91 dev->hw_fib_pa = 0; 92 } 93 94 void aac_fib_vector_assign(struct aac_dev *dev) 95 { 96 u32 i = 0; 97 u32 vector = 1; 98 struct fib *fibptr = NULL; 99 100 for (i = 0, fibptr = &dev->fibs[i]; 101 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); 102 i++, fibptr++) { 103 if ((dev->max_msix == 1) || 104 (i > ((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1) 105 - dev->vector_cap))) { 106 fibptr->vector_no = 0; 107 } else { 108 fibptr->vector_no = vector; 109 vector++; 110 if (vector == dev->max_msix) 111 vector = 1; 112 } 113 } 114 } 115 116 /** 117 * aac_fib_setup - setup the fibs 118 * @dev: Adapter to set up 119 * 120 * Allocate the PCI space for the fibs, map it and then initialise the 121 * fib area, the unmapped fib data and also the free list 122 */ 123 124 int aac_fib_setup(struct aac_dev * dev) 125 { 126 struct fib *fibptr; 127 struct hw_fib *hw_fib; 128 dma_addr_t hw_fib_pa; 129 int i; 130 u32 max_cmds; 131 132 while (((i = fib_map_alloc(dev)) == -ENOMEM) 133 && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) { 134 max_cmds = (dev->scsi_host_ptr->can_queue+AAC_NUM_MGT_FIB) >> 1; 135 dev->scsi_host_ptr->can_queue = max_cmds - AAC_NUM_MGT_FIB; 136 if (dev->comm_interface != AAC_COMM_MESSAGE_TYPE3) 137 dev->init->r7.max_io_commands = cpu_to_le32(max_cmds); 138 } 139 if (i<0) 140 return -ENOMEM; 141 142 memset(dev->hw_fib_va, 0, 143 (dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)) * 144 (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB)); 145 146 /* 32 byte alignment for PMC */ 147 hw_fib_pa = (dev->hw_fib_pa + (ALIGN32 - 1)) & ~(ALIGN32 - 1); 148 hw_fib = (struct hw_fib *)((unsigned char *)dev->hw_fib_va + 149 (hw_fib_pa - dev->hw_fib_pa)); 150 151 /* add Xport header */ 152 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib + 153 sizeof(struct aac_fib_xporthdr)); 154 hw_fib_pa += sizeof(struct aac_fib_xporthdr); 155 156 /* 157 * Initialise the fibs 158 */ 159 for (i = 0, fibptr = &dev->fibs[i]; 160 i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); 161 i++, fibptr++) 162 { 163 fibptr->flags = 0; 164 fibptr->size = sizeof(struct fib); 165 fibptr->dev = dev; 166 fibptr->hw_fib_va = hw_fib; 167 fibptr->data = (void *) fibptr->hw_fib_va->data; 168 fibptr->next = fibptr+1; /* Forward chain the fibs */ 169 init_completion(&fibptr->event_wait); 170 spin_lock_init(&fibptr->event_lock); 171 hw_fib->header.XferState = cpu_to_le32(0xffffffff); 172 hw_fib->header.SenderSize = 173 cpu_to_le16(dev->max_fib_size); /* ?? max_cmd_size */ 174 fibptr->hw_fib_pa = hw_fib_pa; 175 fibptr->hw_sgl_pa = hw_fib_pa + 176 offsetof(struct aac_hba_cmd_req, sge[2]); 177 /* 178 * one element is for the ptr to the separate sg list, 179 * second element for 32 byte alignment 180 */ 181 fibptr->hw_error_pa = hw_fib_pa + 182 offsetof(struct aac_native_hba, resp.resp_bytes[0]); 183 184 hw_fib = (struct hw_fib *)((unsigned char *)hw_fib + 185 dev->max_cmd_size + sizeof(struct aac_fib_xporthdr)); 186 hw_fib_pa = hw_fib_pa + 187 dev->max_cmd_size + sizeof(struct aac_fib_xporthdr); 188 } 189 190 /* 191 *Assign vector numbers to fibs 192 */ 193 aac_fib_vector_assign(dev); 194 195 /* 196 * Add the fib chain to the free list 197 */ 198 dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL; 199 /* 200 * Set 8 fibs aside for management tools 201 */ 202 dev->free_fib = &dev->fibs[dev->scsi_host_ptr->can_queue]; 203 return 0; 204 } 205 206 /** 207 * aac_fib_alloc_tag-allocate a fib using tags 208 * @dev: Adapter to allocate the fib for 209 * @scmd: SCSI command 210 * 211 * Allocate a fib from the adapter fib pool using tags 212 * from the blk layer. 213 */ 214 215 struct fib *aac_fib_alloc_tag(struct aac_dev *dev, struct scsi_cmnd *scmd) 216 { 217 struct fib *fibptr; 218 219 fibptr = &dev->fibs[scsi_cmd_to_rq(scmd)->tag]; 220 /* 221 * Null out fields that depend on being zero at the start of 222 * each I/O 223 */ 224 fibptr->hw_fib_va->header.XferState = 0; 225 fibptr->type = FSAFS_NTC_FIB_CONTEXT; 226 fibptr->callback_data = NULL; 227 fibptr->callback = NULL; 228 fibptr->flags = 0; 229 230 return fibptr; 231 } 232 233 /** 234 * aac_fib_alloc - allocate a fib 235 * @dev: Adapter to allocate the fib for 236 * 237 * Allocate a fib from the adapter fib pool. If the pool is empty we 238 * return NULL. 239 */ 240 241 struct fib *aac_fib_alloc(struct aac_dev *dev) 242 { 243 struct fib * fibptr; 244 unsigned long flags; 245 spin_lock_irqsave(&dev->fib_lock, flags); 246 fibptr = dev->free_fib; 247 if(!fibptr){ 248 spin_unlock_irqrestore(&dev->fib_lock, flags); 249 return fibptr; 250 } 251 dev->free_fib = fibptr->next; 252 spin_unlock_irqrestore(&dev->fib_lock, flags); 253 /* 254 * Set the proper node type code and node byte size 255 */ 256 fibptr->type = FSAFS_NTC_FIB_CONTEXT; 257 fibptr->size = sizeof(struct fib); 258 /* 259 * Null out fields that depend on being zero at the start of 260 * each I/O 261 */ 262 fibptr->hw_fib_va->header.XferState = 0; 263 fibptr->flags = 0; 264 fibptr->callback = NULL; 265 fibptr->callback_data = NULL; 266 267 return fibptr; 268 } 269 270 /** 271 * aac_fib_free - free a fib 272 * @fibptr: fib to free up 273 * 274 * Frees up a fib and places it on the appropriate queue 275 */ 276 277 void aac_fib_free(struct fib *fibptr) 278 { 279 unsigned long flags; 280 281 if (fibptr->done == 2) 282 return; 283 284 spin_lock_irqsave(&fibptr->dev->fib_lock, flags); 285 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) 286 aac_config.fib_timeouts++; 287 if (!(fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) && 288 fibptr->hw_fib_va->header.XferState != 0) { 289 printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n", 290 (void*)fibptr, 291 le32_to_cpu(fibptr->hw_fib_va->header.XferState)); 292 } 293 fibptr->next = fibptr->dev->free_fib; 294 fibptr->dev->free_fib = fibptr; 295 spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags); 296 } 297 298 /** 299 * aac_fib_init - initialise a fib 300 * @fibptr: The fib to initialize 301 * 302 * Set up the generic fib fields ready for use 303 */ 304 305 void aac_fib_init(struct fib *fibptr) 306 { 307 struct hw_fib *hw_fib = fibptr->hw_fib_va; 308 309 memset(&hw_fib->header, 0, sizeof(struct aac_fibhdr)); 310 hw_fib->header.StructType = FIB_MAGIC; 311 hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size); 312 hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable); 313 hw_fib->header.u.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa); 314 hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size); 315 } 316 317 /** 318 * fib_dealloc - deallocate a fib 319 * @fibptr: fib to deallocate 320 * 321 * Will deallocate and return to the free pool the FIB pointed to by the 322 * caller. 323 */ 324 325 static void fib_dealloc(struct fib * fibptr) 326 { 327 struct hw_fib *hw_fib = fibptr->hw_fib_va; 328 hw_fib->header.XferState = 0; 329 } 330 331 /* 332 * Commuication primitives define and support the queuing method we use to 333 * support host to adapter commuication. All queue accesses happen through 334 * these routines and are the only routines which have a knowledge of the 335 * how these queues are implemented. 336 */ 337 338 /** 339 * aac_get_entry - get a queue entry 340 * @dev: Adapter 341 * @qid: Queue Number 342 * @entry: Entry return 343 * @index: Index return 344 * @nonotify: notification control 345 * 346 * With a priority the routine returns a queue entry if the queue has free entries. If the queue 347 * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is 348 * returned. 349 */ 350 351 static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify) 352 { 353 struct aac_queue * q; 354 unsigned long idx; 355 356 /* 357 * All of the queues wrap when they reach the end, so we check 358 * to see if they have reached the end and if they have we just 359 * set the index back to zero. This is a wrap. You could or off 360 * the high bits in all updates but this is a bit faster I think. 361 */ 362 363 q = &dev->queues->queue[qid]; 364 365 idx = *index = le32_to_cpu(*(q->headers.producer)); 366 /* Interrupt Moderation, only interrupt for first two entries */ 367 if (idx != le32_to_cpu(*(q->headers.consumer))) { 368 if (--idx == 0) { 369 if (qid == AdapNormCmdQueue) 370 idx = ADAP_NORM_CMD_ENTRIES; 371 else 372 idx = ADAP_NORM_RESP_ENTRIES; 373 } 374 if (idx != le32_to_cpu(*(q->headers.consumer))) 375 *nonotify = 1; 376 } 377 378 if (qid == AdapNormCmdQueue) { 379 if (*index >= ADAP_NORM_CMD_ENTRIES) 380 *index = 0; /* Wrap to front of the Producer Queue. */ 381 } else { 382 if (*index >= ADAP_NORM_RESP_ENTRIES) 383 *index = 0; /* Wrap to front of the Producer Queue. */ 384 } 385 386 /* Queue is full */ 387 if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) { 388 printk(KERN_WARNING "Queue %d full, %u outstanding.\n", 389 qid, atomic_read(&q->numpending)); 390 return 0; 391 } else { 392 *entry = q->base + *index; 393 return 1; 394 } 395 } 396 397 /** 398 * aac_queue_get - get the next free QE 399 * @dev: Adapter 400 * @index: Returned index 401 * @qid: Queue number 402 * @hw_fib: Fib to associate with the queue entry 403 * @wait: Wait if queue full 404 * @fibptr: Driver fib object to go with fib 405 * @nonotify: Don't notify the adapter 406 * 407 * Gets the next free QE off the requested priorty adapter command 408 * queue and associates the Fib with the QE. The QE represented by 409 * index is ready to insert on the queue when this routine returns 410 * success. 411 */ 412 413 int aac_queue_get(struct aac_dev * dev, u32 * index, u32 qid, struct hw_fib * hw_fib, int wait, struct fib * fibptr, unsigned long *nonotify) 414 { 415 struct aac_entry * entry = NULL; 416 int map = 0; 417 418 if (qid == AdapNormCmdQueue) { 419 /* if no entries wait for some if caller wants to */ 420 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) { 421 printk(KERN_ERR "GetEntries failed\n"); 422 } 423 /* 424 * Setup queue entry with a command, status and fib mapped 425 */ 426 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size)); 427 map = 1; 428 } else { 429 while (!aac_get_entry(dev, qid, &entry, index, nonotify)) { 430 /* if no entries wait for some if caller wants to */ 431 } 432 /* 433 * Setup queue entry with command, status and fib mapped 434 */ 435 entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size)); 436 entry->addr = hw_fib->header.SenderFibAddress; 437 /* Restore adapters pointer to the FIB */ 438 hw_fib->header.u.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */ 439 map = 0; 440 } 441 /* 442 * If MapFib is true than we need to map the Fib and put pointers 443 * in the queue entry. 444 */ 445 if (map) 446 entry->addr = cpu_to_le32(fibptr->hw_fib_pa); 447 return 0; 448 } 449 450 /* 451 * Define the highest level of host to adapter communication routines. 452 * These routines will support host to adapter FS commuication. These 453 * routines have no knowledge of the commuication method used. This level 454 * sends and receives FIBs. This level has no knowledge of how these FIBs 455 * get passed back and forth. 456 */ 457 458 /** 459 * aac_fib_send - send a fib to the adapter 460 * @command: Command to send 461 * @fibptr: The fib 462 * @size: Size of fib data area 463 * @priority: Priority of Fib 464 * @wait: Async/sync select 465 * @reply: True if a reply is wanted 466 * @callback: Called with reply 467 * @callback_data: Passed to callback 468 * 469 * Sends the requested FIB to the adapter and optionally will wait for a 470 * response FIB. If the caller does not wish to wait for a response than 471 * an event to wait on must be supplied. This event will be set when a 472 * response FIB is received from the adapter. 473 */ 474 475 int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size, 476 int priority, int wait, int reply, fib_callback callback, 477 void *callback_data) 478 { 479 struct aac_dev * dev = fibptr->dev; 480 struct hw_fib * hw_fib = fibptr->hw_fib_va; 481 unsigned long flags = 0; 482 unsigned long mflags = 0; 483 unsigned long sflags = 0; 484 485 if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned))) 486 return -EBUSY; 487 488 if (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)) 489 return -EINVAL; 490 491 /* 492 * There are 5 cases with the wait and response requested flags. 493 * The only invalid cases are if the caller requests to wait and 494 * does not request a response and if the caller does not want a 495 * response and the Fib is not allocated from pool. If a response 496 * is not requested the Fib will just be deallocaed by the DPC 497 * routine when the response comes back from the adapter. No 498 * further processing will be done besides deleting the Fib. We 499 * will have a debug mode where the adapter can notify the host 500 * it had a problem and the host can log that fact. 501 */ 502 fibptr->flags = 0; 503 if (wait && !reply) { 504 return -EINVAL; 505 } else if (!wait && reply) { 506 hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected); 507 FIB_COUNTER_INCREMENT(aac_config.AsyncSent); 508 } else if (!wait && !reply) { 509 hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected); 510 FIB_COUNTER_INCREMENT(aac_config.NoResponseSent); 511 } else if (wait && reply) { 512 hw_fib->header.XferState |= cpu_to_le32(ResponseExpected); 513 FIB_COUNTER_INCREMENT(aac_config.NormalSent); 514 } 515 /* 516 * Map the fib into 32bits by using the fib number 517 */ 518 519 hw_fib->header.SenderFibAddress = 520 cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2); 521 522 /* use the same shifted value for handle to be compatible 523 * with the new native hba command handle 524 */ 525 hw_fib->header.Handle = 526 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1); 527 528 /* 529 * Set FIB state to indicate where it came from and if we want a 530 * response from the adapter. Also load the command from the 531 * caller. 532 * 533 * Map the hw fib pointer as a 32bit value 534 */ 535 hw_fib->header.Command = cpu_to_le16(command); 536 hw_fib->header.XferState |= cpu_to_le32(SentFromHost); 537 /* 538 * Set the size of the Fib we want to send to the adapter 539 */ 540 hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size); 541 if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) { 542 return -EMSGSIZE; 543 } 544 /* 545 * Get a queue entry connect the FIB to it and send an notify 546 * the adapter a command is ready. 547 */ 548 hw_fib->header.XferState |= cpu_to_le32(NormalPriority); 549 550 /* 551 * Fill in the Callback and CallbackContext if we are not 552 * going to wait. 553 */ 554 if (!wait) { 555 fibptr->callback = callback; 556 fibptr->callback_data = callback_data; 557 fibptr->flags = FIB_CONTEXT_FLAG; 558 } 559 560 fibptr->done = 0; 561 562 FIB_COUNTER_INCREMENT(aac_config.FibsSent); 563 564 dprintk((KERN_DEBUG "Fib contents:.\n")); 565 dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command))); 566 dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command))); 567 dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState))); 568 dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va)); 569 dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa)); 570 dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr)); 571 572 if (!dev->queues) 573 return -EBUSY; 574 575 if (wait) { 576 577 spin_lock_irqsave(&dev->manage_lock, mflags); 578 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) { 579 printk(KERN_INFO "No management Fibs Available:%d\n", 580 dev->management_fib_count); 581 spin_unlock_irqrestore(&dev->manage_lock, mflags); 582 return -EBUSY; 583 } 584 dev->management_fib_count++; 585 spin_unlock_irqrestore(&dev->manage_lock, mflags); 586 spin_lock_irqsave(&fibptr->event_lock, flags); 587 } 588 589 if (dev->sync_mode) { 590 if (wait) 591 spin_unlock_irqrestore(&fibptr->event_lock, flags); 592 spin_lock_irqsave(&dev->sync_lock, sflags); 593 if (dev->sync_fib) { 594 list_add_tail(&fibptr->fiblink, &dev->sync_fib_list); 595 spin_unlock_irqrestore(&dev->sync_lock, sflags); 596 } else { 597 dev->sync_fib = fibptr; 598 spin_unlock_irqrestore(&dev->sync_lock, sflags); 599 aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB, 600 (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0, 601 NULL, NULL, NULL, NULL, NULL); 602 } 603 if (wait) { 604 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT; 605 if (wait_for_completion_interruptible(&fibptr->event_wait)) { 606 fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT; 607 return -EFAULT; 608 } 609 return 0; 610 } 611 return -EINPROGRESS; 612 } 613 614 if (aac_adapter_deliver(fibptr) != 0) { 615 printk(KERN_ERR "aac_fib_send: returned -EBUSY\n"); 616 if (wait) { 617 spin_unlock_irqrestore(&fibptr->event_lock, flags); 618 spin_lock_irqsave(&dev->manage_lock, mflags); 619 dev->management_fib_count--; 620 spin_unlock_irqrestore(&dev->manage_lock, mflags); 621 } 622 return -EBUSY; 623 } 624 625 626 /* 627 * If the caller wanted us to wait for response wait now. 628 */ 629 630 if (wait) { 631 spin_unlock_irqrestore(&fibptr->event_lock, flags); 632 /* Only set for first known interruptable command */ 633 if (wait < 0) { 634 /* 635 * *VERY* Dangerous to time out a command, the 636 * assumption is made that we have no hope of 637 * functioning because an interrupt routing or other 638 * hardware failure has occurred. 639 */ 640 unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */ 641 while (!try_wait_for_completion(&fibptr->event_wait)) { 642 int blink; 643 if (time_is_before_eq_jiffies(timeout)) { 644 struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue]; 645 atomic_dec(&q->numpending); 646 if (wait == -1) { 647 printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n" 648 "Usually a result of a PCI interrupt routing problem;\n" 649 "update mother board BIOS or consider utilizing one of\n" 650 "the SAFE mode kernel options (acpi, apic etc)\n"); 651 } 652 return -ETIMEDOUT; 653 } 654 655 if (unlikely(aac_pci_offline(dev))) 656 return -EFAULT; 657 658 if ((blink = aac_adapter_check_health(dev)) > 0) { 659 if (wait == -1) { 660 printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n" 661 "Usually a result of a serious unrecoverable hardware problem\n", 662 blink); 663 } 664 return -EFAULT; 665 } 666 /* 667 * Allow other processes / CPUS to use core 668 */ 669 schedule(); 670 } 671 } else if (wait_for_completion_interruptible(&fibptr->event_wait)) { 672 /* Do nothing ... satisfy 673 * wait_for_completion_interruptible must_check */ 674 } 675 676 spin_lock_irqsave(&fibptr->event_lock, flags); 677 if (fibptr->done == 0) { 678 fibptr->done = 2; /* Tell interrupt we aborted */ 679 spin_unlock_irqrestore(&fibptr->event_lock, flags); 680 return -ERESTARTSYS; 681 } 682 spin_unlock_irqrestore(&fibptr->event_lock, flags); 683 BUG_ON(fibptr->done == 0); 684 685 if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) 686 return -ETIMEDOUT; 687 return 0; 688 } 689 /* 690 * If the user does not want a response than return success otherwise 691 * return pending 692 */ 693 if (reply) 694 return -EINPROGRESS; 695 else 696 return 0; 697 } 698 699 int aac_hba_send(u8 command, struct fib *fibptr, fib_callback callback, 700 void *callback_data) 701 { 702 struct aac_dev *dev = fibptr->dev; 703 int wait; 704 unsigned long flags = 0; 705 unsigned long mflags = 0; 706 struct aac_hba_cmd_req *hbacmd = (struct aac_hba_cmd_req *) 707 fibptr->hw_fib_va; 708 709 fibptr->flags = (FIB_CONTEXT_FLAG | FIB_CONTEXT_FLAG_NATIVE_HBA); 710 if (callback) { 711 wait = 0; 712 fibptr->callback = callback; 713 fibptr->callback_data = callback_data; 714 } else 715 wait = 1; 716 717 718 hbacmd->iu_type = command; 719 720 if (command == HBA_IU_TYPE_SCSI_CMD_REQ) { 721 /* bit1 of request_id must be 0 */ 722 hbacmd->request_id = 723 cpu_to_le32((((u32)(fibptr - dev->fibs)) << 2) + 1); 724 fibptr->flags |= FIB_CONTEXT_FLAG_SCSI_CMD; 725 } else 726 return -EINVAL; 727 728 729 if (wait) { 730 spin_lock_irqsave(&dev->manage_lock, mflags); 731 if (dev->management_fib_count >= AAC_NUM_MGT_FIB) { 732 spin_unlock_irqrestore(&dev->manage_lock, mflags); 733 return -EBUSY; 734 } 735 dev->management_fib_count++; 736 spin_unlock_irqrestore(&dev->manage_lock, mflags); 737 spin_lock_irqsave(&fibptr->event_lock, flags); 738 } 739 740 if (aac_adapter_deliver(fibptr) != 0) { 741 if (wait) { 742 spin_unlock_irqrestore(&fibptr->event_lock, flags); 743 spin_lock_irqsave(&dev->manage_lock, mflags); 744 dev->management_fib_count--; 745 spin_unlock_irqrestore(&dev->manage_lock, mflags); 746 } 747 return -EBUSY; 748 } 749 FIB_COUNTER_INCREMENT(aac_config.NativeSent); 750 751 if (wait) { 752 753 spin_unlock_irqrestore(&fibptr->event_lock, flags); 754 755 if (unlikely(aac_pci_offline(dev))) 756 return -EFAULT; 757 758 fibptr->flags |= FIB_CONTEXT_FLAG_WAIT; 759 if (wait_for_completion_interruptible(&fibptr->event_wait)) 760 fibptr->done = 2; 761 fibptr->flags &= ~(FIB_CONTEXT_FLAG_WAIT); 762 763 spin_lock_irqsave(&fibptr->event_lock, flags); 764 if ((fibptr->done == 0) || (fibptr->done == 2)) { 765 fibptr->done = 2; /* Tell interrupt we aborted */ 766 spin_unlock_irqrestore(&fibptr->event_lock, flags); 767 return -ERESTARTSYS; 768 } 769 spin_unlock_irqrestore(&fibptr->event_lock, flags); 770 WARN_ON(fibptr->done == 0); 771 772 if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)) 773 return -ETIMEDOUT; 774 775 return 0; 776 } 777 778 return -EINPROGRESS; 779 } 780 781 /** 782 * aac_consumer_get - get the top of the queue 783 * @dev: Adapter 784 * @q: Queue 785 * @entry: Return entry 786 * 787 * Will return a pointer to the entry on the top of the queue requested that 788 * we are a consumer of, and return the address of the queue entry. It does 789 * not change the state of the queue. 790 */ 791 792 int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry) 793 { 794 u32 index; 795 int status; 796 if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) { 797 status = 0; 798 } else { 799 /* 800 * The consumer index must be wrapped if we have reached 801 * the end of the queue, else we just use the entry 802 * pointed to by the header index 803 */ 804 if (le32_to_cpu(*q->headers.consumer) >= q->entries) 805 index = 0; 806 else 807 index = le32_to_cpu(*q->headers.consumer); 808 *entry = q->base + index; 809 status = 1; 810 } 811 return(status); 812 } 813 814 /** 815 * aac_consumer_free - free consumer entry 816 * @dev: Adapter 817 * @q: Queue 818 * @qid: Queue ident 819 * 820 * Frees up the current top of the queue we are a consumer of. If the 821 * queue was full notify the producer that the queue is no longer full. 822 */ 823 824 void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid) 825 { 826 int wasfull = 0; 827 u32 notify; 828 829 if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer)) 830 wasfull = 1; 831 832 if (le32_to_cpu(*q->headers.consumer) >= q->entries) 833 *q->headers.consumer = cpu_to_le32(1); 834 else 835 le32_add_cpu(q->headers.consumer, 1); 836 837 if (wasfull) { 838 switch (qid) { 839 840 case HostNormCmdQueue: 841 notify = HostNormCmdNotFull; 842 break; 843 case HostNormRespQueue: 844 notify = HostNormRespNotFull; 845 break; 846 default: 847 BUG(); 848 return; 849 } 850 aac_adapter_notify(dev, notify); 851 } 852 } 853 854 /** 855 * aac_fib_adapter_complete - complete adapter issued fib 856 * @fibptr: fib to complete 857 * @size: size of fib 858 * 859 * Will do all necessary work to complete a FIB that was sent from 860 * the adapter. 861 */ 862 863 int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size) 864 { 865 struct hw_fib * hw_fib = fibptr->hw_fib_va; 866 struct aac_dev * dev = fibptr->dev; 867 struct aac_queue * q; 868 unsigned long nointr = 0; 869 unsigned long qflags; 870 871 if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1 || 872 dev->comm_interface == AAC_COMM_MESSAGE_TYPE2 || 873 dev->comm_interface == AAC_COMM_MESSAGE_TYPE3) { 874 kfree(hw_fib); 875 return 0; 876 } 877 878 if (hw_fib->header.XferState == 0) { 879 if (dev->comm_interface == AAC_COMM_MESSAGE) 880 kfree(hw_fib); 881 return 0; 882 } 883 /* 884 * If we plan to do anything check the structure type first. 885 */ 886 if (hw_fib->header.StructType != FIB_MAGIC && 887 hw_fib->header.StructType != FIB_MAGIC2 && 888 hw_fib->header.StructType != FIB_MAGIC2_64) { 889 if (dev->comm_interface == AAC_COMM_MESSAGE) 890 kfree(hw_fib); 891 return -EINVAL; 892 } 893 /* 894 * This block handles the case where the adapter had sent us a 895 * command and we have finished processing the command. We 896 * call completeFib when we are done processing the command 897 * and want to send a response back to the adapter. This will 898 * send the completed cdb to the adapter. 899 */ 900 if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) { 901 if (dev->comm_interface == AAC_COMM_MESSAGE) { 902 kfree (hw_fib); 903 } else { 904 u32 index; 905 hw_fib->header.XferState |= cpu_to_le32(HostProcessed); 906 if (size) { 907 size += sizeof(struct aac_fibhdr); 908 if (size > le16_to_cpu(hw_fib->header.SenderSize)) 909 return -EMSGSIZE; 910 hw_fib->header.Size = cpu_to_le16(size); 911 } 912 q = &dev->queues->queue[AdapNormRespQueue]; 913 spin_lock_irqsave(q->lock, qflags); 914 aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr); 915 *(q->headers.producer) = cpu_to_le32(index + 1); 916 spin_unlock_irqrestore(q->lock, qflags); 917 if (!(nointr & (int)aac_config.irq_mod)) 918 aac_adapter_notify(dev, AdapNormRespQueue); 919 } 920 } else { 921 printk(KERN_WARNING "aac_fib_adapter_complete: " 922 "Unknown xferstate detected.\n"); 923 BUG(); 924 } 925 return 0; 926 } 927 928 /** 929 * aac_fib_complete - fib completion handler 930 * @fibptr: FIB to complete 931 * 932 * Will do all necessary work to complete a FIB. 933 */ 934 935 int aac_fib_complete(struct fib *fibptr) 936 { 937 struct hw_fib * hw_fib = fibptr->hw_fib_va; 938 939 if (fibptr->flags & FIB_CONTEXT_FLAG_NATIVE_HBA) { 940 fib_dealloc(fibptr); 941 return 0; 942 } 943 944 /* 945 * Check for a fib which has already been completed or with a 946 * status wait timeout 947 */ 948 949 if (hw_fib->header.XferState == 0 || fibptr->done == 2) 950 return 0; 951 /* 952 * If we plan to do anything check the structure type first. 953 */ 954 955 if (hw_fib->header.StructType != FIB_MAGIC && 956 hw_fib->header.StructType != FIB_MAGIC2 && 957 hw_fib->header.StructType != FIB_MAGIC2_64) 958 return -EINVAL; 959 /* 960 * This block completes a cdb which orginated on the host and we 961 * just need to deallocate the cdb or reinit it. At this point the 962 * command is complete that we had sent to the adapter and this 963 * cdb could be reused. 964 */ 965 966 if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) && 967 (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed))) 968 { 969 fib_dealloc(fibptr); 970 } 971 else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost)) 972 { 973 /* 974 * This handles the case when the host has aborted the I/O 975 * to the adapter because the adapter is not responding 976 */ 977 fib_dealloc(fibptr); 978 } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) { 979 fib_dealloc(fibptr); 980 } else { 981 BUG(); 982 } 983 return 0; 984 } 985 986 /** 987 * aac_printf - handle printf from firmware 988 * @dev: Adapter 989 * @val: Message info 990 * 991 * Print a message passed to us by the controller firmware on the 992 * Adaptec board 993 */ 994 995 void aac_printf(struct aac_dev *dev, u32 val) 996 { 997 char *cp = dev->printfbuf; 998 if (dev->printf_enabled) 999 { 1000 int length = val & 0xffff; 1001 int level = (val >> 16) & 0xffff; 1002 1003 /* 1004 * The size of the printfbuf is set in port.c 1005 * There is no variable or define for it 1006 */ 1007 if (length > 255) 1008 length = 255; 1009 if (cp[length] != 0) 1010 cp[length] = 0; 1011 if (level == LOG_AAC_HIGH_ERROR) 1012 printk(KERN_WARNING "%s:%s", dev->name, cp); 1013 else 1014 printk(KERN_INFO "%s:%s", dev->name, cp); 1015 } 1016 memset(cp, 0, 256); 1017 } 1018 1019 static inline int aac_aif_data(struct aac_aifcmd *aifcmd, uint32_t index) 1020 { 1021 return le32_to_cpu(((__le32 *)aifcmd->data)[index]); 1022 } 1023 1024 1025 static void aac_handle_aif_bu(struct aac_dev *dev, struct aac_aifcmd *aifcmd) 1026 { 1027 switch (aac_aif_data(aifcmd, 1)) { 1028 case AifBuCacheDataLoss: 1029 if (aac_aif_data(aifcmd, 2)) 1030 dev_info(&dev->pdev->dev, "Backup unit had cache data loss - [%d]\n", 1031 aac_aif_data(aifcmd, 2)); 1032 else 1033 dev_info(&dev->pdev->dev, "Backup Unit had cache data loss\n"); 1034 break; 1035 case AifBuCacheDataRecover: 1036 if (aac_aif_data(aifcmd, 2)) 1037 dev_info(&dev->pdev->dev, "DDR cache data recovered successfully - [%d]\n", 1038 aac_aif_data(aifcmd, 2)); 1039 else 1040 dev_info(&dev->pdev->dev, "DDR cache data recovered successfully\n"); 1041 break; 1042 } 1043 } 1044 1045 #define AIF_SNIFF_TIMEOUT (500*HZ) 1046 /** 1047 * aac_handle_aif - Handle a message from the firmware 1048 * @dev: Which adapter this fib is from 1049 * @fibptr: Pointer to fibptr from adapter 1050 * 1051 * This routine handles a driver notify fib from the adapter and 1052 * dispatches it to the appropriate routine for handling. 1053 */ 1054 static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr) 1055 { 1056 struct hw_fib * hw_fib = fibptr->hw_fib_va; 1057 struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data; 1058 u32 channel, id, lun, container; 1059 struct scsi_device *device; 1060 enum { 1061 NOTHING, 1062 DELETE, 1063 ADD, 1064 CHANGE 1065 } device_config_needed = NOTHING; 1066 1067 /* Sniff for container changes */ 1068 1069 if (!dev || !dev->fsa_dev) 1070 return; 1071 container = channel = id = lun = (u32)-1; 1072 1073 /* 1074 * We have set this up to try and minimize the number of 1075 * re-configures that take place. As a result of this when 1076 * certain AIF's come in we will set a flag waiting for another 1077 * type of AIF before setting the re-config flag. 1078 */ 1079 switch (le32_to_cpu(aifcmd->command)) { 1080 case AifCmdDriverNotify: 1081 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) { 1082 case AifRawDeviceRemove: 1083 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]); 1084 if ((container >> 28)) { 1085 container = (u32)-1; 1086 break; 1087 } 1088 channel = (container >> 24) & 0xF; 1089 if (channel >= dev->maximum_num_channels) { 1090 container = (u32)-1; 1091 break; 1092 } 1093 id = container & 0xFFFF; 1094 if (id >= dev->maximum_num_physicals) { 1095 container = (u32)-1; 1096 break; 1097 } 1098 lun = (container >> 16) & 0xFF; 1099 container = (u32)-1; 1100 channel = aac_phys_to_logical(channel); 1101 device_config_needed = DELETE; 1102 break; 1103 1104 /* 1105 * Morph or Expand complete 1106 */ 1107 case AifDenMorphComplete: 1108 case AifDenVolumeExtendComplete: 1109 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]); 1110 if (container >= dev->maximum_num_containers) 1111 break; 1112 1113 /* 1114 * Find the scsi_device associated with the SCSI 1115 * address. Make sure we have the right array, and if 1116 * so set the flag to initiate a new re-config once we 1117 * see an AifEnConfigChange AIF come through. 1118 */ 1119 1120 if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) { 1121 device = scsi_device_lookup(dev->scsi_host_ptr, 1122 CONTAINER_TO_CHANNEL(container), 1123 CONTAINER_TO_ID(container), 1124 CONTAINER_TO_LUN(container)); 1125 if (device) { 1126 dev->fsa_dev[container].config_needed = CHANGE; 1127 dev->fsa_dev[container].config_waiting_on = AifEnConfigChange; 1128 dev->fsa_dev[container].config_waiting_stamp = jiffies; 1129 scsi_device_put(device); 1130 } 1131 } 1132 } 1133 1134 /* 1135 * If we are waiting on something and this happens to be 1136 * that thing then set the re-configure flag. 1137 */ 1138 if (container != (u32)-1) { 1139 if (container >= dev->maximum_num_containers) 1140 break; 1141 if ((dev->fsa_dev[container].config_waiting_on == 1142 le32_to_cpu(*(__le32 *)aifcmd->data)) && 1143 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) 1144 dev->fsa_dev[container].config_waiting_on = 0; 1145 } else for (container = 0; 1146 container < dev->maximum_num_containers; ++container) { 1147 if ((dev->fsa_dev[container].config_waiting_on == 1148 le32_to_cpu(*(__le32 *)aifcmd->data)) && 1149 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) 1150 dev->fsa_dev[container].config_waiting_on = 0; 1151 } 1152 break; 1153 1154 case AifCmdEventNotify: 1155 switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) { 1156 case AifEnBatteryEvent: 1157 dev->cache_protected = 1158 (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3)); 1159 break; 1160 /* 1161 * Add an Array. 1162 */ 1163 case AifEnAddContainer: 1164 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]); 1165 if (container >= dev->maximum_num_containers) 1166 break; 1167 dev->fsa_dev[container].config_needed = ADD; 1168 dev->fsa_dev[container].config_waiting_on = 1169 AifEnConfigChange; 1170 dev->fsa_dev[container].config_waiting_stamp = jiffies; 1171 break; 1172 1173 /* 1174 * Delete an Array. 1175 */ 1176 case AifEnDeleteContainer: 1177 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]); 1178 if (container >= dev->maximum_num_containers) 1179 break; 1180 dev->fsa_dev[container].config_needed = DELETE; 1181 dev->fsa_dev[container].config_waiting_on = 1182 AifEnConfigChange; 1183 dev->fsa_dev[container].config_waiting_stamp = jiffies; 1184 break; 1185 1186 /* 1187 * Container change detected. If we currently are not 1188 * waiting on something else, setup to wait on a Config Change. 1189 */ 1190 case AifEnContainerChange: 1191 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]); 1192 if (container >= dev->maximum_num_containers) 1193 break; 1194 if (dev->fsa_dev[container].config_waiting_on && 1195 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) 1196 break; 1197 dev->fsa_dev[container].config_needed = CHANGE; 1198 dev->fsa_dev[container].config_waiting_on = 1199 AifEnConfigChange; 1200 dev->fsa_dev[container].config_waiting_stamp = jiffies; 1201 break; 1202 1203 case AifEnConfigChange: 1204 break; 1205 1206 case AifEnAddJBOD: 1207 case AifEnDeleteJBOD: 1208 container = le32_to_cpu(((__le32 *)aifcmd->data)[1]); 1209 if ((container >> 28)) { 1210 container = (u32)-1; 1211 break; 1212 } 1213 channel = (container >> 24) & 0xF; 1214 if (channel >= dev->maximum_num_channels) { 1215 container = (u32)-1; 1216 break; 1217 } 1218 id = container & 0xFFFF; 1219 if (id >= dev->maximum_num_physicals) { 1220 container = (u32)-1; 1221 break; 1222 } 1223 lun = (container >> 16) & 0xFF; 1224 container = (u32)-1; 1225 channel = aac_phys_to_logical(channel); 1226 device_config_needed = 1227 (((__le32 *)aifcmd->data)[0] == 1228 cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE; 1229 if (device_config_needed == ADD) { 1230 device = scsi_device_lookup(dev->scsi_host_ptr, 1231 channel, 1232 id, 1233 lun); 1234 if (device) { 1235 scsi_remove_device(device); 1236 scsi_device_put(device); 1237 } 1238 } 1239 break; 1240 1241 case AifEnEnclosureManagement: 1242 /* 1243 * If in JBOD mode, automatic exposure of new 1244 * physical target to be suppressed until configured. 1245 */ 1246 if (dev->jbod) 1247 break; 1248 switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) { 1249 case EM_DRIVE_INSERTION: 1250 case EM_DRIVE_REMOVAL: 1251 case EM_SES_DRIVE_INSERTION: 1252 case EM_SES_DRIVE_REMOVAL: 1253 container = le32_to_cpu( 1254 ((__le32 *)aifcmd->data)[2]); 1255 if ((container >> 28)) { 1256 container = (u32)-1; 1257 break; 1258 } 1259 channel = (container >> 24) & 0xF; 1260 if (channel >= dev->maximum_num_channels) { 1261 container = (u32)-1; 1262 break; 1263 } 1264 id = container & 0xFFFF; 1265 lun = (container >> 16) & 0xFF; 1266 container = (u32)-1; 1267 if (id >= dev->maximum_num_physicals) { 1268 /* legacy dev_t ? */ 1269 if ((0x2000 <= id) || lun || channel || 1270 ((channel = (id >> 7) & 0x3F) >= 1271 dev->maximum_num_channels)) 1272 break; 1273 lun = (id >> 4) & 7; 1274 id &= 0xF; 1275 } 1276 channel = aac_phys_to_logical(channel); 1277 device_config_needed = 1278 ((((__le32 *)aifcmd->data)[3] 1279 == cpu_to_le32(EM_DRIVE_INSERTION)) || 1280 (((__le32 *)aifcmd->data)[3] 1281 == cpu_to_le32(EM_SES_DRIVE_INSERTION))) ? 1282 ADD : DELETE; 1283 break; 1284 } 1285 break; 1286 case AifBuManagerEvent: 1287 aac_handle_aif_bu(dev, aifcmd); 1288 break; 1289 } 1290 1291 /* 1292 * If we are waiting on something and this happens to be 1293 * that thing then set the re-configure flag. 1294 */ 1295 if (container != (u32)-1) { 1296 if (container >= dev->maximum_num_containers) 1297 break; 1298 if ((dev->fsa_dev[container].config_waiting_on == 1299 le32_to_cpu(*(__le32 *)aifcmd->data)) && 1300 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) 1301 dev->fsa_dev[container].config_waiting_on = 0; 1302 } else for (container = 0; 1303 container < dev->maximum_num_containers; ++container) { 1304 if ((dev->fsa_dev[container].config_waiting_on == 1305 le32_to_cpu(*(__le32 *)aifcmd->data)) && 1306 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) 1307 dev->fsa_dev[container].config_waiting_on = 0; 1308 } 1309 break; 1310 1311 case AifCmdJobProgress: 1312 /* 1313 * These are job progress AIF's. When a Clear is being 1314 * done on a container it is initially created then hidden from 1315 * the OS. When the clear completes we don't get a config 1316 * change so we monitor the job status complete on a clear then 1317 * wait for a container change. 1318 */ 1319 1320 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) && 1321 (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] || 1322 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) { 1323 for (container = 0; 1324 container < dev->maximum_num_containers; 1325 ++container) { 1326 /* 1327 * Stomp on all config sequencing for all 1328 * containers? 1329 */ 1330 dev->fsa_dev[container].config_waiting_on = 1331 AifEnContainerChange; 1332 dev->fsa_dev[container].config_needed = ADD; 1333 dev->fsa_dev[container].config_waiting_stamp = 1334 jiffies; 1335 } 1336 } 1337 if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) && 1338 ((__le32 *)aifcmd->data)[6] == 0 && 1339 ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) { 1340 for (container = 0; 1341 container < dev->maximum_num_containers; 1342 ++container) { 1343 /* 1344 * Stomp on all config sequencing for all 1345 * containers? 1346 */ 1347 dev->fsa_dev[container].config_waiting_on = 1348 AifEnContainerChange; 1349 dev->fsa_dev[container].config_needed = DELETE; 1350 dev->fsa_dev[container].config_waiting_stamp = 1351 jiffies; 1352 } 1353 } 1354 break; 1355 } 1356 1357 container = 0; 1358 retry_next: 1359 if (device_config_needed == NOTHING) { 1360 for (; container < dev->maximum_num_containers; ++container) { 1361 if ((dev->fsa_dev[container].config_waiting_on == 0) && 1362 (dev->fsa_dev[container].config_needed != NOTHING) && 1363 time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) { 1364 device_config_needed = 1365 dev->fsa_dev[container].config_needed; 1366 dev->fsa_dev[container].config_needed = NOTHING; 1367 channel = CONTAINER_TO_CHANNEL(container); 1368 id = CONTAINER_TO_ID(container); 1369 lun = CONTAINER_TO_LUN(container); 1370 break; 1371 } 1372 } 1373 } 1374 if (device_config_needed == NOTHING) 1375 return; 1376 1377 /* 1378 * If we decided that a re-configuration needs to be done, 1379 * schedule it here on the way out the door, please close the door 1380 * behind you. 1381 */ 1382 1383 /* 1384 * Find the scsi_device associated with the SCSI address, 1385 * and mark it as changed, invalidating the cache. This deals 1386 * with changes to existing device IDs. 1387 */ 1388 1389 if (!dev || !dev->scsi_host_ptr) 1390 return; 1391 /* 1392 * force reload of disk info via aac_probe_container 1393 */ 1394 if ((channel == CONTAINER_CHANNEL) && 1395 (device_config_needed != NOTHING)) { 1396 if (dev->fsa_dev[container].valid == 1) 1397 dev->fsa_dev[container].valid = 2; 1398 aac_probe_container(dev, container); 1399 } 1400 device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun); 1401 if (device) { 1402 switch (device_config_needed) { 1403 case DELETE: 1404 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE)) 1405 scsi_remove_device(device); 1406 #else 1407 if (scsi_device_online(device)) { 1408 scsi_device_set_state(device, SDEV_OFFLINE); 1409 sdev_printk(KERN_INFO, device, 1410 "Device offlined - %s\n", 1411 (channel == CONTAINER_CHANNEL) ? 1412 "array deleted" : 1413 "enclosure services event"); 1414 } 1415 #endif 1416 break; 1417 case ADD: 1418 if (!scsi_device_online(device)) { 1419 sdev_printk(KERN_INFO, device, 1420 "Device online - %s\n", 1421 (channel == CONTAINER_CHANNEL) ? 1422 "array created" : 1423 "enclosure services event"); 1424 scsi_device_set_state(device, SDEV_RUNNING); 1425 } 1426 fallthrough; 1427 case CHANGE: 1428 if ((channel == CONTAINER_CHANNEL) 1429 && (!dev->fsa_dev[container].valid)) { 1430 #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE)) 1431 scsi_remove_device(device); 1432 #else 1433 if (!scsi_device_online(device)) 1434 break; 1435 scsi_device_set_state(device, SDEV_OFFLINE); 1436 sdev_printk(KERN_INFO, device, 1437 "Device offlined - %s\n", 1438 "array failed"); 1439 #endif 1440 break; 1441 } 1442 scsi_rescan_device(device); 1443 break; 1444 1445 default: 1446 break; 1447 } 1448 scsi_device_put(device); 1449 device_config_needed = NOTHING; 1450 } 1451 if (device_config_needed == ADD) 1452 scsi_add_device(dev->scsi_host_ptr, channel, id, lun); 1453 if (channel == CONTAINER_CHANNEL) { 1454 container++; 1455 device_config_needed = NOTHING; 1456 goto retry_next; 1457 } 1458 } 1459 1460 static void aac_schedule_bus_scan(struct aac_dev *aac) 1461 { 1462 if (aac->sa_firmware) 1463 aac_schedule_safw_scan_worker(aac); 1464 else 1465 aac_schedule_src_reinit_aif_worker(aac); 1466 } 1467 1468 static int _aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type) 1469 { 1470 int index, quirks; 1471 int retval; 1472 struct Scsi_Host *host = aac->scsi_host_ptr; 1473 int jafo = 0; 1474 int bled; 1475 u64 dmamask; 1476 int num_of_fibs = 0; 1477 1478 /* 1479 * Assumptions: 1480 * - host is locked, unless called by the aacraid thread. 1481 * (a matter of convenience, due to legacy issues surrounding 1482 * eh_host_adapter_reset). 1483 * - in_reset is asserted, so no new i/o is getting to the 1484 * card. 1485 * - The card is dead, or will be very shortly ;-/ so no new 1486 * commands are completing in the interrupt service. 1487 */ 1488 aac_adapter_disable_int(aac); 1489 if (aac->thread && aac->thread->pid != current->pid) { 1490 spin_unlock_irq(host->host_lock); 1491 kthread_stop(aac->thread); 1492 aac->thread = NULL; 1493 jafo = 1; 1494 } 1495 1496 /* 1497 * If a positive health, means in a known DEAD PANIC 1498 * state and the adapter could be reset to `try again'. 1499 */ 1500 bled = forced ? 0 : aac_adapter_check_health(aac); 1501 retval = aac_adapter_restart(aac, bled, reset_type); 1502 1503 if (retval) 1504 goto out; 1505 1506 /* 1507 * Loop through the fibs, close the synchronous FIBS 1508 */ 1509 retval = 1; 1510 num_of_fibs = aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB; 1511 for (index = 0; index < num_of_fibs; index++) { 1512 1513 struct fib *fib = &aac->fibs[index]; 1514 __le32 XferState = fib->hw_fib_va->header.XferState; 1515 bool is_response_expected = false; 1516 1517 if (!(XferState & cpu_to_le32(NoResponseExpected | Async)) && 1518 (XferState & cpu_to_le32(ResponseExpected))) 1519 is_response_expected = true; 1520 1521 if (is_response_expected 1522 || fib->flags & FIB_CONTEXT_FLAG_WAIT) { 1523 unsigned long flagv; 1524 spin_lock_irqsave(&fib->event_lock, flagv); 1525 complete(&fib->event_wait); 1526 spin_unlock_irqrestore(&fib->event_lock, flagv); 1527 schedule(); 1528 retval = 0; 1529 } 1530 } 1531 /* Give some extra time for ioctls to complete. */ 1532 if (retval == 0) 1533 ssleep(2); 1534 index = aac->cardtype; 1535 1536 /* 1537 * Re-initialize the adapter, first free resources, then carefully 1538 * apply the initialization sequence to come back again. Only risk 1539 * is a change in Firmware dropping cache, it is assumed the caller 1540 * will ensure that i/o is queisced and the card is flushed in that 1541 * case. 1542 */ 1543 aac_free_irq(aac); 1544 aac_fib_map_free(aac); 1545 dma_free_coherent(&aac->pdev->dev, aac->comm_size, aac->comm_addr, 1546 aac->comm_phys); 1547 aac_adapter_ioremap(aac, 0); 1548 aac->comm_addr = NULL; 1549 aac->comm_phys = 0; 1550 kfree(aac->queues); 1551 aac->queues = NULL; 1552 kfree(aac->fsa_dev); 1553 aac->fsa_dev = NULL; 1554 1555 dmamask = DMA_BIT_MASK(32); 1556 quirks = aac_get_driver_ident(index)->quirks; 1557 if (quirks & AAC_QUIRK_31BIT) 1558 retval = dma_set_mask(&aac->pdev->dev, dmamask); 1559 else if (!(quirks & AAC_QUIRK_SRC)) 1560 retval = dma_set_mask(&aac->pdev->dev, dmamask); 1561 else 1562 retval = dma_set_coherent_mask(&aac->pdev->dev, dmamask); 1563 1564 if (quirks & AAC_QUIRK_31BIT && !retval) { 1565 dmamask = DMA_BIT_MASK(31); 1566 retval = dma_set_coherent_mask(&aac->pdev->dev, dmamask); 1567 } 1568 1569 if (retval) 1570 goto out; 1571 1572 if ((retval = (*(aac_get_driver_ident(index)->init))(aac))) 1573 goto out; 1574 1575 if (jafo) { 1576 aac->thread = kthread_run(aac_command_thread, aac, "%s", 1577 aac->name); 1578 if (IS_ERR(aac->thread)) { 1579 retval = PTR_ERR(aac->thread); 1580 aac->thread = NULL; 1581 goto out; 1582 } 1583 } 1584 (void)aac_get_adapter_info(aac); 1585 if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) { 1586 host->sg_tablesize = 34; 1587 host->max_sectors = (host->sg_tablesize * 8) + 112; 1588 } 1589 if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) { 1590 host->sg_tablesize = 17; 1591 host->max_sectors = (host->sg_tablesize * 8) + 112; 1592 } 1593 aac_get_config_status(aac, 1); 1594 aac_get_containers(aac); 1595 /* 1596 * This is where the assumption that the Adapter is quiesced 1597 * is important. 1598 */ 1599 scsi_host_complete_all_commands(host, DID_RESET); 1600 1601 retval = 0; 1602 out: 1603 aac->in_reset = 0; 1604 1605 /* 1606 * Issue bus rescan to catch any configuration that might have 1607 * occurred 1608 */ 1609 if (!retval && !is_kdump_kernel()) { 1610 dev_info(&aac->pdev->dev, "Scheduling bus rescan\n"); 1611 aac_schedule_bus_scan(aac); 1612 } 1613 1614 if (jafo) { 1615 spin_lock_irq(host->host_lock); 1616 } 1617 return retval; 1618 } 1619 1620 int aac_reset_adapter(struct aac_dev *aac, int forced, u8 reset_type) 1621 { 1622 unsigned long flagv = 0; 1623 int retval, unblock_retval; 1624 struct Scsi_Host *host = aac->scsi_host_ptr; 1625 int bled; 1626 1627 if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0) 1628 return -EBUSY; 1629 1630 if (aac->in_reset) { 1631 spin_unlock_irqrestore(&aac->fib_lock, flagv); 1632 return -EBUSY; 1633 } 1634 aac->in_reset = 1; 1635 spin_unlock_irqrestore(&aac->fib_lock, flagv); 1636 1637 /* 1638 * Wait for all commands to complete to this specific 1639 * target (block maximum 60 seconds). Although not necessary, 1640 * it does make us a good storage citizen. 1641 */ 1642 scsi_host_block(host); 1643 1644 /* Quiesce build, flush cache, write through mode */ 1645 if (forced < 2) 1646 aac_send_shutdown(aac); 1647 spin_lock_irqsave(host->host_lock, flagv); 1648 bled = forced ? forced : 1649 (aac_check_reset != 0 && aac_check_reset != 1); 1650 retval = _aac_reset_adapter(aac, bled, reset_type); 1651 spin_unlock_irqrestore(host->host_lock, flagv); 1652 1653 unblock_retval = scsi_host_unblock(host, SDEV_RUNNING); 1654 if (!retval) 1655 retval = unblock_retval; 1656 if ((forced < 2) && (retval == -ENODEV)) { 1657 /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */ 1658 struct fib * fibctx = aac_fib_alloc(aac); 1659 if (fibctx) { 1660 struct aac_pause *cmd; 1661 int status; 1662 1663 aac_fib_init(fibctx); 1664 1665 cmd = (struct aac_pause *) fib_data(fibctx); 1666 1667 cmd->command = cpu_to_le32(VM_ContainerConfig); 1668 cmd->type = cpu_to_le32(CT_PAUSE_IO); 1669 cmd->timeout = cpu_to_le32(1); 1670 cmd->min = cpu_to_le32(1); 1671 cmd->noRescan = cpu_to_le32(1); 1672 cmd->count = cpu_to_le32(0); 1673 1674 status = aac_fib_send(ContainerCommand, 1675 fibctx, 1676 sizeof(struct aac_pause), 1677 FsaNormal, 1678 -2 /* Timeout silently */, 1, 1679 NULL, NULL); 1680 1681 if (status >= 0) 1682 aac_fib_complete(fibctx); 1683 /* FIB should be freed only after getting 1684 * the response from the F/W */ 1685 if (status != -ERESTARTSYS) 1686 aac_fib_free(fibctx); 1687 } 1688 } 1689 1690 return retval; 1691 } 1692 1693 static inline int is_safw_raid_volume(struct aac_dev *aac, int bus, int target) 1694 { 1695 return bus == CONTAINER_CHANNEL && target < aac->maximum_num_containers; 1696 } 1697 1698 static struct scsi_device *aac_lookup_safw_scsi_device(struct aac_dev *dev, 1699 int bus, 1700 int target) 1701 { 1702 if (bus != CONTAINER_CHANNEL) 1703 bus = aac_phys_to_logical(bus); 1704 1705 return scsi_device_lookup(dev->scsi_host_ptr, bus, target, 0); 1706 } 1707 1708 static int aac_add_safw_device(struct aac_dev *dev, int bus, int target) 1709 { 1710 if (bus != CONTAINER_CHANNEL) 1711 bus = aac_phys_to_logical(bus); 1712 1713 return scsi_add_device(dev->scsi_host_ptr, bus, target, 0); 1714 } 1715 1716 static void aac_put_safw_scsi_device(struct scsi_device *sdev) 1717 { 1718 if (sdev) 1719 scsi_device_put(sdev); 1720 } 1721 1722 static void aac_remove_safw_device(struct aac_dev *dev, int bus, int target) 1723 { 1724 struct scsi_device *sdev; 1725 1726 sdev = aac_lookup_safw_scsi_device(dev, bus, target); 1727 scsi_remove_device(sdev); 1728 aac_put_safw_scsi_device(sdev); 1729 } 1730 1731 static inline int aac_is_safw_scan_count_equal(struct aac_dev *dev, 1732 int bus, int target) 1733 { 1734 return dev->hba_map[bus][target].scan_counter == dev->scan_counter; 1735 } 1736 1737 static int aac_is_safw_target_valid(struct aac_dev *dev, int bus, int target) 1738 { 1739 if (is_safw_raid_volume(dev, bus, target)) 1740 return dev->fsa_dev[target].valid; 1741 else 1742 return aac_is_safw_scan_count_equal(dev, bus, target); 1743 } 1744 1745 static int aac_is_safw_device_exposed(struct aac_dev *dev, int bus, int target) 1746 { 1747 int is_exposed = 0; 1748 struct scsi_device *sdev; 1749 1750 sdev = aac_lookup_safw_scsi_device(dev, bus, target); 1751 if (sdev) 1752 is_exposed = 1; 1753 aac_put_safw_scsi_device(sdev); 1754 1755 return is_exposed; 1756 } 1757 1758 static int aac_update_safw_host_devices(struct aac_dev *dev) 1759 { 1760 int i; 1761 int bus; 1762 int target; 1763 int is_exposed = 0; 1764 int rcode = 0; 1765 1766 rcode = aac_setup_safw_adapter(dev); 1767 if (unlikely(rcode < 0)) { 1768 goto out; 1769 } 1770 1771 for (i = 0; i < AAC_BUS_TARGET_LOOP; i++) { 1772 1773 bus = get_bus_number(i); 1774 target = get_target_number(i); 1775 1776 is_exposed = aac_is_safw_device_exposed(dev, bus, target); 1777 1778 if (aac_is_safw_target_valid(dev, bus, target) && !is_exposed) 1779 aac_add_safw_device(dev, bus, target); 1780 else if (!aac_is_safw_target_valid(dev, bus, target) && 1781 is_exposed) 1782 aac_remove_safw_device(dev, bus, target); 1783 } 1784 out: 1785 return rcode; 1786 } 1787 1788 static int aac_scan_safw_host(struct aac_dev *dev) 1789 { 1790 int rcode = 0; 1791 1792 rcode = aac_update_safw_host_devices(dev); 1793 if (rcode) 1794 aac_schedule_safw_scan_worker(dev); 1795 1796 return rcode; 1797 } 1798 1799 int aac_scan_host(struct aac_dev *dev) 1800 { 1801 int rcode = 0; 1802 1803 mutex_lock(&dev->scan_mutex); 1804 if (dev->sa_firmware) 1805 rcode = aac_scan_safw_host(dev); 1806 else 1807 scsi_scan_host(dev->scsi_host_ptr); 1808 mutex_unlock(&dev->scan_mutex); 1809 1810 return rcode; 1811 } 1812 1813 void aac_src_reinit_aif_worker(struct work_struct *work) 1814 { 1815 struct aac_dev *dev = container_of(to_delayed_work(work), 1816 struct aac_dev, src_reinit_aif_worker); 1817 1818 wait_event(dev->scsi_host_ptr->host_wait, 1819 !scsi_host_in_recovery(dev->scsi_host_ptr)); 1820 aac_reinit_aif(dev, dev->cardtype); 1821 } 1822 1823 /** 1824 * aac_handle_sa_aif - Handle a message from the firmware 1825 * @dev: Which adapter this fib is from 1826 * @fibptr: Pointer to fibptr from adapter 1827 * 1828 * This routine handles a driver notify fib from the adapter and 1829 * dispatches it to the appropriate routine for handling. 1830 */ 1831 static void aac_handle_sa_aif(struct aac_dev *dev, struct fib *fibptr) 1832 { 1833 int i; 1834 u32 events = 0; 1835 1836 if (fibptr->hbacmd_size & SA_AIF_HOTPLUG) 1837 events = SA_AIF_HOTPLUG; 1838 else if (fibptr->hbacmd_size & SA_AIF_HARDWARE) 1839 events = SA_AIF_HARDWARE; 1840 else if (fibptr->hbacmd_size & SA_AIF_PDEV_CHANGE) 1841 events = SA_AIF_PDEV_CHANGE; 1842 else if (fibptr->hbacmd_size & SA_AIF_LDEV_CHANGE) 1843 events = SA_AIF_LDEV_CHANGE; 1844 else if (fibptr->hbacmd_size & SA_AIF_BPSTAT_CHANGE) 1845 events = SA_AIF_BPSTAT_CHANGE; 1846 else if (fibptr->hbacmd_size & SA_AIF_BPCFG_CHANGE) 1847 events = SA_AIF_BPCFG_CHANGE; 1848 1849 switch (events) { 1850 case SA_AIF_HOTPLUG: 1851 case SA_AIF_HARDWARE: 1852 case SA_AIF_PDEV_CHANGE: 1853 case SA_AIF_LDEV_CHANGE: 1854 case SA_AIF_BPCFG_CHANGE: 1855 1856 aac_scan_host(dev); 1857 1858 break; 1859 1860 case SA_AIF_BPSTAT_CHANGE: 1861 /* currently do nothing */ 1862 break; 1863 } 1864 1865 for (i = 1; i <= 10; ++i) { 1866 events = src_readl(dev, MUnit.IDR); 1867 if (events & (1<<23)) { 1868 pr_warn(" AIF not cleared by firmware - %d/%d)\n", 1869 i, 10); 1870 ssleep(1); 1871 } 1872 } 1873 } 1874 1875 static int get_fib_count(struct aac_dev *dev) 1876 { 1877 unsigned int num = 0; 1878 struct list_head *entry; 1879 unsigned long flagv; 1880 1881 /* 1882 * Warning: no sleep allowed while 1883 * holding spinlock. We take the estimate 1884 * and pre-allocate a set of fibs outside the 1885 * lock. 1886 */ 1887 num = le32_to_cpu(dev->init->r7.adapter_fibs_size) 1888 / sizeof(struct hw_fib); /* some extra */ 1889 spin_lock_irqsave(&dev->fib_lock, flagv); 1890 entry = dev->fib_list.next; 1891 while (entry != &dev->fib_list) { 1892 entry = entry->next; 1893 ++num; 1894 } 1895 spin_unlock_irqrestore(&dev->fib_lock, flagv); 1896 1897 return num; 1898 } 1899 1900 static int fillup_pools(struct aac_dev *dev, struct hw_fib **hw_fib_pool, 1901 struct fib **fib_pool, 1902 unsigned int num) 1903 { 1904 struct hw_fib **hw_fib_p; 1905 struct fib **fib_p; 1906 1907 hw_fib_p = hw_fib_pool; 1908 fib_p = fib_pool; 1909 while (hw_fib_p < &hw_fib_pool[num]) { 1910 *(hw_fib_p) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL); 1911 if (!(*(hw_fib_p++))) { 1912 --hw_fib_p; 1913 break; 1914 } 1915 1916 *(fib_p) = kmalloc(sizeof(struct fib), GFP_KERNEL); 1917 if (!(*(fib_p++))) { 1918 kfree(*(--hw_fib_p)); 1919 break; 1920 } 1921 } 1922 1923 /* 1924 * Get the actual number of allocated fibs 1925 */ 1926 num = hw_fib_p - hw_fib_pool; 1927 return num; 1928 } 1929 1930 static void wakeup_fibctx_threads(struct aac_dev *dev, 1931 struct hw_fib **hw_fib_pool, 1932 struct fib **fib_pool, 1933 struct fib *fib, 1934 struct hw_fib *hw_fib, 1935 unsigned int num) 1936 { 1937 unsigned long flagv; 1938 struct list_head *entry; 1939 struct hw_fib **hw_fib_p; 1940 struct fib **fib_p; 1941 u32 time_now, time_last; 1942 struct hw_fib *hw_newfib; 1943 struct fib *newfib; 1944 struct aac_fib_context *fibctx; 1945 1946 time_now = jiffies/HZ; 1947 spin_lock_irqsave(&dev->fib_lock, flagv); 1948 entry = dev->fib_list.next; 1949 /* 1950 * For each Context that is on the 1951 * fibctxList, make a copy of the 1952 * fib, and then set the event to wake up the 1953 * thread that is waiting for it. 1954 */ 1955 1956 hw_fib_p = hw_fib_pool; 1957 fib_p = fib_pool; 1958 while (entry != &dev->fib_list) { 1959 /* 1960 * Extract the fibctx 1961 */ 1962 fibctx = list_entry(entry, struct aac_fib_context, 1963 next); 1964 /* 1965 * Check if the queue is getting 1966 * backlogged 1967 */ 1968 if (fibctx->count > 20) { 1969 /* 1970 * It's *not* jiffies folks, 1971 * but jiffies / HZ so do not 1972 * panic ... 1973 */ 1974 time_last = fibctx->jiffies; 1975 /* 1976 * Has it been > 2 minutes 1977 * since the last read off 1978 * the queue? 1979 */ 1980 if ((time_now - time_last) > aif_timeout) { 1981 entry = entry->next; 1982 aac_close_fib_context(dev, fibctx); 1983 continue; 1984 } 1985 } 1986 /* 1987 * Warning: no sleep allowed while 1988 * holding spinlock 1989 */ 1990 if (hw_fib_p >= &hw_fib_pool[num]) { 1991 pr_warn("aifd: didn't allocate NewFib\n"); 1992 entry = entry->next; 1993 continue; 1994 } 1995 1996 hw_newfib = *hw_fib_p; 1997 *(hw_fib_p++) = NULL; 1998 newfib = *fib_p; 1999 *(fib_p++) = NULL; 2000 /* 2001 * Make the copy of the FIB 2002 */ 2003 memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib)); 2004 memcpy(newfib, fib, sizeof(struct fib)); 2005 newfib->hw_fib_va = hw_newfib; 2006 /* 2007 * Put the FIB onto the 2008 * fibctx's fibs 2009 */ 2010 list_add_tail(&newfib->fiblink, &fibctx->fib_list); 2011 fibctx->count++; 2012 /* 2013 * Set the event to wake up the 2014 * thread that is waiting. 2015 */ 2016 complete(&fibctx->completion); 2017 2018 entry = entry->next; 2019 } 2020 /* 2021 * Set the status of this FIB 2022 */ 2023 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK); 2024 aac_fib_adapter_complete(fib, sizeof(u32)); 2025 spin_unlock_irqrestore(&dev->fib_lock, flagv); 2026 2027 } 2028 2029 static void aac_process_events(struct aac_dev *dev) 2030 { 2031 struct hw_fib *hw_fib; 2032 struct fib *fib; 2033 unsigned long flags; 2034 spinlock_t *t_lock; 2035 2036 t_lock = dev->queues->queue[HostNormCmdQueue].lock; 2037 spin_lock_irqsave(t_lock, flags); 2038 2039 while (!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) { 2040 struct list_head *entry; 2041 struct aac_aifcmd *aifcmd; 2042 unsigned int num; 2043 struct hw_fib **hw_fib_pool, **hw_fib_p; 2044 struct fib **fib_pool, **fib_p; 2045 2046 set_current_state(TASK_RUNNING); 2047 2048 entry = dev->queues->queue[HostNormCmdQueue].cmdq.next; 2049 list_del(entry); 2050 2051 t_lock = dev->queues->queue[HostNormCmdQueue].lock; 2052 spin_unlock_irqrestore(t_lock, flags); 2053 2054 fib = list_entry(entry, struct fib, fiblink); 2055 hw_fib = fib->hw_fib_va; 2056 if (dev->sa_firmware) { 2057 /* Thor AIF */ 2058 aac_handle_sa_aif(dev, fib); 2059 aac_fib_adapter_complete(fib, (u16)sizeof(u32)); 2060 goto free_fib; 2061 } 2062 /* 2063 * We will process the FIB here or pass it to a 2064 * worker thread that is TBD. We Really can't 2065 * do anything at this point since we don't have 2066 * anything defined for this thread to do. 2067 */ 2068 memset(fib, 0, sizeof(struct fib)); 2069 fib->type = FSAFS_NTC_FIB_CONTEXT; 2070 fib->size = sizeof(struct fib); 2071 fib->hw_fib_va = hw_fib; 2072 fib->data = hw_fib->data; 2073 fib->dev = dev; 2074 /* 2075 * We only handle AifRequest fibs from the adapter. 2076 */ 2077 2078 aifcmd = (struct aac_aifcmd *) hw_fib->data; 2079 if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) { 2080 /* Handle Driver Notify Events */ 2081 aac_handle_aif(dev, fib); 2082 *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK); 2083 aac_fib_adapter_complete(fib, (u16)sizeof(u32)); 2084 goto free_fib; 2085 } 2086 /* 2087 * The u32 here is important and intended. We are using 2088 * 32bit wrapping time to fit the adapter field 2089 */ 2090 2091 /* Sniff events */ 2092 if (aifcmd->command == cpu_to_le32(AifCmdEventNotify) 2093 || aifcmd->command == cpu_to_le32(AifCmdJobProgress)) { 2094 aac_handle_aif(dev, fib); 2095 } 2096 2097 /* 2098 * get number of fibs to process 2099 */ 2100 num = get_fib_count(dev); 2101 if (!num) 2102 goto free_fib; 2103 2104 hw_fib_pool = kmalloc_array(num, sizeof(struct hw_fib *), 2105 GFP_KERNEL); 2106 if (!hw_fib_pool) 2107 goto free_fib; 2108 2109 fib_pool = kmalloc_array(num, sizeof(struct fib *), GFP_KERNEL); 2110 if (!fib_pool) 2111 goto free_hw_fib_pool; 2112 2113 /* 2114 * Fill up fib pointer pools with actual fibs 2115 * and hw_fibs 2116 */ 2117 num = fillup_pools(dev, hw_fib_pool, fib_pool, num); 2118 if (!num) 2119 goto free_mem; 2120 2121 /* 2122 * wakeup the thread that is waiting for 2123 * the response from fw (ioctl) 2124 */ 2125 wakeup_fibctx_threads(dev, hw_fib_pool, fib_pool, 2126 fib, hw_fib, num); 2127 2128 free_mem: 2129 /* Free up the remaining resources */ 2130 hw_fib_p = hw_fib_pool; 2131 fib_p = fib_pool; 2132 while (hw_fib_p < &hw_fib_pool[num]) { 2133 kfree(*hw_fib_p); 2134 kfree(*fib_p); 2135 ++fib_p; 2136 ++hw_fib_p; 2137 } 2138 kfree(fib_pool); 2139 free_hw_fib_pool: 2140 kfree(hw_fib_pool); 2141 free_fib: 2142 kfree(fib); 2143 t_lock = dev->queues->queue[HostNormCmdQueue].lock; 2144 spin_lock_irqsave(t_lock, flags); 2145 } 2146 /* 2147 * There are no more AIF's 2148 */ 2149 t_lock = dev->queues->queue[HostNormCmdQueue].lock; 2150 spin_unlock_irqrestore(t_lock, flags); 2151 } 2152 2153 static int aac_send_wellness_command(struct aac_dev *dev, char *wellness_str, 2154 u32 datasize) 2155 { 2156 struct aac_srb *srbcmd; 2157 struct sgmap64 *sg64; 2158 dma_addr_t addr; 2159 char *dma_buf; 2160 struct fib *fibptr; 2161 int ret = -ENOMEM; 2162 u32 vbus, vid; 2163 2164 fibptr = aac_fib_alloc(dev); 2165 if (!fibptr) 2166 goto out; 2167 2168 dma_buf = dma_alloc_coherent(&dev->pdev->dev, datasize, &addr, 2169 GFP_KERNEL); 2170 if (!dma_buf) 2171 goto fib_free_out; 2172 2173 aac_fib_init(fibptr); 2174 2175 vbus = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_bus); 2176 vid = (u32)le16_to_cpu(dev->supplement_adapter_info.virt_device_target); 2177 2178 srbcmd = (struct aac_srb *)fib_data(fibptr); 2179 2180 srbcmd->function = cpu_to_le32(SRBF_ExecuteScsi); 2181 srbcmd->channel = cpu_to_le32(vbus); 2182 srbcmd->id = cpu_to_le32(vid); 2183 srbcmd->lun = 0; 2184 srbcmd->flags = cpu_to_le32(SRB_DataOut); 2185 srbcmd->timeout = cpu_to_le32(10); 2186 srbcmd->retry_limit = 0; 2187 srbcmd->cdb_size = cpu_to_le32(12); 2188 srbcmd->count = cpu_to_le32(datasize); 2189 2190 memset(srbcmd->cdb, 0, sizeof(srbcmd->cdb)); 2191 srbcmd->cdb[0] = BMIC_OUT; 2192 srbcmd->cdb[6] = WRITE_HOST_WELLNESS; 2193 memcpy(dma_buf, (char *)wellness_str, datasize); 2194 2195 sg64 = (struct sgmap64 *)&srbcmd->sg; 2196 sg64->count = cpu_to_le32(1); 2197 sg64->sg[0].addr[1] = cpu_to_le32((u32)(((addr) >> 16) >> 16)); 2198 sg64->sg[0].addr[0] = cpu_to_le32((u32)(addr & 0xffffffff)); 2199 sg64->sg[0].count = cpu_to_le32(datasize); 2200 2201 ret = aac_fib_send(ScsiPortCommand64, fibptr, 2202 sizeof(struct aac_srb) + sizeof(struct sgentry), 2203 FsaNormal, 1, 1, NULL, NULL); 2204 2205 dma_free_coherent(&dev->pdev->dev, datasize, dma_buf, addr); 2206 2207 /* 2208 * Do not set XferState to zero unless 2209 * receives a response from F/W 2210 */ 2211 if (ret >= 0) 2212 aac_fib_complete(fibptr); 2213 2214 /* 2215 * FIB should be freed only after 2216 * getting the response from the F/W 2217 */ 2218 if (ret != -ERESTARTSYS) 2219 goto fib_free_out; 2220 2221 out: 2222 return ret; 2223 fib_free_out: 2224 aac_fib_free(fibptr); 2225 goto out; 2226 } 2227 2228 static int aac_send_safw_hostttime(struct aac_dev *dev, struct timespec64 *now) 2229 { 2230 struct tm cur_tm; 2231 char wellness_str[] = "<HW>TD\010\0\0\0\0\0\0\0\0\0DW\0\0ZZ"; 2232 u32 datasize = sizeof(wellness_str); 2233 time64_t local_time; 2234 int ret = -ENODEV; 2235 2236 if (!dev->sa_firmware) 2237 goto out; 2238 2239 local_time = (now->tv_sec - (sys_tz.tz_minuteswest * 60)); 2240 time64_to_tm(local_time, 0, &cur_tm); 2241 cur_tm.tm_mon += 1; 2242 cur_tm.tm_year += 1900; 2243 wellness_str[8] = bin2bcd(cur_tm.tm_hour); 2244 wellness_str[9] = bin2bcd(cur_tm.tm_min); 2245 wellness_str[10] = bin2bcd(cur_tm.tm_sec); 2246 wellness_str[12] = bin2bcd(cur_tm.tm_mon); 2247 wellness_str[13] = bin2bcd(cur_tm.tm_mday); 2248 wellness_str[14] = bin2bcd(cur_tm.tm_year / 100); 2249 wellness_str[15] = bin2bcd(cur_tm.tm_year % 100); 2250 2251 ret = aac_send_wellness_command(dev, wellness_str, datasize); 2252 2253 out: 2254 return ret; 2255 } 2256 2257 static int aac_send_hosttime(struct aac_dev *dev, struct timespec64 *now) 2258 { 2259 int ret = -ENOMEM; 2260 struct fib *fibptr; 2261 __le32 *info; 2262 2263 fibptr = aac_fib_alloc(dev); 2264 if (!fibptr) 2265 goto out; 2266 2267 aac_fib_init(fibptr); 2268 info = (__le32 *)fib_data(fibptr); 2269 *info = cpu_to_le32(now->tv_sec); /* overflow in y2106 */ 2270 ret = aac_fib_send(SendHostTime, fibptr, sizeof(*info), FsaNormal, 2271 1, 1, NULL, NULL); 2272 2273 /* 2274 * Do not set XferState to zero unless 2275 * receives a response from F/W 2276 */ 2277 if (ret >= 0) 2278 aac_fib_complete(fibptr); 2279 2280 /* 2281 * FIB should be freed only after 2282 * getting the response from the F/W 2283 */ 2284 if (ret != -ERESTARTSYS) 2285 aac_fib_free(fibptr); 2286 2287 out: 2288 return ret; 2289 } 2290 2291 /** 2292 * aac_command_thread - command processing thread 2293 * @data: Adapter to monitor 2294 * 2295 * Waits on the commandready event in it's queue. When the event gets set 2296 * it will pull FIBs off it's queue. It will continue to pull FIBs off 2297 * until the queue is empty. When the queue is empty it will wait for 2298 * more FIBs. 2299 */ 2300 2301 int aac_command_thread(void *data) 2302 { 2303 struct aac_dev *dev = data; 2304 DECLARE_WAITQUEUE(wait, current); 2305 unsigned long next_jiffies = jiffies + HZ; 2306 unsigned long next_check_jiffies = next_jiffies; 2307 long difference = HZ; 2308 2309 /* 2310 * We can only have one thread per adapter for AIF's. 2311 */ 2312 if (dev->aif_thread) 2313 return -EINVAL; 2314 2315 /* 2316 * Let the DPC know it has a place to send the AIF's to. 2317 */ 2318 dev->aif_thread = 1; 2319 add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait); 2320 set_current_state(TASK_INTERRUPTIBLE); 2321 dprintk ((KERN_INFO "aac_command_thread start\n")); 2322 while (1) { 2323 2324 aac_process_events(dev); 2325 2326 /* 2327 * Background activity 2328 */ 2329 if ((time_before(next_check_jiffies,next_jiffies)) 2330 && ((difference = next_check_jiffies - jiffies) <= 0)) { 2331 next_check_jiffies = next_jiffies; 2332 if (aac_adapter_check_health(dev) == 0) { 2333 difference = ((long)(unsigned)check_interval) 2334 * HZ; 2335 next_check_jiffies = jiffies + difference; 2336 } else if (!dev->queues) 2337 break; 2338 } 2339 if (!time_before(next_check_jiffies,next_jiffies) 2340 && ((difference = next_jiffies - jiffies) <= 0)) { 2341 struct timespec64 now; 2342 int ret; 2343 2344 /* Don't even try to talk to adapter if its sick */ 2345 ret = aac_adapter_check_health(dev); 2346 if (ret || !dev->queues) 2347 break; 2348 next_check_jiffies = jiffies 2349 + ((long)(unsigned)check_interval) 2350 * HZ; 2351 ktime_get_real_ts64(&now); 2352 2353 /* Synchronize our watches */ 2354 if (((NSEC_PER_SEC - (NSEC_PER_SEC / HZ)) > now.tv_nsec) 2355 && (now.tv_nsec > (NSEC_PER_SEC / HZ))) 2356 difference = HZ + HZ / 2 - 2357 now.tv_nsec / (NSEC_PER_SEC / HZ); 2358 else { 2359 if (now.tv_nsec > NSEC_PER_SEC / 2) 2360 ++now.tv_sec; 2361 2362 if (dev->sa_firmware) 2363 ret = 2364 aac_send_safw_hostttime(dev, &now); 2365 else 2366 ret = aac_send_hosttime(dev, &now); 2367 2368 difference = (long)(unsigned)update_interval*HZ; 2369 } 2370 next_jiffies = jiffies + difference; 2371 if (time_before(next_check_jiffies,next_jiffies)) 2372 difference = next_check_jiffies - jiffies; 2373 } 2374 if (difference <= 0) 2375 difference = 1; 2376 set_current_state(TASK_INTERRUPTIBLE); 2377 2378 if (kthread_should_stop()) 2379 break; 2380 2381 /* 2382 * we probably want usleep_range() here instead of the 2383 * jiffies computation 2384 */ 2385 schedule_timeout(difference); 2386 2387 if (kthread_should_stop()) 2388 break; 2389 } 2390 if (dev->queues) 2391 remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait); 2392 dev->aif_thread = 0; 2393 return 0; 2394 } 2395 2396 int aac_acquire_irq(struct aac_dev *dev) 2397 { 2398 int i; 2399 int j; 2400 int ret = 0; 2401 2402 if (!dev->sync_mode && dev->msi_enabled && dev->max_msix > 1) { 2403 for (i = 0; i < dev->max_msix; i++) { 2404 dev->aac_msix[i].vector_no = i; 2405 dev->aac_msix[i].dev = dev; 2406 if (request_irq(pci_irq_vector(dev->pdev, i), 2407 dev->a_ops.adapter_intr, 2408 0, "aacraid", &(dev->aac_msix[i]))) { 2409 printk(KERN_ERR "%s%d: Failed to register IRQ for vector %d.\n", 2410 dev->name, dev->id, i); 2411 for (j = 0 ; j < i ; j++) 2412 free_irq(pci_irq_vector(dev->pdev, j), 2413 &(dev->aac_msix[j])); 2414 pci_disable_msix(dev->pdev); 2415 ret = -1; 2416 } 2417 } 2418 } else { 2419 dev->aac_msix[0].vector_no = 0; 2420 dev->aac_msix[0].dev = dev; 2421 2422 if (request_irq(dev->pdev->irq, dev->a_ops.adapter_intr, 2423 IRQF_SHARED, "aacraid", 2424 &(dev->aac_msix[0])) < 0) { 2425 if (dev->msi) 2426 pci_disable_msi(dev->pdev); 2427 printk(KERN_ERR "%s%d: Interrupt unavailable.\n", 2428 dev->name, dev->id); 2429 ret = -1; 2430 } 2431 } 2432 return ret; 2433 } 2434 2435 void aac_free_irq(struct aac_dev *dev) 2436 { 2437 int i; 2438 2439 if (aac_is_src(dev)) { 2440 if (dev->max_msix > 1) { 2441 for (i = 0; i < dev->max_msix; i++) 2442 free_irq(pci_irq_vector(dev->pdev, i), 2443 &(dev->aac_msix[i])); 2444 } else { 2445 free_irq(dev->pdev->irq, &(dev->aac_msix[0])); 2446 } 2447 } else { 2448 free_irq(dev->pdev->irq, dev); 2449 } 2450 if (dev->msi) 2451 pci_disable_msi(dev->pdev); 2452 else if (dev->max_msix > 1) 2453 pci_disable_msix(dev->pdev); 2454 } 2455