blob: aaa475a3eda6a8e23ad84c2ad95386199ae17b39 [file] [log] [blame]
Jeff Garzikb5762942007-10-25 20:58:22 -04001/*
Andy Yan20b09c22009-05-08 17:46:40 -04002 * Marvell 88SE64xx/88SE94xx main function
3 *
4 * Copyright 2007 Red Hat, Inc.
5 * Copyright 2008 Marvell. <kewei@marvell.com>
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07006 * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
Andy Yan20b09c22009-05-08 17:46:40 -04007 *
8 * This file is licensed under GPLv2.
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 as
12 * published by the Free Software Foundation; version 2 of the
13 * License.
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 GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24*/
Jeff Garzikb5762942007-10-25 20:58:22 -040025
Jeff Garzikdd4969a2009-05-08 17:44:01 -040026#include "mv_sas.h"
Ke Wei8f261aa2008-02-23 21:15:27 +080027
Jeff Garzikdd4969a2009-05-08 17:44:01 -040028static int mvs_find_tag(struct mvs_info *mvi, struct sas_task *task, u32 *tag)
29{
30 if (task->lldd_task) {
31 struct mvs_slot_info *slot;
Andy Yanf9da3be2009-05-14 20:41:21 -040032 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -040033 *tag = slot->slot_tag;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040034 return 1;
35 }
36 return 0;
37}
Ke Wei8f261aa2008-02-23 21:15:27 +080038
Andy Yan20b09c22009-05-08 17:46:40 -040039void mvs_tag_clear(struct mvs_info *mvi, u32 tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040040{
Andy Yanf9da3be2009-05-14 20:41:21 -040041 void *bitmap = &mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040042 clear_bit(tag, bitmap);
43}
Jeff Garzikb5762942007-10-25 20:58:22 -040044
Andy Yan20b09c22009-05-08 17:46:40 -040045void mvs_tag_free(struct mvs_info *mvi, u32 tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040046{
47 mvs_tag_clear(mvi, tag);
48}
Jeff Garzikb5762942007-10-25 20:58:22 -040049
Andy Yan20b09c22009-05-08 17:46:40 -040050void mvs_tag_set(struct mvs_info *mvi, unsigned int tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040051{
Andy Yanf9da3be2009-05-14 20:41:21 -040052 void *bitmap = &mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040053 set_bit(tag, bitmap);
54}
55
Andy Yan20b09c22009-05-08 17:46:40 -040056inline int mvs_tag_alloc(struct mvs_info *mvi, u32 *tag_out)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040057{
58 unsigned int index, tag;
Andy Yanf9da3be2009-05-14 20:41:21 -040059 void *bitmap = &mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040060
Andy Yan20b09c22009-05-08 17:46:40 -040061 index = find_first_zero_bit(bitmap, mvi->tags_num);
Jeff Garzikdd4969a2009-05-08 17:44:01 -040062 tag = index;
Andy Yan20b09c22009-05-08 17:46:40 -040063 if (tag >= mvi->tags_num)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040064 return -SAS_QUEUE_FULL;
65 mvs_tag_set(mvi, tag);
66 *tag_out = tag;
67 return 0;
68}
69
70void mvs_tag_init(struct mvs_info *mvi)
71{
72 int i;
Andy Yan20b09c22009-05-08 17:46:40 -040073 for (i = 0; i < mvi->tags_num; ++i)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040074 mvs_tag_clear(mvi, i);
75}
Jeff Garzikb5762942007-10-25 20:58:22 -040076
Andy Yan20b09c22009-05-08 17:46:40 -040077void mvs_hexdump(u32 size, u8 *data, u32 baseaddr)
Ke Wei8f261aa2008-02-23 21:15:27 +080078{
79 u32 i;
80 u32 run;
81 u32 offset;
82
83 offset = 0;
84 while (size) {
Andy Yan20b09c22009-05-08 17:46:40 -040085 printk(KERN_DEBUG"%08X : ", baseaddr + offset);
Ke Wei8f261aa2008-02-23 21:15:27 +080086 if (size >= 16)
87 run = 16;
88 else
89 run = size;
90 size -= run;
91 for (i = 0; i < 16; i++) {
92 if (i < run)
Andy Yan20b09c22009-05-08 17:46:40 -040093 printk(KERN_DEBUG"%02X ", (u32)data[i]);
Ke Wei8f261aa2008-02-23 21:15:27 +080094 else
Andy Yan20b09c22009-05-08 17:46:40 -040095 printk(KERN_DEBUG" ");
Ke Wei8f261aa2008-02-23 21:15:27 +080096 }
Andy Yan20b09c22009-05-08 17:46:40 -040097 printk(KERN_DEBUG": ");
Ke Wei8f261aa2008-02-23 21:15:27 +080098 for (i = 0; i < run; i++)
Andy Yan20b09c22009-05-08 17:46:40 -040099 printk(KERN_DEBUG"%c",
100 isalnum(data[i]) ? data[i] : '.');
101 printk(KERN_DEBUG"\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800102 data = &data[16];
103 offset += run;
104 }
Andy Yan20b09c22009-05-08 17:46:40 -0400105 printk(KERN_DEBUG"\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800106}
107
Andy Yan20b09c22009-05-08 17:46:40 -0400108#if (_MV_DUMP > 1)
Ke Wei8f261aa2008-02-23 21:15:27 +0800109static void mvs_hba_sb_dump(struct mvs_info *mvi, u32 tag,
110 enum sas_protocol proto)
111{
Ke Wei8f261aa2008-02-23 21:15:27 +0800112 u32 offset;
Ke Wei8f261aa2008-02-23 21:15:27 +0800113 struct mvs_slot_info *slot = &mvi->slot_info[tag];
114
115 offset = slot->cmd_size + MVS_OAF_SZ +
Andy Yan20b09c22009-05-08 17:46:40 -0400116 MVS_CHIP_DISP->prd_size() * slot->n_elem;
117 dev_printk(KERN_DEBUG, mvi->dev, "+---->Status buffer[%d] :\n",
Ke Wei8f261aa2008-02-23 21:15:27 +0800118 tag);
119 mvs_hexdump(32, (u8 *) slot->response,
120 (u32) slot->buf_dma + offset);
Ke Wei8f261aa2008-02-23 21:15:27 +0800121}
Ke Weiee1f1c22008-03-27 14:53:47 +0800122#endif
Ke Wei8f261aa2008-02-23 21:15:27 +0800123
124static void mvs_hba_memory_dump(struct mvs_info *mvi, u32 tag,
125 enum sas_protocol proto)
126{
Andy Yan20b09c22009-05-08 17:46:40 -0400127#if (_MV_DUMP > 1)
Ke Weiee1f1c22008-03-27 14:53:47 +0800128 u32 sz, w_ptr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800129 u64 addr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800130 struct mvs_slot_info *slot = &mvi->slot_info[tag];
131
132 /*Delivery Queue */
Andy Yan20b09c22009-05-08 17:46:40 -0400133 sz = MVS_CHIP_SLOT_SZ;
Ke Weiee1f1c22008-03-27 14:53:47 +0800134 w_ptr = slot->tx;
Andy Yan20b09c22009-05-08 17:46:40 -0400135 addr = mvi->tx_dma;
136 dev_printk(KERN_DEBUG, mvi->dev,
Ke Weiee1f1c22008-03-27 14:53:47 +0800137 "Delivery Queue Size=%04d , WRT_PTR=%04X\n", sz, w_ptr);
Andy Yan20b09c22009-05-08 17:46:40 -0400138 dev_printk(KERN_DEBUG, mvi->dev,
Ke Wei8f261aa2008-02-23 21:15:27 +0800139 "Delivery Queue Base Address=0x%llX (PA)"
140 "(tx_dma=0x%llX), Entry=%04d\n",
Andy Yan20b09c22009-05-08 17:46:40 -0400141 addr, (unsigned long long)mvi->tx_dma, w_ptr);
Ke Wei8f261aa2008-02-23 21:15:27 +0800142 mvs_hexdump(sizeof(u32), (u8 *)(&mvi->tx[mvi->tx_prod]),
143 (u32) mvi->tx_dma + sizeof(u32) * w_ptr);
144 /*Command List */
Ke Weiee1f1c22008-03-27 14:53:47 +0800145 addr = mvi->slot_dma;
Andy Yan20b09c22009-05-08 17:46:40 -0400146 dev_printk(KERN_DEBUG, mvi->dev,
Ke Wei8f261aa2008-02-23 21:15:27 +0800147 "Command List Base Address=0x%llX (PA)"
148 "(slot_dma=0x%llX), Header=%03d\n",
Andy Yan20b09c22009-05-08 17:46:40 -0400149 addr, (unsigned long long)slot->buf_dma, tag);
150 dev_printk(KERN_DEBUG, mvi->dev, "Command Header[%03d]:\n", tag);
Ke Wei8f261aa2008-02-23 21:15:27 +0800151 /*mvs_cmd_hdr */
152 mvs_hexdump(sizeof(struct mvs_cmd_hdr), (u8 *)(&mvi->slot[tag]),
153 (u32) mvi->slot_dma + tag * sizeof(struct mvs_cmd_hdr));
154 /*1.command table area */
Andy Yan20b09c22009-05-08 17:46:40 -0400155 dev_printk(KERN_DEBUG, mvi->dev, "+---->Command Table :\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800156 mvs_hexdump(slot->cmd_size, (u8 *) slot->buf, (u32) slot->buf_dma);
157 /*2.open address frame area */
Andy Yan20b09c22009-05-08 17:46:40 -0400158 dev_printk(KERN_DEBUG, mvi->dev, "+---->Open Address Frame :\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800159 mvs_hexdump(MVS_OAF_SZ, (u8 *) slot->buf + slot->cmd_size,
160 (u32) slot->buf_dma + slot->cmd_size);
161 /*3.status buffer */
162 mvs_hba_sb_dump(mvi, tag, proto);
163 /*4.PRD table */
Andy Yan20b09c22009-05-08 17:46:40 -0400164 dev_printk(KERN_DEBUG, mvi->dev, "+---->PRD table :\n");
165 mvs_hexdump(MVS_CHIP_DISP->prd_size() * slot->n_elem,
Ke Wei8f261aa2008-02-23 21:15:27 +0800166 (u8 *) slot->buf + slot->cmd_size + MVS_OAF_SZ,
167 (u32) slot->buf_dma + slot->cmd_size + MVS_OAF_SZ);
168#endif
169}
170
171static void mvs_hba_cq_dump(struct mvs_info *mvi)
172{
Ke Weiee1f1c22008-03-27 14:53:47 +0800173#if (_MV_DUMP > 2)
Ke Wei8f261aa2008-02-23 21:15:27 +0800174 u64 addr;
175 void __iomem *regs = mvi->regs;
Ke Wei8f261aa2008-02-23 21:15:27 +0800176 u32 entry = mvi->rx_cons + 1;
177 u32 rx_desc = le32_to_cpu(mvi->rx[entry]);
178
179 /*Completion Queue */
180 addr = mr32(RX_HI) << 16 << 16 | mr32(RX_LO);
Andy Yan20b09c22009-05-08 17:46:40 -0400181 dev_printk(KERN_DEBUG, mvi->dev, "Completion Task = 0x%p\n",
Ke Weiee1f1c22008-03-27 14:53:47 +0800182 mvi->slot_info[rx_desc & RXQ_SLOT_MASK].task);
Andy Yan20b09c22009-05-08 17:46:40 -0400183 dev_printk(KERN_DEBUG, mvi->dev,
Ke Wei8f261aa2008-02-23 21:15:27 +0800184 "Completion List Base Address=0x%llX (PA), "
185 "CQ_Entry=%04d, CQ_WP=0x%08X\n",
186 addr, entry - 1, mvi->rx[0]);
187 mvs_hexdump(sizeof(u32), (u8 *)(&rx_desc),
188 mvi->rx_dma + sizeof(u32) * entry);
189#endif
190}
191
Andy Yan20b09c22009-05-08 17:46:40 -0400192void mvs_get_sas_addr(void *buf, u32 buflen)
Ke Wei8f261aa2008-02-23 21:15:27 +0800193{
Andy Yan20b09c22009-05-08 17:46:40 -0400194 /*memcpy(buf, "\x50\x05\x04\x30\x11\xab\x64\x40", 8);*/
195}
Ke Wei8f261aa2008-02-23 21:15:27 +0800196
Andy Yan20b09c22009-05-08 17:46:40 -0400197struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
198{
199 unsigned long i = 0, j = 0, hi = 0;
200 struct sas_ha_struct *sha = dev->port->ha;
201 struct mvs_info *mvi = NULL;
202 struct asd_sas_phy *phy;
Ke Wei8f261aa2008-02-23 21:15:27 +0800203
Andy Yan20b09c22009-05-08 17:46:40 -0400204 while (sha->sas_port[i]) {
205 if (sha->sas_port[i] == dev->port) {
206 phy = container_of(sha->sas_port[i]->phy_list.next,
207 struct asd_sas_phy, port_phy_el);
208 j = 0;
209 while (sha->sas_phy[j]) {
210 if (sha->sas_phy[j] == phy)
211 break;
212 j++;
Jeff Garzikb5762942007-10-25 20:58:22 -0400213 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400214 break;
Jeff Garzikb5762942007-10-25 20:58:22 -0400215 }
Andy Yan20b09c22009-05-08 17:46:40 -0400216 i++;
217 }
218 hi = j/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
219 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
220
221 return mvi;
222
223}
224
225/* FIXME */
226int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
227{
228 unsigned long i = 0, j = 0, n = 0, num = 0;
Andy Yan9870d9a2009-05-11 22:19:25 +0800229 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
230 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -0400231 struct sas_ha_struct *sha = dev->port->ha;
232
233 while (sha->sas_port[i]) {
234 if (sha->sas_port[i] == dev->port) {
235 struct asd_sas_phy *phy;
236 list_for_each_entry(phy,
237 &sha->sas_port[i]->phy_list, port_phy_el) {
238 j = 0;
239 while (sha->sas_phy[j]) {
240 if (sha->sas_phy[j] == phy)
241 break;
242 j++;
243 }
244 phyno[n] = (j >= mvi->chip->n_phy) ?
245 (j - mvi->chip->n_phy) : j;
246 num++;
247 n++;
248 }
249 break;
250 }
251 i++;
252 }
253 return num;
254}
255
Xiangliang Yu534ff102011-05-24 22:26:50 +0800256struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
257 u8 reg_set)
258{
259 u32 dev_no;
260 for (dev_no = 0; dev_no < MVS_MAX_DEVICES; dev_no++) {
261 if (mvi->devices[dev_no].taskfileset == MVS_ID_NOT_MAPPED)
262 continue;
263
264 if (mvi->devices[dev_no].taskfileset == reg_set)
265 return &mvi->devices[dev_no];
266 }
267 return NULL;
268}
269
Andy Yan20b09c22009-05-08 17:46:40 -0400270static inline void mvs_free_reg_set(struct mvs_info *mvi,
271 struct mvs_device *dev)
272{
273 if (!dev) {
274 mv_printk("device has been free.\n");
275 return;
276 }
Andy Yan20b09c22009-05-08 17:46:40 -0400277 if (dev->taskfileset == MVS_ID_NOT_MAPPED)
278 return;
279 MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
280}
281
282static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
283 struct mvs_device *dev)
284{
285 if (dev->taskfileset != MVS_ID_NOT_MAPPED)
286 return 0;
287 return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
288}
289
290void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
291{
292 u32 no;
293 for_each_phy(phy_mask, phy_mask, no) {
294 if (!(phy_mask & 1))
295 continue;
296 MVS_CHIP_DISP->phy_reset(mvi, no, hard);
297 }
298}
299
300/* FIXME: locking? */
301int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
302 void *funcdata)
303{
304 int rc = 0, phy_id = sas_phy->id;
305 u32 tmp, i = 0, hi;
306 struct sas_ha_struct *sha = sas_phy->ha;
307 struct mvs_info *mvi = NULL;
308
309 while (sha->sas_phy[i]) {
310 if (sha->sas_phy[i] == sas_phy)
311 break;
312 i++;
313 }
314 hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
315 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
316
317 switch (func) {
318 case PHY_FUNC_SET_LINK_RATE:
319 MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
320 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400321
322 case PHY_FUNC_HARD_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400323 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400324 if (tmp & PHY_RST_HARD)
325 break;
Andy Yan20b09c22009-05-08 17:46:40 -0400326 MVS_CHIP_DISP->phy_reset(mvi, phy_id, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400327 break;
328
329 case PHY_FUNC_LINK_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400330 MVS_CHIP_DISP->phy_enable(mvi, phy_id);
331 MVS_CHIP_DISP->phy_reset(mvi, phy_id, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400332 break;
333
334 case PHY_FUNC_DISABLE:
Andy Yan20b09c22009-05-08 17:46:40 -0400335 MVS_CHIP_DISP->phy_disable(mvi, phy_id);
336 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400337 case PHY_FUNC_RELEASE_SPINUP_HOLD:
338 default:
339 rc = -EOPNOTSUPP;
Jeff Garzikb5762942007-10-25 20:58:22 -0400340 }
Andy Yan20b09c22009-05-08 17:46:40 -0400341 msleep(200);
Jeff Garzikb5762942007-10-25 20:58:22 -0400342 return rc;
343}
344
Andy Yan20b09c22009-05-08 17:46:40 -0400345void __devinit mvs_set_sas_addr(struct mvs_info *mvi, int port_id,
346 u32 off_lo, u32 off_hi, u64 sas_addr)
347{
348 u32 lo = (u32)sas_addr;
349 u32 hi = (u32)(sas_addr>>32);
350
351 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_lo);
352 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, lo);
353 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_hi);
354 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, hi);
355}
356
Ke Wei8f261aa2008-02-23 21:15:27 +0800357static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
358{
359 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -0400360 struct asd_sas_phy *sas_phy = &phy->sas_phy;
361 struct sas_ha_struct *sas_ha;
Ke Wei8f261aa2008-02-23 21:15:27 +0800362 if (!phy->phy_attached)
363 return;
364
Andy Yan20b09c22009-05-08 17:46:40 -0400365 if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
366 && phy->phy_type & PORT_TYPE_SAS) {
367 return;
368 }
369
370 sas_ha = mvi->sas;
371 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
372
Ke Weiee1f1c22008-03-27 14:53:47 +0800373 if (sas_phy->phy) {
374 struct sas_phy *sphy = sas_phy->phy;
375
376 sphy->negotiated_linkrate = sas_phy->linkrate;
377 sphy->minimum_linkrate = phy->minimum_linkrate;
378 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
379 sphy->maximum_linkrate = phy->maximum_linkrate;
Andy Yan20b09c22009-05-08 17:46:40 -0400380 sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
Ke Weiee1f1c22008-03-27 14:53:47 +0800381 }
382
Ke Wei8f261aa2008-02-23 21:15:27 +0800383 if (phy->phy_type & PORT_TYPE_SAS) {
384 struct sas_identify_frame *id;
385
386 id = (struct sas_identify_frame *)phy->frame_rcvd;
387 id->dev_type = phy->identify.device_type;
388 id->initiator_bits = SAS_PROTOCOL_ALL;
389 id->target_bits = phy->identify.target_port_protocols;
390 } else if (phy->phy_type & PORT_TYPE_SATA) {
Andy Yan20b09c22009-05-08 17:46:40 -0400391 /*Nothing*/
Ke Wei8f261aa2008-02-23 21:15:27 +0800392 }
Andy Yan20b09c22009-05-08 17:46:40 -0400393 mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
394
395 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
396
397 mvi->sas->notify_port_event(sas_phy,
Ke Wei8f261aa2008-02-23 21:15:27 +0800398 PORTE_BYTES_DMAED);
399}
400
Andy Yan20b09c22009-05-08 17:46:40 -0400401int mvs_slave_alloc(struct scsi_device *scsi_dev)
402{
403 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
404 if (dev_is_sata(dev)) {
405 /* We don't need to rescan targets
406 * if REPORT_LUNS request is failed
407 */
408 if (scsi_dev->lun > 0)
409 return -ENXIO;
410 scsi_dev->tagged_supported = 1;
411 }
412
413 return sas_slave_alloc(scsi_dev);
414}
415
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400416int mvs_slave_configure(struct scsi_device *sdev)
Ke Wei8f261aa2008-02-23 21:15:27 +0800417{
Ke Weiee1f1c22008-03-27 14:53:47 +0800418 struct domain_device *dev = sdev_to_domain_dev(sdev);
419 int ret = sas_slave_configure(sdev);
Ke Wei8f261aa2008-02-23 21:15:27 +0800420
Ke Weiee1f1c22008-03-27 14:53:47 +0800421 if (ret)
422 return ret;
Ke Weiee1f1c22008-03-27 14:53:47 +0800423 if (dev_is_sata(dev)) {
Andy Yan20b09c22009-05-08 17:46:40 -0400424 /* may set PIO mode */
425 #if MV_DISABLE_NCQ
426 struct ata_port *ap = dev->sata_dev.ap;
427 struct ata_device *adev = ap->link.device;
428 adev->flags |= ATA_DFLAG_NCQ_OFF;
Ke Weiee1f1c22008-03-27 14:53:47 +0800429 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, 1);
Andy Yan20b09c22009-05-08 17:46:40 -0400430 #endif
Ke Weiee1f1c22008-03-27 14:53:47 +0800431 }
432 return 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400433}
434
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400435void mvs_scan_start(struct Scsi_Host *shost)
Jeff Garzikb5762942007-10-25 20:58:22 -0400436{
Andy Yan20b09c22009-05-08 17:46:40 -0400437 int i, j;
438 unsigned short core_nr;
439 struct mvs_info *mvi;
440 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
Jeff Garzikb5762942007-10-25 20:58:22 -0400441
Andy Yan20b09c22009-05-08 17:46:40 -0400442 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
443
444 for (j = 0; j < core_nr; j++) {
445 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
446 for (i = 0; i < mvi->chip->n_phy; ++i)
447 mvs_bytes_dmaed(mvi, i);
Jeff Garzikb5762942007-10-25 20:58:22 -0400448 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400449}
450
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400451int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
Jeff Garzikb5762942007-10-25 20:58:22 -0400452{
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400453 /* give the phy enabling interrupt event time to come in (1s
454 * is empirically about all it takes) */
455 if (time < HZ)
Ke Wei8f261aa2008-02-23 21:15:27 +0800456 return 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400457 /* Wait for discovery to finish */
458 scsi_flush_work(shost);
459 return 1;
Jeff Garzikb5762942007-10-25 20:58:22 -0400460}
461
Ke Wei8f261aa2008-02-23 21:15:27 +0800462static int mvs_task_prep_smp(struct mvs_info *mvi,
463 struct mvs_task_exec_info *tei)
Jeff Garzikb5762942007-10-25 20:58:22 -0400464{
Ke Wei8f261aa2008-02-23 21:15:27 +0800465 int elem, rc, i;
466 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400467 struct mvs_cmd_hdr *hdr = tei->hdr;
Andy Yan20b09c22009-05-08 17:46:40 -0400468 struct domain_device *dev = task->dev;
469 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400470 struct scatterlist *sg_req, *sg_resp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800471 u32 req_len, resp_len, tag = tei->tag;
472 void *buf_tmp;
473 u8 *buf_oaf;
474 dma_addr_t buf_tmp_dma;
Andy Yan20b09c22009-05-08 17:46:40 -0400475 void *buf_prd;
Ke Wei8f261aa2008-02-23 21:15:27 +0800476 struct mvs_slot_info *slot = &mvi->slot_info[tag];
Ke Wei8f261aa2008-02-23 21:15:27 +0800477 u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
478#if _MV_DUMP
479 u8 *buf_cmd;
480 void *from;
481#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400482 /*
483 * DMA-map SMP request, response buffers
484 */
Ke Wei8f261aa2008-02-23 21:15:27 +0800485 sg_req = &task->smp_task.smp_req;
Andy Yan20b09c22009-05-08 17:46:40 -0400486 elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400487 if (!elem)
488 return -ENOMEM;
489 req_len = sg_dma_len(sg_req);
490
Ke Wei8f261aa2008-02-23 21:15:27 +0800491 sg_resp = &task->smp_task.smp_resp;
Andy Yan20b09c22009-05-08 17:46:40 -0400492 elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400493 if (!elem) {
494 rc = -ENOMEM;
495 goto err_out;
496 }
Andy Yan20b09c22009-05-08 17:46:40 -0400497 resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400498
499 /* must be in dwords */
500 if ((req_len & 0x3) || (resp_len & 0x3)) {
501 rc = -EINVAL;
502 goto err_out_2;
503 }
504
505 /*
Ke Wei8f261aa2008-02-23 21:15:27 +0800506 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
Jeff Garzikb5762942007-10-25 20:58:22 -0400507 */
508
Andy Yan20b09c22009-05-08 17:46:40 -0400509 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800510 buf_tmp = slot->buf;
511 buf_tmp_dma = slot->buf_dma;
Jeff Garzikb5762942007-10-25 20:58:22 -0400512
Ke Wei8f261aa2008-02-23 21:15:27 +0800513#if _MV_DUMP
514 buf_cmd = buf_tmp;
515 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
516 buf_tmp += req_len;
517 buf_tmp_dma += req_len;
518 slot->cmd_size = req_len;
519#else
520 hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
521#endif
522
523 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
524 buf_oaf = buf_tmp;
525 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
526
527 buf_tmp += MVS_OAF_SZ;
528 buf_tmp_dma += MVS_OAF_SZ;
529
Andy Yan20b09c22009-05-08 17:46:40 -0400530 /* region 3: PRD table *********************************** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800531 buf_prd = buf_tmp;
532 if (tei->n_elem)
533 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
534 else
535 hdr->prd_tbl = 0;
536
Andy Yan20b09c22009-05-08 17:46:40 -0400537 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Ke Wei8f261aa2008-02-23 21:15:27 +0800538 buf_tmp += i;
539 buf_tmp_dma += i;
540
541 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
542 slot->response = buf_tmp;
543 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400544 if (mvi->flags & MVF_FLAG_SOC)
545 hdr->reserved[0] = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800546
547 /*
548 * Fill in TX ring and command slot header
549 */
550 slot->tx = mvi->tx_prod;
551 mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
552 TXQ_MODE_I | tag |
553 (sas_port->phy_mask << TXQ_PHY_SHIFT));
554
555 hdr->flags |= flags;
556 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
Jeff Garzikb5762942007-10-25 20:58:22 -0400557 hdr->tags = cpu_to_le32(tag);
558 hdr->data_len = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400559
Ke Wei8f261aa2008-02-23 21:15:27 +0800560 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400561 /* initiator, SMP, ftype 1h */
562 buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
563 buf_oaf[1] = dev->linkrate & 0xf;
Ke Wei8f261aa2008-02-23 21:15:27 +0800564 *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
Andy Yan20b09c22009-05-08 17:46:40 -0400565 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Ke Wei8f261aa2008-02-23 21:15:27 +0800566
567 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400568 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Ke Wei8f261aa2008-02-23 21:15:27 +0800569
570#if _MV_DUMP
571 /* copy cmd table */
572 from = kmap_atomic(sg_page(sg_req), KM_IRQ0);
573 memcpy(buf_cmd, from + sg_req->offset, req_len);
574 kunmap_atomic(from, KM_IRQ0);
575#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400576 return 0;
577
578err_out_2:
Andy Yan20b09c22009-05-08 17:46:40 -0400579 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400580 PCI_DMA_FROMDEVICE);
581err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400582 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400583 PCI_DMA_TODEVICE);
584 return rc;
585}
586
Ke Wei4e52fc02008-03-27 14:54:50 +0800587static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
Ke Wei8f261aa2008-02-23 21:15:27 +0800588{
Ke Wei8f261aa2008-02-23 21:15:27 +0800589 struct ata_queued_cmd *qc = task->uldd_task;
590
Ke Wei4e52fc02008-03-27 14:54:50 +0800591 if (qc) {
592 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
593 qc->tf.command == ATA_CMD_FPDMA_READ) {
594 *tag = qc->tag;
595 return 1;
596 }
597 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800598
Ke Wei4e52fc02008-03-27 14:54:50 +0800599 return 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800600}
601
Jeff Garzikb5762942007-10-25 20:58:22 -0400602static int mvs_task_prep_ata(struct mvs_info *mvi,
603 struct mvs_task_exec_info *tei)
604{
605 struct sas_task *task = tei->task;
606 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400607 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400608 struct mvs_cmd_hdr *hdr = tei->hdr;
609 struct asd_sas_port *sas_port = dev->port;
Ke Wei8f261aa2008-02-23 21:15:27 +0800610 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400611 void *buf_prd;
612 u32 tag = tei->tag, hdr_tag;
613 u32 flags, del_q;
Jeff Garzikb5762942007-10-25 20:58:22 -0400614 void *buf_tmp;
615 u8 *buf_cmd, *buf_oaf;
616 dma_addr_t buf_tmp_dma;
Ke Wei8f261aa2008-02-23 21:15:27 +0800617 u32 i, req_len, resp_len;
618 const u32 max_resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400619
Andy Yan20b09c22009-05-08 17:46:40 -0400620 if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
621 mv_dprintk("Have not enough regiset for dev %d.\n",
622 mvi_dev->device_id);
Ke Wei8f261aa2008-02-23 21:15:27 +0800623 return -EBUSY;
Andy Yan20b09c22009-05-08 17:46:40 -0400624 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800625 slot = &mvi->slot_info[tag];
626 slot->tx = mvi->tx_prod;
Andy Yan20b09c22009-05-08 17:46:40 -0400627 del_q = TXQ_MODE_I | tag |
628 (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
629 (sas_port->phy_mask << TXQ_PHY_SHIFT) |
630 (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
631 mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
Jeff Garzikb5762942007-10-25 20:58:22 -0400632
Andy Yan20b09c22009-05-08 17:46:40 -0400633#ifndef DISABLE_HOTPLUG_DMA_FIX
634 if (task->data_dir == DMA_FROM_DEVICE)
635 flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
636 else
637 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
638#else
639 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
640#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400641 if (task->ata_task.use_ncq)
642 flags |= MCH_FPDMA;
Ke Wei8f261aa2008-02-23 21:15:27 +0800643 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
644 if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
645 flags |= MCH_ATAPI;
646 }
647
Jeff Garzikb5762942007-10-25 20:58:22 -0400648 /* FIXME: fill in port multiplier number */
649
650 hdr->flags = cpu_to_le32(flags);
Ke Wei8f261aa2008-02-23 21:15:27 +0800651
652 /* FIXME: the low order order 5 bits for the TAG if enable NCQ */
Andy Yan20b09c22009-05-08 17:46:40 -0400653 if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
654 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
Ke Wei4e52fc02008-03-27 14:54:50 +0800655 else
Andy Yan20b09c22009-05-08 17:46:40 -0400656 hdr_tag = tag;
657
658 hdr->tags = cpu_to_le32(hdr_tag);
659
Jeff Garzikb5762942007-10-25 20:58:22 -0400660 hdr->data_len = cpu_to_le32(task->total_xfer_len);
661
662 /*
663 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
664 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400665
Ke Wei8f261aa2008-02-23 21:15:27 +0800666 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
667 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400668 buf_tmp_dma = slot->buf_dma;
669
670 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
671
672 buf_tmp += MVS_ATA_CMD_SZ;
673 buf_tmp_dma += MVS_ATA_CMD_SZ;
Ke Wei8f261aa2008-02-23 21:15:27 +0800674#if _MV_DUMP
675 slot->cmd_size = MVS_ATA_CMD_SZ;
676#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400677
Ke Wei8f261aa2008-02-23 21:15:27 +0800678 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400679 /* used for STP. unused for SATA? */
680 buf_oaf = buf_tmp;
681 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
682
683 buf_tmp += MVS_OAF_SZ;
684 buf_tmp_dma += MVS_OAF_SZ;
685
Ke Wei8f261aa2008-02-23 21:15:27 +0800686 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400687 buf_prd = buf_tmp;
Andy Yan20b09c22009-05-08 17:46:40 -0400688
Ke Wei8f261aa2008-02-23 21:15:27 +0800689 if (tei->n_elem)
690 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
691 else
692 hdr->prd_tbl = 0;
Andy Yan20b09c22009-05-08 17:46:40 -0400693 i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
Jeff Garzikb5762942007-10-25 20:58:22 -0400694
Jeff Garzikb5762942007-10-25 20:58:22 -0400695 buf_tmp += i;
696 buf_tmp_dma += i;
697
Ke Wei8f261aa2008-02-23 21:15:27 +0800698 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400699 /* FIXME: probably unused, for SATA. kept here just in case
700 * we get a STP/SATA error information record
701 */
702 slot->response = buf_tmp;
703 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400704 if (mvi->flags & MVF_FLAG_SOC)
705 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400706
Ke Wei8f261aa2008-02-23 21:15:27 +0800707 req_len = sizeof(struct host_to_dev_fis);
Jeff Garzikb5762942007-10-25 20:58:22 -0400708 resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800709 sizeof(struct mvs_err_info) - i;
Jeff Garzikb5762942007-10-25 20:58:22 -0400710
711 /* request, response lengths */
Ke Wei8f261aa2008-02-23 21:15:27 +0800712 resp_len = min(resp_len, max_resp_len);
Jeff Garzikb5762942007-10-25 20:58:22 -0400713 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
714
Andy Yan20b09c22009-05-08 17:46:40 -0400715 if (likely(!task->ata_task.device_control_reg_update))
716 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
Jeff Garzikb5762942007-10-25 20:58:22 -0400717 /* fill in command FIS and ATAPI CDB */
Ke Wei8f261aa2008-02-23 21:15:27 +0800718 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
719 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
720 memcpy(buf_cmd + STP_ATAPI_CMD,
721 task->ata_task.atapi_packet, 16);
722
723 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400724 /* initiator, STP, ftype 1h */
725 buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
726 buf_oaf[1] = dev->linkrate & 0xf;
727 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
728 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400729
730 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400731 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
732#ifndef DISABLE_HOTPLUG_DMA_FIX
733 if (task->data_dir == DMA_FROM_DEVICE)
734 MVS_CHIP_DISP->dma_fix(mvi->bulk_buffer_dma,
735 TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
736#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400737 return 0;
738}
739
740static int mvs_task_prep_ssp(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -0400741 struct mvs_task_exec_info *tei, int is_tmf,
742 struct mvs_tmf_task *tmf)
Jeff Garzikb5762942007-10-25 20:58:22 -0400743{
744 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400745 struct mvs_cmd_hdr *hdr = tei->hdr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800746 struct mvs_port *port = tei->port;
Andy Yan20b09c22009-05-08 17:46:40 -0400747 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400748 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -0400749 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400750 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400751 void *buf_prd;
Jeff Garzikb5762942007-10-25 20:58:22 -0400752 struct ssp_frame_hdr *ssp_hdr;
753 void *buf_tmp;
754 u8 *buf_cmd, *buf_oaf, fburst = 0;
755 dma_addr_t buf_tmp_dma;
756 u32 flags;
Ke Wei8f261aa2008-02-23 21:15:27 +0800757 u32 resp_len, req_len, i, tag = tei->tag;
758 const u32 max_resp_len = SB_RFB_MAX;
Andy Yan20b09c22009-05-08 17:46:40 -0400759 u32 phy_mask;
Jeff Garzikb5762942007-10-25 20:58:22 -0400760
761 slot = &mvi->slot_info[tag];
762
Andy Yan20b09c22009-05-08 17:46:40 -0400763 phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
764 sas_port->phy_mask) & TXQ_PHY_MASK;
765
Ke Wei8f261aa2008-02-23 21:15:27 +0800766 slot->tx = mvi->tx_prod;
767 mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
768 (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
Ke Wei4e52fc02008-03-27 14:54:50 +0800769 (phy_mask << TXQ_PHY_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400770
771 flags = MCH_RETRY;
772 if (task->ssp_task.enable_first_burst) {
773 flags |= MCH_FBURST;
774 fburst = (1 << 7);
775 }
Andy Yan2b288132009-05-11 20:01:55 +0800776 if (is_tmf)
777 flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
Andy Yan2b288132009-05-11 20:01:55 +0800778 hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400779 hdr->tags = cpu_to_le32(tag);
780 hdr->data_len = cpu_to_le32(task->total_xfer_len);
781
782 /*
783 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
784 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400785
Ke Wei8f261aa2008-02-23 21:15:27 +0800786 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
787 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400788 buf_tmp_dma = slot->buf_dma;
789
790 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
791
792 buf_tmp += MVS_SSP_CMD_SZ;
793 buf_tmp_dma += MVS_SSP_CMD_SZ;
Ke Wei8f261aa2008-02-23 21:15:27 +0800794#if _MV_DUMP
795 slot->cmd_size = MVS_SSP_CMD_SZ;
796#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400797
Ke Wei8f261aa2008-02-23 21:15:27 +0800798 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400799 buf_oaf = buf_tmp;
800 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
801
802 buf_tmp += MVS_OAF_SZ;
803 buf_tmp_dma += MVS_OAF_SZ;
804
Ke Wei8f261aa2008-02-23 21:15:27 +0800805 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400806 buf_prd = buf_tmp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800807 if (tei->n_elem)
808 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
809 else
810 hdr->prd_tbl = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400811
Andy Yan20b09c22009-05-08 17:46:40 -0400812 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Jeff Garzikb5762942007-10-25 20:58:22 -0400813 buf_tmp += i;
814 buf_tmp_dma += i;
815
Ke Wei8f261aa2008-02-23 21:15:27 +0800816 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400817 slot->response = buf_tmp;
818 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400819 if (mvi->flags & MVF_FLAG_SOC)
820 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400821
Jeff Garzikb5762942007-10-25 20:58:22 -0400822 resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800823 sizeof(struct mvs_err_info) - i;
824 resp_len = min(resp_len, max_resp_len);
825
826 req_len = sizeof(struct ssp_frame_hdr) + 28;
Jeff Garzikb5762942007-10-25 20:58:22 -0400827
828 /* request, response lengths */
829 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
830
831 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400832 /* initiator, SSP, ftype 1h */
833 buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
834 buf_oaf[1] = dev->linkrate & 0xf;
835 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
836 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400837
Ke Wei8f261aa2008-02-23 21:15:27 +0800838 /* fill in SSP frame header (Command Table.SSP frame header) */
839 ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
Andy Yan20b09c22009-05-08 17:46:40 -0400840
841 if (is_tmf)
842 ssp_hdr->frame_type = SSP_TASK;
843 else
844 ssp_hdr->frame_type = SSP_COMMAND;
845
846 memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
Jeff Garzikb5762942007-10-25 20:58:22 -0400847 HASHED_SAS_ADDR_SIZE);
848 memcpy(ssp_hdr->hashed_src_addr,
Andy Yan20b09c22009-05-08 17:46:40 -0400849 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400850 ssp_hdr->tag = cpu_to_be16(tag);
851
Andy Yan20b09c22009-05-08 17:46:40 -0400852 /* fill in IU for TASK and Command Frame */
Jeff Garzikb5762942007-10-25 20:58:22 -0400853 buf_cmd += sizeof(*ssp_hdr);
854 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
Jeff Garzikb5762942007-10-25 20:58:22 -0400855
Andy Yan20b09c22009-05-08 17:46:40 -0400856 if (ssp_hdr->frame_type != SSP_TASK) {
857 buf_cmd[9] = fburst | task->ssp_task.task_attr |
858 (task->ssp_task.task_prio << 3);
859 memcpy(buf_cmd + 12, &task->ssp_task.cdb, 16);
860 } else{
861 buf_cmd[10] = tmf->tmf;
862 switch (tmf->tmf) {
863 case TMF_ABORT_TASK:
864 case TMF_QUERY_TASK:
865 buf_cmd[12] =
866 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
867 buf_cmd[13] =
868 tmf->tag_of_task_to_be_managed & 0xff;
869 break;
870 default:
871 break;
872 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400873 }
Andy Yan20b09c22009-05-08 17:46:40 -0400874 /* fill in PRD (scatter/gather) table, if any */
875 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Jeff Garzikb5762942007-10-25 20:58:22 -0400876 return 0;
877}
878
Andy Yan20b09c22009-05-08 17:46:40 -0400879#define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == NO_DEVICE)))
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700880static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
881 struct mvs_tmf_task *tmf, int *pass)
Jeff Garzikb5762942007-10-25 20:58:22 -0400882{
Ke Wei8f261aa2008-02-23 21:15:27 +0800883 struct domain_device *dev = task->dev;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700884 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400885 struct mvs_task_exec_info tei;
Ke Wei4e52fc02008-03-27 14:54:50 +0800886 struct mvs_slot_info *slot;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700887 u32 tag = 0xdeadbeef, n_elem = 0;
888 int rc = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400889
Andy Yan20b09c22009-05-08 17:46:40 -0400890 if (!dev->port) {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700891 struct task_status_struct *tsm = &task->task_status;
Andy Yan20b09c22009-05-08 17:46:40 -0400892
893 tsm->resp = SAS_TASK_UNDELIVERED;
894 tsm->stat = SAS_PHY_DOWN;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700895 /*
896 * libsas will use dev->port, should
897 * not call task_done for sata
898 */
Srinivas9dc9fd92010-02-15 00:00:00 -0600899 if (dev->dev_type != SATA_DEV)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700900 task->task_done(task);
901 return rc;
Andy Yan20b09c22009-05-08 17:46:40 -0400902 }
903
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700904 if (DEV_IS_GONE(mvi_dev)) {
905 if (mvi_dev)
906 mv_dprintk("device %d not ready.\n",
907 mvi_dev->device_id);
908 else
909 mv_dprintk("device %016llx not ready.\n",
910 SAS_ADDR(dev->sas_addr));
Andy Yan20b09c22009-05-08 17:46:40 -0400911
912 rc = SAS_PHY_DOWN;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700913 return rc;
914 }
915 tei.port = dev->port->lldd_port;
916 if (tei.port && !tei.port->port_attached && !tmf) {
917 if (sas_protocol_ata(task->task_proto)) {
918 struct task_status_struct *ts = &task->task_status;
919 mv_dprintk("SATA/STP port %d does not attach"
920 "device.\n", dev->port->id);
921 ts->resp = SAS_TASK_COMPLETE;
922 ts->stat = SAS_PHY_DOWN;
Andy Yan20b09c22009-05-08 17:46:40 -0400923
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700924 task->task_done(task);
Jeff Garzikb5762942007-10-25 20:58:22 -0400925
Ke Wei8f261aa2008-02-23 21:15:27 +0800926 } else {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700927 struct task_status_struct *ts = &task->task_status;
928 mv_dprintk("SAS port %d does not attach"
929 "device.\n", dev->port->id);
930 ts->resp = SAS_TASK_UNDELIVERED;
931 ts->stat = SAS_PHY_DOWN;
932 task->task_done(task);
Ke Wei8f261aa2008-02-23 21:15:27 +0800933 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700934 return rc;
935 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400936
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700937 if (!sas_protocol_ata(task->task_proto)) {
938 if (task->num_scatter) {
939 n_elem = dma_map_sg(mvi->dev,
940 task->scatter,
941 task->num_scatter,
942 task->data_dir);
943 if (!n_elem) {
944 rc = -ENOMEM;
945 goto prep_out;
946 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800947 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700948 } else {
949 n_elem = task->num_scatter;
950 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400951
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700952 rc = mvs_tag_alloc(mvi, &tag);
953 if (rc)
954 goto err_out;
Jeff Garzikb5762942007-10-25 20:58:22 -0400955
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700956 slot = &mvi->slot_info[tag];
Srinivas9dc9fd92010-02-15 00:00:00 -0600957
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700958 task->lldd_task = NULL;
959 slot->n_elem = n_elem;
960 slot->slot_tag = tag;
Jeff Garzikb5762942007-10-25 20:58:22 -0400961
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700962 slot->buf = pci_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
963 if (!slot->buf)
964 goto err_out_tag;
965 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
966
967 tei.task = task;
968 tei.hdr = &mvi->slot[tag];
969 tei.tag = tag;
970 tei.n_elem = n_elem;
971 switch (task->task_proto) {
972 case SAS_PROTOCOL_SMP:
973 rc = mvs_task_prep_smp(mvi, &tei);
974 break;
975 case SAS_PROTOCOL_SSP:
976 rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
977 break;
978 case SAS_PROTOCOL_SATA:
979 case SAS_PROTOCOL_STP:
980 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
981 rc = mvs_task_prep_ata(mvi, &tei);
982 break;
983 default:
984 dev_printk(KERN_ERR, mvi->dev,
985 "unknown sas_task proto: 0x%x\n",
986 task->task_proto);
987 rc = -EINVAL;
988 break;
989 }
990
991 if (rc) {
992 mv_dprintk("rc is %x\n", rc);
993 goto err_out_slot_buf;
994 }
995 slot->task = task;
996 slot->port = tei.port;
997 task->lldd_task = slot;
998 list_add_tail(&slot->entry, &tei.port->list);
999 spin_lock(&task->task_state_lock);
1000 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1001 spin_unlock(&task->task_state_lock);
1002
1003 mvs_hba_memory_dump(mvi, tag, task->task_proto);
1004 mvi_dev->running_req++;
1005 ++(*pass);
1006 mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
1007
1008 return rc;
1009
1010err_out_slot_buf:
1011 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
Jeff Garzikb5762942007-10-25 20:58:22 -04001012err_out_tag:
Ke Wei8f261aa2008-02-23 21:15:27 +08001013 mvs_tag_free(mvi, tag);
Jeff Garzikb5762942007-10-25 20:58:22 -04001014err_out:
Andy Yan20b09c22009-05-08 17:46:40 -04001015
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001016 dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
1017 if (!sas_protocol_ata(task->task_proto))
Ke Wei8f261aa2008-02-23 21:15:27 +08001018 if (n_elem)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001019 dma_unmap_sg(mvi->dev, task->scatter, n_elem,
1020 task->data_dir);
1021prep_out:
1022 return rc;
1023}
1024
1025static struct mvs_task_list *mvs_task_alloc_list(int *num, gfp_t gfp_flags)
1026{
1027 struct mvs_task_list *first = NULL;
1028
1029 for (; *num > 0; --*num) {
1030 struct mvs_task_list *mvs_list = kmem_cache_zalloc(mvs_task_list_cache, gfp_flags);
1031
1032 if (!mvs_list)
1033 break;
1034
1035 INIT_LIST_HEAD(&mvs_list->list);
1036 if (!first)
1037 first = mvs_list;
1038 else
1039 list_add_tail(&mvs_list->list, &first->list);
1040
1041 }
1042
1043 return first;
1044}
1045
1046static inline void mvs_task_free_list(struct mvs_task_list *mvs_list)
1047{
1048 LIST_HEAD(list);
1049 struct list_head *pos, *a;
1050 struct mvs_task_list *mlist = NULL;
1051
1052 __list_add(&list, mvs_list->list.prev, &mvs_list->list);
1053
1054 list_for_each_safe(pos, a, &list) {
1055 list_del_init(pos);
1056 mlist = list_entry(pos, struct mvs_task_list, list);
1057 kmem_cache_free(mvs_task_list_cache, mlist);
1058 }
1059}
1060
1061static int mvs_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
1062 struct completion *completion, int is_tmf,
1063 struct mvs_tmf_task *tmf)
1064{
1065 struct domain_device *dev = task->dev;
1066 struct mvs_info *mvi = NULL;
1067 u32 rc = 0;
1068 u32 pass = 0;
1069 unsigned long flags = 0;
1070
1071 mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
1072
1073 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
1074 spin_unlock_irq(dev->sata_dev.ap->lock);
1075
1076 spin_lock_irqsave(&mvi->lock, flags);
1077 rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
1078 if (rc)
1079 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
1080
1081 if (likely(pass))
1082 MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
1083 (MVS_CHIP_SLOT_SZ - 1));
Andy Yan0b84b702009-05-11 20:05:26 +08001084 spin_unlock_irqrestore(&mvi->lock, flags);
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001085
1086 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
1087 spin_lock_irq(dev->sata_dev.ap->lock);
1088
1089 return rc;
1090}
1091
1092static int mvs_collector_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
1093 struct completion *completion, int is_tmf,
1094 struct mvs_tmf_task *tmf)
1095{
1096 struct domain_device *dev = task->dev;
1097 struct mvs_prv_info *mpi = dev->port->ha->lldd_ha;
1098 struct mvs_info *mvi = NULL;
1099 struct sas_task *t = task;
1100 struct mvs_task_list *mvs_list = NULL, *a;
1101 LIST_HEAD(q);
1102 int pass[2] = {0};
1103 u32 rc = 0;
1104 u32 n = num;
1105 unsigned long flags = 0;
1106
1107 mvs_list = mvs_task_alloc_list(&n, gfp_flags);
1108 if (n) {
1109 printk(KERN_ERR "%s: mvs alloc list failed.\n", __func__);
1110 rc = -ENOMEM;
1111 goto free_list;
1112 }
1113
1114 __list_add(&q, mvs_list->list.prev, &mvs_list->list);
1115
1116 list_for_each_entry(a, &q, list) {
1117 a->task = t;
1118 t = list_entry(t->list.next, struct sas_task, list);
1119 }
1120
1121 list_for_each_entry(a, &q , list) {
1122
1123 t = a->task;
1124 mvi = ((struct mvs_device *)t->dev->lldd_dev)->mvi_info;
1125
1126 spin_lock_irqsave(&mvi->lock, flags);
1127 rc = mvs_task_prep(t, mvi, is_tmf, tmf, &pass[mvi->id]);
1128 if (rc)
1129 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
1130 spin_unlock_irqrestore(&mvi->lock, flags);
1131 }
1132
1133 if (likely(pass[0]))
1134 MVS_CHIP_DISP->start_delivery(mpi->mvi[0],
1135 (mpi->mvi[0]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
1136
1137 if (likely(pass[1]))
1138 MVS_CHIP_DISP->start_delivery(mpi->mvi[1],
1139 (mpi->mvi[1]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
1140
1141 list_del_init(&q);
1142
1143free_list:
1144 if (mvs_list)
1145 mvs_task_free_list(mvs_list);
1146
Jeff Garzikb5762942007-10-25 20:58:22 -04001147 return rc;
1148}
1149
Andy Yan20b09c22009-05-08 17:46:40 -04001150int mvs_queue_command(struct sas_task *task, const int num,
1151 gfp_t gfp_flags)
1152{
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001153 struct mvs_device *mvi_dev = task->dev->lldd_dev;
1154 struct sas_ha_struct *sas = mvi_dev->mvi_info->sas;
1155
1156 if (sas->lldd_max_execute_num < 2)
1157 return mvs_task_exec(task, num, gfp_flags, NULL, 0, NULL);
1158 else
1159 return mvs_collector_task_exec(task, num, gfp_flags, NULL, 0, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04001160}
1161
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001162static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
Ke Wei8f261aa2008-02-23 21:15:27 +08001163{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001164 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1165 mvs_tag_clear(mvi, slot_idx);
1166}
Ke Wei8f261aa2008-02-23 21:15:27 +08001167
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001168static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
1169 struct mvs_slot_info *slot, u32 slot_idx)
1170{
Andy Yan20b09c22009-05-08 17:46:40 -04001171 if (!slot->task)
1172 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001173 if (!sas_protocol_ata(task->task_proto))
1174 if (slot->n_elem)
Andy Yan20b09c22009-05-08 17:46:40 -04001175 dma_unmap_sg(mvi->dev, task->scatter,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001176 slot->n_elem, task->data_dir);
Ke Wei8f261aa2008-02-23 21:15:27 +08001177
Ke Wei8f261aa2008-02-23 21:15:27 +08001178 switch (task->task_proto) {
1179 case SAS_PROTOCOL_SMP:
Andy Yan20b09c22009-05-08 17:46:40 -04001180 dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001181 PCI_DMA_FROMDEVICE);
Andy Yan20b09c22009-05-08 17:46:40 -04001182 dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001183 PCI_DMA_TODEVICE);
Ke Wei8f261aa2008-02-23 21:15:27 +08001184 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001185
Ke Wei8f261aa2008-02-23 21:15:27 +08001186 case SAS_PROTOCOL_SATA:
1187 case SAS_PROTOCOL_STP:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001188 case SAS_PROTOCOL_SSP:
Ke Wei8f261aa2008-02-23 21:15:27 +08001189 default:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001190 /* do nothing */
Ke Wei8f261aa2008-02-23 21:15:27 +08001191 break;
1192 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001193
1194 if (slot->buf) {
1195 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
1196 slot->buf = NULL;
1197 }
Andy Yan20b09c22009-05-08 17:46:40 -04001198 list_del_init(&slot->entry);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001199 task->lldd_task = NULL;
1200 slot->task = NULL;
1201 slot->port = NULL;
Andy Yan20b09c22009-05-08 17:46:40 -04001202 slot->slot_tag = 0xFFFFFFFF;
1203 mvs_slot_free(mvi, slot_idx);
Ke Wei8f261aa2008-02-23 21:15:27 +08001204}
1205
1206static void mvs_update_wideport(struct mvs_info *mvi, int i)
1207{
1208 struct mvs_phy *phy = &mvi->phy[i];
1209 struct mvs_port *port = phy->port;
1210 int j, no;
1211
Andy Yan20b09c22009-05-08 17:46:40 -04001212 for_each_phy(port->wide_port_phymap, j, no) {
1213 if (j & 1) {
1214 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1215 PHYR_WIDE_PORT);
1216 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
Ke Wei8f261aa2008-02-23 21:15:27 +08001217 port->wide_port_phymap);
1218 } else {
Andy Yan20b09c22009-05-08 17:46:40 -04001219 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1220 PHYR_WIDE_PORT);
1221 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1222 0);
Ke Wei8f261aa2008-02-23 21:15:27 +08001223 }
Andy Yan20b09c22009-05-08 17:46:40 -04001224 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001225}
1226
1227static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
1228{
1229 u32 tmp;
1230 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001231 struct mvs_port *port = phy->port;
Ke Wei8f261aa2008-02-23 21:15:27 +08001232
Andy Yan20b09c22009-05-08 17:46:40 -04001233 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001234 if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
Ke Wei963829e2008-03-27 14:55:23 +08001235 if (!port)
Ke Wei8f261aa2008-02-23 21:15:27 +08001236 phy->phy_attached = 1;
1237 return tmp;
1238 }
1239
Ke Wei8f261aa2008-02-23 21:15:27 +08001240 if (port) {
1241 if (phy->phy_type & PORT_TYPE_SAS) {
1242 port->wide_port_phymap &= ~(1U << i);
1243 if (!port->wide_port_phymap)
1244 port->port_attached = 0;
1245 mvs_update_wideport(mvi, i);
1246 } else if (phy->phy_type & PORT_TYPE_SATA)
1247 port->port_attached = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +08001248 phy->port = NULL;
1249 phy->phy_attached = 0;
1250 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1251 }
1252 return 0;
1253}
1254
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001255static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
1256{
1257 u32 *s = (u32 *) buf;
1258
1259 if (!s)
1260 return NULL;
1261
Andy Yan20b09c22009-05-08 17:46:40 -04001262 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
1263 s[3] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001264
Andy Yan20b09c22009-05-08 17:46:40 -04001265 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
1266 s[2] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001267
Andy Yan20b09c22009-05-08 17:46:40 -04001268 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
1269 s[1] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001270
Andy Yan20b09c22009-05-08 17:46:40 -04001271 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
1272 s[0] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
1273
1274 /* Workaround: take some ATAPI devices for ATA */
1275 if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1276 s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001277
Andy Yanf9da3be2009-05-14 20:41:21 -04001278 return s;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001279}
1280
1281static u32 mvs_is_sig_fis_received(u32 irq_status)
1282{
1283 return irq_status & PHYEV_SIG_FIS;
1284}
1285
Andy Yan20b09c22009-05-08 17:46:40 -04001286void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
Ke Wei8f261aa2008-02-23 21:15:27 +08001287{
1288 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001289 struct sas_identify_frame *id;
Ke Wei8f261aa2008-02-23 21:15:27 +08001290
Andy Yan20b09c22009-05-08 17:46:40 -04001291 id = (struct sas_identify_frame *)phy->frame_rcvd;
Ke Wei8f261aa2008-02-23 21:15:27 +08001292
1293 if (get_st) {
Andy Yan20b09c22009-05-08 17:46:40 -04001294 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001295 phy->phy_status = mvs_is_phy_ready(mvi, i);
1296 }
1297
1298 if (phy->phy_status) {
Andy Yan20b09c22009-05-08 17:46:40 -04001299 int oob_done = 0;
1300 struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
Ke Wei8f261aa2008-02-23 21:15:27 +08001301
Andy Yan20b09c22009-05-08 17:46:40 -04001302 oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001303
Andy Yan20b09c22009-05-08 17:46:40 -04001304 MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
1305 if (phy->phy_type & PORT_TYPE_SATA) {
1306 phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
1307 if (mvs_is_sig_fis_received(phy->irq_status)) {
1308 phy->phy_attached = 1;
1309 phy->att_dev_sas_addr =
1310 i + mvi->id * mvi->chip->n_phy;
1311 if (oob_done)
1312 sas_phy->oob_mode = SATA_OOB_MODE;
1313 phy->frame_rcvd_size =
1314 sizeof(struct dev_to_host_fis);
Andy Yanf9da3be2009-05-14 20:41:21 -04001315 mvs_get_d2h_reg(mvi, i, id);
Andy Yan20b09c22009-05-08 17:46:40 -04001316 } else {
1317 u32 tmp;
1318 dev_printk(KERN_DEBUG, mvi->dev,
1319 "Phy%d : No sig fis\n", i);
1320 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1321 MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1322 tmp | PHYEV_SIG_FIS);
1323 phy->phy_attached = 0;
1324 phy->phy_type &= ~PORT_TYPE_SATA;
1325 MVS_CHIP_DISP->phy_reset(mvi, i, 0);
1326 goto out_done;
1327 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001328 } else if (phy->phy_type & PORT_TYPE_SAS
Andy Yan20b09c22009-05-08 17:46:40 -04001329 || phy->att_dev_info & PORT_SSP_INIT_MASK) {
1330 phy->phy_attached = 1;
Ke Wei8f261aa2008-02-23 21:15:27 +08001331 phy->identify.device_type =
Andy Yan20b09c22009-05-08 17:46:40 -04001332 phy->att_dev_info & PORT_DEV_TYPE_MASK;
Ke Wei8f261aa2008-02-23 21:15:27 +08001333
1334 if (phy->identify.device_type == SAS_END_DEV)
1335 phy->identify.target_port_protocols =
1336 SAS_PROTOCOL_SSP;
1337 else if (phy->identify.device_type != NO_DEVICE)
1338 phy->identify.target_port_protocols =
1339 SAS_PROTOCOL_SMP;
Andy Yan20b09c22009-05-08 17:46:40 -04001340 if (oob_done)
Ke Wei8f261aa2008-02-23 21:15:27 +08001341 sas_phy->oob_mode = SAS_OOB_MODE;
1342 phy->frame_rcvd_size =
1343 sizeof(struct sas_identify_frame);
Ke Wei8f261aa2008-02-23 21:15:27 +08001344 }
Andy Yan20b09c22009-05-08 17:46:40 -04001345 memcpy(sas_phy->attached_sas_addr,
1346 &phy->att_dev_sas_addr, SAS_ADDR_SIZE);
Ke Weie9ff91b2008-03-27 14:55:33 +08001347
Andy Yan20b09c22009-05-08 17:46:40 -04001348 if (MVS_CHIP_DISP->phy_work_around)
1349 MVS_CHIP_DISP->phy_work_around(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001350 }
Andy Yan20b09c22009-05-08 17:46:40 -04001351 mv_dprintk("port %d attach dev info is %x\n",
1352 i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
1353 mv_dprintk("port %d attach sas addr is %llx\n",
1354 i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
Ke Weie9ff91b2008-03-27 14:55:33 +08001355out_done:
Ke Wei8f261aa2008-02-23 21:15:27 +08001356 if (get_st)
Andy Yan20b09c22009-05-08 17:46:40 -04001357 MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
Ke Wei8f261aa2008-02-23 21:15:27 +08001358}
1359
Andy Yan20b09c22009-05-08 17:46:40 -04001360static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
1361{
1362 struct sas_ha_struct *sas_ha = sas_phy->ha;
1363 struct mvs_info *mvi = NULL; int i = 0, hi;
1364 struct mvs_phy *phy = sas_phy->lldd_phy;
1365 struct asd_sas_port *sas_port = sas_phy->port;
1366 struct mvs_port *port;
1367 unsigned long flags = 0;
1368 if (!sas_port)
1369 return;
1370
1371 while (sas_ha->sas_phy[i]) {
1372 if (sas_ha->sas_phy[i] == sas_phy)
1373 break;
1374 i++;
1375 }
1376 hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
1377 mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
1378 if (sas_port->id >= mvi->chip->n_phy)
1379 port = &mvi->port[sas_port->id - mvi->chip->n_phy];
1380 else
1381 port = &mvi->port[sas_port->id];
1382 if (lock)
1383 spin_lock_irqsave(&mvi->lock, flags);
1384 port->port_attached = 1;
1385 phy->port = port;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001386 sas_port->lldd_port = port;
Andy Yan20b09c22009-05-08 17:46:40 -04001387 if (phy->phy_type & PORT_TYPE_SAS) {
1388 port->wide_port_phymap = sas_port->phy_mask;
1389 mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1390 mvs_update_wideport(mvi, sas_phy->id);
1391 }
1392 if (lock)
1393 spin_unlock_irqrestore(&mvi->lock, flags);
1394}
1395
1396static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1397{
Srinivas9dc9fd92010-02-15 00:00:00 -06001398 struct domain_device *dev;
1399 struct mvs_phy *phy = sas_phy->lldd_phy;
1400 struct mvs_info *mvi = phy->mvi;
1401 struct asd_sas_port *port = sas_phy->port;
1402 int phy_no = 0;
1403
1404 while (phy != &mvi->phy[phy_no]) {
1405 phy_no++;
1406 if (phy_no >= MVS_MAX_PHYS)
1407 return;
1408 }
1409 list_for_each_entry(dev, &port->dev_list, dev_list_node)
1410 mvs_do_release_task(phy->mvi, phy_no, NULL);
1411
Andy Yan20b09c22009-05-08 17:46:40 -04001412}
1413
1414
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001415void mvs_port_formed(struct asd_sas_phy *sas_phy)
Ke Wei8f261aa2008-02-23 21:15:27 +08001416{
Andy Yan20b09c22009-05-08 17:46:40 -04001417 mvs_port_notify_formed(sas_phy, 1);
1418}
Ke Wei8f261aa2008-02-23 21:15:27 +08001419
Andy Yan20b09c22009-05-08 17:46:40 -04001420void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1421{
1422 mvs_port_notify_deformed(sas_phy, 1);
1423}
1424
1425struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
1426{
1427 u32 dev;
1428 for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
1429 if (mvi->devices[dev].dev_type == NO_DEVICE) {
1430 mvi->devices[dev].device_id = dev;
1431 return &mvi->devices[dev];
1432 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001433 }
Andy Yan20b09c22009-05-08 17:46:40 -04001434
1435 if (dev == MVS_MAX_DEVICES)
1436 mv_printk("max support %d devices, ignore ..\n",
1437 MVS_MAX_DEVICES);
1438
1439 return NULL;
1440}
1441
1442void mvs_free_dev(struct mvs_device *mvi_dev)
1443{
1444 u32 id = mvi_dev->device_id;
1445 memset(mvi_dev, 0, sizeof(*mvi_dev));
1446 mvi_dev->device_id = id;
1447 mvi_dev->dev_type = NO_DEVICE;
1448 mvi_dev->dev_status = MVS_DEV_NORMAL;
1449 mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1450}
1451
1452int mvs_dev_found_notify(struct domain_device *dev, int lock)
1453{
1454 unsigned long flags = 0;
1455 int res = 0;
1456 struct mvs_info *mvi = NULL;
1457 struct domain_device *parent_dev = dev->parent;
1458 struct mvs_device *mvi_device;
1459
1460 mvi = mvs_find_dev_mvi(dev);
1461
1462 if (lock)
1463 spin_lock_irqsave(&mvi->lock, flags);
1464
1465 mvi_device = mvs_alloc_dev(mvi);
1466 if (!mvi_device) {
1467 res = -1;
1468 goto found_out;
1469 }
Andy Yanf9da3be2009-05-14 20:41:21 -04001470 dev->lldd_dev = mvi_device;
Srinivas9dc9fd92010-02-15 00:00:00 -06001471 mvi_device->dev_status = MVS_DEV_NORMAL;
Andy Yan20b09c22009-05-08 17:46:40 -04001472 mvi_device->dev_type = dev->dev_type;
Andy Yan9870d9a2009-05-11 22:19:25 +08001473 mvi_device->mvi_info = mvi;
Andy Yan20b09c22009-05-08 17:46:40 -04001474 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1475 int phy_id;
1476 u8 phy_num = parent_dev->ex_dev.num_phys;
1477 struct ex_phy *phy;
1478 for (phy_id = 0; phy_id < phy_num; phy_id++) {
1479 phy = &parent_dev->ex_dev.ex_phy[phy_id];
1480 if (SAS_ADDR(phy->attached_sas_addr) ==
1481 SAS_ADDR(dev->sas_addr)) {
1482 mvi_device->attached_phy = phy_id;
1483 break;
1484 }
1485 }
1486
1487 if (phy_id == phy_num) {
1488 mv_printk("Error: no attached dev:%016llx"
1489 "at ex:%016llx.\n",
1490 SAS_ADDR(dev->sas_addr),
1491 SAS_ADDR(parent_dev->sas_addr));
1492 res = -1;
1493 }
1494 }
1495
1496found_out:
1497 if (lock)
1498 spin_unlock_irqrestore(&mvi->lock, flags);
1499 return res;
1500}
1501
1502int mvs_dev_found(struct domain_device *dev)
1503{
1504 return mvs_dev_found_notify(dev, 1);
1505}
1506
Srinivas9dc9fd92010-02-15 00:00:00 -06001507void mvs_dev_gone_notify(struct domain_device *dev)
Andy Yan20b09c22009-05-08 17:46:40 -04001508{
1509 unsigned long flags = 0;
Andy Yanf9da3be2009-05-14 20:41:21 -04001510 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001511 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001512
Srinivas9dc9fd92010-02-15 00:00:00 -06001513 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001514
1515 if (mvi_dev) {
1516 mv_dprintk("found dev[%d:%x] is gone.\n",
1517 mvi_dev->device_id, mvi_dev->dev_type);
Srinivas9dc9fd92010-02-15 00:00:00 -06001518 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001519 mvs_free_reg_set(mvi, mvi_dev);
1520 mvs_free_dev(mvi_dev);
1521 } else {
1522 mv_dprintk("found dev has gone.\n");
1523 }
1524 dev->lldd_dev = NULL;
1525
Srinivas9dc9fd92010-02-15 00:00:00 -06001526 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001527}
1528
1529
1530void mvs_dev_gone(struct domain_device *dev)
1531{
Srinivas9dc9fd92010-02-15 00:00:00 -06001532 mvs_dev_gone_notify(dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001533}
1534
1535static struct sas_task *mvs_alloc_task(void)
1536{
1537 struct sas_task *task = kzalloc(sizeof(struct sas_task), GFP_KERNEL);
1538
1539 if (task) {
1540 INIT_LIST_HEAD(&task->list);
1541 spin_lock_init(&task->task_state_lock);
1542 task->task_state_flags = SAS_TASK_STATE_PENDING;
1543 init_timer(&task->timer);
1544 init_completion(&task->completion);
1545 }
1546 return task;
1547}
1548
1549static void mvs_free_task(struct sas_task *task)
1550{
1551 if (task) {
1552 BUG_ON(!list_empty(&task->list));
1553 kfree(task);
1554 }
1555}
1556
1557static void mvs_task_done(struct sas_task *task)
1558{
1559 if (!del_timer(&task->timer))
1560 return;
1561 complete(&task->completion);
1562}
1563
1564static void mvs_tmf_timedout(unsigned long data)
1565{
1566 struct sas_task *task = (struct sas_task *)data;
1567
1568 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1569 complete(&task->completion);
1570}
1571
1572/* XXX */
1573#define MVS_TASK_TIMEOUT 20
1574static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1575 void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1576{
1577 int res, retry;
1578 struct sas_task *task = NULL;
1579
1580 for (retry = 0; retry < 3; retry++) {
1581 task = mvs_alloc_task();
1582 if (!task)
1583 return -ENOMEM;
1584
1585 task->dev = dev;
1586 task->task_proto = dev->tproto;
1587
1588 memcpy(&task->ssp_task, parameter, para_len);
1589 task->task_done = mvs_task_done;
1590
1591 task->timer.data = (unsigned long) task;
1592 task->timer.function = mvs_tmf_timedout;
1593 task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1594 add_timer(&task->timer);
1595
Andy Yan0b84b702009-05-11 20:05:26 +08001596 res = mvs_task_exec(task, 1, GFP_KERNEL, NULL, 1, tmf);
Andy Yan20b09c22009-05-08 17:46:40 -04001597
1598 if (res) {
1599 del_timer(&task->timer);
1600 mv_printk("executing internel task failed:%d\n", res);
1601 goto ex_err;
1602 }
1603
1604 wait_for_completion(&task->completion);
1605 res = -TMF_RESP_FUNC_FAILED;
1606 /* Even TMF timed out, return direct. */
1607 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1608 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1609 mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1610 goto ex_err;
1611 }
1612 }
1613
1614 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -05001615 task->task_status.stat == SAM_STAT_GOOD) {
Andy Yan20b09c22009-05-08 17:46:40 -04001616 res = TMF_RESP_FUNC_COMPLETE;
1617 break;
1618 }
1619
1620 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1621 task->task_status.stat == SAS_DATA_UNDERRUN) {
1622 /* no error, but return the number of bytes of
1623 * underrun */
1624 res = task->task_status.residual;
1625 break;
1626 }
1627
1628 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1629 task->task_status.stat == SAS_DATA_OVERRUN) {
1630 mv_dprintk("blocked task error.\n");
1631 res = -EMSGSIZE;
1632 break;
1633 } else {
1634 mv_dprintk(" task to dev %016llx response: 0x%x "
1635 "status 0x%x\n",
1636 SAS_ADDR(dev->sas_addr),
1637 task->task_status.resp,
1638 task->task_status.stat);
1639 mvs_free_task(task);
1640 task = NULL;
1641
1642 }
1643 }
1644ex_err:
1645 BUG_ON(retry == 3 && task != NULL);
1646 if (task != NULL)
1647 mvs_free_task(task);
1648 return res;
1649}
1650
1651static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1652 u8 *lun, struct mvs_tmf_task *tmf)
1653{
1654 struct sas_ssp_task ssp_task;
1655 DECLARE_COMPLETION_ONSTACK(completion);
1656 if (!(dev->tproto & SAS_PROTOCOL_SSP))
1657 return TMF_RESP_FUNC_ESUPP;
1658
1659 strncpy((u8 *)&ssp_task.LUN, lun, 8);
1660
1661 return mvs_exec_internal_tmf_task(dev, &ssp_task,
1662 sizeof(ssp_task), tmf);
1663}
1664
1665
1666/* Standard mandates link reset for ATA (type 0)
1667 and hard reset for SSP (type 1) , only for RECOVERY */
1668static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1669{
1670 int rc;
1671 struct sas_phy *phy = sas_find_local_phy(dev);
1672 int reset_type = (dev->dev_type == SATA_DEV ||
1673 (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1674 rc = sas_phy_reset(phy, reset_type);
1675 msleep(2000);
1676 return rc;
1677}
1678
1679/* mandatory SAM-3 */
1680int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1681{
1682 unsigned long flags;
1683 int i, phyno[WIDE_PORT_MAX_PHY], num , rc = TMF_RESP_FUNC_FAILED;
1684 struct mvs_tmf_task tmf_task;
Andy Yanf9da3be2009-05-14 20:41:21 -04001685 struct mvs_device * mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001686 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001687
1688 tmf_task.tmf = TMF_LU_RESET;
1689 mvi_dev->dev_status = MVS_DEV_EH;
1690 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1691 if (rc == TMF_RESP_FUNC_COMPLETE) {
1692 num = mvs_find_dev_phyno(dev, phyno);
1693 spin_lock_irqsave(&mvi->lock, flags);
1694 for (i = 0; i < num; i++)
Srinivas9dc9fd92010-02-15 00:00:00 -06001695 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001696 spin_unlock_irqrestore(&mvi->lock, flags);
1697 }
1698 /* If failed, fall-through I_T_Nexus reset */
1699 mv_printk("%s for device[%x]:rc= %d\n", __func__,
1700 mvi_dev->device_id, rc);
1701 return rc;
Jeff Garzikb5762942007-10-25 20:58:22 -04001702}
1703
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001704int mvs_I_T_nexus_reset(struct domain_device *dev)
Ke Weie9ff91b2008-03-27 14:55:33 +08001705{
Andy Yan20b09c22009-05-08 17:46:40 -04001706 unsigned long flags;
Srinivas9dc9fd92010-02-15 00:00:00 -06001707 int rc = TMF_RESP_FUNC_FAILED;
1708 struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001709 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001710
1711 if (mvi_dev->dev_status != MVS_DEV_EH)
1712 return TMF_RESP_FUNC_COMPLETE;
1713 rc = mvs_debug_I_T_nexus_reset(dev);
1714 mv_printk("%s for device[%x]:rc= %d\n",
1715 __func__, mvi_dev->device_id, rc);
1716
1717 /* housekeeper */
Andy Yan20b09c22009-05-08 17:46:40 -04001718 spin_lock_irqsave(&mvi->lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001719 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001720 spin_unlock_irqrestore(&mvi->lock, flags);
1721
1722 return rc;
1723}
1724/* optional SAM-3 */
1725int mvs_query_task(struct sas_task *task)
1726{
1727 u32 tag;
1728 struct scsi_lun lun;
1729 struct mvs_tmf_task tmf_task;
1730 int rc = TMF_RESP_FUNC_FAILED;
1731
1732 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1733 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1734 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001735 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1736 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001737
1738 int_to_scsilun(cmnd->device->lun, &lun);
1739 rc = mvs_find_tag(mvi, task, &tag);
1740 if (rc == 0) {
1741 rc = TMF_RESP_FUNC_FAILED;
1742 return rc;
1743 }
1744
1745 tmf_task.tmf = TMF_QUERY_TASK;
1746 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1747
1748 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1749 switch (rc) {
1750 /* The task is still in Lun, release it then */
1751 case TMF_RESP_FUNC_SUCC:
1752 /* The task is not in Lun or failed, reset the phy */
1753 case TMF_RESP_FUNC_FAILED:
1754 case TMF_RESP_FUNC_COMPLETE:
1755 break;
Srinivas9dc9fd92010-02-15 00:00:00 -06001756 default:
1757 rc = TMF_RESP_FUNC_COMPLETE;
1758 break;
Andy Yan20b09c22009-05-08 17:46:40 -04001759 }
1760 }
1761 mv_printk("%s:rc= %d\n", __func__, rc);
1762 return rc;
1763}
1764
1765/* mandatory SAM-3, still need free task/slot info */
1766int mvs_abort_task(struct sas_task *task)
1767{
1768 struct scsi_lun lun;
1769 struct mvs_tmf_task tmf_task;
1770 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001771 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
Jiri Slaby24ae1632010-06-22 13:42:02 +02001772 struct mvs_info *mvi;
Andy Yan20b09c22009-05-08 17:46:40 -04001773 int rc = TMF_RESP_FUNC_FAILED;
1774 unsigned long flags;
1775 u32 tag;
Andy Yan9870d9a2009-05-11 22:19:25 +08001776
Srinivas9dc9fd92010-02-15 00:00:00 -06001777 if (!mvi_dev) {
1778 mv_printk("%s:%d TMF_RESP_FUNC_FAILED\n", __func__, __LINE__);
1779 rc = TMF_RESP_FUNC_FAILED;
1780 }
1781
Jiri Slaby24ae1632010-06-22 13:42:02 +02001782 mvi = mvi_dev->mvi_info;
1783
Andy Yan20b09c22009-05-08 17:46:40 -04001784 spin_lock_irqsave(&task->task_state_lock, flags);
1785 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1786 spin_unlock_irqrestore(&task->task_state_lock, flags);
1787 rc = TMF_RESP_FUNC_COMPLETE;
1788 goto out;
1789 }
1790 spin_unlock_irqrestore(&task->task_state_lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001791 mvi_dev->dev_status = MVS_DEV_EH;
Andy Yan20b09c22009-05-08 17:46:40 -04001792 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1793 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1794
1795 int_to_scsilun(cmnd->device->lun, &lun);
1796 rc = mvs_find_tag(mvi, task, &tag);
1797 if (rc == 0) {
1798 mv_printk("No such tag in %s\n", __func__);
1799 rc = TMF_RESP_FUNC_FAILED;
1800 return rc;
1801 }
1802
1803 tmf_task.tmf = TMF_ABORT_TASK;
1804 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1805
1806 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1807
1808 /* if successful, clear the task and callback forwards.*/
1809 if (rc == TMF_RESP_FUNC_COMPLETE) {
1810 u32 slot_no;
1811 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -04001812
1813 if (task->lldd_task) {
Andy Yanf9da3be2009-05-14 20:41:21 -04001814 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -04001815 slot_no = (u32) (slot - mvi->slot_info);
Srinivas9dc9fd92010-02-15 00:00:00 -06001816 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001817 mvs_slot_complete(mvi, slot_no, 1);
Srinivas9dc9fd92010-02-15 00:00:00 -06001818 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001819 }
1820 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001821
Andy Yan20b09c22009-05-08 17:46:40 -04001822 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1823 task->task_proto & SAS_PROTOCOL_STP) {
1824 /* to do free register_set */
Srinivas9dc9fd92010-02-15 00:00:00 -06001825 if (SATA_DEV == dev->dev_type) {
1826 struct mvs_slot_info *slot = task->lldd_task;
1827 struct task_status_struct *tstat;
1828 u32 slot_idx = (u32)(slot - mvi->slot_info);
1829 tstat = &task->task_status;
1830 mv_dprintk(KERN_DEBUG "mv_abort_task() mvi=%p task=%p "
1831 "slot=%p slot_idx=x%x\n",
1832 mvi, task, slot, slot_idx);
1833 tstat->stat = SAS_ABORTED_TASK;
1834 if (mvi_dev && mvi_dev->running_req)
1835 mvi_dev->running_req--;
1836 if (sas_protocol_ata(task->task_proto))
1837 mvs_free_reg_set(mvi, mvi_dev);
1838 mvs_slot_task_free(mvi, task, slot, slot_idx);
1839 return -1;
1840 }
Andy Yan20b09c22009-05-08 17:46:40 -04001841 } else {
1842 /* SMP */
1843
1844 }
1845out:
1846 if (rc != TMF_RESP_FUNC_COMPLETE)
1847 mv_printk("%s:rc= %d\n", __func__, rc);
1848 return rc;
1849}
1850
1851int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1852{
1853 int rc = TMF_RESP_FUNC_FAILED;
1854 struct mvs_tmf_task tmf_task;
1855
1856 tmf_task.tmf = TMF_ABORT_TASK_SET;
1857 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1858
1859 return rc;
1860}
1861
1862int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1863{
1864 int rc = TMF_RESP_FUNC_FAILED;
1865 struct mvs_tmf_task tmf_task;
1866
1867 tmf_task.tmf = TMF_CLEAR_ACA;
1868 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1869
1870 return rc;
1871}
1872
1873int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1874{
1875 int rc = TMF_RESP_FUNC_FAILED;
1876 struct mvs_tmf_task tmf_task;
1877
1878 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1879 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1880
1881 return rc;
Ke Weie9ff91b2008-03-27 14:55:33 +08001882}
1883
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001884static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1885 u32 slot_idx, int err)
1886{
Andy Yanf9da3be2009-05-14 20:41:21 -04001887 struct mvs_device *mvi_dev = task->dev->lldd_dev;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001888 struct task_status_struct *tstat = &task->task_status;
1889 struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
James Bottomleydf64d3c2010-07-27 15:51:13 -05001890 int stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001891
Andy Yan20b09c22009-05-08 17:46:40 -04001892
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001893 resp->frame_len = sizeof(struct dev_to_host_fis);
1894 memcpy(&resp->ending_fis[0],
Andy Yan20b09c22009-05-08 17:46:40 -04001895 SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001896 sizeof(struct dev_to_host_fis));
1897 tstat->buf_valid_size = sizeof(*resp);
Srinivas9dc9fd92010-02-15 00:00:00 -06001898 if (unlikely(err)) {
1899 if (unlikely(err & CMD_ISS_STPD))
1900 stat = SAS_OPEN_REJECT;
1901 else
1902 stat = SAS_PROTO_RESPONSE;
1903 }
1904
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001905 return stat;
1906}
1907
1908static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1909 u32 slot_idx)
1910{
1911 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
Andy Yan20b09c22009-05-08 17:46:40 -04001912 int stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001913 u32 err_dw0 = le32_to_cpu(*(u32 *) (slot->response));
Andy Yan20b09c22009-05-08 17:46:40 -04001914 u32 tfs = 0;
1915 enum mvs_port_type type = PORT_TYPE_SAS;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001916
Andy Yan20b09c22009-05-08 17:46:40 -04001917 if (err_dw0 & CMD_ISS_STPD)
1918 MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1919
1920 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1921
James Bottomleydf64d3c2010-07-27 15:51:13 -05001922 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001923 switch (task->task_proto) {
1924 case SAS_PROTOCOL_SSP:
Andy Yan20b09c22009-05-08 17:46:40 -04001925 stat = SAS_ABORTED_TASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001926 break;
1927 case SAS_PROTOCOL_SMP:
James Bottomleydf64d3c2010-07-27 15:51:13 -05001928 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001929 break;
Andy Yan20b09c22009-05-08 17:46:40 -04001930
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001931 case SAS_PROTOCOL_SATA:
1932 case SAS_PROTOCOL_STP:
1933 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
Andy Yan20b09c22009-05-08 17:46:40 -04001934 {
1935 if (err_dw0 == 0x80400002)
1936 mv_printk("find reserved error, why?\n");
1937
1938 task->ata_task.use_ncq = 0;
Srinivas9dc9fd92010-02-15 00:00:00 -06001939 mvs_sata_done(mvi, task, slot_idx, err_dw0);
Andy Yan20b09c22009-05-08 17:46:40 -04001940 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001941 break;
1942 default:
1943 break;
1944 }
1945
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001946 return stat;
1947}
1948
Andy Yan20b09c22009-05-08 17:46:40 -04001949int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001950{
1951 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1952 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1953 struct sas_task *task = slot->task;
Andy Yan20b09c22009-05-08 17:46:40 -04001954 struct mvs_device *mvi_dev = NULL;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001955 struct task_status_struct *tstat;
Srinivas9dc9fd92010-02-15 00:00:00 -06001956 struct domain_device *dev;
1957 u32 aborted;
Andy Yan20b09c22009-05-08 17:46:40 -04001958
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001959 void *to;
Andy Yan20b09c22009-05-08 17:46:40 -04001960 enum exec_status sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001961
Andy Yan20b09c22009-05-08 17:46:40 -04001962 if (mvi->exp_req)
1963 mvi->exp_req--;
Srinivas9dc9fd92010-02-15 00:00:00 -06001964 if (unlikely(!task || !task->lldd_task || !task->dev))
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001965 return -1;
1966
Andy Yan20b09c22009-05-08 17:46:40 -04001967 tstat = &task->task_status;
Srinivas9dc9fd92010-02-15 00:00:00 -06001968 dev = task->dev;
1969 mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -04001970
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001971 mvs_hba_cq_dump(mvi);
1972
1973 spin_lock(&task->task_state_lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001974 task->task_state_flags &=
1975 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1976 task->task_state_flags |= SAS_TASK_STATE_DONE;
1977 /* race condition*/
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001978 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001979 spin_unlock(&task->task_state_lock);
1980
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001981 memset(tstat, 0, sizeof(*tstat));
1982 tstat->resp = SAS_TASK_COMPLETE;
1983
Andy Yan20b09c22009-05-08 17:46:40 -04001984 if (unlikely(aborted)) {
1985 tstat->stat = SAS_ABORTED_TASK;
Srinivas9dc9fd92010-02-15 00:00:00 -06001986 if (mvi_dev && mvi_dev->running_req)
1987 mvi_dev->running_req--;
Andy Yan20b09c22009-05-08 17:46:40 -04001988 if (sas_protocol_ata(task->task_proto))
1989 mvs_free_reg_set(mvi, mvi_dev);
1990
1991 mvs_slot_task_free(mvi, task, slot, slot_idx);
1992 return -1;
1993 }
1994
Srinivas9dc9fd92010-02-15 00:00:00 -06001995 if (unlikely(!mvi_dev || flags)) {
1996 if (!mvi_dev)
1997 mv_dprintk("port has not device.\n");
Andy Yan20b09c22009-05-08 17:46:40 -04001998 tstat->stat = SAS_PHY_DOWN;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001999 goto out;
2000 }
2001
2002 /* error info record present */
2003 if (unlikely((rx_desc & RXQ_ERR) && (*(u64 *) slot->response))) {
2004 tstat->stat = mvs_slot_err(mvi, task, slot_idx);
Srinivas9dc9fd92010-02-15 00:00:00 -06002005 tstat->resp = SAS_TASK_COMPLETE;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002006 goto out;
2007 }
2008
2009 switch (task->task_proto) {
2010 case SAS_PROTOCOL_SSP:
2011 /* hw says status == 0, datapres == 0 */
2012 if (rx_desc & RXQ_GOOD) {
James Bottomleydf64d3c2010-07-27 15:51:13 -05002013 tstat->stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002014 tstat->resp = SAS_TASK_COMPLETE;
2015 }
2016 /* response frame present */
2017 else if (rx_desc & RXQ_RSP) {
Andy Yan20b09c22009-05-08 17:46:40 -04002018 struct ssp_response_iu *iu = slot->response +
2019 sizeof(struct mvs_err_info);
2020 sas_ssp_task_response(mvi->dev, task, iu);
2021 } else
James Bottomleydf64d3c2010-07-27 15:51:13 -05002022 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002023 break;
2024
2025 case SAS_PROTOCOL_SMP: {
2026 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
James Bottomleydf64d3c2010-07-27 15:51:13 -05002027 tstat->stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002028 to = kmap_atomic(sg_page(sg_resp), KM_IRQ0);
2029 memcpy(to + sg_resp->offset,
2030 slot->response + sizeof(struct mvs_err_info),
2031 sg_dma_len(sg_resp));
2032 kunmap_atomic(to, KM_IRQ0);
2033 break;
2034 }
2035
2036 case SAS_PROTOCOL_SATA:
2037 case SAS_PROTOCOL_STP:
2038 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
2039 tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
2040 break;
2041 }
2042
2043 default:
James Bottomleydf64d3c2010-07-27 15:51:13 -05002044 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002045 break;
2046 }
Srinivas9dc9fd92010-02-15 00:00:00 -06002047 if (!slot->port->port_attached) {
2048 mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
2049 tstat->stat = SAS_PHY_DOWN;
2050 }
2051
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002052
2053out:
Srinivas9dc9fd92010-02-15 00:00:00 -06002054 if (mvi_dev && mvi_dev->running_req) {
2055 mvi_dev->running_req--;
2056 if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
Andy Yan0f980a82009-05-11 21:49:52 +08002057 mvs_free_reg_set(mvi, mvi_dev);
2058 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002059 mvs_slot_task_free(mvi, task, slot, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04002060 sts = tstat->stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002061
2062 spin_unlock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04002063 if (task->task_done)
2064 task->task_done(task);
2065 else
2066 mv_dprintk("why has not task_done.\n");
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002067 spin_lock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04002068
2069 return sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002070}
2071
Srinivas9dc9fd92010-02-15 00:00:00 -06002072void mvs_do_release_task(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -04002073 int phy_no, struct domain_device *dev)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002074{
Srinivas9dc9fd92010-02-15 00:00:00 -06002075 u32 slot_idx;
Andy Yan20b09c22009-05-08 17:46:40 -04002076 struct mvs_phy *phy;
2077 struct mvs_port *port;
2078 struct mvs_slot_info *slot, *slot2;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002079
Andy Yan20b09c22009-05-08 17:46:40 -04002080 phy = &mvi->phy[phy_no];
2081 port = phy->port;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002082 if (!port)
2083 return;
Srinivas9dc9fd92010-02-15 00:00:00 -06002084 /* clean cmpl queue in case request is already finished */
2085 mvs_int_rx(mvi, false);
2086
2087
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002088
Andy Yan20b09c22009-05-08 17:46:40 -04002089 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
2090 struct sas_task *task;
2091 slot_idx = (u32) (slot - mvi->slot_info);
2092 task = slot->task;
2093
2094 if (dev && task->dev != dev)
2095 continue;
2096
2097 mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
2098 slot_idx, slot->slot_tag, task);
Srinivas9dc9fd92010-02-15 00:00:00 -06002099 MVS_CHIP_DISP->command_active(mvi, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04002100
2101 mvs_slot_complete(mvi, slot_idx, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002102 }
2103}
2104
Srinivas9dc9fd92010-02-15 00:00:00 -06002105void mvs_release_task(struct mvs_info *mvi,
2106 struct domain_device *dev)
2107{
2108 int i, phyno[WIDE_PORT_MAX_PHY], num;
2109 /* housekeeper */
2110 num = mvs_find_dev_phyno(dev, phyno);
2111 for (i = 0; i < num; i++)
2112 mvs_do_release_task(mvi, phyno[i], dev);
2113}
2114
Andy Yan20b09c22009-05-08 17:46:40 -04002115static void mvs_phy_disconnected(struct mvs_phy *phy)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002116{
Andy Yan20b09c22009-05-08 17:46:40 -04002117 phy->phy_attached = 0;
2118 phy->att_dev_info = 0;
2119 phy->att_dev_sas_addr = 0;
2120}
2121
2122static void mvs_work_queue(struct work_struct *work)
2123{
2124 struct delayed_work *dw = container_of(work, struct delayed_work, work);
2125 struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
2126 struct mvs_info *mvi = mwq->mvi;
2127 unsigned long flags;
2128
2129 spin_lock_irqsave(&mvi->lock, flags);
2130 if (mwq->handler & PHY_PLUG_EVENT) {
2131 u32 phy_no = (unsigned long) mwq->data;
2132 struct sas_ha_struct *sas_ha = mvi->sas;
2133 struct mvs_phy *phy = &mvi->phy[phy_no];
2134 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2135
2136 if (phy->phy_event & PHY_PLUG_OUT) {
2137 u32 tmp;
2138 struct sas_identify_frame *id;
2139 id = (struct sas_identify_frame *)phy->frame_rcvd;
2140 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
2141 phy->phy_event &= ~PHY_PLUG_OUT;
2142 if (!(tmp & PHY_READY_MASK)) {
2143 sas_phy_disconnected(sas_phy);
2144 mvs_phy_disconnected(phy);
2145 sas_ha->notify_phy_event(sas_phy,
2146 PHYE_LOSS_OF_SIGNAL);
2147 mv_dprintk("phy%d Removed Device\n", phy_no);
2148 } else {
2149 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2150 mvs_update_phyinfo(mvi, phy_no, 1);
2151 mvs_bytes_dmaed(mvi, phy_no);
2152 mvs_port_notify_formed(sas_phy, 0);
2153 mv_dprintk("phy%d Attached Device\n", phy_no);
2154 }
2155 }
2156 }
2157 list_del(&mwq->entry);
2158 spin_unlock_irqrestore(&mvi->lock, flags);
2159 kfree(mwq);
2160}
2161
2162static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
2163{
2164 struct mvs_wq *mwq;
2165 int ret = 0;
2166
2167 mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
2168 if (mwq) {
2169 mwq->mvi = mvi;
2170 mwq->data = data;
2171 mwq->handler = handler;
2172 MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
2173 list_add_tail(&mwq->entry, &mvi->wq_list);
2174 schedule_delayed_work(&mwq->work_q, HZ * 2);
2175 } else
2176 ret = -ENOMEM;
2177
2178 return ret;
2179}
2180
2181static void mvs_sig_time_out(unsigned long tphy)
2182{
2183 struct mvs_phy *phy = (struct mvs_phy *)tphy;
2184 struct mvs_info *mvi = phy->mvi;
2185 u8 phy_no;
2186
2187 for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
2188 if (&mvi->phy[phy_no] == phy) {
2189 mv_dprintk("Get signature time out, reset phy %d\n",
2190 phy_no+mvi->id*mvi->chip->n_phy);
2191 MVS_CHIP_DISP->phy_reset(mvi, phy_no, 1);
2192 }
2193 }
2194}
2195
2196static void mvs_sig_remove_timer(struct mvs_phy *phy)
2197{
2198 if (phy->timer.function)
2199 del_timer(&phy->timer);
2200 phy->timer.function = NULL;
2201}
2202
2203void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
2204{
2205 u32 tmp;
2206 struct sas_ha_struct *sas_ha = mvi->sas;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002207 struct mvs_phy *phy = &mvi->phy[phy_no];
2208 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2209
Andy Yan20b09c22009-05-08 17:46:40 -04002210 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
2211 mv_dprintk("port %d ctrl sts=0x%X.\n", phy_no+mvi->id*mvi->chip->n_phy,
2212 MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
2213 mv_dprintk("Port %d irq sts = 0x%X\n", phy_no+mvi->id*mvi->chip->n_phy,
2214 phy->irq_status);
2215
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002216 /*
2217 * events is port event now ,
2218 * we need check the interrupt status which belongs to per port.
2219 */
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002220
Srinivas9dc9fd92010-02-15 00:00:00 -06002221 if (phy->irq_status & PHYEV_DCDR_ERR) {
Andy Yan20b09c22009-05-08 17:46:40 -04002222 mv_dprintk("port %d STP decoding error.\n",
Srinivas9dc9fd92010-02-15 00:00:00 -06002223 phy_no + mvi->id*mvi->chip->n_phy);
2224 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002225
Andy Yan20b09c22009-05-08 17:46:40 -04002226 if (phy->irq_status & PHYEV_POOF) {
2227 if (!(phy->phy_event & PHY_PLUG_OUT)) {
2228 int dev_sata = phy->phy_type & PORT_TYPE_SATA;
2229 int ready;
Srinivas9dc9fd92010-02-15 00:00:00 -06002230 mvs_do_release_task(mvi, phy_no, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04002231 phy->phy_event |= PHY_PLUG_OUT;
Srinivas9dc9fd92010-02-15 00:00:00 -06002232 MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
Andy Yan20b09c22009-05-08 17:46:40 -04002233 mvs_handle_event(mvi,
2234 (void *)(unsigned long)phy_no,
2235 PHY_PLUG_EVENT);
2236 ready = mvs_is_phy_ready(mvi, phy_no);
2237 if (!ready)
2238 mv_dprintk("phy%d Unplug Notice\n",
2239 phy_no +
2240 mvi->id * mvi->chip->n_phy);
2241 if (ready || dev_sata) {
2242 if (MVS_CHIP_DISP->stp_reset)
2243 MVS_CHIP_DISP->stp_reset(mvi,
2244 phy_no);
2245 else
2246 MVS_CHIP_DISP->phy_reset(mvi,
2247 phy_no, 0);
2248 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002249 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002250 }
2251 }
Andy Yan20b09c22009-05-08 17:46:40 -04002252
2253 if (phy->irq_status & PHYEV_COMWAKE) {
2254 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2255 MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2256 tmp | PHYEV_SIG_FIS);
2257 if (phy->timer.function == NULL) {
2258 phy->timer.data = (unsigned long)phy;
2259 phy->timer.function = mvs_sig_time_out;
2260 phy->timer.expires = jiffies + 10*HZ;
2261 add_timer(&phy->timer);
2262 }
2263 }
2264 if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2265 phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
2266 mvs_sig_remove_timer(phy);
2267 mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2268 if (phy->phy_status) {
2269 mdelay(10);
2270 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2271 if (phy->phy_type & PORT_TYPE_SATA) {
2272 tmp = MVS_CHIP_DISP->read_port_irq_mask(
2273 mvi, phy_no);
2274 tmp &= ~PHYEV_SIG_FIS;
2275 MVS_CHIP_DISP->write_port_irq_mask(mvi,
2276 phy_no, tmp);
2277 }
2278 mvs_update_phyinfo(mvi, phy_no, 0);
Srinivas9dc9fd92010-02-15 00:00:00 -06002279 if (phy->phy_type & PORT_TYPE_SAS) {
2280 MVS_CHIP_DISP->phy_reset(mvi, phy_no, 2);
2281 mdelay(10);
2282 }
2283
Andy Yan20b09c22009-05-08 17:46:40 -04002284 mvs_bytes_dmaed(mvi, phy_no);
2285 /* whether driver is going to handle hot plug */
2286 if (phy->phy_event & PHY_PLUG_OUT) {
2287 mvs_port_notify_formed(sas_phy, 0);
2288 phy->phy_event &= ~PHY_PLUG_OUT;
2289 }
2290 } else {
2291 mv_dprintk("plugin interrupt but phy%d is gone\n",
2292 phy_no + mvi->id*mvi->chip->n_phy);
2293 }
2294 } else if (phy->irq_status & PHYEV_BROAD_CH) {
2295 mv_dprintk("port %d broadcast change.\n",
2296 phy_no + mvi->id*mvi->chip->n_phy);
2297 /* exception for Samsung disk drive*/
2298 mdelay(1000);
2299 sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
2300 }
2301 MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002302}
2303
Andy Yan20b09c22009-05-08 17:46:40 -04002304int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002305{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002306 u32 rx_prod_idx, rx_desc;
2307 bool attn = false;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002308
2309 /* the first dword in the RX ring is special: it contains
2310 * a mirror of the hardware's RX producer index, so that
2311 * we don't have to stall the CPU reading that register.
2312 * The actual RX ring is offset by one dword, due to this.
2313 */
2314 rx_prod_idx = mvi->rx_cons;
2315 mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2316 if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
2317 return 0;
2318
2319 /* The CMPL_Q may come late, read from register and try again
2320 * note: if coalescing is enabled,
2321 * it will need to read from register every time for sure
2322 */
Andy Yan20b09c22009-05-08 17:46:40 -04002323 if (unlikely(mvi->rx_cons == rx_prod_idx))
2324 mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002325
2326 if (mvi->rx_cons == rx_prod_idx)
2327 return 0;
2328
2329 while (mvi->rx_cons != rx_prod_idx) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002330 /* increment our internal RX consumer pointer */
2331 rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002332 rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2333
2334 if (likely(rx_desc & RXQ_DONE))
2335 mvs_slot_complete(mvi, rx_desc, 0);
2336 if (rx_desc & RXQ_ATTN) {
2337 attn = true;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002338 } else if (rx_desc & RXQ_ERR) {
2339 if (!(rx_desc & RXQ_DONE))
2340 mvs_slot_complete(mvi, rx_desc, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002341 } else if (rx_desc & RXQ_SLOT_RESET) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002342 mvs_slot_free(mvi, rx_desc);
2343 }
2344 }
2345
2346 if (attn && self_clear)
Andy Yan20b09c22009-05-08 17:46:40 -04002347 MVS_CHIP_DISP->int_full(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002348 return 0;
2349}
2350