commsup.c 48 KB

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