commsup.c 37 KB

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