commsup.c 48 KB

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