commsup.c 48 KB

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