commsup.c 44 KB

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