PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/scsi/aacraid/commsup.c

https://bitbucket.org/abioy/linux
C | 1853 lines | 1158 code | 151 blank | 544 comment | 310 complexity | b9ee617db002054fae241f56d6efce0d MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc.
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000-2007 Adaptec, Inc. (aacraid@adaptec.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2, or (at your option)
  13. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Module Name:
  25. * commsup.c
  26. *
  27. * Abstract: Contain all routines that are required for FSA host/adapter
  28. * communication.
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/init.h>
  33. #include <linux/types.h>
  34. #include <linux/sched.h>
  35. #include <linux/pci.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/slab.h>
  38. #include <linux/completion.h>
  39. #include <linux/blkdev.h>
  40. #include <linux/delay.h>
  41. #include <linux/kthread.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/semaphore.h>
  44. #include <scsi/scsi.h>
  45. #include <scsi/scsi_host.h>
  46. #include <scsi/scsi_device.h>
  47. #include <scsi/scsi_cmnd.h>
  48. #include "aacraid.h"
  49. /**
  50. * fib_map_alloc - allocate the fib objects
  51. * @dev: Adapter to allocate for
  52. *
  53. * Allocate and map the shared PCI space for the FIB blocks used to
  54. * talk to the Adaptec firmware.
  55. */
  56. static int fib_map_alloc(struct aac_dev *dev)
  57. {
  58. dprintk((KERN_INFO
  59. "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
  60. dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
  61. AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
  62. if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, dev->max_fib_size
  63. * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
  64. &dev->hw_fib_pa))==NULL)
  65. return -ENOMEM;
  66. return 0;
  67. }
  68. /**
  69. * aac_fib_map_free - free the fib objects
  70. * @dev: Adapter to free
  71. *
  72. * Free the PCI mappings and the memory allocated for FIB blocks
  73. * on this adapter.
  74. */
  75. void aac_fib_map_free(struct aac_dev *dev)
  76. {
  77. pci_free_consistent(dev->pdev,
  78. dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
  79. dev->hw_fib_va, dev->hw_fib_pa);
  80. dev->hw_fib_va = NULL;
  81. dev->hw_fib_pa = 0;
  82. }
  83. /**
  84. * aac_fib_setup - setup the fibs
  85. * @dev: Adapter to set up
  86. *
  87. * Allocate the PCI space for the fibs, map it and then intialise the
  88. * fib area, the unmapped fib data and also the free list
  89. */
  90. int aac_fib_setup(struct aac_dev * dev)
  91. {
  92. struct fib *fibptr;
  93. struct hw_fib *hw_fib;
  94. dma_addr_t hw_fib_pa;
  95. int i;
  96. while (((i = fib_map_alloc(dev)) == -ENOMEM)
  97. && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
  98. dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
  99. dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
  100. }
  101. if (i<0)
  102. return -ENOMEM;
  103. hw_fib = dev->hw_fib_va;
  104. hw_fib_pa = dev->hw_fib_pa;
  105. memset(hw_fib, 0, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
  106. /*
  107. * Initialise the fibs
  108. */
  109. for (i = 0, fibptr = &dev->fibs[i];
  110. i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB);
  111. i++, fibptr++)
  112. {
  113. fibptr->dev = dev;
  114. fibptr->hw_fib_va = hw_fib;
  115. fibptr->data = (void *) fibptr->hw_fib_va->data;
  116. fibptr->next = fibptr+1; /* Forward chain the fibs */
  117. init_MUTEX_LOCKED(&fibptr->event_wait);
  118. spin_lock_init(&fibptr->event_lock);
  119. hw_fib->header.XferState = cpu_to_le32(0xffffffff);
  120. hw_fib->header.SenderSize = cpu_to_le16(dev->max_fib_size);
  121. fibptr->hw_fib_pa = hw_fib_pa;
  122. hw_fib = (struct hw_fib *)((unsigned char *)hw_fib + dev->max_fib_size);
  123. hw_fib_pa = hw_fib_pa + dev->max_fib_size;
  124. }
  125. /*
  126. * Add the fib chain to the free list
  127. */
  128. dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
  129. /*
  130. * Enable this to debug out of queue space
  131. */
  132. dev->free_fib = &dev->fibs[0];
  133. return 0;
  134. }
  135. /**
  136. * aac_fib_alloc - allocate a fib
  137. * @dev: Adapter to allocate the fib for
  138. *
  139. * Allocate a fib from the adapter fib pool. If the pool is empty we
  140. * return NULL.
  141. */
  142. struct fib *aac_fib_alloc(struct aac_dev *dev)
  143. {
  144. struct fib * fibptr;
  145. unsigned long flags;
  146. spin_lock_irqsave(&dev->fib_lock, flags);
  147. fibptr = dev->free_fib;
  148. if(!fibptr){
  149. spin_unlock_irqrestore(&dev->fib_lock, flags);
  150. return fibptr;
  151. }
  152. dev->free_fib = fibptr->next;
  153. spin_unlock_irqrestore(&dev->fib_lock, flags);
  154. /*
  155. * Set the proper node type code and node byte size
  156. */
  157. fibptr->type = FSAFS_NTC_FIB_CONTEXT;
  158. fibptr->size = sizeof(struct fib);
  159. /*
  160. * Null out fields that depend on being zero at the start of
  161. * each I/O
  162. */
  163. fibptr->hw_fib_va->header.XferState = 0;
  164. fibptr->flags = 0;
  165. fibptr->callback = NULL;
  166. fibptr->callback_data = NULL;
  167. return fibptr;
  168. }
  169. /**
  170. * aac_fib_free - free a fib
  171. * @fibptr: fib to free up
  172. *
  173. * Frees up a fib and places it on the appropriate queue
  174. */
  175. void aac_fib_free(struct fib *fibptr)
  176. {
  177. unsigned long flags, flagsv;
  178. spin_lock_irqsave(&fibptr->event_lock, flagsv);
  179. if (fibptr->done == 2) {
  180. spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
  181. return;
  182. }
  183. spin_unlock_irqrestore(&fibptr->event_lock, flagsv);
  184. spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
  185. if (unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  186. aac_config.fib_timeouts++;
  187. if (fibptr->hw_fib_va->header.XferState != 0) {
  188. printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
  189. (void*)fibptr,
  190. le32_to_cpu(fibptr->hw_fib_va->header.XferState));
  191. }
  192. fibptr->next = fibptr->dev->free_fib;
  193. fibptr->dev->free_fib = fibptr;
  194. spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
  195. }
  196. /**
  197. * aac_fib_init - initialise a fib
  198. * @fibptr: The fib to initialize
  199. *
  200. * Set up the generic fib fields ready for use
  201. */
  202. void aac_fib_init(struct fib *fibptr)
  203. {
  204. struct hw_fib *hw_fib = fibptr->hw_fib_va;
  205. hw_fib->header.StructType = FIB_MAGIC;
  206. hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
  207. hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
  208. hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
  209. hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
  210. hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
  211. }
  212. /**
  213. * fib_deallocate - deallocate a fib
  214. * @fibptr: fib to deallocate
  215. *
  216. * Will deallocate and return to the free pool the FIB pointed to by the
  217. * caller.
  218. */
  219. static void fib_dealloc(struct fib * fibptr)
  220. {
  221. struct hw_fib *hw_fib = fibptr->hw_fib_va;
  222. BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
  223. hw_fib->header.XferState = 0;
  224. }
  225. /*
  226. * Commuication primitives define and support the queuing method we use to
  227. * support host to adapter commuication. All queue accesses happen through
  228. * these routines and are the only routines which have a knowledge of the
  229. * how these queues are implemented.
  230. */
  231. /**
  232. * aac_get_entry - get a queue entry
  233. * @dev: Adapter
  234. * @qid: Queue Number
  235. * @entry: Entry return
  236. * @index: Index return
  237. * @nonotify: notification control
  238. *
  239. * With a priority the routine returns a queue entry if the queue has free entries. If the queue
  240. * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
  241. * returned.
  242. */
  243. static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
  244. {
  245. struct aac_queue * q;
  246. unsigned long idx;
  247. /*
  248. * All of the queues wrap when they reach the end, so we check
  249. * to see if they have reached the end and if they have we just
  250. * set the index back to zero. This is a wrap. You could or off
  251. * the high bits in all updates but this is a bit faster I think.
  252. */
  253. q = &dev->queues->queue[qid];
  254. idx = *index = le32_to_cpu(*(q->headers.producer));
  255. /* Interrupt Moderation, only interrupt for first two entries */
  256. if (idx != le32_to_cpu(*(q->headers.consumer))) {
  257. if (--idx == 0) {
  258. if (qid == AdapNormCmdQueue)
  259. idx = ADAP_NORM_CMD_ENTRIES;
  260. else
  261. idx = ADAP_NORM_RESP_ENTRIES;
  262. }
  263. if (idx != le32_to_cpu(*(q->headers.consumer)))
  264. *nonotify = 1;
  265. }
  266. if (qid == AdapNormCmdQueue) {
  267. if (*index >= ADAP_NORM_CMD_ENTRIES)
  268. *index = 0; /* Wrap to front of the Producer Queue. */
  269. } else {
  270. if (*index >= ADAP_NORM_RESP_ENTRIES)
  271. *index = 0; /* Wrap to front of the Producer Queue. */
  272. }
  273. /* Queue is full */
  274. if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) {
  275. printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
  276. qid, q->numpending);
  277. return 0;
  278. } else {
  279. *entry = q->base + *index;
  280. return 1;
  281. }
  282. }
  283. /**
  284. * aac_queue_get - get the next free QE
  285. * @dev: Adapter
  286. * @index: Returned index
  287. * @priority: Priority of fib
  288. * @fib: Fib to associate with the queue entry
  289. * @wait: Wait if queue full
  290. * @fibptr: Driver fib object to go with fib
  291. * @nonotify: Don't notify the adapter
  292. *
  293. * Gets the next free QE off the requested priorty adapter command
  294. * queue and associates the Fib with the QE. The QE represented by
  295. * index is ready to insert on the queue when this routine returns
  296. * success.
  297. */
  298. 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)
  299. {
  300. struct aac_entry * entry = NULL;
  301. int map = 0;
  302. if (qid == AdapNormCmdQueue) {
  303. /* if no entries wait for some if caller wants to */
  304. while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
  305. printk(KERN_ERR "GetEntries failed\n");
  306. }
  307. /*
  308. * Setup queue entry with a command, status and fib mapped
  309. */
  310. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  311. map = 1;
  312. } else {
  313. while (!aac_get_entry(dev, qid, &entry, index, nonotify)) {
  314. /* if no entries wait for some if caller wants to */
  315. }
  316. /*
  317. * Setup queue entry with command, status and fib mapped
  318. */
  319. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  320. entry->addr = hw_fib->header.SenderFibAddress;
  321. /* Restore adapters pointer to the FIB */
  322. hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
  323. map = 0;
  324. }
  325. /*
  326. * If MapFib is true than we need to map the Fib and put pointers
  327. * in the queue entry.
  328. */
  329. if (map)
  330. entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
  331. return 0;
  332. }
  333. /*
  334. * Define the highest level of host to adapter communication routines.
  335. * These routines will support host to adapter FS commuication. These
  336. * routines have no knowledge of the commuication method used. This level
  337. * sends and receives FIBs. This level has no knowledge of how these FIBs
  338. * get passed back and forth.
  339. */
  340. /**
  341. * aac_fib_send - send a fib to the adapter
  342. * @command: Command to send
  343. * @fibptr: The fib
  344. * @size: Size of fib data area
  345. * @priority: Priority of Fib
  346. * @wait: Async/sync select
  347. * @reply: True if a reply is wanted
  348. * @callback: Called with reply
  349. * @callback_data: Passed to callback
  350. *
  351. * Sends the requested FIB to the adapter and optionally will wait for a
  352. * response FIB. If the caller does not wish to wait for a response than
  353. * an event to wait on must be supplied. This event will be set when a
  354. * response FIB is received from the adapter.
  355. */
  356. int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
  357. int priority, int wait, int reply, fib_callback callback,
  358. void *callback_data)
  359. {
  360. struct aac_dev * dev = fibptr->dev;
  361. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  362. unsigned long flags = 0;
  363. unsigned long qflags;
  364. unsigned long mflags = 0;
  365. if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
  366. return -EBUSY;
  367. /*
  368. * There are 5 cases with the wait and reponse requested flags.
  369. * The only invalid cases are if the caller requests to wait and
  370. * does not request a response and if the caller does not want a
  371. * response and the Fib is not allocated from pool. If a response
  372. * is not requesed the Fib will just be deallocaed by the DPC
  373. * routine when the response comes back from the adapter. No
  374. * further processing will be done besides deleting the Fib. We
  375. * will have a debug mode where the adapter can notify the host
  376. * it had a problem and the host can log that fact.
  377. */
  378. fibptr->flags = 0;
  379. if (wait && !reply) {
  380. return -EINVAL;
  381. } else if (!wait && reply) {
  382. hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
  383. FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
  384. } else if (!wait && !reply) {
  385. hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
  386. FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
  387. } else if (wait && reply) {
  388. hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
  389. FIB_COUNTER_INCREMENT(aac_config.NormalSent);
  390. }
  391. /*
  392. * Map the fib into 32bits by using the fib number
  393. */
  394. hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
  395. hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
  396. /*
  397. * Set FIB state to indicate where it came from and if we want a
  398. * response from the adapter. Also load the command from the
  399. * caller.
  400. *
  401. * Map the hw fib pointer as a 32bit value
  402. */
  403. hw_fib->header.Command = cpu_to_le16(command);
  404. hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
  405. fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
  406. /*
  407. * Set the size of the Fib we want to send to the adapter
  408. */
  409. hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
  410. if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
  411. return -EMSGSIZE;
  412. }
  413. /*
  414. * Get a queue entry connect the FIB to it and send an notify
  415. * the adapter a command is ready.
  416. */
  417. hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
  418. /*
  419. * Fill in the Callback and CallbackContext if we are not
  420. * going to wait.
  421. */
  422. if (!wait) {
  423. fibptr->callback = callback;
  424. fibptr->callback_data = callback_data;
  425. fibptr->flags = FIB_CONTEXT_FLAG;
  426. }
  427. fibptr->done = 0;
  428. FIB_COUNTER_INCREMENT(aac_config.FibsSent);
  429. dprintk((KERN_DEBUG "Fib contents:.\n"));
  430. dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
  431. dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
  432. dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
  433. dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
  434. dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
  435. dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
  436. if (!dev->queues)
  437. return -EBUSY;
  438. if (wait) {
  439. spin_lock_irqsave(&dev->manage_lock, mflags);
  440. if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
  441. printk(KERN_INFO "No management Fibs Available:%d\n",
  442. dev->management_fib_count);
  443. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  444. return -EBUSY;
  445. }
  446. dev->management_fib_count++;
  447. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  448. spin_lock_irqsave(&fibptr->event_lock, flags);
  449. }
  450. if (aac_adapter_deliver(fibptr) != 0) {
  451. printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
  452. if (wait) {
  453. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  454. spin_lock_irqsave(&dev->manage_lock, mflags);
  455. dev->management_fib_count--;
  456. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  457. }
  458. return -EBUSY;
  459. }
  460. /*
  461. * If the caller wanted us to wait for response wait now.
  462. */
  463. if (wait) {
  464. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  465. /* Only set for first known interruptable command */
  466. if (wait < 0) {
  467. /*
  468. * *VERY* Dangerous to time out a command, the
  469. * assumption is made that we have no hope of
  470. * functioning because an interrupt routing or other
  471. * hardware failure has occurred.
  472. */
  473. unsigned long count = 36000000L; /* 3 minutes */
  474. while (down_trylock(&fibptr->event_wait)) {
  475. int blink;
  476. if (--count == 0) {
  477. struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
  478. spin_lock_irqsave(q->lock, qflags);
  479. q->numpending--;
  480. spin_unlock_irqrestore(q->lock, qflags);
  481. if (wait == -1) {
  482. printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
  483. "Usually a result of a PCI interrupt routing problem;\n"
  484. "update mother board BIOS or consider utilizing one of\n"
  485. "the SAFE mode kernel options (acpi, apic etc)\n");
  486. }
  487. return -ETIMEDOUT;
  488. }
  489. if ((blink = aac_adapter_check_health(dev)) > 0) {
  490. if (wait == -1) {
  491. printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
  492. "Usually a result of a serious unrecoverable hardware problem\n",
  493. blink);
  494. }
  495. return -EFAULT;
  496. }
  497. udelay(5);
  498. }
  499. } else if (down_interruptible(&fibptr->event_wait)) {
  500. /* Do nothing ... satisfy
  501. * down_interruptible must_check */
  502. }
  503. spin_lock_irqsave(&fibptr->event_lock, flags);
  504. if (fibptr->done == 0) {
  505. fibptr->done = 2; /* Tell interrupt we aborted */
  506. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  507. return -ERESTARTSYS;
  508. }
  509. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  510. BUG_ON(fibptr->done == 0);
  511. if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  512. return -ETIMEDOUT;
  513. return 0;
  514. }
  515. /*
  516. * If the user does not want a response than return success otherwise
  517. * return pending
  518. */
  519. if (reply)
  520. return -EINPROGRESS;
  521. else
  522. return 0;
  523. }
  524. /**
  525. * aac_consumer_get - get the top of the queue
  526. * @dev: Adapter
  527. * @q: Queue
  528. * @entry: Return entry
  529. *
  530. * Will return a pointer to the entry on the top of the queue requested that
  531. * we are a consumer of, and return the address of the queue entry. It does
  532. * not change the state of the queue.
  533. */
  534. int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
  535. {
  536. u32 index;
  537. int status;
  538. if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
  539. status = 0;
  540. } else {
  541. /*
  542. * The consumer index must be wrapped if we have reached
  543. * the end of the queue, else we just use the entry
  544. * pointed to by the header index
  545. */
  546. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  547. index = 0;
  548. else
  549. index = le32_to_cpu(*q->headers.consumer);
  550. *entry = q->base + index;
  551. status = 1;
  552. }
  553. return(status);
  554. }
  555. /**
  556. * aac_consumer_free - free consumer entry
  557. * @dev: Adapter
  558. * @q: Queue
  559. * @qid: Queue ident
  560. *
  561. * Frees up the current top of the queue we are a consumer of. If the
  562. * queue was full notify the producer that the queue is no longer full.
  563. */
  564. void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
  565. {
  566. int wasfull = 0;
  567. u32 notify;
  568. if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
  569. wasfull = 1;
  570. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  571. *q->headers.consumer = cpu_to_le32(1);
  572. else
  573. le32_add_cpu(q->headers.consumer, 1);
  574. if (wasfull) {
  575. switch (qid) {
  576. case HostNormCmdQueue:
  577. notify = HostNormCmdNotFull;
  578. break;
  579. case HostNormRespQueue:
  580. notify = HostNormRespNotFull;
  581. break;
  582. default:
  583. BUG();
  584. return;
  585. }
  586. aac_adapter_notify(dev, notify);
  587. }
  588. }
  589. /**
  590. * aac_fib_adapter_complete - complete adapter issued fib
  591. * @fibptr: fib to complete
  592. * @size: size of fib
  593. *
  594. * Will do all necessary work to complete a FIB that was sent from
  595. * the adapter.
  596. */
  597. int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
  598. {
  599. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  600. struct aac_dev * dev = fibptr->dev;
  601. struct aac_queue * q;
  602. unsigned long nointr = 0;
  603. unsigned long qflags;
  604. if (hw_fib->header.XferState == 0) {
  605. if (dev->comm_interface == AAC_COMM_MESSAGE)
  606. kfree (hw_fib);
  607. return 0;
  608. }
  609. /*
  610. * If we plan to do anything check the structure type first.
  611. */
  612. if (hw_fib->header.StructType != FIB_MAGIC) {
  613. if (dev->comm_interface == AAC_COMM_MESSAGE)
  614. kfree (hw_fib);
  615. return -EINVAL;
  616. }
  617. /*
  618. * This block handles the case where the adapter had sent us a
  619. * command and we have finished processing the command. We
  620. * call completeFib when we are done processing the command
  621. * and want to send a response back to the adapter. This will
  622. * send the completed cdb to the adapter.
  623. */
  624. if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
  625. if (dev->comm_interface == AAC_COMM_MESSAGE) {
  626. kfree (hw_fib);
  627. } else {
  628. u32 index;
  629. hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
  630. if (size) {
  631. size += sizeof(struct aac_fibhdr);
  632. if (size > le16_to_cpu(hw_fib->header.SenderSize))
  633. return -EMSGSIZE;
  634. hw_fib->header.Size = cpu_to_le16(size);
  635. }
  636. q = &dev->queues->queue[AdapNormRespQueue];
  637. spin_lock_irqsave(q->lock, qflags);
  638. aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
  639. *(q->headers.producer) = cpu_to_le32(index + 1);
  640. spin_unlock_irqrestore(q->lock, qflags);
  641. if (!(nointr & (int)aac_config.irq_mod))
  642. aac_adapter_notify(dev, AdapNormRespQueue);
  643. }
  644. } else {
  645. printk(KERN_WARNING "aac_fib_adapter_complete: "
  646. "Unknown xferstate detected.\n");
  647. BUG();
  648. }
  649. return 0;
  650. }
  651. /**
  652. * aac_fib_complete - fib completion handler
  653. * @fib: FIB to complete
  654. *
  655. * Will do all necessary work to complete a FIB.
  656. */
  657. int aac_fib_complete(struct fib *fibptr)
  658. {
  659. unsigned long flags;
  660. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  661. /*
  662. * Check for a fib which has already been completed
  663. */
  664. if (hw_fib->header.XferState == 0)
  665. return 0;
  666. /*
  667. * If we plan to do anything check the structure type first.
  668. */
  669. if (hw_fib->header.StructType != FIB_MAGIC)
  670. return -EINVAL;
  671. /*
  672. * This block completes a cdb which orginated on the host and we
  673. * just need to deallocate the cdb or reinit it. At this point the
  674. * command is complete that we had sent to the adapter and this
  675. * cdb could be reused.
  676. */
  677. spin_lock_irqsave(&fibptr->event_lock, flags);
  678. if (fibptr->done == 2) {
  679. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  680. return 0;
  681. }
  682. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  683. if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
  684. (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
  685. {
  686. fib_dealloc(fibptr);
  687. }
  688. else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
  689. {
  690. /*
  691. * This handles the case when the host has aborted the I/O
  692. * to the adapter because the adapter is not responding
  693. */
  694. fib_dealloc(fibptr);
  695. } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
  696. fib_dealloc(fibptr);
  697. } else {
  698. BUG();
  699. }
  700. return 0;
  701. }
  702. /**
  703. * aac_printf - handle printf from firmware
  704. * @dev: Adapter
  705. * @val: Message info
  706. *
  707. * Print a message passed to us by the controller firmware on the
  708. * Adaptec board
  709. */
  710. void aac_printf(struct aac_dev *dev, u32 val)
  711. {
  712. char *cp = dev->printfbuf;
  713. if (dev->printf_enabled)
  714. {
  715. int length = val & 0xffff;
  716. int level = (val >> 16) & 0xffff;
  717. /*
  718. * The size of the printfbuf is set in port.c
  719. * There is no variable or define for it
  720. */
  721. if (length > 255)
  722. length = 255;
  723. if (cp[length] != 0)
  724. cp[length] = 0;
  725. if (level == LOG_AAC_HIGH_ERROR)
  726. printk(KERN_WARNING "%s:%s", dev->name, cp);
  727. else
  728. printk(KERN_INFO "%s:%s", dev->name, cp);
  729. }
  730. memset(cp, 0, 256);
  731. }
  732. /**
  733. * aac_handle_aif - Handle a message from the firmware
  734. * @dev: Which adapter this fib is from
  735. * @fibptr: Pointer to fibptr from adapter
  736. *
  737. * This routine handles a driver notify fib from the adapter and
  738. * dispatches it to the appropriate routine for handling.
  739. */
  740. #define AIF_SNIFF_TIMEOUT (30*HZ)
  741. static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
  742. {
  743. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  744. struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
  745. u32 channel, id, lun, container;
  746. struct scsi_device *device;
  747. enum {
  748. NOTHING,
  749. DELETE,
  750. ADD,
  751. CHANGE
  752. } device_config_needed = NOTHING;
  753. /* Sniff for container changes */
  754. if (!dev || !dev->fsa_dev)
  755. return;
  756. container = channel = id = lun = (u32)-1;
  757. /*
  758. * We have set this up to try and minimize the number of
  759. * re-configures that take place. As a result of this when
  760. * certain AIF's come in we will set a flag waiting for another
  761. * type of AIF before setting the re-config flag.
  762. */
  763. switch (le32_to_cpu(aifcmd->command)) {
  764. case AifCmdDriverNotify:
  765. switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
  766. /*
  767. * Morph or Expand complete
  768. */
  769. case AifDenMorphComplete:
  770. case AifDenVolumeExtendComplete:
  771. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  772. if (container >= dev->maximum_num_containers)
  773. break;
  774. /*
  775. * Find the scsi_device associated with the SCSI
  776. * address. Make sure we have the right array, and if
  777. * so set the flag to initiate a new re-config once we
  778. * see an AifEnConfigChange AIF come through.
  779. */
  780. if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
  781. device = scsi_device_lookup(dev->scsi_host_ptr,
  782. CONTAINER_TO_CHANNEL(container),
  783. CONTAINER_TO_ID(container),
  784. CONTAINER_TO_LUN(container));
  785. if (device) {
  786. dev->fsa_dev[container].config_needed = CHANGE;
  787. dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
  788. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  789. scsi_device_put(device);
  790. }
  791. }
  792. }
  793. /*
  794. * If we are waiting on something and this happens to be
  795. * that thing then set the re-configure flag.
  796. */
  797. if (container != (u32)-1) {
  798. if (container >= dev->maximum_num_containers)
  799. break;
  800. if ((dev->fsa_dev[container].config_waiting_on ==
  801. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  802. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  803. dev->fsa_dev[container].config_waiting_on = 0;
  804. } else for (container = 0;
  805. container < dev->maximum_num_containers; ++container) {
  806. if ((dev->fsa_dev[container].config_waiting_on ==
  807. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  808. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  809. dev->fsa_dev[container].config_waiting_on = 0;
  810. }
  811. break;
  812. case AifCmdEventNotify:
  813. switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
  814. case AifEnBatteryEvent:
  815. dev->cache_protected =
  816. (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
  817. break;
  818. /*
  819. * Add an Array.
  820. */
  821. case AifEnAddContainer:
  822. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  823. if (container >= dev->maximum_num_containers)
  824. break;
  825. dev->fsa_dev[container].config_needed = ADD;
  826. dev->fsa_dev[container].config_waiting_on =
  827. AifEnConfigChange;
  828. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  829. break;
  830. /*
  831. * Delete an Array.
  832. */
  833. case AifEnDeleteContainer:
  834. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  835. if (container >= dev->maximum_num_containers)
  836. break;
  837. dev->fsa_dev[container].config_needed = DELETE;
  838. dev->fsa_dev[container].config_waiting_on =
  839. AifEnConfigChange;
  840. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  841. break;
  842. /*
  843. * Container change detected. If we currently are not
  844. * waiting on something else, setup to wait on a Config Change.
  845. */
  846. case AifEnContainerChange:
  847. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  848. if (container >= dev->maximum_num_containers)
  849. break;
  850. if (dev->fsa_dev[container].config_waiting_on &&
  851. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  852. break;
  853. dev->fsa_dev[container].config_needed = CHANGE;
  854. dev->fsa_dev[container].config_waiting_on =
  855. AifEnConfigChange;
  856. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  857. break;
  858. case AifEnConfigChange:
  859. break;
  860. case AifEnAddJBOD:
  861. case AifEnDeleteJBOD:
  862. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  863. if ((container >> 28)) {
  864. container = (u32)-1;
  865. break;
  866. }
  867. channel = (container >> 24) & 0xF;
  868. if (channel >= dev->maximum_num_channels) {
  869. container = (u32)-1;
  870. break;
  871. }
  872. id = container & 0xFFFF;
  873. if (id >= dev->maximum_num_physicals) {
  874. container = (u32)-1;
  875. break;
  876. }
  877. lun = (container >> 16) & 0xFF;
  878. container = (u32)-1;
  879. channel = aac_phys_to_logical(channel);
  880. device_config_needed =
  881. (((__le32 *)aifcmd->data)[0] ==
  882. cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
  883. break;
  884. case AifEnEnclosureManagement:
  885. /*
  886. * If in JBOD mode, automatic exposure of new
  887. * physical target to be suppressed until configured.
  888. */
  889. if (dev->jbod)
  890. break;
  891. switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
  892. case EM_DRIVE_INSERTION:
  893. case EM_DRIVE_REMOVAL:
  894. container = le32_to_cpu(
  895. ((__le32 *)aifcmd->data)[2]);
  896. if ((container >> 28)) {
  897. container = (u32)-1;
  898. break;
  899. }
  900. channel = (container >> 24) & 0xF;
  901. if (channel >= dev->maximum_num_channels) {
  902. container = (u32)-1;
  903. break;
  904. }
  905. id = container & 0xFFFF;
  906. lun = (container >> 16) & 0xFF;
  907. container = (u32)-1;
  908. if (id >= dev->maximum_num_physicals) {
  909. /* legacy dev_t ? */
  910. if ((0x2000 <= id) || lun || channel ||
  911. ((channel = (id >> 7) & 0x3F) >=
  912. dev->maximum_num_channels))
  913. break;
  914. lun = (id >> 4) & 7;
  915. id &= 0xF;
  916. }
  917. channel = aac_phys_to_logical(channel);
  918. device_config_needed =
  919. (((__le32 *)aifcmd->data)[3]
  920. == cpu_to_le32(EM_DRIVE_INSERTION)) ?
  921. ADD : DELETE;
  922. break;
  923. }
  924. break;
  925. }
  926. /*
  927. * If we are waiting on something and this happens to be
  928. * that thing then set the re-configure flag.
  929. */
  930. if (container != (u32)-1) {
  931. if (container >= dev->maximum_num_containers)
  932. break;
  933. if ((dev->fsa_dev[container].config_waiting_on ==
  934. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  935. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  936. dev->fsa_dev[container].config_waiting_on = 0;
  937. } else for (container = 0;
  938. container < dev->maximum_num_containers; ++container) {
  939. if ((dev->fsa_dev[container].config_waiting_on ==
  940. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  941. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  942. dev->fsa_dev[container].config_waiting_on = 0;
  943. }
  944. break;
  945. case AifCmdJobProgress:
  946. /*
  947. * These are job progress AIF's. When a Clear is being
  948. * done on a container it is initially created then hidden from
  949. * the OS. When the clear completes we don't get a config
  950. * change so we monitor the job status complete on a clear then
  951. * wait for a container change.
  952. */
  953. if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
  954. (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
  955. ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
  956. for (container = 0;
  957. container < dev->maximum_num_containers;
  958. ++container) {
  959. /*
  960. * Stomp on all config sequencing for all
  961. * containers?
  962. */
  963. dev->fsa_dev[container].config_waiting_on =
  964. AifEnContainerChange;
  965. dev->fsa_dev[container].config_needed = ADD;
  966. dev->fsa_dev[container].config_waiting_stamp =
  967. jiffies;
  968. }
  969. }
  970. if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
  971. ((__le32 *)aifcmd->data)[6] == 0 &&
  972. ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
  973. for (container = 0;
  974. container < dev->maximum_num_containers;
  975. ++container) {
  976. /*
  977. * Stomp on all config sequencing for all
  978. * containers?
  979. */
  980. dev->fsa_dev[container].config_waiting_on =
  981. AifEnContainerChange;
  982. dev->fsa_dev[container].config_needed = DELETE;
  983. dev->fsa_dev[container].config_waiting_stamp =
  984. jiffies;
  985. }
  986. }
  987. break;
  988. }
  989. container = 0;
  990. retry_next:
  991. if (device_config_needed == NOTHING)
  992. for (; container < dev->maximum_num_containers; ++container) {
  993. if ((dev->fsa_dev[container].config_waiting_on == 0) &&
  994. (dev->fsa_dev[container].config_needed != NOTHING) &&
  995. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
  996. device_config_needed =
  997. dev->fsa_dev[container].config_needed;
  998. dev->fsa_dev[container].config_needed = NOTHING;
  999. channel = CONTAINER_TO_CHANNEL(container);
  1000. id = CONTAINER_TO_ID(container);
  1001. lun = CONTAINER_TO_LUN(container);
  1002. break;
  1003. }
  1004. }
  1005. if (device_config_needed == NOTHING)
  1006. return;
  1007. /*
  1008. * If we decided that a re-configuration needs to be done,
  1009. * schedule it here on the way out the door, please close the door
  1010. * behind you.
  1011. */
  1012. /*
  1013. * Find the scsi_device associated with the SCSI address,
  1014. * and mark it as changed, invalidating the cache. This deals
  1015. * with changes to existing device IDs.
  1016. */
  1017. if (!dev || !dev->scsi_host_ptr)
  1018. return;
  1019. /*
  1020. * force reload of disk info via aac_probe_container
  1021. */
  1022. if ((channel == CONTAINER_CHANNEL) &&
  1023. (device_config_needed != NOTHING)) {
  1024. if (dev->fsa_dev[container].valid == 1)
  1025. dev->fsa_dev[container].valid = 2;
  1026. aac_probe_container(dev, container);
  1027. }
  1028. device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
  1029. if (device) {
  1030. switch (device_config_needed) {
  1031. case DELETE:
  1032. if (scsi_device_online(device)) {
  1033. scsi_device_set_state(device, SDEV_OFFLINE);
  1034. sdev_printk(KERN_INFO, device,
  1035. "Device offlined - %s\n",
  1036. (channel == CONTAINER_CHANNEL) ?
  1037. "array deleted" :
  1038. "enclosure services event");
  1039. }
  1040. break;
  1041. case ADD:
  1042. if (!scsi_device_online(device)) {
  1043. sdev_printk(KERN_INFO, device,
  1044. "Device online - %s\n",
  1045. (channel == CONTAINER_CHANNEL) ?
  1046. "array created" :
  1047. "enclosure services event");
  1048. scsi_device_set_state(device, SDEV_RUNNING);
  1049. }
  1050. /* FALLTHRU */
  1051. case CHANGE:
  1052. if ((channel == CONTAINER_CHANNEL)
  1053. && (!dev->fsa_dev[container].valid)) {
  1054. if (!scsi_device_online(device))
  1055. break;
  1056. scsi_device_set_state(device, SDEV_OFFLINE);
  1057. sdev_printk(KERN_INFO, device,
  1058. "Device offlined - %s\n",
  1059. "array failed");
  1060. break;
  1061. }
  1062. scsi_rescan_device(&device->sdev_gendev);
  1063. default:
  1064. break;
  1065. }
  1066. scsi_device_put(device);
  1067. device_config_needed = NOTHING;
  1068. }
  1069. if (device_config_needed == ADD)
  1070. scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
  1071. if (channel == CONTAINER_CHANNEL) {
  1072. container++;
  1073. device_config_needed = NOTHING;
  1074. goto retry_next;
  1075. }
  1076. }
  1077. static int _aac_reset_adapter(struct aac_dev *aac, int forced)
  1078. {
  1079. int index, quirks;
  1080. int retval;
  1081. struct Scsi_Host *host;
  1082. struct scsi_device *dev;
  1083. struct scsi_cmnd *command;
  1084. struct scsi_cmnd *command_list;
  1085. int jafo = 0;
  1086. /*
  1087. * Assumptions:
  1088. * - host is locked, unless called by the aacraid thread.
  1089. * (a matter of convenience, due to legacy issues surrounding
  1090. * eh_host_adapter_reset).
  1091. * - in_reset is asserted, so no new i/o is getting to the
  1092. * card.
  1093. * - The card is dead, or will be very shortly ;-/ so no new
  1094. * commands are completing in the interrupt service.
  1095. */
  1096. host = aac->scsi_host_ptr;
  1097. scsi_block_requests(host);
  1098. aac_adapter_disable_int(aac);
  1099. if (aac->thread->pid != current->pid) {
  1100. spin_unlock_irq(host->host_lock);
  1101. kthread_stop(aac->thread);
  1102. jafo = 1;
  1103. }
  1104. /*
  1105. * If a positive health, means in a known DEAD PANIC
  1106. * state and the adapter could be reset to `try again'.
  1107. */
  1108. retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
  1109. if (retval)
  1110. goto out;
  1111. /*
  1112. * Loop through the fibs, close the synchronous FIBS
  1113. */
  1114. for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
  1115. struct fib *fib = &aac->fibs[index];
  1116. if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
  1117. (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
  1118. unsigned long flagv;
  1119. spin_lock_irqsave(&fib->event_lock, flagv);
  1120. up(&fib->event_wait);
  1121. spin_unlock_irqrestore(&fib->event_lock, flagv);
  1122. schedule();
  1123. retval = 0;
  1124. }
  1125. }
  1126. /* Give some extra time for ioctls to complete. */
  1127. if (retval == 0)
  1128. ssleep(2);
  1129. index = aac->cardtype;
  1130. /*
  1131. * Re-initialize the adapter, first free resources, then carefully
  1132. * apply the initialization sequence to come back again. Only risk
  1133. * is a change in Firmware dropping cache, it is assumed the caller
  1134. * will ensure that i/o is queisced and the card is flushed in that
  1135. * case.
  1136. */
  1137. aac_fib_map_free(aac);
  1138. pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
  1139. aac->comm_addr = NULL;
  1140. aac->comm_phys = 0;
  1141. kfree(aac->queues);
  1142. aac->queues = NULL;
  1143. free_irq(aac->pdev->irq, aac);
  1144. kfree(aac->fsa_dev);
  1145. aac->fsa_dev = NULL;
  1146. quirks = aac_get_driver_ident(index)->quirks;
  1147. if (quirks & AAC_QUIRK_31BIT) {
  1148. if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
  1149. ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
  1150. goto out;
  1151. } else {
  1152. if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
  1153. ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
  1154. goto out;
  1155. }
  1156. if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
  1157. goto out;
  1158. if (quirks & AAC_QUIRK_31BIT)
  1159. if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
  1160. goto out;
  1161. if (jafo) {
  1162. aac->thread = kthread_run(aac_command_thread, aac, aac->name);
  1163. if (IS_ERR(aac->thread)) {
  1164. retval = PTR_ERR(aac->thread);
  1165. goto out;
  1166. }
  1167. }
  1168. (void)aac_get_adapter_info(aac);
  1169. if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
  1170. host->sg_tablesize = 34;
  1171. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1172. }
  1173. if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
  1174. host->sg_tablesize = 17;
  1175. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1176. }
  1177. aac_get_config_status(aac, 1);
  1178. aac_get_containers(aac);
  1179. /*
  1180. * This is where the assumption that the Adapter is quiesced
  1181. * is important.
  1182. */
  1183. command_list = NULL;
  1184. __shost_for_each_device(dev, host) {
  1185. unsigned long flags;
  1186. spin_lock_irqsave(&dev->list_lock, flags);
  1187. list_for_each_entry(command, &dev->cmd_list, list)
  1188. if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
  1189. command->SCp.buffer = (struct scatterlist *)command_list;
  1190. command_list = command;
  1191. }
  1192. spin_unlock_irqrestore(&dev->list_lock, flags);
  1193. }
  1194. while ((command = command_list)) {
  1195. command_list = (struct scsi_cmnd *)command->SCp.buffer;
  1196. command->SCp.buffer = NULL;
  1197. command->result = DID_OK << 16
  1198. | COMMAND_COMPLETE << 8
  1199. | SAM_STAT_TASK_SET_FULL;
  1200. command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
  1201. command->scsi_done(command);
  1202. }
  1203. retval = 0;
  1204. out:
  1205. aac->in_reset = 0;
  1206. scsi_unblock_requests(host);
  1207. if (jafo) {
  1208. spin_lock_irq(host->host_lock);
  1209. }
  1210. return retval;
  1211. }
  1212. int aac_reset_adapter(struct aac_dev * aac, int forced)
  1213. {
  1214. unsigned long flagv = 0;
  1215. int retval;
  1216. struct Scsi_Host * host;
  1217. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1218. return -EBUSY;
  1219. if (aac->in_reset) {
  1220. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1221. return -EBUSY;
  1222. }
  1223. aac->in_reset = 1;
  1224. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1225. /*
  1226. * Wait for all commands to complete to this specific
  1227. * target (block maximum 60 seconds). Although not necessary,
  1228. * it does make us a good storage citizen.
  1229. */
  1230. host = aac->scsi_host_ptr;
  1231. scsi_block_requests(host);
  1232. if (forced < 2) for (retval = 60; retval; --retval) {
  1233. struct scsi_device * dev;
  1234. struct scsi_cmnd * command;
  1235. int active = 0;
  1236. __shost_for_each_device(dev, host) {
  1237. spin_lock_irqsave(&dev->list_lock, flagv);
  1238. list_for_each_entry(command, &dev->cmd_list, list) {
  1239. if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
  1240. active++;
  1241. break;
  1242. }
  1243. }
  1244. spin_unlock_irqrestore(&dev->list_lock, flagv);
  1245. if (active)
  1246. break;
  1247. }
  1248. /*
  1249. * We can exit If all the commands are complete
  1250. */
  1251. if (active == 0)
  1252. break;
  1253. ssleep(1);
  1254. }
  1255. /* Quiesce build, flush cache, write through mode */
  1256. if (forced < 2)
  1257. aac_send_shutdown(aac);
  1258. spin_lock_irqsave(host->host_lock, flagv);
  1259. retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
  1260. spin_unlock_irqrestore(host->host_lock, flagv);
  1261. if ((forced < 2) && (retval == -ENODEV)) {
  1262. /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
  1263. struct fib * fibctx = aac_fib_alloc(aac);
  1264. if (fibctx) {
  1265. struct aac_pause *cmd;
  1266. int status;
  1267. aac_fib_init(fibctx);
  1268. cmd = (struct aac_pause *) fib_data(fibctx);
  1269. cmd->command = cpu_to_le32(VM_ContainerConfig);
  1270. cmd->type = cpu_to_le32(CT_PAUSE_IO);
  1271. cmd->timeout = cpu_to_le32(1);
  1272. cmd->min = cpu_to_le32(1);
  1273. cmd->noRescan = cpu_to_le32(1);
  1274. cmd->count = cpu_to_le32(0);
  1275. status = aac_fib_send(ContainerCommand,
  1276. fibctx,
  1277. sizeof(struct aac_pause),
  1278. FsaNormal,
  1279. -2 /* Timeout silently */, 1,
  1280. NULL, NULL);
  1281. if (status >= 0)
  1282. aac_fib_complete(fibctx);
  1283. /* FIB should be freed only after getting
  1284. * the response from the F/W */
  1285. if (status != -ERESTARTSYS)
  1286. aac_fib_free(fibctx);
  1287. }
  1288. }
  1289. return retval;
  1290. }
  1291. int aac_check_health(struct aac_dev * aac)
  1292. {
  1293. int BlinkLED;
  1294. unsigned long time_now, flagv = 0;
  1295. struct list_head * entry;
  1296. struct Scsi_Host * host;
  1297. /* Extending the scope of fib_lock slightly to protect aac->in_reset */
  1298. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1299. return 0;
  1300. if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
  1301. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1302. return 0; /* OK */
  1303. }
  1304. aac->in_reset = 1;
  1305. /* Fake up an AIF:
  1306. * aac_aifcmd.command = AifCmdEventNotify = 1
  1307. * aac_aifcmd.seqnum = 0xFFFFFFFF
  1308. * aac_aifcmd.data[0] = AifEnExpEvent = 23
  1309. * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
  1310. * aac.aifcmd.data[2] = AifHighPriority = 3
  1311. * aac.aifcmd.data[3] = BlinkLED
  1312. */
  1313. time_now = jiffies/HZ;
  1314. entry = aac->fib_list.next;
  1315. /*
  1316. * For each Context that is on the
  1317. * fibctxList, make a copy of the
  1318. * fib, and then set the event to wake up the
  1319. * thread that is waiting for it.
  1320. */
  1321. while (entry != &aac->fib_list) {
  1322. /*
  1323. * Extract the fibctx
  1324. */
  1325. struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
  1326. struct hw_fib * hw_fib;
  1327. struct fib * fib;
  1328. /*
  1329. * Check if the queue is getting
  1330. * backlogged
  1331. */
  1332. if (fibctx->count > 20) {
  1333. /*
  1334. * It's *not* jiffies folks,
  1335. * but jiffies / HZ, so do not
  1336. * panic ...
  1337. */
  1338. u32 time_last = fibctx->jiffies;
  1339. /*
  1340. * Has it been > 2 minutes
  1341. * since the last read off
  1342. * the queue?
  1343. */
  1344. if ((time_now - time_last) > aif_timeout) {
  1345. entry = entry->next;
  1346. aac_close_fib_context(aac, fibctx);
  1347. continue;
  1348. }
  1349. }
  1350. /*
  1351. * Warning: no sleep allowed while
  1352. * holding spinlock
  1353. */
  1354. hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
  1355. fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
  1356. if (fib && hw_fib) {
  1357. struct aac_aifcmd * aif;
  1358. fib->hw_fib_va = hw_fib;
  1359. fib->dev = aac;
  1360. aac_fib_init(fib);
  1361. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1362. fib->size = sizeof (struct fib);
  1363. fib->data = hw_fib->data;
  1364. aif = (struct aac_aifcmd *)hw_fib->data;
  1365. aif->command = cpu_to_le32(AifCmdEventNotify);
  1366. aif->seqnum = cpu_to_le32(0xFFFFFFFF);
  1367. ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
  1368. ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
  1369. ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
  1370. ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
  1371. /*
  1372. * Put the FIB onto the
  1373. * fibctx's fibs
  1374. */
  1375. list_add_tail(&fib->fiblink, &fibctx->fib_list);
  1376. fibctx->count++;
  1377. /*
  1378. * Set the event to wake up the
  1379. * thread that will waiting.
  1380. */
  1381. up(&fibctx->wait_sem);
  1382. } else {
  1383. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1384. kfree(fib);
  1385. kfree(hw_fib);
  1386. }
  1387. entry = entry->next;
  1388. }
  1389. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1390. if (BlinkLED < 0) {
  1391. printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
  1392. goto out;
  1393. }
  1394. printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
  1395. if (!aac_check_reset || ((aac_check_reset == 1) &&
  1396. (aac->supplement_adapter_info.SupportedOptions2 &
  1397. AAC_OPTION_IGNORE_RESET)))
  1398. goto out;
  1399. host = aac->scsi_host_ptr;
  1400. if (aac->thread->pid != current->pid)
  1401. spin_lock_irqsave(host->host_lock, flagv);
  1402. BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
  1403. if (aac->thread->pid != current->pid)
  1404. spin_unlock_irqrestore(host->host_lock, flagv);
  1405. return BlinkLED;
  1406. out:
  1407. aac->in_reset = 0;
  1408. return BlinkLED;
  1409. }
  1410. /**
  1411. * aac_command_thread - command processing thread
  1412. * @dev: Adapter to monitor
  1413. *
  1414. * Waits on the commandready event in it's queue. When the event gets set
  1415. * it will pull FIBs off it's queue. It will continue to pull FIBs off
  1416. * until the queue is empty. When the queue is empty it will wait for
  1417. * more FIBs.
  1418. */
  1419. int aac_command_thread(void *data)
  1420. {
  1421. struct aac_dev *dev = data;
  1422. struct hw_fib *hw_fib, *hw_newfib;
  1423. struct fib *fib, *newfib;
  1424. struct aac_fib_context *fibctx;
  1425. unsigned long flags;
  1426. DECLARE_WAITQUEUE(wait, current);
  1427. unsigned long next_jiffies = jiffies + HZ;
  1428. unsigned long next_check_jiffies = next_jiffies;
  1429. long difference = HZ;
  1430. /*
  1431. * We can only have one thread per adapter for AIF's.
  1432. */
  1433. if (dev->aif_thread)
  1434. return -EINVAL;
  1435. /*
  1436. * Let the DPC know it has a place to send the AIF's to.
  1437. */
  1438. dev->aif_thread = 1;
  1439. add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1440. set_current_state(TASK_INTERRUPTIBLE);
  1441. dprintk ((KERN_INFO "aac_command_thread start\n"));
  1442. while (1) {
  1443. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1444. while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
  1445. struct list_head *entry;
  1446. struct aac_aifcmd * aifcmd;
  1447. set_current_state(TASK_RUNNING);
  1448. entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
  1449. list_del(entry);
  1450. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1451. fib = list_entry(entry, struct fib, fiblink);
  1452. /*
  1453. * We will process the FIB here or pass it to a
  1454. * worker thread that is TBD. We Really can't
  1455. * do anything at this point since we don't have
  1456. * anything defined for this thread to do.
  1457. */
  1458. hw_fib = fib->hw_fib_va;
  1459. memset(fib, 0, sizeof(struct fib));
  1460. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1461. fib->size = sizeof(struct fib);
  1462. fib->hw_fib_va = hw_fib;
  1463. fib->data = hw_fib->data;
  1464. fib->dev = dev;
  1465. /*
  1466. * We only handle AifRequest fibs from the adapter.
  1467. */
  1468. aifcmd = (struct aac_aifcmd *) hw_fib->data;
  1469. if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
  1470. /* Handle Driver Notify Events */
  1471. aac_handle_aif(dev, fib);
  1472. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1473. aac_fib_adapter_complete(fib, (u16)sizeof(u32));
  1474. } else {
  1475. /* The u32 here is important and intended. We are using
  1476. 32bit wrapping time to fit the adapter field */
  1477. u32 time_now, time_last;
  1478. unsigned long flagv;
  1479. unsigned num;
  1480. struct hw_fib ** hw_fib_pool, ** hw_fib_p;
  1481. struct fib ** fib_pool, ** fib_p;
  1482. /* Sniff events */
  1483. if ((aifcmd->command ==
  1484. cpu_to_le32(AifCmdEventNotify)) ||
  1485. (aifcmd->command ==
  1486. cpu_to_le32(AifCmdJobProgress))) {
  1487. aac_handle_aif(dev, fib);
  1488. }
  1489. time_now = jiffies/HZ;
  1490. /*
  1491. * Warning: no sleep allowed while
  1492. * holding spinlock. We take the estimate
  1493. * and pre-allocate a set of fibs outside the
  1494. * lock.
  1495. */
  1496. num = le32_to_cpu(dev->init->AdapterFibsSize)
  1497. / sizeof(struct hw_fib); /* some extra */
  1498. spin_lock_irqsave(&dev->fib_lock, flagv);
  1499. entry = dev->fib_list.next;
  1500. while (entry != &dev->fib_list) {
  1501. entry = entry->next;
  1502. ++num;
  1503. }
  1504. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1505. hw_fib_pool = NULL;
  1506. fib_pool = NULL;
  1507. if (num
  1508. && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
  1509. && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
  1510. hw_fib_p = hw_fib_pool;
  1511. fib_p = fib_pool;
  1512. while (hw_fib_p < &hw_fib_pool[num]) {
  1513. if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
  1514. --hw_fib_p;
  1515. break;
  1516. }
  1517. if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
  1518. kfree(*(--hw_fib_p));
  1519. break;
  1520. }
  1521. }
  1522. if ((num = hw_fib_p - hw_fib_pool) == 0) {
  1523. kfree(fib_pool);
  1524. fib_pool = NULL;
  1525. kfree(hw_fib_pool);
  1526. hw_fib_pool = NULL;
  1527. }
  1528. } else {
  1529. kfree(hw_fib_pool);
  1530. hw_fib_pool = NULL;
  1531. }
  1532. spin_lock_irqsave(&dev->fib_lock, flagv);
  1533. entry = dev->fib_list.next;
  1534. /*
  1535. * For each Context that is on the
  1536. * fibctxList, make a copy of the
  1537. * fib, and then set the event to wake up the
  1538. * thread that is waiting for it.
  1539. */
  1540. hw_fib_p = hw_fib_pool;
  1541. fib_p = fib_pool;
  1542. while (entry != &dev->fib_list) {
  1543. /*
  1544. * Extract the fibctx
  1545. */
  1546. fibctx = list_entry(entry, struct aac_fib_context, next);
  1547. /*
  1548. * Check if the queue is getting
  1549. * backlogged
  1550. */
  1551. if (fibctx->count > 20)
  1552. {
  1553. /*
  1554. * It's *not* jiffies folks,
  1555. * but jiffies / HZ so do not
  1556. * panic ...
  1557. */
  1558. time_last = fibctx->jiffies;
  1559. /*
  1560. * Has it been > 2 minutes
  1561. * since the last read off
  1562. * the queue?
  1563. */
  1564. if ((time_now - time_last) > aif_timeout) {
  1565. entry = entry->next;
  1566. aac_close_fib_context(dev, fibctx);
  1567. continue;
  1568. }
  1569. }
  1570. /*
  1571. * Warning: no sleep allowed while
  1572. * holding spinlock
  1573. */
  1574. if (hw_fib_p < &hw_fib_pool[num]) {
  1575. hw_newfib = *hw_fib_p;
  1576. *(hw_fib_p++) = NULL;
  1577. newfib = *fib_p;
  1578. *(fib_p++) = NULL;
  1579. /*
  1580. * Make the copy of the FIB
  1581. */
  1582. memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
  1583. memcpy(newfib, fib, sizeof(struct fib));
  1584. newfib->hw_fib_va = hw_newfib;
  1585. /*
  1586. * Put the FIB onto the
  1587. * fibctx's fibs
  1588. */
  1589. list_add_tail(&newfib->fiblink, &fibctx->fib_list);
  1590. fibctx->count++;
  1591. /*
  1592. * Set the event to wake up the
  1593. * thread that is waiting.
  1594. */
  1595. up(&fibctx->wait_sem);
  1596. } else {
  1597. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1598. }
  1599. entry = entry->next;
  1600. }
  1601. /*
  1602. * Set the status of this FIB
  1603. */
  1604. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1605. aac_fib_adapter_complete(fib, sizeof(u32));
  1606. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1607. /* Free up the remaining resources */
  1608. hw_fib_p = hw_fib_pool;
  1609. fib_p = fib_pool;
  1610. while (hw_fib_p < &hw_fib_pool[num]) {
  1611. kfree(*hw_fib_p);
  1612. kfree(*fib_p);
  1613. ++fib_p;
  1614. ++hw_fib_p;
  1615. }
  1616. kfree(hw_fib_pool);
  1617. kfree(fib_pool);
  1618. }
  1619. kfree(fib);
  1620. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1621. }
  1622. /*
  1623. * There are no more AIF's
  1624. */
  1625. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1626. /*
  1627. * Background activity
  1628. */
  1629. if ((time_before(next_check_jiffies,next_jiffies))
  1630. && ((difference = next_check_jiffies - jiffies) <= 0)) {
  1631. next_check_jiffies = next_jiffies;
  1632. if (aac_check_health(dev) == 0) {
  1633. difference = ((long)(unsigned)check_interval)
  1634. * HZ;
  1635. next_check_jiffies = jiffies + difference;
  1636. } else if (!dev->queues)
  1637. break;
  1638. }
  1639. if (!time_before(next_check_jiffies,next_jiffies)
  1640. && ((difference = next_jiffies - jiffies) <= 0)) {
  1641. struct timeval now;
  1642. int ret;
  1643. /* Don't even try to talk to adapter if its sick */
  1644. ret = aac_check_health(dev);
  1645. if (!ret && !dev->queues)
  1646. break;
  1647. next_check_jiffies = jiffies
  1648. + ((long)(unsigned)check_interval)
  1649. * HZ;
  1650. do_gettimeofday(&now);
  1651. /* Synchronize our watches */
  1652. if (((1000000 - (1000000 / HZ)) > now.tv_usec)
  1653. && (now.tv_usec > (1000000 / HZ)))
  1654. difference = (((1000000 - now.tv_usec) * HZ)
  1655. + 500000) / 1000000;
  1656. else if (ret == 0) {
  1657. struct fib *fibptr;
  1658. if ((fibptr = aac_fib_alloc(dev))) {
  1659. int status;
  1660. __le32 *info;
  1661. aac_fib_init(fibptr);
  1662. info = (__le32 *) fib_data(fibptr);
  1663. if (now.tv_usec > 500000)
  1664. ++now.tv_sec;
  1665. *info = cpu_to_le32(now.tv_sec);
  1666. status = aac_fib_send(SendHostTime,
  1667. fibptr,
  1668. sizeof(*info),
  1669. FsaNormal,
  1670. 1, 1,
  1671. NULL,
  1672. NULL);
  1673. /* Do not set XferState to zero unless
  1674. * receives a response from F/W */
  1675. if (status >= 0)
  1676. aac_fib_complete(fibptr);
  1677. /* FIB should be freed only after
  1678. * getting the response from the F/W */
  1679. if (status != -ERESTARTSYS)
  1680. aac_fib_free(fibptr);
  1681. }
  1682. difference = (long)(unsigned)update_interval*HZ;
  1683. } else {
  1684. /* retry shortly */
  1685. difference = 10 * HZ;
  1686. }
  1687. next_jiffies = jiffies + difference;
  1688. if (time_before(next_check_jiffies,next_jiffies))
  1689. difference = next_check_jiffies - jiffies;
  1690. }
  1691. if (difference <= 0)
  1692. difference = 1;
  1693. set_current_state(TASK_INTERRUPTIBLE);
  1694. schedule_timeout(difference);
  1695. if (kthread_should_stop())
  1696. break;
  1697. }
  1698. if (dev->queues)
  1699. remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1700. dev->aif_thread = 0;
  1701. return 0;
  1702. }