aic79xx_osm.h 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*
  2. * Adaptec AIC79xx device driver for Linux.
  3. *
  4. * Copyright (c) 2000-2001 Adaptec Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  14. * substantially similar to the "NO WARRANTY" disclaimer below
  15. * ("Disclaimer") and any redistribution must be conditioned upon
  16. * including a substantially similar Disclaimer requirement for further
  17. * binary redistribution.
  18. * 3. Neither the names of the above-listed copyright holders nor the names
  19. * of any contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * Alternatively, this software may be distributed under the terms of the
  23. * GNU General Public License ("GPL") version 2 as published by the Free
  24. * Software Foundation.
  25. *
  26. * NO WARRANTY
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  33. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  35. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  36. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGES.
  38. *
  39. * $Id: //depot/aic7xxx/linux/drivers/scsi/aic7xxx/aic79xx_osm.h#137 $
  40. *
  41. */
  42. #ifndef _AIC79XX_LINUX_H_
  43. #define _AIC79XX_LINUX_H_
  44. #include <linux/types.h>
  45. #include <linux/blkdev.h>
  46. #include <linux/delay.h>
  47. #include <linux/ioport.h>
  48. #include <linux/pci.h>
  49. #include <linux/smp_lock.h>
  50. #include <linux/version.h>
  51. #include <linux/module.h>
  52. #include <asm/byteorder.h>
  53. #include <asm/io.h>
  54. #include <linux/interrupt.h> /* For tasklet support. */
  55. #include <linux/config.h>
  56. #include <linux/slab.h>
  57. /* Core SCSI definitions */
  58. #define AIC_LIB_PREFIX ahd
  59. #include "scsi.h"
  60. #include <scsi/scsi_host.h>
  61. /* Name space conflict with BSD queue macros */
  62. #ifdef LIST_HEAD
  63. #undef LIST_HEAD
  64. #endif
  65. #include "cam.h"
  66. #include "queue.h"
  67. #include "scsi_message.h"
  68. #include "scsi_iu.h"
  69. #include "aiclib.h"
  70. /*********************************** Debugging ********************************/
  71. #ifdef CONFIG_AIC79XX_DEBUG_ENABLE
  72. #ifdef CONFIG_AIC79XX_DEBUG_MASK
  73. #define AHD_DEBUG 1
  74. #define AHD_DEBUG_OPTS CONFIG_AIC79XX_DEBUG_MASK
  75. #else
  76. /*
  77. * Compile in debugging code, but do not enable any printfs.
  78. */
  79. #define AHD_DEBUG 1
  80. #define AHD_DEBUG_OPTS 0
  81. #endif
  82. /* No debugging code. */
  83. #endif
  84. /********************************** Misc Macros *******************************/
  85. #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
  86. #define powerof2(x) ((((x)-1)&(x))==0)
  87. /************************* Forward Declarations *******************************/
  88. struct ahd_softc;
  89. typedef struct pci_dev *ahd_dev_softc_t;
  90. typedef Scsi_Cmnd *ahd_io_ctx_t;
  91. /******************************* Byte Order ***********************************/
  92. #define ahd_htobe16(x) cpu_to_be16(x)
  93. #define ahd_htobe32(x) cpu_to_be32(x)
  94. #define ahd_htobe64(x) cpu_to_be64(x)
  95. #define ahd_htole16(x) cpu_to_le16(x)
  96. #define ahd_htole32(x) cpu_to_le32(x)
  97. #define ahd_htole64(x) cpu_to_le64(x)
  98. #define ahd_be16toh(x) be16_to_cpu(x)
  99. #define ahd_be32toh(x) be32_to_cpu(x)
  100. #define ahd_be64toh(x) be64_to_cpu(x)
  101. #define ahd_le16toh(x) le16_to_cpu(x)
  102. #define ahd_le32toh(x) le32_to_cpu(x)
  103. #define ahd_le64toh(x) le64_to_cpu(x)
  104. /************************* Configuration Data *********************************/
  105. extern uint32_t aic79xx_allow_memio;
  106. extern int aic79xx_detect_complete;
  107. extern Scsi_Host_Template aic79xx_driver_template;
  108. /***************************** Bus Space/DMA **********************************/
  109. typedef uint32_t bus_size_t;
  110. typedef enum {
  111. BUS_SPACE_MEMIO,
  112. BUS_SPACE_PIO
  113. } bus_space_tag_t;
  114. typedef union {
  115. u_long ioport;
  116. volatile uint8_t __iomem *maddr;
  117. } bus_space_handle_t;
  118. typedef struct bus_dma_segment
  119. {
  120. dma_addr_t ds_addr;
  121. bus_size_t ds_len;
  122. } bus_dma_segment_t;
  123. struct ahd_linux_dma_tag
  124. {
  125. bus_size_t alignment;
  126. bus_size_t boundary;
  127. bus_size_t maxsize;
  128. };
  129. typedef struct ahd_linux_dma_tag* bus_dma_tag_t;
  130. struct ahd_linux_dmamap
  131. {
  132. dma_addr_t bus_addr;
  133. };
  134. typedef struct ahd_linux_dmamap* bus_dmamap_t;
  135. typedef int bus_dma_filter_t(void*, dma_addr_t);
  136. typedef void bus_dmamap_callback_t(void *, bus_dma_segment_t *, int, int);
  137. #define BUS_DMA_WAITOK 0x0
  138. #define BUS_DMA_NOWAIT 0x1
  139. #define BUS_DMA_ALLOCNOW 0x2
  140. #define BUS_DMA_LOAD_SEGS 0x4 /*
  141. * Argument is an S/G list not
  142. * a single buffer.
  143. */
  144. #define BUS_SPACE_MAXADDR 0xFFFFFFFF
  145. #define BUS_SPACE_MAXADDR_32BIT 0xFFFFFFFF
  146. #define BUS_SPACE_MAXSIZE_32BIT 0xFFFFFFFF
  147. int ahd_dma_tag_create(struct ahd_softc *, bus_dma_tag_t /*parent*/,
  148. bus_size_t /*alignment*/, bus_size_t /*boundary*/,
  149. dma_addr_t /*lowaddr*/, dma_addr_t /*highaddr*/,
  150. bus_dma_filter_t*/*filter*/, void */*filterarg*/,
  151. bus_size_t /*maxsize*/, int /*nsegments*/,
  152. bus_size_t /*maxsegsz*/, int /*flags*/,
  153. bus_dma_tag_t */*dma_tagp*/);
  154. void ahd_dma_tag_destroy(struct ahd_softc *, bus_dma_tag_t /*tag*/);
  155. int ahd_dmamem_alloc(struct ahd_softc *, bus_dma_tag_t /*dmat*/,
  156. void** /*vaddr*/, int /*flags*/,
  157. bus_dmamap_t* /*mapp*/);
  158. void ahd_dmamem_free(struct ahd_softc *, bus_dma_tag_t /*dmat*/,
  159. void* /*vaddr*/, bus_dmamap_t /*map*/);
  160. void ahd_dmamap_destroy(struct ahd_softc *, bus_dma_tag_t /*tag*/,
  161. bus_dmamap_t /*map*/);
  162. int ahd_dmamap_load(struct ahd_softc *ahd, bus_dma_tag_t /*dmat*/,
  163. bus_dmamap_t /*map*/, void * /*buf*/,
  164. bus_size_t /*buflen*/, bus_dmamap_callback_t *,
  165. void */*callback_arg*/, int /*flags*/);
  166. int ahd_dmamap_unload(struct ahd_softc *, bus_dma_tag_t, bus_dmamap_t);
  167. /*
  168. * Operations performed by ahd_dmamap_sync().
  169. */
  170. #define BUS_DMASYNC_PREREAD 0x01 /* pre-read synchronization */
  171. #define BUS_DMASYNC_POSTREAD 0x02 /* post-read synchronization */
  172. #define BUS_DMASYNC_PREWRITE 0x04 /* pre-write synchronization */
  173. #define BUS_DMASYNC_POSTWRITE 0x08 /* post-write synchronization */
  174. /*
  175. * XXX
  176. * ahd_dmamap_sync is only used on buffers allocated with
  177. * the pci_alloc_consistent() API. Although I'm not sure how
  178. * this works on architectures with a write buffer, Linux does
  179. * not have an API to sync "coherent" memory. Perhaps we need
  180. * to do an mb()?
  181. */
  182. #define ahd_dmamap_sync(ahd, dma_tag, dmamap, offset, len, op)
  183. /************************** Timer DataStructures ******************************/
  184. typedef struct timer_list ahd_timer_t;
  185. /********************************** Includes **********************************/
  186. #ifdef CONFIG_AIC79XX_REG_PRETTY_PRINT
  187. #define AIC_DEBUG_REGISTERS 1
  188. #else
  189. #define AIC_DEBUG_REGISTERS 0
  190. #endif
  191. #include "aic79xx.h"
  192. /***************************** Timer Facilities *******************************/
  193. #define ahd_timer_init init_timer
  194. #define ahd_timer_stop del_timer_sync
  195. typedef void ahd_linux_callback_t (u_long);
  196. static __inline void ahd_timer_reset(ahd_timer_t *timer, u_int usec,
  197. ahd_callback_t *func, void *arg);
  198. static __inline void ahd_scb_timer_reset(struct scb *scb, u_int usec);
  199. static __inline void
  200. ahd_timer_reset(ahd_timer_t *timer, u_int usec, ahd_callback_t *func, void *arg)
  201. {
  202. struct ahd_softc *ahd;
  203. ahd = (struct ahd_softc *)arg;
  204. del_timer(timer);
  205. timer->data = (u_long)arg;
  206. timer->expires = jiffies + (usec * HZ)/1000000;
  207. timer->function = (ahd_linux_callback_t*)func;
  208. add_timer(timer);
  209. }
  210. static __inline void
  211. ahd_scb_timer_reset(struct scb *scb, u_int usec)
  212. {
  213. mod_timer(&scb->io_ctx->eh_timeout, jiffies + (usec * HZ)/1000000);
  214. }
  215. /***************************** SMP support ************************************/
  216. #include <linux/spinlock.h>
  217. #define AHD_SCSI_HAS_HOST_LOCK 1
  218. #define AIC79XX_DRIVER_VERSION "1.3.11"
  219. /**************************** Front End Queues ********************************/
  220. /*
  221. * Data structure used to cast the Linux struct scsi_cmnd to something
  222. * that allows us to use the queue macros. The linux structure has
  223. * plenty of space to hold the links fields as required by the queue
  224. * macros, but the queue macors require them to have the correct type.
  225. */
  226. struct ahd_cmd_internal {
  227. /* Area owned by the Linux scsi layer. */
  228. uint8_t private[offsetof(struct scsi_cmnd, SCp.Status)];
  229. union {
  230. STAILQ_ENTRY(ahd_cmd) ste;
  231. LIST_ENTRY(ahd_cmd) le;
  232. TAILQ_ENTRY(ahd_cmd) tqe;
  233. } links;
  234. uint32_t end;
  235. };
  236. struct ahd_cmd {
  237. union {
  238. struct ahd_cmd_internal icmd;
  239. struct scsi_cmnd scsi_cmd;
  240. } un;
  241. };
  242. #define acmd_icmd(cmd) ((cmd)->un.icmd)
  243. #define acmd_scsi_cmd(cmd) ((cmd)->un.scsi_cmd)
  244. #define acmd_links un.icmd.links
  245. /*************************** Device Data Structures ***************************/
  246. /*
  247. * A per probed device structure used to deal with some error recovery
  248. * scenarios that the Linux mid-layer code just doesn't know how to
  249. * handle. The structure allocated for a device only becomes persistent
  250. * after a successfully completed inquiry command to the target when
  251. * that inquiry data indicates a lun is present.
  252. */
  253. typedef enum {
  254. AHD_DEV_UNCONFIGURED = 0x01,
  255. AHD_DEV_FREEZE_TIL_EMPTY = 0x02, /* Freeze queue until active == 0 */
  256. AHD_DEV_TIMER_ACTIVE = 0x04, /* Our timer is active */
  257. AHD_DEV_Q_BASIC = 0x10, /* Allow basic device queuing */
  258. AHD_DEV_Q_TAGGED = 0x20, /* Allow full SCSI2 command queueing */
  259. AHD_DEV_PERIODIC_OTAG = 0x40, /* Send OTAG to prevent starvation */
  260. AHD_DEV_SLAVE_CONFIGURED = 0x80 /* slave_configure() has been called */
  261. } ahd_linux_dev_flags;
  262. struct ahd_linux_target;
  263. struct ahd_linux_device {
  264. TAILQ_ENTRY(ahd_linux_device) links;
  265. /*
  266. * The number of transactions currently
  267. * queued to the device.
  268. */
  269. int active;
  270. /*
  271. * The currently allowed number of
  272. * transactions that can be queued to
  273. * the device. Must be signed for
  274. * conversion from tagged to untagged
  275. * mode where the device may have more
  276. * than one outstanding active transaction.
  277. */
  278. int openings;
  279. /*
  280. * A positive count indicates that this
  281. * device's queue is halted.
  282. */
  283. u_int qfrozen;
  284. /*
  285. * Cumulative command counter.
  286. */
  287. u_long commands_issued;
  288. /*
  289. * The number of tagged transactions when
  290. * running at our current opening level
  291. * that have been successfully received by
  292. * this device since the last QUEUE FULL.
  293. */
  294. u_int tag_success_count;
  295. #define AHD_TAG_SUCCESS_INTERVAL 50
  296. ahd_linux_dev_flags flags;
  297. /*
  298. * Per device timer.
  299. */
  300. struct timer_list timer;
  301. /*
  302. * The high limit for the tags variable.
  303. */
  304. u_int maxtags;
  305. /*
  306. * The computed number of tags outstanding
  307. * at the time of the last QUEUE FULL event.
  308. */
  309. u_int tags_on_last_queuefull;
  310. /*
  311. * How many times we have seen a queue full
  312. * with the same number of tags. This is used
  313. * to stop our adaptive queue depth algorithm
  314. * on devices with a fixed number of tags.
  315. */
  316. u_int last_queuefull_same_count;
  317. #define AHD_LOCK_TAGS_COUNT 50
  318. /*
  319. * How many transactions have been queued
  320. * without the device going idle. We use
  321. * this statistic to determine when to issue
  322. * an ordered tag to prevent transaction
  323. * starvation. This statistic is only updated
  324. * if the AHD_DEV_PERIODIC_OTAG flag is set
  325. * on this device.
  326. */
  327. u_int commands_since_idle_or_otag;
  328. #define AHD_OTAG_THRESH 500
  329. int lun;
  330. Scsi_Device *scsi_device;
  331. struct ahd_linux_target *target;
  332. };
  333. typedef enum {
  334. AHD_DV_REQUIRED = 0x01,
  335. AHD_INQ_VALID = 0x02,
  336. AHD_BASIC_DV = 0x04,
  337. AHD_ENHANCED_DV = 0x08
  338. } ahd_linux_targ_flags;
  339. /* DV States */
  340. typedef enum {
  341. AHD_DV_STATE_EXIT = 0,
  342. AHD_DV_STATE_INQ_SHORT_ASYNC,
  343. AHD_DV_STATE_INQ_ASYNC,
  344. AHD_DV_STATE_INQ_ASYNC_VERIFY,
  345. AHD_DV_STATE_TUR,
  346. AHD_DV_STATE_REBD,
  347. AHD_DV_STATE_INQ_VERIFY,
  348. AHD_DV_STATE_WEB,
  349. AHD_DV_STATE_REB,
  350. AHD_DV_STATE_SU,
  351. AHD_DV_STATE_BUSY
  352. } ahd_dv_state;
  353. struct ahd_linux_target {
  354. struct ahd_linux_device *devices[AHD_NUM_LUNS];
  355. int channel;
  356. int target;
  357. int refcount;
  358. struct ahd_transinfo last_tinfo;
  359. struct ahd_softc *ahd;
  360. ahd_linux_targ_flags flags;
  361. struct scsi_inquiry_data *inq_data;
  362. /*
  363. * The next "fallback" period to use for narrow/wide transfers.
  364. */
  365. uint8_t dv_next_narrow_period;
  366. uint8_t dv_next_wide_period;
  367. uint8_t dv_max_width;
  368. uint8_t dv_max_ppr_options;
  369. uint8_t dv_last_ppr_options;
  370. u_int dv_echo_size;
  371. ahd_dv_state dv_state;
  372. u_int dv_state_retry;
  373. uint8_t *dv_buffer;
  374. uint8_t *dv_buffer1;
  375. /*
  376. * Cumulative counter of errors.
  377. */
  378. u_long errors_detected;
  379. u_long cmds_since_error;
  380. };
  381. /********************* Definitions Required by the Core ***********************/
  382. /*
  383. * Number of SG segments we require. So long as the S/G segments for
  384. * a particular transaction are allocated in a physically contiguous
  385. * manner and are allocated below 4GB, the number of S/G segments is
  386. * unrestricted.
  387. */
  388. #define AHD_NSEG 128
  389. /*
  390. * Per-SCB OSM storage.
  391. */
  392. typedef enum {
  393. AHD_SCB_UP_EH_SEM = 0x1
  394. } ahd_linux_scb_flags;
  395. struct scb_platform_data {
  396. struct ahd_linux_device *dev;
  397. dma_addr_t buf_busaddr;
  398. uint32_t xfer_len;
  399. uint32_t sense_resid; /* Auto-Sense residual */
  400. ahd_linux_scb_flags flags;
  401. };
  402. /*
  403. * Define a structure used for each host adapter. All members are
  404. * aligned on a boundary >= the size of the member to honor the
  405. * alignment restrictions of the various platforms supported by
  406. * this driver.
  407. */
  408. typedef enum {
  409. AHD_DV_WAIT_SIMQ_EMPTY = 0x01,
  410. AHD_DV_WAIT_SIMQ_RELEASE = 0x02,
  411. AHD_DV_ACTIVE = 0x04,
  412. AHD_DV_SHUTDOWN = 0x08,
  413. AHD_RUN_CMPLT_Q_TIMER = 0x10
  414. } ahd_linux_softc_flags;
  415. TAILQ_HEAD(ahd_completeq, ahd_cmd);
  416. struct ahd_platform_data {
  417. /*
  418. * Fields accessed from interrupt context.
  419. */
  420. struct ahd_linux_target *targets[AHD_NUM_TARGETS];
  421. struct ahd_completeq completeq;
  422. spinlock_t spin_lock;
  423. u_int qfrozen;
  424. pid_t dv_pid;
  425. struct timer_list completeq_timer;
  426. struct timer_list reset_timer;
  427. struct timer_list stats_timer;
  428. struct semaphore eh_sem;
  429. struct semaphore dv_sem;
  430. struct semaphore dv_cmd_sem; /* XXX This needs to be in
  431. * the target struct
  432. */
  433. struct scsi_device *dv_scsi_dev;
  434. struct Scsi_Host *host; /* pointer to scsi host */
  435. #define AHD_LINUX_NOIRQ ((uint32_t)~0)
  436. uint32_t irq; /* IRQ for this adapter */
  437. uint32_t bios_address;
  438. uint32_t mem_busaddr; /* Mem Base Addr */
  439. uint64_t hw_dma_mask;
  440. ahd_linux_softc_flags flags;
  441. };
  442. /************************** OS Utility Wrappers *******************************/
  443. #define printf printk
  444. #define M_NOWAIT GFP_ATOMIC
  445. #define M_WAITOK 0
  446. #define malloc(size, type, flags) kmalloc(size, flags)
  447. #define free(ptr, type) kfree(ptr)
  448. static __inline void ahd_delay(long);
  449. static __inline void
  450. ahd_delay(long usec)
  451. {
  452. /*
  453. * udelay on Linux can have problems for
  454. * multi-millisecond waits. Wait at most
  455. * 1024us per call.
  456. */
  457. while (usec > 0) {
  458. udelay(usec % 1024);
  459. usec -= 1024;
  460. }
  461. }
  462. /***************************** Low Level I/O **********************************/
  463. static __inline uint8_t ahd_inb(struct ahd_softc * ahd, long port);
  464. static __inline uint16_t ahd_inw_atomic(struct ahd_softc * ahd, long port);
  465. static __inline void ahd_outb(struct ahd_softc * ahd, long port, uint8_t val);
  466. static __inline void ahd_outw_atomic(struct ahd_softc * ahd,
  467. long port, uint16_t val);
  468. static __inline void ahd_outsb(struct ahd_softc * ahd, long port,
  469. uint8_t *, int count);
  470. static __inline void ahd_insb(struct ahd_softc * ahd, long port,
  471. uint8_t *, int count);
  472. static __inline uint8_t
  473. ahd_inb(struct ahd_softc * ahd, long port)
  474. {
  475. uint8_t x;
  476. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  477. x = readb(ahd->bshs[0].maddr + port);
  478. } else {
  479. x = inb(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
  480. }
  481. mb();
  482. return (x);
  483. }
  484. static __inline uint16_t
  485. ahd_inw_atomic(struct ahd_softc * ahd, long port)
  486. {
  487. uint8_t x;
  488. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  489. x = readw(ahd->bshs[0].maddr + port);
  490. } else {
  491. x = inw(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
  492. }
  493. mb();
  494. return (x);
  495. }
  496. static __inline void
  497. ahd_outb(struct ahd_softc * ahd, long port, uint8_t val)
  498. {
  499. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  500. writeb(val, ahd->bshs[0].maddr + port);
  501. } else {
  502. outb(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
  503. }
  504. mb();
  505. }
  506. static __inline void
  507. ahd_outw_atomic(struct ahd_softc * ahd, long port, uint16_t val)
  508. {
  509. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  510. writew(val, ahd->bshs[0].maddr + port);
  511. } else {
  512. outw(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
  513. }
  514. mb();
  515. }
  516. static __inline void
  517. ahd_outsb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
  518. {
  519. int i;
  520. /*
  521. * There is probably a more efficient way to do this on Linux
  522. * but we don't use this for anything speed critical and this
  523. * should work.
  524. */
  525. for (i = 0; i < count; i++)
  526. ahd_outb(ahd, port, *array++);
  527. }
  528. static __inline void
  529. ahd_insb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
  530. {
  531. int i;
  532. /*
  533. * There is probably a more efficient way to do this on Linux
  534. * but we don't use this for anything speed critical and this
  535. * should work.
  536. */
  537. for (i = 0; i < count; i++)
  538. *array++ = ahd_inb(ahd, port);
  539. }
  540. /**************************** Initialization **********************************/
  541. int ahd_linux_register_host(struct ahd_softc *,
  542. Scsi_Host_Template *);
  543. uint64_t ahd_linux_get_memsize(void);
  544. /*************************** Pretty Printing **********************************/
  545. struct info_str {
  546. char *buffer;
  547. int length;
  548. off_t offset;
  549. int pos;
  550. };
  551. void ahd_format_transinfo(struct info_str *info,
  552. struct ahd_transinfo *tinfo);
  553. /******************************** Locking *************************************/
  554. /* Lock protecting internal data structures */
  555. static __inline void ahd_lockinit(struct ahd_softc *);
  556. static __inline void ahd_lock(struct ahd_softc *, unsigned long *flags);
  557. static __inline void ahd_unlock(struct ahd_softc *, unsigned long *flags);
  558. /* Lock acquisition and release of the above lock in midlayer entry points. */
  559. static __inline void ahd_midlayer_entrypoint_lock(struct ahd_softc *,
  560. unsigned long *flags);
  561. static __inline void ahd_midlayer_entrypoint_unlock(struct ahd_softc *,
  562. unsigned long *flags);
  563. /* Lock held during command compeletion to the upper layer */
  564. static __inline void ahd_done_lockinit(struct ahd_softc *);
  565. static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *flags);
  566. static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *flags);
  567. /* Lock held during ahd_list manipulation and ahd softc frees */
  568. extern spinlock_t ahd_list_spinlock;
  569. static __inline void ahd_list_lockinit(void);
  570. static __inline void ahd_list_lock(unsigned long *flags);
  571. static __inline void ahd_list_unlock(unsigned long *flags);
  572. static __inline void
  573. ahd_lockinit(struct ahd_softc *ahd)
  574. {
  575. spin_lock_init(&ahd->platform_data->spin_lock);
  576. }
  577. static __inline void
  578. ahd_lock(struct ahd_softc *ahd, unsigned long *flags)
  579. {
  580. spin_lock_irqsave(&ahd->platform_data->spin_lock, *flags);
  581. }
  582. static __inline void
  583. ahd_unlock(struct ahd_softc *ahd, unsigned long *flags)
  584. {
  585. spin_unlock_irqrestore(&ahd->platform_data->spin_lock, *flags);
  586. }
  587. static __inline void
  588. ahd_midlayer_entrypoint_lock(struct ahd_softc *ahd, unsigned long *flags)
  589. {
  590. /*
  591. * In 2.5.X and some 2.4.X versions, the midlayer takes our
  592. * lock just before calling us, so we avoid locking again.
  593. * For other kernel versions, the io_request_lock is taken
  594. * just before our entry point is called. In this case, we
  595. * trade the io_request_lock for our per-softc lock.
  596. */
  597. #if AHD_SCSI_HAS_HOST_LOCK == 0
  598. spin_unlock(&io_request_lock);
  599. spin_lock(&ahd->platform_data->spin_lock);
  600. #endif
  601. }
  602. static __inline void
  603. ahd_midlayer_entrypoint_unlock(struct ahd_softc *ahd, unsigned long *flags)
  604. {
  605. #if AHD_SCSI_HAS_HOST_LOCK == 0
  606. spin_unlock(&ahd->platform_data->spin_lock);
  607. spin_lock(&io_request_lock);
  608. #endif
  609. }
  610. static __inline void
  611. ahd_done_lockinit(struct ahd_softc *ahd)
  612. {
  613. /*
  614. * In 2.5.X, our own lock is held during completions.
  615. * In previous versions, the io_request_lock is used.
  616. * In either case, we can't initialize this lock again.
  617. */
  618. }
  619. static __inline void
  620. ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
  621. {
  622. #if AHD_SCSI_HAS_HOST_LOCK == 0
  623. spin_lock(&io_request_lock);
  624. #endif
  625. }
  626. static __inline void
  627. ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
  628. {
  629. #if AHD_SCSI_HAS_HOST_LOCK == 0
  630. spin_unlock(&io_request_lock);
  631. #endif
  632. }
  633. static __inline void
  634. ahd_list_lockinit(void)
  635. {
  636. spin_lock_init(&ahd_list_spinlock);
  637. }
  638. static __inline void
  639. ahd_list_lock(unsigned long *flags)
  640. {
  641. spin_lock_irqsave(&ahd_list_spinlock, *flags);
  642. }
  643. static __inline void
  644. ahd_list_unlock(unsigned long *flags)
  645. {
  646. spin_unlock_irqrestore(&ahd_list_spinlock, *flags);
  647. }
  648. /******************************* PCI Definitions ******************************/
  649. /*
  650. * PCIM_xxx: mask to locate subfield in register
  651. * PCIR_xxx: config register offset
  652. * PCIC_xxx: device class
  653. * PCIS_xxx: device subclass
  654. * PCIP_xxx: device programming interface
  655. * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices)
  656. * PCID_xxx: device ID
  657. */
  658. #define PCIR_DEVVENDOR 0x00
  659. #define PCIR_VENDOR 0x00
  660. #define PCIR_DEVICE 0x02
  661. #define PCIR_COMMAND 0x04
  662. #define PCIM_CMD_PORTEN 0x0001
  663. #define PCIM_CMD_MEMEN 0x0002
  664. #define PCIM_CMD_BUSMASTEREN 0x0004
  665. #define PCIM_CMD_MWRICEN 0x0010
  666. #define PCIM_CMD_PERRESPEN 0x0040
  667. #define PCIM_CMD_SERRESPEN 0x0100
  668. #define PCIR_STATUS 0x06
  669. #define PCIR_REVID 0x08
  670. #define PCIR_PROGIF 0x09
  671. #define PCIR_SUBCLASS 0x0a
  672. #define PCIR_CLASS 0x0b
  673. #define PCIR_CACHELNSZ 0x0c
  674. #define PCIR_LATTIMER 0x0d
  675. #define PCIR_HEADERTYPE 0x0e
  676. #define PCIM_MFDEV 0x80
  677. #define PCIR_BIST 0x0f
  678. #define PCIR_CAP_PTR 0x34
  679. /* config registers for header type 0 devices */
  680. #define PCIR_MAPS 0x10
  681. #define PCIR_SUBVEND_0 0x2c
  682. #define PCIR_SUBDEV_0 0x2e
  683. /****************************** PCI-X definitions *****************************/
  684. #define PCIXR_COMMAND 0x96
  685. #define PCIXR_DEVADDR 0x98
  686. #define PCIXM_DEVADDR_FNUM 0x0003 /* Function Number */
  687. #define PCIXM_DEVADDR_DNUM 0x00F8 /* Device Number */
  688. #define PCIXM_DEVADDR_BNUM 0xFF00 /* Bus Number */
  689. #define PCIXR_STATUS 0x9A
  690. #define PCIXM_STATUS_64BIT 0x0001 /* Active 64bit connection to device. */
  691. #define PCIXM_STATUS_133CAP 0x0002 /* Device is 133MHz capable */
  692. #define PCIXM_STATUS_SCDISC 0x0004 /* Split Completion Discarded */
  693. #define PCIXM_STATUS_UNEXPSC 0x0008 /* Unexpected Split Completion */
  694. #define PCIXM_STATUS_CMPLEXDEV 0x0010 /* Device Complexity (set == bridge) */
  695. #define PCIXM_STATUS_MAXMRDBC 0x0060 /* Maximum Burst Read Count */
  696. #define PCIXM_STATUS_MAXSPLITS 0x0380 /* Maximum Split Transactions */
  697. #define PCIXM_STATUS_MAXCRDS 0x1C00 /* Maximum Cumulative Read Size */
  698. #define PCIXM_STATUS_RCVDSCEM 0x2000 /* Received a Split Comp w/Error msg */
  699. extern struct pci_driver aic79xx_pci_driver;
  700. typedef enum
  701. {
  702. AHD_POWER_STATE_D0,
  703. AHD_POWER_STATE_D1,
  704. AHD_POWER_STATE_D2,
  705. AHD_POWER_STATE_D3
  706. } ahd_power_state;
  707. void ahd_power_state_change(struct ahd_softc *ahd,
  708. ahd_power_state new_state);
  709. /******************************* PCI Routines *********************************/
  710. int ahd_linux_pci_init(void);
  711. void ahd_linux_pci_exit(void);
  712. int ahd_pci_map_registers(struct ahd_softc *ahd);
  713. int ahd_pci_map_int(struct ahd_softc *ahd);
  714. static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t pci,
  715. int reg, int width);
  716. static __inline uint32_t
  717. ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
  718. {
  719. switch (width) {
  720. case 1:
  721. {
  722. uint8_t retval;
  723. pci_read_config_byte(pci, reg, &retval);
  724. return (retval);
  725. }
  726. case 2:
  727. {
  728. uint16_t retval;
  729. pci_read_config_word(pci, reg, &retval);
  730. return (retval);
  731. }
  732. case 4:
  733. {
  734. uint32_t retval;
  735. pci_read_config_dword(pci, reg, &retval);
  736. return (retval);
  737. }
  738. default:
  739. panic("ahd_pci_read_config: Read size too big");
  740. /* NOTREACHED */
  741. return (0);
  742. }
  743. }
  744. static __inline void ahd_pci_write_config(ahd_dev_softc_t pci,
  745. int reg, uint32_t value,
  746. int width);
  747. static __inline void
  748. ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
  749. {
  750. switch (width) {
  751. case 1:
  752. pci_write_config_byte(pci, reg, value);
  753. break;
  754. case 2:
  755. pci_write_config_word(pci, reg, value);
  756. break;
  757. case 4:
  758. pci_write_config_dword(pci, reg, value);
  759. break;
  760. default:
  761. panic("ahd_pci_write_config: Write size too big");
  762. /* NOTREACHED */
  763. }
  764. }
  765. static __inline int ahd_get_pci_function(ahd_dev_softc_t);
  766. static __inline int
  767. ahd_get_pci_function(ahd_dev_softc_t pci)
  768. {
  769. return (PCI_FUNC(pci->devfn));
  770. }
  771. static __inline int ahd_get_pci_slot(ahd_dev_softc_t);
  772. static __inline int
  773. ahd_get_pci_slot(ahd_dev_softc_t pci)
  774. {
  775. return (PCI_SLOT(pci->devfn));
  776. }
  777. static __inline int ahd_get_pci_bus(ahd_dev_softc_t);
  778. static __inline int
  779. ahd_get_pci_bus(ahd_dev_softc_t pci)
  780. {
  781. return (pci->bus->number);
  782. }
  783. static __inline void ahd_flush_device_writes(struct ahd_softc *);
  784. static __inline void
  785. ahd_flush_device_writes(struct ahd_softc *ahd)
  786. {
  787. /* XXX Is this sufficient for all architectures??? */
  788. ahd_inb(ahd, INTSTAT);
  789. }
  790. /**************************** Proc FS Support *********************************/
  791. int ahd_linux_proc_info(struct Scsi_Host *, char *, char **,
  792. off_t, int, int);
  793. /*************************** Domain Validation ********************************/
  794. #define AHD_DV_CMD(cmd) ((cmd)->scsi_done == ahd_linux_dv_complete)
  795. #define AHD_DV_SIMQ_FROZEN(ahd) \
  796. ((((ahd)->platform_data->flags & AHD_DV_ACTIVE) != 0) \
  797. && (ahd)->platform_data->qfrozen == 1)
  798. /*********************** Transaction Access Wrappers **************************/
  799. static __inline void ahd_cmd_set_transaction_status(Scsi_Cmnd *, uint32_t);
  800. static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
  801. static __inline void ahd_cmd_set_scsi_status(Scsi_Cmnd *, uint32_t);
  802. static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
  803. static __inline uint32_t ahd_cmd_get_transaction_status(Scsi_Cmnd *cmd);
  804. static __inline uint32_t ahd_get_transaction_status(struct scb *);
  805. static __inline uint32_t ahd_cmd_get_scsi_status(Scsi_Cmnd *cmd);
  806. static __inline uint32_t ahd_get_scsi_status(struct scb *);
  807. static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
  808. static __inline u_long ahd_get_transfer_length(struct scb *);
  809. static __inline int ahd_get_transfer_dir(struct scb *);
  810. static __inline void ahd_set_residual(struct scb *, u_long);
  811. static __inline void ahd_set_sense_residual(struct scb *scb, u_long resid);
  812. static __inline u_long ahd_get_residual(struct scb *);
  813. static __inline u_long ahd_get_sense_residual(struct scb *);
  814. static __inline int ahd_perform_autosense(struct scb *);
  815. static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc *,
  816. struct scb *);
  817. static __inline void ahd_notify_xfer_settings_change(struct ahd_softc *,
  818. struct ahd_devinfo *);
  819. static __inline void ahd_platform_scb_free(struct ahd_softc *ahd,
  820. struct scb *scb);
  821. static __inline void ahd_freeze_scb(struct scb *scb);
  822. static __inline
  823. void ahd_cmd_set_transaction_status(Scsi_Cmnd *cmd, uint32_t status)
  824. {
  825. cmd->result &= ~(CAM_STATUS_MASK << 16);
  826. cmd->result |= status << 16;
  827. }
  828. static __inline
  829. void ahd_set_transaction_status(struct scb *scb, uint32_t status)
  830. {
  831. ahd_cmd_set_transaction_status(scb->io_ctx,status);
  832. }
  833. static __inline
  834. void ahd_cmd_set_scsi_status(Scsi_Cmnd *cmd, uint32_t status)
  835. {
  836. cmd->result &= ~0xFFFF;
  837. cmd->result |= status;
  838. }
  839. static __inline
  840. void ahd_set_scsi_status(struct scb *scb, uint32_t status)
  841. {
  842. ahd_cmd_set_scsi_status(scb->io_ctx, status);
  843. }
  844. static __inline
  845. uint32_t ahd_cmd_get_transaction_status(Scsi_Cmnd *cmd)
  846. {
  847. return ((cmd->result >> 16) & CAM_STATUS_MASK);
  848. }
  849. static __inline
  850. uint32_t ahd_get_transaction_status(struct scb *scb)
  851. {
  852. return (ahd_cmd_get_transaction_status(scb->io_ctx));
  853. }
  854. static __inline
  855. uint32_t ahd_cmd_get_scsi_status(Scsi_Cmnd *cmd)
  856. {
  857. return (cmd->result & 0xFFFF);
  858. }
  859. static __inline
  860. uint32_t ahd_get_scsi_status(struct scb *scb)
  861. {
  862. return (ahd_cmd_get_scsi_status(scb->io_ctx));
  863. }
  864. static __inline
  865. void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
  866. {
  867. /*
  868. * Nothing to do for linux as the incoming transaction
  869. * has no concept of tag/non tagged, etc.
  870. */
  871. }
  872. static __inline
  873. u_long ahd_get_transfer_length(struct scb *scb)
  874. {
  875. return (scb->platform_data->xfer_len);
  876. }
  877. static __inline
  878. int ahd_get_transfer_dir(struct scb *scb)
  879. {
  880. return (scb->io_ctx->sc_data_direction);
  881. }
  882. static __inline
  883. void ahd_set_residual(struct scb *scb, u_long resid)
  884. {
  885. scb->io_ctx->resid = resid;
  886. }
  887. static __inline
  888. void ahd_set_sense_residual(struct scb *scb, u_long resid)
  889. {
  890. scb->platform_data->sense_resid = resid;
  891. }
  892. static __inline
  893. u_long ahd_get_residual(struct scb *scb)
  894. {
  895. return (scb->io_ctx->resid);
  896. }
  897. static __inline
  898. u_long ahd_get_sense_residual(struct scb *scb)
  899. {
  900. return (scb->platform_data->sense_resid);
  901. }
  902. static __inline
  903. int ahd_perform_autosense(struct scb *scb)
  904. {
  905. /*
  906. * We always perform autosense in Linux.
  907. * On other platforms this is set on a
  908. * per-transaction basis.
  909. */
  910. return (1);
  911. }
  912. static __inline uint32_t
  913. ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
  914. {
  915. return (sizeof(struct scsi_sense_data));
  916. }
  917. static __inline void
  918. ahd_notify_xfer_settings_change(struct ahd_softc *ahd,
  919. struct ahd_devinfo *devinfo)
  920. {
  921. /* Nothing to do here for linux */
  922. }
  923. static __inline void
  924. ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
  925. {
  926. ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
  927. }
  928. int ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
  929. void ahd_platform_free(struct ahd_softc *ahd);
  930. void ahd_platform_init(struct ahd_softc *ahd);
  931. void ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb);
  932. void ahd_freeze_simq(struct ahd_softc *ahd);
  933. void ahd_release_simq(struct ahd_softc *ahd);
  934. static __inline void
  935. ahd_freeze_scb(struct scb *scb)
  936. {
  937. if ((scb->io_ctx->result & (CAM_DEV_QFRZN << 16)) == 0) {
  938. scb->io_ctx->result |= CAM_DEV_QFRZN << 16;
  939. scb->platform_data->dev->qfrozen++;
  940. }
  941. }
  942. void ahd_platform_set_tags(struct ahd_softc *ahd,
  943. struct ahd_devinfo *devinfo, ahd_queue_alg);
  944. int ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
  945. char channel, int lun, u_int tag,
  946. role_t role, uint32_t status);
  947. irqreturn_t
  948. ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs);
  949. void ahd_platform_flushwork(struct ahd_softc *ahd);
  950. int ahd_softc_comp(struct ahd_softc *, struct ahd_softc *);
  951. void ahd_done(struct ahd_softc*, struct scb*);
  952. void ahd_send_async(struct ahd_softc *, char channel,
  953. u_int target, u_int lun, ac_code, void *);
  954. void ahd_print_path(struct ahd_softc *, struct scb *);
  955. #ifdef CONFIG_PCI
  956. #define AHD_PCI_CONFIG 1
  957. #else
  958. #define AHD_PCI_CONFIG 0
  959. #endif
  960. #define bootverbose aic79xx_verbose
  961. extern uint32_t aic79xx_verbose;
  962. #endif /* _AIC79XX_LINUX_H_ */