blob: 0d2138641214f22f0d3c9431e66ee5b506a63c9c [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>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; version 2 of the
12 * License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23*/
Jeff Garzikb5762942007-10-25 20:58:22 -040024
Jeff Garzikdd4969a2009-05-08 17:44:01 -040025#include "mv_sas.h"
Ke Wei8f261aa2008-02-23 21:15:27 +080026
Jeff Garzikdd4969a2009-05-08 17:44:01 -040027static int mvs_find_tag(struct mvs_info *mvi, struct sas_task *task, u32 *tag)
28{
29 if (task->lldd_task) {
30 struct mvs_slot_info *slot;
Andy Yanf9da3be2009-05-14 20:41:21 -040031 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -040032 *tag = slot->slot_tag;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040033 return 1;
34 }
35 return 0;
36}
Ke Wei8f261aa2008-02-23 21:15:27 +080037
Andy Yan20b09c22009-05-08 17:46:40 -040038void mvs_tag_clear(struct mvs_info *mvi, u32 tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040039{
Andy Yanf9da3be2009-05-14 20:41:21 -040040 void *bitmap = &mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040041 clear_bit(tag, bitmap);
42}
Jeff Garzikb5762942007-10-25 20:58:22 -040043
Andy Yan20b09c22009-05-08 17:46:40 -040044void mvs_tag_free(struct mvs_info *mvi, u32 tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040045{
46 mvs_tag_clear(mvi, tag);
47}
Jeff Garzikb5762942007-10-25 20:58:22 -040048
Andy Yan20b09c22009-05-08 17:46:40 -040049void mvs_tag_set(struct mvs_info *mvi, unsigned int tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040050{
Andy Yanf9da3be2009-05-14 20:41:21 -040051 void *bitmap = &mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040052 set_bit(tag, bitmap);
53}
54
Andy Yan20b09c22009-05-08 17:46:40 -040055inline int mvs_tag_alloc(struct mvs_info *mvi, u32 *tag_out)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040056{
57 unsigned int index, tag;
Andy Yanf9da3be2009-05-14 20:41:21 -040058 void *bitmap = &mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040059
Andy Yan20b09c22009-05-08 17:46:40 -040060 index = find_first_zero_bit(bitmap, mvi->tags_num);
Jeff Garzikdd4969a2009-05-08 17:44:01 -040061 tag = index;
Andy Yan20b09c22009-05-08 17:46:40 -040062 if (tag >= mvi->tags_num)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040063 return -SAS_QUEUE_FULL;
64 mvs_tag_set(mvi, tag);
65 *tag_out = tag;
66 return 0;
67}
68
69void mvs_tag_init(struct mvs_info *mvi)
70{
71 int i;
Andy Yan20b09c22009-05-08 17:46:40 -040072 for (i = 0; i < mvi->tags_num; ++i)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040073 mvs_tag_clear(mvi, i);
74}
Jeff Garzikb5762942007-10-25 20:58:22 -040075
Andy Yan20b09c22009-05-08 17:46:40 -040076void mvs_hexdump(u32 size, u8 *data, u32 baseaddr)
Ke Wei8f261aa2008-02-23 21:15:27 +080077{
78 u32 i;
79 u32 run;
80 u32 offset;
81
82 offset = 0;
83 while (size) {
Andy Yan20b09c22009-05-08 17:46:40 -040084 printk(KERN_DEBUG"%08X : ", baseaddr + offset);
Ke Wei8f261aa2008-02-23 21:15:27 +080085 if (size >= 16)
86 run = 16;
87 else
88 run = size;
89 size -= run;
90 for (i = 0; i < 16; i++) {
91 if (i < run)
Andy Yan20b09c22009-05-08 17:46:40 -040092 printk(KERN_DEBUG"%02X ", (u32)data[i]);
Ke Wei8f261aa2008-02-23 21:15:27 +080093 else
Andy Yan20b09c22009-05-08 17:46:40 -040094 printk(KERN_DEBUG" ");
Ke Wei8f261aa2008-02-23 21:15:27 +080095 }
Andy Yan20b09c22009-05-08 17:46:40 -040096 printk(KERN_DEBUG": ");
Ke Wei8f261aa2008-02-23 21:15:27 +080097 for (i = 0; i < run; i++)
Andy Yan20b09c22009-05-08 17:46:40 -040098 printk(KERN_DEBUG"%c",
99 isalnum(data[i]) ? data[i] : '.');
100 printk(KERN_DEBUG"\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800101 data = &data[16];
102 offset += run;
103 }
Andy Yan20b09c22009-05-08 17:46:40 -0400104 printk(KERN_DEBUG"\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800105}
106
Andy Yan20b09c22009-05-08 17:46:40 -0400107#if (_MV_DUMP > 1)
Ke Wei8f261aa2008-02-23 21:15:27 +0800108static void mvs_hba_sb_dump(struct mvs_info *mvi, u32 tag,
109 enum sas_protocol proto)
110{
Ke Wei8f261aa2008-02-23 21:15:27 +0800111 u32 offset;
Ke Wei8f261aa2008-02-23 21:15:27 +0800112 struct mvs_slot_info *slot = &mvi->slot_info[tag];
113
114 offset = slot->cmd_size + MVS_OAF_SZ +
Andy Yan20b09c22009-05-08 17:46:40 -0400115 MVS_CHIP_DISP->prd_size() * slot->n_elem;
116 dev_printk(KERN_DEBUG, mvi->dev, "+---->Status buffer[%d] :\n",
Ke Wei8f261aa2008-02-23 21:15:27 +0800117 tag);
118 mvs_hexdump(32, (u8 *) slot->response,
119 (u32) slot->buf_dma + offset);
Ke Wei8f261aa2008-02-23 21:15:27 +0800120}
Ke Weiee1f1c22008-03-27 14:53:47 +0800121#endif
Ke Wei8f261aa2008-02-23 21:15:27 +0800122
123static void mvs_hba_memory_dump(struct mvs_info *mvi, u32 tag,
124 enum sas_protocol proto)
125{
Andy Yan20b09c22009-05-08 17:46:40 -0400126#if (_MV_DUMP > 1)
Ke Weiee1f1c22008-03-27 14:53:47 +0800127 u32 sz, w_ptr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800128 u64 addr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800129 struct mvs_slot_info *slot = &mvi->slot_info[tag];
130
131 /*Delivery Queue */
Andy Yan20b09c22009-05-08 17:46:40 -0400132 sz = MVS_CHIP_SLOT_SZ;
Ke Weiee1f1c22008-03-27 14:53:47 +0800133 w_ptr = slot->tx;
Andy Yan20b09c22009-05-08 17:46:40 -0400134 addr = mvi->tx_dma;
135 dev_printk(KERN_DEBUG, mvi->dev,
Ke Weiee1f1c22008-03-27 14:53:47 +0800136 "Delivery Queue Size=%04d , WRT_PTR=%04X\n", sz, w_ptr);
Andy Yan20b09c22009-05-08 17:46:40 -0400137 dev_printk(KERN_DEBUG, mvi->dev,
Ke Wei8f261aa2008-02-23 21:15:27 +0800138 "Delivery Queue Base Address=0x%llX (PA)"
139 "(tx_dma=0x%llX), Entry=%04d\n",
Andy Yan20b09c22009-05-08 17:46:40 -0400140 addr, (unsigned long long)mvi->tx_dma, w_ptr);
Ke Wei8f261aa2008-02-23 21:15:27 +0800141 mvs_hexdump(sizeof(u32), (u8 *)(&mvi->tx[mvi->tx_prod]),
142 (u32) mvi->tx_dma + sizeof(u32) * w_ptr);
143 /*Command List */
Ke Weiee1f1c22008-03-27 14:53:47 +0800144 addr = mvi->slot_dma;
Andy Yan20b09c22009-05-08 17:46:40 -0400145 dev_printk(KERN_DEBUG, mvi->dev,
Ke Wei8f261aa2008-02-23 21:15:27 +0800146 "Command List Base Address=0x%llX (PA)"
147 "(slot_dma=0x%llX), Header=%03d\n",
Andy Yan20b09c22009-05-08 17:46:40 -0400148 addr, (unsigned long long)slot->buf_dma, tag);
149 dev_printk(KERN_DEBUG, mvi->dev, "Command Header[%03d]:\n", tag);
Ke Wei8f261aa2008-02-23 21:15:27 +0800150 /*mvs_cmd_hdr */
151 mvs_hexdump(sizeof(struct mvs_cmd_hdr), (u8 *)(&mvi->slot[tag]),
152 (u32) mvi->slot_dma + tag * sizeof(struct mvs_cmd_hdr));
153 /*1.command table area */
Andy Yan20b09c22009-05-08 17:46:40 -0400154 dev_printk(KERN_DEBUG, mvi->dev, "+---->Command Table :\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800155 mvs_hexdump(slot->cmd_size, (u8 *) slot->buf, (u32) slot->buf_dma);
156 /*2.open address frame area */
Andy Yan20b09c22009-05-08 17:46:40 -0400157 dev_printk(KERN_DEBUG, mvi->dev, "+---->Open Address Frame :\n");
Ke Wei8f261aa2008-02-23 21:15:27 +0800158 mvs_hexdump(MVS_OAF_SZ, (u8 *) slot->buf + slot->cmd_size,
159 (u32) slot->buf_dma + slot->cmd_size);
160 /*3.status buffer */
161 mvs_hba_sb_dump(mvi, tag, proto);
162 /*4.PRD table */
Andy Yan20b09c22009-05-08 17:46:40 -0400163 dev_printk(KERN_DEBUG, mvi->dev, "+---->PRD table :\n");
164 mvs_hexdump(MVS_CHIP_DISP->prd_size() * slot->n_elem,
Ke Wei8f261aa2008-02-23 21:15:27 +0800165 (u8 *) slot->buf + slot->cmd_size + MVS_OAF_SZ,
166 (u32) slot->buf_dma + slot->cmd_size + MVS_OAF_SZ);
167#endif
168}
169
170static void mvs_hba_cq_dump(struct mvs_info *mvi)
171{
Ke Weiee1f1c22008-03-27 14:53:47 +0800172#if (_MV_DUMP > 2)
Ke Wei8f261aa2008-02-23 21:15:27 +0800173 u64 addr;
174 void __iomem *regs = mvi->regs;
Ke Wei8f261aa2008-02-23 21:15:27 +0800175 u32 entry = mvi->rx_cons + 1;
176 u32 rx_desc = le32_to_cpu(mvi->rx[entry]);
177
178 /*Completion Queue */
179 addr = mr32(RX_HI) << 16 << 16 | mr32(RX_LO);
Andy Yan20b09c22009-05-08 17:46:40 -0400180 dev_printk(KERN_DEBUG, mvi->dev, "Completion Task = 0x%p\n",
Ke Weiee1f1c22008-03-27 14:53:47 +0800181 mvi->slot_info[rx_desc & RXQ_SLOT_MASK].task);
Andy Yan20b09c22009-05-08 17:46:40 -0400182 dev_printk(KERN_DEBUG, mvi->dev,
Ke Wei8f261aa2008-02-23 21:15:27 +0800183 "Completion List Base Address=0x%llX (PA), "
184 "CQ_Entry=%04d, CQ_WP=0x%08X\n",
185 addr, entry - 1, mvi->rx[0]);
186 mvs_hexdump(sizeof(u32), (u8 *)(&rx_desc),
187 mvi->rx_dma + sizeof(u32) * entry);
188#endif
189}
190
Andy Yan20b09c22009-05-08 17:46:40 -0400191void mvs_get_sas_addr(void *buf, u32 buflen)
Ke Wei8f261aa2008-02-23 21:15:27 +0800192{
Andy Yan20b09c22009-05-08 17:46:40 -0400193 /*memcpy(buf, "\x50\x05\x04\x30\x11\xab\x64\x40", 8);*/
194}
Ke Wei8f261aa2008-02-23 21:15:27 +0800195
Andy Yan20b09c22009-05-08 17:46:40 -0400196struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
197{
198 unsigned long i = 0, j = 0, hi = 0;
199 struct sas_ha_struct *sha = dev->port->ha;
200 struct mvs_info *mvi = NULL;
201 struct asd_sas_phy *phy;
Ke Wei8f261aa2008-02-23 21:15:27 +0800202
Andy Yan20b09c22009-05-08 17:46:40 -0400203 while (sha->sas_port[i]) {
204 if (sha->sas_port[i] == dev->port) {
205 phy = container_of(sha->sas_port[i]->phy_list.next,
206 struct asd_sas_phy, port_phy_el);
207 j = 0;
208 while (sha->sas_phy[j]) {
209 if (sha->sas_phy[j] == phy)
210 break;
211 j++;
Jeff Garzikb5762942007-10-25 20:58:22 -0400212 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400213 break;
Jeff Garzikb5762942007-10-25 20:58:22 -0400214 }
Andy Yan20b09c22009-05-08 17:46:40 -0400215 i++;
216 }
217 hi = j/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
218 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
219
220 return mvi;
221
222}
223
224/* FIXME */
225int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
226{
227 unsigned long i = 0, j = 0, n = 0, num = 0;
Andy Yan9870d9a2009-05-11 22:19:25 +0800228 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
229 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -0400230 struct sas_ha_struct *sha = dev->port->ha;
231
232 while (sha->sas_port[i]) {
233 if (sha->sas_port[i] == dev->port) {
234 struct asd_sas_phy *phy;
235 list_for_each_entry(phy,
236 &sha->sas_port[i]->phy_list, port_phy_el) {
237 j = 0;
238 while (sha->sas_phy[j]) {
239 if (sha->sas_phy[j] == phy)
240 break;
241 j++;
242 }
243 phyno[n] = (j >= mvi->chip->n_phy) ?
244 (j - mvi->chip->n_phy) : j;
245 num++;
246 n++;
247 }
248 break;
249 }
250 i++;
251 }
252 return num;
253}
254
255static inline void mvs_free_reg_set(struct mvs_info *mvi,
256 struct mvs_device *dev)
257{
258 if (!dev) {
259 mv_printk("device has been free.\n");
260 return;
261 }
262 if (dev->runing_req != 0)
263 return;
264 if (dev->taskfileset == MVS_ID_NOT_MAPPED)
265 return;
266 MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
267}
268
269static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
270 struct mvs_device *dev)
271{
272 if (dev->taskfileset != MVS_ID_NOT_MAPPED)
273 return 0;
274 return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
275}
276
277void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
278{
279 u32 no;
280 for_each_phy(phy_mask, phy_mask, no) {
281 if (!(phy_mask & 1))
282 continue;
283 MVS_CHIP_DISP->phy_reset(mvi, no, hard);
284 }
285}
286
287/* FIXME: locking? */
288int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
289 void *funcdata)
290{
291 int rc = 0, phy_id = sas_phy->id;
292 u32 tmp, i = 0, hi;
293 struct sas_ha_struct *sha = sas_phy->ha;
294 struct mvs_info *mvi = NULL;
295
296 while (sha->sas_phy[i]) {
297 if (sha->sas_phy[i] == sas_phy)
298 break;
299 i++;
300 }
301 hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
302 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
303
304 switch (func) {
305 case PHY_FUNC_SET_LINK_RATE:
306 MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
307 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400308
309 case PHY_FUNC_HARD_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400310 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400311 if (tmp & PHY_RST_HARD)
312 break;
Andy Yan20b09c22009-05-08 17:46:40 -0400313 MVS_CHIP_DISP->phy_reset(mvi, phy_id, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400314 break;
315
316 case PHY_FUNC_LINK_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400317 MVS_CHIP_DISP->phy_enable(mvi, phy_id);
318 MVS_CHIP_DISP->phy_reset(mvi, phy_id, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400319 break;
320
321 case PHY_FUNC_DISABLE:
Andy Yan20b09c22009-05-08 17:46:40 -0400322 MVS_CHIP_DISP->phy_disable(mvi, phy_id);
323 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400324 case PHY_FUNC_RELEASE_SPINUP_HOLD:
325 default:
326 rc = -EOPNOTSUPP;
Jeff Garzikb5762942007-10-25 20:58:22 -0400327 }
Andy Yan20b09c22009-05-08 17:46:40 -0400328 msleep(200);
Jeff Garzikb5762942007-10-25 20:58:22 -0400329 return rc;
330}
331
Andy Yan20b09c22009-05-08 17:46:40 -0400332void __devinit mvs_set_sas_addr(struct mvs_info *mvi, int port_id,
333 u32 off_lo, u32 off_hi, u64 sas_addr)
334{
335 u32 lo = (u32)sas_addr;
336 u32 hi = (u32)(sas_addr>>32);
337
338 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_lo);
339 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, lo);
340 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_hi);
341 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, hi);
342}
343
Ke Wei8f261aa2008-02-23 21:15:27 +0800344static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
345{
346 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -0400347 struct asd_sas_phy *sas_phy = &phy->sas_phy;
348 struct sas_ha_struct *sas_ha;
Ke Wei8f261aa2008-02-23 21:15:27 +0800349 if (!phy->phy_attached)
350 return;
351
Andy Yan20b09c22009-05-08 17:46:40 -0400352 if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
353 && phy->phy_type & PORT_TYPE_SAS) {
354 return;
355 }
356
357 sas_ha = mvi->sas;
358 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
359
Ke Weiee1f1c22008-03-27 14:53:47 +0800360 if (sas_phy->phy) {
361 struct sas_phy *sphy = sas_phy->phy;
362
363 sphy->negotiated_linkrate = sas_phy->linkrate;
364 sphy->minimum_linkrate = phy->minimum_linkrate;
365 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
366 sphy->maximum_linkrate = phy->maximum_linkrate;
Andy Yan20b09c22009-05-08 17:46:40 -0400367 sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
Ke Weiee1f1c22008-03-27 14:53:47 +0800368 }
369
Ke Wei8f261aa2008-02-23 21:15:27 +0800370 if (phy->phy_type & PORT_TYPE_SAS) {
371 struct sas_identify_frame *id;
372
373 id = (struct sas_identify_frame *)phy->frame_rcvd;
374 id->dev_type = phy->identify.device_type;
375 id->initiator_bits = SAS_PROTOCOL_ALL;
376 id->target_bits = phy->identify.target_port_protocols;
377 } else if (phy->phy_type & PORT_TYPE_SATA) {
Andy Yan20b09c22009-05-08 17:46:40 -0400378 /*Nothing*/
Ke Wei8f261aa2008-02-23 21:15:27 +0800379 }
Andy Yan20b09c22009-05-08 17:46:40 -0400380 mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
381
382 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
383
384 mvi->sas->notify_port_event(sas_phy,
Ke Wei8f261aa2008-02-23 21:15:27 +0800385 PORTE_BYTES_DMAED);
386}
387
Andy Yan20b09c22009-05-08 17:46:40 -0400388int mvs_slave_alloc(struct scsi_device *scsi_dev)
389{
390 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
391 if (dev_is_sata(dev)) {
392 /* We don't need to rescan targets
393 * if REPORT_LUNS request is failed
394 */
395 if (scsi_dev->lun > 0)
396 return -ENXIO;
397 scsi_dev->tagged_supported = 1;
398 }
399
400 return sas_slave_alloc(scsi_dev);
401}
402
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400403int mvs_slave_configure(struct scsi_device *sdev)
Ke Wei8f261aa2008-02-23 21:15:27 +0800404{
Ke Weiee1f1c22008-03-27 14:53:47 +0800405 struct domain_device *dev = sdev_to_domain_dev(sdev);
406 int ret = sas_slave_configure(sdev);
Ke Wei8f261aa2008-02-23 21:15:27 +0800407
Ke Weiee1f1c22008-03-27 14:53:47 +0800408 if (ret)
409 return ret;
Ke Weiee1f1c22008-03-27 14:53:47 +0800410 if (dev_is_sata(dev)) {
Andy Yan20b09c22009-05-08 17:46:40 -0400411 /* may set PIO mode */
412 #if MV_DISABLE_NCQ
413 struct ata_port *ap = dev->sata_dev.ap;
414 struct ata_device *adev = ap->link.device;
415 adev->flags |= ATA_DFLAG_NCQ_OFF;
Ke Weiee1f1c22008-03-27 14:53:47 +0800416 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, 1);
Andy Yan20b09c22009-05-08 17:46:40 -0400417 #endif
Ke Weiee1f1c22008-03-27 14:53:47 +0800418 }
419 return 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400420}
421
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400422void mvs_scan_start(struct Scsi_Host *shost)
Jeff Garzikb5762942007-10-25 20:58:22 -0400423{
Andy Yan20b09c22009-05-08 17:46:40 -0400424 int i, j;
425 unsigned short core_nr;
426 struct mvs_info *mvi;
427 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
Jeff Garzikb5762942007-10-25 20:58:22 -0400428
Andy Yan20b09c22009-05-08 17:46:40 -0400429 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
430
431 for (j = 0; j < core_nr; j++) {
432 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
433 for (i = 0; i < mvi->chip->n_phy; ++i)
434 mvs_bytes_dmaed(mvi, i);
Jeff Garzikb5762942007-10-25 20:58:22 -0400435 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400436}
437
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400438int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
Jeff Garzikb5762942007-10-25 20:58:22 -0400439{
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400440 /* give the phy enabling interrupt event time to come in (1s
441 * is empirically about all it takes) */
442 if (time < HZ)
Ke Wei8f261aa2008-02-23 21:15:27 +0800443 return 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400444 /* Wait for discovery to finish */
445 scsi_flush_work(shost);
446 return 1;
Jeff Garzikb5762942007-10-25 20:58:22 -0400447}
448
Ke Wei8f261aa2008-02-23 21:15:27 +0800449static int mvs_task_prep_smp(struct mvs_info *mvi,
450 struct mvs_task_exec_info *tei)
Jeff Garzikb5762942007-10-25 20:58:22 -0400451{
Ke Wei8f261aa2008-02-23 21:15:27 +0800452 int elem, rc, i;
453 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400454 struct mvs_cmd_hdr *hdr = tei->hdr;
Andy Yan20b09c22009-05-08 17:46:40 -0400455 struct domain_device *dev = task->dev;
456 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400457 struct scatterlist *sg_req, *sg_resp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800458 u32 req_len, resp_len, tag = tei->tag;
459 void *buf_tmp;
460 u8 *buf_oaf;
461 dma_addr_t buf_tmp_dma;
Andy Yan20b09c22009-05-08 17:46:40 -0400462 void *buf_prd;
Ke Wei8f261aa2008-02-23 21:15:27 +0800463 struct mvs_slot_info *slot = &mvi->slot_info[tag];
Ke Wei8f261aa2008-02-23 21:15:27 +0800464 u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
465#if _MV_DUMP
466 u8 *buf_cmd;
467 void *from;
468#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400469 /*
470 * DMA-map SMP request, response buffers
471 */
Ke Wei8f261aa2008-02-23 21:15:27 +0800472 sg_req = &task->smp_task.smp_req;
Andy Yan20b09c22009-05-08 17:46:40 -0400473 elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400474 if (!elem)
475 return -ENOMEM;
476 req_len = sg_dma_len(sg_req);
477
Ke Wei8f261aa2008-02-23 21:15:27 +0800478 sg_resp = &task->smp_task.smp_resp;
Andy Yan20b09c22009-05-08 17:46:40 -0400479 elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400480 if (!elem) {
481 rc = -ENOMEM;
482 goto err_out;
483 }
Andy Yan20b09c22009-05-08 17:46:40 -0400484 resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400485
486 /* must be in dwords */
487 if ((req_len & 0x3) || (resp_len & 0x3)) {
488 rc = -EINVAL;
489 goto err_out_2;
490 }
491
492 /*
Ke Wei8f261aa2008-02-23 21:15:27 +0800493 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
Jeff Garzikb5762942007-10-25 20:58:22 -0400494 */
495
Andy Yan20b09c22009-05-08 17:46:40 -0400496 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800497 buf_tmp = slot->buf;
498 buf_tmp_dma = slot->buf_dma;
Jeff Garzikb5762942007-10-25 20:58:22 -0400499
Ke Wei8f261aa2008-02-23 21:15:27 +0800500#if _MV_DUMP
501 buf_cmd = buf_tmp;
502 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
503 buf_tmp += req_len;
504 buf_tmp_dma += req_len;
505 slot->cmd_size = req_len;
506#else
507 hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
508#endif
509
510 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
511 buf_oaf = buf_tmp;
512 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
513
514 buf_tmp += MVS_OAF_SZ;
515 buf_tmp_dma += MVS_OAF_SZ;
516
Andy Yan20b09c22009-05-08 17:46:40 -0400517 /* region 3: PRD table *********************************** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800518 buf_prd = buf_tmp;
519 if (tei->n_elem)
520 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
521 else
522 hdr->prd_tbl = 0;
523
Andy Yan20b09c22009-05-08 17:46:40 -0400524 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Ke Wei8f261aa2008-02-23 21:15:27 +0800525 buf_tmp += i;
526 buf_tmp_dma += i;
527
528 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
529 slot->response = buf_tmp;
530 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400531 if (mvi->flags & MVF_FLAG_SOC)
532 hdr->reserved[0] = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800533
534 /*
535 * Fill in TX ring and command slot header
536 */
537 slot->tx = mvi->tx_prod;
538 mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
539 TXQ_MODE_I | tag |
540 (sas_port->phy_mask << TXQ_PHY_SHIFT));
541
542 hdr->flags |= flags;
543 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
Jeff Garzikb5762942007-10-25 20:58:22 -0400544 hdr->tags = cpu_to_le32(tag);
545 hdr->data_len = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400546
Ke Wei8f261aa2008-02-23 21:15:27 +0800547 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400548 /* initiator, SMP, ftype 1h */
549 buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
550 buf_oaf[1] = dev->linkrate & 0xf;
Ke Wei8f261aa2008-02-23 21:15:27 +0800551 *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
Andy Yan20b09c22009-05-08 17:46:40 -0400552 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Ke Wei8f261aa2008-02-23 21:15:27 +0800553
554 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400555 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Ke Wei8f261aa2008-02-23 21:15:27 +0800556
557#if _MV_DUMP
558 /* copy cmd table */
559 from = kmap_atomic(sg_page(sg_req), KM_IRQ0);
560 memcpy(buf_cmd, from + sg_req->offset, req_len);
561 kunmap_atomic(from, KM_IRQ0);
562#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400563 return 0;
564
565err_out_2:
Andy Yan20b09c22009-05-08 17:46:40 -0400566 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400567 PCI_DMA_FROMDEVICE);
568err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400569 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400570 PCI_DMA_TODEVICE);
571 return rc;
572}
573
Ke Wei4e52fc02008-03-27 14:54:50 +0800574static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
Ke Wei8f261aa2008-02-23 21:15:27 +0800575{
Ke Wei8f261aa2008-02-23 21:15:27 +0800576 struct ata_queued_cmd *qc = task->uldd_task;
577
Ke Wei4e52fc02008-03-27 14:54:50 +0800578 if (qc) {
579 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
580 qc->tf.command == ATA_CMD_FPDMA_READ) {
581 *tag = qc->tag;
582 return 1;
583 }
584 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800585
Ke Wei4e52fc02008-03-27 14:54:50 +0800586 return 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800587}
588
Jeff Garzikb5762942007-10-25 20:58:22 -0400589static int mvs_task_prep_ata(struct mvs_info *mvi,
590 struct mvs_task_exec_info *tei)
591{
592 struct sas_task *task = tei->task;
593 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400594 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400595 struct mvs_cmd_hdr *hdr = tei->hdr;
596 struct asd_sas_port *sas_port = dev->port;
Ke Wei8f261aa2008-02-23 21:15:27 +0800597 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400598 void *buf_prd;
599 u32 tag = tei->tag, hdr_tag;
600 u32 flags, del_q;
Jeff Garzikb5762942007-10-25 20:58:22 -0400601 void *buf_tmp;
602 u8 *buf_cmd, *buf_oaf;
603 dma_addr_t buf_tmp_dma;
Ke Wei8f261aa2008-02-23 21:15:27 +0800604 u32 i, req_len, resp_len;
605 const u32 max_resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400606
Andy Yan20b09c22009-05-08 17:46:40 -0400607 if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
608 mv_dprintk("Have not enough regiset for dev %d.\n",
609 mvi_dev->device_id);
Ke Wei8f261aa2008-02-23 21:15:27 +0800610 return -EBUSY;
Andy Yan20b09c22009-05-08 17:46:40 -0400611 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800612 slot = &mvi->slot_info[tag];
613 slot->tx = mvi->tx_prod;
Andy Yan20b09c22009-05-08 17:46:40 -0400614 del_q = TXQ_MODE_I | tag |
615 (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
616 (sas_port->phy_mask << TXQ_PHY_SHIFT) |
617 (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
618 mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
Jeff Garzikb5762942007-10-25 20:58:22 -0400619
Andy Yan20b09c22009-05-08 17:46:40 -0400620#ifndef DISABLE_HOTPLUG_DMA_FIX
621 if (task->data_dir == DMA_FROM_DEVICE)
622 flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
623 else
624 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
625#else
626 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
627#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400628 if (task->ata_task.use_ncq)
629 flags |= MCH_FPDMA;
Ke Wei8f261aa2008-02-23 21:15:27 +0800630 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
631 if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
632 flags |= MCH_ATAPI;
633 }
634
Jeff Garzikb5762942007-10-25 20:58:22 -0400635 /* FIXME: fill in port multiplier number */
636
637 hdr->flags = cpu_to_le32(flags);
Ke Wei8f261aa2008-02-23 21:15:27 +0800638
639 /* FIXME: the low order order 5 bits for the TAG if enable NCQ */
Andy Yan20b09c22009-05-08 17:46:40 -0400640 if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
641 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
Ke Wei4e52fc02008-03-27 14:54:50 +0800642 else
Andy Yan20b09c22009-05-08 17:46:40 -0400643 hdr_tag = tag;
644
645 hdr->tags = cpu_to_le32(hdr_tag);
646
Jeff Garzikb5762942007-10-25 20:58:22 -0400647 hdr->data_len = cpu_to_le32(task->total_xfer_len);
648
649 /*
650 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
651 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400652
Ke Wei8f261aa2008-02-23 21:15:27 +0800653 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
654 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400655 buf_tmp_dma = slot->buf_dma;
656
657 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
658
659 buf_tmp += MVS_ATA_CMD_SZ;
660 buf_tmp_dma += MVS_ATA_CMD_SZ;
Ke Wei8f261aa2008-02-23 21:15:27 +0800661#if _MV_DUMP
662 slot->cmd_size = MVS_ATA_CMD_SZ;
663#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400664
Ke Wei8f261aa2008-02-23 21:15:27 +0800665 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400666 /* used for STP. unused for SATA? */
667 buf_oaf = buf_tmp;
668 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
669
670 buf_tmp += MVS_OAF_SZ;
671 buf_tmp_dma += MVS_OAF_SZ;
672
Ke Wei8f261aa2008-02-23 21:15:27 +0800673 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400674 buf_prd = buf_tmp;
Andy Yan20b09c22009-05-08 17:46:40 -0400675
Ke Wei8f261aa2008-02-23 21:15:27 +0800676 if (tei->n_elem)
677 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
678 else
679 hdr->prd_tbl = 0;
Andy Yan20b09c22009-05-08 17:46:40 -0400680 i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
Jeff Garzikb5762942007-10-25 20:58:22 -0400681
Jeff Garzikb5762942007-10-25 20:58:22 -0400682 buf_tmp += i;
683 buf_tmp_dma += i;
684
Ke Wei8f261aa2008-02-23 21:15:27 +0800685 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400686 /* FIXME: probably unused, for SATA. kept here just in case
687 * we get a STP/SATA error information record
688 */
689 slot->response = buf_tmp;
690 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400691 if (mvi->flags & MVF_FLAG_SOC)
692 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400693
Ke Wei8f261aa2008-02-23 21:15:27 +0800694 req_len = sizeof(struct host_to_dev_fis);
Jeff Garzikb5762942007-10-25 20:58:22 -0400695 resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800696 sizeof(struct mvs_err_info) - i;
Jeff Garzikb5762942007-10-25 20:58:22 -0400697
698 /* request, response lengths */
Ke Wei8f261aa2008-02-23 21:15:27 +0800699 resp_len = min(resp_len, max_resp_len);
Jeff Garzikb5762942007-10-25 20:58:22 -0400700 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
701
Andy Yan20b09c22009-05-08 17:46:40 -0400702 if (likely(!task->ata_task.device_control_reg_update))
703 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
Jeff Garzikb5762942007-10-25 20:58:22 -0400704 /* fill in command FIS and ATAPI CDB */
Ke Wei8f261aa2008-02-23 21:15:27 +0800705 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
706 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
707 memcpy(buf_cmd + STP_ATAPI_CMD,
708 task->ata_task.atapi_packet, 16);
709
710 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400711 /* initiator, STP, ftype 1h */
712 buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
713 buf_oaf[1] = dev->linkrate & 0xf;
714 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
715 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400716
717 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400718 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
719#ifndef DISABLE_HOTPLUG_DMA_FIX
720 if (task->data_dir == DMA_FROM_DEVICE)
721 MVS_CHIP_DISP->dma_fix(mvi->bulk_buffer_dma,
722 TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
723#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400724 return 0;
725}
726
727static int mvs_task_prep_ssp(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -0400728 struct mvs_task_exec_info *tei, int is_tmf,
729 struct mvs_tmf_task *tmf)
Jeff Garzikb5762942007-10-25 20:58:22 -0400730{
731 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400732 struct mvs_cmd_hdr *hdr = tei->hdr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800733 struct mvs_port *port = tei->port;
Andy Yan20b09c22009-05-08 17:46:40 -0400734 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400735 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -0400736 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400737 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400738 void *buf_prd;
Jeff Garzikb5762942007-10-25 20:58:22 -0400739 struct ssp_frame_hdr *ssp_hdr;
740 void *buf_tmp;
741 u8 *buf_cmd, *buf_oaf, fburst = 0;
742 dma_addr_t buf_tmp_dma;
743 u32 flags;
Ke Wei8f261aa2008-02-23 21:15:27 +0800744 u32 resp_len, req_len, i, tag = tei->tag;
745 const u32 max_resp_len = SB_RFB_MAX;
Andy Yan20b09c22009-05-08 17:46:40 -0400746 u32 phy_mask;
Jeff Garzikb5762942007-10-25 20:58:22 -0400747
748 slot = &mvi->slot_info[tag];
749
Andy Yan20b09c22009-05-08 17:46:40 -0400750 phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
751 sas_port->phy_mask) & TXQ_PHY_MASK;
752
Ke Wei8f261aa2008-02-23 21:15:27 +0800753 slot->tx = mvi->tx_prod;
754 mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
755 (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
Ke Wei4e52fc02008-03-27 14:54:50 +0800756 (phy_mask << TXQ_PHY_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400757
758 flags = MCH_RETRY;
759 if (task->ssp_task.enable_first_burst) {
760 flags |= MCH_FBURST;
761 fburst = (1 << 7);
762 }
Andy Yan2b288132009-05-11 20:01:55 +0800763 if (is_tmf)
764 flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
765 else
766 flags |= (MCH_SSP_FR_CMD << MCH_SSP_FR_TYPE_SHIFT);
767 hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400768 hdr->tags = cpu_to_le32(tag);
769 hdr->data_len = cpu_to_le32(task->total_xfer_len);
770
771 /*
772 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
773 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400774
Ke Wei8f261aa2008-02-23 21:15:27 +0800775 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
776 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400777 buf_tmp_dma = slot->buf_dma;
778
779 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
780
781 buf_tmp += MVS_SSP_CMD_SZ;
782 buf_tmp_dma += MVS_SSP_CMD_SZ;
Ke Wei8f261aa2008-02-23 21:15:27 +0800783#if _MV_DUMP
784 slot->cmd_size = MVS_SSP_CMD_SZ;
785#endif
Jeff Garzikb5762942007-10-25 20:58:22 -0400786
Ke Wei8f261aa2008-02-23 21:15:27 +0800787 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400788 buf_oaf = buf_tmp;
789 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
790
791 buf_tmp += MVS_OAF_SZ;
792 buf_tmp_dma += MVS_OAF_SZ;
793
Ke Wei8f261aa2008-02-23 21:15:27 +0800794 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400795 buf_prd = buf_tmp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800796 if (tei->n_elem)
797 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
798 else
799 hdr->prd_tbl = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400800
Andy Yan20b09c22009-05-08 17:46:40 -0400801 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Jeff Garzikb5762942007-10-25 20:58:22 -0400802 buf_tmp += i;
803 buf_tmp_dma += i;
804
Ke Wei8f261aa2008-02-23 21:15:27 +0800805 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400806 slot->response = buf_tmp;
807 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400808 if (mvi->flags & MVF_FLAG_SOC)
809 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400810
Jeff Garzikb5762942007-10-25 20:58:22 -0400811 resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800812 sizeof(struct mvs_err_info) - i;
813 resp_len = min(resp_len, max_resp_len);
814
815 req_len = sizeof(struct ssp_frame_hdr) + 28;
Jeff Garzikb5762942007-10-25 20:58:22 -0400816
817 /* request, response lengths */
818 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
819
820 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400821 /* initiator, SSP, ftype 1h */
822 buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
823 buf_oaf[1] = dev->linkrate & 0xf;
824 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
825 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400826
Ke Wei8f261aa2008-02-23 21:15:27 +0800827 /* fill in SSP frame header (Command Table.SSP frame header) */
828 ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
Andy Yan20b09c22009-05-08 17:46:40 -0400829
830 if (is_tmf)
831 ssp_hdr->frame_type = SSP_TASK;
832 else
833 ssp_hdr->frame_type = SSP_COMMAND;
834
835 memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
Jeff Garzikb5762942007-10-25 20:58:22 -0400836 HASHED_SAS_ADDR_SIZE);
837 memcpy(ssp_hdr->hashed_src_addr,
Andy Yan20b09c22009-05-08 17:46:40 -0400838 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400839 ssp_hdr->tag = cpu_to_be16(tag);
840
Andy Yan20b09c22009-05-08 17:46:40 -0400841 /* fill in IU for TASK and Command Frame */
Jeff Garzikb5762942007-10-25 20:58:22 -0400842 buf_cmd += sizeof(*ssp_hdr);
843 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
Jeff Garzikb5762942007-10-25 20:58:22 -0400844
Andy Yan20b09c22009-05-08 17:46:40 -0400845 if (ssp_hdr->frame_type != SSP_TASK) {
846 buf_cmd[9] = fburst | task->ssp_task.task_attr |
847 (task->ssp_task.task_prio << 3);
848 memcpy(buf_cmd + 12, &task->ssp_task.cdb, 16);
849 } else{
850 buf_cmd[10] = tmf->tmf;
851 switch (tmf->tmf) {
852 case TMF_ABORT_TASK:
853 case TMF_QUERY_TASK:
854 buf_cmd[12] =
855 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
856 buf_cmd[13] =
857 tmf->tag_of_task_to_be_managed & 0xff;
858 break;
859 default:
860 break;
861 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400862 }
Andy Yan20b09c22009-05-08 17:46:40 -0400863 /* fill in PRD (scatter/gather) table, if any */
864 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Jeff Garzikb5762942007-10-25 20:58:22 -0400865 return 0;
866}
867
Andy Yan20b09c22009-05-08 17:46:40 -0400868#define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == NO_DEVICE)))
869static int mvs_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
Andy Yan0b84b702009-05-11 20:05:26 +0800870 struct completion *completion,int is_tmf,
871 struct mvs_tmf_task *tmf)
Jeff Garzikb5762942007-10-25 20:58:22 -0400872{
Ke Wei8f261aa2008-02-23 21:15:27 +0800873 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +0800874 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
875 struct mvs_info *mvi = mvi_dev->mvi_info;
Jeff Garzikb5762942007-10-25 20:58:22 -0400876 struct mvs_task_exec_info tei;
Ke Wei8f261aa2008-02-23 21:15:27 +0800877 struct sas_task *t = task;
Ke Wei4e52fc02008-03-27 14:54:50 +0800878 struct mvs_slot_info *slot;
Ke Wei8f261aa2008-02-23 21:15:27 +0800879 u32 tag = 0xdeadbeef, rc, n_elem = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800880 u32 n = num, pass = 0;
Andy Yan20b09c22009-05-08 17:46:40 -0400881 unsigned long flags = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400882
Andy Yan20b09c22009-05-08 17:46:40 -0400883 if (!dev->port) {
884 struct task_status_struct *tsm = &t->task_status;
885
886 tsm->resp = SAS_TASK_UNDELIVERED;
887 tsm->stat = SAS_PHY_DOWN;
888 t->task_done(t);
889 return 0;
890 }
891
Andy Yan0b84b702009-05-11 20:05:26 +0800892 spin_lock_irqsave(&mvi->lock, flags);
Ke Wei8f261aa2008-02-23 21:15:27 +0800893 do {
Ke Wei4e52fc02008-03-27 14:54:50 +0800894 dev = t->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400895 mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -0400896 if (DEV_IS_GONE(mvi_dev)) {
897 if (mvi_dev)
898 mv_dprintk("device %d not ready.\n",
899 mvi_dev->device_id);
900 else
901 mv_dprintk("device %016llx not ready.\n",
902 SAS_ADDR(dev->sas_addr));
903
904 rc = SAS_PHY_DOWN;
905 goto out_done;
906 }
907
908 if (dev->port->id >= mvi->chip->n_phy)
909 tei.port = &mvi->port[dev->port->id - mvi->chip->n_phy];
910 else
911 tei.port = &mvi->port[dev->port->id];
Jeff Garzikb5762942007-10-25 20:58:22 -0400912
Ke Wei8f261aa2008-02-23 21:15:27 +0800913 if (!tei.port->port_attached) {
Ke Wei4e52fc02008-03-27 14:54:50 +0800914 if (sas_protocol_ata(t->task_proto)) {
Andy Yan20b09c22009-05-08 17:46:40 -0400915 mv_dprintk("port %d does not"
916 "attached device.\n", dev->port->id);
Ke Wei4e52fc02008-03-27 14:54:50 +0800917 rc = SAS_PHY_DOWN;
918 goto out_done;
919 } else {
920 struct task_status_struct *ts = &t->task_status;
921 ts->resp = SAS_TASK_UNDELIVERED;
922 ts->stat = SAS_PHY_DOWN;
923 t->task_done(t);
924 if (n > 1)
925 t = list_entry(t->list.next,
926 struct sas_task, list);
927 continue;
928 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800929 }
Ke Wei4e52fc02008-03-27 14:54:50 +0800930
Ke Wei8f261aa2008-02-23 21:15:27 +0800931 if (!sas_protocol_ata(t->task_proto)) {
932 if (t->num_scatter) {
Andy Yan20b09c22009-05-08 17:46:40 -0400933 n_elem = dma_map_sg(mvi->dev,
934 t->scatter,
Ke Wei8f261aa2008-02-23 21:15:27 +0800935 t->num_scatter,
936 t->data_dir);
937 if (!n_elem) {
938 rc = -ENOMEM;
939 goto err_out;
940 }
941 }
942 } else {
943 n_elem = t->num_scatter;
944 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400945
Ke Wei8f261aa2008-02-23 21:15:27 +0800946 rc = mvs_tag_alloc(mvi, &tag);
947 if (rc)
948 goto err_out;
Jeff Garzikb5762942007-10-25 20:58:22 -0400949
Ke Wei4e52fc02008-03-27 14:54:50 +0800950 slot = &mvi->slot_info[tag];
Andy Yan20b09c22009-05-08 17:46:40 -0400951
952
Ke Wei4e52fc02008-03-27 14:54:50 +0800953 t->lldd_task = NULL;
954 slot->n_elem = n_elem;
Andy Yan20b09c22009-05-08 17:46:40 -0400955 slot->slot_tag = tag;
Ke Wei4e52fc02008-03-27 14:54:50 +0800956 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
Andy Yan20b09c22009-05-08 17:46:40 -0400957
Ke Wei8f261aa2008-02-23 21:15:27 +0800958 tei.task = t;
959 tei.hdr = &mvi->slot[tag];
960 tei.tag = tag;
961 tei.n_elem = n_elem;
Ke Wei8f261aa2008-02-23 21:15:27 +0800962 switch (t->task_proto) {
963 case SAS_PROTOCOL_SMP:
964 rc = mvs_task_prep_smp(mvi, &tei);
965 break;
966 case SAS_PROTOCOL_SSP:
Andy Yan20b09c22009-05-08 17:46:40 -0400967 rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
Ke Wei8f261aa2008-02-23 21:15:27 +0800968 break;
969 case SAS_PROTOCOL_SATA:
970 case SAS_PROTOCOL_STP:
971 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
972 rc = mvs_task_prep_ata(mvi, &tei);
973 break;
974 default:
Andy Yan20b09c22009-05-08 17:46:40 -0400975 dev_printk(KERN_ERR, mvi->dev,
Ke Wei8f261aa2008-02-23 21:15:27 +0800976 "unknown sas_task proto: 0x%x\n",
977 t->task_proto);
978 rc = -EINVAL;
979 break;
980 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400981
Andy Yan20b09c22009-05-08 17:46:40 -0400982 if (rc) {
983 mv_dprintk("rc is %x\n", rc);
Ke Wei8f261aa2008-02-23 21:15:27 +0800984 goto err_out_tag;
Andy Yan20b09c22009-05-08 17:46:40 -0400985 }
Ke Wei4e52fc02008-03-27 14:54:50 +0800986 slot->task = t;
987 slot->port = tei.port;
Andy Yanf9da3be2009-05-14 20:41:21 -0400988 t->lldd_task = slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400989 list_add_tail(&slot->entry, &tei.port->list);
Ke Wei8f261aa2008-02-23 21:15:27 +0800990 /* TODO: select normal or high priority */
Ke Wei8f261aa2008-02-23 21:15:27 +0800991 spin_lock(&t->task_state_lock);
992 t->task_state_flags |= SAS_TASK_AT_INITIATOR;
993 spin_unlock(&t->task_state_lock);
Jeff Garzikb5762942007-10-25 20:58:22 -0400994
Ke Wei8f261aa2008-02-23 21:15:27 +0800995 mvs_hba_memory_dump(mvi, tag, t->task_proto);
Andy Yan20b09c22009-05-08 17:46:40 -0400996 mvi_dev->runing_req++;
Ke Wei8f261aa2008-02-23 21:15:27 +0800997 ++pass;
998 mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
Ke Wei4e52fc02008-03-27 14:54:50 +0800999 if (n > 1)
1000 t = list_entry(t->list.next, struct sas_task, list);
Ke Wei8f261aa2008-02-23 21:15:27 +08001001 } while (--n);
Ke Wei4e52fc02008-03-27 14:54:50 +08001002 rc = 0;
1003 goto out_done;
Jeff Garzikb5762942007-10-25 20:58:22 -04001004
1005err_out_tag:
Ke Wei8f261aa2008-02-23 21:15:27 +08001006 mvs_tag_free(mvi, tag);
Jeff Garzikb5762942007-10-25 20:58:22 -04001007err_out:
Andy Yan20b09c22009-05-08 17:46:40 -04001008
1009 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
Ke Wei8f261aa2008-02-23 21:15:27 +08001010 if (!sas_protocol_ata(t->task_proto))
1011 if (n_elem)
Andy Yan20b09c22009-05-08 17:46:40 -04001012 dma_unmap_sg(mvi->dev, t->scatter, n_elem,
Ke Wei8f261aa2008-02-23 21:15:27 +08001013 t->data_dir);
Ke Wei4e52fc02008-03-27 14:54:50 +08001014out_done:
Andy Yan20b09c22009-05-08 17:46:40 -04001015 if (likely(pass)) {
1016 MVS_CHIP_DISP->start_delivery(mvi,
1017 (mvi->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
1018 }
Andy Yan0b84b702009-05-11 20:05:26 +08001019 spin_unlock_irqrestore(&mvi->lock, flags);
Jeff Garzikb5762942007-10-25 20:58:22 -04001020 return rc;
1021}
1022
Andy Yan20b09c22009-05-08 17:46:40 -04001023int mvs_queue_command(struct sas_task *task, const int num,
1024 gfp_t gfp_flags)
1025{
Andy Yan0b84b702009-05-11 20:05:26 +08001026 return mvs_task_exec(task, num, gfp_flags, NULL, 0, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04001027}
1028
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001029static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
Ke Wei8f261aa2008-02-23 21:15:27 +08001030{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001031 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1032 mvs_tag_clear(mvi, slot_idx);
1033}
Ke Wei8f261aa2008-02-23 21:15:27 +08001034
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001035static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
1036 struct mvs_slot_info *slot, u32 slot_idx)
1037{
Andy Yan20b09c22009-05-08 17:46:40 -04001038 if (!slot->task)
1039 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001040 if (!sas_protocol_ata(task->task_proto))
1041 if (slot->n_elem)
Andy Yan20b09c22009-05-08 17:46:40 -04001042 dma_unmap_sg(mvi->dev, task->scatter,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001043 slot->n_elem, task->data_dir);
Ke Wei8f261aa2008-02-23 21:15:27 +08001044
Ke Wei8f261aa2008-02-23 21:15:27 +08001045 switch (task->task_proto) {
1046 case SAS_PROTOCOL_SMP:
Andy Yan20b09c22009-05-08 17:46:40 -04001047 dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001048 PCI_DMA_FROMDEVICE);
Andy Yan20b09c22009-05-08 17:46:40 -04001049 dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001050 PCI_DMA_TODEVICE);
Ke Wei8f261aa2008-02-23 21:15:27 +08001051 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001052
Ke Wei8f261aa2008-02-23 21:15:27 +08001053 case SAS_PROTOCOL_SATA:
1054 case SAS_PROTOCOL_STP:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001055 case SAS_PROTOCOL_SSP:
Ke Wei8f261aa2008-02-23 21:15:27 +08001056 default:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001057 /* do nothing */
Ke Wei8f261aa2008-02-23 21:15:27 +08001058 break;
1059 }
Andy Yan20b09c22009-05-08 17:46:40 -04001060 list_del_init(&slot->entry);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001061 task->lldd_task = NULL;
1062 slot->task = NULL;
1063 slot->port = NULL;
Andy Yan20b09c22009-05-08 17:46:40 -04001064 slot->slot_tag = 0xFFFFFFFF;
1065 mvs_slot_free(mvi, slot_idx);
Ke Wei8f261aa2008-02-23 21:15:27 +08001066}
1067
1068static void mvs_update_wideport(struct mvs_info *mvi, int i)
1069{
1070 struct mvs_phy *phy = &mvi->phy[i];
1071 struct mvs_port *port = phy->port;
1072 int j, no;
1073
Andy Yan20b09c22009-05-08 17:46:40 -04001074 for_each_phy(port->wide_port_phymap, j, no) {
1075 if (j & 1) {
1076 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1077 PHYR_WIDE_PORT);
1078 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
Ke Wei8f261aa2008-02-23 21:15:27 +08001079 port->wide_port_phymap);
1080 } else {
Andy Yan20b09c22009-05-08 17:46:40 -04001081 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1082 PHYR_WIDE_PORT);
1083 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1084 0);
Ke Wei8f261aa2008-02-23 21:15:27 +08001085 }
Andy Yan20b09c22009-05-08 17:46:40 -04001086 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001087}
1088
1089static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
1090{
1091 u32 tmp;
1092 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001093 struct mvs_port *port = phy->port;
Ke Wei8f261aa2008-02-23 21:15:27 +08001094
Andy Yan20b09c22009-05-08 17:46:40 -04001095 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001096 if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
Ke Wei963829e2008-03-27 14:55:23 +08001097 if (!port)
Ke Wei8f261aa2008-02-23 21:15:27 +08001098 phy->phy_attached = 1;
1099 return tmp;
1100 }
1101
Ke Wei8f261aa2008-02-23 21:15:27 +08001102 if (port) {
1103 if (phy->phy_type & PORT_TYPE_SAS) {
1104 port->wide_port_phymap &= ~(1U << i);
1105 if (!port->wide_port_phymap)
1106 port->port_attached = 0;
1107 mvs_update_wideport(mvi, i);
1108 } else if (phy->phy_type & PORT_TYPE_SATA)
1109 port->port_attached = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +08001110 phy->port = NULL;
1111 phy->phy_attached = 0;
1112 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1113 }
1114 return 0;
1115}
1116
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001117static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
1118{
1119 u32 *s = (u32 *) buf;
1120
1121 if (!s)
1122 return NULL;
1123
Andy Yan20b09c22009-05-08 17:46:40 -04001124 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
1125 s[3] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001126
Andy Yan20b09c22009-05-08 17:46:40 -04001127 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
1128 s[2] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001129
Andy Yan20b09c22009-05-08 17:46:40 -04001130 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
1131 s[1] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001132
Andy Yan20b09c22009-05-08 17:46:40 -04001133 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
1134 s[0] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
1135
1136 /* Workaround: take some ATAPI devices for ATA */
1137 if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1138 s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001139
Andy Yanf9da3be2009-05-14 20:41:21 -04001140 return s;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001141}
1142
1143static u32 mvs_is_sig_fis_received(u32 irq_status)
1144{
1145 return irq_status & PHYEV_SIG_FIS;
1146}
1147
Andy Yan20b09c22009-05-08 17:46:40 -04001148void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
Ke Wei8f261aa2008-02-23 21:15:27 +08001149{
1150 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001151 struct sas_identify_frame *id;
Ke Wei8f261aa2008-02-23 21:15:27 +08001152
Andy Yan20b09c22009-05-08 17:46:40 -04001153 id = (struct sas_identify_frame *)phy->frame_rcvd;
Ke Wei8f261aa2008-02-23 21:15:27 +08001154
1155 if (get_st) {
Andy Yan20b09c22009-05-08 17:46:40 -04001156 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001157 phy->phy_status = mvs_is_phy_ready(mvi, i);
1158 }
1159
1160 if (phy->phy_status) {
Andy Yan20b09c22009-05-08 17:46:40 -04001161 int oob_done = 0;
1162 struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
Ke Wei8f261aa2008-02-23 21:15:27 +08001163
Andy Yan20b09c22009-05-08 17:46:40 -04001164 oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001165
Andy Yan20b09c22009-05-08 17:46:40 -04001166 MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
1167 if (phy->phy_type & PORT_TYPE_SATA) {
1168 phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
1169 if (mvs_is_sig_fis_received(phy->irq_status)) {
1170 phy->phy_attached = 1;
1171 phy->att_dev_sas_addr =
1172 i + mvi->id * mvi->chip->n_phy;
1173 if (oob_done)
1174 sas_phy->oob_mode = SATA_OOB_MODE;
1175 phy->frame_rcvd_size =
1176 sizeof(struct dev_to_host_fis);
Andy Yanf9da3be2009-05-14 20:41:21 -04001177 mvs_get_d2h_reg(mvi, i, id);
Andy Yan20b09c22009-05-08 17:46:40 -04001178 } else {
1179 u32 tmp;
1180 dev_printk(KERN_DEBUG, mvi->dev,
1181 "Phy%d : No sig fis\n", i);
1182 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1183 MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1184 tmp | PHYEV_SIG_FIS);
1185 phy->phy_attached = 0;
1186 phy->phy_type &= ~PORT_TYPE_SATA;
1187 MVS_CHIP_DISP->phy_reset(mvi, i, 0);
1188 goto out_done;
1189 }
1190 } else if (phy->phy_type & PORT_TYPE_SAS
1191 || phy->att_dev_info & PORT_SSP_INIT_MASK) {
1192 phy->phy_attached = 1;
Ke Wei8f261aa2008-02-23 21:15:27 +08001193 phy->identify.device_type =
Andy Yan20b09c22009-05-08 17:46:40 -04001194 phy->att_dev_info & PORT_DEV_TYPE_MASK;
Ke Wei8f261aa2008-02-23 21:15:27 +08001195
1196 if (phy->identify.device_type == SAS_END_DEV)
1197 phy->identify.target_port_protocols =
1198 SAS_PROTOCOL_SSP;
1199 else if (phy->identify.device_type != NO_DEVICE)
1200 phy->identify.target_port_protocols =
1201 SAS_PROTOCOL_SMP;
Andy Yan20b09c22009-05-08 17:46:40 -04001202 if (oob_done)
Ke Wei8f261aa2008-02-23 21:15:27 +08001203 sas_phy->oob_mode = SAS_OOB_MODE;
1204 phy->frame_rcvd_size =
1205 sizeof(struct sas_identify_frame);
Ke Wei8f261aa2008-02-23 21:15:27 +08001206 }
Andy Yan20b09c22009-05-08 17:46:40 -04001207 memcpy(sas_phy->attached_sas_addr,
1208 &phy->att_dev_sas_addr, SAS_ADDR_SIZE);
Ke Weie9ff91b2008-03-27 14:55:33 +08001209
Andy Yan20b09c22009-05-08 17:46:40 -04001210 if (MVS_CHIP_DISP->phy_work_around)
1211 MVS_CHIP_DISP->phy_work_around(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001212 }
Andy Yan20b09c22009-05-08 17:46:40 -04001213 mv_dprintk("port %d attach dev info is %x\n",
1214 i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
1215 mv_dprintk("port %d attach sas addr is %llx\n",
1216 i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
Ke Weie9ff91b2008-03-27 14:55:33 +08001217out_done:
Ke Wei8f261aa2008-02-23 21:15:27 +08001218 if (get_st)
Andy Yan20b09c22009-05-08 17:46:40 -04001219 MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
Ke Wei8f261aa2008-02-23 21:15:27 +08001220}
1221
Andy Yan20b09c22009-05-08 17:46:40 -04001222static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
1223{
1224 struct sas_ha_struct *sas_ha = sas_phy->ha;
1225 struct mvs_info *mvi = NULL; int i = 0, hi;
1226 struct mvs_phy *phy = sas_phy->lldd_phy;
1227 struct asd_sas_port *sas_port = sas_phy->port;
1228 struct mvs_port *port;
1229 unsigned long flags = 0;
1230 if (!sas_port)
1231 return;
1232
1233 while (sas_ha->sas_phy[i]) {
1234 if (sas_ha->sas_phy[i] == sas_phy)
1235 break;
1236 i++;
1237 }
1238 hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
1239 mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
1240 if (sas_port->id >= mvi->chip->n_phy)
1241 port = &mvi->port[sas_port->id - mvi->chip->n_phy];
1242 else
1243 port = &mvi->port[sas_port->id];
1244 if (lock)
1245 spin_lock_irqsave(&mvi->lock, flags);
1246 port->port_attached = 1;
1247 phy->port = port;
1248 if (phy->phy_type & PORT_TYPE_SAS) {
1249 port->wide_port_phymap = sas_port->phy_mask;
1250 mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1251 mvs_update_wideport(mvi, sas_phy->id);
1252 }
1253 if (lock)
1254 spin_unlock_irqrestore(&mvi->lock, flags);
1255}
1256
1257static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1258{
1259 /*Nothing*/
1260}
1261
1262
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001263void mvs_port_formed(struct asd_sas_phy *sas_phy)
Ke Wei8f261aa2008-02-23 21:15:27 +08001264{
Andy Yan20b09c22009-05-08 17:46:40 -04001265 mvs_port_notify_formed(sas_phy, 1);
1266}
Ke Wei8f261aa2008-02-23 21:15:27 +08001267
Andy Yan20b09c22009-05-08 17:46:40 -04001268void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1269{
1270 mvs_port_notify_deformed(sas_phy, 1);
1271}
1272
1273struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
1274{
1275 u32 dev;
1276 for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
1277 if (mvi->devices[dev].dev_type == NO_DEVICE) {
1278 mvi->devices[dev].device_id = dev;
1279 return &mvi->devices[dev];
1280 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001281 }
Andy Yan20b09c22009-05-08 17:46:40 -04001282
1283 if (dev == MVS_MAX_DEVICES)
1284 mv_printk("max support %d devices, ignore ..\n",
1285 MVS_MAX_DEVICES);
1286
1287 return NULL;
1288}
1289
1290void mvs_free_dev(struct mvs_device *mvi_dev)
1291{
1292 u32 id = mvi_dev->device_id;
1293 memset(mvi_dev, 0, sizeof(*mvi_dev));
1294 mvi_dev->device_id = id;
1295 mvi_dev->dev_type = NO_DEVICE;
1296 mvi_dev->dev_status = MVS_DEV_NORMAL;
1297 mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1298}
1299
1300int mvs_dev_found_notify(struct domain_device *dev, int lock)
1301{
1302 unsigned long flags = 0;
1303 int res = 0;
1304 struct mvs_info *mvi = NULL;
1305 struct domain_device *parent_dev = dev->parent;
1306 struct mvs_device *mvi_device;
1307
1308 mvi = mvs_find_dev_mvi(dev);
1309
1310 if (lock)
1311 spin_lock_irqsave(&mvi->lock, flags);
1312
1313 mvi_device = mvs_alloc_dev(mvi);
1314 if (!mvi_device) {
1315 res = -1;
1316 goto found_out;
1317 }
Andy Yanf9da3be2009-05-14 20:41:21 -04001318 dev->lldd_dev = mvi_device;
Andy Yan20b09c22009-05-08 17:46:40 -04001319 mvi_device->dev_type = dev->dev_type;
Andy Yan9870d9a2009-05-11 22:19:25 +08001320 mvi_device->mvi_info = mvi;
Andy Yan20b09c22009-05-08 17:46:40 -04001321 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1322 int phy_id;
1323 u8 phy_num = parent_dev->ex_dev.num_phys;
1324 struct ex_phy *phy;
1325 for (phy_id = 0; phy_id < phy_num; phy_id++) {
1326 phy = &parent_dev->ex_dev.ex_phy[phy_id];
1327 if (SAS_ADDR(phy->attached_sas_addr) ==
1328 SAS_ADDR(dev->sas_addr)) {
1329 mvi_device->attached_phy = phy_id;
1330 break;
1331 }
1332 }
1333
1334 if (phy_id == phy_num) {
1335 mv_printk("Error: no attached dev:%016llx"
1336 "at ex:%016llx.\n",
1337 SAS_ADDR(dev->sas_addr),
1338 SAS_ADDR(parent_dev->sas_addr));
1339 res = -1;
1340 }
1341 }
1342
1343found_out:
1344 if (lock)
1345 spin_unlock_irqrestore(&mvi->lock, flags);
1346 return res;
1347}
1348
1349int mvs_dev_found(struct domain_device *dev)
1350{
1351 return mvs_dev_found_notify(dev, 1);
1352}
1353
1354void mvs_dev_gone_notify(struct domain_device *dev, int lock)
1355{
1356 unsigned long flags = 0;
Andy Yanf9da3be2009-05-14 20:41:21 -04001357 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001358 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001359
1360 if (lock)
1361 spin_lock_irqsave(&mvi->lock, flags);
1362
1363 if (mvi_dev) {
1364 mv_dprintk("found dev[%d:%x] is gone.\n",
1365 mvi_dev->device_id, mvi_dev->dev_type);
1366 mvs_free_reg_set(mvi, mvi_dev);
1367 mvs_free_dev(mvi_dev);
1368 } else {
1369 mv_dprintk("found dev has gone.\n");
1370 }
1371 dev->lldd_dev = NULL;
1372
1373 if (lock)
1374 spin_unlock_irqrestore(&mvi->lock, flags);
1375}
1376
1377
1378void mvs_dev_gone(struct domain_device *dev)
1379{
1380 mvs_dev_gone_notify(dev, 1);
1381}
1382
1383static struct sas_task *mvs_alloc_task(void)
1384{
1385 struct sas_task *task = kzalloc(sizeof(struct sas_task), GFP_KERNEL);
1386
1387 if (task) {
1388 INIT_LIST_HEAD(&task->list);
1389 spin_lock_init(&task->task_state_lock);
1390 task->task_state_flags = SAS_TASK_STATE_PENDING;
1391 init_timer(&task->timer);
1392 init_completion(&task->completion);
1393 }
1394 return task;
1395}
1396
1397static void mvs_free_task(struct sas_task *task)
1398{
1399 if (task) {
1400 BUG_ON(!list_empty(&task->list));
1401 kfree(task);
1402 }
1403}
1404
1405static void mvs_task_done(struct sas_task *task)
1406{
1407 if (!del_timer(&task->timer))
1408 return;
1409 complete(&task->completion);
1410}
1411
1412static void mvs_tmf_timedout(unsigned long data)
1413{
1414 struct sas_task *task = (struct sas_task *)data;
1415
1416 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1417 complete(&task->completion);
1418}
1419
1420/* XXX */
1421#define MVS_TASK_TIMEOUT 20
1422static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1423 void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1424{
1425 int res, retry;
1426 struct sas_task *task = NULL;
1427
1428 for (retry = 0; retry < 3; retry++) {
1429 task = mvs_alloc_task();
1430 if (!task)
1431 return -ENOMEM;
1432
1433 task->dev = dev;
1434 task->task_proto = dev->tproto;
1435
1436 memcpy(&task->ssp_task, parameter, para_len);
1437 task->task_done = mvs_task_done;
1438
1439 task->timer.data = (unsigned long) task;
1440 task->timer.function = mvs_tmf_timedout;
1441 task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1442 add_timer(&task->timer);
1443
Andy Yan0b84b702009-05-11 20:05:26 +08001444 res = mvs_task_exec(task, 1, GFP_KERNEL, NULL, 1, tmf);
Andy Yan20b09c22009-05-08 17:46:40 -04001445
1446 if (res) {
1447 del_timer(&task->timer);
1448 mv_printk("executing internel task failed:%d\n", res);
1449 goto ex_err;
1450 }
1451
1452 wait_for_completion(&task->completion);
1453 res = -TMF_RESP_FUNC_FAILED;
1454 /* Even TMF timed out, return direct. */
1455 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1456 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1457 mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1458 goto ex_err;
1459 }
1460 }
1461
1462 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1463 task->task_status.stat == SAM_GOOD) {
1464 res = TMF_RESP_FUNC_COMPLETE;
1465 break;
1466 }
1467
1468 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1469 task->task_status.stat == SAS_DATA_UNDERRUN) {
1470 /* no error, but return the number of bytes of
1471 * underrun */
1472 res = task->task_status.residual;
1473 break;
1474 }
1475
1476 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1477 task->task_status.stat == SAS_DATA_OVERRUN) {
1478 mv_dprintk("blocked task error.\n");
1479 res = -EMSGSIZE;
1480 break;
1481 } else {
1482 mv_dprintk(" task to dev %016llx response: 0x%x "
1483 "status 0x%x\n",
1484 SAS_ADDR(dev->sas_addr),
1485 task->task_status.resp,
1486 task->task_status.stat);
1487 mvs_free_task(task);
1488 task = NULL;
1489
1490 }
1491 }
1492ex_err:
1493 BUG_ON(retry == 3 && task != NULL);
1494 if (task != NULL)
1495 mvs_free_task(task);
1496 return res;
1497}
1498
1499static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1500 u8 *lun, struct mvs_tmf_task *tmf)
1501{
1502 struct sas_ssp_task ssp_task;
1503 DECLARE_COMPLETION_ONSTACK(completion);
1504 if (!(dev->tproto & SAS_PROTOCOL_SSP))
1505 return TMF_RESP_FUNC_ESUPP;
1506
1507 strncpy((u8 *)&ssp_task.LUN, lun, 8);
1508
1509 return mvs_exec_internal_tmf_task(dev, &ssp_task,
1510 sizeof(ssp_task), tmf);
1511}
1512
1513
1514/* Standard mandates link reset for ATA (type 0)
1515 and hard reset for SSP (type 1) , only for RECOVERY */
1516static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1517{
1518 int rc;
1519 struct sas_phy *phy = sas_find_local_phy(dev);
1520 int reset_type = (dev->dev_type == SATA_DEV ||
1521 (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1522 rc = sas_phy_reset(phy, reset_type);
1523 msleep(2000);
1524 return rc;
1525}
1526
1527/* mandatory SAM-3 */
1528int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1529{
1530 unsigned long flags;
1531 int i, phyno[WIDE_PORT_MAX_PHY], num , rc = TMF_RESP_FUNC_FAILED;
1532 struct mvs_tmf_task tmf_task;
Andy Yanf9da3be2009-05-14 20:41:21 -04001533 struct mvs_device * mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001534 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001535
1536 tmf_task.tmf = TMF_LU_RESET;
1537 mvi_dev->dev_status = MVS_DEV_EH;
1538 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1539 if (rc == TMF_RESP_FUNC_COMPLETE) {
1540 num = mvs_find_dev_phyno(dev, phyno);
1541 spin_lock_irqsave(&mvi->lock, flags);
1542 for (i = 0; i < num; i++)
1543 mvs_release_task(mvi, phyno[i], dev);
1544 spin_unlock_irqrestore(&mvi->lock, flags);
1545 }
1546 /* If failed, fall-through I_T_Nexus reset */
1547 mv_printk("%s for device[%x]:rc= %d\n", __func__,
1548 mvi_dev->device_id, rc);
1549 return rc;
Jeff Garzikb5762942007-10-25 20:58:22 -04001550}
1551
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001552int mvs_I_T_nexus_reset(struct domain_device *dev)
Ke Weie9ff91b2008-03-27 14:55:33 +08001553{
Andy Yan20b09c22009-05-08 17:46:40 -04001554 unsigned long flags;
1555 int i, phyno[WIDE_PORT_MAX_PHY], num , rc = TMF_RESP_FUNC_FAILED;
Andy Yan9870d9a2009-05-11 22:19:25 +08001556 struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
1557 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001558
1559 if (mvi_dev->dev_status != MVS_DEV_EH)
1560 return TMF_RESP_FUNC_COMPLETE;
1561 rc = mvs_debug_I_T_nexus_reset(dev);
1562 mv_printk("%s for device[%x]:rc= %d\n",
1563 __func__, mvi_dev->device_id, rc);
1564
1565 /* housekeeper */
1566 num = mvs_find_dev_phyno(dev, phyno);
1567 spin_lock_irqsave(&mvi->lock, flags);
1568 for (i = 0; i < num; i++)
1569 mvs_release_task(mvi, phyno[i], dev);
1570 spin_unlock_irqrestore(&mvi->lock, flags);
1571
1572 return rc;
1573}
1574/* optional SAM-3 */
1575int mvs_query_task(struct sas_task *task)
1576{
1577 u32 tag;
1578 struct scsi_lun lun;
1579 struct mvs_tmf_task tmf_task;
1580 int rc = TMF_RESP_FUNC_FAILED;
1581
1582 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1583 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1584 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001585 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1586 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001587
1588 int_to_scsilun(cmnd->device->lun, &lun);
1589 rc = mvs_find_tag(mvi, task, &tag);
1590 if (rc == 0) {
1591 rc = TMF_RESP_FUNC_FAILED;
1592 return rc;
1593 }
1594
1595 tmf_task.tmf = TMF_QUERY_TASK;
1596 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1597
1598 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1599 switch (rc) {
1600 /* The task is still in Lun, release it then */
1601 case TMF_RESP_FUNC_SUCC:
1602 /* The task is not in Lun or failed, reset the phy */
1603 case TMF_RESP_FUNC_FAILED:
1604 case TMF_RESP_FUNC_COMPLETE:
1605 break;
1606 }
1607 }
1608 mv_printk("%s:rc= %d\n", __func__, rc);
1609 return rc;
1610}
1611
1612/* mandatory SAM-3, still need free task/slot info */
1613int mvs_abort_task(struct sas_task *task)
1614{
1615 struct scsi_lun lun;
1616 struct mvs_tmf_task tmf_task;
1617 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001618 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1619 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001620 int rc = TMF_RESP_FUNC_FAILED;
1621 unsigned long flags;
1622 u32 tag;
Andy Yan9870d9a2009-05-11 22:19:25 +08001623
Andy Yan20b09c22009-05-08 17:46:40 -04001624 if (mvi->exp_req)
1625 mvi->exp_req--;
1626 spin_lock_irqsave(&task->task_state_lock, flags);
1627 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1628 spin_unlock_irqrestore(&task->task_state_lock, flags);
1629 rc = TMF_RESP_FUNC_COMPLETE;
1630 goto out;
1631 }
1632 spin_unlock_irqrestore(&task->task_state_lock, flags);
1633 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1634 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1635
1636 int_to_scsilun(cmnd->device->lun, &lun);
1637 rc = mvs_find_tag(mvi, task, &tag);
1638 if (rc == 0) {
1639 mv_printk("No such tag in %s\n", __func__);
1640 rc = TMF_RESP_FUNC_FAILED;
1641 return rc;
1642 }
1643
1644 tmf_task.tmf = TMF_ABORT_TASK;
1645 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1646
1647 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1648
1649 /* if successful, clear the task and callback forwards.*/
1650 if (rc == TMF_RESP_FUNC_COMPLETE) {
1651 u32 slot_no;
1652 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -04001653
1654 if (task->lldd_task) {
Andy Yanf9da3be2009-05-14 20:41:21 -04001655 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -04001656 slot_no = (u32) (slot - mvi->slot_info);
1657 mvs_slot_complete(mvi, slot_no, 1);
1658 }
1659 }
1660 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1661 task->task_proto & SAS_PROTOCOL_STP) {
1662 /* to do free register_set */
1663 } else {
1664 /* SMP */
1665
1666 }
1667out:
1668 if (rc != TMF_RESP_FUNC_COMPLETE)
1669 mv_printk("%s:rc= %d\n", __func__, rc);
1670 return rc;
1671}
1672
1673int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1674{
1675 int rc = TMF_RESP_FUNC_FAILED;
1676 struct mvs_tmf_task tmf_task;
1677
1678 tmf_task.tmf = TMF_ABORT_TASK_SET;
1679 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1680
1681 return rc;
1682}
1683
1684int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1685{
1686 int rc = TMF_RESP_FUNC_FAILED;
1687 struct mvs_tmf_task tmf_task;
1688
1689 tmf_task.tmf = TMF_CLEAR_ACA;
1690 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1691
1692 return rc;
1693}
1694
1695int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1696{
1697 int rc = TMF_RESP_FUNC_FAILED;
1698 struct mvs_tmf_task tmf_task;
1699
1700 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1701 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1702
1703 return rc;
Ke Weie9ff91b2008-03-27 14:55:33 +08001704}
1705
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001706static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1707 u32 slot_idx, int err)
1708{
Andy Yanf9da3be2009-05-14 20:41:21 -04001709 struct mvs_device *mvi_dev = task->dev->lldd_dev;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001710 struct task_status_struct *tstat = &task->task_status;
1711 struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
1712 int stat = SAM_GOOD;
1713
Andy Yan20b09c22009-05-08 17:46:40 -04001714
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001715 resp->frame_len = sizeof(struct dev_to_host_fis);
1716 memcpy(&resp->ending_fis[0],
Andy Yan20b09c22009-05-08 17:46:40 -04001717 SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001718 sizeof(struct dev_to_host_fis));
1719 tstat->buf_valid_size = sizeof(*resp);
1720 if (unlikely(err))
1721 stat = SAS_PROTO_RESPONSE;
1722 return stat;
1723}
1724
1725static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1726 u32 slot_idx)
1727{
1728 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
Andy Yan20b09c22009-05-08 17:46:40 -04001729 int stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001730 u32 err_dw0 = le32_to_cpu(*(u32 *) (slot->response));
Andy Yan20b09c22009-05-08 17:46:40 -04001731 u32 tfs = 0;
1732 enum mvs_port_type type = PORT_TYPE_SAS;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001733
Andy Yan20b09c22009-05-08 17:46:40 -04001734 if (err_dw0 & CMD_ISS_STPD)
1735 MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1736
1737 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1738
1739 stat = SAM_CHECK_COND;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001740 switch (task->task_proto) {
1741 case SAS_PROTOCOL_SSP:
Andy Yan20b09c22009-05-08 17:46:40 -04001742 stat = SAS_ABORTED_TASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001743 break;
1744 case SAS_PROTOCOL_SMP:
Andy Yan20b09c22009-05-08 17:46:40 -04001745 stat = SAM_CHECK_COND;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001746 break;
Andy Yan20b09c22009-05-08 17:46:40 -04001747
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001748 case SAS_PROTOCOL_SATA:
1749 case SAS_PROTOCOL_STP:
1750 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
Andy Yan20b09c22009-05-08 17:46:40 -04001751 {
1752 if (err_dw0 == 0x80400002)
1753 mv_printk("find reserved error, why?\n");
1754
1755 task->ata_task.use_ncq = 0;
1756 stat = SAS_PROTO_RESPONSE;
1757 mvs_sata_done(mvi, task, slot_idx, 1);
1758
1759 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001760 break;
1761 default:
1762 break;
1763 }
1764
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001765 return stat;
1766}
1767
Andy Yan20b09c22009-05-08 17:46:40 -04001768int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001769{
1770 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1771 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1772 struct sas_task *task = slot->task;
Andy Yan20b09c22009-05-08 17:46:40 -04001773 struct mvs_device *mvi_dev = NULL;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001774 struct task_status_struct *tstat;
Andy Yan20b09c22009-05-08 17:46:40 -04001775
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001776 bool aborted;
1777 void *to;
Andy Yan20b09c22009-05-08 17:46:40 -04001778 enum exec_status sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001779
Andy Yan20b09c22009-05-08 17:46:40 -04001780 if (mvi->exp_req)
1781 mvi->exp_req--;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001782 if (unlikely(!task || !task->lldd_task))
1783 return -1;
1784
Andy Yan20b09c22009-05-08 17:46:40 -04001785 tstat = &task->task_status;
Andy Yanf9da3be2009-05-14 20:41:21 -04001786 mvi_dev = task->dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -04001787
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001788 mvs_hba_cq_dump(mvi);
1789
1790 spin_lock(&task->task_state_lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001791 task->task_state_flags &=
1792 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1793 task->task_state_flags |= SAS_TASK_STATE_DONE;
1794 /* race condition*/
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001795 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001796 spin_unlock(&task->task_state_lock);
1797
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001798 memset(tstat, 0, sizeof(*tstat));
1799 tstat->resp = SAS_TASK_COMPLETE;
1800
Andy Yan20b09c22009-05-08 17:46:40 -04001801 if (unlikely(aborted)) {
1802 tstat->stat = SAS_ABORTED_TASK;
1803 if (mvi_dev)
1804 mvi_dev->runing_req--;
1805 if (sas_protocol_ata(task->task_proto))
1806 mvs_free_reg_set(mvi, mvi_dev);
1807
1808 mvs_slot_task_free(mvi, task, slot, slot_idx);
1809 return -1;
1810 }
1811
1812 if (unlikely(!mvi_dev || !slot->port->port_attached || flags)) {
1813 mv_dprintk("port has not device.\n");
1814 tstat->stat = SAS_PHY_DOWN;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001815 goto out;
1816 }
1817
Andy Yan20b09c22009-05-08 17:46:40 -04001818 /*
1819 if (unlikely((rx_desc & RXQ_ERR) || (*(u64 *) slot->response))) {
1820 mv_dprintk("Find device[%016llx] RXQ_ERR %X,
1821 err info:%016llx\n",
1822 SAS_ADDR(task->dev->sas_addr),
1823 rx_desc, (u64)(*(u64 *) slot->response));
1824 }
1825 */
1826
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001827 /* error info record present */
1828 if (unlikely((rx_desc & RXQ_ERR) && (*(u64 *) slot->response))) {
1829 tstat->stat = mvs_slot_err(mvi, task, slot_idx);
1830 goto out;
1831 }
1832
1833 switch (task->task_proto) {
1834 case SAS_PROTOCOL_SSP:
1835 /* hw says status == 0, datapres == 0 */
1836 if (rx_desc & RXQ_GOOD) {
1837 tstat->stat = SAM_GOOD;
1838 tstat->resp = SAS_TASK_COMPLETE;
1839 }
1840 /* response frame present */
1841 else if (rx_desc & RXQ_RSP) {
Andy Yan20b09c22009-05-08 17:46:40 -04001842 struct ssp_response_iu *iu = slot->response +
1843 sizeof(struct mvs_err_info);
1844 sas_ssp_task_response(mvi->dev, task, iu);
1845 } else
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001846 tstat->stat = SAM_CHECK_COND;
1847 break;
1848
1849 case SAS_PROTOCOL_SMP: {
1850 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
1851 tstat->stat = SAM_GOOD;
1852 to = kmap_atomic(sg_page(sg_resp), KM_IRQ0);
1853 memcpy(to + sg_resp->offset,
1854 slot->response + sizeof(struct mvs_err_info),
1855 sg_dma_len(sg_resp));
1856 kunmap_atomic(to, KM_IRQ0);
1857 break;
1858 }
1859
1860 case SAS_PROTOCOL_SATA:
1861 case SAS_PROTOCOL_STP:
1862 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
1863 tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
1864 break;
1865 }
1866
1867 default:
1868 tstat->stat = SAM_CHECK_COND;
1869 break;
1870 }
1871
1872out:
Andy Yan0f980a82009-05-11 21:49:52 +08001873 if (mvi_dev) {
Andy Yan20b09c22009-05-08 17:46:40 -04001874 mvi_dev->runing_req--;
Andy Yan0f980a82009-05-11 21:49:52 +08001875 if (sas_protocol_ata(task->task_proto))
1876 mvs_free_reg_set(mvi, mvi_dev);
1877 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001878 mvs_slot_task_free(mvi, task, slot, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04001879 sts = tstat->stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001880
1881 spin_unlock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001882 if (task->task_done)
1883 task->task_done(task);
1884 else
1885 mv_dprintk("why has not task_done.\n");
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001886 spin_lock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001887
1888 return sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001889}
1890
Andy Yan20b09c22009-05-08 17:46:40 -04001891void mvs_release_task(struct mvs_info *mvi,
1892 int phy_no, struct domain_device *dev)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001893{
Andy Yan20b09c22009-05-08 17:46:40 -04001894 int i = 0; u32 slot_idx;
1895 struct mvs_phy *phy;
1896 struct mvs_port *port;
1897 struct mvs_slot_info *slot, *slot2;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001898
Andy Yan20b09c22009-05-08 17:46:40 -04001899 phy = &mvi->phy[phy_no];
1900 port = phy->port;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001901 if (!port)
1902 return;
1903
Andy Yan20b09c22009-05-08 17:46:40 -04001904 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
1905 struct sas_task *task;
1906 slot_idx = (u32) (slot - mvi->slot_info);
1907 task = slot->task;
1908
1909 if (dev && task->dev != dev)
1910 continue;
1911
1912 mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
1913 slot_idx, slot->slot_tag, task);
1914
1915 if (task->task_proto & SAS_PROTOCOL_SSP) {
1916 mv_printk("attached with SSP task CDB[");
1917 for (i = 0; i < 16; i++)
1918 mv_printk(" %02x", task->ssp_task.cdb[i]);
1919 mv_printk(" ]\n");
1920 }
1921
1922 mvs_slot_complete(mvi, slot_idx, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001923 }
1924}
1925
Andy Yan20b09c22009-05-08 17:46:40 -04001926static void mvs_phy_disconnected(struct mvs_phy *phy)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001927{
Andy Yan20b09c22009-05-08 17:46:40 -04001928 phy->phy_attached = 0;
1929 phy->att_dev_info = 0;
1930 phy->att_dev_sas_addr = 0;
1931}
1932
1933static void mvs_work_queue(struct work_struct *work)
1934{
1935 struct delayed_work *dw = container_of(work, struct delayed_work, work);
1936 struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
1937 struct mvs_info *mvi = mwq->mvi;
1938 unsigned long flags;
1939
1940 spin_lock_irqsave(&mvi->lock, flags);
1941 if (mwq->handler & PHY_PLUG_EVENT) {
1942 u32 phy_no = (unsigned long) mwq->data;
1943 struct sas_ha_struct *sas_ha = mvi->sas;
1944 struct mvs_phy *phy = &mvi->phy[phy_no];
1945 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1946
1947 if (phy->phy_event & PHY_PLUG_OUT) {
1948 u32 tmp;
1949 struct sas_identify_frame *id;
1950 id = (struct sas_identify_frame *)phy->frame_rcvd;
1951 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
1952 phy->phy_event &= ~PHY_PLUG_OUT;
1953 if (!(tmp & PHY_READY_MASK)) {
1954 sas_phy_disconnected(sas_phy);
1955 mvs_phy_disconnected(phy);
1956 sas_ha->notify_phy_event(sas_phy,
1957 PHYE_LOSS_OF_SIGNAL);
1958 mv_dprintk("phy%d Removed Device\n", phy_no);
1959 } else {
1960 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
1961 mvs_update_phyinfo(mvi, phy_no, 1);
1962 mvs_bytes_dmaed(mvi, phy_no);
1963 mvs_port_notify_formed(sas_phy, 0);
1964 mv_dprintk("phy%d Attached Device\n", phy_no);
1965 }
1966 }
1967 }
1968 list_del(&mwq->entry);
1969 spin_unlock_irqrestore(&mvi->lock, flags);
1970 kfree(mwq);
1971}
1972
1973static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
1974{
1975 struct mvs_wq *mwq;
1976 int ret = 0;
1977
1978 mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
1979 if (mwq) {
1980 mwq->mvi = mvi;
1981 mwq->data = data;
1982 mwq->handler = handler;
1983 MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
1984 list_add_tail(&mwq->entry, &mvi->wq_list);
1985 schedule_delayed_work(&mwq->work_q, HZ * 2);
1986 } else
1987 ret = -ENOMEM;
1988
1989 return ret;
1990}
1991
1992static void mvs_sig_time_out(unsigned long tphy)
1993{
1994 struct mvs_phy *phy = (struct mvs_phy *)tphy;
1995 struct mvs_info *mvi = phy->mvi;
1996 u8 phy_no;
1997
1998 for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
1999 if (&mvi->phy[phy_no] == phy) {
2000 mv_dprintk("Get signature time out, reset phy %d\n",
2001 phy_no+mvi->id*mvi->chip->n_phy);
2002 MVS_CHIP_DISP->phy_reset(mvi, phy_no, 1);
2003 }
2004 }
2005}
2006
2007static void mvs_sig_remove_timer(struct mvs_phy *phy)
2008{
2009 if (phy->timer.function)
2010 del_timer(&phy->timer);
2011 phy->timer.function = NULL;
2012}
2013
2014void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
2015{
2016 u32 tmp;
2017 struct sas_ha_struct *sas_ha = mvi->sas;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002018 struct mvs_phy *phy = &mvi->phy[phy_no];
2019 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2020
Andy Yan20b09c22009-05-08 17:46:40 -04002021 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
2022 mv_dprintk("port %d ctrl sts=0x%X.\n", phy_no+mvi->id*mvi->chip->n_phy,
2023 MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
2024 mv_dprintk("Port %d irq sts = 0x%X\n", phy_no+mvi->id*mvi->chip->n_phy,
2025 phy->irq_status);
2026
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002027 /*
2028 * events is port event now ,
2029 * we need check the interrupt status which belongs to per port.
2030 */
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002031
Andy Yan20b09c22009-05-08 17:46:40 -04002032 if (phy->irq_status & PHYEV_DCDR_ERR)
2033 mv_dprintk("port %d STP decoding error.\n",
2034 phy_no+mvi->id*mvi->chip->n_phy);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002035
Andy Yan20b09c22009-05-08 17:46:40 -04002036 if (phy->irq_status & PHYEV_POOF) {
2037 if (!(phy->phy_event & PHY_PLUG_OUT)) {
2038 int dev_sata = phy->phy_type & PORT_TYPE_SATA;
2039 int ready;
2040 mvs_release_task(mvi, phy_no, NULL);
2041 phy->phy_event |= PHY_PLUG_OUT;
2042 mvs_handle_event(mvi,
2043 (void *)(unsigned long)phy_no,
2044 PHY_PLUG_EVENT);
2045 ready = mvs_is_phy_ready(mvi, phy_no);
2046 if (!ready)
2047 mv_dprintk("phy%d Unplug Notice\n",
2048 phy_no +
2049 mvi->id * mvi->chip->n_phy);
2050 if (ready || dev_sata) {
2051 if (MVS_CHIP_DISP->stp_reset)
2052 MVS_CHIP_DISP->stp_reset(mvi,
2053 phy_no);
2054 else
2055 MVS_CHIP_DISP->phy_reset(mvi,
2056 phy_no, 0);
2057 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002058 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002059 }
2060 }
Andy Yan20b09c22009-05-08 17:46:40 -04002061
2062 if (phy->irq_status & PHYEV_COMWAKE) {
2063 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2064 MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2065 tmp | PHYEV_SIG_FIS);
2066 if (phy->timer.function == NULL) {
2067 phy->timer.data = (unsigned long)phy;
2068 phy->timer.function = mvs_sig_time_out;
2069 phy->timer.expires = jiffies + 10*HZ;
2070 add_timer(&phy->timer);
2071 }
2072 }
2073 if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2074 phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
2075 mvs_sig_remove_timer(phy);
2076 mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2077 if (phy->phy_status) {
2078 mdelay(10);
2079 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2080 if (phy->phy_type & PORT_TYPE_SATA) {
2081 tmp = MVS_CHIP_DISP->read_port_irq_mask(
2082 mvi, phy_no);
2083 tmp &= ~PHYEV_SIG_FIS;
2084 MVS_CHIP_DISP->write_port_irq_mask(mvi,
2085 phy_no, tmp);
2086 }
2087 mvs_update_phyinfo(mvi, phy_no, 0);
2088 mvs_bytes_dmaed(mvi, phy_no);
2089 /* whether driver is going to handle hot plug */
2090 if (phy->phy_event & PHY_PLUG_OUT) {
2091 mvs_port_notify_formed(sas_phy, 0);
2092 phy->phy_event &= ~PHY_PLUG_OUT;
2093 }
2094 } else {
2095 mv_dprintk("plugin interrupt but phy%d is gone\n",
2096 phy_no + mvi->id*mvi->chip->n_phy);
2097 }
2098 } else if (phy->irq_status & PHYEV_BROAD_CH) {
2099 mv_dprintk("port %d broadcast change.\n",
2100 phy_no + mvi->id*mvi->chip->n_phy);
2101 /* exception for Samsung disk drive*/
2102 mdelay(1000);
2103 sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
2104 }
2105 MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002106}
2107
Andy Yan20b09c22009-05-08 17:46:40 -04002108int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002109{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002110 u32 rx_prod_idx, rx_desc;
2111 bool attn = false;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002112
2113 /* the first dword in the RX ring is special: it contains
2114 * a mirror of the hardware's RX producer index, so that
2115 * we don't have to stall the CPU reading that register.
2116 * The actual RX ring is offset by one dword, due to this.
2117 */
2118 rx_prod_idx = mvi->rx_cons;
2119 mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2120 if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
2121 return 0;
2122
2123 /* The CMPL_Q may come late, read from register and try again
2124 * note: if coalescing is enabled,
2125 * it will need to read from register every time for sure
2126 */
Andy Yan20b09c22009-05-08 17:46:40 -04002127 if (unlikely(mvi->rx_cons == rx_prod_idx))
2128 mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002129
2130 if (mvi->rx_cons == rx_prod_idx)
2131 return 0;
2132
2133 while (mvi->rx_cons != rx_prod_idx) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002134 /* increment our internal RX consumer pointer */
2135 rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002136 rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2137
2138 if (likely(rx_desc & RXQ_DONE))
2139 mvs_slot_complete(mvi, rx_desc, 0);
2140 if (rx_desc & RXQ_ATTN) {
2141 attn = true;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002142 } else if (rx_desc & RXQ_ERR) {
2143 if (!(rx_desc & RXQ_DONE))
2144 mvs_slot_complete(mvi, rx_desc, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002145 } else if (rx_desc & RXQ_SLOT_RESET) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002146 mvs_slot_free(mvi, rx_desc);
2147 }
2148 }
2149
2150 if (attn && self_clear)
Andy Yan20b09c22009-05-08 17:46:40 -04002151 MVS_CHIP_DISP->int_full(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002152 return 0;
2153}
2154