aic7xxx_osm.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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/interrupt.h>
  69. #include <linux/module.h>
  70. #include <asm/byteorder.h>
  71. #include <asm/io.h>
  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. /*
  299. * The number of transactions currently
  300. * queued to the device.
  301. */
  302. int active;
  303. /*
  304. * The currently allowed number of
  305. * transactions that can be queued to
  306. * the device. Must be signed for
  307. * conversion from tagged to untagged
  308. * mode where the device may have more
  309. * than one outstanding active transaction.
  310. */
  311. int openings;
  312. /*
  313. * A positive count indicates that this
  314. * device's queue is halted.
  315. */
  316. u_int qfrozen;
  317. /*
  318. * Cumulative command counter.
  319. */
  320. u_long commands_issued;
  321. /*
  322. * The number of tagged transactions when
  323. * running at our current opening level
  324. * that have been successfully received by
  325. * this device since the last QUEUE FULL.
  326. */
  327. u_int tag_success_count;
  328. #define AHC_TAG_SUCCESS_INTERVAL 50
  329. ahc_linux_dev_flags flags;
  330. /*
  331. * Per device timer.
  332. */
  333. struct timer_list timer;
  334. /*
  335. * The high limit for the tags variable.
  336. */
  337. u_int maxtags;
  338. /*
  339. * The computed number of tags outstanding
  340. * at the time of the last QUEUE FULL event.
  341. */
  342. u_int tags_on_last_queuefull;
  343. /*
  344. * How many times we have seen a queue full
  345. * with the same number of tags. This is used
  346. * to stop our adaptive queue depth algorithm
  347. * on devices with a fixed number of tags.
  348. */
  349. u_int last_queuefull_same_count;
  350. #define AHC_LOCK_TAGS_COUNT 50
  351. /*
  352. * How many transactions have been queued
  353. * without the device going idle. We use
  354. * this statistic to determine when to issue
  355. * an ordered tag to prevent transaction
  356. * starvation. This statistic is only updated
  357. * if the AHC_DEV_PERIODIC_OTAG flag is set
  358. * on this device.
  359. */
  360. u_int commands_since_idle_or_otag;
  361. #define AHC_OTAG_THRESH 500
  362. int lun;
  363. Scsi_Device *scsi_device;
  364. struct ahc_linux_target *target;
  365. };
  366. struct ahc_linux_target {
  367. struct ahc_linux_device *devices[AHC_NUM_LUNS];
  368. int channel;
  369. int target;
  370. int refcount;
  371. struct ahc_transinfo last_tinfo;
  372. struct ahc_softc *ahc;
  373. };
  374. /********************* Definitions Required by the Core ***********************/
  375. /*
  376. * Number of SG segments we require. So long as the S/G segments for
  377. * a particular transaction are allocated in a physically contiguous
  378. * manner and are allocated below 4GB, the number of S/G segments is
  379. * unrestricted.
  380. */
  381. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  382. /*
  383. * We dynamically adjust the number of segments in pre-2.5 kernels to
  384. * avoid fragmentation issues in the SCSI mid-layer's private memory
  385. * allocator. See aic7xxx_osm.c ahc_linux_size_nseg() for details.
  386. */
  387. extern u_int ahc_linux_nseg;
  388. #define AHC_NSEG ahc_linux_nseg
  389. #define AHC_LINUX_MIN_NSEG 64
  390. #else
  391. #define AHC_NSEG 128
  392. #endif
  393. /*
  394. * Per-SCB OSM storage.
  395. */
  396. typedef enum {
  397. AHC_UP_EH_SEMAPHORE = 0x1
  398. } ahc_linux_scb_flags;
  399. struct scb_platform_data {
  400. struct ahc_linux_device *dev;
  401. dma_addr_t buf_busaddr;
  402. uint32_t xfer_len;
  403. uint32_t sense_resid; /* Auto-Sense residual */
  404. ahc_linux_scb_flags flags;
  405. };
  406. /*
  407. * Define a structure used for each host adapter. All members are
  408. * aligned on a boundary >= the size of the member to honor the
  409. * alignment restrictions of the various platforms supported by
  410. * this driver.
  411. */
  412. typedef enum {
  413. AHC_RUN_CMPLT_Q_TIMER = 0x10
  414. } ahc_linux_softc_flags;
  415. TAILQ_HEAD(ahc_completeq, ahc_cmd);
  416. struct ahc_platform_data {
  417. /*
  418. * Fields accessed from interrupt context.
  419. */
  420. struct ahc_linux_target *targets[AHC_NUM_TARGETS];
  421. TAILQ_HEAD(, ahc_linux_device) device_runq;
  422. struct ahc_completeq completeq;
  423. spinlock_t spin_lock;
  424. u_int qfrozen;
  425. struct timer_list completeq_timer;
  426. struct timer_list reset_timer;
  427. struct semaphore eh_sem;
  428. struct Scsi_Host *host; /* pointer to scsi host */
  429. #define AHC_LINUX_NOIRQ ((uint32_t)~0)
  430. uint32_t irq; /* IRQ for this adapter */
  431. uint32_t bios_address;
  432. uint32_t mem_busaddr; /* Mem Base Addr */
  433. uint64_t hw_dma_mask;
  434. ahc_linux_softc_flags flags;
  435. };
  436. /************************** OS Utility Wrappers *******************************/
  437. #define printf printk
  438. #define M_NOWAIT GFP_ATOMIC
  439. #define M_WAITOK 0
  440. #define malloc(size, type, flags) kmalloc(size, flags)
  441. #define free(ptr, type) kfree(ptr)
  442. static __inline void ahc_delay(long);
  443. static __inline void
  444. ahc_delay(long usec)
  445. {
  446. /*
  447. * udelay on Linux can have problems for
  448. * multi-millisecond waits. Wait at most
  449. * 1024us per call.
  450. */
  451. while (usec > 0) {
  452. udelay(usec % 1024);
  453. usec -= 1024;
  454. }
  455. }
  456. /***************************** Low Level I/O **********************************/
  457. static __inline uint8_t ahc_inb(struct ahc_softc * ahc, long port);
  458. static __inline void ahc_outb(struct ahc_softc * ahc, long port, uint8_t val);
  459. static __inline void ahc_outsb(struct ahc_softc * ahc, long port,
  460. uint8_t *, int count);
  461. static __inline void ahc_insb(struct ahc_softc * ahc, long port,
  462. uint8_t *, int count);
  463. static __inline uint8_t
  464. ahc_inb(struct ahc_softc * ahc, long port)
  465. {
  466. uint8_t x;
  467. if (ahc->tag == BUS_SPACE_MEMIO) {
  468. x = readb(ahc->bsh.maddr + port);
  469. } else {
  470. x = inb(ahc->bsh.ioport + port);
  471. }
  472. mb();
  473. return (x);
  474. }
  475. static __inline void
  476. ahc_outb(struct ahc_softc * ahc, long port, uint8_t val)
  477. {
  478. if (ahc->tag == BUS_SPACE_MEMIO) {
  479. writeb(val, ahc->bsh.maddr + port);
  480. } else {
  481. outb(val, ahc->bsh.ioport + port);
  482. }
  483. mb();
  484. }
  485. static __inline void
  486. ahc_outsb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
  487. {
  488. int i;
  489. /*
  490. * There is probably a more efficient way to do this on Linux
  491. * but we don't use this for anything speed critical and this
  492. * should work.
  493. */
  494. for (i = 0; i < count; i++)
  495. ahc_outb(ahc, port, *array++);
  496. }
  497. static __inline void
  498. ahc_insb(struct ahc_softc * ahc, long port, uint8_t *array, int count)
  499. {
  500. int i;
  501. /*
  502. * There is probably a more efficient way to do this on Linux
  503. * but we don't use this for anything speed critical and this
  504. * should work.
  505. */
  506. for (i = 0; i < count; i++)
  507. *array++ = ahc_inb(ahc, port);
  508. }
  509. /**************************** Initialization **********************************/
  510. int ahc_linux_register_host(struct ahc_softc *,
  511. Scsi_Host_Template *);
  512. uint64_t ahc_linux_get_memsize(void);
  513. /*************************** Pretty Printing **********************************/
  514. struct info_str {
  515. char *buffer;
  516. int length;
  517. off_t offset;
  518. int pos;
  519. };
  520. void ahc_format_transinfo(struct info_str *info,
  521. struct ahc_transinfo *tinfo);
  522. /******************************** Locking *************************************/
  523. /* Lock protecting internal data structures */
  524. static __inline void ahc_lockinit(struct ahc_softc *);
  525. static __inline void ahc_lock(struct ahc_softc *, unsigned long *flags);
  526. static __inline void ahc_unlock(struct ahc_softc *, unsigned long *flags);
  527. /* Lock acquisition and release of the above lock in midlayer entry points. */
  528. static __inline void ahc_midlayer_entrypoint_lock(struct ahc_softc *,
  529. unsigned long *flags);
  530. static __inline void ahc_midlayer_entrypoint_unlock(struct ahc_softc *,
  531. unsigned long *flags);
  532. /* Lock held during command compeletion to the upper layer */
  533. static __inline void ahc_done_lockinit(struct ahc_softc *);
  534. static __inline void ahc_done_lock(struct ahc_softc *, unsigned long *flags);
  535. static __inline void ahc_done_unlock(struct ahc_softc *, unsigned long *flags);
  536. /* Lock held during ahc_list manipulation and ahc softc frees */
  537. extern spinlock_t ahc_list_spinlock;
  538. static __inline void ahc_list_lockinit(void);
  539. static __inline void ahc_list_lock(unsigned long *flags);
  540. static __inline void ahc_list_unlock(unsigned long *flags);
  541. static __inline void
  542. ahc_lockinit(struct ahc_softc *ahc)
  543. {
  544. spin_lock_init(&ahc->platform_data->spin_lock);
  545. }
  546. static __inline void
  547. ahc_lock(struct ahc_softc *ahc, unsigned long *flags)
  548. {
  549. spin_lock_irqsave(&ahc->platform_data->spin_lock, *flags);
  550. }
  551. static __inline void
  552. ahc_unlock(struct ahc_softc *ahc, unsigned long *flags)
  553. {
  554. spin_unlock_irqrestore(&ahc->platform_data->spin_lock, *flags);
  555. }
  556. static __inline void
  557. ahc_midlayer_entrypoint_lock(struct ahc_softc *ahc, unsigned long *flags)
  558. {
  559. /*
  560. * In 2.5.X and some 2.4.X versions, the midlayer takes our
  561. * lock just before calling us, so we avoid locking again.
  562. * For other kernel versions, the io_request_lock is taken
  563. * just before our entry point is called. In this case, we
  564. * trade the io_request_lock for our per-softc lock.
  565. */
  566. #if AHC_SCSI_HAS_HOST_LOCK == 0
  567. spin_unlock(&io_request_lock);
  568. spin_lock(&ahc->platform_data->spin_lock);
  569. #endif
  570. }
  571. static __inline void
  572. ahc_midlayer_entrypoint_unlock(struct ahc_softc *ahc, unsigned long *flags)
  573. {
  574. #if AHC_SCSI_HAS_HOST_LOCK == 0
  575. spin_unlock(&ahc->platform_data->spin_lock);
  576. spin_lock(&io_request_lock);
  577. #endif
  578. }
  579. static __inline void
  580. ahc_done_lockinit(struct ahc_softc *ahc)
  581. {
  582. /*
  583. * In 2.5.X, our own lock is held during completions.
  584. * In previous versions, the io_request_lock is used.
  585. * In either case, we can't initialize this lock again.
  586. */
  587. }
  588. static __inline void
  589. ahc_done_lock(struct ahc_softc *ahc, unsigned long *flags)
  590. {
  591. #if AHC_SCSI_HAS_HOST_LOCK == 0
  592. spin_lock_irqsave(&io_request_lock, *flags);
  593. #endif
  594. }
  595. static __inline void
  596. ahc_done_unlock(struct ahc_softc *ahc, unsigned long *flags)
  597. {
  598. #if AHC_SCSI_HAS_HOST_LOCK == 0
  599. spin_unlock_irqrestore(&io_request_lock, *flags);
  600. #endif
  601. }
  602. static __inline void
  603. ahc_list_lockinit(void)
  604. {
  605. spin_lock_init(&ahc_list_spinlock);
  606. }
  607. static __inline void
  608. ahc_list_lock(unsigned long *flags)
  609. {
  610. spin_lock_irqsave(&ahc_list_spinlock, *flags);
  611. }
  612. static __inline void
  613. ahc_list_unlock(unsigned long *flags)
  614. {
  615. spin_unlock_irqrestore(&ahc_list_spinlock, *flags);
  616. }
  617. /******************************* PCI Definitions ******************************/
  618. /*
  619. * PCIM_xxx: mask to locate subfield in register
  620. * PCIR_xxx: config register offset
  621. * PCIC_xxx: device class
  622. * PCIS_xxx: device subclass
  623. * PCIP_xxx: device programming interface
  624. * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices)
  625. * PCID_xxx: device ID
  626. */
  627. #define PCIR_DEVVENDOR 0x00
  628. #define PCIR_VENDOR 0x00
  629. #define PCIR_DEVICE 0x02
  630. #define PCIR_COMMAND 0x04
  631. #define PCIM_CMD_PORTEN 0x0001
  632. #define PCIM_CMD_MEMEN 0x0002
  633. #define PCIM_CMD_BUSMASTEREN 0x0004
  634. #define PCIM_CMD_MWRICEN 0x0010
  635. #define PCIM_CMD_PERRESPEN 0x0040
  636. #define PCIM_CMD_SERRESPEN 0x0100
  637. #define PCIR_STATUS 0x06
  638. #define PCIR_REVID 0x08
  639. #define PCIR_PROGIF 0x09
  640. #define PCIR_SUBCLASS 0x0a
  641. #define PCIR_CLASS 0x0b
  642. #define PCIR_CACHELNSZ 0x0c
  643. #define PCIR_LATTIMER 0x0d
  644. #define PCIR_HEADERTYPE 0x0e
  645. #define PCIM_MFDEV 0x80
  646. #define PCIR_BIST 0x0f
  647. #define PCIR_CAP_PTR 0x34
  648. /* config registers for header type 0 devices */
  649. #define PCIR_MAPS 0x10
  650. #define PCIR_SUBVEND_0 0x2c
  651. #define PCIR_SUBDEV_0 0x2e
  652. extern struct pci_driver aic7xxx_pci_driver;
  653. typedef enum
  654. {
  655. AHC_POWER_STATE_D0,
  656. AHC_POWER_STATE_D1,
  657. AHC_POWER_STATE_D2,
  658. AHC_POWER_STATE_D3
  659. } ahc_power_state;
  660. /**************************** VL/EISA Routines ********************************/
  661. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) \
  662. && (defined(__i386__) || defined(__alpha__)) \
  663. && (!defined(CONFIG_EISA)))
  664. #define CONFIG_EISA
  665. #endif
  666. #ifdef CONFIG_EISA
  667. extern uint32_t aic7xxx_probe_eisa_vl;
  668. int ahc_linux_eisa_init(void);
  669. void ahc_linux_eisa_exit(void);
  670. int aic7770_map_registers(struct ahc_softc *ahc,
  671. u_int port);
  672. int aic7770_map_int(struct ahc_softc *ahc, u_int irq);
  673. #else
  674. static inline int ahc_linux_eisa_init(void) {
  675. return -ENODEV;
  676. }
  677. static inline void ahc_linux_eisa_exit(void) {
  678. }
  679. #endif
  680. /******************************* PCI Routines *********************************/
  681. #ifdef CONFIG_PCI
  682. int ahc_linux_pci_init(void);
  683. void ahc_linux_pci_exit(void);
  684. int ahc_pci_map_registers(struct ahc_softc *ahc);
  685. int ahc_pci_map_int(struct ahc_softc *ahc);
  686. static __inline uint32_t ahc_pci_read_config(ahc_dev_softc_t pci,
  687. int reg, int width);
  688. static __inline uint32_t
  689. ahc_pci_read_config(ahc_dev_softc_t pci, int reg, int width)
  690. {
  691. switch (width) {
  692. case 1:
  693. {
  694. uint8_t retval;
  695. pci_read_config_byte(pci, reg, &retval);
  696. return (retval);
  697. }
  698. case 2:
  699. {
  700. uint16_t retval;
  701. pci_read_config_word(pci, reg, &retval);
  702. return (retval);
  703. }
  704. case 4:
  705. {
  706. uint32_t retval;
  707. pci_read_config_dword(pci, reg, &retval);
  708. return (retval);
  709. }
  710. default:
  711. panic("ahc_pci_read_config: Read size too big");
  712. /* NOTREACHED */
  713. return (0);
  714. }
  715. }
  716. static __inline void ahc_pci_write_config(ahc_dev_softc_t pci,
  717. int reg, uint32_t value,
  718. int width);
  719. static __inline void
  720. ahc_pci_write_config(ahc_dev_softc_t pci, int reg, uint32_t value, int width)
  721. {
  722. switch (width) {
  723. case 1:
  724. pci_write_config_byte(pci, reg, value);
  725. break;
  726. case 2:
  727. pci_write_config_word(pci, reg, value);
  728. break;
  729. case 4:
  730. pci_write_config_dword(pci, reg, value);
  731. break;
  732. default:
  733. panic("ahc_pci_write_config: Write size too big");
  734. /* NOTREACHED */
  735. }
  736. }
  737. static __inline int ahc_get_pci_function(ahc_dev_softc_t);
  738. static __inline int
  739. ahc_get_pci_function(ahc_dev_softc_t pci)
  740. {
  741. return (PCI_FUNC(pci->devfn));
  742. }
  743. static __inline int ahc_get_pci_slot(ahc_dev_softc_t);
  744. static __inline int
  745. ahc_get_pci_slot(ahc_dev_softc_t pci)
  746. {
  747. return (PCI_SLOT(pci->devfn));
  748. }
  749. static __inline int ahc_get_pci_bus(ahc_dev_softc_t);
  750. static __inline int
  751. ahc_get_pci_bus(ahc_dev_softc_t pci)
  752. {
  753. return (pci->bus->number);
  754. }
  755. #else
  756. static inline int ahc_linux_pci_init(void) {
  757. return 0;
  758. }
  759. static inline void ahc_linux_pci_exit(void) {
  760. }
  761. #endif
  762. static __inline void ahc_flush_device_writes(struct ahc_softc *);
  763. static __inline void
  764. ahc_flush_device_writes(struct ahc_softc *ahc)
  765. {
  766. /* XXX Is this sufficient for all architectures??? */
  767. ahc_inb(ahc, INTSTAT);
  768. }
  769. /**************************** Proc FS Support *********************************/
  770. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  771. int ahc_linux_proc_info(char *, char **, off_t, int, int, int);
  772. #else
  773. int ahc_linux_proc_info(struct Scsi_Host *, char *, char **,
  774. off_t, int, int);
  775. #endif
  776. /*************************** Domain Validation ********************************/
  777. /*********************** Transaction Access Wrappers *************************/
  778. static __inline void ahc_cmd_set_transaction_status(Scsi_Cmnd *, uint32_t);
  779. static __inline void ahc_set_transaction_status(struct scb *, uint32_t);
  780. static __inline void ahc_cmd_set_scsi_status(Scsi_Cmnd *, uint32_t);
  781. static __inline void ahc_set_scsi_status(struct scb *, uint32_t);
  782. static __inline uint32_t ahc_cmd_get_transaction_status(Scsi_Cmnd *cmd);
  783. static __inline uint32_t ahc_get_transaction_status(struct scb *);
  784. static __inline uint32_t ahc_cmd_get_scsi_status(Scsi_Cmnd *cmd);
  785. static __inline uint32_t ahc_get_scsi_status(struct scb *);
  786. static __inline void ahc_set_transaction_tag(struct scb *, int, u_int);
  787. static __inline u_long ahc_get_transfer_length(struct scb *);
  788. static __inline int ahc_get_transfer_dir(struct scb *);
  789. static __inline void ahc_set_residual(struct scb *, u_long);
  790. static __inline void ahc_set_sense_residual(struct scb *scb, u_long resid);
  791. static __inline u_long ahc_get_residual(struct scb *);
  792. static __inline u_long ahc_get_sense_residual(struct scb *);
  793. static __inline int ahc_perform_autosense(struct scb *);
  794. static __inline uint32_t ahc_get_sense_bufsize(struct ahc_softc *,
  795. struct scb *);
  796. static __inline void ahc_notify_xfer_settings_change(struct ahc_softc *,
  797. struct ahc_devinfo *);
  798. static __inline void ahc_platform_scb_free(struct ahc_softc *ahc,
  799. struct scb *scb);
  800. static __inline void ahc_freeze_scb(struct scb *scb);
  801. static __inline
  802. void ahc_cmd_set_transaction_status(Scsi_Cmnd *cmd, uint32_t status)
  803. {
  804. cmd->result &= ~(CAM_STATUS_MASK << 16);
  805. cmd->result |= status << 16;
  806. }
  807. static __inline
  808. void ahc_set_transaction_status(struct scb *scb, uint32_t status)
  809. {
  810. ahc_cmd_set_transaction_status(scb->io_ctx,status);
  811. }
  812. static __inline
  813. void ahc_cmd_set_scsi_status(Scsi_Cmnd *cmd, uint32_t status)
  814. {
  815. cmd->result &= ~0xFFFF;
  816. cmd->result |= status;
  817. }
  818. static __inline
  819. void ahc_set_scsi_status(struct scb *scb, uint32_t status)
  820. {
  821. ahc_cmd_set_scsi_status(scb->io_ctx, status);
  822. }
  823. static __inline
  824. uint32_t ahc_cmd_get_transaction_status(Scsi_Cmnd *cmd)
  825. {
  826. return ((cmd->result >> 16) & CAM_STATUS_MASK);
  827. }
  828. static __inline
  829. uint32_t ahc_get_transaction_status(struct scb *scb)
  830. {
  831. return (ahc_cmd_get_transaction_status(scb->io_ctx));
  832. }
  833. static __inline
  834. uint32_t ahc_cmd_get_scsi_status(Scsi_Cmnd *cmd)
  835. {
  836. return (cmd->result & 0xFFFF);
  837. }
  838. static __inline
  839. uint32_t ahc_get_scsi_status(struct scb *scb)
  840. {
  841. return (ahc_cmd_get_scsi_status(scb->io_ctx));
  842. }
  843. static __inline
  844. void ahc_set_transaction_tag(struct scb *scb, int enabled, u_int type)
  845. {
  846. /*
  847. * Nothing to do for linux as the incoming transaction
  848. * has no concept of tag/non tagged, etc.
  849. */
  850. }
  851. static __inline
  852. u_long ahc_get_transfer_length(struct scb *scb)
  853. {
  854. return (scb->platform_data->xfer_len);
  855. }
  856. static __inline
  857. int ahc_get_transfer_dir(struct scb *scb)
  858. {
  859. return (scb->io_ctx->sc_data_direction);
  860. }
  861. static __inline
  862. void ahc_set_residual(struct scb *scb, u_long resid)
  863. {
  864. scb->io_ctx->resid = resid;
  865. }
  866. static __inline
  867. void ahc_set_sense_residual(struct scb *scb, u_long resid)
  868. {
  869. scb->platform_data->sense_resid = resid;
  870. }
  871. static __inline
  872. u_long ahc_get_residual(struct scb *scb)
  873. {
  874. return (scb->io_ctx->resid);
  875. }
  876. static __inline
  877. u_long ahc_get_sense_residual(struct scb *scb)
  878. {
  879. return (scb->platform_data->sense_resid);
  880. }
  881. static __inline
  882. int ahc_perform_autosense(struct scb *scb)
  883. {
  884. /*
  885. * We always perform autosense in Linux.
  886. * On other platforms this is set on a
  887. * per-transaction basis.
  888. */
  889. return (1);
  890. }
  891. static __inline uint32_t
  892. ahc_get_sense_bufsize(struct ahc_softc *ahc, struct scb *scb)
  893. {
  894. return (sizeof(struct scsi_sense_data));
  895. }
  896. static __inline void
  897. ahc_notify_xfer_settings_change(struct ahc_softc *ahc,
  898. struct ahc_devinfo *devinfo)
  899. {
  900. /* Nothing to do here for linux */
  901. }
  902. static __inline void
  903. ahc_platform_scb_free(struct ahc_softc *ahc, struct scb *scb)
  904. {
  905. ahc->flags &= ~AHC_RESOURCE_SHORTAGE;
  906. }
  907. int ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg);
  908. void ahc_platform_free(struct ahc_softc *ahc);
  909. void ahc_platform_freeze_devq(struct ahc_softc *ahc, struct scb *scb);
  910. static __inline void
  911. ahc_freeze_scb(struct scb *scb)
  912. {
  913. if ((scb->io_ctx->result & (CAM_DEV_QFRZN << 16)) == 0) {
  914. scb->io_ctx->result |= CAM_DEV_QFRZN << 16;
  915. scb->platform_data->dev->qfrozen++;
  916. }
  917. }
  918. void ahc_platform_set_tags(struct ahc_softc *ahc,
  919. struct ahc_devinfo *devinfo, ahc_queue_alg);
  920. int ahc_platform_abort_scbs(struct ahc_softc *ahc, int target,
  921. char channel, int lun, u_int tag,
  922. role_t role, uint32_t status);
  923. irqreturn_t
  924. ahc_linux_isr(int irq, void *dev_id, struct pt_regs * regs);
  925. void ahc_platform_flushwork(struct ahc_softc *ahc);
  926. int ahc_softc_comp(struct ahc_softc *, struct ahc_softc *);
  927. void ahc_done(struct ahc_softc*, struct scb*);
  928. void ahc_send_async(struct ahc_softc *, char channel,
  929. u_int target, u_int lun, ac_code, void *);
  930. void ahc_print_path(struct ahc_softc *, struct scb *);
  931. void ahc_platform_dump_card_state(struct ahc_softc *ahc);
  932. #ifdef CONFIG_PCI
  933. #define AHC_PCI_CONFIG 1
  934. #else
  935. #define AHC_PCI_CONFIG 0
  936. #endif
  937. #define bootverbose aic7xxx_verbose
  938. extern u_int aic7xxx_verbose;
  939. #endif /* _AIC7XXX_LINUX_H_ */