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