1 /*
2  * This is the Fusion MPT base driver providing common API layer interface
3  * for access to MPT (Message Passing Technology) firmware.
4  *
5  * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.h
6  * Copyright (C) 2012-2014  LSI Corporation
7  * Copyright (C) 2013-2014 Avago Technologies
8  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * NO WARRANTY
21  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25  * solely responsible for determining the appropriateness of using and
26  * distributing the Program and assumes all risks associated with its
27  * exercise of rights under this Agreement, including but not limited to
28  * the risks and costs of program errors, damage to or loss of data,
29  * programs or equipment, and unavailability or interruption of operations.
30 
31  * DISCLAIMER OF LIABILITY
32  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
43  * USA.
44  */
45 
46 #ifndef MPT3SAS_BASE_H_INCLUDED
47 #define MPT3SAS_BASE_H_INCLUDED
48 
49 #include "mpi/mpi2_type.h"
50 #include "mpi/mpi2.h"
51 #include "mpi/mpi2_ioc.h"
52 #include "mpi/mpi2_cnfg.h"
53 #include "mpi/mpi2_init.h"
54 #include "mpi/mpi2_raid.h"
55 #include "mpi/mpi2_tool.h"
56 #include "mpi/mpi2_sas.h"
57 
58 #include <scsi/scsi.h>
59 #include <scsi/scsi_cmnd.h>
60 #include <scsi/scsi_device.h>
61 #include <scsi/scsi_host.h>
62 #include <scsi/scsi_tcq.h>
63 #include <scsi/scsi_transport_sas.h>
64 #include <scsi/scsi_dbg.h>
65 #include <scsi/scsi_eh.h>
66 #include <linux/pci.h>
67 #include <linux/poll.h>
68 
69 #include "mpt3sas_debug.h"
70 #include "mpt3sas_trigger_diag.h"
71 
72 /* driver versioning info */
73 #define MPT3SAS_DRIVER_NAME		"mpt3sas"
74 #define MPT3SAS_AUTHOR "Avago Technologies <MPT-FusionLinux.pdl@avagotech.com>"
75 #define MPT3SAS_DESCRIPTION	"LSI MPT Fusion SAS 3.0 Device Driver"
76 #define MPT3SAS_DRIVER_VERSION		"15.100.00.00"
77 #define MPT3SAS_MAJOR_VERSION		15
78 #define MPT3SAS_MINOR_VERSION		100
79 #define MPT3SAS_BUILD_VERSION		0
80 #define MPT3SAS_RELEASE_VERSION	00
81 
82 #define MPT2SAS_DRIVER_NAME		"mpt2sas"
83 #define MPT2SAS_DESCRIPTION	"LSI MPT Fusion SAS 2.0 Device Driver"
84 #define MPT2SAS_DRIVER_VERSION		"20.102.00.00"
85 #define MPT2SAS_MAJOR_VERSION		20
86 #define MPT2SAS_MINOR_VERSION		102
87 #define MPT2SAS_BUILD_VERSION		0
88 #define MPT2SAS_RELEASE_VERSION	00
89 
90 /*
91  * Set MPT3SAS_SG_DEPTH value based on user input.
92  */
93 #define MPT_MAX_PHYS_SEGMENTS	SG_CHUNK_SIZE
94 #define MPT_MIN_PHYS_SEGMENTS	16
95 
96 #ifdef CONFIG_SCSI_MPT3SAS_MAX_SGE
97 #define MPT3SAS_SG_DEPTH		CONFIG_SCSI_MPT3SAS_MAX_SGE
98 #else
99 #define MPT3SAS_SG_DEPTH		MPT_MAX_PHYS_SEGMENTS
100 #endif
101 
102 #ifdef CONFIG_SCSI_MPT2SAS_MAX_SGE
103 #define MPT2SAS_SG_DEPTH		CONFIG_SCSI_MPT2SAS_MAX_SGE
104 #else
105 #define MPT2SAS_SG_DEPTH		MPT_MAX_PHYS_SEGMENTS
106 #endif
107 
108 /*
109  * Generic Defines
110  */
111 #define MPT3SAS_SATA_QUEUE_DEPTH	32
112 #define MPT3SAS_SAS_QUEUE_DEPTH		254
113 #define MPT3SAS_RAID_QUEUE_DEPTH	128
114 
115 #define MPT3SAS_RAID_MAX_SECTORS	8192
116 
117 #define MPT_NAME_LENGTH			32	/* generic length of strings */
118 #define MPT_STRING_LENGTH		64
119 
120 #define MPT_MAX_CALLBACKS		32
121 
122 #define INTERNAL_CMDS_COUNT		10	/* reserved cmds */
123 /* reserved for issuing internally framed scsi io cmds */
124 #define INTERNAL_SCSIIO_CMDS_COUNT	3
125 
126 #define MPI3_HIM_MASK			0xFFFFFFFF /* mask every bit*/
127 
128 #define MPT3SAS_INVALID_DEVICE_HANDLE	0xFFFF
129 
130 #define MAX_CHAIN_ELEMT_SZ		16
131 #define DEFAULT_NUM_FWCHAIN_ELEMTS	8
132 
133 /*
134  * reset phases
135  */
136 #define MPT3_IOC_PRE_RESET		1 /* prior to host reset */
137 #define MPT3_IOC_AFTER_RESET		2 /* just after host reset */
138 #define MPT3_IOC_DONE_RESET		3 /* links re-initialized */
139 
140 /*
141  * logging format
142  */
143 #define MPT3SAS_FMT			"%s: "
144 
145 /*
146  *  WarpDrive Specific Log codes
147  */
148 
149 #define MPT2_WARPDRIVE_LOGENTRY		(0x8002)
150 #define MPT2_WARPDRIVE_LC_SSDT			(0x41)
151 #define MPT2_WARPDRIVE_LC_SSDLW		(0x43)
152 #define MPT2_WARPDRIVE_LC_SSDLF		(0x44)
153 #define MPT2_WARPDRIVE_LC_BRMF			(0x4D)
154 
155 /*
156  * per target private data
157  */
158 #define MPT_TARGET_FLAGS_RAID_COMPONENT	0x01
159 #define MPT_TARGET_FLAGS_VOLUME		0x02
160 #define MPT_TARGET_FLAGS_DELETED	0x04
161 #define MPT_TARGET_FASTPATH_IO		0x08
162 
163 #define SAS2_PCI_DEVICE_B0_REVISION	(0x01)
164 #define SAS3_PCI_DEVICE_C0_REVISION	(0x02)
165 
166 /*
167  * Intel HBA branding
168  */
169 #define MPT2SAS_INTEL_RMS25JB080_BRANDING    \
170 	"Intel(R) Integrated RAID Module RMS25JB080"
171 #define MPT2SAS_INTEL_RMS25JB040_BRANDING    \
172 	"Intel(R) Integrated RAID Module RMS25JB040"
173 #define MPT2SAS_INTEL_RMS25KB080_BRANDING    \
174 	"Intel(R) Integrated RAID Module RMS25KB080"
175 #define MPT2SAS_INTEL_RMS25KB040_BRANDING    \
176 	"Intel(R) Integrated RAID Module RMS25KB040"
177 #define MPT2SAS_INTEL_RMS25LB040_BRANDING	\
178 	"Intel(R) Integrated RAID Module RMS25LB040"
179 #define MPT2SAS_INTEL_RMS25LB080_BRANDING	\
180 	"Intel(R) Integrated RAID Module RMS25LB080"
181 #define MPT2SAS_INTEL_RMS2LL080_BRANDING	\
182 	"Intel Integrated RAID Module RMS2LL080"
183 #define MPT2SAS_INTEL_RMS2LL040_BRANDING	\
184 	"Intel Integrated RAID Module RMS2LL040"
185 #define MPT2SAS_INTEL_RS25GB008_BRANDING       \
186 	"Intel(R) RAID Controller RS25GB008"
187 #define MPT2SAS_INTEL_SSD910_BRANDING          \
188 	"Intel(R) SSD 910 Series"
189 
190 #define MPT3SAS_INTEL_RMS3JC080_BRANDING       \
191 	"Intel(R) Integrated RAID Module RMS3JC080"
192 #define MPT3SAS_INTEL_RS3GC008_BRANDING       \
193 	"Intel(R) RAID Controller RS3GC008"
194 #define MPT3SAS_INTEL_RS3FC044_BRANDING       \
195 	"Intel(R) RAID Controller RS3FC044"
196 #define MPT3SAS_INTEL_RS3UC080_BRANDING       \
197 	"Intel(R) RAID Controller RS3UC080"
198 
199 /*
200  * Intel HBA SSDIDs
201  */
202 #define MPT2SAS_INTEL_RMS25JB080_SSDID		0x3516
203 #define MPT2SAS_INTEL_RMS25JB040_SSDID		0x3517
204 #define MPT2SAS_INTEL_RMS25KB080_SSDID		0x3518
205 #define MPT2SAS_INTEL_RMS25KB040_SSDID		0x3519
206 #define MPT2SAS_INTEL_RMS25LB040_SSDID		0x351A
207 #define MPT2SAS_INTEL_RMS25LB080_SSDID		0x351B
208 #define MPT2SAS_INTEL_RMS2LL080_SSDID		0x350E
209 #define MPT2SAS_INTEL_RMS2LL040_SSDID		0x350F
210 #define MPT2SAS_INTEL_RS25GB008_SSDID		0x3000
211 #define MPT2SAS_INTEL_SSD910_SSDID		0x3700
212 
213 #define MPT3SAS_INTEL_RMS3JC080_SSDID		0x3521
214 #define MPT3SAS_INTEL_RS3GC008_SSDID		0x3522
215 #define MPT3SAS_INTEL_RS3FC044_SSDID		0x3523
216 #define MPT3SAS_INTEL_RS3UC080_SSDID		0x3524
217 
218 /*
219  * Dell HBA branding
220  */
221 #define MPT2SAS_DELL_BRANDING_SIZE                 32
222 
223 #define MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING        "Dell 6Gbps SAS HBA"
224 #define MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING    "Dell PERC H200 Adapter"
225 #define MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING "Dell PERC H200 Integrated"
226 #define MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING    "Dell PERC H200 Modular"
227 #define MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING   "Dell PERC H200 Embedded"
228 #define MPT2SAS_DELL_PERC_H200_BRANDING            "Dell PERC H200"
229 #define MPT2SAS_DELL_6GBPS_SAS_BRANDING            "Dell 6Gbps SAS"
230 
231 #define MPT3SAS_DELL_12G_HBA_BRANDING       \
232 	"Dell 12Gbps HBA"
233 
234 /*
235  * Dell HBA SSDIDs
236  */
237 #define MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID	0x1F1C
238 #define MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID	0x1F1D
239 #define MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID	0x1F1E
240 #define MPT2SAS_DELL_PERC_H200_MODULAR_SSDID	0x1F1F
241 #define MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID	0x1F20
242 #define MPT2SAS_DELL_PERC_H200_SSDID		0x1F21
243 #define MPT2SAS_DELL_6GBPS_SAS_SSDID		0x1F22
244 
245 #define MPT3SAS_DELL_12G_HBA_SSDID		0x1F46
246 
247 /*
248  * Cisco HBA branding
249  */
250 #define MPT3SAS_CISCO_12G_8E_HBA_BRANDING		\
251 	"Cisco 9300-8E 12G SAS HBA"
252 #define MPT3SAS_CISCO_12G_8I_HBA_BRANDING		\
253 	"Cisco 9300-8i 12G SAS HBA"
254 #define MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING	\
255 	"Cisco 12G Modular SAS Pass through Controller"
256 #define MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING		\
257 	"UCS C3X60 12G SAS Pass through Controller"
258 /*
259  * Cisco HBA SSSDIDs
260  */
261 #define MPT3SAS_CISCO_12G_8E_HBA_SSDID  0x14C
262 #define MPT3SAS_CISCO_12G_8I_HBA_SSDID  0x154
263 #define MPT3SAS_CISCO_12G_AVILA_HBA_SSDID  0x155
264 #define MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID  0x156
265 
266 /*
267  * status bits for ioc->diag_buffer_status
268  */
269 #define MPT3_DIAG_BUFFER_IS_REGISTERED	(0x01)
270 #define MPT3_DIAG_BUFFER_IS_RELEASED	(0x02)
271 #define MPT3_DIAG_BUFFER_IS_DIAG_RESET	(0x04)
272 
273 /*
274  * HP HBA branding
275  */
276 #define MPT2SAS_HP_3PAR_SSVID                0x1590
277 
278 #define MPT2SAS_HP_2_4_INTERNAL_BRANDING	\
279 	"HP H220 Host Bus Adapter"
280 #define MPT2SAS_HP_2_4_EXTERNAL_BRANDING	\
281 	"HP H221 Host Bus Adapter"
282 #define MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING	\
283 	"HP H222 Host Bus Adapter"
284 #define MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING	\
285 	"HP H220i Host Bus Adapter"
286 #define MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING	\
287 	"HP H210i Host Bus Adapter"
288 
289 /*
290  * HO HBA SSDIDs
291  */
292 #define MPT2SAS_HP_2_4_INTERNAL_SSDID			0x0041
293 #define MPT2SAS_HP_2_4_EXTERNAL_SSDID			0x0042
294 #define MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID	0x0043
295 #define MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID		0x0044
296 #define MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID		0x0046
297 
298 /*
299  * Combined Reply Queue constants,
300  * There are twelve Supplemental Reply Post Host Index Registers
301  * and each register is at offset 0x10 bytes from the previous one.
302  */
303 #define MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G3	12
304 #define MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G35	16
305 #define MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET	(0x10)
306 
307 /* OEM Identifiers */
308 #define MFG10_OEM_ID_INVALID                   (0x00000000)
309 #define MFG10_OEM_ID_DELL                      (0x00000001)
310 #define MFG10_OEM_ID_FSC                       (0x00000002)
311 #define MFG10_OEM_ID_SUN                       (0x00000003)
312 #define MFG10_OEM_ID_IBM                       (0x00000004)
313 
314 /* GENERIC Flags 0*/
315 #define MFG10_GF0_OCE_DISABLED                 (0x00000001)
316 #define MFG10_GF0_R1E_DRIVE_COUNT              (0x00000002)
317 #define MFG10_GF0_R10_DISPLAY                  (0x00000004)
318 #define MFG10_GF0_SSD_DATA_SCRUB_DISABLE       (0x00000008)
319 #define MFG10_GF0_SINGLE_DRIVE_R0              (0x00000010)
320 
321 #define VIRTUAL_IO_FAILED_RETRY			(0x32010081)
322 
323 /* OEM Specific Flags will come from OEM specific header files */
324 struct Mpi2ManufacturingPage10_t {
325 	MPI2_CONFIG_PAGE_HEADER	Header;		/* 00h */
326 	U8	OEMIdentifier;			/* 04h */
327 	U8	Reserved1;			/* 05h */
328 	U16	Reserved2;			/* 08h */
329 	U32	Reserved3;			/* 0Ch */
330 	U32	GenericFlags0;			/* 10h */
331 	U32	GenericFlags1;			/* 14h */
332 	U32	Reserved4;			/* 18h */
333 	U32	OEMSpecificFlags0;		/* 1Ch */
334 	U32	OEMSpecificFlags1;		/* 20h */
335 	U32	Reserved5[18];			/* 24h - 60h*/
336 };
337 
338 
339 /* Miscellaneous options */
340 struct Mpi2ManufacturingPage11_t {
341 	MPI2_CONFIG_PAGE_HEADER Header;		/* 00h */
342 	__le32	Reserved1;			/* 04h */
343 	u8	Reserved2;			/* 08h */
344 	u8	EEDPTagMode;			/* 09h */
345 	u8	Reserved3;			/* 0Ah */
346 	u8	Reserved4;			/* 0Bh */
347 	__le32	Reserved5[23];			/* 0Ch-60h*/
348 };
349 
350 /**
351  * struct MPT3SAS_TARGET - starget private hostdata
352  * @starget: starget object
353  * @sas_address: target sas address
354  * @raid_device: raid_device pointer to access volume data
355  * @handle: device handle
356  * @num_luns: number luns
357  * @flags: MPT_TARGET_FLAGS_XXX flags
358  * @deleted: target flaged for deletion
359  * @tm_busy: target is busy with TM request.
360  * @sdev: The sas_device associated with this target
361  */
362 struct MPT3SAS_TARGET {
363 	struct scsi_target *starget;
364 	u64	sas_address;
365 	struct _raid_device *raid_device;
366 	u16	handle;
367 	int	num_luns;
368 	u32	flags;
369 	u8	deleted;
370 	u8	tm_busy;
371 	struct _sas_device *sdev;
372 };
373 
374 
375 /*
376  * per device private data
377  */
378 #define MPT_DEVICE_FLAGS_INIT		0x01
379 
380 #define MFG_PAGE10_HIDE_SSDS_MASK	(0x00000003)
381 #define MFG_PAGE10_HIDE_ALL_DISKS	(0x00)
382 #define MFG_PAGE10_EXPOSE_ALL_DISKS	(0x01)
383 #define MFG_PAGE10_HIDE_IF_VOL_PRESENT	(0x02)
384 
385 /**
386  * struct MPT3SAS_DEVICE - sdev private hostdata
387  * @sas_target: starget private hostdata
388  * @lun: lun number
389  * @flags: MPT_DEVICE_XXX flags
390  * @configured_lun: lun is configured
391  * @block: device is in SDEV_BLOCK state
392  * @tlr_snoop_check: flag used in determining whether to disable TLR
393  * @eedp_enable: eedp support enable bit
394  * @eedp_type: 0(type_1), 1(type_2), 2(type_3)
395  * @eedp_block_length: block size
396  * @ata_command_pending: SATL passthrough outstanding for device
397  */
398 struct MPT3SAS_DEVICE {
399 	struct MPT3SAS_TARGET *sas_target;
400 	unsigned int	lun;
401 	u32	flags;
402 	u8	configured_lun;
403 	u8	block;
404 	u8	tlr_snoop_check;
405 	u8	ignore_delay_remove;
406 	/* Iopriority Command Handling */
407 	u8	ncq_prio_enable;
408 	/*
409 	 * Bug workaround for SATL handling: the mpt2/3sas firmware
410 	 * doesn't return BUSY or TASK_SET_FULL for subsequent
411 	 * commands while a SATL pass through is in operation as the
412 	 * spec requires, it simply does nothing with them until the
413 	 * pass through completes, causing them possibly to timeout if
414 	 * the passthrough is a long executing command (like format or
415 	 * secure erase).  This variable allows us to do the right
416 	 * thing while a SATL command is pending.
417 	 */
418 	unsigned long ata_command_pending;
419 
420 };
421 
422 #define MPT3_CMD_NOT_USED	0x8000	/* free */
423 #define MPT3_CMD_COMPLETE	0x0001	/* completed */
424 #define MPT3_CMD_PENDING	0x0002	/* pending */
425 #define MPT3_CMD_REPLY_VALID	0x0004	/* reply is valid */
426 #define MPT3_CMD_RESET		0x0008	/* host reset dropped the command */
427 
428 /**
429  * struct _internal_cmd - internal commands struct
430  * @mutex: mutex
431  * @done: completion
432  * @reply: reply message pointer
433  * @sense: sense data
434  * @status: MPT3_CMD_XXX status
435  * @smid: system message id
436  */
437 struct _internal_cmd {
438 	struct mutex mutex;
439 	struct completion done;
440 	void	*reply;
441 	void	*sense;
442 	u16	status;
443 	u16	smid;
444 };
445 
446 
447 
448 /**
449  * struct _sas_device - attached device information
450  * @list: sas device list
451  * @starget: starget object
452  * @sas_address: device sas address
453  * @device_name: retrieved from the SAS IDENTIFY frame.
454  * @handle: device handle
455  * @sas_address_parent: sas address of parent expander or sas host
456  * @enclosure_handle: enclosure handle
457  * @enclosure_logical_id: enclosure logical identifier
458  * @volume_handle: volume handle (valid when hidden raid member)
459  * @volume_wwid: volume unique identifier
460  * @device_info: bitfield provides detailed info about the device
461  * @id: target id
462  * @channel: target channel
463  * @slot: number number
464  * @phy: phy identifier provided in sas device page 0
465  * @responding: used in _scsih_sas_device_mark_responding
466  * @fast_path: fast path feature enable bit
467  * @pfa_led_on: flag for PFA LED status
468  * @pend_sas_rphy_add: flag to check if device is in sas_rphy_add()
469  *	addition routine.
470  */
471 struct _sas_device {
472 	struct list_head list;
473 	struct scsi_target *starget;
474 	u64	sas_address;
475 	u64	device_name;
476 	u16	handle;
477 	u64	sas_address_parent;
478 	u16	enclosure_handle;
479 	u64	enclosure_logical_id;
480 	u16	volume_handle;
481 	u64	volume_wwid;
482 	u32	device_info;
483 	int	id;
484 	int	channel;
485 	u16	slot;
486 	u8	phy;
487 	u8	responding;
488 	u8	fast_path;
489 	u8	pfa_led_on;
490 	u8	pend_sas_rphy_add;
491 	u8	enclosure_level;
492 	u8	connector_name[5];
493 	struct kref refcount;
494 };
495 
496 static inline void sas_device_get(struct _sas_device *s)
497 {
498 	kref_get(&s->refcount);
499 }
500 
501 static inline void sas_device_free(struct kref *r)
502 {
503 	kfree(container_of(r, struct _sas_device, refcount));
504 }
505 
506 static inline void sas_device_put(struct _sas_device *s)
507 {
508 	kref_put(&s->refcount, sas_device_free);
509 }
510 
511 /**
512  * struct _raid_device - raid volume link list
513  * @list: sas device list
514  * @starget: starget object
515  * @sdev: scsi device struct (volumes are single lun)
516  * @wwid: unique identifier for the volume
517  * @handle: device handle
518  * @block_size: Block size of the volume
519  * @id: target id
520  * @channel: target channel
521  * @volume_type: the raid level
522  * @device_info: bitfield provides detailed info about the hidden components
523  * @num_pds: number of hidden raid components
524  * @responding: used in _scsih_raid_device_mark_responding
525  * @percent_complete: resync percent complete
526  * @direct_io_enabled: Whether direct io to PDs are allowed or not
527  * @stripe_exponent: X where 2powX is the stripe sz in blocks
528  * @block_exponent: X where 2powX is the block sz in bytes
529  * @max_lba: Maximum number of LBA in the volume
530  * @stripe_sz: Stripe Size of the volume
531  * @device_info: Device info of the volume member disk
532  * @pd_handle: Array of handles of the physical drives for direct I/O in le16
533  */
534 #define MPT_MAX_WARPDRIVE_PDS		8
535 struct _raid_device {
536 	struct list_head list;
537 	struct scsi_target *starget;
538 	struct scsi_device *sdev;
539 	u64	wwid;
540 	u16	handle;
541 	u16	block_sz;
542 	int	id;
543 	int	channel;
544 	u8	volume_type;
545 	u8	num_pds;
546 	u8	responding;
547 	u8	percent_complete;
548 	u8	direct_io_enabled;
549 	u8	stripe_exponent;
550 	u8	block_exponent;
551 	u64	max_lba;
552 	u32	stripe_sz;
553 	u32	device_info;
554 	u16	pd_handle[MPT_MAX_WARPDRIVE_PDS];
555 };
556 
557 /**
558  * struct _boot_device - boot device info
559  * @is_raid: flag to indicate whether this is volume
560  * @device: holds pointer for either struct _sas_device or
561  *     struct _raid_device
562  */
563 struct _boot_device {
564 	u8 is_raid;
565 	void *device;
566 };
567 
568 /**
569  * struct _sas_port - wide/narrow sas port information
570  * @port_list: list of ports belonging to expander
571  * @num_phys: number of phys belonging to this port
572  * @remote_identify: attached device identification
573  * @rphy: sas transport rphy object
574  * @port: sas transport wide/narrow port object
575  * @phy_list: _sas_phy list objects belonging to this port
576  */
577 struct _sas_port {
578 	struct list_head port_list;
579 	u8	num_phys;
580 	struct sas_identify remote_identify;
581 	struct sas_rphy *rphy;
582 	struct sas_port *port;
583 	struct list_head phy_list;
584 };
585 
586 /**
587  * struct _sas_phy - phy information
588  * @port_siblings: list of phys belonging to a port
589  * @identify: phy identification
590  * @remote_identify: attached device identification
591  * @phy: sas transport phy object
592  * @phy_id: unique phy id
593  * @handle: device handle for this phy
594  * @attached_handle: device handle for attached device
595  * @phy_belongs_to_port: port has been created for this phy
596  */
597 struct _sas_phy {
598 	struct list_head port_siblings;
599 	struct sas_identify identify;
600 	struct sas_identify remote_identify;
601 	struct sas_phy *phy;
602 	u8	phy_id;
603 	u16	handle;
604 	u16	attached_handle;
605 	u8	phy_belongs_to_port;
606 };
607 
608 /**
609  * struct _sas_node - sas_host/expander information
610  * @list: list of expanders
611  * @parent_dev: parent device class
612  * @num_phys: number phys belonging to this sas_host/expander
613  * @sas_address: sas address of this sas_host/expander
614  * @handle: handle for this sas_host/expander
615  * @sas_address_parent: sas address of parent expander or sas host
616  * @enclosure_handle: handle for this a member of an enclosure
617  * @device_info: bitwise defining capabilities of this sas_host/expander
618  * @responding: used in _scsih_expander_device_mark_responding
619  * @phy: a list of phys that make up this sas_host/expander
620  * @sas_port_list: list of ports attached to this sas_host/expander
621  */
622 struct _sas_node {
623 	struct list_head list;
624 	struct device *parent_dev;
625 	u8	num_phys;
626 	u64	sas_address;
627 	u16	handle;
628 	u64	sas_address_parent;
629 	u16	enclosure_handle;
630 	u64	enclosure_logical_id;
631 	u8	responding;
632 	struct	_sas_phy *phy;
633 	struct list_head sas_port_list;
634 };
635 
636 /**
637  * enum reset_type - reset state
638  * @FORCE_BIG_HAMMER: issue diagnostic reset
639  * @SOFT_RESET: issue message_unit_reset, if fails to to big hammer
640  */
641 enum reset_type {
642 	FORCE_BIG_HAMMER,
643 	SOFT_RESET,
644 };
645 
646 /**
647  * struct chain_tracker - firmware chain tracker
648  * @chain_buffer: chain buffer
649  * @chain_buffer_dma: physical address
650  * @tracker_list: list of free request (ioc->free_chain_list)
651  */
652 struct chain_tracker {
653 	void *chain_buffer;
654 	dma_addr_t chain_buffer_dma;
655 	struct list_head tracker_list;
656 };
657 
658 /**
659  * struct scsiio_tracker - scsi mf request tracker
660  * @smid: system message id
661  * @scmd: scsi request pointer
662  * @cb_idx: callback index
663  * @direct_io: To indicate whether I/O is direct (WARPDRIVE)
664  * @tracker_list: list of free request (ioc->free_list)
665  * @msix_io: IO's msix
666  */
667 struct scsiio_tracker {
668 	u16	smid;
669 	struct scsi_cmnd *scmd;
670 	u8	cb_idx;
671 	u8	direct_io;
672 	struct list_head chain_list;
673 	struct list_head tracker_list;
674 	u16     msix_io;
675 };
676 
677 /**
678  * struct request_tracker - firmware request tracker
679  * @smid: system message id
680  * @cb_idx: callback index
681  * @tracker_list: list of free request (ioc->free_list)
682  */
683 struct request_tracker {
684 	u16	smid;
685 	u8	cb_idx;
686 	struct list_head tracker_list;
687 };
688 
689 /**
690  * struct _tr_list - target reset list
691  * @handle: device handle
692  * @state: state machine
693  */
694 struct _tr_list {
695 	struct list_head list;
696 	u16	handle;
697 	u16	state;
698 };
699 
700 /**
701  * struct _sc_list - delayed SAS_IO_UNIT_CONTROL message list
702  * @handle: device handle
703  */
704 struct _sc_list {
705 	struct list_head list;
706 	u16     handle;
707 };
708 
709 /**
710  * struct _event_ack_list - delayed event acknowledgment list
711  * @Event: Event ID
712  * @EventContext: used to track the event uniquely
713  */
714 struct _event_ack_list {
715 	struct list_head list;
716 	u16     Event;
717 	u32     EventContext;
718 };
719 
720 /**
721  * struct adapter_reply_queue - the reply queue struct
722  * @ioc: per adapter object
723  * @msix_index: msix index into vector table
724  * @vector: irq vector
725  * @reply_post_host_index: head index in the pool where FW completes IO
726  * @reply_post_free: reply post base virt address
727  * @name: the name registered to request_irq()
728  * @busy: isr is actively processing replies on another cpu
729  * @list: this list
730 */
731 struct adapter_reply_queue {
732 	struct MPT3SAS_ADAPTER	*ioc;
733 	u8			msix_index;
734 	u32			reply_post_host_index;
735 	Mpi2ReplyDescriptorsUnion_t *reply_post_free;
736 	char			name[MPT_NAME_LENGTH];
737 	atomic_t		busy;
738 	struct list_head	list;
739 };
740 
741 typedef void (*MPT_ADD_SGE)(void *paddr, u32 flags_length, dma_addr_t dma_addr);
742 
743 /* SAS3.0 support */
744 typedef int (*MPT_BUILD_SG_SCMD)(struct MPT3SAS_ADAPTER *ioc,
745 		struct scsi_cmnd *scmd, u16 smid);
746 typedef void (*MPT_BUILD_SG)(struct MPT3SAS_ADAPTER *ioc, void *psge,
747 		dma_addr_t data_out_dma, size_t data_out_sz,
748 		dma_addr_t data_in_dma, size_t data_in_sz);
749 typedef void (*MPT_BUILD_ZERO_LEN_SGE)(struct MPT3SAS_ADAPTER *ioc,
750 		void *paddr);
751 
752 /* To support atomic and non atomic descriptors*/
753 typedef void (*PUT_SMID_IO_FP_HIP) (struct MPT3SAS_ADAPTER *ioc, u16 smid,
754 	u16 funcdep);
755 typedef void (*PUT_SMID_DEFAULT) (struct MPT3SAS_ADAPTER *ioc, u16 smid);
756 
757 /* IOC Facts and Port Facts converted from little endian to cpu */
758 union mpi3_version_union {
759 	MPI2_VERSION_STRUCT		Struct;
760 	u32				Word;
761 };
762 
763 struct mpt3sas_facts {
764 	u16			MsgVersion;
765 	u16			HeaderVersion;
766 	u8			IOCNumber;
767 	u8			VP_ID;
768 	u8			VF_ID;
769 	u16			IOCExceptions;
770 	u16			IOCStatus;
771 	u32			IOCLogInfo;
772 	u8			MaxChainDepth;
773 	u8			WhoInit;
774 	u8			NumberOfPorts;
775 	u8			MaxMSIxVectors;
776 	u16			RequestCredit;
777 	u16			ProductID;
778 	u32			IOCCapabilities;
779 	union mpi3_version_union	FWVersion;
780 	u16			IOCRequestFrameSize;
781 	u16			IOCMaxChainSegmentSize;
782 	u16			MaxInitiators;
783 	u16			MaxTargets;
784 	u16			MaxSasExpanders;
785 	u16			MaxEnclosures;
786 	u16			ProtocolFlags;
787 	u16			HighPriorityCredit;
788 	u16			MaxReplyDescriptorPostQueueDepth;
789 	u8			ReplyFrameSize;
790 	u8			MaxVolumes;
791 	u16			MaxDevHandle;
792 	u16			MaxPersistentEntries;
793 	u16			MinDevHandle;
794 };
795 
796 struct mpt3sas_port_facts {
797 	u8			PortNumber;
798 	u8			VP_ID;
799 	u8			VF_ID;
800 	u8			PortType;
801 	u16			MaxPostedCmdBuffers;
802 };
803 
804 struct reply_post_struct {
805 	Mpi2ReplyDescriptorsUnion_t	*reply_post_free;
806 	dma_addr_t			reply_post_free_dma;
807 };
808 
809 typedef void (*MPT3SAS_FLUSH_RUNNING_CMDS)(struct MPT3SAS_ADAPTER *ioc);
810 /**
811  * struct MPT3SAS_ADAPTER - per adapter struct
812  * @list: ioc_list
813  * @shost: shost object
814  * @id: unique adapter id
815  * @cpu_count: number online cpus
816  * @name: generic ioc string
817  * @tmp_string: tmp string used for logging
818  * @pdev: pci pdev object
819  * @pio_chip: physical io register space
820  * @chip: memory mapped register space
821  * @chip_phys: physical addrss prior to mapping
822  * @logging_level: see mpt3sas_debug.h
823  * @fwfault_debug: debuging FW timeouts
824  * @ir_firmware: IR firmware present
825  * @bars: bitmask of BAR's that must be configured
826  * @mask_interrupts: ignore interrupt
827  * @dma_mask: used to set the consistent dma mask
828  * @fault_reset_work_q_name: fw fault work queue
829  * @fault_reset_work_q: ""
830  * @fault_reset_work: ""
831  * @firmware_event_name: fw event work queue
832  * @firmware_event_thread: ""
833  * @fw_event_lock:
834  * @fw_event_list: list of fw events
835  * @aen_event_read_flag: event log was read
836  * @broadcast_aen_busy: broadcast aen waiting to be serviced
837  * @shost_recovery: host reset in progress
838  * @ioc_reset_in_progress_lock:
839  * @ioc_link_reset_in_progress: phy/hard reset in progress
840  * @ignore_loginfos: ignore loginfos during task management
841  * @remove_host: flag for when driver unloads, to avoid sending dev resets
842  * @pci_error_recovery: flag to prevent ioc access until slot reset completes
843  * @wait_for_discovery_to_complete: flag set at driver load time when
844  *                                               waiting on reporting devices
845  * @is_driver_loading: flag set at driver load time
846  * @port_enable_failed: flag set when port enable has failed
847  * @start_scan: flag set from scan_start callback, cleared from _mpt3sas_fw_work
848  * @start_scan_failed: means port enable failed, return's the ioc_status
849  * @msix_enable: flag indicating msix is enabled
850  * @msix_vector_count: number msix vectors
851  * @cpu_msix_table: table for mapping cpus to msix index
852  * @cpu_msix_table_sz: table size
853  * @schedule_dead_ioc_flush_running_cmds: callback to flush pending commands
854  * @scsi_io_cb_idx: shost generated commands
855  * @tm_cb_idx: task management commands
856  * @scsih_cb_idx: scsih internal commands
857  * @transport_cb_idx: transport internal commands
858  * @ctl_cb_idx: clt internal commands
859  * @base_cb_idx: base internal commands
860  * @config_cb_idx: base internal commands
861  * @tm_tr_cb_idx : device removal target reset handshake
862  * @tm_tr_volume_cb_idx : volume removal target reset
863  * @base_cmds:
864  * @transport_cmds:
865  * @scsih_cmds:
866  * @tm_cmds:
867  * @ctl_cmds:
868  * @config_cmds:
869  * @base_add_sg_single: handler for either 32/64 bit sgl's
870  * @event_type: bits indicating which events to log
871  * @event_context: unique id for each logged event
872  * @event_log: event log pointer
873  * @event_masks: events that are masked
874  * @facts: static facts data
875  * @pfacts: static port facts data
876  * @manu_pg0: static manufacturing page 0
877  * @manu_pg10: static manufacturing page 10
878  * @manu_pg11: static manufacturing page 11
879  * @bios_pg2: static bios page 2
880  * @bios_pg3: static bios page 3
881  * @ioc_pg8: static ioc page 8
882  * @iounit_pg0: static iounit page 0
883  * @iounit_pg1: static iounit page 1
884  * @iounit_pg8: static iounit page 8
885  * @sas_hba: sas host object
886  * @sas_expander_list: expander object list
887  * @sas_node_lock:
888  * @sas_device_list: sas device object list
889  * @sas_device_init_list: sas device object list (used only at init time)
890  * @sas_device_lock:
891  * @io_missing_delay: time for IO completed by fw when PDR enabled
892  * @device_missing_delay: time for device missing by fw when PDR enabled
893  * @sas_id : used for setting volume target IDs
894  * @blocking_handles: bitmask used to identify which devices need blocking
895  * @pd_handles : bitmask for PD handles
896  * @pd_handles_sz : size of pd_handle bitmask
897  * @config_page_sz: config page size
898  * @config_page: reserve memory for config page payload
899  * @config_page_dma:
900  * @hba_queue_depth: hba request queue depth
901  * @sge_size: sg element size for either 32/64 bit
902  * @scsiio_depth: SCSI_IO queue depth
903  * @request_sz: per request frame size
904  * @request: pool of request frames
905  * @request_dma:
906  * @request_dma_sz:
907  * @scsi_lookup: firmware request tracker list
908  * @scsi_lookup_lock:
909  * @free_list: free list of request
910  * @pending_io_count:
911  * @reset_wq:
912  * @chain: pool of chains
913  * @chain_dma:
914  * @max_sges_in_main_message: number sg elements in main message
915  * @max_sges_in_chain_message: number sg elements per chain
916  * @chains_needed_per_io: max chains per io
917  * @chain_depth: total chains allocated
918  * @chain_segment_sz: gives the max number of
919  *			SGEs accommodate on single chain buffer
920  * @hi_priority_smid:
921  * @hi_priority:
922  * @hi_priority_dma:
923  * @hi_priority_depth:
924  * @hpr_lookup:
925  * @hpr_free_list:
926  * @internal_smid:
927  * @internal:
928  * @internal_dma:
929  * @internal_depth:
930  * @internal_lookup:
931  * @internal_free_list:
932  * @sense: pool of sense
933  * @sense_dma:
934  * @sense_dma_pool:
935  * @reply_depth: hba reply queue depth:
936  * @reply_sz: per reply frame size:
937  * @reply: pool of replys:
938  * @reply_dma:
939  * @reply_dma_pool:
940  * @reply_free_queue_depth: reply free depth
941  * @reply_free: pool for reply free queue (32 bit addr)
942  * @reply_free_dma:
943  * @reply_free_dma_pool:
944  * @reply_free_host_index: tail index in pool to insert free replys
945  * @reply_post_queue_depth: reply post queue depth
946  * @reply_post_struct: struct for reply_post_free physical & virt address
947  * @rdpq_array_capable: FW supports multiple reply queue addresses in ioc_init
948  * @rdpq_array_enable: rdpq_array support is enabled in the driver
949  * @rdpq_array_enable_assigned: this ensures that rdpq_array_enable flag
950  *				is assigned only ones
951  * @reply_queue_count: number of reply queue's
952  * @reply_queue_list: link list contaning the reply queue info
953  * @msix96_vector: 96 MSI-X vector support
954  * @replyPostRegisterIndex: index of next position in Reply Desc Post Queue
955  * @delayed_tr_list: target reset link list
956  * @delayed_tr_volume_list: volume target reset link list
957  * @delayed_sc_list:
958  * @delayed_event_ack_list:
959  * @temp_sensors_count: flag to carry the number of temperature sensors
960  * @pci_access_mutex: Mutex to synchronize ioctl,sysfs show path and
961  *	pci resource handling. PCI resource freeing will lead to free
962  *	vital hardware/memory resource, which might be in use by cli/sysfs
963  *	path functions resulting in Null pointer reference followed by kernel
964  *	crash. To avoid the above race condition we use mutex syncrhonization
965  *	which ensures the syncrhonization between cli/sysfs_show path.
966  */
967 struct MPT3SAS_ADAPTER {
968 	struct list_head list;
969 	struct Scsi_Host *shost;
970 	u8		id;
971 	int		cpu_count;
972 	char		name[MPT_NAME_LENGTH];
973 	char		driver_name[MPT_NAME_LENGTH];
974 	char		tmp_string[MPT_STRING_LENGTH];
975 	struct pci_dev	*pdev;
976 	Mpi2SystemInterfaceRegs_t __iomem *chip;
977 	resource_size_t	chip_phys;
978 	int		logging_level;
979 	int		fwfault_debug;
980 	u8		ir_firmware;
981 	int		bars;
982 	u8		mask_interrupts;
983 	int		dma_mask;
984 
985 	/* fw fault handler */
986 	char		fault_reset_work_q_name[20];
987 	struct workqueue_struct *fault_reset_work_q;
988 	struct delayed_work fault_reset_work;
989 
990 	/* fw event handler */
991 	char		firmware_event_name[20];
992 	struct workqueue_struct	*firmware_event_thread;
993 	spinlock_t	fw_event_lock;
994 	struct list_head fw_event_list;
995 
996 	 /* misc flags */
997 	int		aen_event_read_flag;
998 	u8		broadcast_aen_busy;
999 	u16		broadcast_aen_pending;
1000 	u8		shost_recovery;
1001 	u8		got_task_abort_from_ioctl;
1002 
1003 	struct mutex	reset_in_progress_mutex;
1004 	spinlock_t	ioc_reset_in_progress_lock;
1005 	u8		ioc_link_reset_in_progress;
1006 	u8		ioc_reset_in_progress_status;
1007 
1008 	u8		ignore_loginfos;
1009 	u8		remove_host;
1010 	u8		pci_error_recovery;
1011 	u8		wait_for_discovery_to_complete;
1012 	u8		is_driver_loading;
1013 	u8		port_enable_failed;
1014 	u8		start_scan;
1015 	u16		start_scan_failed;
1016 
1017 	u8		msix_enable;
1018 	u16		msix_vector_count;
1019 	u8		*cpu_msix_table;
1020 	u16		cpu_msix_table_sz;
1021 	resource_size_t __iomem **reply_post_host_index;
1022 	u32		ioc_reset_count;
1023 	MPT3SAS_FLUSH_RUNNING_CMDS schedule_dead_ioc_flush_running_cmds;
1024 	u32             non_operational_loop;
1025 
1026 	/* internal commands, callback index */
1027 	u8		scsi_io_cb_idx;
1028 	u8		tm_cb_idx;
1029 	u8		transport_cb_idx;
1030 	u8		scsih_cb_idx;
1031 	u8		ctl_cb_idx;
1032 	u8		base_cb_idx;
1033 	u8		port_enable_cb_idx;
1034 	u8		config_cb_idx;
1035 	u8		tm_tr_cb_idx;
1036 	u8		tm_tr_volume_cb_idx;
1037 	u8		tm_sas_control_cb_idx;
1038 	struct _internal_cmd base_cmds;
1039 	struct _internal_cmd port_enable_cmds;
1040 	struct _internal_cmd transport_cmds;
1041 	struct _internal_cmd scsih_cmds;
1042 	struct _internal_cmd tm_cmds;
1043 	struct _internal_cmd ctl_cmds;
1044 	struct _internal_cmd config_cmds;
1045 
1046 	MPT_ADD_SGE	base_add_sg_single;
1047 
1048 	/* function ptr for either IEEE or MPI sg elements */
1049 	MPT_BUILD_SG_SCMD build_sg_scmd;
1050 	MPT_BUILD_SG    build_sg;
1051 	MPT_BUILD_ZERO_LEN_SGE build_zero_len_sge;
1052 	u16             sge_size_ieee;
1053 	u16		hba_mpi_version_belonged;
1054 
1055 	/* function ptr for MPI sg elements only */
1056 	MPT_BUILD_SG    build_sg_mpi;
1057 	MPT_BUILD_ZERO_LEN_SGE build_zero_len_sge_mpi;
1058 
1059 	/* event log */
1060 	u32		event_type[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
1061 	u32		event_context;
1062 	void		*event_log;
1063 	u32		event_masks[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
1064 
1065 	/* static config pages */
1066 	struct mpt3sas_facts facts;
1067 	struct mpt3sas_port_facts *pfacts;
1068 	Mpi2ManufacturingPage0_t manu_pg0;
1069 	struct Mpi2ManufacturingPage10_t manu_pg10;
1070 	struct Mpi2ManufacturingPage11_t manu_pg11;
1071 	Mpi2BiosPage2_t	bios_pg2;
1072 	Mpi2BiosPage3_t	bios_pg3;
1073 	Mpi2IOCPage8_t ioc_pg8;
1074 	Mpi2IOUnitPage0_t iounit_pg0;
1075 	Mpi2IOUnitPage1_t iounit_pg1;
1076 	Mpi2IOUnitPage8_t iounit_pg8;
1077 
1078 	struct _boot_device req_boot_device;
1079 	struct _boot_device req_alt_boot_device;
1080 	struct _boot_device current_boot_device;
1081 
1082 	/* sas hba, expander, and device list */
1083 	struct _sas_node sas_hba;
1084 	struct list_head sas_expander_list;
1085 	spinlock_t	sas_node_lock;
1086 	struct list_head sas_device_list;
1087 	struct list_head sas_device_init_list;
1088 	spinlock_t	sas_device_lock;
1089 	struct list_head raid_device_list;
1090 	spinlock_t	raid_device_lock;
1091 	u8		io_missing_delay;
1092 	u16		device_missing_delay;
1093 	int		sas_id;
1094 
1095 	void		*blocking_handles;
1096 	void		*pd_handles;
1097 	u16		pd_handles_sz;
1098 
1099 	void		*pend_os_device_add;
1100 	u16		pend_os_device_add_sz;
1101 
1102 	/* config page */
1103 	u16		config_page_sz;
1104 	void		*config_page;
1105 	dma_addr_t	config_page_dma;
1106 
1107 	/* scsiio request */
1108 	u16		hba_queue_depth;
1109 	u16		sge_size;
1110 	u16		scsiio_depth;
1111 	u16		request_sz;
1112 	u8		*request;
1113 	dma_addr_t	request_dma;
1114 	u32		request_dma_sz;
1115 	struct scsiio_tracker *scsi_lookup;
1116 	ulong		scsi_lookup_pages;
1117 	spinlock_t	scsi_lookup_lock;
1118 	struct list_head free_list;
1119 	int		pending_io_count;
1120 	wait_queue_head_t reset_wq;
1121 
1122 	/* chain */
1123 	struct chain_tracker *chain_lookup;
1124 	struct list_head free_chain_list;
1125 	struct dma_pool *chain_dma_pool;
1126 	ulong		chain_pages;
1127 	u16		max_sges_in_main_message;
1128 	u16		max_sges_in_chain_message;
1129 	u16		chains_needed_per_io;
1130 	u32		chain_depth;
1131 	u16		chain_segment_sz;
1132 
1133 	/* hi-priority queue */
1134 	u16		hi_priority_smid;
1135 	u8		*hi_priority;
1136 	dma_addr_t	hi_priority_dma;
1137 	u16		hi_priority_depth;
1138 	struct request_tracker *hpr_lookup;
1139 	struct list_head hpr_free_list;
1140 
1141 	/* internal queue */
1142 	u16		internal_smid;
1143 	u8		*internal;
1144 	dma_addr_t	internal_dma;
1145 	u16		internal_depth;
1146 	struct request_tracker *internal_lookup;
1147 	struct list_head internal_free_list;
1148 
1149 	/* sense */
1150 	u8		*sense;
1151 	dma_addr_t	sense_dma;
1152 	struct dma_pool *sense_dma_pool;
1153 
1154 	/* reply */
1155 	u16		reply_sz;
1156 	u8		*reply;
1157 	dma_addr_t	reply_dma;
1158 	u32		reply_dma_max_address;
1159 	u32		reply_dma_min_address;
1160 	struct dma_pool *reply_dma_pool;
1161 
1162 	/* reply free queue */
1163 	u16		reply_free_queue_depth;
1164 	__le32		*reply_free;
1165 	dma_addr_t	reply_free_dma;
1166 	struct dma_pool *reply_free_dma_pool;
1167 	u32		reply_free_host_index;
1168 
1169 	/* reply post queue */
1170 	u16		reply_post_queue_depth;
1171 	struct reply_post_struct *reply_post;
1172 	u8		rdpq_array_capable;
1173 	u8		rdpq_array_enable;
1174 	u8		rdpq_array_enable_assigned;
1175 	struct dma_pool *reply_post_free_dma_pool;
1176 	u8		reply_queue_count;
1177 	struct list_head reply_queue_list;
1178 
1179 	u8		combined_reply_queue;
1180 	u8		combined_reply_index_count;
1181 	/* reply post register index */
1182 	resource_size_t	**replyPostRegisterIndex;
1183 
1184 	struct list_head delayed_tr_list;
1185 	struct list_head delayed_tr_volume_list;
1186 	struct list_head delayed_sc_list;
1187 	struct list_head delayed_event_ack_list;
1188 	u8		temp_sensors_count;
1189 	struct mutex pci_access_mutex;
1190 
1191 	/* diag buffer support */
1192 	u8		*diag_buffer[MPI2_DIAG_BUF_TYPE_COUNT];
1193 	u32		diag_buffer_sz[MPI2_DIAG_BUF_TYPE_COUNT];
1194 	dma_addr_t	diag_buffer_dma[MPI2_DIAG_BUF_TYPE_COUNT];
1195 	u8		diag_buffer_status[MPI2_DIAG_BUF_TYPE_COUNT];
1196 	u32		unique_id[MPI2_DIAG_BUF_TYPE_COUNT];
1197 	u32		product_specific[MPI2_DIAG_BUF_TYPE_COUNT][23];
1198 	u32		diagnostic_flags[MPI2_DIAG_BUF_TYPE_COUNT];
1199 	u32		ring_buffer_offset;
1200 	u32		ring_buffer_sz;
1201 	u8		is_warpdrive;
1202 	u8		hide_ir_msg;
1203 	u8		mfg_pg10_hide_flag;
1204 	u8		hide_drives;
1205 	spinlock_t	diag_trigger_lock;
1206 	u8		diag_trigger_active;
1207 	struct SL_WH_MASTER_TRIGGER_T diag_trigger_master;
1208 	struct SL_WH_EVENT_TRIGGERS_T diag_trigger_event;
1209 	struct SL_WH_SCSI_TRIGGERS_T diag_trigger_scsi;
1210 	struct SL_WH_MPI_TRIGGERS_T diag_trigger_mpi;
1211 	void		*device_remove_in_progress;
1212 	u16		device_remove_in_progress_sz;
1213 	u8		is_gen35_ioc;
1214 	u8		atomic_desc_capable;
1215 	PUT_SMID_IO_FP_HIP put_smid_scsi_io;
1216 	PUT_SMID_IO_FP_HIP put_smid_fast_path;
1217 	PUT_SMID_IO_FP_HIP put_smid_hi_priority;
1218 	PUT_SMID_DEFAULT put_smid_default;
1219 
1220 };
1221 
1222 typedef u8 (*MPT_CALLBACK)(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1223 	u32 reply);
1224 
1225 
1226 /* base shared API */
1227 extern struct list_head mpt3sas_ioc_list;
1228 extern char    driver_name[MPT_NAME_LENGTH];
1229 /* spinlock on list operations over IOCs
1230  * Case: when multiple warpdrive cards(IOCs) are in use
1231  * Each IOC will added to the ioc list structure on initialization.
1232  * Watchdog threads run at regular intervals to check IOC for any
1233  * fault conditions which will trigger the dead_ioc thread to
1234  * deallocate pci resource, resulting deleting the IOC netry from list,
1235  * this deletion need to protected by spinlock to enusre that
1236  * ioc removal is syncrhonized, if not synchronized it might lead to
1237  * list_del corruption as the ioc list is traversed in cli path.
1238  */
1239 extern spinlock_t gioc_lock;
1240 
1241 void mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc);
1242 void mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc);
1243 
1244 int mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc);
1245 void mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc);
1246 int mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc);
1247 void mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc);
1248 int mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
1249 	enum reset_type type);
1250 
1251 void *mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid);
1252 void *mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid);
1253 __le32 mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc,
1254 	u16 smid);
1255 
1256 void mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc);
1257 
1258 /* hi-priority queue */
1259 u16 mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx);
1260 u16 mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
1261 	struct scsi_cmnd *scmd);
1262 
1263 u16 mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx);
1264 void mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid);
1265 void mpt3sas_base_initialize_callback_handler(void);
1266 u8 mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func);
1267 void mpt3sas_base_release_callback_handler(u8 cb_idx);
1268 
1269 u8 mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1270 	u32 reply);
1271 u8 mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid,
1272 	u8 msix_index, u32 reply);
1273 void *mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc,
1274 	u32 phys_addr);
1275 
1276 u32 mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked);
1277 
1278 void mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code);
1279 int mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
1280 	Mpi2SasIoUnitControlReply_t *mpi_reply,
1281 	Mpi2SasIoUnitControlRequest_t *mpi_request);
1282 int mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
1283 	Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request);
1284 
1285 void mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc,
1286 	u32 *event_type);
1287 
1288 void mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc);
1289 
1290 void mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
1291 	u16 device_missing_delay, u8 io_missing_delay);
1292 
1293 int mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc);
1294 
1295 
1296 /* scsih shared API */
1297 u8 mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
1298 	u32 reply);
1299 void mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase);
1300 
1301 int mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
1302 	uint channel, uint id, uint lun, u8 type, u16 smid_task,
1303 	ulong timeout);
1304 int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
1305 	uint channel, uint id, uint lun, u8 type, u16 smid_task,
1306 	ulong timeout);
1307 
1308 void mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle);
1309 void mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle);
1310 void mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address);
1311 void mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1312 	u64 sas_address);
1313 u8 mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc,
1314 	u16 smid);
1315 
1316 struct _sas_node *mpt3sas_scsih_expander_find_by_handle(
1317 	struct MPT3SAS_ADAPTER *ioc, u16 handle);
1318 struct _sas_node *mpt3sas_scsih_expander_find_by_sas_address(
1319 	struct MPT3SAS_ADAPTER *ioc, u64 sas_address);
1320 struct _sas_device *mpt3sas_get_sdev_by_addr(
1321 	 struct MPT3SAS_ADAPTER *ioc, u64 sas_address);
1322 struct _sas_device *__mpt3sas_get_sdev_by_addr(
1323 	 struct MPT3SAS_ADAPTER *ioc, u64 sas_address);
1324 
1325 void mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc);
1326 struct _raid_device *
1327 mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle);
1328 
1329 /* config shared API */
1330 u8 mpt3sas_config_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1331 	u32 reply);
1332 int mpt3sas_config_get_number_hba_phys(struct MPT3SAS_ADAPTER *ioc,
1333 	u8 *num_phys);
1334 int mpt3sas_config_get_manufacturing_pg0(struct MPT3SAS_ADAPTER *ioc,
1335 	Mpi2ConfigReply_t *mpi_reply, Mpi2ManufacturingPage0_t *config_page);
1336 int mpt3sas_config_get_manufacturing_pg7(struct MPT3SAS_ADAPTER *ioc,
1337 	Mpi2ConfigReply_t *mpi_reply, Mpi2ManufacturingPage7_t *config_page,
1338 	u16 sz);
1339 int mpt3sas_config_get_manufacturing_pg10(struct MPT3SAS_ADAPTER *ioc,
1340 	Mpi2ConfigReply_t *mpi_reply,
1341 	struct Mpi2ManufacturingPage10_t *config_page);
1342 
1343 int mpt3sas_config_get_manufacturing_pg11(struct MPT3SAS_ADAPTER *ioc,
1344 	Mpi2ConfigReply_t *mpi_reply,
1345 	struct Mpi2ManufacturingPage11_t  *config_page);
1346 int mpt3sas_config_set_manufacturing_pg11(struct MPT3SAS_ADAPTER *ioc,
1347 	Mpi2ConfigReply_t *mpi_reply,
1348 	struct Mpi2ManufacturingPage11_t *config_page);
1349 
1350 int mpt3sas_config_get_bios_pg2(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1351 	*mpi_reply, Mpi2BiosPage2_t *config_page);
1352 int mpt3sas_config_get_bios_pg3(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1353 	*mpi_reply, Mpi2BiosPage3_t *config_page);
1354 int mpt3sas_config_get_iounit_pg0(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1355 	*mpi_reply, Mpi2IOUnitPage0_t *config_page);
1356 int mpt3sas_config_get_sas_device_pg0(struct MPT3SAS_ADAPTER *ioc,
1357 	Mpi2ConfigReply_t *mpi_reply, Mpi2SasDevicePage0_t *config_page,
1358 	u32 form, u32 handle);
1359 int mpt3sas_config_get_sas_device_pg1(struct MPT3SAS_ADAPTER *ioc,
1360 	Mpi2ConfigReply_t *mpi_reply, Mpi2SasDevicePage1_t *config_page,
1361 	u32 form, u32 handle);
1362 int mpt3sas_config_get_sas_iounit_pg0(struct MPT3SAS_ADAPTER *ioc,
1363 	Mpi2ConfigReply_t *mpi_reply, Mpi2SasIOUnitPage0_t *config_page,
1364 	u16 sz);
1365 int mpt3sas_config_get_iounit_pg1(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1366 	*mpi_reply, Mpi2IOUnitPage1_t *config_page);
1367 int mpt3sas_config_get_iounit_pg3(struct MPT3SAS_ADAPTER *ioc,
1368 	Mpi2ConfigReply_t *mpi_reply, Mpi2IOUnitPage3_t *config_page, u16 sz);
1369 int mpt3sas_config_set_iounit_pg1(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1370 	*mpi_reply, Mpi2IOUnitPage1_t *config_page);
1371 int mpt3sas_config_get_iounit_pg8(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1372 	*mpi_reply, Mpi2IOUnitPage8_t *config_page);
1373 int mpt3sas_config_get_sas_iounit_pg1(struct MPT3SAS_ADAPTER *ioc,
1374 	Mpi2ConfigReply_t *mpi_reply, Mpi2SasIOUnitPage1_t *config_page,
1375 	u16 sz);
1376 int mpt3sas_config_set_sas_iounit_pg1(struct MPT3SAS_ADAPTER *ioc,
1377 	Mpi2ConfigReply_t *mpi_reply, Mpi2SasIOUnitPage1_t *config_page,
1378 	u16 sz);
1379 int mpt3sas_config_get_ioc_pg8(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1380 	*mpi_reply, Mpi2IOCPage8_t *config_page);
1381 int mpt3sas_config_get_expander_pg0(struct MPT3SAS_ADAPTER *ioc,
1382 	Mpi2ConfigReply_t *mpi_reply, Mpi2ExpanderPage0_t *config_page,
1383 	u32 form, u32 handle);
1384 int mpt3sas_config_get_expander_pg1(struct MPT3SAS_ADAPTER *ioc,
1385 	Mpi2ConfigReply_t *mpi_reply, Mpi2ExpanderPage1_t *config_page,
1386 	u32 phy_number, u16 handle);
1387 int mpt3sas_config_get_enclosure_pg0(struct MPT3SAS_ADAPTER *ioc,
1388 	Mpi2ConfigReply_t *mpi_reply, Mpi2SasEnclosurePage0_t *config_page,
1389 	u32 form, u32 handle);
1390 int mpt3sas_config_get_phy_pg0(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1391 	*mpi_reply, Mpi2SasPhyPage0_t *config_page, u32 phy_number);
1392 int mpt3sas_config_get_phy_pg1(struct MPT3SAS_ADAPTER *ioc, Mpi2ConfigReply_t
1393 	*mpi_reply, Mpi2SasPhyPage1_t *config_page, u32 phy_number);
1394 int mpt3sas_config_get_raid_volume_pg1(struct MPT3SAS_ADAPTER *ioc,
1395 	Mpi2ConfigReply_t *mpi_reply, Mpi2RaidVolPage1_t *config_page, u32 form,
1396 	u32 handle);
1397 int mpt3sas_config_get_number_pds(struct MPT3SAS_ADAPTER *ioc, u16 handle,
1398 	u8 *num_pds);
1399 int mpt3sas_config_get_raid_volume_pg0(struct MPT3SAS_ADAPTER *ioc,
1400 	Mpi2ConfigReply_t *mpi_reply, Mpi2RaidVolPage0_t *config_page, u32 form,
1401 	u32 handle, u16 sz);
1402 int mpt3sas_config_get_phys_disk_pg0(struct MPT3SAS_ADAPTER *ioc,
1403 	Mpi2ConfigReply_t *mpi_reply, Mpi2RaidPhysDiskPage0_t *config_page,
1404 	u32 form, u32 form_specific);
1405 int mpt3sas_config_get_volume_handle(struct MPT3SAS_ADAPTER *ioc, u16 pd_handle,
1406 	u16 *volume_handle);
1407 int mpt3sas_config_get_volume_wwid(struct MPT3SAS_ADAPTER *ioc,
1408 	u16 volume_handle, u64 *wwid);
1409 
1410 /* ctl shared API */
1411 extern struct device_attribute *mpt3sas_host_attrs[];
1412 extern struct device_attribute *mpt3sas_dev_attrs[];
1413 void mpt3sas_ctl_init(ushort hbas_to_enumerate);
1414 void mpt3sas_ctl_exit(ushort hbas_to_enumerate);
1415 u8 mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1416 	u32 reply);
1417 void mpt3sas_ctl_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase);
1418 u8 mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc,
1419 	u8 msix_index, u32 reply);
1420 void mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
1421 	Mpi2EventNotificationReply_t *mpi_reply);
1422 
1423 void mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc,
1424 	u8 bits_to_register);
1425 int mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1426 	u8 *issue_reset);
1427 
1428 /* transport shared API */
1429 extern struct scsi_transport_template *mpt3sas_transport_template;
1430 u8 mpt3sas_transport_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
1431 	u32 reply);
1432 struct _sas_port *mpt3sas_transport_port_add(struct MPT3SAS_ADAPTER *ioc,
1433 	u16 handle, u64 sas_address);
1434 void mpt3sas_transport_port_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
1435 	u64 sas_address_parent);
1436 int mpt3sas_transport_add_host_phy(struct MPT3SAS_ADAPTER *ioc, struct _sas_phy
1437 	*mpt3sas_phy, Mpi2SasPhyPage0_t phy_pg0, struct device *parent_dev);
1438 int mpt3sas_transport_add_expander_phy(struct MPT3SAS_ADAPTER *ioc,
1439 	struct _sas_phy *mpt3sas_phy, Mpi2ExpanderPage1_t expander_pg1,
1440 	struct device *parent_dev);
1441 void mpt3sas_transport_update_links(struct MPT3SAS_ADAPTER *ioc,
1442 	u64 sas_address, u16 handle, u8 phy_number, u8 link_rate);
1443 extern struct sas_function_template mpt3sas_transport_functions;
1444 extern struct scsi_transport_template *mpt3sas_transport_template;
1445 /* trigger data externs */
1446 void mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
1447 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data);
1448 void mpt3sas_process_trigger_data(struct MPT3SAS_ADAPTER *ioc,
1449 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data);
1450 void mpt3sas_trigger_master(struct MPT3SAS_ADAPTER *ioc,
1451 	u32 tigger_bitmask);
1452 void mpt3sas_trigger_event(struct MPT3SAS_ADAPTER *ioc, u16 event,
1453 	u16 log_entry_qualifier);
1454 void mpt3sas_trigger_scsi(struct MPT3SAS_ADAPTER *ioc, u8 sense_key,
1455 	u8 asc, u8 ascq);
1456 void mpt3sas_trigger_mpi(struct MPT3SAS_ADAPTER *ioc, u16 ioc_status,
1457 	u32 loginfo);
1458 
1459 /* warpdrive APIs */
1460 u8 mpt3sas_get_num_volumes(struct MPT3SAS_ADAPTER *ioc);
1461 void mpt3sas_init_warpdrive_properties(struct MPT3SAS_ADAPTER *ioc,
1462 	struct _raid_device *raid_device);
1463 u8
1464 mpt3sas_scsi_direct_io_get(struct MPT3SAS_ADAPTER *ioc, u16 smid);
1465 void
1466 mpt3sas_scsi_direct_io_set(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 direct_io);
1467 void
1468 mpt3sas_setup_direct_io(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
1469 	struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
1470 	u16 smid);
1471 
1472 /* NCQ Prio Handling Check */
1473 bool scsih_ncq_prio_supp(struct scsi_device *sdev);
1474 
1475 #endif /* MPT3SAS_BASE_H_INCLUDED */
1476