commsup.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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. if (!dev->queues)
  426. return -ENODEV;
  427. q = &dev->queues->queue[AdapNormCmdQueue];
  428. if(wait)
  429. spin_lock_irqsave(&fibptr->event_lock, flags);
  430. spin_lock_irqsave(q->lock, qflags);
  431. if (dev->new_comm_interface) {
  432. unsigned long count = 10000000L; /* 50 seconds */
  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. spin_unlock_irqrestore(q->lock, qflags);
  442. return -ETIMEDOUT;
  443. }
  444. udelay(5);
  445. }
  446. } else {
  447. u32 index;
  448. unsigned long nointr = 0;
  449. aac_queue_get( dev, &index, AdapNormCmdQueue, hw_fib, 1, fibptr, &nointr);
  450. q->numpending++;
  451. *(q->headers.producer) = cpu_to_le32(index + 1);
  452. spin_unlock_irqrestore(q->lock, qflags);
  453. dprintk((KERN_DEBUG "aac_fib_send: inserting a queue entry at index %d.\n",index));
  454. if (!(nointr & aac_config.irq_mod))
  455. aac_adapter_notify(dev, AdapNormCmdQueue);
  456. }
  457. /*
  458. * If the caller wanted us to wait for response wait now.
  459. */
  460. if (wait) {
  461. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  462. /* Only set for first known interruptable command */
  463. if (wait < 0) {
  464. /*
  465. * *VERY* Dangerous to time out a command, the
  466. * assumption is made that we have no hope of
  467. * functioning because an interrupt routing or other
  468. * hardware failure has occurred.
  469. */
  470. unsigned long count = 36000000L; /* 3 minutes */
  471. while (down_trylock(&fibptr->event_wait)) {
  472. if (--count == 0) {
  473. spin_lock_irqsave(q->lock, qflags);
  474. q->numpending--;
  475. spin_unlock_irqrestore(q->lock, qflags);
  476. if (wait == -1) {
  477. printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
  478. "Usually a result of a PCI interrupt routing problem;\n"
  479. "update mother board BIOS or consider utilizing one of\n"
  480. "the SAFE mode kernel options (acpi, apic etc)\n");
  481. }
  482. return -ETIMEDOUT;
  483. }
  484. udelay(5);
  485. }
  486. } else if (down_interruptible(&fibptr->event_wait)) {
  487. spin_lock_irqsave(&fibptr->event_lock, flags);
  488. if (fibptr->done == 0) {
  489. fibptr->done = 2; /* Tell interrupt we aborted */
  490. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  491. return -EINTR;
  492. }
  493. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  494. }
  495. BUG_ON(fibptr->done == 0);
  496. if((fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT)){
  497. return -ETIMEDOUT;
  498. } else {
  499. return 0;
  500. }
  501. }
  502. /*
  503. * If the user does not want a response than return success otherwise
  504. * return pending
  505. */
  506. if (reply)
  507. return -EINPROGRESS;
  508. else
  509. return 0;
  510. }
  511. /**
  512. * aac_consumer_get - get the top of the queue
  513. * @dev: Adapter
  514. * @q: Queue
  515. * @entry: Return entry
  516. *
  517. * Will return a pointer to the entry on the top of the queue requested that
  518. * we are a consumer of, and return the address of the queue entry. It does
  519. * not change the state of the queue.
  520. */
  521. int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
  522. {
  523. u32 index;
  524. int status;
  525. if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
  526. status = 0;
  527. } else {
  528. /*
  529. * The consumer index must be wrapped if we have reached
  530. * the end of the queue, else we just use the entry
  531. * pointed to by the header index
  532. */
  533. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  534. index = 0;
  535. else
  536. index = le32_to_cpu(*q->headers.consumer);
  537. *entry = q->base + index;
  538. status = 1;
  539. }
  540. return(status);
  541. }
  542. /**
  543. * aac_consumer_free - free consumer entry
  544. * @dev: Adapter
  545. * @q: Queue
  546. * @qid: Queue ident
  547. *
  548. * Frees up the current top of the queue we are a consumer of. If the
  549. * queue was full notify the producer that the queue is no longer full.
  550. */
  551. void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
  552. {
  553. int wasfull = 0;
  554. u32 notify;
  555. if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
  556. wasfull = 1;
  557. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  558. *q->headers.consumer = cpu_to_le32(1);
  559. else
  560. *q->headers.consumer = cpu_to_le32(le32_to_cpu(*q->headers.consumer)+1);
  561. if (wasfull) {
  562. switch (qid) {
  563. case HostNormCmdQueue:
  564. notify = HostNormCmdNotFull;
  565. break;
  566. case HostNormRespQueue:
  567. notify = HostNormRespNotFull;
  568. break;
  569. default:
  570. BUG();
  571. return;
  572. }
  573. aac_adapter_notify(dev, notify);
  574. }
  575. }
  576. /**
  577. * aac_fib_adapter_complete - complete adapter issued fib
  578. * @fibptr: fib to complete
  579. * @size: size of fib
  580. *
  581. * Will do all necessary work to complete a FIB that was sent from
  582. * the adapter.
  583. */
  584. int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
  585. {
  586. struct hw_fib * hw_fib = fibptr->hw_fib;
  587. struct aac_dev * dev = fibptr->dev;
  588. struct aac_queue * q;
  589. unsigned long nointr = 0;
  590. unsigned long qflags;
  591. if (hw_fib->header.XferState == 0) {
  592. if (dev->new_comm_interface)
  593. kfree (hw_fib);
  594. return 0;
  595. }
  596. /*
  597. * If we plan to do anything check the structure type first.
  598. */
  599. if ( hw_fib->header.StructType != FIB_MAGIC ) {
  600. if (dev->new_comm_interface)
  601. kfree (hw_fib);
  602. return -EINVAL;
  603. }
  604. /*
  605. * This block handles the case where the adapter had sent us a
  606. * command and we have finished processing the command. We
  607. * call completeFib when we are done processing the command
  608. * and want to send a response back to the adapter. This will
  609. * send the completed cdb to the adapter.
  610. */
  611. if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
  612. if (dev->new_comm_interface) {
  613. kfree (hw_fib);
  614. } else {
  615. u32 index;
  616. hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
  617. if (size) {
  618. size += sizeof(struct aac_fibhdr);
  619. if (size > le16_to_cpu(hw_fib->header.SenderSize))
  620. return -EMSGSIZE;
  621. hw_fib->header.Size = cpu_to_le16(size);
  622. }
  623. q = &dev->queues->queue[AdapNormRespQueue];
  624. spin_lock_irqsave(q->lock, qflags);
  625. aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
  626. *(q->headers.producer) = cpu_to_le32(index + 1);
  627. spin_unlock_irqrestore(q->lock, qflags);
  628. if (!(nointr & (int)aac_config.irq_mod))
  629. aac_adapter_notify(dev, AdapNormRespQueue);
  630. }
  631. }
  632. else
  633. {
  634. printk(KERN_WARNING "aac_fib_adapter_complete: Unknown xferstate detected.\n");
  635. BUG();
  636. }
  637. return 0;
  638. }
  639. /**
  640. * aac_fib_complete - fib completion handler
  641. * @fib: FIB to complete
  642. *
  643. * Will do all necessary work to complete a FIB.
  644. */
  645. int aac_fib_complete(struct fib *fibptr)
  646. {
  647. struct hw_fib * hw_fib = fibptr->hw_fib;
  648. /*
  649. * Check for a fib which has already been completed
  650. */
  651. if (hw_fib->header.XferState == 0)
  652. return 0;
  653. /*
  654. * If we plan to do anything check the structure type first.
  655. */
  656. if (hw_fib->header.StructType != FIB_MAGIC)
  657. return -EINVAL;
  658. /*
  659. * This block completes a cdb which orginated on the host and we
  660. * just need to deallocate the cdb or reinit it. At this point the
  661. * command is complete that we had sent to the adapter and this
  662. * cdb could be reused.
  663. */
  664. if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
  665. (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
  666. {
  667. fib_dealloc(fibptr);
  668. }
  669. else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
  670. {
  671. /*
  672. * This handles the case when the host has aborted the I/O
  673. * to the adapter because the adapter is not responding
  674. */
  675. fib_dealloc(fibptr);
  676. } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
  677. fib_dealloc(fibptr);
  678. } else {
  679. BUG();
  680. }
  681. return 0;
  682. }
  683. /**
  684. * aac_printf - handle printf from firmware
  685. * @dev: Adapter
  686. * @val: Message info
  687. *
  688. * Print a message passed to us by the controller firmware on the
  689. * Adaptec board
  690. */
  691. void aac_printf(struct aac_dev *dev, u32 val)
  692. {
  693. char *cp = dev->printfbuf;
  694. if (dev->printf_enabled)
  695. {
  696. int length = val & 0xffff;
  697. int level = (val >> 16) & 0xffff;
  698. /*
  699. * The size of the printfbuf is set in port.c
  700. * There is no variable or define for it
  701. */
  702. if (length > 255)
  703. length = 255;
  704. if (cp[length] != 0)
  705. cp[length] = 0;
  706. if (level == LOG_AAC_HIGH_ERROR)
  707. printk(KERN_WARNING "%s:%s", dev->name, cp);
  708. else
  709. printk(KERN_INFO "%s:%s", dev->name, cp);
  710. }
  711. memset(cp, 0, 256);
  712. }
  713. /**
  714. * aac_handle_aif - Handle a message from the firmware
  715. * @dev: Which adapter this fib is from
  716. * @fibptr: Pointer to fibptr from adapter
  717. *
  718. * This routine handles a driver notify fib from the adapter and
  719. * dispatches it to the appropriate routine for handling.
  720. */
  721. #define AIF_SNIFF_TIMEOUT (30*HZ)
  722. static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
  723. {
  724. struct hw_fib * hw_fib = fibptr->hw_fib;
  725. struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
  726. int busy;
  727. u32 container;
  728. struct scsi_device *device;
  729. enum {
  730. NOTHING,
  731. DELETE,
  732. ADD,
  733. CHANGE
  734. } device_config_needed;
  735. /* Sniff for container changes */
  736. if (!dev || !dev->fsa_dev)
  737. return;
  738. container = (u32)-1;
  739. /*
  740. * We have set this up to try and minimize the number of
  741. * re-configures that take place. As a result of this when
  742. * certain AIF's come in we will set a flag waiting for another
  743. * type of AIF before setting the re-config flag.
  744. */
  745. switch (le32_to_cpu(aifcmd->command)) {
  746. case AifCmdDriverNotify:
  747. switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
  748. /*
  749. * Morph or Expand complete
  750. */
  751. case AifDenMorphComplete:
  752. case AifDenVolumeExtendComplete:
  753. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  754. if (container >= dev->maximum_num_containers)
  755. break;
  756. /*
  757. * Find the scsi_device associated with the SCSI
  758. * address. Make sure we have the right array, and if
  759. * so set the flag to initiate a new re-config once we
  760. * see an AifEnConfigChange AIF come through.
  761. */
  762. if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
  763. device = scsi_device_lookup(dev->scsi_host_ptr,
  764. CONTAINER_TO_CHANNEL(container),
  765. CONTAINER_TO_ID(container),
  766. CONTAINER_TO_LUN(container));
  767. if (device) {
  768. dev->fsa_dev[container].config_needed = CHANGE;
  769. dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
  770. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  771. scsi_device_put(device);
  772. }
  773. }
  774. }
  775. /*
  776. * If we are waiting on something and this happens to be
  777. * that thing then set the re-configure flag.
  778. */
  779. if (container != (u32)-1) {
  780. if (container >= dev->maximum_num_containers)
  781. break;
  782. if ((dev->fsa_dev[container].config_waiting_on ==
  783. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  784. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  785. dev->fsa_dev[container].config_waiting_on = 0;
  786. } else for (container = 0;
  787. container < dev->maximum_num_containers; ++container) {
  788. if ((dev->fsa_dev[container].config_waiting_on ==
  789. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  790. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  791. dev->fsa_dev[container].config_waiting_on = 0;
  792. }
  793. break;
  794. case AifCmdEventNotify:
  795. switch (le32_to_cpu(((u32 *)aifcmd->data)[0])) {
  796. /*
  797. * Add an Array.
  798. */
  799. case AifEnAddContainer:
  800. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  801. if (container >= dev->maximum_num_containers)
  802. break;
  803. dev->fsa_dev[container].config_needed = ADD;
  804. dev->fsa_dev[container].config_waiting_on =
  805. AifEnConfigChange;
  806. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  807. break;
  808. /*
  809. * Delete an Array.
  810. */
  811. case AifEnDeleteContainer:
  812. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  813. if (container >= dev->maximum_num_containers)
  814. break;
  815. dev->fsa_dev[container].config_needed = DELETE;
  816. dev->fsa_dev[container].config_waiting_on =
  817. AifEnConfigChange;
  818. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  819. break;
  820. /*
  821. * Container change detected. If we currently are not
  822. * waiting on something else, setup to wait on a Config Change.
  823. */
  824. case AifEnContainerChange:
  825. container = le32_to_cpu(((u32 *)aifcmd->data)[1]);
  826. if (container >= dev->maximum_num_containers)
  827. break;
  828. if (dev->fsa_dev[container].config_waiting_on &&
  829. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  830. break;
  831. dev->fsa_dev[container].config_needed = CHANGE;
  832. dev->fsa_dev[container].config_waiting_on =
  833. AifEnConfigChange;
  834. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  835. break;
  836. case AifEnConfigChange:
  837. break;
  838. }
  839. /*
  840. * If we are waiting on something and this happens to be
  841. * that thing then set the re-configure flag.
  842. */
  843. if (container != (u32)-1) {
  844. if (container >= dev->maximum_num_containers)
  845. break;
  846. if ((dev->fsa_dev[container].config_waiting_on ==
  847. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  848. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  849. dev->fsa_dev[container].config_waiting_on = 0;
  850. } else for (container = 0;
  851. container < dev->maximum_num_containers; ++container) {
  852. if ((dev->fsa_dev[container].config_waiting_on ==
  853. le32_to_cpu(*(u32 *)aifcmd->data)) &&
  854. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  855. dev->fsa_dev[container].config_waiting_on = 0;
  856. }
  857. break;
  858. case AifCmdJobProgress:
  859. /*
  860. * These are job progress AIF's. When a Clear is being
  861. * done on a container it is initially created then hidden from
  862. * the OS. When the clear completes we don't get a config
  863. * change so we monitor the job status complete on a clear then
  864. * wait for a container change.
  865. */
  866. if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
  867. && ((((u32 *)aifcmd->data)[6] == ((u32 *)aifcmd->data)[5])
  868. || (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess)))) {
  869. for (container = 0;
  870. container < dev->maximum_num_containers;
  871. ++container) {
  872. /*
  873. * Stomp on all config sequencing for all
  874. * containers?
  875. */
  876. dev->fsa_dev[container].config_waiting_on =
  877. AifEnContainerChange;
  878. dev->fsa_dev[container].config_needed = ADD;
  879. dev->fsa_dev[container].config_waiting_stamp =
  880. jiffies;
  881. }
  882. }
  883. if ((((u32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero))
  884. && (((u32 *)aifcmd->data)[6] == 0)
  885. && (((u32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning))) {
  886. for (container = 0;
  887. container < dev->maximum_num_containers;
  888. ++container) {
  889. /*
  890. * Stomp on all config sequencing for all
  891. * containers?
  892. */
  893. dev->fsa_dev[container].config_waiting_on =
  894. AifEnContainerChange;
  895. dev->fsa_dev[container].config_needed = DELETE;
  896. dev->fsa_dev[container].config_waiting_stamp =
  897. jiffies;
  898. }
  899. }
  900. break;
  901. }
  902. device_config_needed = NOTHING;
  903. for (container = 0; container < dev->maximum_num_containers;
  904. ++container) {
  905. if ((dev->fsa_dev[container].config_waiting_on == 0) &&
  906. (dev->fsa_dev[container].config_needed != NOTHING) &&
  907. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
  908. device_config_needed =
  909. dev->fsa_dev[container].config_needed;
  910. dev->fsa_dev[container].config_needed = NOTHING;
  911. break;
  912. }
  913. }
  914. if (device_config_needed == NOTHING)
  915. return;
  916. /*
  917. * If we decided that a re-configuration needs to be done,
  918. * schedule it here on the way out the door, please close the door
  919. * behind you.
  920. */
  921. busy = 0;
  922. /*
  923. * Find the scsi_device associated with the SCSI address,
  924. * and mark it as changed, invalidating the cache. This deals
  925. * with changes to existing device IDs.
  926. */
  927. if (!dev || !dev->scsi_host_ptr)
  928. return;
  929. /*
  930. * force reload of disk info via aac_probe_container
  931. */
  932. if ((device_config_needed == CHANGE)
  933. && (dev->fsa_dev[container].valid == 1))
  934. dev->fsa_dev[container].valid = 2;
  935. if ((device_config_needed == CHANGE) ||
  936. (device_config_needed == ADD))
  937. aac_probe_container(dev, container);
  938. device = scsi_device_lookup(dev->scsi_host_ptr,
  939. CONTAINER_TO_CHANNEL(container),
  940. CONTAINER_TO_ID(container),
  941. CONTAINER_TO_LUN(container));
  942. if (device) {
  943. switch (device_config_needed) {
  944. case DELETE:
  945. scsi_remove_device(device);
  946. break;
  947. case CHANGE:
  948. if (!dev->fsa_dev[container].valid) {
  949. scsi_remove_device(device);
  950. break;
  951. }
  952. scsi_rescan_device(&device->sdev_gendev);
  953. default:
  954. break;
  955. }
  956. scsi_device_put(device);
  957. }
  958. if (device_config_needed == ADD) {
  959. scsi_add_device(dev->scsi_host_ptr,
  960. CONTAINER_TO_CHANNEL(container),
  961. CONTAINER_TO_ID(container),
  962. CONTAINER_TO_LUN(container));
  963. }
  964. }
  965. /**
  966. * aac_command_thread - command processing thread
  967. * @dev: Adapter to monitor
  968. *
  969. * Waits on the commandready event in it's queue. When the event gets set
  970. * it will pull FIBs off it's queue. It will continue to pull FIBs off
  971. * until the queue is empty. When the queue is empty it will wait for
  972. * more FIBs.
  973. */
  974. int aac_command_thread(void *data)
  975. {
  976. struct aac_dev *dev = data;
  977. struct hw_fib *hw_fib, *hw_newfib;
  978. struct fib *fib, *newfib;
  979. struct aac_fib_context *fibctx;
  980. unsigned long flags;
  981. DECLARE_WAITQUEUE(wait, current);
  982. /*
  983. * We can only have one thread per adapter for AIF's.
  984. */
  985. if (dev->aif_thread)
  986. return -EINVAL;
  987. /*
  988. * Let the DPC know it has a place to send the AIF's to.
  989. */
  990. dev->aif_thread = 1;
  991. add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  992. set_current_state(TASK_INTERRUPTIBLE);
  993. dprintk ((KERN_INFO "aac_command_thread start\n"));
  994. while(1)
  995. {
  996. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  997. while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
  998. struct list_head *entry;
  999. struct aac_aifcmd * aifcmd;
  1000. set_current_state(TASK_RUNNING);
  1001. entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
  1002. list_del(entry);
  1003. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1004. fib = list_entry(entry, struct fib, fiblink);
  1005. /*
  1006. * We will process the FIB here or pass it to a
  1007. * worker thread that is TBD. We Really can't
  1008. * do anything at this point since we don't have
  1009. * anything defined for this thread to do.
  1010. */
  1011. hw_fib = fib->hw_fib;
  1012. memset(fib, 0, sizeof(struct fib));
  1013. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1014. fib->size = sizeof( struct fib );
  1015. fib->hw_fib = hw_fib;
  1016. fib->data = hw_fib->data;
  1017. fib->dev = dev;
  1018. /*
  1019. * We only handle AifRequest fibs from the adapter.
  1020. */
  1021. aifcmd = (struct aac_aifcmd *) hw_fib->data;
  1022. if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
  1023. /* Handle Driver Notify Events */
  1024. aac_handle_aif(dev, fib);
  1025. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1026. aac_fib_adapter_complete(fib, (u16)sizeof(u32));
  1027. } else {
  1028. struct list_head *entry;
  1029. /* The u32 here is important and intended. We are using
  1030. 32bit wrapping time to fit the adapter field */
  1031. u32 time_now, time_last;
  1032. unsigned long flagv;
  1033. unsigned num;
  1034. struct hw_fib ** hw_fib_pool, ** hw_fib_p;
  1035. struct fib ** fib_pool, ** fib_p;
  1036. /* Sniff events */
  1037. if ((aifcmd->command ==
  1038. cpu_to_le32(AifCmdEventNotify)) ||
  1039. (aifcmd->command ==
  1040. cpu_to_le32(AifCmdJobProgress))) {
  1041. aac_handle_aif(dev, fib);
  1042. }
  1043. time_now = jiffies/HZ;
  1044. /*
  1045. * Warning: no sleep allowed while
  1046. * holding spinlock. We take the estimate
  1047. * and pre-allocate a set of fibs outside the
  1048. * lock.
  1049. */
  1050. num = le32_to_cpu(dev->init->AdapterFibsSize)
  1051. / sizeof(struct hw_fib); /* some extra */
  1052. spin_lock_irqsave(&dev->fib_lock, flagv);
  1053. entry = dev->fib_list.next;
  1054. while (entry != &dev->fib_list) {
  1055. entry = entry->next;
  1056. ++num;
  1057. }
  1058. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1059. hw_fib_pool = NULL;
  1060. fib_pool = NULL;
  1061. if (num
  1062. && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
  1063. && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
  1064. hw_fib_p = hw_fib_pool;
  1065. fib_p = fib_pool;
  1066. while (hw_fib_p < &hw_fib_pool[num]) {
  1067. if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
  1068. --hw_fib_p;
  1069. break;
  1070. }
  1071. if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
  1072. kfree(*(--hw_fib_p));
  1073. break;
  1074. }
  1075. }
  1076. if ((num = hw_fib_p - hw_fib_pool) == 0) {
  1077. kfree(fib_pool);
  1078. fib_pool = NULL;
  1079. kfree(hw_fib_pool);
  1080. hw_fib_pool = NULL;
  1081. }
  1082. } else {
  1083. kfree(hw_fib_pool);
  1084. hw_fib_pool = NULL;
  1085. }
  1086. spin_lock_irqsave(&dev->fib_lock, flagv);
  1087. entry = dev->fib_list.next;
  1088. /*
  1089. * For each Context that is on the
  1090. * fibctxList, make a copy of the
  1091. * fib, and then set the event to wake up the
  1092. * thread that is waiting for it.
  1093. */
  1094. hw_fib_p = hw_fib_pool;
  1095. fib_p = fib_pool;
  1096. while (entry != &dev->fib_list) {
  1097. /*
  1098. * Extract the fibctx
  1099. */
  1100. fibctx = list_entry(entry, struct aac_fib_context, next);
  1101. /*
  1102. * Check if the queue is getting
  1103. * backlogged
  1104. */
  1105. if (fibctx->count > 20)
  1106. {
  1107. /*
  1108. * It's *not* jiffies folks,
  1109. * but jiffies / HZ so do not
  1110. * panic ...
  1111. */
  1112. time_last = fibctx->jiffies;
  1113. /*
  1114. * Has it been > 2 minutes
  1115. * since the last read off
  1116. * the queue?
  1117. */
  1118. if ((time_now - time_last) > aif_timeout) {
  1119. entry = entry->next;
  1120. aac_close_fib_context(dev, fibctx);
  1121. continue;
  1122. }
  1123. }
  1124. /*
  1125. * Warning: no sleep allowed while
  1126. * holding spinlock
  1127. */
  1128. if (hw_fib_p < &hw_fib_pool[num]) {
  1129. hw_newfib = *hw_fib_p;
  1130. *(hw_fib_p++) = NULL;
  1131. newfib = *fib_p;
  1132. *(fib_p++) = NULL;
  1133. /*
  1134. * Make the copy of the FIB
  1135. */
  1136. memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
  1137. memcpy(newfib, fib, sizeof(struct fib));
  1138. newfib->hw_fib = hw_newfib;
  1139. /*
  1140. * Put the FIB onto the
  1141. * fibctx's fibs
  1142. */
  1143. list_add_tail(&newfib->fiblink, &fibctx->fib_list);
  1144. fibctx->count++;
  1145. /*
  1146. * Set the event to wake up the
  1147. * thread that is waiting.
  1148. */
  1149. up(&fibctx->wait_sem);
  1150. } else {
  1151. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1152. }
  1153. entry = entry->next;
  1154. }
  1155. /*
  1156. * Set the status of this FIB
  1157. */
  1158. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1159. aac_fib_adapter_complete(fib, sizeof(u32));
  1160. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1161. /* Free up the remaining resources */
  1162. hw_fib_p = hw_fib_pool;
  1163. fib_p = fib_pool;
  1164. while (hw_fib_p < &hw_fib_pool[num]) {
  1165. kfree(*hw_fib_p);
  1166. kfree(*fib_p);
  1167. ++fib_p;
  1168. ++hw_fib_p;
  1169. }
  1170. kfree(hw_fib_pool);
  1171. kfree(fib_pool);
  1172. }
  1173. kfree(fib);
  1174. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1175. }
  1176. /*
  1177. * There are no more AIF's
  1178. */
  1179. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1180. schedule();
  1181. if (kthread_should_stop())
  1182. break;
  1183. set_current_state(TASK_INTERRUPTIBLE);
  1184. }
  1185. if (dev->queues)
  1186. remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1187. dev->aif_thread = 0;
  1188. return 0;
  1189. }