commsup.c 53 KB

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