aic79xx_osm.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0) || defined(SCSI_HAS_HOST_LOCK))
  218. #define AHD_SCSI_HAS_HOST_LOCK 1
  219. #else
  220. #define AHD_SCSI_HAS_HOST_LOCK 0
  221. #endif
  222. #define AIC79XX_DRIVER_VERSION "1.3.11"
  223. /**************************** Front End Queues ********************************/
  224. /*
  225. * Data structure used to cast the Linux struct scsi_cmnd to something
  226. * that allows us to use the queue macros. The linux structure has
  227. * plenty of space to hold the links fields as required by the queue
  228. * macros, but the queue macors require them to have the correct type.
  229. */
  230. struct ahd_cmd_internal {
  231. /* Area owned by the Linux scsi layer. */
  232. uint8_t private[offsetof(struct scsi_cmnd, SCp.Status)];
  233. union {
  234. STAILQ_ENTRY(ahd_cmd) ste;
  235. LIST_ENTRY(ahd_cmd) le;
  236. TAILQ_ENTRY(ahd_cmd) tqe;
  237. } links;
  238. uint32_t end;
  239. };
  240. struct ahd_cmd {
  241. union {
  242. struct ahd_cmd_internal icmd;
  243. struct scsi_cmnd scsi_cmd;
  244. } un;
  245. };
  246. #define acmd_icmd(cmd) ((cmd)->un.icmd)
  247. #define acmd_scsi_cmd(cmd) ((cmd)->un.scsi_cmd)
  248. #define acmd_links un.icmd.links
  249. /*************************** Device Data Structures ***************************/
  250. /*
  251. * A per probed device structure used to deal with some error recovery
  252. * scenarios that the Linux mid-layer code just doesn't know how to
  253. * handle. The structure allocated for a device only becomes persistent
  254. * after a successfully completed inquiry command to the target when
  255. * that inquiry data indicates a lun is present.
  256. */
  257. TAILQ_HEAD(ahd_busyq, ahd_cmd);
  258. typedef enum {
  259. AHD_DEV_UNCONFIGURED = 0x01,
  260. AHD_DEV_FREEZE_TIL_EMPTY = 0x02, /* Freeze queue until active == 0 */
  261. AHD_DEV_TIMER_ACTIVE = 0x04, /* Our timer is active */
  262. AHD_DEV_ON_RUN_LIST = 0x08, /* Queued to be run later */
  263. AHD_DEV_Q_BASIC = 0x10, /* Allow basic device queuing */
  264. AHD_DEV_Q_TAGGED = 0x20, /* Allow full SCSI2 command queueing */
  265. AHD_DEV_PERIODIC_OTAG = 0x40, /* Send OTAG to prevent starvation */
  266. AHD_DEV_SLAVE_CONFIGURED = 0x80 /* slave_configure() has been called */
  267. } ahd_linux_dev_flags;
  268. struct ahd_linux_target;
  269. struct ahd_linux_device {
  270. TAILQ_ENTRY(ahd_linux_device) links;
  271. struct ahd_busyq busyq;
  272. /*
  273. * The number of transactions currently
  274. * queued to the device.
  275. */
  276. int active;
  277. /*
  278. * The currently allowed number of
  279. * transactions that can be queued to
  280. * the device. Must be signed for
  281. * conversion from tagged to untagged
  282. * mode where the device may have more
  283. * than one outstanding active transaction.
  284. */
  285. int openings;
  286. /*
  287. * A positive count indicates that this
  288. * device's queue is halted.
  289. */
  290. u_int qfrozen;
  291. /*
  292. * Cumulative command counter.
  293. */
  294. u_long commands_issued;
  295. /*
  296. * The number of tagged transactions when
  297. * running at our current opening level
  298. * that have been successfully received by
  299. * this device since the last QUEUE FULL.
  300. */
  301. u_int tag_success_count;
  302. #define AHD_TAG_SUCCESS_INTERVAL 50
  303. ahd_linux_dev_flags flags;
  304. /*
  305. * Per device timer.
  306. */
  307. struct timer_list timer;
  308. /*
  309. * The high limit for the tags variable.
  310. */
  311. u_int maxtags;
  312. /*
  313. * The computed number of tags outstanding
  314. * at the time of the last QUEUE FULL event.
  315. */
  316. u_int tags_on_last_queuefull;
  317. /*
  318. * How many times we have seen a queue full
  319. * with the same number of tags. This is used
  320. * to stop our adaptive queue depth algorithm
  321. * on devices with a fixed number of tags.
  322. */
  323. u_int last_queuefull_same_count;
  324. #define AHD_LOCK_TAGS_COUNT 50
  325. /*
  326. * How many transactions have been queued
  327. * without the device going idle. We use
  328. * this statistic to determine when to issue
  329. * an ordered tag to prevent transaction
  330. * starvation. This statistic is only updated
  331. * if the AHD_DEV_PERIODIC_OTAG flag is set
  332. * on this device.
  333. */
  334. u_int commands_since_idle_or_otag;
  335. #define AHD_OTAG_THRESH 500
  336. int lun;
  337. Scsi_Device *scsi_device;
  338. struct ahd_linux_target *target;
  339. };
  340. typedef enum {
  341. AHD_DV_REQUIRED = 0x01,
  342. AHD_INQ_VALID = 0x02,
  343. AHD_BASIC_DV = 0x04,
  344. AHD_ENHANCED_DV = 0x08
  345. } ahd_linux_targ_flags;
  346. /* DV States */
  347. typedef enum {
  348. AHD_DV_STATE_EXIT = 0,
  349. AHD_DV_STATE_INQ_SHORT_ASYNC,
  350. AHD_DV_STATE_INQ_ASYNC,
  351. AHD_DV_STATE_INQ_ASYNC_VERIFY,
  352. AHD_DV_STATE_TUR,
  353. AHD_DV_STATE_REBD,
  354. AHD_DV_STATE_INQ_VERIFY,
  355. AHD_DV_STATE_WEB,
  356. AHD_DV_STATE_REB,
  357. AHD_DV_STATE_SU,
  358. AHD_DV_STATE_BUSY
  359. } ahd_dv_state;
  360. struct ahd_linux_target {
  361. struct ahd_linux_device *devices[AHD_NUM_LUNS];
  362. int channel;
  363. int target;
  364. int refcount;
  365. struct ahd_transinfo last_tinfo;
  366. struct ahd_softc *ahd;
  367. ahd_linux_targ_flags flags;
  368. struct scsi_inquiry_data *inq_data;
  369. /*
  370. * The next "fallback" period to use for narrow/wide transfers.
  371. */
  372. uint8_t dv_next_narrow_period;
  373. uint8_t dv_next_wide_period;
  374. uint8_t dv_max_width;
  375. uint8_t dv_max_ppr_options;
  376. uint8_t dv_last_ppr_options;
  377. u_int dv_echo_size;
  378. ahd_dv_state dv_state;
  379. u_int dv_state_retry;
  380. uint8_t *dv_buffer;
  381. uint8_t *dv_buffer1;
  382. /*
  383. * Cumulative counter of errors.
  384. */
  385. u_long errors_detected;
  386. u_long cmds_since_error;
  387. };
  388. /********************* Definitions Required by the Core ***********************/
  389. /*
  390. * Number of SG segments we require. So long as the S/G segments for
  391. * a particular transaction are allocated in a physically contiguous
  392. * manner and are allocated below 4GB, the number of S/G segments is
  393. * unrestricted.
  394. */
  395. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  396. /*
  397. * We dynamically adjust the number of segments in pre-2.5 kernels to
  398. * avoid fragmentation issues in the SCSI mid-layer's private memory
  399. * allocator. See aic79xx_osm.c ahd_linux_size_nseg() for details.
  400. */
  401. extern u_int ahd_linux_nseg;
  402. #define AHD_NSEG ahd_linux_nseg
  403. #define AHD_LINUX_MIN_NSEG 64
  404. #else
  405. #define AHD_NSEG 128
  406. #endif
  407. /*
  408. * Per-SCB OSM storage.
  409. */
  410. typedef enum {
  411. AHD_SCB_UP_EH_SEM = 0x1
  412. } ahd_linux_scb_flags;
  413. struct scb_platform_data {
  414. struct ahd_linux_device *dev;
  415. dma_addr_t buf_busaddr;
  416. uint32_t xfer_len;
  417. uint32_t sense_resid; /* Auto-Sense residual */
  418. ahd_linux_scb_flags flags;
  419. };
  420. /*
  421. * Define a structure used for each host adapter. All members are
  422. * aligned on a boundary >= the size of the member to honor the
  423. * alignment restrictions of the various platforms supported by
  424. * this driver.
  425. */
  426. typedef enum {
  427. AHD_DV_WAIT_SIMQ_EMPTY = 0x01,
  428. AHD_DV_WAIT_SIMQ_RELEASE = 0x02,
  429. AHD_DV_ACTIVE = 0x04,
  430. AHD_DV_SHUTDOWN = 0x08,
  431. AHD_RUN_CMPLT_Q_TIMER = 0x10
  432. } ahd_linux_softc_flags;
  433. TAILQ_HEAD(ahd_completeq, ahd_cmd);
  434. struct ahd_platform_data {
  435. /*
  436. * Fields accessed from interrupt context.
  437. */
  438. struct ahd_linux_target *targets[AHD_NUM_TARGETS];
  439. TAILQ_HEAD(, ahd_linux_device) device_runq;
  440. struct ahd_completeq completeq;
  441. spinlock_t spin_lock;
  442. struct tasklet_struct runq_tasklet;
  443. u_int qfrozen;
  444. pid_t dv_pid;
  445. struct timer_list completeq_timer;
  446. struct timer_list reset_timer;
  447. struct timer_list stats_timer;
  448. struct semaphore eh_sem;
  449. struct semaphore dv_sem;
  450. struct semaphore dv_cmd_sem; /* XXX This needs to be in
  451. * the target struct
  452. */
  453. struct scsi_device *dv_scsi_dev;
  454. struct Scsi_Host *host; /* pointer to scsi host */
  455. #define AHD_LINUX_NOIRQ ((uint32_t)~0)
  456. uint32_t irq; /* IRQ for this adapter */
  457. uint32_t bios_address;
  458. uint32_t mem_busaddr; /* Mem Base Addr */
  459. uint64_t hw_dma_mask;
  460. ahd_linux_softc_flags flags;
  461. };
  462. /************************** OS Utility Wrappers *******************************/
  463. #define printf printk
  464. #define M_NOWAIT GFP_ATOMIC
  465. #define M_WAITOK 0
  466. #define malloc(size, type, flags) kmalloc(size, flags)
  467. #define free(ptr, type) kfree(ptr)
  468. static __inline void ahd_delay(long);
  469. static __inline void
  470. ahd_delay(long usec)
  471. {
  472. /*
  473. * udelay on Linux can have problems for
  474. * multi-millisecond waits. Wait at most
  475. * 1024us per call.
  476. */
  477. while (usec > 0) {
  478. udelay(usec % 1024);
  479. usec -= 1024;
  480. }
  481. }
  482. /***************************** Low Level I/O **********************************/
  483. static __inline uint8_t ahd_inb(struct ahd_softc * ahd, long port);
  484. static __inline uint16_t ahd_inw_atomic(struct ahd_softc * ahd, long port);
  485. static __inline void ahd_outb(struct ahd_softc * ahd, long port, uint8_t val);
  486. static __inline void ahd_outw_atomic(struct ahd_softc * ahd,
  487. long port, uint16_t val);
  488. static __inline void ahd_outsb(struct ahd_softc * ahd, long port,
  489. uint8_t *, int count);
  490. static __inline void ahd_insb(struct ahd_softc * ahd, long port,
  491. uint8_t *, int count);
  492. static __inline uint8_t
  493. ahd_inb(struct ahd_softc * ahd, long port)
  494. {
  495. uint8_t x;
  496. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  497. x = readb(ahd->bshs[0].maddr + port);
  498. } else {
  499. x = inb(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
  500. }
  501. mb();
  502. return (x);
  503. }
  504. static __inline uint16_t
  505. ahd_inw_atomic(struct ahd_softc * ahd, long port)
  506. {
  507. uint8_t x;
  508. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  509. x = readw(ahd->bshs[0].maddr + port);
  510. } else {
  511. x = inw(ahd->bshs[(port) >> 8].ioport + ((port) & 0xFF));
  512. }
  513. mb();
  514. return (x);
  515. }
  516. static __inline void
  517. ahd_outb(struct ahd_softc * ahd, long port, uint8_t val)
  518. {
  519. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  520. writeb(val, ahd->bshs[0].maddr + port);
  521. } else {
  522. outb(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
  523. }
  524. mb();
  525. }
  526. static __inline void
  527. ahd_outw_atomic(struct ahd_softc * ahd, long port, uint16_t val)
  528. {
  529. if (ahd->tags[0] == BUS_SPACE_MEMIO) {
  530. writew(val, ahd->bshs[0].maddr + port);
  531. } else {
  532. outw(val, ahd->bshs[(port) >> 8].ioport + (port & 0xFF));
  533. }
  534. mb();
  535. }
  536. static __inline void
  537. ahd_outsb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
  538. {
  539. int i;
  540. /*
  541. * There is probably a more efficient way to do this on Linux
  542. * but we don't use this for anything speed critical and this
  543. * should work.
  544. */
  545. for (i = 0; i < count; i++)
  546. ahd_outb(ahd, port, *array++);
  547. }
  548. static __inline void
  549. ahd_insb(struct ahd_softc * ahd, long port, uint8_t *array, int count)
  550. {
  551. int i;
  552. /*
  553. * There is probably a more efficient way to do this on Linux
  554. * but we don't use this for anything speed critical and this
  555. * should work.
  556. */
  557. for (i = 0; i < count; i++)
  558. *array++ = ahd_inb(ahd, port);
  559. }
  560. /**************************** Initialization **********************************/
  561. int ahd_linux_register_host(struct ahd_softc *,
  562. Scsi_Host_Template *);
  563. uint64_t ahd_linux_get_memsize(void);
  564. /*************************** Pretty Printing **********************************/
  565. struct info_str {
  566. char *buffer;
  567. int length;
  568. off_t offset;
  569. int pos;
  570. };
  571. void ahd_format_transinfo(struct info_str *info,
  572. struct ahd_transinfo *tinfo);
  573. /******************************** Locking *************************************/
  574. /* Lock protecting internal data structures */
  575. static __inline void ahd_lockinit(struct ahd_softc *);
  576. static __inline void ahd_lock(struct ahd_softc *, unsigned long *flags);
  577. static __inline void ahd_unlock(struct ahd_softc *, unsigned long *flags);
  578. /* Lock acquisition and release of the above lock in midlayer entry points. */
  579. static __inline void ahd_midlayer_entrypoint_lock(struct ahd_softc *,
  580. unsigned long *flags);
  581. static __inline void ahd_midlayer_entrypoint_unlock(struct ahd_softc *,
  582. unsigned long *flags);
  583. /* Lock held during command compeletion to the upper layer */
  584. static __inline void ahd_done_lockinit(struct ahd_softc *);
  585. static __inline void ahd_done_lock(struct ahd_softc *, unsigned long *flags);
  586. static __inline void ahd_done_unlock(struct ahd_softc *, unsigned long *flags);
  587. /* Lock held during ahd_list manipulation and ahd softc frees */
  588. extern spinlock_t ahd_list_spinlock;
  589. static __inline void ahd_list_lockinit(void);
  590. static __inline void ahd_list_lock(unsigned long *flags);
  591. static __inline void ahd_list_unlock(unsigned long *flags);
  592. static __inline void
  593. ahd_lockinit(struct ahd_softc *ahd)
  594. {
  595. spin_lock_init(&ahd->platform_data->spin_lock);
  596. }
  597. static __inline void
  598. ahd_lock(struct ahd_softc *ahd, unsigned long *flags)
  599. {
  600. spin_lock_irqsave(&ahd->platform_data->spin_lock, *flags);
  601. }
  602. static __inline void
  603. ahd_unlock(struct ahd_softc *ahd, unsigned long *flags)
  604. {
  605. spin_unlock_irqrestore(&ahd->platform_data->spin_lock, *flags);
  606. }
  607. static __inline void
  608. ahd_midlayer_entrypoint_lock(struct ahd_softc *ahd, unsigned long *flags)
  609. {
  610. /*
  611. * In 2.5.X and some 2.4.X versions, the midlayer takes our
  612. * lock just before calling us, so we avoid locking again.
  613. * For other kernel versions, the io_request_lock is taken
  614. * just before our entry point is called. In this case, we
  615. * trade the io_request_lock for our per-softc lock.
  616. */
  617. #if AHD_SCSI_HAS_HOST_LOCK == 0
  618. spin_unlock(&io_request_lock);
  619. spin_lock(&ahd->platform_data->spin_lock);
  620. #endif
  621. }
  622. static __inline void
  623. ahd_midlayer_entrypoint_unlock(struct ahd_softc *ahd, unsigned long *flags)
  624. {
  625. #if AHD_SCSI_HAS_HOST_LOCK == 0
  626. spin_unlock(&ahd->platform_data->spin_lock);
  627. spin_lock(&io_request_lock);
  628. #endif
  629. }
  630. static __inline void
  631. ahd_done_lockinit(struct ahd_softc *ahd)
  632. {
  633. /*
  634. * In 2.5.X, our own lock is held during completions.
  635. * In previous versions, the io_request_lock is used.
  636. * In either case, we can't initialize this lock again.
  637. */
  638. }
  639. static __inline void
  640. ahd_done_lock(struct ahd_softc *ahd, unsigned long *flags)
  641. {
  642. #if AHD_SCSI_HAS_HOST_LOCK == 0
  643. spin_lock(&io_request_lock);
  644. #endif
  645. }
  646. static __inline void
  647. ahd_done_unlock(struct ahd_softc *ahd, unsigned long *flags)
  648. {
  649. #if AHD_SCSI_HAS_HOST_LOCK == 0
  650. spin_unlock(&io_request_lock);
  651. #endif
  652. }
  653. static __inline void
  654. ahd_list_lockinit(void)
  655. {
  656. spin_lock_init(&ahd_list_spinlock);
  657. }
  658. static __inline void
  659. ahd_list_lock(unsigned long *flags)
  660. {
  661. spin_lock_irqsave(&ahd_list_spinlock, *flags);
  662. }
  663. static __inline void
  664. ahd_list_unlock(unsigned long *flags)
  665. {
  666. spin_unlock_irqrestore(&ahd_list_spinlock, *flags);
  667. }
  668. /******************************* PCI Definitions ******************************/
  669. /*
  670. * PCIM_xxx: mask to locate subfield in register
  671. * PCIR_xxx: config register offset
  672. * PCIC_xxx: device class
  673. * PCIS_xxx: device subclass
  674. * PCIP_xxx: device programming interface
  675. * PCIV_xxx: PCI vendor ID (only required to fixup ancient devices)
  676. * PCID_xxx: device ID
  677. */
  678. #define PCIR_DEVVENDOR 0x00
  679. #define PCIR_VENDOR 0x00
  680. #define PCIR_DEVICE 0x02
  681. #define PCIR_COMMAND 0x04
  682. #define PCIM_CMD_PORTEN 0x0001
  683. #define PCIM_CMD_MEMEN 0x0002
  684. #define PCIM_CMD_BUSMASTEREN 0x0004
  685. #define PCIM_CMD_MWRICEN 0x0010
  686. #define PCIM_CMD_PERRESPEN 0x0040
  687. #define PCIM_CMD_SERRESPEN 0x0100
  688. #define PCIR_STATUS 0x06
  689. #define PCIR_REVID 0x08
  690. #define PCIR_PROGIF 0x09
  691. #define PCIR_SUBCLASS 0x0a
  692. #define PCIR_CLASS 0x0b
  693. #define PCIR_CACHELNSZ 0x0c
  694. #define PCIR_LATTIMER 0x0d
  695. #define PCIR_HEADERTYPE 0x0e
  696. #define PCIM_MFDEV 0x80
  697. #define PCIR_BIST 0x0f
  698. #define PCIR_CAP_PTR 0x34
  699. /* config registers for header type 0 devices */
  700. #define PCIR_MAPS 0x10
  701. #define PCIR_SUBVEND_0 0x2c
  702. #define PCIR_SUBDEV_0 0x2e
  703. /****************************** PCI-X definitions *****************************/
  704. #define PCIXR_COMMAND 0x96
  705. #define PCIXR_DEVADDR 0x98
  706. #define PCIXM_DEVADDR_FNUM 0x0003 /* Function Number */
  707. #define PCIXM_DEVADDR_DNUM 0x00F8 /* Device Number */
  708. #define PCIXM_DEVADDR_BNUM 0xFF00 /* Bus Number */
  709. #define PCIXR_STATUS 0x9A
  710. #define PCIXM_STATUS_64BIT 0x0001 /* Active 64bit connection to device. */
  711. #define PCIXM_STATUS_133CAP 0x0002 /* Device is 133MHz capable */
  712. #define PCIXM_STATUS_SCDISC 0x0004 /* Split Completion Discarded */
  713. #define PCIXM_STATUS_UNEXPSC 0x0008 /* Unexpected Split Completion */
  714. #define PCIXM_STATUS_CMPLEXDEV 0x0010 /* Device Complexity (set == bridge) */
  715. #define PCIXM_STATUS_MAXMRDBC 0x0060 /* Maximum Burst Read Count */
  716. #define PCIXM_STATUS_MAXSPLITS 0x0380 /* Maximum Split Transactions */
  717. #define PCIXM_STATUS_MAXCRDS 0x1C00 /* Maximum Cumulative Read Size */
  718. #define PCIXM_STATUS_RCVDSCEM 0x2000 /* Received a Split Comp w/Error msg */
  719. extern struct pci_driver aic79xx_pci_driver;
  720. typedef enum
  721. {
  722. AHD_POWER_STATE_D0,
  723. AHD_POWER_STATE_D1,
  724. AHD_POWER_STATE_D2,
  725. AHD_POWER_STATE_D3
  726. } ahd_power_state;
  727. void ahd_power_state_change(struct ahd_softc *ahd,
  728. ahd_power_state new_state);
  729. /******************************* PCI Routines *********************************/
  730. int ahd_linux_pci_init(void);
  731. void ahd_linux_pci_exit(void);
  732. int ahd_pci_map_registers(struct ahd_softc *ahd);
  733. int ahd_pci_map_int(struct ahd_softc *ahd);
  734. static __inline uint32_t ahd_pci_read_config(ahd_dev_softc_t pci,
  735. int reg, int width);
  736. static __inline uint32_t
  737. ahd_pci_read_config(ahd_dev_softc_t pci, int reg, int width)
  738. {
  739. switch (width) {
  740. case 1:
  741. {
  742. uint8_t retval;
  743. pci_read_config_byte(pci, reg, &retval);
  744. return (retval);
  745. }
  746. case 2:
  747. {
  748. uint16_t retval;
  749. pci_read_config_word(pci, reg, &retval);
  750. return (retval);
  751. }
  752. case 4:
  753. {
  754. uint32_t retval;
  755. pci_read_config_dword(pci, reg, &retval);
  756. return (retval);
  757. }
  758. default:
  759. panic("ahd_pci_read_config: Read size too big");
  760. /* NOTREACHED */
  761. return (0);
  762. }
  763. }
  764. static __inline void ahd_pci_write_config(ahd_dev_softc_t pci,
  765. int reg, uint32_t value,
  766. int width);
  767. static __inline void
  768. ahd_pci_write_config(ahd_dev_softc_t pci, int reg, uint32_t value, int width)
  769. {
  770. switch (width) {
  771. case 1:
  772. pci_write_config_byte(pci, reg, value);
  773. break;
  774. case 2:
  775. pci_write_config_word(pci, reg, value);
  776. break;
  777. case 4:
  778. pci_write_config_dword(pci, reg, value);
  779. break;
  780. default:
  781. panic("ahd_pci_write_config: Write size too big");
  782. /* NOTREACHED */
  783. }
  784. }
  785. static __inline int ahd_get_pci_function(ahd_dev_softc_t);
  786. static __inline int
  787. ahd_get_pci_function(ahd_dev_softc_t pci)
  788. {
  789. return (PCI_FUNC(pci->devfn));
  790. }
  791. static __inline int ahd_get_pci_slot(ahd_dev_softc_t);
  792. static __inline int
  793. ahd_get_pci_slot(ahd_dev_softc_t pci)
  794. {
  795. return (PCI_SLOT(pci->devfn));
  796. }
  797. static __inline int ahd_get_pci_bus(ahd_dev_softc_t);
  798. static __inline int
  799. ahd_get_pci_bus(ahd_dev_softc_t pci)
  800. {
  801. return (pci->bus->number);
  802. }
  803. static __inline void ahd_flush_device_writes(struct ahd_softc *);
  804. static __inline void
  805. ahd_flush_device_writes(struct ahd_softc *ahd)
  806. {
  807. /* XXX Is this sufficient for all architectures??? */
  808. ahd_inb(ahd, INTSTAT);
  809. }
  810. /**************************** Proc FS Support *********************************/
  811. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  812. int ahd_linux_proc_info(char *, char **, off_t, int, int, int);
  813. #else
  814. int ahd_linux_proc_info(struct Scsi_Host *, char *, char **,
  815. off_t, int, int);
  816. #endif
  817. /*************************** Domain Validation ********************************/
  818. #define AHD_DV_CMD(cmd) ((cmd)->scsi_done == ahd_linux_dv_complete)
  819. #define AHD_DV_SIMQ_FROZEN(ahd) \
  820. ((((ahd)->platform_data->flags & AHD_DV_ACTIVE) != 0) \
  821. && (ahd)->platform_data->qfrozen == 1)
  822. /*********************** Transaction Access Wrappers **************************/
  823. static __inline void ahd_cmd_set_transaction_status(Scsi_Cmnd *, uint32_t);
  824. static __inline void ahd_set_transaction_status(struct scb *, uint32_t);
  825. static __inline void ahd_cmd_set_scsi_status(Scsi_Cmnd *, uint32_t);
  826. static __inline void ahd_set_scsi_status(struct scb *, uint32_t);
  827. static __inline uint32_t ahd_cmd_get_transaction_status(Scsi_Cmnd *cmd);
  828. static __inline uint32_t ahd_get_transaction_status(struct scb *);
  829. static __inline uint32_t ahd_cmd_get_scsi_status(Scsi_Cmnd *cmd);
  830. static __inline uint32_t ahd_get_scsi_status(struct scb *);
  831. static __inline void ahd_set_transaction_tag(struct scb *, int, u_int);
  832. static __inline u_long ahd_get_transfer_length(struct scb *);
  833. static __inline int ahd_get_transfer_dir(struct scb *);
  834. static __inline void ahd_set_residual(struct scb *, u_long);
  835. static __inline void ahd_set_sense_residual(struct scb *scb, u_long resid);
  836. static __inline u_long ahd_get_residual(struct scb *);
  837. static __inline u_long ahd_get_sense_residual(struct scb *);
  838. static __inline int ahd_perform_autosense(struct scb *);
  839. static __inline uint32_t ahd_get_sense_bufsize(struct ahd_softc *,
  840. struct scb *);
  841. static __inline void ahd_notify_xfer_settings_change(struct ahd_softc *,
  842. struct ahd_devinfo *);
  843. static __inline void ahd_platform_scb_free(struct ahd_softc *ahd,
  844. struct scb *scb);
  845. static __inline void ahd_freeze_scb(struct scb *scb);
  846. static __inline
  847. void ahd_cmd_set_transaction_status(Scsi_Cmnd *cmd, uint32_t status)
  848. {
  849. cmd->result &= ~(CAM_STATUS_MASK << 16);
  850. cmd->result |= status << 16;
  851. }
  852. static __inline
  853. void ahd_set_transaction_status(struct scb *scb, uint32_t status)
  854. {
  855. ahd_cmd_set_transaction_status(scb->io_ctx,status);
  856. }
  857. static __inline
  858. void ahd_cmd_set_scsi_status(Scsi_Cmnd *cmd, uint32_t status)
  859. {
  860. cmd->result &= ~0xFFFF;
  861. cmd->result |= status;
  862. }
  863. static __inline
  864. void ahd_set_scsi_status(struct scb *scb, uint32_t status)
  865. {
  866. ahd_cmd_set_scsi_status(scb->io_ctx, status);
  867. }
  868. static __inline
  869. uint32_t ahd_cmd_get_transaction_status(Scsi_Cmnd *cmd)
  870. {
  871. return ((cmd->result >> 16) & CAM_STATUS_MASK);
  872. }
  873. static __inline
  874. uint32_t ahd_get_transaction_status(struct scb *scb)
  875. {
  876. return (ahd_cmd_get_transaction_status(scb->io_ctx));
  877. }
  878. static __inline
  879. uint32_t ahd_cmd_get_scsi_status(Scsi_Cmnd *cmd)
  880. {
  881. return (cmd->result & 0xFFFF);
  882. }
  883. static __inline
  884. uint32_t ahd_get_scsi_status(struct scb *scb)
  885. {
  886. return (ahd_cmd_get_scsi_status(scb->io_ctx));
  887. }
  888. static __inline
  889. void ahd_set_transaction_tag(struct scb *scb, int enabled, u_int type)
  890. {
  891. /*
  892. * Nothing to do for linux as the incoming transaction
  893. * has no concept of tag/non tagged, etc.
  894. */
  895. }
  896. static __inline
  897. u_long ahd_get_transfer_length(struct scb *scb)
  898. {
  899. return (scb->platform_data->xfer_len);
  900. }
  901. static __inline
  902. int ahd_get_transfer_dir(struct scb *scb)
  903. {
  904. return (scb->io_ctx->sc_data_direction);
  905. }
  906. static __inline
  907. void ahd_set_residual(struct scb *scb, u_long resid)
  908. {
  909. scb->io_ctx->resid = resid;
  910. }
  911. static __inline
  912. void ahd_set_sense_residual(struct scb *scb, u_long resid)
  913. {
  914. scb->platform_data->sense_resid = resid;
  915. }
  916. static __inline
  917. u_long ahd_get_residual(struct scb *scb)
  918. {
  919. return (scb->io_ctx->resid);
  920. }
  921. static __inline
  922. u_long ahd_get_sense_residual(struct scb *scb)
  923. {
  924. return (scb->platform_data->sense_resid);
  925. }
  926. static __inline
  927. int ahd_perform_autosense(struct scb *scb)
  928. {
  929. /*
  930. * We always perform autosense in Linux.
  931. * On other platforms this is set on a
  932. * per-transaction basis.
  933. */
  934. return (1);
  935. }
  936. static __inline uint32_t
  937. ahd_get_sense_bufsize(struct ahd_softc *ahd, struct scb *scb)
  938. {
  939. return (sizeof(struct scsi_sense_data));
  940. }
  941. static __inline void
  942. ahd_notify_xfer_settings_change(struct ahd_softc *ahd,
  943. struct ahd_devinfo *devinfo)
  944. {
  945. /* Nothing to do here for linux */
  946. }
  947. static __inline void
  948. ahd_platform_scb_free(struct ahd_softc *ahd, struct scb *scb)
  949. {
  950. ahd->flags &= ~AHD_RESOURCE_SHORTAGE;
  951. }
  952. int ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg);
  953. void ahd_platform_free(struct ahd_softc *ahd);
  954. void ahd_platform_init(struct ahd_softc *ahd);
  955. void ahd_platform_freeze_devq(struct ahd_softc *ahd, struct scb *scb);
  956. void ahd_freeze_simq(struct ahd_softc *ahd);
  957. void ahd_release_simq(struct ahd_softc *ahd);
  958. static __inline void
  959. ahd_freeze_scb(struct scb *scb)
  960. {
  961. if ((scb->io_ctx->result & (CAM_DEV_QFRZN << 16)) == 0) {
  962. scb->io_ctx->result |= CAM_DEV_QFRZN << 16;
  963. scb->platform_data->dev->qfrozen++;
  964. }
  965. }
  966. void ahd_platform_set_tags(struct ahd_softc *ahd,
  967. struct ahd_devinfo *devinfo, ahd_queue_alg);
  968. int ahd_platform_abort_scbs(struct ahd_softc *ahd, int target,
  969. char channel, int lun, u_int tag,
  970. role_t role, uint32_t status);
  971. irqreturn_t
  972. ahd_linux_isr(int irq, void *dev_id, struct pt_regs * regs);
  973. void ahd_platform_flushwork(struct ahd_softc *ahd);
  974. int ahd_softc_comp(struct ahd_softc *, struct ahd_softc *);
  975. void ahd_done(struct ahd_softc*, struct scb*);
  976. void ahd_send_async(struct ahd_softc *, char channel,
  977. u_int target, u_int lun, ac_code, void *);
  978. void ahd_print_path(struct ahd_softc *, struct scb *);
  979. void ahd_platform_dump_card_state(struct ahd_softc *ahd);
  980. #ifdef CONFIG_PCI
  981. #define AHD_PCI_CONFIG 1
  982. #else
  983. #define AHD_PCI_CONFIG 0
  984. #endif
  985. #define bootverbose aic79xx_verbose
  986. extern uint32_t aic79xx_verbose;
  987. #endif /* _AIC79XX_LINUX_H_ */