commsup.c 52 KB

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