aic7xxx_osm.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. * Adaptec AIC7xxx device driver for Linux.
  3. *
  4. * Copyright (c) 1994 John Aycock
  5. * The University of Calgary Department of Computer Science.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; see the file COPYING. If not, write to
  19. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. * Copyright (c) 2000-2003 Adaptec Inc.
  22. * All rights reserved.
  23. *
  24. * Redistribution and use in source and binary forms, with or without
  25. * modification, are permitted provided that the following conditions
  26. * are met:
  27. * 1. Redistributions of source code must retain the above copyright
  28. * notice, this list of conditions, and the following disclaimer,
  29. * without modification.
  30. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  31. * substantially similar to the "NO WARRANTY" disclaimer below
  32. * ("Disclaimer") and any redistribution must be conditioned upon
  33. * including a substantially similar Disclaimer requirement for further
  34. * binary redistribution.
  35. * 3. Neither the names of the above-listed copyright holders nor the names
  36. * of any contributors may be used to endorse or promote products derived
  37. * from this software without specific prior written permission.
  38. *
  39. * Alternatively, this software may be distributed under the terms of the
  40. * GNU General Public License ("GPL") version 2 as published by the Free
  41. * Software Foundation.
  42. *
  43. * NO WARRANTY
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  45. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  47. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  48. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  49. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  50. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  52. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  53. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54. * POSSIBILITY OF SUCH DAMAGES.
  55. *
  56. * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic7xxx_osm.h#151 $
  57. *
  58. */
  59. #ifndef _AIC7XXX_LINUX_H_
  60. #define _AIC7XXX_LINUX_H_
  61. #include <linux/types.h>
  62. #include <linux/blkdev.h>
  63. #include <linux/delay.h>
  64. #include <linux/ioport.h>
  65. #include <linux/pci.h>
  66. #include <linux/smp_lock.h>
  67. #include <linux/version.h>
  68. #include <linux/module.h>
  69. #include <asm/byteorder.h>
  70. #include <asm/io.h>
  71. #include <linux/interrupt.h> /* For tasklet support. */
  72. #include <linux/config.h>
  73. #include <linux/slab.h>
  74. /* Core SCSI definitions */
  75. #define AIC_LIB_PREFIX ahc
  76. #include "scsi.h"
  77. #include <scsi/scsi_host.h>
  78. /* Name space conflict with BSD queue macros */
  79. #ifdef LIST_HEAD
  80. #undef LIST_HEAD
  81. #endif
  82. #include "cam.h"
  83. #include "queue.h"
  84. #include "scsi_message.h"
  85. #include "aiclib.h"
  86. /*********************************** Debugging ********************************/
  87. #ifdef CONFIG_AIC7XXX_DEBUG_ENABLE
  88. #ifdef CONFIG_AIC7XXX_DEBUG_MASK
  89. #define AHC_DEBUG 1
  90. #define AHC_DEBUG_OPTS CONFIG_AIC7XXX_DEBUG_MASK
  91. #else
  92. /*
  93. * Compile in debugging code, but do not enable any printfs.
  94. */
  95. #define AHC_DEBUG 1
  96. #endif
  97. /* No debugging code. */
  98. #endif
  99. /************************* Forward Declarations *******************************/
  100. struct ahc_softc;
  101. typedef struct pci_dev *ahc_dev_softc_t;
  102. typedef Scsi_Cmnd *ahc_io_ctx_t;
  103. /******************************* Byte Order ***********************************/
  104. #define ahc_htobe16(x) cpu_to_be16(x)
  105. #define ahc_htobe32(x) cpu_to_be32(x)
  106. #define ahc_htobe64(x) cpu_to_be64(x)
  107. #define ahc_htole16(x) cpu_to_le16(x)
  108. #define ahc_htole32(x) cpu_to_le32(x)
  109. #define ahc_htole64(x) cpu_to_le64(x)
  110. #define ahc_be16toh(x) be16_to_cpu(x)
  111. #define ahc_be32toh(x) be32_to_cpu(x)
  112. #define ahc_be64toh(x) be64_to_cpu(x)
  113. #define ahc_le16toh(x) le16_to_cpu(x)
  114. #define ahc_le32toh(x) le32_to_cpu(x)
  115. #define ahc_le64toh(x) le64_to_cpu(x)
  116. #ifndef LITTLE_ENDIAN
  117. #define LITTLE_ENDIAN 1234
  118. #endif
  119. #ifndef BIG_ENDIAN
  120. #define BIG_ENDIAN 4321
  121. #endif
  122. #ifndef BYTE_ORDER
  123. #if defined(__BIG_ENDIAN)
  124. #define BYTE_ORDER BIG_ENDIAN
  125. #endif
  126. #if defined(__LITTLE_ENDIAN)
  127. #define BYTE_ORDER LITTLE_ENDIAN
  128. #endif
  129. #endif /* BYTE_ORDER */
  130. /************************* Configuration Data *********************************/
  131. extern u_int aic7xxx_no_probe;
  132. extern u_int aic7xxx_allow_memio;
  133. extern int aic7xxx_detect_complete;
  134. extern Scsi_Host_Template aic7xxx_driver_template;
  135. /***************************** Bus Space/DMA **********************************/
  136. typedef uint32_t bus_size_t;
  137. typedef enum {
  138. BUS_SPACE_MEMIO,
  139. BUS_SPACE_PIO
  140. } bus_space_tag_t;
  141. typedef union {
  142. u_long ioport;
  143. volatile uint8_t __iomem *maddr;
  144. } bus_space_handle_t;
  145. typedef struct bus_dma_segment
  146. {
  147. dma_addr_t ds_addr;
  148. bus_size_t ds_len;
  149. } bus_dma_segment_t;
  150. struct ahc_linux_dma_tag
  151. {
  152. bus_size_t alignment;
  153. bus_size_t boundary;
  154. bus_size_t maxsize;
  155. };
  156. typedef struct ahc_linux_dma_tag* bus_dma_tag_t;
  157. struct ahc_linux_dmamap
  158. {
  159. dma_addr_t bus_addr;
  160. };
  161. typedef struct ahc_linux_dmamap* bus_dmamap_t;
  162. typedef int bus_dma_filter_t(void*, dma_addr_t);
  163. typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
  164. #define BUS_DMA_WAITOK 0x0
  165. #define BUS_DMA_NOWAIT 0x1
  166. #define BUS_DMA_ALLOCNOW 0x2
  167. #define BUS_DMA_LOAD_SEGS 0x4 /*
  168. * Argument is an S/G list not
  169. * a single buffer.
  170. */
  171. #define BUS_SPACE_MAXADDR 0xFFFFFFFF
  172. #define BUS_SPACE_MAXADDR_32BIT 0xFFFFFFFF
  173. #define BUS_SPACE_MAXSIZE_32BIT 0xFFFFFFFF
  174. int ahc_dma_tag_create(struct ahc_softc *, bus_dma_tag_t /*parent*/,
  175. bus_size_t /*alignment*/, bus_size_t /*boundary*/,
  176. dma_addr_t /*lowaddr*/, dma_addr_t /*highaddr*/,
  177. bus_dma_filter_t*/*filter*/, void */*filterarg*/,
  178. bus_size_t /*maxsize*/, int /*nsegments*/,
  179. bus_size_t /*maxsegsz*/, int /*flags*/,
  180. bus_dma_tag_t */*dma_tagp*/);
  181. void ahc_dma_tag_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/);
  182. int ahc_dmamem_alloc(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
  183. void** /*vaddr*/, int /*flags*/,
  184. bus_dmamap_t* /*mapp*/);
  185. void ahc_dmamem_free(struct ahc_softc *, bus_dma_tag_t /*dmat*/,
  186. void* /*vaddr*/, bus_dmamap_t /*map*/);
  187. void ahc_dmamap_destroy(struct ahc_softc *, bus_dma_tag_t /*tag*/,
  188. bus_dmamap_t /*map*/);
  189. int ahc_dmamap_load(struct ahc_softc *ahc, bus_dma_tag_t /*dmat*/,
  190. bus_dmamap_t /*map*/, void * /*buf*/,
  191. bus_size_t /*buflen*/, bus_dmamap_callback_t *,
  192. void */*callback_arg*/, int /*flags*/);
  193. int ahc_dmamap_unload(struct ahc_softc *, bus_dma_tag_t, bus_dmamap_t);
  194. /*
  195. * Operations performed by ahc_dmamap_sync().
  196. */
  197. #define BUS_DMASYNC_PREREAD 0x01 /* pre-read synchronization */
  198. #define BUS_DMASYNC_POSTREAD 0x02 /* post-read synchronization */
  199. #define BUS_DMASYNC_PREWRITE 0x04 /* pre-write synchronization */
  200. #define BUS_DMASYNC_POSTWRITE 0x08 /* post-write synchronization */
  201. /*
  202. * XXX
  203. * ahc_dmamap_sync is only used on buffers allocated with
  204. * the pci_alloc_consistent() API. Although I'm not sure how
  205. * this works on architectures with a write buffer, Linux does
  206. * not have an API to sync "coherent" memory. Perhaps we need
  207. * to do an mb()?
  208. */
  209. #define ahc_dmamap_sync(ahc, dma_tag, dmamap, offset, len, op)
  210. /************************** Timer DataStructures ******************************/
  211. typedef struct timer_list ahc_timer_t;
  212. /********************************** Includes **********************************/
  213. #ifdef CONFIG_AIC7XXX_REG_PRETTY_PRINT
  214. #define AIC_DEBUG_REGISTERS 1
  215. #else
  216. #define AIC_DEBUG_REGISTERS 0
  217. #endif
  218. #include "aic7xxx.h"
  219. /***************************** Timer Facilities *******************************/
  220. #define ahc_timer_init init_timer
  221. #define ahc_timer_stop del_timer_sync
  222. typedef void ahc_linux_callback_t (u_long);
  223. static __inline void ahc_timer_reset(ahc_timer_t *timer, int usec,
  224. ahc_callback_t *func, void *arg);
  225. static __inline void ahc_scb_timer_reset(struct scb *scb, u_int usec);
  226. static __inline void
  227. ahc_timer_reset(ahc_timer_t *timer, int usec, ahc_callback_t *func, void *arg)
  228. {
  229. struct ahc_softc *ahc;
  230. ahc = (struct ahc_softc *)arg;
  231. del_timer(timer);
  232. timer->data = (u_long)arg;
  233. timer->expires = jiffies + (usec * HZ)/1000000;
  234. timer->function = (ahc_linux_callback_t*)func;
  235. add_timer(timer);
  236. }
  237. static __inline void
  238. ahc_scb_timer_reset(struct scb *scb, u_int usec)
  239. {
  240. mod_timer(&scb->io_ctx->eh_timeout, jiffies + (usec * HZ)/1000000);
  241. }
  242. /***************************** SMP support ************************************/
  243. #include <linux/spinlock.h>
  244. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) || defined(SCSI_HAS_HOST_LOCK))
  245. #define AHC_SCSI_HAS_HOST_LOCK 1
  246. #else
  247. #define AHC_SCSI_HAS_HOST_LOCK 0
  248. #endif
  249. #define AIC7XXX_DRIVER_VERSION "6.2.36"
  250. /**************************** Front End Queues ********************************/
  251. /*
  252. * Data structure used to cast the Linux struct scsi_cmnd to something
  253. * that allows us to use the queue macros. The linux structure has
  254. * plenty of space to hold the links fields as required by the queue
  255. * macros, but the queue macors require them to have the correct type.
  256. */
  257. struct ahc_cmd_internal {
  258. /* Area owned by the Linux scsi layer. */
  259. uint8_t private[offsetof(struct scsi_cmnd, SCp.Status)];
  260. union {
  261. STAILQ_ENTRY(ahc_cmd) ste;
  262. LIST_ENTRY(ahc_cmd) le;
  263. TAILQ_ENTRY(ahc_cmd) tqe;
  264. } links;
  265. uint32_t end;
  266. };
  267. struct ahc_cmd {
  268. union {
  269. struct ahc_cmd_internal icmd;
  270. struct scsi_cmnd scsi_cmd;
  271. } un;
  272. };
  273. #define acmd_icmd(cmd) ((cmd)->un.icmd)
  274. #define acmd_scsi_cmd(cmd) ((cmd)->un.scsi_cmd)
  275. #define acmd_links un.icmd.links
  276. /*************************** Device Data Structures ***************************/
  277. /*
  278. * A per probed device structure used to deal with some error recovery
  279. * scenarios that the Linux mid-layer code just doesn't know how to
  280. * handle. The structure allocated for a device only becomes persistent
  281. * after a successfully completed inquiry command to the target when
  282. * that inquiry data indicates a lun is present.
  283. */
  284. TAILQ_HEAD(ahc_busyq, ahc_cmd);
  285. typedef enum {
  286. AHC_DEV_UNCONFIGURED = 0x01,
  287. AHC_DEV_FREEZE_TIL_EMPTY = 0x02, /* Freeze queue until active == 0 */
  288. AHC_DEV_TIMER_ACTIVE = 0x04, /* Our timer is active */
  289. AHC_DEV_ON_RUN_LIST = 0x08, /* Queued to be run later */
  290. AHC_DEV_Q_BASIC = 0x10, /* Allow basic device queuing */
  291. AHC_DEV_Q_TAGGED = 0x20, /* Allow full SCSI2 command queueing */
  292. AHC_DEV_PERIODIC_OTAG = 0x40, /* Send OTAG to prevent starvation */
  293. AHC_DEV_SLAVE_CONFIGURED = 0x80 /* slave_configure() has been called */
  294. } ahc_linux_dev_flags;
  295. struct ahc_linux_target;
  296. struct ahc_linux_device {
  297. TAILQ_ENTRY(ahc_linux_device) links;
  298. struct ahc_busyq busyq;
  299. /*
  300. * The number of transactions currently
  301. * queued to the device.
  302. */
  303. int active;
  304. /*
  305. * The currently allowed number of
  306. * transactions that can be queued to
  307. * the device. Must be signed for
  308. * conversion from tagged to untagged
  309. * mode where the device may have more
  310. * than one outstanding active transaction.
  311. */
  312. int openings;
  313. /*
  314. * A positive count indicates that this
  315. * device's queue is halted.
  316. */
  317. u_int qfrozen;
  318. /*
  319. * Cumulative command counter.
  320. */
  321. u_long commands_issued;
  322. /*
  323. * The number of tagged transactions when
  324. * running at our current opening level
  325. * that have been successfully received by
  326. * this device since the last QUEUE FULL.
  327. */
  328. u_int tag_success_count;
  329. #define AHC_TAG_SUCCESS_INTERVAL 50
  330. ahc_linux_dev_flags flags;
  331. /*
  332. * Per device timer.
  333. */
  334. struct timer_list timer;
  335. /*
  336. * The high limit for the tags variable.
  337. */
  338. u_int maxtags;
  339. /*
  340. * The computed number of tags outstanding
  341. * at the time of the last QUEUE FULL event.
  342. */
  343. u_int tags_on_last_queuefull;
  344. /*
  345. * How many times we have seen a queue full
  346. * with the same number of tags. This is used
  347. * to stop our adaptive queue depth algorithm
  348. * on devices with a fixed number of tags.
  349. */
  350. u_int last_queuefull_same_count;
  351. #define AHC_LOCK_TAGS_COUNT 50
  352. /*
  353. * How many transactions have been queued
  354. * without the device going idle. We use
  355. * this statistic to determine when to issue
  356. * an ordered tag to prevent transaction
  357. * starvation. This statistic is only updated
  358. * if the AHC_DEV_PERIODIC_OTAG flag is set
  359. * on this device.
  360. */
  361. u_int commands_since_idle_or_otag;
  362. #define AHC_OTAG_THRESH 500
  363. int lun;
  364. Scsi_Device *scsi_device;
  365. struct ahc_linux_target *target;
  366. };
  367. typedef enum {
  368. AHC_INQ_VALID = 0x02,
  369. } ahc_linux_targ_flags;
  370. struct ahc_linux_target {
  371. struct ahc_linux_device *devices[AHC_NUM_LUNS];
  372. int channel;
  373. int target;
  374. int refcount;
  375. struct ahc_transinfo last_tinfo;
  376. struct ahc_softc *ahc;
  377. ahc_linux_targ_flags flags;
  378. struct scsi_inquiry_data *inq_data;
  379. };
  380. /********************* Definitions Required by the Core ***********************/
  381. /*
  382. * Number of SG segments we require. So long as the S/G segments for
  383. * a particular transaction are allocated in a physically contiguous
  384. * manner and are allocated below 4GB, the number of S/G segments is
  385. * unrestricted.
  386. */
  387. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  388. /*
  389. * We dynamically adjust the number of segments in pre-2.5 kernels to
  390. * avoid fragmentation issues in the SCSI mid-layer's private memory
  391. * allocator. See aic7xxx_osm.c ahc_linux_size_nseg() for details.
  392. */
  393. extern u_int ahc_linux_nseg;
  394. #define AHC_NSEG ahc_linux_nseg
  395. #define AHC_LINUX_MIN_NSEG 64
  396. #else
  397. #define AHC_NSEG 128
  398. #endif
  399. /*
  400. * Per-SCB OSM storage.
  401. */
  402. typedef enum {
  403. AHC_UP_EH_SEMAPHORE = 0x1
  404. } ahc_linux_scb_flags;
  405. struct scb_platform_data {
  406. struct ahc_linux_device *dev;
  407. dma_addr_t buf_busaddr;
  408. uint32_t xfer_len;
  409. uint32_t sense_resid; /* Auto-Sense residual */
  410. ahc_linux_scb_flags flags;
  411. };
  412. /*
  413. * Define a structure used for each host adapter. All members are
  414. * aligned on a boundary >= the size of the member to honor the
  415. * alignment restrictions of the various platforms supported by
  416. * this driver.
  417. */
  418. typedef enum {
  419. AHC_RUN_CMPLT_Q_TIMER = 0x10
  420. } ahc_linux_softc_flags;
  421. TAILQ_HEAD(ahc_completeq, ahc_cmd);
  422. struct ahc_platform_data {
  423. /*
  424. * Fields accessed from interrupt context.
  425. */
  426. struct ahc_linux_target *targets[AHC_NUM_TARGETS];
  427. TAILQ_HEAD(, ahc_linux_device) device_runq;
  428. struct ahc_completeq completeq;
  429. spinlock_t spin_lock;
  430. struct tasklet_struct runq_tasklet;
  431. u_int qfrozen;
  432. pid_t dv_pid;
  433. struct timer_list completeq_timer;
  434. struct timer_list reset_timer;
  435. struct semaphore eh_sem;
  436. struct semaphore dv_sem;
  437. struct semaphore dv_cmd_sem; /* XXX This needs to be in
  438. * the target struct
  439. */
  440. struct scsi_device *dv_scsi_dev;
  441. struct Scsi_Host *host; /* pointer to scsi host */
  442. #define AHC_LINUX_NOIRQ ((uint32_t)~0)
  443. uint32_t irq; /* IRQ for this adapter */
  444. uint32_t bios_address;
  445. uint32_t mem_busaddr; /* Mem Base Addr */
  446. uint64_t hw_dma_mask;
  447. ahc_linux_softc_flags flags;
  448. };
  449. /************************** OS Utility Wrappers *******************************/
  450. #define printf printk
  451. #define M_NOWAIT GFP_ATOMIC
  452. #define M_WAITOK 0
  453. #define malloc(size, type, flags) kmalloc(size, flags)
  454. #define free(ptr, type) kfree(ptr)
  455. static __inline void ahc_delay(long);
  456. static __inline void
  457. ahc_delay(long usec)
  458. {
  459. /*
  460. * udelay on Linux can have problems for
  461. * multi-millisecond waits. Wait at most
  462. * 1024us per call.
  463. */
  464. while (usec > 0) {
  465. udelay(usec % 1024);
  466. usec -= 1024;
  467. }
  468. }
  469. /***************************** Low Level I/O **********************************/
  470. static __inline uint8_t ahc_inb(struct ahc_softc * ahc, long port);
  471. static __inline void ahc_outb(struct ahc_softc * ahc, long port, uint8_t val);
  472. static __inline void ahc_outsb(struct ahc_softc * ahc, long port,
  473. uint8_t *, int count);
  474. static __inline void ahc_insb(struct ahc_softc * ahc, long port,
  475. uint8_t *, int count);
  476. static __inline uint8_t
  477. ahc_inb(struct ahc_softc * ahc, long port)
  478. {
  479. uint8_t x;
  480. if (ahc->tag == BUS_SPACE_MEMIO) {
  481. x = readb(ahc->bsh.maddr + port);
  482. } else {
  483. x = inb(ahc->bsh.ioport + port);
  484. }
  485. mb();
  486. return (x);
  487. }
  488. static __inline void
  489. ahc_outb(struct ahc_softc * ahc, long port, uint8_t val)
  490. {
  491. if (ahc->tag == BUS_SPACE_MEMIO) {
  492. writeb(val, ahc->bsh.maddr + port);
  493. } else {
  494. outb(val, ahc->bsh.ioport + port);
  495. }
  496. mb();
  497. }
  498. static __inline void
  499. ahc_outsb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
  500. {
  501. int i;
  502. /*
  503. * There is probably a more efficient way to do this on Linux
  504. * but we don't use this for anything speed critical and this
  505. * should work.
  506. */
  507. for (i = 0; i < count; i++)
  508. ahc_outb(ahc, port, *array++);
  509. }
  510. static __inline void
  511. ahc_insb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
  512. {
  513. int i;
  514. /*
  515. * There is probably a more efficient way to do this on Linux
  516. * but we don't use this for anything speed critical and this
  517. * should work.
  518. */
  519. for (i = 0; i < count; i++)
  520. *array++ = ahc_inb(ahc, port);
  521. }
  522. /**************************** Initialization **********************************/
  523. int ahc_linux_register_host(struct ahc_softc *,
  524. Scsi_Host_Template *);
  525. uint64_t ahc_linux_get_memsize(void);
  526. /*************************** Pretty Printing **********************************/
  527. struct info_str {
  528. char *buffer;
  529. int length;
  530. off_t offset;
  531. int pos;
  532. };
  533. void ahc_format_transinfo(struct info_str *info,
  534. struct ahc_transinfo *tinfo);
  535. /******************************** Locking *************************************/
  536. /* Lock protecting internal data structures */
  537. static __inline void ahc_lockinit(struct ahc_softc *);
  538. static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
  539. static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
  540. /* Lock acquisition and release of the above lock in midlayer entry points. */
  541. static __inline void ahc_midlayer_entrypoint_lock(struct ahc_softc *,
  542. unsigned long *flags);
  543. static __inline void ahc_midlayer_entrypoint_unlock(struct ahc_softc *,
  544. unsigned long *flags);
  545. /* Lock held during command compeletion to the upper layer */
  546. static __inline void ahc_done_lockinit(struct ahc_softc *);
  547. static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
  548. static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
  549. /* Lock held during ahc_list manipulation and ahc softc frees */
  550. extern spinlock_t ahc_list_spinlock;
  551. static __inline void ahc_list_lockinit(void);
  552. static __inline void ahc_list_lock(unsigned long *flags);
  553. static __inline void ahc_list_unlock(unsigned long *flags);
  554. static __inline void
  555. ahc_lockinit(struct ahc_softc *ahc)
  556. {
  557. spin_lock_init(&ahc->platform_data->spin_lock);
  558. }
  559. static __inline void
  560. ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
  561. {
  562. spin_lock_irqsave(&ahc->platform_data->spin_lock, *flags);
  563. }
  564. static __inline void
  565. ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
  566. {
  567. spin_unlock_irqrestore(&ahc->platform_data->spin_lock, *flags);
  568. }
  569. static __inline void
  570. ahc_midlayer_entrypoint_lock(struct ahc_softc *ahc, unsigned long *flags)
  571. {
  572. /*
  573. * In 2.5.X and some 2.4.X versions, the midlayer takes our
  574. * lock just before calling us, so we avoid locking again.
  575. * For other kernel versions, the io_request_lock is taken
  576. * just before our entry point is called. In this case, we
  577. * trade the io_request_lock for our per-softc lock.
  578. */
  579. #if AHC_SCSI_HAS_HOST_LOCK == 0
  580. spin_unlock(&io_request_lock);
  581. spin_lock(&ahc->platform_data->spin_lock);
  582. #endif
  583. }
  584. static __inline void
  585. ahc_midlayer_entrypoint_unlock(struct ahc_softc *ahc, unsigned long *flags)
  586. {
  587. #if AHC_SCSI_HAS_HOST_LOCK == 0
  588. spin_unlock(&ahc->platform_data->spin_lock);
  589. spin_lock(&io_request_lock);
  590. #endif
  591. }
  592. static __inline void
  593. ahc_done_lockinit(struct ahc_softc *ahc)
  594. {
  595. /*
  596. * In 2.5.X, our own lock is held during completions.
  597. * In previous versions, the io_request_lock is used.
  598. * In either case, we can't initialize this lock again.
  599. */
  600. }
  601. static __inline void
  602. ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
  603. {
  604. #if AHC_SCSI_HAS_HOST_LOCK == 0
  605. spin_lock_irqsave(&io_request_lock, *flags);
  606. #endif
  607. }
  608. static __inline void
  609. ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
  610. {
  611. #if AHC_SCSI_HAS_HOST_LOCK == 0
  612. spin_unlock_irqrestore(&io_request_lock, *flags);
  613. #endif
  614. }
  615. static __inline void
  616. ahc_list_lockinit(void)
  617. {
  618. spin_lock_init(&ahc_list_spinlock);
  619. }
  620. static __inline void
  621. ahc_list_lock(unsigned long *flags)
  622. {
  623. spin_lock_irqsave(&ahc_list_spinlock, *flags);
  624. }
  625. static __inline void
  626. ahc_list_unlock(unsigned long *flags)
  627. {
  628. spin_unlock_irqrestore(&ahc_list_spinlock, *flags);
  629. }
  630. /******************************* PCI Definitions ******************************/
  631. /*
  632. * PCIM_xxx: mask to locate subfield in register
  633. * PCIR_xxx: config register offset
  634. * PCIC_xxx: device class
  635. * PCIS_xxx: device subclass
  636. * PCIP_xxx: device programming interface
  637. * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices)
  638. * PCID_xxx: device ID
  639. */
  640. #define PCIR_DEVVENDOR 0x00
  641. #define PCIR_VENDOR 0x00
  642. #define PCIR_DEVICE 0x02
  643. #define PCIR_COMMAND 0x04
  644. #define PCIM_CMD_PORTEN 0x0001
  645. #define PCIM_CMD_MEMEN 0x0002
  646. #define PCIM_CMD_BUSMASTEREN 0x0004
  647. #define PCIM_CMD_MWRICEN 0x0010
  648. #define PCIM_CMD_PERRESPEN 0x0040
  649. #define PCIM_CMD_SERRESPEN 0x0100
  650. #define PCIR_STATUS 0x06
  651. #define PCIR_REVID 0x08
  652. #define PCIR_PROGIF 0x09
  653. #define PCIR_SUBCLASS 0x0a
  654. #define PCIR_CLASS 0x0b
  655. #define PCIR_CACHELNSZ 0x0c
  656. #define PCIR_LATTIMER 0x0d
  657. #define PCIR_HEADERTYPE 0x0e
  658. #define PCIM_MFDEV 0x80
  659. #define PCIR_BIST 0x0f
  660. #define PCIR_CAP_PTR 0x34
  661. /* config registers for header type 0 devices */
  662. #define PCIR_MAPS 0x10
  663. #define PCIR_SUBVEND_0 0x2c
  664. #define PCIR_SUBDEV_0 0x2e
  665. extern struct pci_driver aic7xxx_pci_driver;
  666. typedef enum
  667. {
  668. AHC_POWER_STATE_D0,
  669. AHC_POWER_STATE_D1,
  670. AHC_POWER_STATE_D2,
  671. AHC_POWER_STATE_D3
  672. } ahc_power_state;
  673. /**************************** VL/EISA Routines ********************************/
  674. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) \
  675. && (defined(__i386__) || defined(__alpha__)) \
  676. && (!defined(CONFIG_EISA)))
  677. #define CONFIG_EISA
  678. #endif
  679. #ifdef CONFIG_EISA
  680. extern uint32_t aic7xxx_probe_eisa_vl;
  681. int ahc_linux_eisa_init(void);
  682. void ahc_linux_eisa_exit(void);
  683. int aic7770_map_registers(struct ahc_softc *ahc,
  684. u_int port);
  685. int aic7770_map_int(struct ahc_softc *ahc, u_int irq);
  686. #else
  687. static inline int ahc_linux_eisa_init(void) {
  688. return -ENODEV;
  689. }
  690. static inline void ahc_linux_eisa_exit(void) {
  691. }
  692. #endif
  693. /******************************* PCI Routines *********************************/
  694. #ifdef CONFIG_PCI
  695. int ahc_linux_pci_init(void);
  696. void ahc_linux_pci_exit(void);
  697. int ahc_pci_map_registers(struct ahc_softc *ahc);
  698. int ahc_pci_map_int(struct ahc_softc *ahc);
  699. static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
  700. int reg, int width);
  701. static __inline uint32_t
  702. ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
  703. {
  704. switch (width) {
  705. case 1:
  706. {
  707. uint8_t retval;
  708. pci_read_config_byte(pci, reg, &retval);
  709. return (retval);
  710. }
  711. case 2:
  712. {
  713. uint16_t retval;
  714. pci_read_config_word(pci, reg, &retval);
  715. return (retval);
  716. }
  717. case 4:
  718. {
  719. uint32_t retval;
  720. pci_read_config_dword(pci, reg, &retval);
  721. return (retval);
  722. }
  723. default:
  724. panic("ahc_pci_read_config: Read size too big");
  725. /* NOTREACHED */
  726. return (0);
  727. }
  728. }
  729. static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
  730. int reg, uint32_t value,
  731. int width);
  732. static __inline void
  733. ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
  734. {
  735. switch (width) {
  736. case 1:
  737. pci_write_config_byte(pci, reg, value);
  738. break;
  739. case 2:
  740. pci_write_config_word(pci, reg, value);
  741. break;
  742. case 4:
  743. pci_write_config_dword(pci, reg, value);
  744. break;
  745. default:
  746. panic("ahc_pci_write_config: Write size too big");
  747. /* NOTREACHED */
  748. }
  749. }
  750. static __inline int ahc_get_pci_function(ahc_dev_softc_t);
  751. static __inline int
  752. ahc_get_pci_function(ahc_dev_softc_t pci)
  753. {
  754. return (PCI_FUNC(pci->devfn));
  755. }
  756. static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
  757. static __inline int
  758. ahc_get_pci_slot(ahc_dev_softc_t pci)
  759. {
  760. return (PCI_SLOT(pci->devfn));
  761. }
  762. static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
  763. static __inline int
  764. ahc_get_pci_bus(ahc_dev_softc_t pci)
  765. {
  766. return (pci->bus->number);
  767. }
  768. #else
  769. static inline int ahc_linux_pci_init(void) {
  770. return 0;
  771. }
  772. static inline void ahc_linux_pci_exit(void) {
  773. }
  774. #endif
  775. static __inline void ahc_flush_device_writes(struct ahc_softc *);
  776. static __inline void
  777. ahc_flush_device_writes(struct ahc_softc *ahc)
  778. {
  779. /* XXX Is this sufficient for all architectures??? */
  780. ahc_inb(ahc, INTSTAT);
  781. }
  782. /**************************** Proc FS Support *********************************/
  783. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  784. int ahc_linux_proc_info(char *, char **, off_t, int, int, int);
  785. #else
  786. int ahc_linux_proc_info(struct Scsi_Host *, char *, char **,
  787. off_t, int, int);
  788. #endif
  789. /*************************** Domain Validation ********************************/
  790. /*********************** Transaction Access Wrappers *************************/
  791. static __inline void ahc_cmd_set_transaction_status(Scsi_Cmnd *, uint32_t);
  792. static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
  793. static __inline void ahc_cmd_set_scsi_status(Scsi_Cmnd *, uint32_t);
  794. static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
  795. static __inline uint32_t ahc_cmd_get_transaction_status(Scsi_Cmnd *cmd);
  796. static __inline uint32_t ahc_get_transaction_status(struct scb *);
  797. static __inline uint32_t ahc_cmd_get_scsi_status(Scsi_Cmnd *cmd);
  798. static __inline uint32_t ahc_get_scsi_status(struct scb *);
  799. static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
  800. static __inline u_long ahc_get_transfer_length(struct scb *);
  801. static __inline int ahc_get_transfer_dir(struct scb *);
  802. static __inline void ahc_set_residual(struct scb *, u_long);
  803. static __inline void ahc_set_sense_residual(struct scb *scb, u_long resid);
  804. static __inline u_long ahc_get_residual(struct scb *);
  805. static __inline u_long ahc_get_sense_residual(struct scb *);
  806. static __inline int ahc_perform_autosense(struct scb *);
  807. static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc *,
  808. struct scb *);
  809. static __inline void ahc_notify_xfer_settings_change(struct ahc_softc *,
  810. struct ahc_devinfo *);
  811. static __inline void ahc_platform_scb_free(struct ahc_softc *ahc,
  812. struct scb *scb);
  813. static __inline void ahc_freeze_scb(struct scb *scb);
  814. static __inline
  815. void ahc_cmd_set_transaction_status(Scsi_Cmnd *cmd, uint32_t status)
  816. {
  817. cmd->result &= ~(CAM_STATUS_MASK << 16);
  818. cmd->result |= status << 16;
  819. }
  820. static __inline
  821. void ahc_set_transaction_status(struct scb *scb, uint32_t status)
  822. {
  823. ahc_cmd_set_transaction_status(scb->io_ctx,status);
  824. }
  825. static __inline
  826. void ahc_cmd_set_scsi_status(Scsi_Cmnd *cmd, uint32_t status)
  827. {
  828. cmd->result &= ~0xFFFF;
  829. cmd->result |= status;
  830. }
  831. static __inline
  832. void ahc_set_scsi_status(struct scb *scb, uint32_t status)
  833. {
  834. ahc_cmd_set_scsi_status(scb->io_ctx, status);
  835. }
  836. static __inline
  837. uint32_t ahc_cmd_get_transaction_status(Scsi_Cmnd *cmd)
  838. {
  839. return ((cmd->result >> 16) & CAM_STATUS_MASK);
  840. }
  841. static __inline
  842. uint32_t ahc_get_transaction_status(struct scb *scb)
  843. {
  844. return (ahc_cmd_get_transaction_status(scb->io_ctx));
  845. }
  846. static __inline
  847. uint32_t ahc_cmd_get_scsi_status(Scsi_Cmnd *cmd)
  848. {
  849. return (cmd->result & 0xFFFF);
  850. }
  851. static __inline
  852. uint32_t ahc_get_scsi_status(struct scb *scb)
  853. {
  854. return (ahc_cmd_get_scsi_status(scb->io_ctx));
  855. }
  856. static __inline
  857. void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
  858. {
  859. /*
  860. * Nothing to do for linux as the incoming transaction
  861. * has no concept of tag/non tagged, etc.
  862. */
  863. }
  864. static __inline
  865. u_long ahc_get_transfer_length(struct scb *scb)
  866. {
  867. return (scb->platform_data->xfer_len);
  868. }
  869. static __inline
  870. int ahc_get_transfer_dir(struct scb *scb)
  871. {
  872. return (scb->io_ctx->sc_data_direction);
  873. }
  874. static __inline
  875. void ahc_set_residual(struct scb *scb, u_long resid)
  876. {
  877. scb->io_ctx->resid = resid;
  878. }
  879. static __inline
  880. void ahc_set_sense_residual(struct scb *scb, u_long resid)
  881. {
  882. scb->platform_data->sense_resid = resid;
  883. }
  884. static __inline
  885. u_long ahc_get_residual(struct scb *scb)
  886. {
  887. return (scb->io_ctx->resid);
  888. }
  889. static __inline
  890. u_long ahc_get_sense_residual(struct scb *scb)
  891. {
  892. return (scb->platform_data->sense_resid);
  893. }
  894. static __inline
  895. int ahc_perform_autosense(struct scb *scb)
  896. {
  897. /*
  898. * We always perform autosense in Linux.
  899. * On other platforms this is set on a
  900. * per-transaction basis.
  901. */
  902. return (1);
  903. }
  904. static __inline uint32_t
  905. ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
  906. {
  907. return (sizeof(struct scsi_sense_data));
  908. }
  909. static __inline void
  910. ahc_notify_xfer_settings_change(struct ahc_softc *ahc,
  911. struct ahc_devinfo *devinfo)
  912. {
  913. /* Nothing to do here for linux */
  914. }
  915. static __inline void
  916. ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
  917. {
  918. ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
  919. }
  920. int ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
  921. void ahc_platform_free(struct ahc_softc *ahc);
  922. void ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
  923. static __inline void
  924. ahc_freeze_scb(struct scb *scb)
  925. {
  926. if ((scb->io_ctx->result & (CAM_DEV_QFRZN << 16)) == 0) {
  927. scb->io_ctx->result |= CAM_DEV_QFRZN << 16;
  928. scb->platform_data->dev->qfrozen++;
  929. }
  930. }
  931. void ahc_platform_set_tags(struct ahc_softc *ahc,
  932. struct ahc_devinfo *devinfo, ahc_queue_alg);
  933. int ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
  934. char channel, int lun, u_int tag,
  935. role_t role, uint32_t status);
  936. irqreturn_t
  937. ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs);
  938. void ahc_platform_flushwork(struct ahc_softc *ahc);
  939. int ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
  940. void ahc_done(struct ahc_softc*, struct scb*);
  941. void ahc_send_async(struct ahc_softc *, char channel,
  942. u_int target, u_int lun, ac_code, void *);
  943. void ahc_print_path(struct ahc_softc *, struct scb *);
  944. void ahc_platform_dump_card_state(struct ahc_softc *ahc);
  945. #ifdef CONFIG_PCI
  946. #define AHC_PCI_CONFIG 1
  947. #else
  948. #define AHC_PCI_CONFIG 0
  949. #endif
  950. #define bootverbose aic7xxx_verbose
  951. extern u_int aic7xxx_verbose;
  952. #endif /* _AIC7XXX_LINUX_H_ */