commsup.c 43 KB

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