commsup.c 45 KB

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