commsup.c 44 KB

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