commsup.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /*
  2. * Adaptec AAC series RAID controller driver
  3. * (c) Copyright 2001 Red Hat Inc. <alan@redhat.com>
  4. *
  5. * based on the old aacraid driver that is..
  6. * Adaptec aacraid device driver for Linux.
  7. *
  8. * Copyright (c) 2000 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 <scsi/scsi_host.h>
  43. #include <scsi/scsi_device.h>
  44. #include <asm/semaphore.h>
  45. #include "aacraid.h"
  46. /**
  47. * fib_map_alloc - allocate the fib objects
  48. * @dev: Adapter to allocate for
  49. *
  50. * Allocate and map the shared PCI space for the FIB blocks used to
  51. * talk to the Adaptec firmware.
  52. */
  53. static int fib_map_alloc(struct aac_dev *dev)
  54. {
  55. dprintk((KERN_INFO
  56. "allocate hardware fibs pci_alloc_consistent(%p, %d * (%d + %d), %p)\n",
  57. dev->pdev, dev->max_fib_size, dev->scsi_host_ptr->can_queue,
  58. AAC_NUM_MGT_FIB, &dev->hw_fib_pa));
  59. if((dev->hw_fib_va = pci_alloc_consistent(dev->pdev, dev->max_fib_size
  60. * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB),
  61. &dev->hw_fib_pa))==NULL)
  62. return -ENOMEM;
  63. return 0;
  64. }
  65. /**
  66. * aac_fib_map_free - free the fib objects
  67. * @dev: Adapter to free
  68. *
  69. * Free the PCI mappings and the memory allocated for FIB blocks
  70. * on this adapter.
  71. */
  72. void aac_fib_map_free(struct aac_dev *dev)
  73. {
  74. pci_free_consistent(dev->pdev, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB), dev->hw_fib_va, dev->hw_fib_pa);
  75. }
  76. /**
  77. * aac_fib_setup - setup the fibs
  78. * @dev: Adapter to set up
  79. *
  80. * Allocate the PCI space for the fibs, map it and then intialise the
  81. * fib area, the unmapped fib data and also the free list
  82. */
  83. int aac_fib_setup(struct aac_dev * dev)
  84. {
  85. struct fib *fibptr;
  86. struct hw_fib *hw_fib_va;
  87. dma_addr_t hw_fib_pa;
  88. int i;
  89. while (((i = fib_map_alloc(dev)) == -ENOMEM)
  90. && (dev->scsi_host_ptr->can_queue > (64 - AAC_NUM_MGT_FIB))) {
  91. dev->init->MaxIoCommands = cpu_to_le32((dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB) >> 1);
  92. dev->scsi_host_ptr->can_queue = le32_to_cpu(dev->init->MaxIoCommands) - AAC_NUM_MGT_FIB;
  93. }
  94. if (i<0)
  95. return -ENOMEM;
  96. hw_fib_va = dev->hw_fib_va;
  97. hw_fib_pa = dev->hw_fib_pa;
  98. memset(hw_fib_va, 0, dev->max_fib_size * (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB));
  99. /*
  100. * Initialise the fibs
  101. */
  102. for (i = 0, fibptr = &dev->fibs[i]; i < (dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); i++, fibptr++)
  103. {
  104. fibptr->dev = dev;
  105. fibptr->hw_fib = hw_fib_va;
  106. fibptr->data = (void *) fibptr->hw_fib->data;
  107. fibptr->next = fibptr+1; /* Forward chain the fibs */
  108. init_MUTEX_LOCKED(&fibptr->event_wait);
  109. spin_lock_init(&fibptr->event_lock);
  110. hw_fib_va->header.XferState = cpu_to_le32(0xffffffff);
  111. hw_fib_va->header.SenderSize = cpu_to_le16(dev->max_fib_size);
  112. fibptr->hw_fib_pa = hw_fib_pa;
  113. hw_fib_va = (struct hw_fib *)((unsigned char *)hw_fib_va + dev->max_fib_size);
  114. hw_fib_pa = hw_fib_pa + dev->max_fib_size;
  115. }
  116. /*
  117. * Add the fib chain to the free list
  118. */
  119. dev->fibs[dev->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB - 1].next = NULL;
  120. /*
  121. * Enable this to debug out of queue space
  122. */
  123. dev->free_fib = &dev->fibs[0];
  124. return 0;
  125. }
  126. /**
  127. * aac_fib_alloc - allocate a fib
  128. * @dev: Adapter to allocate the fib for
  129. *
  130. * Allocate a fib from the adapter fib pool. If the pool is empty we
  131. * return NULL.
  132. */
  133. struct fib *aac_fib_alloc(struct aac_dev *dev)
  134. {
  135. struct fib * fibptr;
  136. unsigned long flags;
  137. spin_lock_irqsave(&dev->fib_lock, flags);
  138. fibptr = dev->free_fib;
  139. if(!fibptr){
  140. spin_unlock_irqrestore(&dev->fib_lock, flags);
  141. return fibptr;
  142. }
  143. dev->free_fib = fibptr->next;
  144. spin_unlock_irqrestore(&dev->fib_lock, flags);
  145. /*
  146. * Set the proper node type code and node byte size
  147. */
  148. fibptr->type = FSAFS_NTC_FIB_CONTEXT;
  149. fibptr->size = sizeof(struct fib);
  150. /*
  151. * Null out fields that depend on being zero at the start of
  152. * each I/O
  153. */
  154. fibptr->hw_fib->header.XferState = 0;
  155. fibptr->callback = NULL;
  156. fibptr->callback_data = NULL;
  157. return fibptr;
  158. }
  159. /**
  160. * aac_fib_free - free a fib
  161. * @fibptr: fib to free up
  162. *
  163. * Frees up a fib and places it on the appropriate queue
  164. * (either free or timed out)
  165. */
  166. void aac_fib_free(struct fib *fibptr)
  167. {
  168. unsigned long flags;
  169. spin_lock_irqsave(&fibptr->dev->fib_lock, flags);
  170. if (fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT) {
  171. aac_config.fib_timeouts++;
  172. fibptr->next = fibptr->dev->timeout_fib;
  173. fibptr->dev->timeout_fib = fibptr;
  174. } else {
  175. if (fibptr->hw_fib->header.XferState != 0) {
  176. printk(KERN_WARNING "aac_fib_free, XferState != 0, fibptr = 0x%p, XferState = 0x%x\n",
  177. (void*)fibptr,
  178. le32_to_cpu(fibptr->hw_fib->header.XferState));
  179. }
  180. fibptr->next = fibptr->dev->free_fib;
  181. fibptr->dev->free_fib = fibptr;
  182. }
  183. spin_unlock_irqrestore(&fibptr->dev->fib_lock, flags);
  184. }
  185. /**
  186. * aac_fib_init - initialise a fib
  187. * @fibptr: The fib to initialize
  188. *
  189. * Set up the generic fib fields ready for use
  190. */
  191. void aac_fib_init(struct fib *fibptr)
  192. {
  193. struct hw_fib *hw_fib = fibptr->hw_fib;
  194. hw_fib->header.StructType = FIB_MAGIC;
  195. hw_fib->header.Size = cpu_to_le16(fibptr->dev->max_fib_size);
  196. hw_fib->header.XferState = cpu_to_le32(HostOwned | FibInitialized | FibEmpty | FastResponseCapable);
  197. hw_fib->header.SenderFibAddress = 0; /* Filled in later if needed */
  198. hw_fib->header.ReceiverFibAddress = cpu_to_le32(fibptr->hw_fib_pa);
  199. hw_fib->header.SenderSize = cpu_to_le16(fibptr->dev->max_fib_size);
  200. }
  201. /**
  202. * fib_deallocate - deallocate a fib
  203. * @fibptr: fib to deallocate
  204. *
  205. * Will deallocate and return to the free pool the FIB pointed to by the
  206. * caller.
  207. */
  208. static void fib_dealloc(struct fib * fibptr)
  209. {
  210. struct hw_fib *hw_fib = fibptr->hw_fib;
  211. BUG_ON(hw_fib->header.StructType != FIB_MAGIC);
  212. hw_fib->header.XferState = 0;
  213. }
  214. /*
  215. * Commuication primitives define and support the queuing method we use to
  216. * support host to adapter commuication. All queue accesses happen through
  217. * these routines and are the only routines which have a knowledge of the
  218. * how these queues are implemented.
  219. */
  220. /**
  221. * aac_get_entry - get a queue entry
  222. * @dev: Adapter
  223. * @qid: Queue Number
  224. * @entry: Entry return
  225. * @index: Index return
  226. * @nonotify: notification control
  227. *
  228. * With a priority the routine returns a queue entry if the queue has free entries. If the queue
  229. * is full(no free entries) than no entry is returned and the function returns 0 otherwise 1 is
  230. * returned.
  231. */
  232. static int aac_get_entry (struct aac_dev * dev, u32 qid, struct aac_entry **entry, u32 * index, unsigned long *nonotify)
  233. {
  234. struct aac_queue * q;
  235. unsigned long idx;
  236. /*
  237. * All of the queues wrap when they reach the end, so we check
  238. * to see if they have reached the end and if they have we just
  239. * set the index back to zero. This is a wrap. You could or off
  240. * the high bits in all updates but this is a bit faster I think.
  241. */
  242. q = &dev->queues->queue[qid];
  243. idx = *index = le32_to_cpu(*(q->headers.producer));
  244. /* Interrupt Moderation, only interrupt for first two entries */
  245. if (idx != le32_to_cpu(*(q->headers.consumer))) {
  246. if (--idx == 0) {
  247. if (qid == AdapNormCmdQueue)
  248. idx = ADAP_NORM_CMD_ENTRIES;
  249. else
  250. idx = ADAP_NORM_RESP_ENTRIES;
  251. }
  252. if (idx != le32_to_cpu(*(q->headers.consumer)))
  253. *nonotify = 1;
  254. }
  255. if (qid == AdapNormCmdQueue) {
  256. if (*index >= ADAP_NORM_CMD_ENTRIES)
  257. *index = 0; /* Wrap to front of the Producer Queue. */
  258. } else {
  259. if (*index >= ADAP_NORM_RESP_ENTRIES)
  260. *index = 0; /* Wrap to front of the Producer Queue. */
  261. }
  262. if ((*index + 1) == le32_to_cpu(*(q->headers.consumer))) { /* Queue is full */
  263. printk(KERN_WARNING "Queue %d full, %u outstanding.\n",
  264. qid, q->numpending);
  265. return 0;
  266. } else {
  267. *entry = q->base + *index;
  268. return 1;
  269. }
  270. }
  271. /**
  272. * aac_queue_get - get the next free QE
  273. * @dev: Adapter
  274. * @index: Returned index
  275. * @priority: Priority of fib
  276. * @fib: Fib to associate with the queue entry
  277. * @wait: Wait if queue full
  278. * @fibptr: Driver fib object to go with fib
  279. * @nonotify: Don't notify the adapter
  280. *
  281. * Gets the next free QE off the requested priorty adapter command
  282. * queue and associates the Fib with the QE. The QE represented by
  283. * index is ready to insert on the queue when this routine returns
  284. * success.
  285. */
  286. static 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)
  287. {
  288. struct aac_entry * entry = NULL;
  289. int map = 0;
  290. if (qid == AdapNormCmdQueue) {
  291. /* if no entries wait for some if caller wants to */
  292. while (!aac_get_entry(dev, qid, &entry, index, nonotify))
  293. {
  294. printk(KERN_ERR "GetEntries failed\n");
  295. }
  296. /*
  297. * Setup queue entry with a command, status and fib mapped
  298. */
  299. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  300. map = 1;
  301. } else {
  302. while(!aac_get_entry(dev, qid, &entry, index, nonotify))
  303. {
  304. /* if no entries wait for some if caller wants to */
  305. }
  306. /*
  307. * Setup queue entry with command, status and fib mapped
  308. */
  309. entry->size = cpu_to_le32(le16_to_cpu(hw_fib->header.Size));
  310. entry->addr = hw_fib->header.SenderFibAddress;
  311. /* Restore adapters pointer to the FIB */
  312. hw_fib->header.ReceiverFibAddress = hw_fib->header.SenderFibAddress; /* Let the adapter now where to find its data */
  313. map = 0;
  314. }
  315. /*
  316. * If MapFib is true than we need to map the Fib and put pointers
  317. * in the queue entry.
  318. */
  319. if (map)
  320. entry->addr = cpu_to_le32(fibptr->hw_fib_pa);
  321. return 0;
  322. }
  323. /*
  324. * Define the highest level of host to adapter communication routines.
  325. * These routines will support host to adapter FS commuication. These
  326. * routines have no knowledge of the commuication method used. This level
  327. * sends and receives FIBs. This level has no knowledge of how these FIBs
  328. * get passed back and forth.
  329. */
  330. /**
  331. * aac_fib_send - send a fib to the adapter
  332. * @command: Command to send
  333. * @fibptr: The fib
  334. * @size: Size of fib data area
  335. * @priority: Priority of Fib
  336. * @wait: Async/sync select
  337. * @reply: True if a reply is wanted
  338. * @callback: Called with reply
  339. * @callback_data: Passed to callback
  340. *
  341. * Sends the requested FIB to the adapter and optionally will wait for a
  342. * response FIB. If the caller does not wish to wait for a response than
  343. * an event to wait on must be supplied. This event will be set when a
  344. * response FIB is received from the adapter.
  345. */
  346. int aac_fib_send(u16 command, struct fib *fibptr, unsigned long size,
  347. int priority, int wait, int reply, fib_callback callback,
  348. void *callback_data)
  349. {
  350. struct aac_dev * dev = fibptr->dev;
  351. struct hw_fib * hw_fib = fibptr->hw_fib;
  352. struct aac_queue * q;
  353. unsigned long flags = 0;
  354. unsigned long qflags;
  355. if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
  356. return -EBUSY;
  357. /*
  358. * There are 5 cases with the wait and reponse requested flags.
  359. * The only invalid cases are if the caller requests to wait and
  360. * does not request a response and if the caller does not want a
  361. * response and the Fib is not allocated from pool. If a response
  362. * is not requesed the Fib will just be deallocaed by the DPC
  363. * routine when the response comes back from the adapter. No
  364. * further processing will be done besides deleting the Fib. We
  365. * will have a debug mode where the adapter can notify the host
  366. * it had a problem and the host can log that fact.
  367. */
  368. if (wait && !reply) {
  369. return -EINVAL;
  370. } else if (!wait && reply) {
  371. hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
  372. FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
  373. } else if (!wait && !reply) {
  374. hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
  375. FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
  376. } else if (wait && reply) {
  377. hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
  378. FIB_COUNTER_INCREMENT(aac_config.NormalSent);
  379. }
  380. /*
  381. * Map the fib into 32bits by using the fib number
  382. */
  383. hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
  384. hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
  385. /*
  386. * Set FIB state to indicate where it came from and if we want a
  387. * response from the adapter. Also load the command from the
  388. * caller.
  389. *
  390. * Map the hw fib pointer as a 32bit value
  391. */
  392. hw_fib->header.Command = cpu_to_le16(command);
  393. hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
  394. fibptr->hw_fib->header.Flags = 0; /* 0 the flags field - internal only*/
  395. /*
  396. * Set the size of the Fib we want to send to the adapter
  397. */
  398. hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
  399. if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
  400. return -EMSGSIZE;
  401. }
  402. /*
  403. * Get a queue entry connect the FIB to it and send an notify
  404. * the adapter a command is ready.
  405. */
  406. hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
  407. /*
  408. * Fill in the Callback and CallbackContext if we are not
  409. * going to wait.
  410. */
  411. if (!wait) {
  412. fibptr->callback = callback;
  413. fibptr->callback_data = callback_data;
  414. }
  415. fibptr->done = 0;
  416. fibptr->flags = 0;
  417. FIB_COUNTER_INCREMENT(aac_config.FibsSent);
  418. dprintk((KERN_DEBUG "Fib contents:.\n"));
  419. dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
  420. dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
  421. dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
  422. dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib));
  423. dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
  424. dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
  425. q = &dev->queues->queue[AdapNormCmdQueue];
  426. if(wait)
  427. spin_lock_irqsave(&fibptr->event_lock, flags);
  428. spin_lock_irqsave(q->lock, qflags);
  429. if (dev->new_comm_interface) {
  430. unsigned long count = 10000000L; /* 50 seconds */
  431. q->numpending++;
  432. spin_unlock_irqrestore(q->lock, qflags);
  433. while (aac_adapter_send(fibptr) != 0) {
  434. if (--count == 0) {
  435. if (wait)
  436. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  437. spin_lock_irqsave(q->lock, qflags);
  438. q->numpending--;
  439. spin_unlock_irqrestore(q->lock, qflags);
  440. return -ETIMEDOUT;
  441. }
  442. udelay(5);
  443. }
  444. } else {
  445. u32 index;
  446. unsigned long nointr = 0;
  447. aac_queue_get( dev, &index, AdapNormCmdQueue, hw_fib, 1, fibptr, &nointr);
  448. q->numpending++;
  449. *(q->headers.producer) = cpu_to_le32(index + 1);
  450. spin_unlock_irqrestore(q->lock, qflags);
  451. dprintk((KERN_DEBUG "aac_fib_send: inserting a queue entry at index %d.\n",index));
  452. if (!(nointr & aac_config.irq_mod))
  453. aac_adapter_notify(dev, AdapNormCmdQueue);
  454. }
  455. /*
  456. * If the caller wanted us to wait for response wait now.
  457. */
  458. if (wait) {
  459. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  460. /* Only set for first known interruptable command */
  461. if (wait < 0) {
  462. /*
  463. * *VERY* Dangerous to time out a command, the
  464. * assumption is made that we have no hope of
  465. * functioning because an interrupt routing or other
  466. * hardware failure has occurred.
  467. */
  468. unsigned long count = 36000000L; /* 3 minutes */
  469. while (down_trylock(&fibptr->event_wait)) {
  470. if (--count == 0) {
  471. spin_lock_irqsave(q->lock, qflags);
  472. q->numpending--;
  473. spin_unlock_irqrestore(q->lock, qflags);
  474. if (wait == -1) {
  475. printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
  476. "Usually a result of a PCI interrupt routing problem;\n"
  477. "update mother board BIOS or consider utilizing one of\n"
  478. "the SAFE mode kernel options (acpi, apic etc)\n");
  479. }
  480. return -ETIMEDOUT;
  481. }
  482. udelay(5);
  483. }
  484. } else
  485. down(&fibptr->event_wait);
  486. BUG_ON(fibptr->done == 0);
  487. if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)){
  488. return -ETIMEDOUT;
  489. } else {
  490. return 0;
  491. }
  492. }
  493. /*
  494. * If the user does not want a response than return success otherwise
  495. * return pending
  496. */
  497. if (reply)
  498. return -EINPROGRESS;
  499. else
  500. return 0;
  501. }
  502. /**
  503. * aac_consumer_get - get the top of the queue
  504. * @dev: Adapter
  505. * @q: Queue
  506. * @entry: Return entry
  507. *
  508. * Will return a pointer to the entry on the top of the queue requested that
  509. * we are a consumer of, and return the address of the queue entry. It does
  510. * not change the state of the queue.
  511. */
  512. int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
  513. {
  514. u32 index;
  515. int status;
  516. if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
  517. status = 0;
  518. } else {
  519. /*
  520. * The consumer index must be wrapped if we have reached
  521. * the end of the queue, else we just use the entry
  522. * pointed to by the header index
  523. */
  524. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  525. index = 0;
  526. else
  527. index = le32_to_cpu(*q->headers.consumer);
  528. *entry = q->base + index;
  529. status = 1;
  530. }
  531. return(status);
  532. }
  533. /**
  534. * aac_consumer_free - free consumer entry
  535. * @dev: Adapter
  536. * @q: Queue
  537. * @qid: Queue ident
  538. *
  539. * Frees up the current top of the queue we are a consumer of. If the
  540. * queue was full notify the producer that the queue is no longer full.
  541. */
  542. void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
  543. {
  544. int wasfull = 0;
  545. u32 notify;
  546. if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
  547. wasfull = 1;
  548. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  549. *q->headers.consumer = cpu_to_le32(1);
  550. else
  551. *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
  552. if (wasfull) {
  553. switch (qid) {
  554. case HostNormCmdQueue:
  555. notify = HostNormCmdNotFull;
  556. break;
  557. case HostNormRespQueue:
  558. notify = HostNormRespNotFull;
  559. break;
  560. default:
  561. BUG();
  562. return;
  563. }
  564. aac_adapter_notify(dev, notify);
  565. }
  566. }
  567. /**
  568. * aac_fib_adapter_complete - complete adapter issued fib
  569. * @fibptr: fib to complete
  570. * @size: size of fib
  571. *
  572. * Will do all necessary work to complete a FIB that was sent from
  573. * the adapter.
  574. */
  575. int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
  576. {
  577. struct hw_fib * hw_fib = fibptr->hw_fib;
  578. struct aac_dev * dev = fibptr->dev;
  579. struct aac_queue * q;
  580. unsigned long nointr = 0;
  581. unsigned long qflags;
  582. if (hw_fib->header.XferState == 0) {
  583. if (dev->new_comm_interface)
  584. kfree (hw_fib);
  585. return 0;
  586. }
  587. /*
  588. * If we plan to do anything check the structure type first.
  589. */
  590. if ( hw_fib->header.StructType != FIB_MAGIC ) {
  591. if (dev->new_comm_interface)
  592. kfree (hw_fib);
  593. return -EINVAL;
  594. }
  595. /*
  596. * This block handles the case where the adapter had sent us a
  597. * command and we have finished processing the command. We
  598. * call completeFib when we are done processing the command
  599. * and want to send a response back to the adapter. This will
  600. * send the completed cdb to the adapter.
  601. */
  602. if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
  603. if (dev->new_comm_interface) {
  604. kfree (hw_fib);
  605. } else {
  606. u32 index;
  607. hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
  608. if (size) {
  609. size += sizeof(struct aac_fibhdr);
  610. if (size > le16_to_cpu(hw_fib->header.SenderSize))
  611. return -EMSGSIZE;
  612. hw_fib->header.Size = cpu_to_le16(size);
  613. }
  614. q = &dev->queues->queue[AdapNormRespQueue];
  615. spin_lock_irqsave(q->lock, qflags);
  616. aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
  617. *(q->headers.producer) = cpu_to_le32(index + 1);
  618. spin_unlock_irqrestore(q->lock, qflags);
  619. if (!(nointr & (int)aac_config.irq_mod))
  620. aac_adapter_notify(dev, AdapNormRespQueue);
  621. }
  622. }
  623. else
  624. {
  625. printk(KERN_WARNING "aac_fib_adapter_complete: Unknown xferstate detected.\n");
  626. BUG();
  627. }
  628. return 0;
  629. }
  630. /**
  631. * aac_fib_complete - fib completion handler
  632. * @fib: FIB to complete
  633. *
  634. * Will do all necessary work to complete a FIB.
  635. */
  636. int aac_fib_complete(struct fib *fibptr)
  637. {
  638. struct hw_fib * hw_fib = fibptr->hw_fib;
  639. /*
  640. * Check for a fib which has already been completed
  641. */
  642. if (hw_fib->header.XferState == 0)
  643. return 0;
  644. /*
  645. * If we plan to do anything check the structure type first.
  646. */
  647. if (hw_fib->header.StructType != FIB_MAGIC)
  648. return -EINVAL;
  649. /*
  650. * This block completes a cdb which orginated on the host and we
  651. * just need to deallocate the cdb or reinit it. At this point the
  652. * command is complete that we had sent to the adapter and this
  653. * cdb could be reused.
  654. */
  655. if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
  656. (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
  657. {
  658. fib_dealloc(fibptr);
  659. }
  660. else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
  661. {
  662. /*
  663. * This handles the case when the host has aborted the I/O
  664. * to the adapter because the adapter is not responding
  665. */
  666. fib_dealloc(fibptr);
  667. } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
  668. fib_dealloc(fibptr);
  669. } else {
  670. BUG();
  671. }
  672. return 0;
  673. }
  674. /**
  675. * aac_printf - handle printf from firmware
  676. * @dev: Adapter
  677. * @val: Message info
  678. *
  679. * Print a message passed to us by the controller firmware on the
  680. * Adaptec board
  681. */
  682. void aac_printf(struct aac_dev *dev, u32 val)
  683. {
  684. char *cp = dev->printfbuf;
  685. if (dev->printf_enabled)
  686. {
  687. int length = val & 0xffff;
  688. int level = (val >> 16) & 0xffff;
  689. /*
  690. * The size of the printfbuf is set in port.c
  691. * There is no variable or define for it
  692. */
  693. if (length > 255)
  694. length = 255;
  695. if (cp[length] != 0)
  696. cp[length] = 0;
  697. if (level == LOG_AAC_HIGH_ERROR)
  698. printk(KERN_WARNING "%s:%s", dev->name, cp);
  699. else
  700. printk(KERN_INFO "%s:%s", dev->name, cp);
  701. }
  702. memset(cp, 0, 256);
  703. }
  704. /**
  705. * aac_handle_aif - Handle a message from the firmware
  706. * @dev: Which adapter this fib is from
  707. * @fibptr: Pointer to fibptr from adapter
  708. *
  709. * This routine handles a driver notify fib from the adapter and
  710. * dispatches it to the appropriate routine for handling.
  711. */
  712. #define AIF_SNIFF_TIMEOUT (30*HZ)
  713. static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
  714. {
  715. struct hw_fib * hw_fib = fibptr->hw_fib;
  716. struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
  717. int busy;
  718. u32 container;
  719. struct scsi_device *device;
  720. enum {
  721. NOTHING,
  722. DELETE,
  723. ADD,
  724. CHANGE
  725. } device_config_needed;
  726. /* Sniff for container changes */
  727. if (!dev)
  728. return;
  729. container = (u32)-1;
  730. /*
  731. * We have set this up to try and minimize the number of
  732. * re-configures that take place. As a result of this when
  733. * certain AIF's come in we will set a flag waiting for another
  734. * type of AIF before setting the re-config flag.
  735. */
  736. switch (le32_to_cpu(aifcmd->command)) {
  737. case AifCmdDriverNotify:
  738. switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
  739. /*
  740. * Morph or Expand complete
  741. */
  742. case AifDenMorphComplete:
  743. case AifDenVolumeExtendComplete:
  744. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  745. if (container >= dev->maximum_num_containers)
  746. break;
  747. /*
  748. * Find the scsi_device associated with the SCSI
  749. * address. Make sure we have the right array, and if
  750. * so set the flag to initiate a new re-config once we
  751. * see an AifEnConfigChange AIF come through.
  752. */
  753. if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
  754. device = scsi_device_lookup(dev->scsi_host_ptr,
  755. CONTAINER_TO_CHANNEL(container),
  756. CONTAINER_TO_ID(container),
  757. CONTAINER_TO_LUN(container));
  758. if (device) {
  759. dev->fsa_dev[container].config_needed = CHANGE;
  760. dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
  761. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  762. scsi_device_put(device);
  763. }
  764. }
  765. }
  766. /*
  767. * If we are waiting on something and this happens to be
  768. * that thing then set the re-configure flag.
  769. */
  770. if (container != (u32)-1) {
  771. if (container >= dev->maximum_num_containers)
  772. break;
  773. if ((dev->fsa_dev[container].config_waiting_on ==
  774. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  775. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  776. dev->fsa_dev[container].config_waiting_on = 0;
  777. } else for (container = 0;
  778. container < dev->maximum_num_containers; ++container) {
  779. if ((dev->fsa_dev[container].config_waiting_on ==
  780. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  781. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  782. dev->fsa_dev[container].config_waiting_on = 0;
  783. }
  784. break;
  785. case AifCmdEventNotify:
  786. switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
  787. /*
  788. * Add an Array.
  789. */
  790. case AifEnAddContainer:
  791. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  792. if (container >= dev->maximum_num_containers)
  793. break;
  794. dev->fsa_dev[container].config_needed = ADD;
  795. dev->fsa_dev[container].config_waiting_on =
  796. AifEnConfigChange;
  797. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  798. break;
  799. /*
  800. * Delete an Array.
  801. */
  802. case AifEnDeleteContainer:
  803. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  804. if (container >= dev->maximum_num_containers)
  805. break;
  806. dev->fsa_dev[container].config_needed = DELETE;
  807. dev->fsa_dev[container].config_waiting_on =
  808. AifEnConfigChange;
  809. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  810. break;
  811. /*
  812. * Container change detected. If we currently are not
  813. * waiting on something else, setup to wait on a Config Change.
  814. */
  815. case AifEnContainerChange:
  816. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  817. if (container >= dev->maximum_num_containers)
  818. break;
  819. if (dev->fsa_dev[container].config_waiting_on &&
  820. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  821. break;
  822. dev->fsa_dev[container].config_needed = CHANGE;
  823. dev->fsa_dev[container].config_waiting_on =
  824. AifEnConfigChange;
  825. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  826. break;
  827. case AifEnConfigChange:
  828. break;
  829. }
  830. /*
  831. * If we are waiting on something and this happens to be
  832. * that thing then set the re-configure flag.
  833. */
  834. if (container != (u32)-1) {
  835. if (container >= dev->maximum_num_containers)
  836. break;
  837. if ((dev->fsa_dev[container].config_waiting_on ==
  838. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  839. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  840. dev->fsa_dev[container].config_waiting_on = 0;
  841. } else for (container = 0;
  842. container < dev->maximum_num_containers; ++container) {
  843. if ((dev->fsa_dev[container].config_waiting_on ==
  844. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  845. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  846. dev->fsa_dev[container].config_waiting_on = 0;
  847. }
  848. break;
  849. case AifCmdJobProgress:
  850. /*
  851. * These are job progress AIF's. When a Clear is being
  852. * done on a container it is initially created then hidden from
  853. * the OS. When the clear completes we don't get a config
  854. * change so we monitor the job status complete on a clear then
  855. * wait for a container change.
  856. */
  857. if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
  858. && ((((u32 *)aifcmd->data)[6] == ((u32 *)aifcmd->data)[5])
  859. || (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess)))) {
  860. for (container = 0;
  861. container < dev->maximum_num_containers;
  862. ++container) {
  863. /*
  864. * Stomp on all config sequencing for all
  865. * containers?
  866. */
  867. dev->fsa_dev[container].config_waiting_on =
  868. AifEnContainerChange;
  869. dev->fsa_dev[container].config_needed = ADD;
  870. dev->fsa_dev[container].config_waiting_stamp =
  871. jiffies;
  872. }
  873. }
  874. if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
  875. && (((u32 *)aifcmd->data)[6] == 0)
  876. && (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning))) {
  877. for (container = 0;
  878. container < dev->maximum_num_containers;
  879. ++container) {
  880. /*
  881. * Stomp on all config sequencing for all
  882. * containers?
  883. */
  884. dev->fsa_dev[container].config_waiting_on =
  885. AifEnContainerChange;
  886. dev->fsa_dev[container].config_needed = DELETE;
  887. dev->fsa_dev[container].config_waiting_stamp =
  888. jiffies;
  889. }
  890. }
  891. break;
  892. }
  893. device_config_needed = NOTHING;
  894. for (container = 0; container < dev->maximum_num_containers;
  895. ++container) {
  896. if ((dev->fsa_dev[container].config_waiting_on == 0) &&
  897. (dev->fsa_dev[container].config_needed != NOTHING) &&
  898. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
  899. device_config_needed =
  900. dev->fsa_dev[container].config_needed;
  901. dev->fsa_dev[container].config_needed = NOTHING;
  902. break;
  903. }
  904. }
  905. if (device_config_needed == NOTHING)
  906. return;
  907. /*
  908. * If we decided that a re-configuration needs to be done,
  909. * schedule it here on the way out the door, please close the door
  910. * behind you.
  911. */
  912. busy = 0;
  913. /*
  914. * Find the scsi_device associated with the SCSI address,
  915. * and mark it as changed, invalidating the cache. This deals
  916. * with changes to existing device IDs.
  917. */
  918. if (!dev || !dev->scsi_host_ptr)
  919. return;
  920. /*
  921. * force reload of disk info via aac_probe_container
  922. */
  923. if ((device_config_needed == CHANGE)
  924. && (dev->fsa_dev[container].valid == 1))
  925. dev->fsa_dev[container].valid = 2;
  926. if ((device_config_needed == CHANGE) ||
  927. (device_config_needed == ADD))
  928. aac_probe_container(dev, container);
  929. device = scsi_device_lookup(dev->scsi_host_ptr,
  930. CONTAINER_TO_CHANNEL(container),
  931. CONTAINER_TO_ID(container),
  932. CONTAINER_TO_LUN(container));
  933. if (device) {
  934. switch (device_config_needed) {
  935. case DELETE:
  936. scsi_remove_device(device);
  937. break;
  938. case CHANGE:
  939. if (!dev->fsa_dev[container].valid) {
  940. scsi_remove_device(device);
  941. break;
  942. }
  943. scsi_rescan_device(&device->sdev_gendev);
  944. default:
  945. break;
  946. }
  947. scsi_device_put(device);
  948. }
  949. if (device_config_needed == ADD) {
  950. scsi_add_device(dev->scsi_host_ptr,
  951. CONTAINER_TO_CHANNEL(container),
  952. CONTAINER_TO_ID(container),
  953. CONTAINER_TO_LUN(container));
  954. }
  955. }
  956. /**
  957. * aac_command_thread - command processing thread
  958. * @dev: Adapter to monitor
  959. *
  960. * Waits on the commandready event in it's queue. When the event gets set
  961. * it will pull FIBs off it's queue. It will continue to pull FIBs off
  962. * until the queue is empty. When the queue is empty it will wait for
  963. * more FIBs.
  964. */
  965. int aac_command_thread(void *data)
  966. {
  967. struct aac_dev *dev = data;
  968. struct hw_fib *hw_fib, *hw_newfib;
  969. struct fib *fib, *newfib;
  970. struct aac_fib_context *fibctx;
  971. unsigned long flags;
  972. DECLARE_WAITQUEUE(wait, current);
  973. /*
  974. * We can only have one thread per adapter for AIF's.
  975. */
  976. if (dev->aif_thread)
  977. return -EINVAL;
  978. /*
  979. * Let the DPC know it has a place to send the AIF's to.
  980. */
  981. dev->aif_thread = 1;
  982. add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  983. set_current_state(TASK_INTERRUPTIBLE);
  984. dprintk ((KERN_INFO "aac_command_thread start\n"));
  985. while(1)
  986. {
  987. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  988. while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
  989. struct list_head *entry;
  990. struct aac_aifcmd * aifcmd;
  991. set_current_state(TASK_RUNNING);
  992. entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
  993. list_del(entry);
  994. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  995. fib = list_entry(entry, struct fib, fiblink);
  996. /*
  997. * We will process the FIB here or pass it to a
  998. * worker thread that is TBD. We Really can't
  999. * do anything at this point since we don't have
  1000. * anything defined for this thread to do.
  1001. */
  1002. hw_fib = fib->hw_fib;
  1003. memset(fib, 0, sizeof(struct fib));
  1004. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1005. fib->size = sizeof( struct fib );
  1006. fib->hw_fib = hw_fib;
  1007. fib->data = hw_fib->data;
  1008. fib->dev = dev;
  1009. /*
  1010. * We only handle AifRequest fibs from the adapter.
  1011. */
  1012. aifcmd = (struct aac_aifcmd *) hw_fib->data;
  1013. if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
  1014. /* Handle Driver Notify Events */
  1015. aac_handle_aif(dev, fib);
  1016. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1017. aac_fib_adapter_complete(fib, (u16)sizeof(u32));
  1018. } else {
  1019. struct list_head *entry;
  1020. /* The u32 here is important and intended. We are using
  1021. 32bit wrapping time to fit the adapter field */
  1022. u32 time_now, time_last;
  1023. unsigned long flagv;
  1024. unsigned num;
  1025. struct hw_fib ** hw_fib_pool, ** hw_fib_p;
  1026. struct fib ** fib_pool, ** fib_p;
  1027. /* Sniff events */
  1028. if ((aifcmd->command ==
  1029. cpu_to_le32(AifCmdEventNotify)) ||
  1030. (aifcmd->command ==
  1031. cpu_to_le32(AifCmdJobProgress))) {
  1032. aac_handle_aif(dev, fib);
  1033. }
  1034. time_now = jiffies/HZ;
  1035. /*
  1036. * Warning: no sleep allowed while
  1037. * holding spinlock. We take the estimate
  1038. * and pre-allocate a set of fibs outside the
  1039. * lock.
  1040. */
  1041. num = le32_to_cpu(dev->init->AdapterFibsSize)
  1042. / sizeof(struct hw_fib); /* some extra */
  1043. spin_lock_irqsave(&dev->fib_lock, flagv);
  1044. entry = dev->fib_list.next;
  1045. while (entry != &dev->fib_list) {
  1046. entry = entry->next;
  1047. ++num;
  1048. }
  1049. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1050. hw_fib_pool = NULL;
  1051. fib_pool = NULL;
  1052. if (num
  1053. && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
  1054. && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
  1055. hw_fib_p = hw_fib_pool;
  1056. fib_p = fib_pool;
  1057. while (hw_fib_p < &hw_fib_pool[num]) {
  1058. if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
  1059. --hw_fib_p;
  1060. break;
  1061. }
  1062. if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
  1063. kfree(*(--hw_fib_p));
  1064. break;
  1065. }
  1066. }
  1067. if ((num = hw_fib_p - hw_fib_pool) == 0) {
  1068. kfree(fib_pool);
  1069. fib_pool = NULL;
  1070. kfree(hw_fib_pool);
  1071. hw_fib_pool = NULL;
  1072. }
  1073. } else {
  1074. kfree(hw_fib_pool);
  1075. hw_fib_pool = NULL;
  1076. }
  1077. spin_lock_irqsave(&dev->fib_lock, flagv);
  1078. entry = dev->fib_list.next;
  1079. /*
  1080. * For each Context that is on the
  1081. * fibctxList, make a copy of the
  1082. * fib, and then set the event to wake up the
  1083. * thread that is waiting for it.
  1084. */
  1085. hw_fib_p = hw_fib_pool;
  1086. fib_p = fib_pool;
  1087. while (entry != &dev->fib_list) {
  1088. /*
  1089. * Extract the fibctx
  1090. */
  1091. fibctx = list_entry(entry, struct aac_fib_context, next);
  1092. /*
  1093. * Check if the queue is getting
  1094. * backlogged
  1095. */
  1096. if (fibctx->count > 20)
  1097. {
  1098. /*
  1099. * It's *not* jiffies folks,
  1100. * but jiffies / HZ so do not
  1101. * panic ...
  1102. */
  1103. time_last = fibctx->jiffies;
  1104. /*
  1105. * Has it been > 2 minutes
  1106. * since the last read off
  1107. * the queue?
  1108. */
  1109. if ((time_now - time_last) > aif_timeout) {
  1110. entry = entry->next;
  1111. aac_close_fib_context(dev, fibctx);
  1112. continue;
  1113. }
  1114. }
  1115. /*
  1116. * Warning: no sleep allowed while
  1117. * holding spinlock
  1118. */
  1119. if (hw_fib_p < &hw_fib_pool[num]) {
  1120. hw_newfib = *hw_fib_p;
  1121. *(hw_fib_p++) = NULL;
  1122. newfib = *fib_p;
  1123. *(fib_p++) = NULL;
  1124. /*
  1125. * Make the copy of the FIB
  1126. */
  1127. memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
  1128. memcpy(newfib, fib, sizeof(struct fib));
  1129. newfib->hw_fib = hw_newfib;
  1130. /*
  1131. * Put the FIB onto the
  1132. * fibctx's fibs
  1133. */
  1134. list_add_tail(&newfib->fiblink, &fibctx->fib_list);
  1135. fibctx->count++;
  1136. /*
  1137. * Set the event to wake up the
  1138. * thread that is waiting.
  1139. */
  1140. up(&fibctx->wait_sem);
  1141. } else {
  1142. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1143. }
  1144. entry = entry->next;
  1145. }
  1146. /*
  1147. * Set the status of this FIB
  1148. */
  1149. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1150. aac_fib_adapter_complete(fib, sizeof(u32));
  1151. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1152. /* Free up the remaining resources */
  1153. hw_fib_p = hw_fib_pool;
  1154. fib_p = fib_pool;
  1155. while (hw_fib_p < &hw_fib_pool[num]) {
  1156. kfree(*hw_fib_p);
  1157. kfree(*fib_p);
  1158. ++fib_p;
  1159. ++hw_fib_p;
  1160. }
  1161. kfree(hw_fib_pool);
  1162. kfree(fib_pool);
  1163. }
  1164. kfree(fib);
  1165. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1166. }
  1167. /*
  1168. * There are no more AIF's
  1169. */
  1170. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1171. schedule();
  1172. if (kthread_should_stop())
  1173. break;
  1174. set_current_state(TASK_INTERRUPTIBLE);
  1175. }
  1176. if (dev->queues)
  1177. remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1178. dev->aif_thread = 0;
  1179. return 0;
  1180. }