commsup.c 57 KB

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