commsup.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925
  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. unsigned long sflags = 0;
  382. if (!(hw_fib->header.XferState & cpu_to_le32(HostOwned)))
  383. return -EBUSY;
  384. /*
  385. * There are 5 cases with the wait and response requested flags.
  386. * The only invalid cases are if the caller requests to wait and
  387. * does not request a response and if the caller does not want a
  388. * response and the Fib is not allocated from pool. If a response
  389. * is not requesed the Fib will just be deallocaed by the DPC
  390. * routine when the response comes back from the adapter. No
  391. * further processing will be done besides deleting the Fib. We
  392. * will have a debug mode where the adapter can notify the host
  393. * it had a problem and the host can log that fact.
  394. */
  395. fibptr->flags = 0;
  396. if (wait && !reply) {
  397. return -EINVAL;
  398. } else if (!wait && reply) {
  399. hw_fib->header.XferState |= cpu_to_le32(Async | ResponseExpected);
  400. FIB_COUNTER_INCREMENT(aac_config.AsyncSent);
  401. } else if (!wait && !reply) {
  402. hw_fib->header.XferState |= cpu_to_le32(NoResponseExpected);
  403. FIB_COUNTER_INCREMENT(aac_config.NoResponseSent);
  404. } else if (wait && reply) {
  405. hw_fib->header.XferState |= cpu_to_le32(ResponseExpected);
  406. FIB_COUNTER_INCREMENT(aac_config.NormalSent);
  407. }
  408. /*
  409. * Map the fib into 32bits by using the fib number
  410. */
  411. hw_fib->header.SenderFibAddress = cpu_to_le32(((u32)(fibptr - dev->fibs)) << 2);
  412. hw_fib->header.SenderData = (u32)(fibptr - dev->fibs);
  413. /*
  414. * Set FIB state to indicate where it came from and if we want a
  415. * response from the adapter. Also load the command from the
  416. * caller.
  417. *
  418. * Map the hw fib pointer as a 32bit value
  419. */
  420. hw_fib->header.Command = cpu_to_le16(command);
  421. hw_fib->header.XferState |= cpu_to_le32(SentFromHost);
  422. fibptr->hw_fib_va->header.Flags = 0; /* 0 the flags field - internal only*/
  423. /*
  424. * Set the size of the Fib we want to send to the adapter
  425. */
  426. hw_fib->header.Size = cpu_to_le16(sizeof(struct aac_fibhdr) + size);
  427. if (le16_to_cpu(hw_fib->header.Size) > le16_to_cpu(hw_fib->header.SenderSize)) {
  428. return -EMSGSIZE;
  429. }
  430. /*
  431. * Get a queue entry connect the FIB to it and send an notify
  432. * the adapter a command is ready.
  433. */
  434. hw_fib->header.XferState |= cpu_to_le32(NormalPriority);
  435. /*
  436. * Fill in the Callback and CallbackContext if we are not
  437. * going to wait.
  438. */
  439. if (!wait) {
  440. fibptr->callback = callback;
  441. fibptr->callback_data = callback_data;
  442. fibptr->flags = FIB_CONTEXT_FLAG;
  443. }
  444. fibptr->done = 0;
  445. FIB_COUNTER_INCREMENT(aac_config.FibsSent);
  446. dprintk((KERN_DEBUG "Fib contents:.\n"));
  447. dprintk((KERN_DEBUG " Command = %d.\n", le32_to_cpu(hw_fib->header.Command)));
  448. dprintk((KERN_DEBUG " SubCommand = %d.\n", le32_to_cpu(((struct aac_query_mount *)fib_data(fibptr))->command)));
  449. dprintk((KERN_DEBUG " XferState = %x.\n", le32_to_cpu(hw_fib->header.XferState)));
  450. dprintk((KERN_DEBUG " hw_fib va being sent=%p\n",fibptr->hw_fib_va));
  451. dprintk((KERN_DEBUG " hw_fib pa being sent=%lx\n",(ulong)fibptr->hw_fib_pa));
  452. dprintk((KERN_DEBUG " fib being sent=%p\n",fibptr));
  453. if (!dev->queues)
  454. return -EBUSY;
  455. if (wait) {
  456. spin_lock_irqsave(&dev->manage_lock, mflags);
  457. if (dev->management_fib_count >= AAC_NUM_MGT_FIB) {
  458. printk(KERN_INFO "No management Fibs Available:%d\n",
  459. dev->management_fib_count);
  460. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  461. return -EBUSY;
  462. }
  463. dev->management_fib_count++;
  464. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  465. spin_lock_irqsave(&fibptr->event_lock, flags);
  466. }
  467. if (dev->sync_mode) {
  468. if (wait)
  469. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  470. spin_lock_irqsave(&dev->sync_lock, sflags);
  471. if (dev->sync_fib) {
  472. list_add_tail(&fibptr->fiblink, &dev->sync_fib_list);
  473. spin_unlock_irqrestore(&dev->sync_lock, sflags);
  474. } else {
  475. dev->sync_fib = fibptr;
  476. spin_unlock_irqrestore(&dev->sync_lock, sflags);
  477. aac_adapter_sync_cmd(dev, SEND_SYNCHRONOUS_FIB,
  478. (u32)fibptr->hw_fib_pa, 0, 0, 0, 0, 0,
  479. NULL, NULL, NULL, NULL, NULL);
  480. }
  481. if (wait) {
  482. fibptr->flags |= FIB_CONTEXT_FLAG_WAIT;
  483. if (down_interruptible(&fibptr->event_wait)) {
  484. fibptr->flags &= ~FIB_CONTEXT_FLAG_WAIT;
  485. return -EFAULT;
  486. }
  487. return 0;
  488. }
  489. return -EINPROGRESS;
  490. }
  491. if (aac_adapter_deliver(fibptr) != 0) {
  492. printk(KERN_ERR "aac_fib_send: returned -EBUSY\n");
  493. if (wait) {
  494. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  495. spin_lock_irqsave(&dev->manage_lock, mflags);
  496. dev->management_fib_count--;
  497. spin_unlock_irqrestore(&dev->manage_lock, mflags);
  498. }
  499. return -EBUSY;
  500. }
  501. /*
  502. * If the caller wanted us to wait for response wait now.
  503. */
  504. if (wait) {
  505. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  506. /* Only set for first known interruptable command */
  507. if (wait < 0) {
  508. /*
  509. * *VERY* Dangerous to time out a command, the
  510. * assumption is made that we have no hope of
  511. * functioning because an interrupt routing or other
  512. * hardware failure has occurred.
  513. */
  514. unsigned long timeout = jiffies + (180 * HZ); /* 3 minutes */
  515. while (down_trylock(&fibptr->event_wait)) {
  516. int blink;
  517. if (time_is_before_eq_jiffies(timeout)) {
  518. struct aac_queue * q = &dev->queues->queue[AdapNormCmdQueue];
  519. spin_lock_irqsave(q->lock, qflags);
  520. q->numpending--;
  521. spin_unlock_irqrestore(q->lock, qflags);
  522. if (wait == -1) {
  523. printk(KERN_ERR "aacraid: aac_fib_send: first asynchronous command timed out.\n"
  524. "Usually a result of a PCI interrupt routing problem;\n"
  525. "update mother board BIOS or consider utilizing one of\n"
  526. "the SAFE mode kernel options (acpi, apic etc)\n");
  527. }
  528. return -ETIMEDOUT;
  529. }
  530. if ((blink = aac_adapter_check_health(dev)) > 0) {
  531. if (wait == -1) {
  532. printk(KERN_ERR "aacraid: aac_fib_send: adapter blinkLED 0x%x.\n"
  533. "Usually a result of a serious unrecoverable hardware problem\n",
  534. blink);
  535. }
  536. return -EFAULT;
  537. }
  538. /* We used to udelay() here but that absorbed
  539. * a CPU when a timeout occured. Not very
  540. * useful. */
  541. cpu_relax();
  542. }
  543. } else if (down_interruptible(&fibptr->event_wait)) {
  544. /* Do nothing ... satisfy
  545. * down_interruptible must_check */
  546. }
  547. spin_lock_irqsave(&fibptr->event_lock, flags);
  548. if (fibptr->done == 0) {
  549. fibptr->done = 2; /* Tell interrupt we aborted */
  550. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  551. return -ERESTARTSYS;
  552. }
  553. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  554. BUG_ON(fibptr->done == 0);
  555. if(unlikely(fibptr->flags & FIB_CONTEXT_FLAG_TIMED_OUT))
  556. return -ETIMEDOUT;
  557. return 0;
  558. }
  559. /*
  560. * If the user does not want a response than return success otherwise
  561. * return pending
  562. */
  563. if (reply)
  564. return -EINPROGRESS;
  565. else
  566. return 0;
  567. }
  568. /**
  569. * aac_consumer_get - get the top of the queue
  570. * @dev: Adapter
  571. * @q: Queue
  572. * @entry: Return entry
  573. *
  574. * Will return a pointer to the entry on the top of the queue requested that
  575. * we are a consumer of, and return the address of the queue entry. It does
  576. * not change the state of the queue.
  577. */
  578. int aac_consumer_get(struct aac_dev * dev, struct aac_queue * q, struct aac_entry **entry)
  579. {
  580. u32 index;
  581. int status;
  582. if (le32_to_cpu(*q->headers.producer) == le32_to_cpu(*q->headers.consumer)) {
  583. status = 0;
  584. } else {
  585. /*
  586. * The consumer index must be wrapped if we have reached
  587. * the end of the queue, else we just use the entry
  588. * pointed to by the header index
  589. */
  590. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  591. index = 0;
  592. else
  593. index = le32_to_cpu(*q->headers.consumer);
  594. *entry = q->base + index;
  595. status = 1;
  596. }
  597. return(status);
  598. }
  599. /**
  600. * aac_consumer_free - free consumer entry
  601. * @dev: Adapter
  602. * @q: Queue
  603. * @qid: Queue ident
  604. *
  605. * Frees up the current top of the queue we are a consumer of. If the
  606. * queue was full notify the producer that the queue is no longer full.
  607. */
  608. void aac_consumer_free(struct aac_dev * dev, struct aac_queue *q, u32 qid)
  609. {
  610. int wasfull = 0;
  611. u32 notify;
  612. if ((le32_to_cpu(*q->headers.producer)+1) == le32_to_cpu(*q->headers.consumer))
  613. wasfull = 1;
  614. if (le32_to_cpu(*q->headers.consumer) >= q->entries)
  615. *q->headers.consumer = cpu_to_le32(1);
  616. else
  617. le32_add_cpu(q->headers.consumer, 1);
  618. if (wasfull) {
  619. switch (qid) {
  620. case HostNormCmdQueue:
  621. notify = HostNormCmdNotFull;
  622. break;
  623. case HostNormRespQueue:
  624. notify = HostNormRespNotFull;
  625. break;
  626. default:
  627. BUG();
  628. return;
  629. }
  630. aac_adapter_notify(dev, notify);
  631. }
  632. }
  633. /**
  634. * aac_fib_adapter_complete - complete adapter issued fib
  635. * @fibptr: fib to complete
  636. * @size: size of fib
  637. *
  638. * Will do all necessary work to complete a FIB that was sent from
  639. * the adapter.
  640. */
  641. int aac_fib_adapter_complete(struct fib *fibptr, unsigned short size)
  642. {
  643. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  644. struct aac_dev * dev = fibptr->dev;
  645. struct aac_queue * q;
  646. unsigned long nointr = 0;
  647. unsigned long qflags;
  648. if (dev->comm_interface == AAC_COMM_MESSAGE_TYPE1) {
  649. kfree(hw_fib);
  650. return 0;
  651. }
  652. if (hw_fib->header.XferState == 0) {
  653. if (dev->comm_interface == AAC_COMM_MESSAGE)
  654. kfree(hw_fib);
  655. return 0;
  656. }
  657. /*
  658. * If we plan to do anything check the structure type first.
  659. */
  660. if (hw_fib->header.StructType != FIB_MAGIC) {
  661. if (dev->comm_interface == AAC_COMM_MESSAGE)
  662. kfree(hw_fib);
  663. return -EINVAL;
  664. }
  665. /*
  666. * This block handles the case where the adapter had sent us a
  667. * command and we have finished processing the command. We
  668. * call completeFib when we are done processing the command
  669. * and want to send a response back to the adapter. This will
  670. * send the completed cdb to the adapter.
  671. */
  672. if (hw_fib->header.XferState & cpu_to_le32(SentFromAdapter)) {
  673. if (dev->comm_interface == AAC_COMM_MESSAGE) {
  674. kfree (hw_fib);
  675. } else {
  676. u32 index;
  677. hw_fib->header.XferState |= cpu_to_le32(HostProcessed);
  678. if (size) {
  679. size += sizeof(struct aac_fibhdr);
  680. if (size > le16_to_cpu(hw_fib->header.SenderSize))
  681. return -EMSGSIZE;
  682. hw_fib->header.Size = cpu_to_le16(size);
  683. }
  684. q = &dev->queues->queue[AdapNormRespQueue];
  685. spin_lock_irqsave(q->lock, qflags);
  686. aac_queue_get(dev, &index, AdapNormRespQueue, hw_fib, 1, NULL, &nointr);
  687. *(q->headers.producer) = cpu_to_le32(index + 1);
  688. spin_unlock_irqrestore(q->lock, qflags);
  689. if (!(nointr & (int)aac_config.irq_mod))
  690. aac_adapter_notify(dev, AdapNormRespQueue);
  691. }
  692. } else {
  693. printk(KERN_WARNING "aac_fib_adapter_complete: "
  694. "Unknown xferstate detected.\n");
  695. BUG();
  696. }
  697. return 0;
  698. }
  699. /**
  700. * aac_fib_complete - fib completion handler
  701. * @fib: FIB to complete
  702. *
  703. * Will do all necessary work to complete a FIB.
  704. */
  705. int aac_fib_complete(struct fib *fibptr)
  706. {
  707. unsigned long flags;
  708. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  709. /*
  710. * Check for a fib which has already been completed
  711. */
  712. if (hw_fib->header.XferState == 0)
  713. return 0;
  714. /*
  715. * If we plan to do anything check the structure type first.
  716. */
  717. if (hw_fib->header.StructType != FIB_MAGIC)
  718. return -EINVAL;
  719. /*
  720. * This block completes a cdb which orginated on the host and we
  721. * just need to deallocate the cdb or reinit it. At this point the
  722. * command is complete that we had sent to the adapter and this
  723. * cdb could be reused.
  724. */
  725. spin_lock_irqsave(&fibptr->event_lock, flags);
  726. if (fibptr->done == 2) {
  727. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  728. return 0;
  729. }
  730. spin_unlock_irqrestore(&fibptr->event_lock, flags);
  731. if((hw_fib->header.XferState & cpu_to_le32(SentFromHost)) &&
  732. (hw_fib->header.XferState & cpu_to_le32(AdapterProcessed)))
  733. {
  734. fib_dealloc(fibptr);
  735. }
  736. else if(hw_fib->header.XferState & cpu_to_le32(SentFromHost))
  737. {
  738. /*
  739. * This handles the case when the host has aborted the I/O
  740. * to the adapter because the adapter is not responding
  741. */
  742. fib_dealloc(fibptr);
  743. } else if(hw_fib->header.XferState & cpu_to_le32(HostOwned)) {
  744. fib_dealloc(fibptr);
  745. } else {
  746. BUG();
  747. }
  748. return 0;
  749. }
  750. /**
  751. * aac_printf - handle printf from firmware
  752. * @dev: Adapter
  753. * @val: Message info
  754. *
  755. * Print a message passed to us by the controller firmware on the
  756. * Adaptec board
  757. */
  758. void aac_printf(struct aac_dev *dev, u32 val)
  759. {
  760. char *cp = dev->printfbuf;
  761. if (dev->printf_enabled)
  762. {
  763. int length = val & 0xffff;
  764. int level = (val >> 16) & 0xffff;
  765. /*
  766. * The size of the printfbuf is set in port.c
  767. * There is no variable or define for it
  768. */
  769. if (length > 255)
  770. length = 255;
  771. if (cp[length] != 0)
  772. cp[length] = 0;
  773. if (level == LOG_AAC_HIGH_ERROR)
  774. printk(KERN_WARNING "%s:%s", dev->name, cp);
  775. else
  776. printk(KERN_INFO "%s:%s", dev->name, cp);
  777. }
  778. memset(cp, 0, 256);
  779. }
  780. /**
  781. * aac_handle_aif - Handle a message from the firmware
  782. * @dev: Which adapter this fib is from
  783. * @fibptr: Pointer to fibptr from adapter
  784. *
  785. * This routine handles a driver notify fib from the adapter and
  786. * dispatches it to the appropriate routine for handling.
  787. */
  788. #define AIF_SNIFF_TIMEOUT (30*HZ)
  789. static void aac_handle_aif(struct aac_dev * dev, struct fib * fibptr)
  790. {
  791. struct hw_fib * hw_fib = fibptr->hw_fib_va;
  792. struct aac_aifcmd * aifcmd = (struct aac_aifcmd *)hw_fib->data;
  793. u32 channel, id, lun, container;
  794. struct scsi_device *device;
  795. enum {
  796. NOTHING,
  797. DELETE,
  798. ADD,
  799. CHANGE
  800. } device_config_needed = NOTHING;
  801. /* Sniff for container changes */
  802. if (!dev || !dev->fsa_dev)
  803. return;
  804. container = channel = id = lun = (u32)-1;
  805. /*
  806. * We have set this up to try and minimize the number of
  807. * re-configures that take place. As a result of this when
  808. * certain AIF's come in we will set a flag waiting for another
  809. * type of AIF before setting the re-config flag.
  810. */
  811. switch (le32_to_cpu(aifcmd->command)) {
  812. case AifCmdDriverNotify:
  813. switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
  814. /*
  815. * Morph or Expand complete
  816. */
  817. case AifDenMorphComplete:
  818. case AifDenVolumeExtendComplete:
  819. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  820. if (container >= dev->maximum_num_containers)
  821. break;
  822. /*
  823. * Find the scsi_device associated with the SCSI
  824. * address. Make sure we have the right array, and if
  825. * so set the flag to initiate a new re-config once we
  826. * see an AifEnConfigChange AIF come through.
  827. */
  828. if ((dev != NULL) && (dev->scsi_host_ptr != NULL)) {
  829. device = scsi_device_lookup(dev->scsi_host_ptr,
  830. CONTAINER_TO_CHANNEL(container),
  831. CONTAINER_TO_ID(container),
  832. CONTAINER_TO_LUN(container));
  833. if (device) {
  834. dev->fsa_dev[container].config_needed = CHANGE;
  835. dev->fsa_dev[container].config_waiting_on = AifEnConfigChange;
  836. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  837. scsi_device_put(device);
  838. }
  839. }
  840. }
  841. /*
  842. * If we are waiting on something and this happens to be
  843. * that thing then set the re-configure flag.
  844. */
  845. if (container != (u32)-1) {
  846. if (container >= dev->maximum_num_containers)
  847. break;
  848. if ((dev->fsa_dev[container].config_waiting_on ==
  849. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  850. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  851. dev->fsa_dev[container].config_waiting_on = 0;
  852. } else for (container = 0;
  853. container < dev->maximum_num_containers; ++container) {
  854. if ((dev->fsa_dev[container].config_waiting_on ==
  855. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  856. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  857. dev->fsa_dev[container].config_waiting_on = 0;
  858. }
  859. break;
  860. case AifCmdEventNotify:
  861. switch (le32_to_cpu(((__le32 *)aifcmd->data)[0])) {
  862. case AifEnBatteryEvent:
  863. dev->cache_protected =
  864. (((__le32 *)aifcmd->data)[1] == cpu_to_le32(3));
  865. break;
  866. /*
  867. * Add an Array.
  868. */
  869. case AifEnAddContainer:
  870. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  871. if (container >= dev->maximum_num_containers)
  872. break;
  873. dev->fsa_dev[container].config_needed = ADD;
  874. dev->fsa_dev[container].config_waiting_on =
  875. AifEnConfigChange;
  876. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  877. break;
  878. /*
  879. * Delete an Array.
  880. */
  881. case AifEnDeleteContainer:
  882. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  883. if (container >= dev->maximum_num_containers)
  884. break;
  885. dev->fsa_dev[container].config_needed = DELETE;
  886. dev->fsa_dev[container].config_waiting_on =
  887. AifEnConfigChange;
  888. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  889. break;
  890. /*
  891. * Container change detected. If we currently are not
  892. * waiting on something else, setup to wait on a Config Change.
  893. */
  894. case AifEnContainerChange:
  895. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  896. if (container >= dev->maximum_num_containers)
  897. break;
  898. if (dev->fsa_dev[container].config_waiting_on &&
  899. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  900. break;
  901. dev->fsa_dev[container].config_needed = CHANGE;
  902. dev->fsa_dev[container].config_waiting_on =
  903. AifEnConfigChange;
  904. dev->fsa_dev[container].config_waiting_stamp = jiffies;
  905. break;
  906. case AifEnConfigChange:
  907. break;
  908. case AifEnAddJBOD:
  909. case AifEnDeleteJBOD:
  910. container = le32_to_cpu(((__le32 *)aifcmd->data)[1]);
  911. if ((container >> 28)) {
  912. container = (u32)-1;
  913. break;
  914. }
  915. channel = (container >> 24) & 0xF;
  916. if (channel >= dev->maximum_num_channels) {
  917. container = (u32)-1;
  918. break;
  919. }
  920. id = container & 0xFFFF;
  921. if (id >= dev->maximum_num_physicals) {
  922. container = (u32)-1;
  923. break;
  924. }
  925. lun = (container >> 16) & 0xFF;
  926. container = (u32)-1;
  927. channel = aac_phys_to_logical(channel);
  928. device_config_needed =
  929. (((__le32 *)aifcmd->data)[0] ==
  930. cpu_to_le32(AifEnAddJBOD)) ? ADD : DELETE;
  931. if (device_config_needed == ADD) {
  932. device = scsi_device_lookup(dev->scsi_host_ptr,
  933. channel,
  934. id,
  935. lun);
  936. if (device) {
  937. scsi_remove_device(device);
  938. scsi_device_put(device);
  939. }
  940. }
  941. break;
  942. case AifEnEnclosureManagement:
  943. /*
  944. * If in JBOD mode, automatic exposure of new
  945. * physical target to be suppressed until configured.
  946. */
  947. if (dev->jbod)
  948. break;
  949. switch (le32_to_cpu(((__le32 *)aifcmd->data)[3])) {
  950. case EM_DRIVE_INSERTION:
  951. case EM_DRIVE_REMOVAL:
  952. container = le32_to_cpu(
  953. ((__le32 *)aifcmd->data)[2]);
  954. if ((container >> 28)) {
  955. container = (u32)-1;
  956. break;
  957. }
  958. channel = (container >> 24) & 0xF;
  959. if (channel >= dev->maximum_num_channels) {
  960. container = (u32)-1;
  961. break;
  962. }
  963. id = container & 0xFFFF;
  964. lun = (container >> 16) & 0xFF;
  965. container = (u32)-1;
  966. if (id >= dev->maximum_num_physicals) {
  967. /* legacy dev_t ? */
  968. if ((0x2000 <= id) || lun || channel ||
  969. ((channel = (id >> 7) & 0x3F) >=
  970. dev->maximum_num_channels))
  971. break;
  972. lun = (id >> 4) & 7;
  973. id &= 0xF;
  974. }
  975. channel = aac_phys_to_logical(channel);
  976. device_config_needed =
  977. (((__le32 *)aifcmd->data)[3]
  978. == cpu_to_le32(EM_DRIVE_INSERTION)) ?
  979. ADD : DELETE;
  980. break;
  981. }
  982. break;
  983. }
  984. /*
  985. * If we are waiting on something and this happens to be
  986. * that thing then set the re-configure flag.
  987. */
  988. if (container != (u32)-1) {
  989. if (container >= dev->maximum_num_containers)
  990. break;
  991. if ((dev->fsa_dev[container].config_waiting_on ==
  992. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  993. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  994. dev->fsa_dev[container].config_waiting_on = 0;
  995. } else for (container = 0;
  996. container < dev->maximum_num_containers; ++container) {
  997. if ((dev->fsa_dev[container].config_waiting_on ==
  998. le32_to_cpu(*(__le32 *)aifcmd->data)) &&
  999. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT))
  1000. dev->fsa_dev[container].config_waiting_on = 0;
  1001. }
  1002. break;
  1003. case AifCmdJobProgress:
  1004. /*
  1005. * These are job progress AIF's. When a Clear is being
  1006. * done on a container it is initially created then hidden from
  1007. * the OS. When the clear completes we don't get a config
  1008. * change so we monitor the job status complete on a clear then
  1009. * wait for a container change.
  1010. */
  1011. if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
  1012. (((__le32 *)aifcmd->data)[6] == ((__le32 *)aifcmd->data)[5] ||
  1013. ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsSuccess))) {
  1014. for (container = 0;
  1015. container < dev->maximum_num_containers;
  1016. ++container) {
  1017. /*
  1018. * Stomp on all config sequencing for all
  1019. * containers?
  1020. */
  1021. dev->fsa_dev[container].config_waiting_on =
  1022. AifEnContainerChange;
  1023. dev->fsa_dev[container].config_needed = ADD;
  1024. dev->fsa_dev[container].config_waiting_stamp =
  1025. jiffies;
  1026. }
  1027. }
  1028. if (((__le32 *)aifcmd->data)[1] == cpu_to_le32(AifJobCtrZero) &&
  1029. ((__le32 *)aifcmd->data)[6] == 0 &&
  1030. ((__le32 *)aifcmd->data)[4] == cpu_to_le32(AifJobStsRunning)) {
  1031. for (container = 0;
  1032. container < dev->maximum_num_containers;
  1033. ++container) {
  1034. /*
  1035. * Stomp on all config sequencing for all
  1036. * containers?
  1037. */
  1038. dev->fsa_dev[container].config_waiting_on =
  1039. AifEnContainerChange;
  1040. dev->fsa_dev[container].config_needed = DELETE;
  1041. dev->fsa_dev[container].config_waiting_stamp =
  1042. jiffies;
  1043. }
  1044. }
  1045. break;
  1046. }
  1047. container = 0;
  1048. retry_next:
  1049. if (device_config_needed == NOTHING)
  1050. for (; container < dev->maximum_num_containers; ++container) {
  1051. if ((dev->fsa_dev[container].config_waiting_on == 0) &&
  1052. (dev->fsa_dev[container].config_needed != NOTHING) &&
  1053. time_before(jiffies, dev->fsa_dev[container].config_waiting_stamp + AIF_SNIFF_TIMEOUT)) {
  1054. device_config_needed =
  1055. dev->fsa_dev[container].config_needed;
  1056. dev->fsa_dev[container].config_needed = NOTHING;
  1057. channel = CONTAINER_TO_CHANNEL(container);
  1058. id = CONTAINER_TO_ID(container);
  1059. lun = CONTAINER_TO_LUN(container);
  1060. break;
  1061. }
  1062. }
  1063. if (device_config_needed == NOTHING)
  1064. return;
  1065. /*
  1066. * If we decided that a re-configuration needs to be done,
  1067. * schedule it here on the way out the door, please close the door
  1068. * behind you.
  1069. */
  1070. /*
  1071. * Find the scsi_device associated with the SCSI address,
  1072. * and mark it as changed, invalidating the cache. This deals
  1073. * with changes to existing device IDs.
  1074. */
  1075. if (!dev || !dev->scsi_host_ptr)
  1076. return;
  1077. /*
  1078. * force reload of disk info via aac_probe_container
  1079. */
  1080. if ((channel == CONTAINER_CHANNEL) &&
  1081. (device_config_needed != NOTHING)) {
  1082. if (dev->fsa_dev[container].valid == 1)
  1083. dev->fsa_dev[container].valid = 2;
  1084. aac_probe_container(dev, container);
  1085. }
  1086. device = scsi_device_lookup(dev->scsi_host_ptr, channel, id, lun);
  1087. if (device) {
  1088. switch (device_config_needed) {
  1089. case DELETE:
  1090. #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
  1091. scsi_remove_device(device);
  1092. #else
  1093. if (scsi_device_online(device)) {
  1094. scsi_device_set_state(device, SDEV_OFFLINE);
  1095. sdev_printk(KERN_INFO, device,
  1096. "Device offlined - %s\n",
  1097. (channel == CONTAINER_CHANNEL) ?
  1098. "array deleted" :
  1099. "enclosure services event");
  1100. }
  1101. #endif
  1102. break;
  1103. case ADD:
  1104. if (!scsi_device_online(device)) {
  1105. sdev_printk(KERN_INFO, device,
  1106. "Device online - %s\n",
  1107. (channel == CONTAINER_CHANNEL) ?
  1108. "array created" :
  1109. "enclosure services event");
  1110. scsi_device_set_state(device, SDEV_RUNNING);
  1111. }
  1112. /* FALLTHRU */
  1113. case CHANGE:
  1114. if ((channel == CONTAINER_CHANNEL)
  1115. && (!dev->fsa_dev[container].valid)) {
  1116. #if (defined(AAC_DEBUG_INSTRUMENT_AIF_DELETE))
  1117. scsi_remove_device(device);
  1118. #else
  1119. if (!scsi_device_online(device))
  1120. break;
  1121. scsi_device_set_state(device, SDEV_OFFLINE);
  1122. sdev_printk(KERN_INFO, device,
  1123. "Device offlined - %s\n",
  1124. "array failed");
  1125. #endif
  1126. break;
  1127. }
  1128. scsi_rescan_device(&device->sdev_gendev);
  1129. default:
  1130. break;
  1131. }
  1132. scsi_device_put(device);
  1133. device_config_needed = NOTHING;
  1134. }
  1135. if (device_config_needed == ADD)
  1136. scsi_add_device(dev->scsi_host_ptr, channel, id, lun);
  1137. if (channel == CONTAINER_CHANNEL) {
  1138. container++;
  1139. device_config_needed = NOTHING;
  1140. goto retry_next;
  1141. }
  1142. }
  1143. static int _aac_reset_adapter(struct aac_dev *aac, int forced)
  1144. {
  1145. int index, quirks;
  1146. int retval;
  1147. struct Scsi_Host *host;
  1148. struct scsi_device *dev;
  1149. struct scsi_cmnd *command;
  1150. struct scsi_cmnd *command_list;
  1151. int jafo = 0;
  1152. /*
  1153. * Assumptions:
  1154. * - host is locked, unless called by the aacraid thread.
  1155. * (a matter of convenience, due to legacy issues surrounding
  1156. * eh_host_adapter_reset).
  1157. * - in_reset is asserted, so no new i/o is getting to the
  1158. * card.
  1159. * - The card is dead, or will be very shortly ;-/ so no new
  1160. * commands are completing in the interrupt service.
  1161. */
  1162. host = aac->scsi_host_ptr;
  1163. scsi_block_requests(host);
  1164. aac_adapter_disable_int(aac);
  1165. if (aac->thread->pid != current->pid) {
  1166. spin_unlock_irq(host->host_lock);
  1167. kthread_stop(aac->thread);
  1168. jafo = 1;
  1169. }
  1170. /*
  1171. * If a positive health, means in a known DEAD PANIC
  1172. * state and the adapter could be reset to `try again'.
  1173. */
  1174. retval = aac_adapter_restart(aac, forced ? 0 : aac_adapter_check_health(aac));
  1175. if (retval)
  1176. goto out;
  1177. /*
  1178. * Loop through the fibs, close the synchronous FIBS
  1179. */
  1180. for (retval = 1, index = 0; index < (aac->scsi_host_ptr->can_queue + AAC_NUM_MGT_FIB); index++) {
  1181. struct fib *fib = &aac->fibs[index];
  1182. if (!(fib->hw_fib_va->header.XferState & cpu_to_le32(NoResponseExpected | Async)) &&
  1183. (fib->hw_fib_va->header.XferState & cpu_to_le32(ResponseExpected))) {
  1184. unsigned long flagv;
  1185. spin_lock_irqsave(&fib->event_lock, flagv);
  1186. up(&fib->event_wait);
  1187. spin_unlock_irqrestore(&fib->event_lock, flagv);
  1188. schedule();
  1189. retval = 0;
  1190. }
  1191. }
  1192. /* Give some extra time for ioctls to complete. */
  1193. if (retval == 0)
  1194. ssleep(2);
  1195. index = aac->cardtype;
  1196. /*
  1197. * Re-initialize the adapter, first free resources, then carefully
  1198. * apply the initialization sequence to come back again. Only risk
  1199. * is a change in Firmware dropping cache, it is assumed the caller
  1200. * will ensure that i/o is queisced and the card is flushed in that
  1201. * case.
  1202. */
  1203. aac_fib_map_free(aac);
  1204. pci_free_consistent(aac->pdev, aac->comm_size, aac->comm_addr, aac->comm_phys);
  1205. aac->comm_addr = NULL;
  1206. aac->comm_phys = 0;
  1207. kfree(aac->queues);
  1208. aac->queues = NULL;
  1209. free_irq(aac->pdev->irq, aac);
  1210. if (aac->msi)
  1211. pci_disable_msi(aac->pdev);
  1212. kfree(aac->fsa_dev);
  1213. aac->fsa_dev = NULL;
  1214. quirks = aac_get_driver_ident(index)->quirks;
  1215. if (quirks & AAC_QUIRK_31BIT) {
  1216. if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(31)))) ||
  1217. ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(31)))))
  1218. goto out;
  1219. } else {
  1220. if (((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32)))) ||
  1221. ((retval = pci_set_consistent_dma_mask(aac->pdev, DMA_BIT_MASK(32)))))
  1222. goto out;
  1223. }
  1224. if ((retval = (*(aac_get_driver_ident(index)->init))(aac)))
  1225. goto out;
  1226. if (quirks & AAC_QUIRK_31BIT)
  1227. if ((retval = pci_set_dma_mask(aac->pdev, DMA_BIT_MASK(32))))
  1228. goto out;
  1229. if (jafo) {
  1230. aac->thread = kthread_run(aac_command_thread, aac, aac->name);
  1231. if (IS_ERR(aac->thread)) {
  1232. retval = PTR_ERR(aac->thread);
  1233. goto out;
  1234. }
  1235. }
  1236. (void)aac_get_adapter_info(aac);
  1237. if ((quirks & AAC_QUIRK_34SG) && (host->sg_tablesize > 34)) {
  1238. host->sg_tablesize = 34;
  1239. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1240. }
  1241. if ((quirks & AAC_QUIRK_17SG) && (host->sg_tablesize > 17)) {
  1242. host->sg_tablesize = 17;
  1243. host->max_sectors = (host->sg_tablesize * 8) + 112;
  1244. }
  1245. aac_get_config_status(aac, 1);
  1246. aac_get_containers(aac);
  1247. /*
  1248. * This is where the assumption that the Adapter is quiesced
  1249. * is important.
  1250. */
  1251. command_list = NULL;
  1252. __shost_for_each_device(dev, host) {
  1253. unsigned long flags;
  1254. spin_lock_irqsave(&dev->list_lock, flags);
  1255. list_for_each_entry(command, &dev->cmd_list, list)
  1256. if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
  1257. command->SCp.buffer = (struct scatterlist *)command_list;
  1258. command_list = command;
  1259. }
  1260. spin_unlock_irqrestore(&dev->list_lock, flags);
  1261. }
  1262. while ((command = command_list)) {
  1263. command_list = (struct scsi_cmnd *)command->SCp.buffer;
  1264. command->SCp.buffer = NULL;
  1265. command->result = DID_OK << 16
  1266. | COMMAND_COMPLETE << 8
  1267. | SAM_STAT_TASK_SET_FULL;
  1268. command->SCp.phase = AAC_OWNER_ERROR_HANDLER;
  1269. command->scsi_done(command);
  1270. }
  1271. retval = 0;
  1272. out:
  1273. aac->in_reset = 0;
  1274. scsi_unblock_requests(host);
  1275. if (jafo) {
  1276. spin_lock_irq(host->host_lock);
  1277. }
  1278. return retval;
  1279. }
  1280. int aac_reset_adapter(struct aac_dev * aac, int forced)
  1281. {
  1282. unsigned long flagv = 0;
  1283. int retval;
  1284. struct Scsi_Host * host;
  1285. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1286. return -EBUSY;
  1287. if (aac->in_reset) {
  1288. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1289. return -EBUSY;
  1290. }
  1291. aac->in_reset = 1;
  1292. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1293. /*
  1294. * Wait for all commands to complete to this specific
  1295. * target (block maximum 60 seconds). Although not necessary,
  1296. * it does make us a good storage citizen.
  1297. */
  1298. host = aac->scsi_host_ptr;
  1299. scsi_block_requests(host);
  1300. if (forced < 2) for (retval = 60; retval; --retval) {
  1301. struct scsi_device * dev;
  1302. struct scsi_cmnd * command;
  1303. int active = 0;
  1304. __shost_for_each_device(dev, host) {
  1305. spin_lock_irqsave(&dev->list_lock, flagv);
  1306. list_for_each_entry(command, &dev->cmd_list, list) {
  1307. if (command->SCp.phase == AAC_OWNER_FIRMWARE) {
  1308. active++;
  1309. break;
  1310. }
  1311. }
  1312. spin_unlock_irqrestore(&dev->list_lock, flagv);
  1313. if (active)
  1314. break;
  1315. }
  1316. /*
  1317. * We can exit If all the commands are complete
  1318. */
  1319. if (active == 0)
  1320. break;
  1321. ssleep(1);
  1322. }
  1323. /* Quiesce build, flush cache, write through mode */
  1324. if (forced < 2)
  1325. aac_send_shutdown(aac);
  1326. spin_lock_irqsave(host->host_lock, flagv);
  1327. retval = _aac_reset_adapter(aac, forced ? forced : ((aac_check_reset != 0) && (aac_check_reset != 1)));
  1328. spin_unlock_irqrestore(host->host_lock, flagv);
  1329. if ((forced < 2) && (retval == -ENODEV)) {
  1330. /* Unwind aac_send_shutdown() IOP_RESET unsupported/disabled */
  1331. struct fib * fibctx = aac_fib_alloc(aac);
  1332. if (fibctx) {
  1333. struct aac_pause *cmd;
  1334. int status;
  1335. aac_fib_init(fibctx);
  1336. cmd = (struct aac_pause *) fib_data(fibctx);
  1337. cmd->command = cpu_to_le32(VM_ContainerConfig);
  1338. cmd->type = cpu_to_le32(CT_PAUSE_IO);
  1339. cmd->timeout = cpu_to_le32(1);
  1340. cmd->min = cpu_to_le32(1);
  1341. cmd->noRescan = cpu_to_le32(1);
  1342. cmd->count = cpu_to_le32(0);
  1343. status = aac_fib_send(ContainerCommand,
  1344. fibctx,
  1345. sizeof(struct aac_pause),
  1346. FsaNormal,
  1347. -2 /* Timeout silently */, 1,
  1348. NULL, NULL);
  1349. if (status >= 0)
  1350. aac_fib_complete(fibctx);
  1351. /* FIB should be freed only after getting
  1352. * the response from the F/W */
  1353. if (status != -ERESTARTSYS)
  1354. aac_fib_free(fibctx);
  1355. }
  1356. }
  1357. return retval;
  1358. }
  1359. int aac_check_health(struct aac_dev * aac)
  1360. {
  1361. int BlinkLED;
  1362. unsigned long time_now, flagv = 0;
  1363. struct list_head * entry;
  1364. struct Scsi_Host * host;
  1365. /* Extending the scope of fib_lock slightly to protect aac->in_reset */
  1366. if (spin_trylock_irqsave(&aac->fib_lock, flagv) == 0)
  1367. return 0;
  1368. if (aac->in_reset || !(BlinkLED = aac_adapter_check_health(aac))) {
  1369. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1370. return 0; /* OK */
  1371. }
  1372. aac->in_reset = 1;
  1373. /* Fake up an AIF:
  1374. * aac_aifcmd.command = AifCmdEventNotify = 1
  1375. * aac_aifcmd.seqnum = 0xFFFFFFFF
  1376. * aac_aifcmd.data[0] = AifEnExpEvent = 23
  1377. * aac_aifcmd.data[1] = AifExeFirmwarePanic = 3
  1378. * aac.aifcmd.data[2] = AifHighPriority = 3
  1379. * aac.aifcmd.data[3] = BlinkLED
  1380. */
  1381. time_now = jiffies/HZ;
  1382. entry = aac->fib_list.next;
  1383. /*
  1384. * For each Context that is on the
  1385. * fibctxList, make a copy of the
  1386. * fib, and then set the event to wake up the
  1387. * thread that is waiting for it.
  1388. */
  1389. while (entry != &aac->fib_list) {
  1390. /*
  1391. * Extract the fibctx
  1392. */
  1393. struct aac_fib_context *fibctx = list_entry(entry, struct aac_fib_context, next);
  1394. struct hw_fib * hw_fib;
  1395. struct fib * fib;
  1396. /*
  1397. * Check if the queue is getting
  1398. * backlogged
  1399. */
  1400. if (fibctx->count > 20) {
  1401. /*
  1402. * It's *not* jiffies folks,
  1403. * but jiffies / HZ, so do not
  1404. * panic ...
  1405. */
  1406. u32 time_last = fibctx->jiffies;
  1407. /*
  1408. * Has it been > 2 minutes
  1409. * since the last read off
  1410. * the queue?
  1411. */
  1412. if ((time_now - time_last) > aif_timeout) {
  1413. entry = entry->next;
  1414. aac_close_fib_context(aac, fibctx);
  1415. continue;
  1416. }
  1417. }
  1418. /*
  1419. * Warning: no sleep allowed while
  1420. * holding spinlock
  1421. */
  1422. hw_fib = kzalloc(sizeof(struct hw_fib), GFP_ATOMIC);
  1423. fib = kzalloc(sizeof(struct fib), GFP_ATOMIC);
  1424. if (fib && hw_fib) {
  1425. struct aac_aifcmd * aif;
  1426. fib->hw_fib_va = hw_fib;
  1427. fib->dev = aac;
  1428. aac_fib_init(fib);
  1429. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1430. fib->size = sizeof (struct fib);
  1431. fib->data = hw_fib->data;
  1432. aif = (struct aac_aifcmd *)hw_fib->data;
  1433. aif->command = cpu_to_le32(AifCmdEventNotify);
  1434. aif->seqnum = cpu_to_le32(0xFFFFFFFF);
  1435. ((__le32 *)aif->data)[0] = cpu_to_le32(AifEnExpEvent);
  1436. ((__le32 *)aif->data)[1] = cpu_to_le32(AifExeFirmwarePanic);
  1437. ((__le32 *)aif->data)[2] = cpu_to_le32(AifHighPriority);
  1438. ((__le32 *)aif->data)[3] = cpu_to_le32(BlinkLED);
  1439. /*
  1440. * Put the FIB onto the
  1441. * fibctx's fibs
  1442. */
  1443. list_add_tail(&fib->fiblink, &fibctx->fib_list);
  1444. fibctx->count++;
  1445. /*
  1446. * Set the event to wake up the
  1447. * thread that will waiting.
  1448. */
  1449. up(&fibctx->wait_sem);
  1450. } else {
  1451. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1452. kfree(fib);
  1453. kfree(hw_fib);
  1454. }
  1455. entry = entry->next;
  1456. }
  1457. spin_unlock_irqrestore(&aac->fib_lock, flagv);
  1458. if (BlinkLED < 0) {
  1459. printk(KERN_ERR "%s: Host adapter dead %d\n", aac->name, BlinkLED);
  1460. goto out;
  1461. }
  1462. printk(KERN_ERR "%s: Host adapter BLINK LED 0x%x\n", aac->name, BlinkLED);
  1463. if (!aac_check_reset || ((aac_check_reset == 1) &&
  1464. (aac->supplement_adapter_info.SupportedOptions2 &
  1465. AAC_OPTION_IGNORE_RESET)))
  1466. goto out;
  1467. host = aac->scsi_host_ptr;
  1468. if (aac->thread->pid != current->pid)
  1469. spin_lock_irqsave(host->host_lock, flagv);
  1470. BlinkLED = _aac_reset_adapter(aac, aac_check_reset != 1);
  1471. if (aac->thread->pid != current->pid)
  1472. spin_unlock_irqrestore(host->host_lock, flagv);
  1473. return BlinkLED;
  1474. out:
  1475. aac->in_reset = 0;
  1476. return BlinkLED;
  1477. }
  1478. /**
  1479. * aac_command_thread - command processing thread
  1480. * @dev: Adapter to monitor
  1481. *
  1482. * Waits on the commandready event in it's queue. When the event gets set
  1483. * it will pull FIBs off it's queue. It will continue to pull FIBs off
  1484. * until the queue is empty. When the queue is empty it will wait for
  1485. * more FIBs.
  1486. */
  1487. int aac_command_thread(void *data)
  1488. {
  1489. struct aac_dev *dev = data;
  1490. struct hw_fib *hw_fib, *hw_newfib;
  1491. struct fib *fib, *newfib;
  1492. struct aac_fib_context *fibctx;
  1493. unsigned long flags;
  1494. DECLARE_WAITQUEUE(wait, current);
  1495. unsigned long next_jiffies = jiffies + HZ;
  1496. unsigned long next_check_jiffies = next_jiffies;
  1497. long difference = HZ;
  1498. /*
  1499. * We can only have one thread per adapter for AIF's.
  1500. */
  1501. if (dev->aif_thread)
  1502. return -EINVAL;
  1503. /*
  1504. * Let the DPC know it has a place to send the AIF's to.
  1505. */
  1506. dev->aif_thread = 1;
  1507. add_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1508. set_current_state(TASK_INTERRUPTIBLE);
  1509. dprintk ((KERN_INFO "aac_command_thread start\n"));
  1510. while (1) {
  1511. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1512. while(!list_empty(&(dev->queues->queue[HostNormCmdQueue].cmdq))) {
  1513. struct list_head *entry;
  1514. struct aac_aifcmd * aifcmd;
  1515. set_current_state(TASK_RUNNING);
  1516. entry = dev->queues->queue[HostNormCmdQueue].cmdq.next;
  1517. list_del(entry);
  1518. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1519. fib = list_entry(entry, struct fib, fiblink);
  1520. /*
  1521. * We will process the FIB here or pass it to a
  1522. * worker thread that is TBD. We Really can't
  1523. * do anything at this point since we don't have
  1524. * anything defined for this thread to do.
  1525. */
  1526. hw_fib = fib->hw_fib_va;
  1527. memset(fib, 0, sizeof(struct fib));
  1528. fib->type = FSAFS_NTC_FIB_CONTEXT;
  1529. fib->size = sizeof(struct fib);
  1530. fib->hw_fib_va = hw_fib;
  1531. fib->data = hw_fib->data;
  1532. fib->dev = dev;
  1533. /*
  1534. * We only handle AifRequest fibs from the adapter.
  1535. */
  1536. aifcmd = (struct aac_aifcmd *) hw_fib->data;
  1537. if (aifcmd->command == cpu_to_le32(AifCmdDriverNotify)) {
  1538. /* Handle Driver Notify Events */
  1539. aac_handle_aif(dev, fib);
  1540. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1541. aac_fib_adapter_complete(fib, (u16)sizeof(u32));
  1542. } else {
  1543. /* The u32 here is important and intended. We are using
  1544. 32bit wrapping time to fit the adapter field */
  1545. u32 time_now, time_last;
  1546. unsigned long flagv;
  1547. unsigned num;
  1548. struct hw_fib ** hw_fib_pool, ** hw_fib_p;
  1549. struct fib ** fib_pool, ** fib_p;
  1550. /* Sniff events */
  1551. if ((aifcmd->command ==
  1552. cpu_to_le32(AifCmdEventNotify)) ||
  1553. (aifcmd->command ==
  1554. cpu_to_le32(AifCmdJobProgress))) {
  1555. aac_handle_aif(dev, fib);
  1556. }
  1557. time_now = jiffies/HZ;
  1558. /*
  1559. * Warning: no sleep allowed while
  1560. * holding spinlock. We take the estimate
  1561. * and pre-allocate a set of fibs outside the
  1562. * lock.
  1563. */
  1564. num = le32_to_cpu(dev->init->AdapterFibsSize)
  1565. / sizeof(struct hw_fib); /* some extra */
  1566. spin_lock_irqsave(&dev->fib_lock, flagv);
  1567. entry = dev->fib_list.next;
  1568. while (entry != &dev->fib_list) {
  1569. entry = entry->next;
  1570. ++num;
  1571. }
  1572. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1573. hw_fib_pool = NULL;
  1574. fib_pool = NULL;
  1575. if (num
  1576. && ((hw_fib_pool = kmalloc(sizeof(struct hw_fib *) * num, GFP_KERNEL)))
  1577. && ((fib_pool = kmalloc(sizeof(struct fib *) * num, GFP_KERNEL)))) {
  1578. hw_fib_p = hw_fib_pool;
  1579. fib_p = fib_pool;
  1580. while (hw_fib_p < &hw_fib_pool[num]) {
  1581. if (!(*(hw_fib_p++) = kmalloc(sizeof(struct hw_fib), GFP_KERNEL))) {
  1582. --hw_fib_p;
  1583. break;
  1584. }
  1585. if (!(*(fib_p++) = kmalloc(sizeof(struct fib), GFP_KERNEL))) {
  1586. kfree(*(--hw_fib_p));
  1587. break;
  1588. }
  1589. }
  1590. if ((num = hw_fib_p - hw_fib_pool) == 0) {
  1591. kfree(fib_pool);
  1592. fib_pool = NULL;
  1593. kfree(hw_fib_pool);
  1594. hw_fib_pool = NULL;
  1595. }
  1596. } else {
  1597. kfree(hw_fib_pool);
  1598. hw_fib_pool = NULL;
  1599. }
  1600. spin_lock_irqsave(&dev->fib_lock, flagv);
  1601. entry = dev->fib_list.next;
  1602. /*
  1603. * For each Context that is on the
  1604. * fibctxList, make a copy of the
  1605. * fib, and then set the event to wake up the
  1606. * thread that is waiting for it.
  1607. */
  1608. hw_fib_p = hw_fib_pool;
  1609. fib_p = fib_pool;
  1610. while (entry != &dev->fib_list) {
  1611. /*
  1612. * Extract the fibctx
  1613. */
  1614. fibctx = list_entry(entry, struct aac_fib_context, next);
  1615. /*
  1616. * Check if the queue is getting
  1617. * backlogged
  1618. */
  1619. if (fibctx->count > 20)
  1620. {
  1621. /*
  1622. * It's *not* jiffies folks,
  1623. * but jiffies / HZ so do not
  1624. * panic ...
  1625. */
  1626. time_last = fibctx->jiffies;
  1627. /*
  1628. * Has it been > 2 minutes
  1629. * since the last read off
  1630. * the queue?
  1631. */
  1632. if ((time_now - time_last) > aif_timeout) {
  1633. entry = entry->next;
  1634. aac_close_fib_context(dev, fibctx);
  1635. continue;
  1636. }
  1637. }
  1638. /*
  1639. * Warning: no sleep allowed while
  1640. * holding spinlock
  1641. */
  1642. if (hw_fib_p < &hw_fib_pool[num]) {
  1643. hw_newfib = *hw_fib_p;
  1644. *(hw_fib_p++) = NULL;
  1645. newfib = *fib_p;
  1646. *(fib_p++) = NULL;
  1647. /*
  1648. * Make the copy of the FIB
  1649. */
  1650. memcpy(hw_newfib, hw_fib, sizeof(struct hw_fib));
  1651. memcpy(newfib, fib, sizeof(struct fib));
  1652. newfib->hw_fib_va = hw_newfib;
  1653. /*
  1654. * Put the FIB onto the
  1655. * fibctx's fibs
  1656. */
  1657. list_add_tail(&newfib->fiblink, &fibctx->fib_list);
  1658. fibctx->count++;
  1659. /*
  1660. * Set the event to wake up the
  1661. * thread that is waiting.
  1662. */
  1663. up(&fibctx->wait_sem);
  1664. } else {
  1665. printk(KERN_WARNING "aifd: didn't allocate NewFib.\n");
  1666. }
  1667. entry = entry->next;
  1668. }
  1669. /*
  1670. * Set the status of this FIB
  1671. */
  1672. *(__le32 *)hw_fib->data = cpu_to_le32(ST_OK);
  1673. aac_fib_adapter_complete(fib, sizeof(u32));
  1674. spin_unlock_irqrestore(&dev->fib_lock, flagv);
  1675. /* Free up the remaining resources */
  1676. hw_fib_p = hw_fib_pool;
  1677. fib_p = fib_pool;
  1678. while (hw_fib_p < &hw_fib_pool[num]) {
  1679. kfree(*hw_fib_p);
  1680. kfree(*fib_p);
  1681. ++fib_p;
  1682. ++hw_fib_p;
  1683. }
  1684. kfree(hw_fib_pool);
  1685. kfree(fib_pool);
  1686. }
  1687. kfree(fib);
  1688. spin_lock_irqsave(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1689. }
  1690. /*
  1691. * There are no more AIF's
  1692. */
  1693. spin_unlock_irqrestore(dev->queues->queue[HostNormCmdQueue].lock, flags);
  1694. /*
  1695. * Background activity
  1696. */
  1697. if ((time_before(next_check_jiffies,next_jiffies))
  1698. && ((difference = next_check_jiffies - jiffies) <= 0)) {
  1699. next_check_jiffies = next_jiffies;
  1700. if (aac_check_health(dev) == 0) {
  1701. difference = ((long)(unsigned)check_interval)
  1702. * HZ;
  1703. next_check_jiffies = jiffies + difference;
  1704. } else if (!dev->queues)
  1705. break;
  1706. }
  1707. if (!time_before(next_check_jiffies,next_jiffies)
  1708. && ((difference = next_jiffies - jiffies) <= 0)) {
  1709. struct timeval now;
  1710. int ret;
  1711. /* Don't even try to talk to adapter if its sick */
  1712. ret = aac_check_health(dev);
  1713. if (!ret && !dev->queues)
  1714. break;
  1715. next_check_jiffies = jiffies
  1716. + ((long)(unsigned)check_interval)
  1717. * HZ;
  1718. do_gettimeofday(&now);
  1719. /* Synchronize our watches */
  1720. if (((1000000 - (1000000 / HZ)) > now.tv_usec)
  1721. && (now.tv_usec > (1000000 / HZ)))
  1722. difference = (((1000000 - now.tv_usec) * HZ)
  1723. + 500000) / 1000000;
  1724. else if (ret == 0) {
  1725. struct fib *fibptr;
  1726. if ((fibptr = aac_fib_alloc(dev))) {
  1727. int status;
  1728. __le32 *info;
  1729. aac_fib_init(fibptr);
  1730. info = (__le32 *) fib_data(fibptr);
  1731. if (now.tv_usec > 500000)
  1732. ++now.tv_sec;
  1733. *info = cpu_to_le32(now.tv_sec);
  1734. status = aac_fib_send(SendHostTime,
  1735. fibptr,
  1736. sizeof(*info),
  1737. FsaNormal,
  1738. 1, 1,
  1739. NULL,
  1740. NULL);
  1741. /* Do not set XferState to zero unless
  1742. * receives a response from F/W */
  1743. if (status >= 0)
  1744. aac_fib_complete(fibptr);
  1745. /* FIB should be freed only after
  1746. * getting the response from the F/W */
  1747. if (status != -ERESTARTSYS)
  1748. aac_fib_free(fibptr);
  1749. }
  1750. difference = (long)(unsigned)update_interval*HZ;
  1751. } else {
  1752. /* retry shortly */
  1753. difference = 10 * HZ;
  1754. }
  1755. next_jiffies = jiffies + difference;
  1756. if (time_before(next_check_jiffies,next_jiffies))
  1757. difference = next_check_jiffies - jiffies;
  1758. }
  1759. if (difference <= 0)
  1760. difference = 1;
  1761. set_current_state(TASK_INTERRUPTIBLE);
  1762. schedule_timeout(difference);
  1763. if (kthread_should_stop())
  1764. break;
  1765. }
  1766. if (dev->queues)
  1767. remove_wait_queue(&dev->queues->queue[HostNormCmdQueue].cmdready, &wait);
  1768. dev->aif_thread = 0;
  1769. return 0;
  1770. }