commsup.c 43 KB

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