xref: /openbmc/linux/drivers/scsi/megaraid.h (revision ccc319dc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __MEGARAID_H__
3 #define __MEGARAID_H__
4 
5 #include <linux/spinlock.h>
6 #include <linux/mutex.h>
7 #include <scsi/scsi_cmnd.h>
8 
9 #define MEGARAID_VERSION	\
10 	"v2.00.4 (Release Date: Thu Feb 9 08:51:30 EST 2006)\n"
11 
12 /*
13  * Driver features - change the values to enable or disable features in the
14  * driver.
15  */
16 
17 /*
18  * Command coalescing - This feature allows the driver to be able to combine
19  * two or more commands and issue as one command in order to boost I/O
20  * performance. Useful if the nature of the I/O is sequential. It is not very
21  * useful for random natured I/Os.
22  */
23 #define MEGA_HAVE_COALESCING	0
24 
25 /*
26  * Clustering support - Set this flag if you are planning to use the
27  * clustering services provided by the megaraid controllers and planning to
28  * setup a cluster
29  */
30 #define MEGA_HAVE_CLUSTERING	1
31 
32 /*
33  * Driver statistics - Set this flag if you are interested in statics about
34  * number of I/O completed on each logical drive and how many interrupts
35  * generated. If enabled, this information is available through /proc
36  * interface and through the private ioctl. Setting this flag has a
37  * performance penalty.
38  */
39 #define MEGA_HAVE_STATS		0
40 
41 /*
42  * Enhanced /proc interface - This feature will allow you to have a more
43  * detailed /proc interface for megaraid driver. E.g., a real time update of
44  * the status of the logical drives, battery status, physical drives etc.
45  */
46 #define MEGA_HAVE_ENH_PROC	1
47 
48 #define MAX_DEV_TYPE	32
49 
50 #define PCI_DEVICE_ID_DISCOVERY		0x000E
51 #define PCI_DEVICE_ID_PERC4_DI		0x000F
52 #define PCI_DEVICE_ID_PERC4_QC_VERDE	0x0407
53 
54 #define HBA_SIGNATURE	      		0x3344
55 #define HBA_SIGNATURE_471	  	0xCCCC
56 #define HBA_SIGNATURE_64BIT		0x0299
57 
58 #define MBOX_BUSY_WAIT			10	/* wait for up to 10 usec for
59 						   mailbox to be free */
60 #define DEFAULT_INITIATOR_ID	7
61 
62 #define MAX_SGLIST		64	/* max supported in f/w */
63 #define MIN_SGLIST		26	/* guaranteed to support these many */
64 #define MAX_COMMANDS		126
65 #define CMDID_INT_CMDS		MAX_COMMANDS+1	/* make sure CMDID_INT_CMDS
66 					 	is less than max commands
67 						supported by any f/w */
68 
69 #define MAX_CDB_LEN	     	10
70 #define MAX_EXT_CDB_LEN		16	/* we support cdb length up to 16 */
71 
72 #define DEF_CMD_PER_LUN		63
73 #define MAX_CMD_PER_LUN		MAX_COMMANDS
74 #define MAX_FIRMWARE_STATUS	46
75 #define MAX_XFER_PER_CMD	(64*1024)
76 #define MAX_SECTORS_PER_IO	128
77 
78 #define MAX_LOGICAL_DRIVES_40LD		40
79 #define FC_MAX_PHYSICAL_DEVICES		256
80 #define MAX_LOGICAL_DRIVES_8LD		8
81 #define MAX_CHANNELS			5
82 #define MAX_TARGET			15
83 #define MAX_PHYSICAL_DRIVES		MAX_CHANNELS*MAX_TARGET
84 #define MAX_ROW_SIZE_40LD		32
85 #define MAX_ROW_SIZE_8LD		8
86 #define MAX_SPAN_DEPTH			8
87 
88 #define NVIRT_CHAN		4	/* # of virtual channels to represent
89 					   up to 60 logical drives */
90 struct mbox_out {
91 	/* 0x0 */ u8 cmd;
92 	/* 0x1 */ u8 cmdid;
93 	/* 0x2 */ u16 numsectors;
94 	/* 0x4 */ u32 lba;
95 	/* 0x8 */ u32 xferaddr;
96 	/* 0xC */ u8 logdrv;
97 	/* 0xD */ u8 numsgelements;
98 	/* 0xE */ u8 resvd;
99 } __attribute__ ((packed));
100 
101 struct mbox_in {
102 	/* 0xF */ volatile u8 busy;
103 	/* 0x10 */ volatile u8 numstatus;
104 	/* 0x11 */ volatile u8 status;
105 	/* 0x12 */ volatile u8 completed[MAX_FIRMWARE_STATUS];
106 	volatile u8 poll;
107 	volatile u8 ack;
108 } __attribute__ ((packed));
109 
110 typedef struct {
111 	struct mbox_out	m_out;
112 	struct mbox_in	m_in;
113 } __attribute__ ((packed)) mbox_t;
114 
115 typedef struct {
116 	u32 xfer_segment_lo;
117 	u32 xfer_segment_hi;
118 	mbox_t mbox;
119 } __attribute__ ((packed)) mbox64_t;
120 
121 
122 /*
123  * Passthru definitions
124  */
125 #define MAX_REQ_SENSE_LEN       0x20
126 
127 typedef struct {
128 	u8 timeout:3;		/* 0=6sec/1=60sec/2=10min/3=3hrs */
129 	u8 ars:1;
130 	u8 reserved:3;
131 	u8 islogical:1;
132 	u8 logdrv;		/* if islogical == 1 */
133 	u8 channel;		/* if islogical == 0 */
134 	u8 target;		/* if islogical == 0 */
135 	u8 queuetag;		/* unused */
136 	u8 queueaction;		/* unused */
137 	u8 cdb[MAX_CDB_LEN];
138 	u8 cdblen;
139 	u8 reqsenselen;
140 	u8 reqsensearea[MAX_REQ_SENSE_LEN];
141 	u8 numsgelements;
142 	u8 scsistatus;
143 	u32 dataxferaddr;
144 	u32 dataxferlen;
145 } __attribute__ ((packed)) mega_passthru;
146 
147 
148 /*
149  * Extended passthru: support CDB > 10 bytes
150  */
151 typedef struct {
152 	u8 timeout:3;		/* 0=6sec/1=60sec/2=10min/3=3hrs */
153 	u8 ars:1;
154 	u8 rsvd1:1;
155 	u8 cd_rom:1;
156 	u8 rsvd2:1;
157 	u8 islogical:1;
158 	u8 logdrv;		/* if islogical == 1 */
159 	u8 channel;		/* if islogical == 0 */
160 	u8 target;		/* if islogical == 0 */
161 	u8 queuetag;		/* unused */
162 	u8 queueaction;		/* unused */
163 	u8 cdblen;
164 	u8 rsvd3;
165 	u8 cdb[MAX_EXT_CDB_LEN];
166 	u8 numsgelements;
167 	u8 status;
168 	u8 reqsenselen;
169 	u8 reqsensearea[MAX_REQ_SENSE_LEN];
170 	u8 rsvd4;
171 	u32 dataxferaddr;
172 	u32 dataxferlen;
173 } __attribute__ ((packed)) mega_ext_passthru;
174 
175 typedef struct {
176 	u64 address;
177 	u32 length;
178 } __attribute__ ((packed)) mega_sgl64;
179 
180 typedef struct {
181 	u32 address;
182 	u32 length;
183 } __attribute__ ((packed)) mega_sglist;
184 
185 
186 /* Queued command data */
187 typedef struct {
188 	int	idx;
189 	u32	state;
190 	struct list_head	list;
191 	u8	raw_mbox[66];
192 	u32	dma_type;
193 	u32	dma_direction;
194 
195 	struct scsi_cmnd	*cmd;
196 	dma_addr_t	dma_h_bulkdata;
197 	dma_addr_t	dma_h_sgdata;
198 
199 	mega_sglist	*sgl;
200 	mega_sgl64	*sgl64;
201 	dma_addr_t	sgl_dma_addr;
202 
203 	mega_passthru		*pthru;
204 	dma_addr_t		pthru_dma_addr;
205 	mega_ext_passthru	*epthru;
206 	dma_addr_t		epthru_dma_addr;
207 } scb_t;
208 
209 /*
210  * Flags to follow the scb as it transitions between various stages
211  */
212 #define SCB_FREE	0x0000	/* on the free list */
213 #define SCB_ACTIVE	0x0001	/* off the free list */
214 #define SCB_PENDQ	0x0002	/* on the pending queue */
215 #define SCB_ISSUED	0x0004	/* issued - owner f/w */
216 #define SCB_ABORT	0x0008	/* Got an abort for this one */
217 #define SCB_RESET	0x0010	/* Got a reset for this one */
218 
219 /*
220  * Utilities declare this strcture size as 1024 bytes. So more fields can
221  * be added in future.
222  */
223 typedef struct {
224 	u32	data_size; /* current size in bytes (not including resvd) */
225 
226 	u32	config_signature;
227 		/* Current value is 0x00282008
228 		 * 0x28=MAX_LOGICAL_DRIVES,
229 		 * 0x20=Number of stripes and
230 		 * 0x08=Number of spans */
231 
232 	u8	fw_version[16];		/* printable ASCI string */
233 	u8	bios_version[16];	/* printable ASCI string */
234 	u8	product_name[80];	/* printable ASCI string */
235 
236 	u8	max_commands;		/* Max. concurrent commands supported */
237 	u8	nchannels;		/* Number of SCSI Channels detected */
238 	u8	fc_loop_present;	/* Number of Fibre Loops detected */
239 	u8	mem_type;		/* EDO, FPM, SDRAM etc */
240 
241 	u32	signature;
242 	u16	dram_size;		/* In terms of MB */
243 	u16	subsysid;
244 
245 	u16	subsysvid;
246 	u8	notify_counters;
247 	u8	pad1k[889];		/* 135 + 889 resvd = 1024 total size */
248 } __attribute__ ((packed)) mega_product_info;
249 
250 struct notify {
251 	u32 global_counter;	/* Any change increments this counter */
252 
253 	u8 param_counter;	/* Indicates any params changed  */
254 	u8 param_id;		/* Param modified - defined below */
255 	u16 param_val;		/* New val of last param modified */
256 
257 	u8 write_config_counter;	/* write config occurred */
258 	u8 write_config_rsvd[3];
259 
260 	u8 ldrv_op_counter;	/* Indicates ldrv op started/completed */
261 	u8 ldrv_opid;		/* ldrv num */
262 	u8 ldrv_opcmd;		/* ldrv operation - defined below */
263 	u8 ldrv_opstatus;	/* status of the operation */
264 
265 	u8 ldrv_state_counter;	/* Indicates change of ldrv state */
266 	u8 ldrv_state_id;		/* ldrv num */
267 	u8 ldrv_state_new;	/* New state */
268 	u8 ldrv_state_old;	/* old state */
269 
270 	u8 pdrv_state_counter;	/* Indicates change of ldrv state */
271 	u8 pdrv_state_id;		/* pdrv id */
272 	u8 pdrv_state_new;	/* New state */
273 	u8 pdrv_state_old;	/* old state */
274 
275 	u8 pdrv_fmt_counter;	/* Indicates pdrv format started/over */
276 	u8 pdrv_fmt_id;		/* pdrv id */
277 	u8 pdrv_fmt_val;		/* format started/over */
278 	u8 pdrv_fmt_rsvd;
279 
280 	u8 targ_xfer_counter;	/* Indicates SCSI-2 Xfer rate change */
281 	u8 targ_xfer_id;	/* pdrv Id  */
282 	u8 targ_xfer_val;		/* new Xfer params of last pdrv */
283 	u8 targ_xfer_rsvd;
284 
285 	u8 fcloop_id_chg_counter;	/* Indicates loopid changed */
286 	u8 fcloopid_pdrvid;		/* pdrv id */
287 	u8 fcloop_id0;			/* loopid on fc loop 0 */
288 	u8 fcloop_id1;			/* loopid on fc loop 1 */
289 
290 	u8 fcloop_state_counter;	/* Indicates loop state changed */
291 	u8 fcloop_state0;		/* state of fc loop 0 */
292 	u8 fcloop_state1;		/* state of fc loop 1 */
293 	u8 fcloop_state_rsvd;
294 } __attribute__ ((packed));
295 
296 #define MAX_NOTIFY_SIZE     0x80
297 #define CUR_NOTIFY_SIZE     sizeof(struct notify)
298 
299 typedef struct {
300 	u32	data_size; /* current size in bytes (not including resvd) */
301 
302 	struct notify notify;
303 
304 	u8	notify_rsvd[MAX_NOTIFY_SIZE - CUR_NOTIFY_SIZE];
305 
306 	u8	rebuild_rate;		/* Rebuild rate (0% - 100%) */
307 	u8	cache_flush_interval;	/* In terms of Seconds */
308 	u8	sense_alert;
309 	u8	drive_insert_count;	/* drive insertion count */
310 
311 	u8	battery_status;
312 	u8	num_ldrv;		/* No. of Log Drives configured */
313 	u8	recon_state[MAX_LOGICAL_DRIVES_40LD / 8];	/* State of
314 							   reconstruct */
315 	u16	ldrv_op_status[MAX_LOGICAL_DRIVES_40LD / 8]; /* logdrv
316 								 Status */
317 
318 	u32	ldrv_size[MAX_LOGICAL_DRIVES_40LD];/* Size of each log drv */
319 	u8	ldrv_prop[MAX_LOGICAL_DRIVES_40LD];
320 	u8	ldrv_state[MAX_LOGICAL_DRIVES_40LD];/* State of log drives */
321 	u8	pdrv_state[FC_MAX_PHYSICAL_DEVICES];/* State of phys drvs. */
322 	u16	pdrv_format[FC_MAX_PHYSICAL_DEVICES / 16];
323 
324 	u8	targ_xfer[80];	/* phys device transfer rate */
325 	u8	pad1k[263];	/* 761 + 263reserved = 1024 bytes total size */
326 } __attribute__ ((packed)) mega_inquiry3;
327 
328 
329 /* Structures */
330 typedef struct {
331 	u8	max_commands;	/* Max concurrent commands supported */
332 	u8	rebuild_rate;	/* Rebuild rate - 0% thru 100% */
333 	u8	max_targ_per_chan;	/* Max targ per channel */
334 	u8	nchannels;	/* Number of channels on HBA */
335 	u8	fw_version[4];	/* Firmware version */
336 	u16	age_of_flash;	/* Number of times FW has been flashed */
337 	u8	chip_set_value;	/* Contents of 0xC0000832 */
338 	u8	dram_size;	/* In MB */
339 	u8	cache_flush_interval;	/* in seconds */
340 	u8	bios_version[4];
341 	u8	board_type;
342 	u8	sense_alert;
343 	u8	write_config_count;	/* Increase with every configuration
344 					   change */
345 	u8	drive_inserted_count;	/* Increase with every drive inserted
346 					 */
347 	u8	inserted_drive;	/* Channel:Id of inserted drive */
348 	u8	battery_status;	/*
349 				 * BIT 0: battery module missing
350 				 * BIT 1: VBAD
351 				 * BIT 2: temperature high
352 				 * BIT 3: battery pack missing
353 				 * BIT 4,5:
354 				 *   00 - charge complete
355 				 *   01 - fast charge in progress
356 				 *   10 - fast charge fail
357 				 *   11 - undefined
358 				 * Bit 6: counter > 1000
359 				 * Bit 7: Undefined
360 				 */
361 	u8	dec_fault_bus_info;
362 } __attribute__ ((packed)) mega_adp_info;
363 
364 
365 typedef struct {
366 	u8	num_ldrv;	/* Number of logical drives configured */
367 	u8	rsvd[3];
368 	u32	ldrv_size[MAX_LOGICAL_DRIVES_8LD];
369 	u8	ldrv_prop[MAX_LOGICAL_DRIVES_8LD];
370 	u8	ldrv_state[MAX_LOGICAL_DRIVES_8LD];
371 } __attribute__ ((packed)) mega_ldrv_info;
372 
373 typedef struct {
374 	u8	pdrv_state[MAX_PHYSICAL_DRIVES];
375 	u8	rsvd;
376 } __attribute__ ((packed)) mega_pdrv_info;
377 
378 /* RAID inquiry: Mailbox command 0x05*/
379 typedef struct {
380 	mega_adp_info	adapter_info;
381 	mega_ldrv_info	logdrv_info;
382 	mega_pdrv_info	pdrv_info;
383 } __attribute__ ((packed)) mraid_inquiry;
384 
385 
386 /* RAID extended inquiry: Mailbox command 0x04*/
387 typedef struct {
388 	mraid_inquiry	raid_inq;
389 	u16	phys_drv_format[MAX_CHANNELS];
390 	u8	stack_attn;
391 	u8	modem_status;
392 	u8	rsvd[2];
393 } __attribute__ ((packed)) mraid_ext_inquiry;
394 
395 
396 typedef struct {
397 	u8	channel;
398 	u8	target;
399 }__attribute__ ((packed)) adp_device;
400 
401 typedef struct {
402 	u32		start_blk;	/* starting block */
403 	u32		num_blks;	/* # of blocks */
404 	adp_device	device[MAX_ROW_SIZE_40LD];
405 }__attribute__ ((packed)) adp_span_40ld;
406 
407 typedef struct {
408 	u32		start_blk;	/* starting block */
409 	u32		num_blks;	/* # of blocks */
410 	adp_device	device[MAX_ROW_SIZE_8LD];
411 }__attribute__ ((packed)) adp_span_8ld;
412 
413 typedef struct {
414 	u8	span_depth;	/* Total # of spans */
415 	u8	level;		/* RAID level */
416 	u8	read_ahead;	/* read ahead, no read ahead, adaptive read
417 				   ahead */
418 	u8	stripe_sz;	/* Encoded stripe size */
419 	u8	status;		/* Status of the logical drive */
420 	u8	write_mode;	/* write mode, write_through/write_back */
421 	u8	direct_io;	/* direct io or through cache */
422 	u8	row_size;	/* Number of stripes in a row */
423 } __attribute__ ((packed)) logdrv_param;
424 
425 typedef struct {
426 	logdrv_param	lparam;
427 	adp_span_40ld	span[MAX_SPAN_DEPTH];
428 }__attribute__ ((packed)) logdrv_40ld;
429 
430 typedef struct {
431 	logdrv_param	lparam;
432 	adp_span_8ld	span[MAX_SPAN_DEPTH];
433 }__attribute__ ((packed)) logdrv_8ld;
434 
435 typedef struct {
436 	u8	type;		/* Type of the device */
437 	u8	cur_status;	/* current status of the device */
438 	u8	tag_depth;	/* Level of tagging */
439 	u8	sync_neg;	/* sync negotiation - ENABLE or DISABLE */
440 	u32	size;		/* configurable size in terms of 512 byte
441 				   blocks */
442 }__attribute__ ((packed)) phys_drv;
443 
444 typedef struct {
445 	u8		nlog_drives;		/* number of logical drives */
446 	u8		resvd[3];
447 	logdrv_40ld	ldrv[MAX_LOGICAL_DRIVES_40LD];
448 	phys_drv	pdrv[MAX_PHYSICAL_DRIVES];
449 }__attribute__ ((packed)) disk_array_40ld;
450 
451 typedef struct {
452 	u8		nlog_drives;	/* number of logical drives */
453 	u8		resvd[3];
454 	logdrv_8ld	ldrv[MAX_LOGICAL_DRIVES_8LD];
455 	phys_drv	pdrv[MAX_PHYSICAL_DRIVES];
456 }__attribute__ ((packed)) disk_array_8ld;
457 
458 
459 /*
460  * User ioctl structure.
461  * This structure will be used for Traditional Method ioctl interface
462  * commands (0x80),Alternate Buffer Method (0x81) ioctl commands and the
463  * Driver ioctls.
464  * The Driver ioctl interface handles the commands at the driver level,
465  * without being sent to the card.
466  */
467 /* system call imposed limit. Change accordingly */
468 #define IOCTL_MAX_DATALEN       4096
469 
470 struct uioctl_t {
471 	u32 inlen;
472 	u32 outlen;
473 	union {
474 		u8 fca[16];
475 		struct {
476 			u8 opcode;
477 			u8 subopcode;
478 			u16 adapno;
479 #if BITS_PER_LONG == 32
480 			u8 *buffer;
481 			u8 pad[4];
482 #endif
483 #if BITS_PER_LONG == 64
484 			u8 *buffer;
485 #endif
486 			u32 length;
487 		} __attribute__ ((packed)) fcs;
488 	} __attribute__ ((packed)) ui;
489 	u8 mbox[18];		/* 16 bytes + 2 status bytes */
490 	mega_passthru pthru;
491 #if BITS_PER_LONG == 32
492 	char __user *data;		/* buffer <= 4096 for 0x80 commands */
493 	char pad[4];
494 #endif
495 #if BITS_PER_LONG == 64
496 	char __user *data;
497 #endif
498 } __attribute__ ((packed));
499 
500 /*
501  * struct mcontroller is used to pass information about the controllers in the
502  * system. Its up to the application how to use the information. We are passing
503  * as much info about the cards as possible and useful. Before issuing the
504  * call to find information about the cards, the application needs to issue a
505  * ioctl first to find out the number of controllers in the system.
506  */
507 #define MAX_CONTROLLERS 32
508 
509 struct mcontroller {
510 	u64 base;
511 	u8 irq;
512 	u8 numldrv;
513 	u8 pcibus;
514 	u16 pcidev;
515 	u8 pcifun;
516 	u16 pciid;
517 	u16 pcivendor;
518 	u8 pcislot;
519 	u32 uid;
520 };
521 
522 /*
523  * mailbox structure used for internal commands
524  */
525 typedef struct {
526 	u8	cmd;
527 	u8	cmdid;
528 	u8	opcode;
529 	u8	subopcode;
530 	u32	lba;
531 	u32	xferaddr;
532 	u8	logdrv;
533 	u8	rsvd[3];
534 	u8	numstatus;
535 	u8	status;
536 } __attribute__ ((packed)) megacmd_t;
537 
538 /*
539  * Defines for Driver IOCTL interface
540  */
541 #define MEGAIOC_MAGIC  	'm'
542 
543 #define MEGAIOC_QNADAP		'm'	/* Query # of adapters */
544 #define MEGAIOC_QDRVRVER	'e'	/* Query driver version */
545 #define MEGAIOC_QADAPINFO   	'g'	/* Query adapter information */
546 #define MKADAP(adapno)	  	(MEGAIOC_MAGIC << 8 | (adapno) )
547 #define GETADAP(mkadap)	 	( (mkadap) ^ MEGAIOC_MAGIC << 8 )
548 
549 /*
550  * Definition for the new ioctl interface (NIT)
551  */
552 
553 /*
554  * Vendor specific Group-7 commands
555  */
556 #define VENDOR_SPECIFIC_COMMANDS	0xE0
557 #define MEGA_INTERNAL_CMD		VENDOR_SPECIFIC_COMMANDS + 0x01
558 
559 /*
560  * The ioctl command. No other command shall be used for this interface
561  */
562 #define USCSICMD	VENDOR_SPECIFIC_COMMANDS
563 
564 /*
565  * Data direction flags
566  */
567 #define UIOC_RD		0x00001
568 #define UIOC_WR		0x00002
569 
570 /*
571  * ioctl opcodes
572  */
573 #define MBOX_CMD	0x00000	/* DCMD or passthru command */
574 #define GET_DRIVER_VER	0x10000	/* Get driver version */
575 #define GET_N_ADAP	0x20000	/* Get number of adapters */
576 #define GET_ADAP_INFO	0x30000	/* Get information about a adapter */
577 #define GET_CAP		0x40000	/* Get ioctl capabilities */
578 #define GET_STATS	0x50000	/* Get statistics, including error info */
579 
580 
581 /*
582  * The ioctl structure.
583  * MBOX macro converts a nitioctl_t structure to megacmd_t pointer and
584  * MBOX_P macro converts a nitioctl_t pointer to megacmd_t pointer.
585  */
586 typedef struct {
587 	char		signature[8];	/* Must contain "MEGANIT" */
588 	u32		opcode;		/* opcode for the command */
589 	u32		adapno;		/* adapter number */
590 	union {
591 		u8	__raw_mbox[18];
592 		void __user *__uaddr; /* xferaddr for non-mbox cmds */
593 	}__ua;
594 
595 #define uioc_rmbox	__ua.__raw_mbox
596 #define MBOX(uioc)	((megacmd_t *)&((uioc).__ua.__raw_mbox[0]))
597 #define MBOX_P(uioc)	((megacmd_t __user *)&((uioc)->__ua.__raw_mbox[0]))
598 #define uioc_uaddr	__ua.__uaddr
599 
600 	u32		xferlen;	/* xferlen for DCMD and non-mbox
601 					   commands */
602 	u32		flags;		/* data direction flags */
603 }nitioctl_t;
604 
605 
606 /*
607  * I/O statistics for some applications like SNMP agent. The caller must
608  * provide the number of logical drives for which status should be reported.
609  */
610 typedef struct {
611 	int	num_ldrv;	/* Number for logical drives for which the
612 				   status should be reported. */
613 	u32	nreads[MAX_LOGICAL_DRIVES_40LD];	/* number of reads for
614 							each logical drive */
615 	u32	nreadblocks[MAX_LOGICAL_DRIVES_40LD];	/* number of blocks
616 							read for each logical
617 							drive */
618 	u32	nwrites[MAX_LOGICAL_DRIVES_40LD];	/* number of writes
619 							for each logical
620 							drive */
621 	u32	nwriteblocks[MAX_LOGICAL_DRIVES_40LD];	/* number of blocks
622 							writes for each
623 							logical drive */
624 	u32	rd_errors[MAX_LOGICAL_DRIVES_40LD];	/* number of read
625 							   errors for each
626 							   logical drive */
627 	u32	wr_errors[MAX_LOGICAL_DRIVES_40LD];	/* number of write
628 							   errors for each
629 							   logical drive */
630 }megastat_t;
631 
632 
633 struct private_bios_data {
634 	u8	geometry:4;	/*
635 				 * bits 0-3 - BIOS geometry
636 				 * 0x0001 - 1GB
637 				 * 0x0010 - 2GB
638 				 * 0x1000 - 8GB
639 				 * Others values are invalid
640 							 */
641 	u8	unused:4;	/* bits 4-7 are unused */
642 	u8	boot_drv;	/*
643 				 * logical drive set as boot drive
644 				 * 0..7 - for 8LD cards
645 				 * 0..39 - for 40LD cards
646 				 */
647 	u8	rsvd[12];
648 	u16	cksum;	/* 0-(sum of first 13 bytes of this structure) */
649 } __attribute__ ((packed));
650 
651 
652 
653 
654 /*
655  * Mailbox and firmware commands and subopcodes used in this driver.
656  */
657 
658 #define MEGA_MBOXCMD_LREAD	0x01
659 #define MEGA_MBOXCMD_LWRITE	0x02
660 #define MEGA_MBOXCMD_PASSTHRU	0x03
661 #define MEGA_MBOXCMD_ADPEXTINQ	0x04
662 #define MEGA_MBOXCMD_ADAPTERINQ	0x05
663 #define MEGA_MBOXCMD_LREAD64	0xA7
664 #define MEGA_MBOXCMD_LWRITE64	0xA8
665 #define MEGA_MBOXCMD_PASSTHRU64	0xC3
666 #define MEGA_MBOXCMD_EXTPTHRU	0xE3
667 
668 #define MAIN_MISC_OPCODE	0xA4	/* f/w misc opcode */
669 #define GET_MAX_SG_SUPPORT	0x01	/* get max sg len supported by f/w */
670 
671 #define FC_NEW_CONFIG		0xA1
672 #define NC_SUBOP_PRODUCT_INFO	0x0E
673 #define NC_SUBOP_ENQUIRY3	0x0F
674 #define ENQ3_GET_SOLICITED_FULL	0x02
675 #define OP_DCMD_READ_CONFIG	0x04
676 #define NEW_READ_CONFIG_8LD	0x67
677 #define READ_CONFIG_8LD		0x07
678 #define FLUSH_ADAPTER		0x0A
679 #define FLUSH_SYSTEM		0xFE
680 
681 /*
682  * Command for random deletion of logical drives
683  */
684 #define	FC_DEL_LOGDRV		0xA4	/* f/w command */
685 #define	OP_SUP_DEL_LOGDRV	0x2A	/* is feature supported */
686 #define OP_GET_LDID_MAP		0x18	/* get ldid and logdrv number map */
687 #define OP_DEL_LOGDRV		0x1C	/* delete logical drive */
688 
689 /*
690  * BIOS commands
691  */
692 #define IS_BIOS_ENABLED		0x62
693 #define GET_BIOS		0x01
694 #define CHNL_CLASS		0xA9
695 #define GET_CHNL_CLASS		0x00
696 #define SET_CHNL_CLASS		0x01
697 #define CH_RAID			0x01
698 #define CH_SCSI			0x00
699 #define BIOS_PVT_DATA		0x40
700 #define GET_BIOS_PVT_DATA	0x00
701 
702 
703 /*
704  * Commands to support clustering
705  */
706 #define MEGA_GET_TARGET_ID	0x7D
707 #define MEGA_CLUSTER_OP		0x70
708 #define MEGA_GET_CLUSTER_MODE	0x02
709 #define MEGA_CLUSTER_CMD	0x6E
710 #define MEGA_RESERVE_LD		0x01
711 #define MEGA_RELEASE_LD		0x02
712 #define MEGA_RESET_RESERVATIONS	0x03
713 #define MEGA_RESERVATION_STATUS	0x04
714 #define MEGA_RESERVE_PD		0x05
715 #define MEGA_RELEASE_PD		0x06
716 
717 
718 /*
719  * Module battery status
720  */
721 #define MEGA_BATT_MODULE_MISSING	0x01
722 #define MEGA_BATT_LOW_VOLTAGE		0x02
723 #define MEGA_BATT_TEMP_HIGH		0x04
724 #define MEGA_BATT_PACK_MISSING		0x08
725 #define MEGA_BATT_CHARGE_MASK		0x30
726 #define MEGA_BATT_CHARGE_DONE		0x00
727 #define MEGA_BATT_CHARGE_INPROG		0x10
728 #define MEGA_BATT_CHARGE_FAIL		0x20
729 #define MEGA_BATT_CYCLES_EXCEEDED	0x40
730 
731 /*
732  * Physical drive states.
733  */
734 #define PDRV_UNCNF	0
735 #define PDRV_ONLINE	3
736 #define PDRV_FAILED	4
737 #define PDRV_RBLD	5
738 #define PDRV_HOTSPARE	6
739 
740 
741 /*
742  * Raid logical drive states.
743  */
744 #define RDRV_OFFLINE	0
745 #define RDRV_DEGRADED	1
746 #define RDRV_OPTIMAL	2
747 #define RDRV_DELETED	3
748 
749 /*
750  * Read, write and cache policies
751  */
752 #define NO_READ_AHEAD		0
753 #define READ_AHEAD		1
754 #define ADAP_READ_AHEAD		2
755 #define WRMODE_WRITE_THRU	0
756 #define WRMODE_WRITE_BACK	1
757 #define CACHED_IO		0
758 #define DIRECT_IO		1
759 
760 struct megaraid_cmd_priv {
761 	struct list_head entry;
762 };
763 
764 #define SCSI_LIST(scp)							\
765 	(&((struct megaraid_cmd_priv *)scsi_cmd_priv(scp))->entry)
766 
767 struct scsi_cmd_and_priv {
768 	struct scsi_cmnd	 cmd;
769 	struct megaraid_cmd_priv priv;
770 };
771 
772 static inline struct scsi_cmnd *
773 megaraid_to_scsi_cmd(struct megaraid_cmd_priv *cmd_priv)
774 {
775 	/* See also scsi_mq_setup_tags() */
776 	BUILD_BUG_ON(sizeof(struct scsi_cmd_and_priv) !=
777 		     sizeof(struct scsi_cmnd) +
778 		     sizeof(struct megaraid_cmd_priv));
779 
780 	return &container_of(cmd_priv, struct scsi_cmd_and_priv, priv)->cmd;
781 }
782 
783 /*
784  * Each controller's soft state
785  */
786 typedef struct {
787 	int	this_id;	/* our id, may set to different than 7 if
788 				   clustering is available */
789 	u32	flag;
790 
791 	unsigned long		base;
792 	void __iomem		*mmio_base;
793 
794 	/* mbox64 with mbox not aligned on 16-byte boundary */
795 	mbox64_t	*una_mbox64;
796 	dma_addr_t	una_mbox64_dma;
797 
798 	volatile mbox64_t	*mbox64;/* ptr to 64-bit mailbox */
799 	volatile mbox_t		*mbox;	/* ptr to standard mailbox */
800 	dma_addr_t		mbox_dma;
801 
802 	struct pci_dev	*dev;
803 
804 	struct list_head	free_list;
805 	struct list_head	pending_list;
806 	struct list_head	completed_list;
807 
808 	struct Scsi_Host	*host;
809 
810 #define MEGA_BUFFER_SIZE (2*1024)
811 	u8		*mega_buffer;
812 	dma_addr_t	buf_dma_handle;
813 
814 	mega_product_info	product_info;
815 
816 	u8		max_cmds;
817 	scb_t		*scb_list;
818 
819 	atomic_t	pend_cmds;	/* maintain a counter for pending
820 					   commands in firmware */
821 
822 #if MEGA_HAVE_STATS
823 	u32	nreads[MAX_LOGICAL_DRIVES_40LD];
824 	u32	nreadblocks[MAX_LOGICAL_DRIVES_40LD];
825 	u32	nwrites[MAX_LOGICAL_DRIVES_40LD];
826 	u32	nwriteblocks[MAX_LOGICAL_DRIVES_40LD];
827 	u32	rd_errors[MAX_LOGICAL_DRIVES_40LD];
828 	u32	wr_errors[MAX_LOGICAL_DRIVES_40LD];
829 #endif
830 
831 	/* Host adapter parameters */
832 	u8	numldrv;
833 	u8	fw_version[7];
834 	u8	bios_version[7];
835 
836 #ifdef CONFIG_PROC_FS
837 	struct proc_dir_entry	*controller_proc_dir_entry;
838 #endif
839 
840 	int	has_64bit_addr;		/* are we using 64-bit addressing */
841 	int	support_ext_cdb;
842 	int	boot_ldrv_enabled;
843 	int	boot_ldrv;
844 	int	boot_pdrv_enabled;	/* boot from physical drive */
845 	int	boot_pdrv_ch;		/* boot physical drive channel */
846 	int	boot_pdrv_tgt;		/* boot physical drive target */
847 
848 
849 	int	support_random_del;	/* Do we support random deletion of
850 					   logdrvs */
851 	int	read_ldidmap;	/* set after logical drive deltion. The
852 				   logical drive number must be read from the
853 				   map */
854 	atomic_t	quiescent;	/* a stage reached when delete logical
855 					   drive needs to be done. Stop
856 					   sending requests to the hba till
857 					   delete operation is completed */
858 	spinlock_t	lock;
859 
860 	u8	logdrv_chan[MAX_CHANNELS+NVIRT_CHAN]; /* logical drive are on
861 							what channels. */
862 	int	mega_ch_class;
863 
864 	u8	sglen;	/* f/w supported scatter-gather list length */
865 
866 	scb_t			int_scb;
867 	struct mutex		int_mtx;	/* To synchronize the internal
868 						commands */
869 	int			int_status;	/* status of internal cmd */
870 	struct completion	int_waitq;	/* wait queue for internal
871 						 cmds */
872 
873 	int	has_cluster;	/* cluster support on this HBA */
874 }adapter_t;
875 
876 
877 struct mega_hbas {
878 	int is_bios_enabled;
879 	adapter_t *hostdata_addr;
880 };
881 
882 
883 /*
884  * For state flag. Do not use LSB(8 bits) which are
885  * reserved for storing info about channels.
886  */
887 #define IN_ABORT	0x80000000L
888 #define IN_RESET	0x40000000L
889 #define BOARD_MEMMAP	0x20000000L
890 #define BOARD_IOMAP	0x10000000L
891 #define BOARD_40LD   	0x08000000L
892 #define BOARD_64BIT	0x04000000L
893 
894 #define INTR_VALID			0x40
895 
896 #define PCI_CONF_AMISIG			0xa0
897 #define PCI_CONF_AMISIG64		0xa4
898 
899 
900 #define MEGA_DMA_TYPE_NONE		0xFFFF
901 #define MEGA_BULK_DATA			0x0001
902 #define MEGA_SGLIST			0x0002
903 
904 /*
905  * Parameters for the io-mapped controllers
906  */
907 
908 /* I/O Port offsets */
909 #define CMD_PORT	 	0x00
910 #define ACK_PORT	 	0x00
911 #define TOGGLE_PORT		0x01
912 #define INTR_PORT	  	0x0a
913 
914 #define MBOX_BUSY_PORT     	0x00
915 #define MBOX_PORT0	 	0x04
916 #define MBOX_PORT1	 	0x05
917 #define MBOX_PORT2	 	0x06
918 #define MBOX_PORT3	 	0x07
919 #define ENABLE_MBOX_REGION 	0x0B
920 
921 /* I/O Port Values */
922 #define ISSUE_BYTE	 	0x10
923 #define ACK_BYTE	   	0x08
924 #define ENABLE_INTR_BYTE   	0xc0
925 #define DISABLE_INTR_BYTE  	0x00
926 #define VALID_INTR_BYTE    	0x40
927 #define MBOX_BUSY_BYTE     	0x10
928 #define ENABLE_MBOX_BYTE   	0x00
929 
930 
931 /* Setup some port macros here */
932 #define issue_command(adapter)	\
933 		outb_p(ISSUE_BYTE, (adapter)->base + CMD_PORT)
934 
935 #define irq_state(adapter)	inb_p((adapter)->base + INTR_PORT)
936 
937 #define set_irq_state(adapter, value)	\
938 		outb_p((value), (adapter)->base + INTR_PORT)
939 
940 #define irq_ack(adapter)	\
941 		outb_p(ACK_BYTE, (adapter)->base + ACK_PORT)
942 
943 #define irq_enable(adapter)	\
944 	outb_p(ENABLE_INTR_BYTE, (adapter)->base + TOGGLE_PORT)
945 
946 #define irq_disable(adapter)	\
947 	outb_p(DISABLE_INTR_BYTE, (adapter)->base + TOGGLE_PORT)
948 
949 
950 /*
951  * This is our SYSDEP area. All kernel specific detail should be placed here -
952  * as much as possible
953  */
954 
955 /*
956  * End of SYSDEP area
957  */
958 
959 const char *megaraid_info (struct Scsi_Host *);
960 
961 static int mega_query_adapter(adapter_t *);
962 static int issue_scb(adapter_t *, scb_t *);
963 static int mega_setup_mailbox(adapter_t *);
964 
965 static int megaraid_queue (struct Scsi_Host *, struct scsi_cmnd *);
966 static scb_t * mega_build_cmd(adapter_t *, struct scsi_cmnd *, int *);
967 static void __mega_runpendq(adapter_t *);
968 static int issue_scb_block(adapter_t *, u_char *);
969 
970 static irqreturn_t megaraid_isr_memmapped(int, void *);
971 static irqreturn_t megaraid_isr_iomapped(int, void *);
972 
973 static void mega_free_scb(adapter_t *, scb_t *);
974 
975 static int megaraid_abort(struct scsi_cmnd *);
976 static int megaraid_reset(struct scsi_cmnd *);
977 static int megaraid_abort_and_reset(adapter_t *, struct scsi_cmnd *, int);
978 static int megaraid_biosparam(struct scsi_device *, struct block_device *,
979 		sector_t, int []);
980 
981 static int mega_build_sglist (adapter_t *adapter, scb_t *scb,
982 			      u32 *buffer, u32 *length);
983 static int __mega_busywait_mbox (adapter_t *);
984 static void mega_rundoneq (adapter_t *);
985 static void mega_cmd_done(adapter_t *, u8 [], int, int);
986 static inline void mega_free_sgl (adapter_t *adapter);
987 static void mega_8_to_40ld (mraid_inquiry *inquiry,
988 		mega_inquiry3 *enquiry3, mega_product_info *);
989 
990 static int megadev_open (struct inode *, struct file *);
991 static int megadev_ioctl (struct file *, unsigned int, unsigned long);
992 static int mega_m_to_n(void __user *, nitioctl_t *);
993 static int mega_n_to_m(void __user *, megacmd_t *);
994 
995 static int mega_init_scb (adapter_t *);
996 
997 static int mega_is_bios_enabled (adapter_t *);
998 
999 #ifdef CONFIG_PROC_FS
1000 static void mega_create_proc_entry(int, struct proc_dir_entry *);
1001 static int mega_adapinq(adapter_t *, dma_addr_t);
1002 static int mega_internal_dev_inquiry(adapter_t *, u8, u8, dma_addr_t);
1003 #endif
1004 
1005 static int mega_support_ext_cdb(adapter_t *);
1006 static mega_passthru* mega_prepare_passthru(adapter_t *, scb_t *,
1007 		struct scsi_cmnd *, int, int);
1008 static mega_ext_passthru* mega_prepare_extpassthru(adapter_t *,
1009 		scb_t *, struct scsi_cmnd *, int, int);
1010 static void mega_enum_raid_scsi(adapter_t *);
1011 static void mega_get_boot_drv(adapter_t *);
1012 static int mega_support_random_del(adapter_t *);
1013 static int mega_del_logdrv(adapter_t *, int);
1014 static int mega_do_del_logdrv(adapter_t *, int);
1015 static void mega_get_max_sgl(adapter_t *);
1016 static int mega_internal_command(adapter_t *, megacmd_t *, mega_passthru *);
1017 static int mega_support_cluster(adapter_t *);
1018 #endif
1019 
1020 /* vi: set ts=8 sw=8 tw=78: */
1021