2 mvsas.c - Marvell 88SE6440 SAS/SATA support
4 Copyright 2007 Red Hat, Inc.
5 Copyright 2008 Marvell. <kewei@marvell.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2,
10 or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty
14 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public
18 License along with this program; see the file COPYING. If not,
19 write to the Free Software Foundation, 675 Mass Ave, Cambridge,
22 ---------------------------------------------------------------
25 * hardware supports controlling the endian-ness of data
26 structures. this permits elimination of all the le32_to_cpu()
27 and cpu_to_le32() conversions.
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/pci.h>
34 #include <linux/interrupt.h>
35 #include <linux/spinlock.h>
36 #include <linux/delay.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/ctype.h>
39 #include <scsi/libsas.h>
42 #define DRV_NAME "mvsas"
43 #define DRV_VERSION "0.5.1"
45 #define MVS_DISABLE_NVRAM
46 #define MVS_DISABLE_MSI
48 #define mr32(reg) readl(regs + MVS_##reg)
49 #define mw32(reg,val) writel((val), regs + MVS_##reg)
50 #define mw32_f(reg,val) do { \
51 writel((val), regs + MVS_##reg); \
52 readl(regs + MVS_##reg); \
55 #define MVS_ID_NOT_MAPPED 0xff
56 #define MVS_CHIP_SLOT_SZ (1U << mvi->chip->slot_width)
58 /* offset for D2H FIS in the Received FIS List Structure */
59 #define SATA_RECEIVED_D2H_FIS(reg_set) \
60 ((void *) mvi->rx_fis + 0x400 + 0x100 * reg_set + 0x40)
61 #define SATA_RECEIVED_PIO_FIS(reg_set) \
62 ((void *) mvi->rx_fis + 0x400 + 0x100 * reg_set + 0x20)
63 #define UNASSOC_D2H_FIS(id) \
64 ((void *) mvi->rx_fis + 0x100 * id)
66 #define for_each_phy(__lseq_mask, __mc, __lseq, __rest) \
67 for ((__mc) = (__lseq_mask), (__lseq) = 0; \
68 (__mc) != 0 && __rest; \
69 (++__lseq), (__mc) >>= 1)
71 /* driver compile-time configuration */
72 enum driver_configuration
{
73 MVS_TX_RING_SZ
= 1024, /* TX ring size (12-bit) */
74 MVS_RX_RING_SZ
= 1024, /* RX ring size (12-bit) */
75 /* software requires power-of-2
78 MVS_SLOTS
= 512, /* command slots */
79 MVS_SLOT_BUF_SZ
= 8192, /* cmd tbl + IU + status + PRD */
80 MVS_SSP_CMD_SZ
= 64, /* SSP command table buffer size */
81 MVS_ATA_CMD_SZ
= 96, /* SATA command table buffer size */
82 MVS_OAF_SZ
= 64, /* Open address frame buffer size */
84 MVS_RX_FIS_COUNT
= 17, /* Optional rx'd FISs (max 17) */
86 MVS_QUEUE_SIZE
= 30, /* Support Queue depth */
89 /* unchangeable hardware details */
90 enum hardware_details
{
91 MVS_MAX_PHYS
= 8, /* max. possible phys */
92 MVS_MAX_PORTS
= 8, /* max. possible ports */
93 MVS_RX_FISL_SZ
= 0x400 + (MVS_RX_FIS_COUNT
* 0x100),
96 /* peripheral registers (BAR2) */
97 enum peripheral_registers
{
98 SPI_CTL
= 0x10, /* EEPROM control */
99 SPI_CMD
= 0x14, /* EEPROM command */
100 SPI_DATA
= 0x18, /* EEPROM data */
103 enum peripheral_register_bits
{
104 TWSI_RDY
= (1U << 7), /* EEPROM interface ready */
105 TWSI_RD
= (1U << 4), /* EEPROM read access */
107 SPI_ADDR_MASK
= 0x3ffff, /* bits 17:0 */
110 /* enhanced mode registers (BAR4) */
112 MVS_GBL_CTL
= 0x04, /* global control */
113 MVS_GBL_INT_STAT
= 0x08, /* global irq status */
114 MVS_GBL_PI
= 0x0C, /* ports implemented bitmask */
115 MVS_GBL_PORT_TYPE
= 0xa0, /* port type */
117 MVS_CTL
= 0x100, /* SAS/SATA port configuration */
118 MVS_PCS
= 0x104, /* SAS/SATA port control/status */
119 MVS_CMD_LIST_LO
= 0x108, /* cmd list addr */
120 MVS_CMD_LIST_HI
= 0x10C,
121 MVS_RX_FIS_LO
= 0x110, /* RX FIS list addr */
122 MVS_RX_FIS_HI
= 0x114,
124 MVS_TX_CFG
= 0x120, /* TX configuration */
125 MVS_TX_LO
= 0x124, /* TX (delivery) ring addr */
128 MVS_TX_PROD_IDX
= 0x12C, /* TX producer pointer */
129 MVS_TX_CONS_IDX
= 0x130, /* TX consumer pointer (RO) */
130 MVS_RX_CFG
= 0x134, /* RX configuration */
131 MVS_RX_LO
= 0x138, /* RX (completion) ring addr */
133 MVS_RX_CONS_IDX
= 0x140, /* RX consumer pointer (RO) */
135 MVS_INT_COAL
= 0x148, /* Int coalescing config */
136 MVS_INT_COAL_TMOUT
= 0x14C, /* Int coalescing timeout */
137 MVS_INT_STAT
= 0x150, /* Central int status */
138 MVS_INT_MASK
= 0x154, /* Central int enable */
139 MVS_INT_STAT_SRS
= 0x158, /* SATA register set status */
140 MVS_INT_MASK_SRS
= 0x15C,
142 /* ports 1-3 follow after this */
143 MVS_P0_INT_STAT
= 0x160, /* port0 interrupt status */
144 MVS_P0_INT_MASK
= 0x164, /* port0 interrupt mask */
145 MVS_P4_INT_STAT
= 0x200, /* Port 4 interrupt status */
146 MVS_P4_INT_MASK
= 0x204, /* Port 4 interrupt enable mask */
148 /* ports 1-3 follow after this */
149 MVS_P0_SER_CTLSTAT
= 0x180, /* port0 serial control/status */
150 MVS_P4_SER_CTLSTAT
= 0x220, /* port4 serial control/status */
152 MVS_CMD_ADDR
= 0x1B8, /* Command register port (addr) */
153 MVS_CMD_DATA
= 0x1BC, /* Command register port (data) */
155 /* ports 1-3 follow after this */
156 MVS_P0_CFG_ADDR
= 0x1C0, /* port0 phy register address */
157 MVS_P0_CFG_DATA
= 0x1C4, /* port0 phy register data */
158 MVS_P4_CFG_ADDR
= 0x230, /* Port 4 config address */
159 MVS_P4_CFG_DATA
= 0x234, /* Port 4 config data */
161 /* ports 1-3 follow after this */
162 MVS_P0_VSR_ADDR
= 0x1E0, /* port0 VSR address */
163 MVS_P0_VSR_DATA
= 0x1E4, /* port0 VSR data */
164 MVS_P4_VSR_ADDR
= 0x250, /* port 4 VSR addr */
165 MVS_P4_VSR_DATA
= 0x254, /* port 4 VSR data */
168 enum hw_register_bits
{
170 INT_EN
= (1U << 1), /* Global int enable */
171 HBA_RST
= (1U << 0), /* HBA reset */
173 /* MVS_GBL_INT_STAT */
174 INT_XOR
= (1U << 4), /* XOR engine event */
175 INT_SAS_SATA
= (1U << 0), /* SAS/SATA event */
177 /* MVS_GBL_PORT_TYPE */ /* shl for ports 1-3 */
178 SATA_TARGET
= (1U << 16), /* port0 SATA target enable */
179 MODE_AUTO_DET_PORT7
= (1U << 15), /* port0 SAS/SATA autodetect */
180 MODE_AUTO_DET_PORT6
= (1U << 14),
181 MODE_AUTO_DET_PORT5
= (1U << 13),
182 MODE_AUTO_DET_PORT4
= (1U << 12),
183 MODE_AUTO_DET_PORT3
= (1U << 11),
184 MODE_AUTO_DET_PORT2
= (1U << 10),
185 MODE_AUTO_DET_PORT1
= (1U << 9),
186 MODE_AUTO_DET_PORT0
= (1U << 8),
187 MODE_AUTO_DET_EN
= MODE_AUTO_DET_PORT0
| MODE_AUTO_DET_PORT1
|
188 MODE_AUTO_DET_PORT2
| MODE_AUTO_DET_PORT3
|
189 MODE_AUTO_DET_PORT4
| MODE_AUTO_DET_PORT5
|
190 MODE_AUTO_DET_PORT6
| MODE_AUTO_DET_PORT7
,
191 MODE_SAS_PORT7_MASK
= (1U << 7), /* port0 SAS(1), SATA(0) mode */
192 MODE_SAS_PORT6_MASK
= (1U << 6),
193 MODE_SAS_PORT5_MASK
= (1U << 5),
194 MODE_SAS_PORT4_MASK
= (1U << 4),
195 MODE_SAS_PORT3_MASK
= (1U << 3),
196 MODE_SAS_PORT2_MASK
= (1U << 2),
197 MODE_SAS_PORT1_MASK
= (1U << 1),
198 MODE_SAS_PORT0_MASK
= (1U << 0),
199 MODE_SAS_SATA
= MODE_SAS_PORT0_MASK
| MODE_SAS_PORT1_MASK
|
200 MODE_SAS_PORT2_MASK
| MODE_SAS_PORT3_MASK
|
201 MODE_SAS_PORT4_MASK
| MODE_SAS_PORT5_MASK
|
202 MODE_SAS_PORT6_MASK
| MODE_SAS_PORT7_MASK
,
204 /* SAS_MODE value may be
205 * dictated (in hw) by values
206 * of SATA_TARGET & AUTO_DET
210 TX_EN
= (1U << 16), /* Enable TX */
211 TX_RING_SZ_MASK
= 0xfff, /* TX ring size, bits 11:0 */
214 RX_EN
= (1U << 16), /* Enable RX */
215 RX_RING_SZ_MASK
= 0xfff, /* RX ring size, bits 11:0 */
218 COAL_EN
= (1U << 16), /* Enable int coalescing */
220 /* MVS_INT_STAT, MVS_INT_MASK */
221 CINT_I2C
= (1U << 31), /* I2C event */
222 CINT_SW0
= (1U << 30), /* software event 0 */
223 CINT_SW1
= (1U << 29), /* software event 1 */
224 CINT_PRD_BC
= (1U << 28), /* PRD BC err for read cmd */
225 CINT_DMA_PCIE
= (1U << 27), /* DMA to PCIE timeout */
226 CINT_MEM
= (1U << 26), /* int mem parity err */
227 CINT_I2C_SLAVE
= (1U << 25), /* slave I2C event */
228 CINT_SRS
= (1U << 3), /* SRS event */
229 CINT_CI_STOP
= (1U << 1), /* cmd issue stopped */
230 CINT_DONE
= (1U << 0), /* cmd completion */
232 /* shl for ports 1-3 */
233 CINT_PORT_STOPPED
= (1U << 16), /* port0 stopped */
234 CINT_PORT
= (1U << 8), /* port0 event */
235 CINT_PORT_MASK_OFFSET
= 8,
236 CINT_PORT_MASK
= (0xFF << CINT_PORT_MASK_OFFSET
),
238 /* TX (delivery) ring bits */
240 TXQ_CMD_SSP
= 1, /* SSP protocol */
241 TXQ_CMD_SMP
= 2, /* SMP protocol */
242 TXQ_CMD_STP
= 3, /* STP/SATA protocol */
243 TXQ_CMD_SSP_FREE_LIST
= 4, /* add to SSP targ free list */
244 TXQ_CMD_SLOT_RESET
= 7, /* reset command slot */
245 TXQ_MODE_I
= (1U << 28), /* mode: 0=target,1=initiator */
246 TXQ_PRIO_HI
= (1U << 27), /* priority: 0=normal, 1=high */
247 TXQ_SRS_SHIFT
= 20, /* SATA register set */
249 TXQ_PHY_SHIFT
= 12, /* PHY bitmap */
251 TXQ_SLOT_MASK
= 0xfff, /* slot number */
253 /* RX (completion) ring bits */
254 RXQ_GOOD
= (1U << 23), /* Response good */
255 RXQ_SLOT_RESET
= (1U << 21), /* Slot reset complete */
256 RXQ_CMD_RX
= (1U << 20), /* target cmd received */
257 RXQ_ATTN
= (1U << 19), /* attention */
258 RXQ_RSP
= (1U << 18), /* response frame xfer'd */
259 RXQ_ERR
= (1U << 17), /* err info rec xfer'd */
260 RXQ_DONE
= (1U << 16), /* cmd complete */
261 RXQ_SLOT_MASK
= 0xfff, /* slot number */
263 /* mvs_cmd_hdr bits */
264 MCH_PRD_LEN_SHIFT
= 16, /* 16-bit PRD table len */
265 MCH_SSP_FR_TYPE_SHIFT
= 13, /* SSP frame type */
267 /* SSP initiator only */
268 MCH_SSP_FR_CMD
= 0x0, /* COMMAND frame */
270 /* SSP initiator or target */
271 MCH_SSP_FR_TASK
= 0x1, /* TASK frame */
273 /* SSP target only */
274 MCH_SSP_FR_XFER_RDY
= 0x4, /* XFER_RDY frame */
275 MCH_SSP_FR_RESP
= 0x5, /* RESPONSE frame */
276 MCH_SSP_FR_READ
= 0x6, /* Read DATA frame(s) */
277 MCH_SSP_FR_READ_RESP
= 0x7, /* ditto, plus RESPONSE */
279 MCH_PASSTHRU
= (1U << 12), /* pass-through (SSP) */
280 MCH_FBURST
= (1U << 11), /* first burst (SSP) */
281 MCH_CHK_LEN
= (1U << 10), /* chk xfer len (SSP) */
282 MCH_RETRY
= (1U << 9), /* tport layer retry (SSP) */
283 MCH_PROTECTION
= (1U << 8), /* protection info rec (SSP) */
284 MCH_RESET
= (1U << 7), /* Reset (STP/SATA) */
285 MCH_FPDMA
= (1U << 6), /* First party DMA (STP/SATA) */
286 MCH_ATAPI
= (1U << 5), /* ATAPI (STP/SATA) */
287 MCH_BIST
= (1U << 4), /* BIST activate (STP/SATA) */
288 MCH_PMP_MASK
= 0xf, /* PMP from cmd FIS (STP/SATA)*/
290 CCTL_RST
= (1U << 5), /* port logic reset */
292 /* 0(LSB first), 1(MSB first) */
293 CCTL_ENDIAN_DATA
= (1U << 3), /* PRD data */
294 CCTL_ENDIAN_RSP
= (1U << 2), /* response frame */
295 CCTL_ENDIAN_OPEN
= (1U << 1), /* open address frame */
296 CCTL_ENDIAN_CMD
= (1U << 0), /* command table */
298 /* MVS_Px_SER_CTLSTAT (per-phy control) */
299 PHY_SSP_RST
= (1U << 3), /* reset SSP link layer */
300 PHY_BCAST_CHG
= (1U << 2), /* broadcast(change) notif */
301 PHY_RST_HARD
= (1U << 1), /* hard reset + phy reset */
302 PHY_RST
= (1U << 0), /* phy reset */
303 PHY_MIN_SPP_PHYS_LINK_RATE_MASK
= (0xF << 8),
304 PHY_MAX_SPP_PHYS_LINK_RATE_MASK
= (0xF << 12),
305 PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET
= (16),
306 PHY_NEG_SPP_PHYS_LINK_RATE_MASK
=
307 (0xF << PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET
),
308 PHY_READY_MASK
= (1U << 20),
310 /* MVS_Px_INT_STAT, MVS_Px_INT_MASK (per-phy events) */
311 PHYEV_DEC_ERR
= (1U << 24), /* Phy Decoding Error */
312 PHYEV_UNASSOC_FIS
= (1U << 19), /* unassociated FIS rx'd */
313 PHYEV_AN
= (1U << 18), /* SATA async notification */
314 PHYEV_BIST_ACT
= (1U << 17), /* BIST activate FIS */
315 PHYEV_SIG_FIS
= (1U << 16), /* signature FIS */
316 PHYEV_POOF
= (1U << 12), /* phy ready from 1 -> 0 */
317 PHYEV_IU_BIG
= (1U << 11), /* IU too long err */
318 PHYEV_IU_SMALL
= (1U << 10), /* IU too short err */
319 PHYEV_UNK_TAG
= (1U << 9), /* unknown tag */
320 PHYEV_BROAD_CH
= (1U << 8), /* broadcast(CHANGE) */
321 PHYEV_COMWAKE
= (1U << 7), /* COMWAKE rx'd */
322 PHYEV_PORT_SEL
= (1U << 6), /* port selector present */
323 PHYEV_HARD_RST
= (1U << 5), /* hard reset rx'd */
324 PHYEV_ID_TMOUT
= (1U << 4), /* identify timeout */
325 PHYEV_ID_FAIL
= (1U << 3), /* identify failed */
326 PHYEV_ID_DONE
= (1U << 2), /* identify done */
327 PHYEV_HARD_RST_DONE
= (1U << 1), /* hard reset done */
328 PHYEV_RDY_CH
= (1U << 0), /* phy ready changed state */
331 PCS_EN_SATA_REG_SHIFT
= (16), /* Enable SATA Register Set */
332 PCS_EN_PORT_XMT_SHIFT
= (12), /* Enable Port Transmit */
333 PCS_EN_PORT_XMT_SHIFT2
= (8), /* For 6480 */
334 PCS_SATA_RETRY
= (1U << 8), /* retry ctl FIS on R_ERR */
335 PCS_RSP_RX_EN
= (1U << 7), /* raw response rx */
336 PCS_SELF_CLEAR
= (1U << 5), /* self-clearing int mode */
337 PCS_FIS_RX_EN
= (1U << 4), /* FIS rx enable */
338 PCS_CMD_STOP_ERR
= (1U << 3), /* cmd stop-on-err enable */
339 PCS_CMD_RST
= (1U << 1), /* reset cmd issue */
340 PCS_CMD_EN
= (1U << 0), /* enable cmd issue */
342 /* Port n Attached Device Info */
343 PORT_DEV_SSP_TRGT
= (1U << 19),
344 PORT_DEV_SMP_TRGT
= (1U << 18),
345 PORT_DEV_STP_TRGT
= (1U << 17),
346 PORT_DEV_SSP_INIT
= (1U << 11),
347 PORT_DEV_SMP_INIT
= (1U << 10),
348 PORT_DEV_STP_INIT
= (1U << 9),
349 PORT_PHY_ID_MASK
= (0xFFU
<< 24),
350 PORT_DEV_TRGT_MASK
= (0x7U
<< 17),
351 PORT_DEV_INIT_MASK
= (0x7U
<< 9),
352 PORT_DEV_TYPE_MASK
= (0x7U
<< 0),
354 /* Port n PHY Status */
356 PHY_DW_SYNC
= (1U << 1),
357 PHY_OOB_DTCTD
= (1U << 0),
360 /* PHYMODE 6 (CDB) */
361 PHY_MODE6_DTL_SPEED
= (1U << 27),
364 enum mvs_info_flags
{
365 MVF_MSI
= (1U << 0), /* MSI is enabled */
366 MVF_PHY_PWR_FIX
= (1U << 1), /* bug workaround */
369 enum sas_cmd_port_registers
{
370 CMD_CMRST_OOB_DET
= 0x100, /* COMRESET OOB detect register */
371 CMD_CMWK_OOB_DET
= 0x104, /* COMWAKE OOB detect register */
372 CMD_CMSAS_OOB_DET
= 0x108, /* COMSAS OOB detect register */
373 CMD_BRST_OOB_DET
= 0x10c, /* burst OOB detect register */
374 CMD_OOB_SPACE
= 0x110, /* OOB space control register */
375 CMD_OOB_BURST
= 0x114, /* OOB burst control register */
376 CMD_PHY_TIMER
= 0x118, /* PHY timer control register */
377 CMD_PHY_CONFIG0
= 0x11c, /* PHY config register 0 */
378 CMD_PHY_CONFIG1
= 0x120, /* PHY config register 1 */
379 CMD_SAS_CTL0
= 0x124, /* SAS control register 0 */
380 CMD_SAS_CTL1
= 0x128, /* SAS control register 1 */
381 CMD_SAS_CTL2
= 0x12c, /* SAS control register 2 */
382 CMD_SAS_CTL3
= 0x130, /* SAS control register 3 */
383 CMD_ID_TEST
= 0x134, /* ID test register */
384 CMD_PL_TIMER
= 0x138, /* PL timer register */
385 CMD_WD_TIMER
= 0x13c, /* WD timer register */
386 CMD_PORT_SEL_COUNT
= 0x140, /* port selector count register */
387 CMD_APP_MEM_CTL
= 0x144, /* Application Memory Control */
388 CMD_XOR_MEM_CTL
= 0x148, /* XOR Block Memory Control */
389 CMD_DMA_MEM_CTL
= 0x14c, /* DMA Block Memory Control */
390 CMD_PORT_MEM_CTL0
= 0x150, /* Port Memory Control 0 */
391 CMD_PORT_MEM_CTL1
= 0x154, /* Port Memory Control 1 */
392 CMD_SATA_PORT_MEM_CTL0
= 0x158, /* SATA Port Memory Control 0 */
393 CMD_SATA_PORT_MEM_CTL1
= 0x15c, /* SATA Port Memory Control 1 */
394 CMD_XOR_MEM_BIST_CTL
= 0x160, /* XOR Memory BIST Control */
395 CMD_XOR_MEM_BIST_STAT
= 0x164, /* XOR Memroy BIST Status */
396 CMD_DMA_MEM_BIST_CTL
= 0x168, /* DMA Memory BIST Control */
397 CMD_DMA_MEM_BIST_STAT
= 0x16c, /* DMA Memory BIST Status */
398 CMD_PORT_MEM_BIST_CTL
= 0x170, /* Port Memory BIST Control */
399 CMD_PORT_MEM_BIST_STAT0
= 0x174, /* Port Memory BIST Status 0 */
400 CMD_PORT_MEM_BIST_STAT1
= 0x178, /* Port Memory BIST Status 1 */
401 CMD_STP_MEM_BIST_CTL
= 0x17c, /* STP Memory BIST Control */
402 CMD_STP_MEM_BIST_STAT0
= 0x180, /* STP Memory BIST Status 0 */
403 CMD_STP_MEM_BIST_STAT1
= 0x184, /* STP Memory BIST Status 1 */
404 CMD_RESET_COUNT
= 0x188, /* Reset Count */
405 CMD_MONTR_DATA_SEL
= 0x18C, /* Monitor Data/Select */
406 CMD_PLL_PHY_CONFIG
= 0x190, /* PLL/PHY Configuration */
407 CMD_PHY_CTL
= 0x194, /* PHY Control and Status */
408 CMD_PHY_TEST_COUNT0
= 0x198, /* Phy Test Count 0 */
409 CMD_PHY_TEST_COUNT1
= 0x19C, /* Phy Test Count 1 */
410 CMD_PHY_TEST_COUNT2
= 0x1A0, /* Phy Test Count 2 */
411 CMD_APP_ERR_CONFIG
= 0x1A4, /* Application Error Configuration */
412 CMD_PND_FIFO_CTL0
= 0x1A8, /* Pending FIFO Control 0 */
413 CMD_HOST_CTL
= 0x1AC, /* Host Control Status */
414 CMD_HOST_WR_DATA
= 0x1B0, /* Host Write Data */
415 CMD_HOST_RD_DATA
= 0x1B4, /* Host Read Data */
416 CMD_PHY_MODE_21
= 0x1B8, /* Phy Mode 21 */
417 CMD_SL_MODE0
= 0x1BC, /* SL Mode 0 */
418 CMD_SL_MODE1
= 0x1C0, /* SL Mode 1 */
419 CMD_PND_FIFO_CTL1
= 0x1C4, /* Pending FIFO Control 1 */
422 /* SAS/SATA configuration port registers, aka phy registers */
423 enum sas_sata_config_port_regs
{
424 PHYR_IDENTIFY
= 0x00, /* info for IDENTIFY frame */
425 PHYR_ADDR_LO
= 0x04, /* my SAS address (low) */
426 PHYR_ADDR_HI
= 0x08, /* my SAS address (high) */
427 PHYR_ATT_DEV_INFO
= 0x0C, /* attached device info */
428 PHYR_ATT_ADDR_LO
= 0x10, /* attached dev SAS addr (low) */
429 PHYR_ATT_ADDR_HI
= 0x14, /* attached dev SAS addr (high) */
430 PHYR_SATA_CTL
= 0x18, /* SATA control */
431 PHYR_PHY_STAT
= 0x1C, /* PHY status */
432 PHYR_SATA_SIG0
= 0x20, /*port SATA signature FIS(Byte 0-3) */
433 PHYR_SATA_SIG1
= 0x24, /*port SATA signature FIS(Byte 4-7) */
434 PHYR_SATA_SIG2
= 0x28, /*port SATA signature FIS(Byte 8-11) */
435 PHYR_SATA_SIG3
= 0x2c, /*port SATA signature FIS(Byte 12-15) */
436 PHYR_R_ERR_COUNT
= 0x30, /* port R_ERR count register */
437 PHYR_CRC_ERR_COUNT
= 0x34, /* port CRC error count register */
438 PHYR_WIDE_PORT
= 0x38, /* wide port participating */
439 PHYR_CURRENT0
= 0x80, /* current connection info 0 */
440 PHYR_CURRENT1
= 0x84, /* current connection info 1 */
441 PHYR_CURRENT2
= 0x88, /* current connection info 2 */
444 /* SAS/SATA Vendor Specific Port Registers */
445 enum sas_sata_vsp_regs
{
446 VSR_PHY_STAT
= 0x00, /* Phy Status */
447 VSR_PHY_MODE1
= 0x01, /* phy tx */
448 VSR_PHY_MODE2
= 0x02, /* tx scc */
449 VSR_PHY_MODE3
= 0x03, /* pll */
450 VSR_PHY_MODE4
= 0x04, /* VCO */
451 VSR_PHY_MODE5
= 0x05, /* Rx */
452 VSR_PHY_MODE6
= 0x06, /* CDR */
453 VSR_PHY_MODE7
= 0x07, /* Impedance */
454 VSR_PHY_MODE8
= 0x08, /* Voltage */
455 VSR_PHY_MODE9
= 0x09, /* Test */
456 VSR_PHY_MODE10
= 0x0A, /* Power */
457 VSR_PHY_MODE11
= 0x0B, /* Phy Mode */
458 VSR_PHY_VS0
= 0x0C, /* Vednor Specific 0 */
459 VSR_PHY_VS1
= 0x0D, /* Vednor Specific 1 */
462 enum pci_cfg_registers
{
468 enum pci_cfg_register_bits
{
469 PCTL_PWR_ON
= (0xFU
<< 24),
470 PCTL_OFF
= (0xFU
<< 12),
471 PRD_REQ_SIZE
= (0x4000),
472 PRD_REQ_MASK
= (0x00007000),
475 enum nvram_layout_offsets
{
476 NVR_SIG
= 0x00, /* 0xAA, 0x55 */
477 NVR_SAS_ADDR
= 0x02, /* 8-byte SAS address */
487 PORT_TYPE_SAS
= (1L << 1),
488 PORT_TYPE_SATA
= (1L << 0),
491 /* Command Table Format */
499 STP_ATAPI_CMD
= 0x40,
508 SB_EIR_OFF
= 0x00, /* Error Information Record */
509 SB_RFB_OFF
= 0x08, /* Response Frame Buffer */
510 SB_RFB_MAX
= 0x400, /* RFB size*/
513 enum error_info_rec
{
514 CMD_ISS_STPD
= (1U << 31), /* Cmd Issue Stopped */
517 struct mvs_chip_info
{
523 struct mvs_err_info
{
529 __le64 addr
; /* 64-bit buffer address */
531 __le32 len
; /* 16-bit length */
535 __le32 flags
; /* PRD tbl len; SAS, SATA ctl */
536 __le32 lens
; /* cmd, max resp frame len */
537 __le32 tags
; /* targ port xfer tag; tag */
538 __le32 data_len
; /* data xfer len */
539 __le64 cmd_tbl
; /* command table address */
540 __le64 open_frame
; /* open addr frame address */
541 __le64 status_buf
; /* status buffer address */
542 __le64 prd_tbl
; /* PRD tbl address */
546 struct mvs_slot_info
{
547 struct sas_task
*task
;
551 /* DMA buffer for storing cmd tbl, open addr frame, status buffer,
564 struct asd_sas_port sas_port
;
571 struct mvs_port
*port
;
572 struct asd_sas_phy sas_phy
;
573 struct sas_identify identify
;
574 struct scsi_device
*sdev
;
576 u64 att_dev_sas_addr
;
590 spinlock_t lock
; /* host-wide lock */
591 struct pci_dev
*pdev
; /* our device */
592 void __iomem
*regs
; /* enhanced mode registers */
593 void __iomem
*peri_regs
; /* peripheral registers */
595 u8 sas_addr
[SAS_ADDR_SIZE
];
596 struct sas_ha_struct sas
; /* SCSI/SAS glue */
597 struct Scsi_Host
*shost
;
599 __le32
*tx
; /* TX (delivery) DMA ring */
601 u32 tx_prod
; /* cached next-producer idx */
603 __le32
*rx
; /* RX (completion) DMA ring */
605 u32 rx_cons
; /* RX consumer idx */
607 __le32
*rx_fis
; /* RX'd FIS area */
608 dma_addr_t rx_fis_dma
;
610 struct mvs_cmd_hdr
*slot
; /* DMA command header slots */
613 const struct mvs_chip_info
*chip
;
615 unsigned long tags
[MVS_SLOTS
];
616 struct mvs_slot_info slot_info
[MVS_SLOTS
];
617 /* further per-slot information */
618 struct mvs_phy phy
[MVS_MAX_PHYS
];
619 struct mvs_port port
[MVS_MAX_PHYS
];
621 u32 can_queue
; /* per adapter */
626 struct mvs_queue_task
{
627 struct list_head list
;
632 static int mvs_phy_control(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
634 static u32
mvs_read_phy_ctl(struct mvs_info
*mvi
, u32 port
);
635 static void mvs_write_phy_ctl(struct mvs_info
*mvi
, u32 port
, u32 val
);
636 static u32
mvs_read_port_irq_stat(struct mvs_info
*mvi
, u32 port
);
637 static void mvs_write_port_irq_stat(struct mvs_info
*mvi
, u32 port
, u32 val
);
638 static void mvs_write_port_irq_mask(struct mvs_info
*mvi
, u32 port
, u32 val
);
639 static u32
mvs_read_port_irq_mask(struct mvs_info
*mvi
, u32 port
);
641 static u32
mvs_is_phy_ready(struct mvs_info
*mvi
, int i
);
642 static void mvs_detect_porttype(struct mvs_info
*mvi
, int i
);
643 static void mvs_update_phyinfo(struct mvs_info
*mvi
, int i
, int get_st
);
645 static int mvs_scan_finished(struct Scsi_Host
*, unsigned long);
646 static void mvs_scan_start(struct Scsi_Host
*);
647 static int mvs_sas_slave_alloc(struct scsi_device
*scsi_dev
);
649 static struct scsi_transport_template
*mvs_stt
;
651 static const struct mvs_chip_info mvs_chips
[] = {
652 [chip_6320
] = { 2, 16, 9 },
653 [chip_6440
] = { 4, 16, 9 },
654 [chip_6480
] = { 8, 32, 10 },
657 static struct scsi_host_template mvs_sht
= {
658 .module
= THIS_MODULE
,
660 .queuecommand
= sas_queuecommand
,
661 .target_alloc
= sas_target_alloc
,
662 .slave_configure
= sas_slave_configure
,
663 .slave_destroy
= sas_slave_destroy
,
664 .scan_finished
= mvs_scan_finished
,
665 .scan_start
= mvs_scan_start
,
666 .change_queue_depth
= sas_change_queue_depth
,
667 .change_queue_type
= sas_change_queue_type
,
668 .bios_param
= sas_bios_param
,
672 .sg_tablesize
= SG_ALL
,
673 .max_sectors
= SCSI_DEFAULT_MAX_SECTORS
,
674 .use_clustering
= ENABLE_CLUSTERING
,
675 .eh_device_reset_handler
= sas_eh_device_reset_handler
,
676 .eh_bus_reset_handler
= sas_eh_bus_reset_handler
,
677 .slave_alloc
= mvs_sas_slave_alloc
,
678 .target_destroy
= sas_target_destroy
,
682 static void mvs_hexdump(u32 size
, u8
*data
, u32 baseaddr
)
690 printk("%08X : ", baseaddr
+ offset
);
696 for (i
= 0; i
< 16; i
++) {
698 printk("%02X ", (u32
)data
[i
]);
703 for (i
= 0; i
< run
; i
++)
704 printk("%c", isalnum(data
[i
]) ? data
[i
] : '.');
712 static void mvs_hba_sb_dump(struct mvs_info
*mvi
, u32 tag
,
713 enum sas_protocol proto
)
717 struct pci_dev
*pdev
= mvi
->pdev
;
718 struct mvs_slot_info
*slot
= &mvi
->slot_info
[tag
];
720 offset
= slot
->cmd_size
+ MVS_OAF_SZ
+
721 sizeof(struct mvs_prd
) * slot
->n_elem
;
722 dev_printk(KERN_DEBUG
, &pdev
->dev
, "+---->Status buffer[%d] :\n",
724 mvs_hexdump(32, (u8
*) slot
->response
,
725 (u32
) slot
->buf_dma
+ offset
);
729 static void mvs_hba_memory_dump(struct mvs_info
*mvi
, u32 tag
,
730 enum sas_protocol proto
)
733 u32 sz
, w_ptr
, r_ptr
;
735 void __iomem
*regs
= mvi
->regs
;
736 struct pci_dev
*pdev
= mvi
->pdev
;
737 struct mvs_slot_info
*slot
= &mvi
->slot_info
[tag
];
740 sz
= mr32(TX_CFG
) & TX_RING_SZ_MASK
;
741 w_ptr
= mr32(TX_PROD_IDX
) & TX_RING_SZ_MASK
;
742 r_ptr
= mr32(TX_CONS_IDX
) & TX_RING_SZ_MASK
;
743 addr
= mr32(TX_HI
) << 16 << 16 | mr32(TX_LO
);
744 dev_printk(KERN_DEBUG
, &pdev
->dev
,
745 "Delivery Queue Size=%04d , WRT_PTR=%04X , RD_PTR=%04X\n",
747 dev_printk(KERN_DEBUG
, &pdev
->dev
,
748 "Delivery Queue Base Address=0x%llX (PA)"
749 "(tx_dma=0x%llX), Entry=%04d\n",
750 addr
, mvi
->tx_dma
, w_ptr
);
751 mvs_hexdump(sizeof(u32
), (u8
*)(&mvi
->tx
[mvi
->tx_prod
]),
752 (u32
) mvi
->tx_dma
+ sizeof(u32
) * w_ptr
);
754 addr
= mr32(CMD_LIST_HI
) << 16 << 16 | mr32(CMD_LIST_LO
);
755 dev_printk(KERN_DEBUG
, &pdev
->dev
,
756 "Command List Base Address=0x%llX (PA)"
757 "(slot_dma=0x%llX), Header=%03d\n",
758 addr
, mvi
->slot_dma
, tag
);
759 dev_printk(KERN_DEBUG
, &pdev
->dev
, "Command Header[%03d]:\n", tag
);
761 mvs_hexdump(sizeof(struct mvs_cmd_hdr
), (u8
*)(&mvi
->slot
[tag
]),
762 (u32
) mvi
->slot_dma
+ tag
* sizeof(struct mvs_cmd_hdr
));
763 /*1.command table area */
764 dev_printk(KERN_DEBUG
, &pdev
->dev
, "+---->Command Table :\n");
765 mvs_hexdump(slot
->cmd_size
, (u8
*) slot
->buf
, (u32
) slot
->buf_dma
);
766 /*2.open address frame area */
767 dev_printk(KERN_DEBUG
, &pdev
->dev
, "+---->Open Address Frame :\n");
768 mvs_hexdump(MVS_OAF_SZ
, (u8
*) slot
->buf
+ slot
->cmd_size
,
769 (u32
) slot
->buf_dma
+ slot
->cmd_size
);
771 mvs_hba_sb_dump(mvi
, tag
, proto
);
773 dev_printk(KERN_DEBUG
, &pdev
->dev
, "+---->PRD table :\n");
774 mvs_hexdump(sizeof(struct mvs_prd
) * slot
->n_elem
,
775 (u8
*) slot
->buf
+ slot
->cmd_size
+ MVS_OAF_SZ
,
776 (u32
) slot
->buf_dma
+ slot
->cmd_size
+ MVS_OAF_SZ
);
780 static void mvs_hba_cq_dump(struct mvs_info
*mvi
)
784 void __iomem
*regs
= mvi
->regs
;
785 struct pci_dev
*pdev
= mvi
->pdev
;
786 u32 entry
= mvi
->rx_cons
+ 1;
787 u32 rx_desc
= le32_to_cpu(mvi
->rx
[entry
]);
789 /*Completion Queue */
790 addr
= mr32(RX_HI
) << 16 << 16 | mr32(RX_LO
);
791 dev_printk(KERN_DEBUG
, &pdev
->dev
, "Completion Task = 0x%08X\n",
792 (u32
) mvi
->slot_info
[rx_desc
& RXQ_SLOT_MASK
].task
);
793 dev_printk(KERN_DEBUG
, &pdev
->dev
,
794 "Completion List Base Address=0x%llX (PA), "
795 "CQ_Entry=%04d, CQ_WP=0x%08X\n",
796 addr
, entry
- 1, mvi
->rx
[0]);
797 mvs_hexdump(sizeof(u32
), (u8
*)(&rx_desc
),
798 mvi
->rx_dma
+ sizeof(u32
) * entry
);
802 static void mvs_hba_interrupt_enable(struct mvs_info
*mvi
)
804 void __iomem
*regs
= mvi
->regs
;
809 mw32(GBL_CTL
, tmp
| INT_EN
);
812 static void mvs_hba_interrupt_disable(struct mvs_info
*mvi
)
814 void __iomem
*regs
= mvi
->regs
;
819 mw32(GBL_CTL
, tmp
& ~INT_EN
);
822 static int mvs_int_rx(struct mvs_info
*mvi
, bool self_clear
);
824 /* move to PCI layer or libata core? */
825 static int pci_go_64(struct pci_dev
*pdev
)
829 if (!pci_set_dma_mask(pdev
, DMA_64BIT_MASK
)) {
830 rc
= pci_set_consistent_dma_mask(pdev
, DMA_64BIT_MASK
);
832 rc
= pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
);
834 dev_printk(KERN_ERR
, &pdev
->dev
,
835 "64-bit DMA enable failed\n");
840 rc
= pci_set_dma_mask(pdev
, DMA_32BIT_MASK
);
842 dev_printk(KERN_ERR
, &pdev
->dev
,
843 "32-bit DMA enable failed\n");
846 rc
= pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
);
848 dev_printk(KERN_ERR
, &pdev
->dev
,
849 "32-bit consistent DMA enable failed\n");
857 static void mvs_tag_clear(struct mvs_info
*mvi
, u32 tag
)
859 mvi
->tag_in
= (mvi
->tag_in
+ 1) & (MVS_SLOTS
- 1);
860 mvi
->tags
[mvi
->tag_in
] = tag
;
863 static void mvs_tag_free(struct mvs_info
*mvi
, u32 tag
)
865 mvi
->tag_out
= (mvi
->tag_out
- 1) & (MVS_SLOTS
- 1);
868 static int mvs_tag_alloc(struct mvs_info
*mvi
, u32
*tag_out
)
870 if (mvi
->tag_out
!= mvi
->tag_in
) {
871 *tag_out
= mvi
->tags
[mvi
->tag_out
];
872 mvi
->tag_out
= (mvi
->tag_out
+ 1) & (MVS_SLOTS
- 1);
878 static void mvs_tag_init(struct mvs_info
*mvi
)
881 for (i
= 0; i
< MVS_SLOTS
; ++i
)
884 mvi
->tag_in
= MVS_SLOTS
- 1;
887 #ifndef MVS_DISABLE_NVRAM
888 static int mvs_eep_read(void __iomem
*regs
, u32 addr
, u32
*data
)
892 if (addr
& ~SPI_ADDR_MASK
)
895 writel(addr
, regs
+ SPI_CMD
);
896 writel(TWSI_RD
, regs
+ SPI_CTL
);
898 while (timeout
-- > 0) {
899 if (readl(regs
+ SPI_CTL
) & TWSI_RDY
) {
900 *data
= readl(regs
+ SPI_DATA
);
910 static int mvs_eep_read_buf(void __iomem
*regs
, u32 addr
,
911 void *buf
, u32 buflen
)
913 u32 addr_end
, tmp_addr
, i
, j
;
916 u8
*tmp8
, *buf8
= buf
;
918 addr_end
= addr
+ buflen
;
919 tmp_addr
= ALIGN(addr
, 4);
925 rc
= mvs_eep_read(regs
, tmp_addr
, &tmp
);
930 for (i
= j
; i
< 4; i
++)
936 for (j
= ALIGN(addr_end
, 4); tmp_addr
< j
; tmp_addr
+= 4) {
937 rc
= mvs_eep_read(regs
, tmp_addr
, &tmp
);
941 memcpy(buf8
, &tmp
, 4);
945 if (tmp_addr
< addr_end
) {
946 rc
= mvs_eep_read(regs
, tmp_addr
, &tmp
);
951 j
= addr_end
- tmp_addr
;
952 for (i
= 0; i
< j
; i
++)
962 static int mvs_nvram_read(struct mvs_info
*mvi
, u32 addr
,
963 void *buf
, u32 buflen
)
965 #ifndef MVS_DISABLE_NVRAM
966 void __iomem
*regs
= mvi
->regs
;
972 rc
= mvs_eep_read_buf(regs
, addr
, &hdr
, 2);
974 msg
= "nvram hdr read failed";
977 rc
= mvs_eep_read_buf(regs
, addr
+ 2, buf
, buflen
);
979 msg
= "nvram read failed";
983 if (hdr
[0] != 0x5A) {
985 msg
= "invalid nvram entry id";
991 sum
= ((u32
)hdr
[0]) + ((u32
)hdr
[1]);
992 for (i
= 0; i
< buflen
; i
++)
993 sum
+= ((u32
)tmp
[i
]);
996 msg
= "nvram checksum failure";
1004 dev_printk(KERN_ERR
, &mvi
->pdev
->dev
, "%s", msg
);
1007 /* FIXME , For SAS target mode */
1008 memcpy(buf
, "\x50\x05\x04\x30\x11\xab\x00\x00", 8);
1013 static void mvs_bytes_dmaed(struct mvs_info
*mvi
, int i
)
1015 struct mvs_phy
*phy
= &mvi
->phy
[i
];
1017 if (!phy
->phy_attached
)
1020 if (phy
->phy_type
& PORT_TYPE_SAS
) {
1021 struct sas_identify_frame
*id
;
1023 id
= (struct sas_identify_frame
*)phy
->frame_rcvd
;
1024 id
->dev_type
= phy
->identify
.device_type
;
1025 id
->initiator_bits
= SAS_PROTOCOL_ALL
;
1026 id
->target_bits
= phy
->identify
.target_port_protocols
;
1027 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
1030 mvi
->sas
.sas_phy
[i
]->frame_rcvd_size
= phy
->frame_rcvd_size
;
1031 mvi
->sas
.notify_port_event(mvi
->sas
.sas_phy
[i
],
1035 static int mvs_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
1037 /* give the phy enabling interrupt event time to come in (1s
1038 * is empirically about all it takes) */
1041 /* Wait for discovery to finish */
1042 scsi_flush_work(shost
);
1046 static void mvs_scan_start(struct Scsi_Host
*shost
)
1049 struct mvs_info
*mvi
= SHOST_TO_SAS_HA(shost
)->lldd_ha
;
1051 for (i
= 0; i
< mvi
->chip
->n_phy
; ++i
) {
1052 mvs_bytes_dmaed(mvi
, i
);
1056 static int mvs_sas_slave_alloc(struct scsi_device
*scsi_dev
)
1060 rc
= sas_slave_alloc(scsi_dev
);
1065 static void mvs_int_port(struct mvs_info
*mvi
, int port_no
, u32 events
)
1067 struct pci_dev
*pdev
= mvi
->pdev
;
1068 struct sas_ha_struct
*sas_ha
= &mvi
->sas
;
1069 struct mvs_phy
*phy
= &mvi
->phy
[port_no
];
1070 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
1072 phy
->irq_status
= mvs_read_port_irq_stat(mvi
, port_no
);
1074 * events is port event now ,
1075 * we need check the interrupt status which belongs to per port.
1077 dev_printk(KERN_DEBUG
, &pdev
->dev
,
1078 "Port %d Event = %X\n",
1079 port_no
, phy
->irq_status
);
1081 if (phy
->irq_status
& (PHYEV_POOF
| PHYEV_DEC_ERR
)) {
1082 if (!mvs_is_phy_ready(mvi
, port_no
)) {
1083 sas_phy_disconnected(sas_phy
);
1084 sas_ha
->notify_phy_event(sas_phy
, PHYE_LOSS_OF_SIGNAL
);
1086 mvs_phy_control(sas_phy
, PHY_FUNC_LINK_RESET
, NULL
);
1088 if (!(phy
->irq_status
& PHYEV_DEC_ERR
)) {
1089 if (phy
->irq_status
& PHYEV_COMWAKE
) {
1090 u32 tmp
= mvs_read_port_irq_mask(mvi
, port_no
);
1091 mvs_write_port_irq_mask(mvi
, port_no
,
1092 tmp
| PHYEV_SIG_FIS
);
1094 if (phy
->irq_status
& (PHYEV_SIG_FIS
| PHYEV_ID_DONE
)) {
1095 phy
->phy_status
= mvs_is_phy_ready(mvi
, port_no
);
1096 if (phy
->phy_status
) {
1097 mvs_detect_porttype(mvi
, port_no
);
1099 if (phy
->phy_type
& PORT_TYPE_SATA
) {
1100 u32 tmp
= mvs_read_port_irq_mask(mvi
,
1102 tmp
&= ~PHYEV_SIG_FIS
;
1103 mvs_write_port_irq_mask(mvi
,
1107 mvs_update_phyinfo(mvi
, port_no
, 0);
1108 sas_ha
->notify_phy_event(sas_phy
,
1110 mvs_bytes_dmaed(mvi
, port_no
);
1112 dev_printk(KERN_DEBUG
, &pdev
->dev
,
1113 "plugin interrupt but phy is gone\n");
1114 mvs_phy_control(sas_phy
, PHY_FUNC_LINK_RESET
,
1117 } else if (phy
->irq_status
& PHYEV_BROAD_CH
)
1118 sas_ha
->notify_port_event(sas_phy
,
1119 PORTE_BROADCAST_RCVD
);
1121 mvs_write_port_irq_stat(mvi
, port_no
, phy
->irq_status
);
1124 static void mvs_int_sata(struct mvs_info
*mvi
)
1129 static void mvs_slot_free(struct mvs_info
*mvi
, struct sas_task
*task
,
1130 struct mvs_slot_info
*slot
, u32 slot_idx
)
1132 if (!sas_protocol_ata(task
->task_proto
))
1134 pci_unmap_sg(mvi
->pdev
, task
->scatter
,
1135 slot
->n_elem
, task
->data_dir
);
1137 switch (task
->task_proto
) {
1138 case SAS_PROTOCOL_SMP
:
1139 pci_unmap_sg(mvi
->pdev
, &task
->smp_task
.smp_resp
, 1,
1140 PCI_DMA_FROMDEVICE
);
1141 pci_unmap_sg(mvi
->pdev
, &task
->smp_task
.smp_req
, 1,
1145 case SAS_PROTOCOL_SATA
:
1146 case SAS_PROTOCOL_STP
:
1147 case SAS_PROTOCOL_SSP
:
1154 mvs_tag_clear(mvi
, slot_idx
);
1157 static void mvs_slot_err(struct mvs_info
*mvi
, struct sas_task
*task
,
1160 struct mvs_slot_info
*slot
= &mvi
->slot_info
[slot_idx
];
1161 u64 err_dw0
= *(u32
*) slot
->response
;
1162 void __iomem
*regs
= mvi
->regs
;
1165 if (err_dw0
& CMD_ISS_STPD
)
1166 if (sas_protocol_ata(task
->task_proto
)) {
1167 tmp
= mr32(INT_STAT_SRS
);
1168 mw32(INT_STAT_SRS
, tmp
& 0xFFFF);
1171 mvs_hba_sb_dump(mvi
, slot_idx
, task
->task_proto
);
1174 static int mvs_slot_complete(struct mvs_info
*mvi
, u32 rx_desc
)
1176 u32 slot_idx
= rx_desc
& RXQ_SLOT_MASK
;
1177 struct mvs_slot_info
*slot
= &mvi
->slot_info
[slot_idx
];
1178 struct sas_task
*task
= slot
->task
;
1179 struct task_status_struct
*tstat
= &task
->task_status
;
1180 struct mvs_port
*port
= &mvi
->port
[task
->dev
->port
->id
];
1184 spin_lock(&task
->task_state_lock
);
1185 aborted
= task
->task_state_flags
& SAS_TASK_STATE_ABORTED
;
1187 task
->task_state_flags
&=
1188 ~(SAS_TASK_STATE_PENDING
| SAS_TASK_AT_INITIATOR
);
1189 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
1191 spin_unlock(&task
->task_state_lock
);
1196 memset(tstat
, 0, sizeof(*tstat
));
1197 tstat
->resp
= SAS_TASK_COMPLETE
;
1200 if (unlikely(!port
->port_attached
)) {
1201 tstat
->stat
= SAS_PHY_DOWN
;
1205 /* error info record present */
1206 if ((rx_desc
& RXQ_ERR
) && (*(u64
*) slot
->response
)) {
1207 tstat
->stat
= SAM_CHECK_COND
;
1208 mvs_slot_err(mvi
, task
, slot_idx
);
1212 switch (task
->task_proto
) {
1213 case SAS_PROTOCOL_SSP
:
1214 /* hw says status == 0, datapres == 0 */
1215 if (rx_desc
& RXQ_GOOD
) {
1216 tstat
->stat
= SAM_GOOD
;
1217 tstat
->resp
= SAS_TASK_COMPLETE
;
1219 /* response frame present */
1220 else if (rx_desc
& RXQ_RSP
) {
1221 struct ssp_response_iu
*iu
=
1222 slot
->response
+ sizeof(struct mvs_err_info
);
1223 sas_ssp_task_response(&mvi
->pdev
->dev
, task
, iu
);
1226 /* should never happen? */
1228 tstat
->stat
= SAM_CHECK_COND
;
1231 case SAS_PROTOCOL_SMP
: {
1232 struct scatterlist
*sg_resp
= &task
->smp_task
.smp_resp
;
1233 tstat
->stat
= SAM_GOOD
;
1234 to
= kmap_atomic(sg_page(sg_resp
), KM_IRQ0
);
1235 memcpy(to
+ sg_resp
->offset
,
1236 slot
->response
+ sizeof(struct mvs_err_info
),
1237 sg_dma_len(sg_resp
));
1238 kunmap_atomic(to
, KM_IRQ0
);
1242 case SAS_PROTOCOL_SATA
:
1243 case SAS_PROTOCOL_STP
:
1244 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
: {
1245 struct ata_task_resp
*resp
=
1246 (struct ata_task_resp
*)tstat
->buf
;
1248 if ((rx_desc
& (RXQ_DONE
| RXQ_ERR
| RXQ_ATTN
)) ==
1250 tstat
->stat
= SAM_GOOD
;
1252 tstat
->stat
= SAM_CHECK_COND
;
1254 resp
->frame_len
= sizeof(struct dev_to_host_fis
);
1255 memcpy(&resp
->ending_fis
[0],
1256 SATA_RECEIVED_D2H_FIS(port
->taskfileset
),
1257 sizeof(struct dev_to_host_fis
));
1258 if (resp
->ending_fis
[2] & ATA_ERR
)
1259 mvs_hexdump(16, resp
->ending_fis
, 0);
1264 tstat
->stat
= SAM_CHECK_COND
;
1269 mvs_slot_free(mvi
, task
, slot
, slot_idx
);
1270 task
->task_done(task
);
1274 static void mvs_int_full(struct mvs_info
*mvi
)
1276 void __iomem
*regs
= mvi
->regs
;
1280 stat
= mr32(INT_STAT
);
1282 mvs_int_rx(mvi
, false);
1284 for (i
= 0; i
< MVS_MAX_PORTS
; i
++) {
1285 tmp
= (stat
>> i
) & (CINT_PORT
| CINT_PORT_STOPPED
);
1287 mvs_int_port(mvi
, i
, tmp
);
1290 if (stat
& CINT_SRS
)
1293 mw32(INT_STAT
, stat
);
1296 static int mvs_int_rx(struct mvs_info
*mvi
, bool self_clear
)
1298 void __iomem
*regs
= mvi
->regs
;
1299 u32 rx_prod_idx
, rx_desc
;
1301 struct pci_dev
*pdev
= mvi
->pdev
;
1303 /* the first dword in the RX ring is special: it contains
1304 * a mirror of the hardware's RX producer index, so that
1305 * we don't have to stall the CPU reading that register.
1306 * The actual RX ring is offset by one dword, due to this.
1308 rx_prod_idx
= mr32(RX_CONS_IDX
) & RX_RING_SZ_MASK
;
1309 if (rx_prod_idx
== 0xfff) { /* h/w hasn't touched RX ring yet */
1310 mvi
->rx_cons
= 0xfff;
1314 /* The CMPL_Q may come late, read from register and try again
1315 * note: if coalescing is enabled,
1316 * it will need to read from register every time for sure
1318 if (mvi
->rx_cons
== rx_prod_idx
)
1321 if (mvi
->rx_cons
== 0xfff)
1322 mvi
->rx_cons
= MVS_RX_RING_SZ
- 1;
1324 while (mvi
->rx_cons
!= rx_prod_idx
) {
1326 /* increment our internal RX consumer pointer */
1327 mvi
->rx_cons
= (mvi
->rx_cons
+ 1) & (MVS_RX_RING_SZ
- 1);
1329 rx_desc
= le32_to_cpu(mvi
->rx
[mvi
->rx_cons
+ 1]);
1331 mvs_hba_cq_dump(mvi
);
1333 if (likely(rx_desc
& RXQ_DONE
))
1334 mvs_slot_complete(mvi
, rx_desc
);
1335 if (rx_desc
& RXQ_ATTN
) {
1337 dev_printk(KERN_DEBUG
, &pdev
->dev
, "ATTN %X\n",
1339 } else if (rx_desc
& RXQ_ERR
) {
1340 dev_printk(KERN_DEBUG
, &pdev
->dev
, "RXQ_ERR %X\n",
1345 if (attn
&& self_clear
)
1351 static irqreturn_t
mvs_interrupt(int irq
, void *opaque
)
1353 struct mvs_info
*mvi
= opaque
;
1354 void __iomem
*regs
= mvi
->regs
;
1357 stat
= mr32(GBL_INT_STAT
);
1359 /* clear CMD_CMPLT ASAP */
1360 mw32_f(INT_STAT
, CINT_DONE
);
1362 if (stat
== 0 || stat
== 0xffffffff)
1365 spin_lock(&mvi
->lock
);
1369 spin_unlock(&mvi
->lock
);
1374 #ifndef MVS_DISABLE_MSI
1375 static irqreturn_t
mvs_msi_interrupt(int irq
, void *opaque
)
1377 struct mvs_info
*mvi
= opaque
;
1379 spin_lock(&mvi
->lock
);
1381 mvs_int_rx(mvi
, true);
1383 spin_unlock(&mvi
->lock
);
1389 struct mvs_task_exec_info
{
1390 struct sas_task
*task
;
1391 struct mvs_cmd_hdr
*hdr
;
1392 struct mvs_port
*port
;
1397 static int mvs_task_prep_smp(struct mvs_info
*mvi
,
1398 struct mvs_task_exec_info
*tei
)
1401 struct sas_task
*task
= tei
->task
;
1402 struct mvs_cmd_hdr
*hdr
= tei
->hdr
;
1403 struct scatterlist
*sg_req
, *sg_resp
;
1404 u32 req_len
, resp_len
, tag
= tei
->tag
;
1407 dma_addr_t buf_tmp_dma
;
1408 struct mvs_prd
*buf_prd
;
1409 struct scatterlist
*sg
;
1410 struct mvs_slot_info
*slot
= &mvi
->slot_info
[tag
];
1411 struct asd_sas_port
*sas_port
= task
->dev
->port
;
1412 u32 flags
= (tei
->n_elem
<< MCH_PRD_LEN_SHIFT
);
1418 * DMA-map SMP request, response buffers
1420 sg_req
= &task
->smp_task
.smp_req
;
1421 elem
= pci_map_sg(mvi
->pdev
, sg_req
, 1, PCI_DMA_TODEVICE
);
1424 req_len
= sg_dma_len(sg_req
);
1426 sg_resp
= &task
->smp_task
.smp_resp
;
1427 elem
= pci_map_sg(mvi
->pdev
, sg_resp
, 1, PCI_DMA_FROMDEVICE
);
1432 resp_len
= sg_dma_len(sg_resp
);
1434 /* must be in dwords */
1435 if ((req_len
& 0x3) || (resp_len
& 0x3)) {
1441 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
1444 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
1445 buf_tmp
= slot
->buf
;
1446 buf_tmp_dma
= slot
->buf_dma
;
1450 hdr
->cmd_tbl
= cpu_to_le64(buf_tmp_dma
);
1452 buf_tmp_dma
+= req_len
;
1453 slot
->cmd_size
= req_len
;
1455 hdr
->cmd_tbl
= cpu_to_le64(sg_dma_address(sg_req
));
1458 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
1460 hdr
->open_frame
= cpu_to_le64(buf_tmp_dma
);
1462 buf_tmp
+= MVS_OAF_SZ
;
1463 buf_tmp_dma
+= MVS_OAF_SZ
;
1465 /* region 3: PRD table ********************************************* */
1468 hdr
->prd_tbl
= cpu_to_le64(buf_tmp_dma
);
1472 i
= sizeof(struct mvs_prd
) * tei
->n_elem
;
1476 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
1477 slot
->response
= buf_tmp
;
1478 hdr
->status_buf
= cpu_to_le64(buf_tmp_dma
);
1481 * Fill in TX ring and command slot header
1483 slot
->tx
= mvi
->tx_prod
;
1484 mvi
->tx
[mvi
->tx_prod
] = cpu_to_le32((TXQ_CMD_SMP
<< TXQ_CMD_SHIFT
) |
1486 (sas_port
->phy_mask
<< TXQ_PHY_SHIFT
));
1488 hdr
->flags
|= flags
;
1489 hdr
->lens
= cpu_to_le32(((resp_len
/ 4) << 16) | ((req_len
- 4) / 4));
1490 hdr
->tags
= cpu_to_le32(tag
);
1493 /* generate open address frame hdr (first 12 bytes) */
1494 buf_oaf
[0] = (1 << 7) | (0 << 4) | 0x01; /* initiator, SMP, ftype 1h */
1495 buf_oaf
[1] = task
->dev
->linkrate
& 0xf;
1496 *(u16
*)(buf_oaf
+ 2) = 0xFFFF; /* SAS SPEC */
1497 memcpy(buf_oaf
+ 4, task
->dev
->sas_addr
, SAS_ADDR_SIZE
);
1499 /* fill in PRD (scatter/gather) table, if any */
1500 for_each_sg(task
->scatter
, sg
, tei
->n_elem
, i
) {
1501 buf_prd
->addr
= cpu_to_le64(sg_dma_address(sg
));
1502 buf_prd
->len
= cpu_to_le32(sg_dma_len(sg
));
1507 /* copy cmd table */
1508 from
= kmap_atomic(sg_page(sg_req
), KM_IRQ0
);
1509 memcpy(buf_cmd
, from
+ sg_req
->offset
, req_len
);
1510 kunmap_atomic(from
, KM_IRQ0
);
1515 pci_unmap_sg(mvi
->pdev
, &tei
->task
->smp_task
.smp_resp
, 1,
1516 PCI_DMA_FROMDEVICE
);
1518 pci_unmap_sg(mvi
->pdev
, &tei
->task
->smp_task
.smp_req
, 1,
1523 static void mvs_free_reg_set(struct mvs_info
*mvi
, struct mvs_port
*port
)
1525 void __iomem
*regs
= mvi
->regs
;
1527 u8
*tfs
= &port
->taskfileset
;
1529 if (*tfs
== MVS_ID_NOT_MAPPED
)
1532 offs
= 1U << ((*tfs
& 0x0f) + PCS_EN_SATA_REG_SHIFT
);
1535 mw32(PCS
, tmp
& ~offs
);
1538 mw32(CTL
, tmp
& ~offs
);
1541 tmp
= mr32(INT_STAT_SRS
) & (1U << *tfs
);
1543 mw32(INT_STAT_SRS
, tmp
);
1545 *tfs
= MVS_ID_NOT_MAPPED
;
1548 static u8
mvs_assign_reg_set(struct mvs_info
*mvi
, struct mvs_port
*port
)
1552 void __iomem
*regs
= mvi
->regs
;
1554 if (port
->taskfileset
!= MVS_ID_NOT_MAPPED
)
1559 for (i
= 0; i
< mvi
->chip
->srs_sz
; i
++) {
1562 offs
= 1U << ((i
& 0x0f) + PCS_EN_SATA_REG_SHIFT
);
1563 if (!(tmp
& offs
)) {
1564 port
->taskfileset
= i
;
1567 mw32(PCS
, tmp
| offs
);
1569 mw32(CTL
, tmp
| offs
);
1570 tmp
= mr32(INT_STAT_SRS
) & (1U << i
);
1572 mw32(INT_STAT_SRS
, tmp
);
1576 return MVS_ID_NOT_MAPPED
;
1579 static u32
mvs_get_ncq_tag(struct sas_task
*task
)
1582 struct ata_queued_cmd
*qc
= task
->uldd_task
;
1590 static int mvs_task_prep_ata(struct mvs_info
*mvi
,
1591 struct mvs_task_exec_info
*tei
)
1593 struct sas_task
*task
= tei
->task
;
1594 struct domain_device
*dev
= task
->dev
;
1595 struct mvs_cmd_hdr
*hdr
= tei
->hdr
;
1596 struct asd_sas_port
*sas_port
= dev
->port
;
1597 struct mvs_slot_info
*slot
;
1598 struct scatterlist
*sg
;
1599 struct mvs_prd
*buf_prd
;
1600 struct mvs_port
*port
= tei
->port
;
1602 u32 flags
= (tei
->n_elem
<< MCH_PRD_LEN_SHIFT
);
1604 u8
*buf_cmd
, *buf_oaf
;
1605 dma_addr_t buf_tmp_dma
;
1606 u32 i
, req_len
, resp_len
;
1607 const u32 max_resp_len
= SB_RFB_MAX
;
1609 if (mvs_assign_reg_set(mvi
, port
) == MVS_ID_NOT_MAPPED
)
1612 slot
= &mvi
->slot_info
[tag
];
1613 slot
->tx
= mvi
->tx_prod
;
1614 mvi
->tx
[mvi
->tx_prod
] = cpu_to_le32(TXQ_MODE_I
| tag
|
1615 (TXQ_CMD_STP
<< TXQ_CMD_SHIFT
) |
1616 (sas_port
->phy_mask
<< TXQ_PHY_SHIFT
) |
1617 (port
->taskfileset
<< TXQ_SRS_SHIFT
));
1619 if (task
->ata_task
.use_ncq
)
1621 if (dev
->sata_dev
.command_set
== ATAPI_COMMAND_SET
) {
1622 if (task
->ata_task
.fis
.command
!= ATA_CMD_ID_ATAPI
)
1626 /* FIXME: fill in port multiplier number */
1628 hdr
->flags
= cpu_to_le32(flags
);
1630 /* FIXME: the low order order 5 bits for the TAG if enable NCQ */
1631 if (task
->ata_task
.use_ncq
) {
1632 hdr
->tags
= cpu_to_le32(mvs_get_ncq_tag(task
));
1633 /*Fill in task file */
1634 task
->ata_task
.fis
.sector_count
= hdr
->tags
<< 3;
1636 hdr
->tags
= cpu_to_le32(tag
);
1637 hdr
->data_len
= cpu_to_le32(task
->total_xfer_len
);
1640 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
1643 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
1644 buf_cmd
= buf_tmp
= slot
->buf
;
1645 buf_tmp_dma
= slot
->buf_dma
;
1647 hdr
->cmd_tbl
= cpu_to_le64(buf_tmp_dma
);
1649 buf_tmp
+= MVS_ATA_CMD_SZ
;
1650 buf_tmp_dma
+= MVS_ATA_CMD_SZ
;
1652 slot
->cmd_size
= MVS_ATA_CMD_SZ
;
1655 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
1656 /* used for STP. unused for SATA? */
1658 hdr
->open_frame
= cpu_to_le64(buf_tmp_dma
);
1660 buf_tmp
+= MVS_OAF_SZ
;
1661 buf_tmp_dma
+= MVS_OAF_SZ
;
1663 /* region 3: PRD table ********************************************* */
1666 hdr
->prd_tbl
= cpu_to_le64(buf_tmp_dma
);
1670 i
= sizeof(struct mvs_prd
) * tei
->n_elem
;
1674 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
1675 /* FIXME: probably unused, for SATA. kept here just in case
1676 * we get a STP/SATA error information record
1678 slot
->response
= buf_tmp
;
1679 hdr
->status_buf
= cpu_to_le64(buf_tmp_dma
);
1681 req_len
= sizeof(struct host_to_dev_fis
);
1682 resp_len
= MVS_SLOT_BUF_SZ
- MVS_ATA_CMD_SZ
-
1683 sizeof(struct mvs_err_info
) - i
;
1685 /* request, response lengths */
1686 resp_len
= min(resp_len
, max_resp_len
);
1687 hdr
->lens
= cpu_to_le32(((resp_len
/ 4) << 16) | (req_len
/ 4));
1689 task
->ata_task
.fis
.flags
|= 0x80; /* C=1: update ATA cmd reg */
1690 /* fill in command FIS and ATAPI CDB */
1691 memcpy(buf_cmd
, &task
->ata_task
.fis
, sizeof(struct host_to_dev_fis
));
1692 if (dev
->sata_dev
.command_set
== ATAPI_COMMAND_SET
)
1693 memcpy(buf_cmd
+ STP_ATAPI_CMD
,
1694 task
->ata_task
.atapi_packet
, 16);
1696 /* generate open address frame hdr (first 12 bytes) */
1697 buf_oaf
[0] = (1 << 7) | (2 << 4) | 0x1; /* initiator, STP, ftype 1h */
1698 buf_oaf
[1] = task
->dev
->linkrate
& 0xf;
1699 *(u16
*)(buf_oaf
+ 2) = cpu_to_be16(tag
);
1700 memcpy(buf_oaf
+ 4, task
->dev
->sas_addr
, SAS_ADDR_SIZE
);
1702 /* fill in PRD (scatter/gather) table, if any */
1703 for_each_sg(task
->scatter
, sg
, tei
->n_elem
, i
) {
1704 buf_prd
->addr
= cpu_to_le64(sg_dma_address(sg
));
1705 buf_prd
->len
= cpu_to_le32(sg_dma_len(sg
));
1712 static int mvs_task_prep_ssp(struct mvs_info
*mvi
,
1713 struct mvs_task_exec_info
*tei
)
1715 struct sas_task
*task
= tei
->task
;
1716 struct mvs_cmd_hdr
*hdr
= tei
->hdr
;
1717 struct mvs_port
*port
= tei
->port
;
1718 struct mvs_slot_info
*slot
;
1719 struct scatterlist
*sg
;
1720 struct mvs_prd
*buf_prd
;
1721 struct ssp_frame_hdr
*ssp_hdr
;
1723 u8
*buf_cmd
, *buf_oaf
, fburst
= 0;
1724 dma_addr_t buf_tmp_dma
;
1726 u32 resp_len
, req_len
, i
, tag
= tei
->tag
;
1727 const u32 max_resp_len
= SB_RFB_MAX
;
1729 slot
= &mvi
->slot_info
[tag
];
1731 slot
->tx
= mvi
->tx_prod
;
1732 mvi
->tx
[mvi
->tx_prod
] = cpu_to_le32(TXQ_MODE_I
| tag
|
1733 (TXQ_CMD_SSP
<< TXQ_CMD_SHIFT
) |
1734 (port
->wide_port_phymap
<< TXQ_PHY_SHIFT
));
1737 if (task
->ssp_task
.enable_first_burst
) {
1738 flags
|= MCH_FBURST
;
1741 hdr
->flags
= cpu_to_le32(flags
|
1742 (tei
->n_elem
<< MCH_PRD_LEN_SHIFT
) |
1743 (MCH_SSP_FR_CMD
<< MCH_SSP_FR_TYPE_SHIFT
));
1745 hdr
->tags
= cpu_to_le32(tag
);
1746 hdr
->data_len
= cpu_to_le32(task
->total_xfer_len
);
1749 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
1752 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
1753 buf_cmd
= buf_tmp
= slot
->buf
;
1754 buf_tmp_dma
= slot
->buf_dma
;
1756 hdr
->cmd_tbl
= cpu_to_le64(buf_tmp_dma
);
1758 buf_tmp
+= MVS_SSP_CMD_SZ
;
1759 buf_tmp_dma
+= MVS_SSP_CMD_SZ
;
1761 slot
->cmd_size
= MVS_SSP_CMD_SZ
;
1764 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
1766 hdr
->open_frame
= cpu_to_le64(buf_tmp_dma
);
1768 buf_tmp
+= MVS_OAF_SZ
;
1769 buf_tmp_dma
+= MVS_OAF_SZ
;
1771 /* region 3: PRD table ********************************************* */
1774 hdr
->prd_tbl
= cpu_to_le64(buf_tmp_dma
);
1778 i
= sizeof(struct mvs_prd
) * tei
->n_elem
;
1782 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
1783 slot
->response
= buf_tmp
;
1784 hdr
->status_buf
= cpu_to_le64(buf_tmp_dma
);
1786 resp_len
= MVS_SLOT_BUF_SZ
- MVS_SSP_CMD_SZ
- MVS_OAF_SZ
-
1787 sizeof(struct mvs_err_info
) - i
;
1788 resp_len
= min(resp_len
, max_resp_len
);
1790 req_len
= sizeof(struct ssp_frame_hdr
) + 28;
1792 /* request, response lengths */
1793 hdr
->lens
= cpu_to_le32(((resp_len
/ 4) << 16) | (req_len
/ 4));
1795 /* generate open address frame hdr (first 12 bytes) */
1796 buf_oaf
[0] = (1 << 7) | (1 << 4) | 0x1; /* initiator, SSP, ftype 1h */
1797 buf_oaf
[1] = task
->dev
->linkrate
& 0xf;
1798 *(u16
*)(buf_oaf
+ 2) = cpu_to_be16(tag
);
1799 memcpy(buf_oaf
+ 4, task
->dev
->sas_addr
, SAS_ADDR_SIZE
);
1801 /* fill in SSP frame header (Command Table.SSP frame header) */
1802 ssp_hdr
= (struct ssp_frame_hdr
*)buf_cmd
;
1803 ssp_hdr
->frame_type
= SSP_COMMAND
;
1804 memcpy(ssp_hdr
->hashed_dest_addr
, task
->dev
->hashed_sas_addr
,
1805 HASHED_SAS_ADDR_SIZE
);
1806 memcpy(ssp_hdr
->hashed_src_addr
,
1807 task
->dev
->port
->ha
->hashed_sas_addr
, HASHED_SAS_ADDR_SIZE
);
1808 ssp_hdr
->tag
= cpu_to_be16(tag
);
1810 /* fill in command frame IU */
1811 buf_cmd
+= sizeof(*ssp_hdr
);
1812 memcpy(buf_cmd
, &task
->ssp_task
.LUN
, 8);
1813 buf_cmd
[9] = fburst
| task
->ssp_task
.task_attr
|
1814 (task
->ssp_task
.task_prio
<< 3);
1815 memcpy(buf_cmd
+ 12, &task
->ssp_task
.cdb
, 16);
1817 /* fill in PRD (scatter/gather) table, if any */
1818 for_each_sg(task
->scatter
, sg
, tei
->n_elem
, i
) {
1819 buf_prd
->addr
= cpu_to_le64(sg_dma_address(sg
));
1820 buf_prd
->len
= cpu_to_le32(sg_dma_len(sg
));
1827 static int mvs_task_exec(struct sas_task
*task
, const int num
, gfp_t gfp_flags
)
1829 struct domain_device
*dev
= task
->dev
;
1830 struct mvs_info
*mvi
= dev
->port
->ha
->lldd_ha
;
1831 struct pci_dev
*pdev
= mvi
->pdev
;
1832 void __iomem
*regs
= mvi
->regs
;
1833 struct mvs_task_exec_info tei
;
1834 struct sas_task
*t
= task
;
1835 u32 tag
= 0xdeadbeef, rc
, n_elem
= 0;
1836 unsigned long flags
;
1837 u32 n
= num
, pass
= 0;
1839 spin_lock_irqsave(&mvi
->lock
, flags
);
1842 tei
.port
= &mvi
->port
[dev
->port
->id
];
1844 if (!tei
.port
->port_attached
) {
1845 struct task_status_struct
*ts
= &t
->task_status
;
1846 ts
->stat
= SAS_PHY_DOWN
;
1851 if (!sas_protocol_ata(t
->task_proto
)) {
1852 if (t
->num_scatter
) {
1853 n_elem
= pci_map_sg(mvi
->pdev
, t
->scatter
,
1862 n_elem
= t
->num_scatter
;
1865 rc
= mvs_tag_alloc(mvi
, &tag
);
1869 mvi
->slot_info
[tag
].task
= t
;
1870 mvi
->slot_info
[tag
].n_elem
= n_elem
;
1871 memset(mvi
->slot_info
[tag
].buf
, 0, MVS_SLOT_BUF_SZ
);
1873 tei
.hdr
= &mvi
->slot
[tag
];
1875 tei
.n_elem
= n_elem
;
1877 switch (t
->task_proto
) {
1878 case SAS_PROTOCOL_SMP
:
1879 rc
= mvs_task_prep_smp(mvi
, &tei
);
1881 case SAS_PROTOCOL_SSP
:
1882 rc
= mvs_task_prep_ssp(mvi
, &tei
);
1884 case SAS_PROTOCOL_SATA
:
1885 case SAS_PROTOCOL_STP
:
1886 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
1887 rc
= mvs_task_prep_ata(mvi
, &tei
);
1890 dev_printk(KERN_ERR
, &pdev
->dev
,
1891 "unknown sas_task proto: 0x%x\n",
1900 /* TODO: select normal or high priority */
1902 spin_lock(&t
->task_state_lock
);
1903 t
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
1904 spin_unlock(&t
->task_state_lock
);
1907 spin_unlock_irqrestore(&mvi
->lock
, flags
);
1908 mw32(TX_PROD_IDX
, mvi
->tx_prod
);
1910 mvs_hba_memory_dump(mvi
, tag
, t
->task_proto
);
1913 mvi
->tx_prod
= (mvi
->tx_prod
+ 1) & (MVS_CHIP_SLOT_SZ
- 1);
1918 t
= list_entry(t
->list
.next
, struct sas_task
, list
);
1924 mvs_tag_free(mvi
, tag
);
1926 dev_printk(KERN_ERR
, &pdev
->dev
, "mvsas exec failed[%d]!\n", rc
);
1927 if (!sas_protocol_ata(t
->task_proto
))
1929 pci_unmap_sg(mvi
->pdev
, t
->scatter
, n_elem
,
1933 mw32(TX_PROD_IDX
, (mvi
->tx_prod
- 1) & (MVS_CHIP_SLOT_SZ
- 1));
1934 spin_unlock_irqrestore(&mvi
->lock
, flags
);
1938 static int mvs_task_abort(struct sas_task
*task
)
1941 unsigned long flags
;
1942 struct mvs_info
*mvi
= task
->dev
->port
->ha
->lldd_ha
;
1943 struct pci_dev
*pdev
= mvi
->pdev
;
1945 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1946 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1947 rc
= TMF_RESP_FUNC_COMPLETE
;
1950 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1953 rc
= TMF_RESP_FUNC_COMPLETE
;
1955 switch (task
->task_proto
) {
1956 case SAS_PROTOCOL_SMP
:
1957 dev_printk(KERN_DEBUG
, &pdev
->dev
, "SMP Abort! ");
1959 case SAS_PROTOCOL_SSP
:
1960 dev_printk(KERN_DEBUG
, &pdev
->dev
, "SSP Abort! ");
1962 case SAS_PROTOCOL_SATA
:
1963 case SAS_PROTOCOL_STP
:
1964 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:{
1965 dev_printk(KERN_DEBUG
, &pdev
->dev
, "STP Abort! "
1966 "Dump D2H FIS: \n");
1967 mvs_hexdump(sizeof(struct host_to_dev_fis
),
1968 (void *)&task
->ata_task
.fis
, 0);
1969 dev_printk(KERN_DEBUG
, &pdev
->dev
, "Dump ATAPI Cmd : \n");
1970 mvs_hexdump(16, task
->ata_task
.atapi_packet
, 0);
1980 static void mvs_free(struct mvs_info
*mvi
)
1987 for (i
= 0; i
< MVS_SLOTS
; i
++) {
1988 struct mvs_slot_info
*slot
= &mvi
->slot_info
[i
];
1991 dma_free_coherent(&mvi
->pdev
->dev
, MVS_SLOT_BUF_SZ
,
1992 slot
->buf
, slot
->buf_dma
);
1996 dma_free_coherent(&mvi
->pdev
->dev
,
1997 sizeof(*mvi
->tx
) * MVS_CHIP_SLOT_SZ
,
1998 mvi
->tx
, mvi
->tx_dma
);
2000 dma_free_coherent(&mvi
->pdev
->dev
, MVS_RX_FISL_SZ
,
2001 mvi
->rx_fis
, mvi
->rx_fis_dma
);
2003 dma_free_coherent(&mvi
->pdev
->dev
,
2004 sizeof(*mvi
->rx
) * MVS_RX_RING_SZ
,
2005 mvi
->rx
, mvi
->rx_dma
);
2007 dma_free_coherent(&mvi
->pdev
->dev
,
2008 sizeof(*mvi
->slot
) * MVS_SLOTS
,
2009 mvi
->slot
, mvi
->slot_dma
);
2010 #ifdef MVS_ENABLE_PERI
2012 iounmap(mvi
->peri_regs
);
2017 scsi_host_put(mvi
->shost
);
2018 kfree(mvi
->sas
.sas_port
);
2019 kfree(mvi
->sas
.sas_phy
);
2023 /* FIXME: locking? */
2024 static int mvs_phy_control(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
2027 struct mvs_info
*mvi
= sas_phy
->ha
->lldd_ha
;
2028 int rc
= 0, phy_id
= sas_phy
->id
;
2031 tmp
= mvs_read_phy_ctl(mvi
, phy_id
);
2034 case PHY_FUNC_SET_LINK_RATE
:{
2035 struct sas_phy_linkrates
*rates
= funcdata
;
2036 u32 lrmin
= 0, lrmax
= 0;
2038 lrmin
= (rates
->minimum_linkrate
<< 8);
2039 lrmax
= (rates
->maximum_linkrate
<< 12);
2046 tmp
&= ~(0xf << 12);
2049 mvs_write_phy_ctl(mvi
, phy_id
, tmp
);
2053 case PHY_FUNC_HARD_RESET
:
2054 if (tmp
& PHY_RST_HARD
)
2056 mvs_write_phy_ctl(mvi
, phy_id
, tmp
| PHY_RST_HARD
);
2059 case PHY_FUNC_LINK_RESET
:
2060 mvs_write_phy_ctl(mvi
, phy_id
, tmp
| PHY_RST
);
2063 case PHY_FUNC_DISABLE
:
2064 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
2072 static void __devinit
mvs_phy_init(struct mvs_info
*mvi
, int phy_id
)
2074 struct mvs_phy
*phy
= &mvi
->phy
[phy_id
];
2075 struct asd_sas_phy
*sas_phy
= &phy
->sas_phy
;
2077 sas_phy
->enabled
= (phy_id
< mvi
->chip
->n_phy
) ? 1 : 0;
2078 sas_phy
->class = SAS
;
2079 sas_phy
->iproto
= SAS_PROTOCOL_ALL
;
2080 sas_phy
->tproto
= 0;
2081 sas_phy
->type
= PHY_TYPE_PHYSICAL
;
2082 sas_phy
->role
= PHY_ROLE_INITIATOR
;
2083 sas_phy
->oob_mode
= OOB_NOT_CONNECTED
;
2084 sas_phy
->linkrate
= SAS_LINK_RATE_UNKNOWN
;
2086 sas_phy
->id
= phy_id
;
2087 sas_phy
->sas_addr
= &mvi
->sas_addr
[0];
2088 sas_phy
->frame_rcvd
= &phy
->frame_rcvd
[0];
2089 sas_phy
->ha
= &mvi
->sas
;
2090 sas_phy
->lldd_phy
= phy
;
2093 static struct mvs_info
*__devinit
mvs_alloc(struct pci_dev
*pdev
,
2094 const struct pci_device_id
*ent
)
2096 struct mvs_info
*mvi
;
2097 unsigned long res_start
, res_len
, res_flag
;
2098 struct asd_sas_phy
**arr_phy
;
2099 struct asd_sas_port
**arr_port
;
2100 const struct mvs_chip_info
*chip
= &mvs_chips
[ent
->driver_data
];
2104 * alloc and init our per-HBA mvs_info struct
2107 mvi
= kzalloc(sizeof(*mvi
), GFP_KERNEL
);
2111 spin_lock_init(&mvi
->lock
);
2115 if (pdev
->device
== 0x6440 && pdev
->revision
== 0)
2116 mvi
->flags
|= MVF_PHY_PWR_FIX
;
2119 * alloc and init SCSI, SAS glue
2122 mvi
->shost
= scsi_host_alloc(&mvs_sht
, sizeof(void *));
2126 arr_phy
= kcalloc(MVS_MAX_PHYS
, sizeof(void *), GFP_KERNEL
);
2127 arr_port
= kcalloc(MVS_MAX_PHYS
, sizeof(void *), GFP_KERNEL
);
2128 if (!arr_phy
|| !arr_port
)
2131 for (i
= 0; i
< MVS_MAX_PHYS
; i
++) {
2132 mvs_phy_init(mvi
, i
);
2133 arr_phy
[i
] = &mvi
->phy
[i
].sas_phy
;
2134 arr_port
[i
] = &mvi
->port
[i
].sas_port
;
2137 SHOST_TO_SAS_HA(mvi
->shost
) = &mvi
->sas
;
2138 mvi
->shost
->transportt
= mvs_stt
;
2139 mvi
->shost
->max_id
= 21;
2140 mvi
->shost
->max_lun
= ~0;
2141 mvi
->shost
->max_channel
= 0;
2142 mvi
->shost
->max_cmd_len
= 16;
2144 mvi
->sas
.sas_ha_name
= DRV_NAME
;
2145 mvi
->sas
.dev
= &pdev
->dev
;
2146 mvi
->sas
.lldd_module
= THIS_MODULE
;
2147 mvi
->sas
.sas_addr
= &mvi
->sas_addr
[0];
2148 mvi
->sas
.sas_phy
= arr_phy
;
2149 mvi
->sas
.sas_port
= arr_port
;
2150 mvi
->sas
.num_phys
= chip
->n_phy
;
2151 mvi
->sas
.lldd_max_execute_num
= MVS_CHIP_SLOT_SZ
- 1;
2152 mvi
->sas
.lldd_queue_size
= MVS_QUEUE_SIZE
;
2153 mvi
->can_queue
= (MVS_CHIP_SLOT_SZ
>> 1) - 1;
2154 mvi
->sas
.lldd_ha
= mvi
;
2155 mvi
->sas
.core
.shost
= mvi
->shost
;
2160 * ioremap main and peripheral registers
2163 #ifdef MVS_ENABLE_PERI
2164 res_start
= pci_resource_start(pdev
, 2);
2165 res_len
= pci_resource_len(pdev
, 2);
2166 if (!res_start
|| !res_len
)
2169 mvi
->peri_regs
= ioremap_nocache(res_start
, res_len
);
2170 if (!mvi
->peri_regs
)
2174 res_start
= pci_resource_start(pdev
, 4);
2175 res_len
= pci_resource_len(pdev
, 4);
2176 if (!res_start
|| !res_len
)
2179 res_flag
= pci_resource_flags(pdev
, 4);
2180 if (res_flag
& IORESOURCE_CACHEABLE
)
2181 mvi
->regs
= ioremap(res_start
, res_len
);
2183 mvi
->regs
= ioremap_nocache(res_start
, res_len
);
2189 * alloc and init our DMA areas
2192 mvi
->tx
= dma_alloc_coherent(&pdev
->dev
,
2193 sizeof(*mvi
->tx
) * MVS_CHIP_SLOT_SZ
,
2194 &mvi
->tx_dma
, GFP_KERNEL
);
2197 memset(mvi
->tx
, 0, sizeof(*mvi
->tx
) * MVS_CHIP_SLOT_SZ
);
2199 mvi
->rx_fis
= dma_alloc_coherent(&pdev
->dev
, MVS_RX_FISL_SZ
,
2200 &mvi
->rx_fis_dma
, GFP_KERNEL
);
2203 memset(mvi
->rx_fis
, 0, MVS_RX_FISL_SZ
);
2205 mvi
->rx
= dma_alloc_coherent(&pdev
->dev
,
2206 sizeof(*mvi
->rx
) * MVS_RX_RING_SZ
,
2207 &mvi
->rx_dma
, GFP_KERNEL
);
2210 memset(mvi
->rx
, 0, sizeof(*mvi
->rx
) * MVS_RX_RING_SZ
);
2212 mvi
->rx
[0] = cpu_to_le32(0xfff);
2213 mvi
->rx_cons
= 0xfff;
2215 mvi
->slot
= dma_alloc_coherent(&pdev
->dev
,
2216 sizeof(*mvi
->slot
) * MVS_SLOTS
,
2217 &mvi
->slot_dma
, GFP_KERNEL
);
2220 memset(mvi
->slot
, 0, sizeof(*mvi
->slot
) * MVS_SLOTS
);
2222 for (i
= 0; i
< MVS_SLOTS
; i
++) {
2223 struct mvs_slot_info
*slot
= &mvi
->slot_info
[i
];
2225 slot
->buf
= dma_alloc_coherent(&pdev
->dev
, MVS_SLOT_BUF_SZ
,
2226 &slot
->buf_dma
, GFP_KERNEL
);
2229 memset(slot
->buf
, 0, MVS_SLOT_BUF_SZ
);
2232 /* finally, read NVRAM to get our SAS address */
2233 if (mvs_nvram_read(mvi
, NVR_SAS_ADDR
, &mvi
->sas_addr
, 8))
2242 static u32
mvs_cr32(void __iomem
*regs
, u32 addr
)
2244 mw32(CMD_ADDR
, addr
);
2245 return mr32(CMD_DATA
);
2248 static void mvs_cw32(void __iomem
*regs
, u32 addr
, u32 val
)
2250 mw32(CMD_ADDR
, addr
);
2251 mw32(CMD_DATA
, val
);
2254 static u32
mvs_read_phy_ctl(struct mvs_info
*mvi
, u32 port
)
2256 void __iomem
*regs
= mvi
->regs
;
2257 return (port
< 4)?mr32(P0_SER_CTLSTAT
+ port
* 4):
2258 mr32(P4_SER_CTLSTAT
+ (port
- 4) * 4);
2261 static void mvs_write_phy_ctl(struct mvs_info
*mvi
, u32 port
, u32 val
)
2263 void __iomem
*regs
= mvi
->regs
;
2265 mw32(P0_SER_CTLSTAT
+ port
* 4, val
);
2267 mw32(P4_SER_CTLSTAT
+ (port
- 4) * 4, val
);
2270 static u32
mvs_read_port(struct mvs_info
*mvi
, u32 off
, u32 off2
, u32 port
)
2272 void __iomem
*regs
= mvi
->regs
+ off
;
2273 void __iomem
*regs2
= mvi
->regs
+ off2
;
2274 return (port
< 4)?readl(regs
+ port
* 8):
2275 readl(regs2
+ (port
- 4) * 8);
2278 static void mvs_write_port(struct mvs_info
*mvi
, u32 off
, u32 off2
,
2281 void __iomem
*regs
= mvi
->regs
+ off
;
2282 void __iomem
*regs2
= mvi
->regs
+ off2
;
2284 writel(val
, regs
+ port
* 8);
2286 writel(val
, regs2
+ (port
- 4) * 8);
2289 static u32
mvs_read_port_cfg_data(struct mvs_info
*mvi
, u32 port
)
2291 return mvs_read_port(mvi
, MVS_P0_CFG_DATA
, MVS_P4_CFG_DATA
, port
);
2294 static void mvs_write_port_cfg_data(struct mvs_info
*mvi
, u32 port
, u32 val
)
2296 mvs_write_port(mvi
, MVS_P0_CFG_DATA
, MVS_P4_CFG_DATA
, port
, val
);
2299 static void mvs_write_port_cfg_addr(struct mvs_info
*mvi
, u32 port
, u32 addr
)
2301 mvs_write_port(mvi
, MVS_P0_CFG_ADDR
, MVS_P4_CFG_ADDR
, port
, addr
);
2304 static u32
mvs_read_port_vsr_data(struct mvs_info
*mvi
, u32 port
)
2306 return mvs_read_port(mvi
, MVS_P0_VSR_DATA
, MVS_P4_VSR_DATA
, port
);
2309 static void mvs_write_port_vsr_data(struct mvs_info
*mvi
, u32 port
, u32 val
)
2311 mvs_write_port(mvi
, MVS_P0_VSR_DATA
, MVS_P4_VSR_DATA
, port
, val
);
2314 static void mvs_write_port_vsr_addr(struct mvs_info
*mvi
, u32 port
, u32 addr
)
2316 mvs_write_port(mvi
, MVS_P0_VSR_ADDR
, MVS_P4_VSR_ADDR
, port
, addr
);
2319 static u32
mvs_read_port_irq_stat(struct mvs_info
*mvi
, u32 port
)
2321 return mvs_read_port(mvi
, MVS_P0_INT_STAT
, MVS_P4_INT_STAT
, port
);
2324 static void mvs_write_port_irq_stat(struct mvs_info
*mvi
, u32 port
, u32 val
)
2326 mvs_write_port(mvi
, MVS_P0_INT_STAT
, MVS_P4_INT_STAT
, port
, val
);
2329 static u32
mvs_read_port_irq_mask(struct mvs_info
*mvi
, u32 port
)
2331 return mvs_read_port(mvi
, MVS_P0_INT_MASK
, MVS_P4_INT_MASK
, port
);
2334 static void mvs_write_port_irq_mask(struct mvs_info
*mvi
, u32 port
, u32 val
)
2336 mvs_write_port(mvi
, MVS_P0_INT_MASK
, MVS_P4_INT_MASK
, port
, val
);
2339 static void __devinit
mvs_phy_hacks(struct mvs_info
*mvi
)
2341 void __iomem
*regs
= mvi
->regs
;
2344 /* workaround for SATA R-ERR, to ignore phy glitch */
2345 tmp
= mvs_cr32(regs
, CMD_PHY_TIMER
);
2348 mvs_cw32(regs
, CMD_PHY_TIMER
, tmp
);
2350 /* enable retry 127 times */
2351 mvs_cw32(regs
, CMD_SAS_CTL1
, 0x7f7f);
2353 /* extend open frame timeout to max */
2354 tmp
= mvs_cr32(regs
, CMD_SAS_CTL0
);
2357 mvs_cw32(regs
, CMD_SAS_CTL0
, tmp
);
2359 /* workaround for WDTIMEOUT , set to 550 ms */
2360 mvs_cw32(regs
, CMD_WD_TIMER
, 0xffffff);
2362 /* not to halt for different port op during wideport link change */
2363 mvs_cw32(regs
, CMD_APP_ERR_CONFIG
, 0xffefbf7d);
2365 /* workaround for Seagate disk not-found OOB sequence, recv
2366 * COMINIT before sending out COMWAKE */
2367 tmp
= mvs_cr32(regs
, CMD_PHY_MODE_21
);
2370 mvs_cw32(regs
, CMD_PHY_MODE_21
, tmp
);
2372 tmp
= mvs_cr32(regs
, CMD_PHY_TIMER
);
2374 tmp
|= (2U << 29); /* 8 ms retry */
2375 mvs_cw32(regs
, CMD_PHY_TIMER
, tmp
);
2377 /* TEST - for phy decoding error, adjust voltage levels */
2378 mw32(P0_VSR_ADDR
+ 0, 0x8);
2379 mw32(P0_VSR_DATA
+ 0, 0x2F0);
2381 mw32(P0_VSR_ADDR
+ 8, 0x8);
2382 mw32(P0_VSR_DATA
+ 8, 0x2F0);
2384 mw32(P0_VSR_ADDR
+ 16, 0x8);
2385 mw32(P0_VSR_DATA
+ 16, 0x2F0);
2387 mw32(P0_VSR_ADDR
+ 24, 0x8);
2388 mw32(P0_VSR_DATA
+ 24, 0x2F0);
2392 static void mvs_enable_xmt(struct mvs_info
*mvi
, int PhyId
)
2394 void __iomem
*regs
= mvi
->regs
;
2398 if (mvi
->chip
->n_phy
<= 4)
2399 tmp
|= 1 << (PhyId
+ PCS_EN_PORT_XMT_SHIFT
);
2401 tmp
|= 1 << (PhyId
+ PCS_EN_PORT_XMT_SHIFT2
);
2405 static void mvs_detect_porttype(struct mvs_info
*mvi
, int i
)
2407 void __iomem
*regs
= mvi
->regs
;
2409 struct mvs_phy
*phy
= &mvi
->phy
[i
];
2411 /* TODO check & save device type */
2412 reg
= mr32(GBL_PORT_TYPE
);
2414 if (reg
& MODE_SAS_SATA
& (1 << i
))
2415 phy
->phy_type
|= PORT_TYPE_SAS
;
2417 phy
->phy_type
|= PORT_TYPE_SATA
;
2420 static void *mvs_get_d2h_reg(struct mvs_info
*mvi
, int i
, void *buf
)
2422 u32
*s
= (u32
*) buf
;
2427 mvs_write_port_cfg_addr(mvi
, i
, PHYR_SATA_SIG3
);
2428 s
[3] = mvs_read_port_cfg_data(mvi
, i
);
2430 mvs_write_port_cfg_addr(mvi
, i
, PHYR_SATA_SIG2
);
2431 s
[2] = mvs_read_port_cfg_data(mvi
, i
);
2433 mvs_write_port_cfg_addr(mvi
, i
, PHYR_SATA_SIG1
);
2434 s
[1] = mvs_read_port_cfg_data(mvi
, i
);
2436 mvs_write_port_cfg_addr(mvi
, i
, PHYR_SATA_SIG0
);
2437 s
[0] = mvs_read_port_cfg_data(mvi
, i
);
2442 static u32
mvs_is_sig_fis_received(u32 irq_status
)
2444 return irq_status
& PHYEV_SIG_FIS
;
2447 static void mvs_update_wideport(struct mvs_info
*mvi
, int i
)
2449 struct mvs_phy
*phy
= &mvi
->phy
[i
];
2450 struct mvs_port
*port
= phy
->port
;
2453 for_each_phy(port
->wide_port_phymap
, no
, j
, mvi
->chip
->n_phy
)
2455 mvs_write_port_cfg_addr(mvi
, no
, PHYR_WIDE_PORT
);
2456 mvs_write_port_cfg_data(mvi
, no
,
2457 port
->wide_port_phymap
);
2459 mvs_write_port_cfg_addr(mvi
, no
, PHYR_WIDE_PORT
);
2460 mvs_write_port_cfg_data(mvi
, no
, 0);
2464 static u32
mvs_is_phy_ready(struct mvs_info
*mvi
, int i
)
2467 struct mvs_phy
*phy
= &mvi
->phy
[i
];
2468 struct mvs_port
*port
;
2470 tmp
= mvs_read_phy_ctl(mvi
, i
);
2472 if ((tmp
& PHY_READY_MASK
) && !(phy
->irq_status
& PHYEV_POOF
)) {
2474 phy
->phy_attached
= 1;
2480 if (phy
->phy_type
& PORT_TYPE_SAS
) {
2481 port
->wide_port_phymap
&= ~(1U << i
);
2482 if (!port
->wide_port_phymap
)
2483 port
->port_attached
= 0;
2484 mvs_update_wideport(mvi
, i
);
2485 } else if (phy
->phy_type
& PORT_TYPE_SATA
)
2486 port
->port_attached
= 0;
2487 mvs_free_reg_set(mvi
, phy
->port
);
2489 phy
->phy_attached
= 0;
2490 phy
->phy_type
&= ~(PORT_TYPE_SAS
| PORT_TYPE_SATA
);
2495 static void mvs_update_phyinfo(struct mvs_info
*mvi
, int i
,
2498 struct mvs_phy
*phy
= &mvi
->phy
[i
];
2499 struct pci_dev
*pdev
= mvi
->pdev
;
2503 mvs_write_port_cfg_addr(mvi
, i
, PHYR_IDENTIFY
);
2504 phy
->dev_info
= mvs_read_port_cfg_data(mvi
, i
);
2506 mvs_write_port_cfg_addr(mvi
, i
, PHYR_ADDR_HI
);
2507 phy
->dev_sas_addr
= (u64
) mvs_read_port_cfg_data(mvi
, i
) << 32;
2509 mvs_write_port_cfg_addr(mvi
, i
, PHYR_ADDR_LO
);
2510 phy
->dev_sas_addr
|= mvs_read_port_cfg_data(mvi
, i
);
2513 phy
->irq_status
= mvs_read_port_irq_stat(mvi
, i
);
2514 phy
->phy_status
= mvs_is_phy_ready(mvi
, i
);
2517 if (phy
->phy_status
) {
2519 struct asd_sas_phy
*sas_phy
= mvi
->sas
.sas_phy
[i
];
2521 mvs_write_port_cfg_addr(mvi
, i
, PHYR_PHY_STAT
);
2522 phy_st
= mvs_read_port_cfg_data(mvi
, i
);
2525 (phy
->phy_status
& PHY_NEG_SPP_PHYS_LINK_RATE_MASK
) >>
2526 PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET
;
2528 /* Updated attached_sas_addr */
2529 mvs_write_port_cfg_addr(mvi
, i
, PHYR_ATT_ADDR_HI
);
2530 phy
->att_dev_sas_addr
=
2531 (u64
) mvs_read_port_cfg_data(mvi
, i
) << 32;
2533 mvs_write_port_cfg_addr(mvi
, i
, PHYR_ATT_ADDR_LO
);
2534 phy
->att_dev_sas_addr
|= mvs_read_port_cfg_data(mvi
, i
);
2536 dev_printk(KERN_DEBUG
, &pdev
->dev
,
2537 "phy[%d] Get Attached Address 0x%llX ,"
2538 " SAS Address 0x%llX\n",
2539 i
, phy
->att_dev_sas_addr
, phy
->dev_sas_addr
);
2540 dev_printk(KERN_DEBUG
, &pdev
->dev
,
2541 "Rate = %x , type = %d\n",
2542 sas_phy
->linkrate
, phy
->phy_type
);
2546 * If the device is capable of supporting a wide port
2547 * on its phys, it may configure the phys as a wide port.
2549 if (phy
->phy_type
& PORT_TYPE_SAS
)
2550 for (j
= 0; j
< mvi
->chip
->n_phy
&& j
!= i
; ++j
) {
2551 if ((mvi
->phy
[j
].phy_attached
) &&
2552 (mvi
->phy
[j
].phy_type
& PORT_TYPE_SAS
))
2553 if (phy
->att_dev_sas_addr
==
2554 mvi
->phy
[j
].att_dev_sas_addr
- 1) {
2555 phy
->att_dev_sas_addr
=
2556 mvi
->phy
[j
].att_dev_sas_addr
;
2563 tmp64
= cpu_to_be64(phy
->att_dev_sas_addr
);
2564 memcpy(sas_phy
->attached_sas_addr
, &tmp64
, SAS_ADDR_SIZE
);
2566 if (phy
->phy_type
& PORT_TYPE_SAS
) {
2567 mvs_write_port_cfg_addr(mvi
, i
, PHYR_ATT_DEV_INFO
);
2568 phy
->att_dev_info
= mvs_read_port_cfg_data(mvi
, i
);
2569 phy
->identify
.device_type
=
2570 phy
->att_dev_info
& PORT_DEV_TYPE_MASK
;
2572 if (phy
->identify
.device_type
== SAS_END_DEV
)
2573 phy
->identify
.target_port_protocols
=
2575 else if (phy
->identify
.device_type
!= NO_DEVICE
)
2576 phy
->identify
.target_port_protocols
=
2578 if (phy_st
& PHY_OOB_DTCTD
)
2579 sas_phy
->oob_mode
= SAS_OOB_MODE
;
2580 phy
->frame_rcvd_size
=
2581 sizeof(struct sas_identify_frame
);
2582 } else if (phy
->phy_type
& PORT_TYPE_SATA
) {
2583 phy
->identify
.target_port_protocols
= SAS_PROTOCOL_STP
;
2584 if (mvs_is_sig_fis_received(phy
->irq_status
)) {
2585 if (phy_st
& PHY_OOB_DTCTD
)
2586 sas_phy
->oob_mode
= SATA_OOB_MODE
;
2587 phy
->frame_rcvd_size
=
2588 sizeof(struct dev_to_host_fis
);
2589 mvs_get_d2h_reg(mvi
, i
,
2590 (void *)sas_phy
->frame_rcvd
);
2592 dev_printk(KERN_DEBUG
, &pdev
->dev
,
2596 /* workaround for HW phy decoding error on 1.5g disk drive */
2597 mvs_write_port_vsr_addr(mvi
, i
, VSR_PHY_MODE6
);
2598 tmp
= mvs_read_port_vsr_data(mvi
, i
);
2599 if (((phy
->phy_status
& PHY_NEG_SPP_PHYS_LINK_RATE_MASK
) >>
2600 PHY_NEG_SPP_PHYS_LINK_RATE_MASK_OFFSET
) ==
2601 SAS_LINK_RATE_1_5_GBPS
)
2602 tmp
&= ~PHY_MODE6_DTL_SPEED
;
2604 tmp
|= PHY_MODE6_DTL_SPEED
;
2605 mvs_write_port_vsr_data(mvi
, i
, tmp
);
2609 mvs_write_port_irq_stat(mvi
, i
, phy
->irq_status
);
2612 static void mvs_port_formed(struct asd_sas_phy
*sas_phy
)
2614 struct sas_ha_struct
*sas_ha
= sas_phy
->ha
;
2615 struct mvs_info
*mvi
= sas_ha
->lldd_ha
;
2616 struct asd_sas_port
*sas_port
= sas_phy
->port
;
2617 struct mvs_phy
*phy
= sas_phy
->lldd_phy
;
2618 struct mvs_port
*port
= &mvi
->port
[sas_port
->id
];
2619 unsigned long flags
;
2621 spin_lock_irqsave(&mvi
->lock
, flags
);
2622 port
->port_attached
= 1;
2624 port
->taskfileset
= MVS_ID_NOT_MAPPED
;
2625 if (phy
->phy_type
& PORT_TYPE_SAS
) {
2626 port
->wide_port_phymap
= sas_port
->phy_mask
;
2627 mvs_update_wideport(mvi
, sas_phy
->id
);
2629 spin_unlock_irqrestore(&mvi
->lock
, flags
);
2632 static int __devinit
mvs_hw_init(struct mvs_info
*mvi
)
2634 void __iomem
*regs
= mvi
->regs
;
2638 /* make sure interrupts are masked immediately (paranoia) */
2640 tmp
= mr32(GBL_CTL
);
2642 /* Reset Controller */
2643 if (!(tmp
& HBA_RST
)) {
2644 if (mvi
->flags
& MVF_PHY_PWR_FIX
) {
2645 pci_read_config_dword(mvi
->pdev
, PCR_PHY_CTL
, &tmp
);
2646 tmp
&= ~PCTL_PWR_ON
;
2648 pci_write_config_dword(mvi
->pdev
, PCR_PHY_CTL
, tmp
);
2650 pci_read_config_dword(mvi
->pdev
, PCR_PHY_CTL2
, &tmp
);
2651 tmp
&= ~PCTL_PWR_ON
;
2653 pci_write_config_dword(mvi
->pdev
, PCR_PHY_CTL2
, tmp
);
2656 /* global reset, incl. COMRESET/H_RESET_N (self-clearing) */
2657 mw32_f(GBL_CTL
, HBA_RST
);
2660 /* wait for reset to finish; timeout is just a guess */
2665 if (!(mr32(GBL_CTL
) & HBA_RST
))
2668 if (mr32(GBL_CTL
) & HBA_RST
) {
2669 dev_printk(KERN_ERR
, &mvi
->pdev
->dev
, "HBA reset failed\n");
2674 /* make sure RST is set; HBA_RST /should/ have done that for us */
2676 if (cctl
& CCTL_RST
)
2679 mw32_f(CTL
, cctl
| CCTL_RST
);
2681 /* write to device control _AND_ device status register? - A.C. */
2682 pci_read_config_dword(mvi
->pdev
, PCR_DEV_CTRL
, &tmp
);
2683 tmp
&= ~PRD_REQ_MASK
;
2684 tmp
|= PRD_REQ_SIZE
;
2685 pci_write_config_dword(mvi
->pdev
, PCR_DEV_CTRL
, tmp
);
2687 pci_read_config_dword(mvi
->pdev
, PCR_PHY_CTL
, &tmp
);
2690 pci_write_config_dword(mvi
->pdev
, PCR_PHY_CTL
, tmp
);
2692 pci_read_config_dword(mvi
->pdev
, PCR_PHY_CTL2
, &tmp
);
2695 pci_write_config_dword(mvi
->pdev
, PCR_PHY_CTL2
, tmp
);
2700 mw32(PCS
, 0); /*MVS_PCS */
2704 mw32(CMD_LIST_LO
, mvi
->slot_dma
);
2705 mw32(CMD_LIST_HI
, (mvi
->slot_dma
>> 16) >> 16);
2707 mw32(RX_FIS_LO
, mvi
->rx_fis_dma
);
2708 mw32(RX_FIS_HI
, (mvi
->rx_fis_dma
>> 16) >> 16);
2710 mw32(TX_CFG
, MVS_CHIP_SLOT_SZ
);
2711 mw32(TX_LO
, mvi
->tx_dma
);
2712 mw32(TX_HI
, (mvi
->tx_dma
>> 16) >> 16);
2714 mw32(RX_CFG
, MVS_RX_RING_SZ
);
2715 mw32(RX_LO
, mvi
->rx_dma
);
2716 mw32(RX_HI
, (mvi
->rx_dma
>> 16) >> 16);
2718 /* enable auto port detection */
2719 mw32(GBL_PORT_TYPE
, MODE_AUTO_DET_EN
);
2721 /* init and reset phys */
2722 for (i
= 0; i
< mvi
->chip
->n_phy
; i
++) {
2723 u32 lo
= be32_to_cpu(*(u32
*)&mvi
->sas_addr
[4]);
2724 u32 hi
= be32_to_cpu(*(u32
*)&mvi
->sas_addr
[0]);
2726 mvs_detect_porttype(mvi
, i
);
2728 /* set phy local SAS address */
2729 mvs_write_port_cfg_addr(mvi
, i
, PHYR_ADDR_LO
);
2730 mvs_write_port_cfg_data(mvi
, i
, lo
);
2731 mvs_write_port_cfg_addr(mvi
, i
, PHYR_ADDR_HI
);
2732 mvs_write_port_cfg_data(mvi
, i
, hi
);
2735 tmp
= mvs_read_phy_ctl(mvi
, i
);
2737 mvs_write_phy_ctl(mvi
, i
, tmp
);
2742 for (i
= 0; i
< mvi
->chip
->n_phy
; i
++) {
2743 /* clear phy int status */
2744 tmp
= mvs_read_port_irq_stat(mvi
, i
);
2745 tmp
&= ~PHYEV_SIG_FIS
;
2746 mvs_write_port_irq_stat(mvi
, i
, tmp
);
2748 /* set phy int mask */
2749 tmp
= PHYEV_RDY_CH
| PHYEV_BROAD_CH
| PHYEV_UNASSOC_FIS
|
2750 PHYEV_ID_DONE
| PHYEV_DEC_ERR
;
2751 mvs_write_port_irq_mask(mvi
, i
, tmp
);
2754 mvs_update_phyinfo(mvi
, i
, 1);
2755 mvs_enable_xmt(mvi
, i
);
2758 /* FIXME: update wide port bitmaps */
2760 /* little endian for open address and command table, etc. */
2762 * it seems that ( from the spec ) turning on big-endian won't
2763 * do us any good on big-endian machines, need further confirmation
2766 cctl
|= CCTL_ENDIAN_CMD
;
2767 cctl
|= CCTL_ENDIAN_DATA
;
2768 cctl
&= ~CCTL_ENDIAN_OPEN
;
2769 cctl
|= CCTL_ENDIAN_RSP
;
2772 /* reset CMD queue */
2776 /* interrupt coalescing may cause missing HW interrput in some case,
2777 * and the max count is 0x1ff, while our max slot is 0x200,
2778 * it will make count 0.
2781 mw32(INT_COAL
, tmp
);
2784 mw32(INT_COAL_TMOUT
, tmp
);
2786 /* ladies and gentlemen, start your engines */
2788 mw32(TX_CFG
, MVS_CHIP_SLOT_SZ
| TX_EN
);
2789 mw32(RX_CFG
, MVS_RX_RING_SZ
| RX_EN
);
2790 /* enable CMD/CMPL_Q/RESP mode */
2791 mw32(PCS
, PCS_SATA_RETRY
| PCS_FIS_RX_EN
| PCS_CMD_EN
);
2793 /* re-enable interrupts globally */
2794 mvs_hba_interrupt_enable(mvi
);
2796 /* enable completion queue interrupt */
2797 tmp
= (CINT_PORT_MASK
| CINT_DONE
| CINT_MEM
);
2798 mw32(INT_MASK
, tmp
);
2803 static void __devinit
mvs_print_info(struct mvs_info
*mvi
)
2805 struct pci_dev
*pdev
= mvi
->pdev
;
2806 static int printed_version
;
2808 if (!printed_version
++)
2809 dev_printk(KERN_INFO
, &pdev
->dev
, "version " DRV_VERSION
"\n");
2811 dev_printk(KERN_INFO
, &pdev
->dev
, "%u phys, addr %llx\n",
2812 mvi
->chip
->n_phy
, SAS_ADDR(mvi
->sas_addr
));
2815 static int __devinit
mvs_pci_init(struct pci_dev
*pdev
,
2816 const struct pci_device_id
*ent
)
2819 struct mvs_info
*mvi
;
2820 irq_handler_t irq_handler
= mvs_interrupt
;
2822 rc
= pci_enable_device(pdev
);
2826 pci_set_master(pdev
);
2828 rc
= pci_request_regions(pdev
, DRV_NAME
);
2830 goto err_out_disable
;
2832 rc
= pci_go_64(pdev
);
2834 goto err_out_regions
;
2836 mvi
= mvs_alloc(pdev
, ent
);
2839 goto err_out_regions
;
2842 rc
= mvs_hw_init(mvi
);
2846 #ifndef MVS_DISABLE_MSI
2847 if (!pci_enable_msi(pdev
)) {
2849 void __iomem
*regs
= mvi
->regs
;
2850 mvi
->flags
|= MVF_MSI
;
2851 irq_handler
= mvs_msi_interrupt
;
2853 mw32(PCS
, tmp
| PCS_SELF_CLEAR
);
2857 rc
= request_irq(pdev
->irq
, irq_handler
, IRQF_SHARED
, DRV_NAME
, mvi
);
2861 rc
= scsi_add_host(mvi
->shost
, &pdev
->dev
);
2865 rc
= sas_register_ha(&mvi
->sas
);
2869 pci_set_drvdata(pdev
, mvi
);
2871 mvs_print_info(mvi
);
2873 scsi_scan_host(mvi
->shost
);
2878 scsi_remove_host(mvi
->shost
);
2880 free_irq(pdev
->irq
, mvi
);
2882 if (mvi
->flags
|= MVF_MSI
)
2883 pci_disable_msi(pdev
);
2887 pci_release_regions(pdev
);
2889 pci_disable_device(pdev
);
2893 static void __devexit
mvs_pci_remove(struct pci_dev
*pdev
)
2895 struct mvs_info
*mvi
= pci_get_drvdata(pdev
);
2897 pci_set_drvdata(pdev
, NULL
);
2900 sas_unregister_ha(&mvi
->sas
);
2901 mvs_hba_interrupt_disable(mvi
);
2902 sas_remove_host(mvi
->shost
);
2903 scsi_remove_host(mvi
->shost
);
2905 free_irq(pdev
->irq
, mvi
);
2906 if (mvi
->flags
& MVF_MSI
)
2907 pci_disable_msi(pdev
);
2909 pci_release_regions(pdev
);
2911 pci_disable_device(pdev
);
2914 static struct sas_domain_function_template mvs_transport_ops
= {
2915 .lldd_execute_task
= mvs_task_exec
,
2916 .lldd_control_phy
= mvs_phy_control
,
2917 .lldd_abort_task
= mvs_task_abort
,
2918 .lldd_port_formed
= mvs_port_formed
2921 static struct pci_device_id __devinitdata mvs_pci_table
[] = {
2922 { PCI_VDEVICE(MARVELL
, 0x6320), chip_6320
},
2923 { PCI_VDEVICE(MARVELL
, 0x6340), chip_6440
},
2924 { PCI_VDEVICE(MARVELL
, 0x6440), chip_6440
},
2925 { PCI_VDEVICE(MARVELL
, 0x6480), chip_6480
},
2927 { } /* terminate list */
2930 static struct pci_driver mvs_pci_driver
= {
2932 .id_table
= mvs_pci_table
,
2933 .probe
= mvs_pci_init
,
2934 .remove
= __devexit_p(mvs_pci_remove
),
2937 static int __init
mvs_init(void)
2941 mvs_stt
= sas_domain_attach_transport(&mvs_transport_ops
);
2945 rc
= pci_register_driver(&mvs_pci_driver
);
2952 sas_release_transport(mvs_stt
);
2956 static void __exit
mvs_exit(void)
2958 pci_unregister_driver(&mvs_pci_driver
);
2959 sas_release_transport(mvs_stt
);
2962 module_init(mvs_init
);
2963 module_exit(mvs_exit
);
2965 MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
2966 MODULE_DESCRIPTION("Marvell 88SE6440 SAS/SATA controller driver");
2967 MODULE_VERSION(DRV_VERSION
);
2968 MODULE_LICENSE("GPL");
2969 MODULE_DEVICE_TABLE(pci
, mvs_pci_table
);