commsup.c 37 KB

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