blob: 31ca8fe25caee9c7cf3ccf9010b9ac624bd197df [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{
Xiangliang Yub89e8f52011-05-24 22:35:09 +080041 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{
Xiangliang Yub89e8f52011-05-24 22:35:09 +080052 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;
Xiangliang Yub89e8f52011-05-24 22:35:09 +080059 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 -040077struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
78{
79 unsigned long i = 0, j = 0, hi = 0;
80 struct sas_ha_struct *sha = dev->port->ha;
81 struct mvs_info *mvi = NULL;
82 struct asd_sas_phy *phy;
Ke Wei8f261aa2008-02-23 21:15:27 +080083
Andy Yan20b09c22009-05-08 17:46:40 -040084 while (sha->sas_port[i]) {
85 if (sha->sas_port[i] == dev->port) {
86 phy = container_of(sha->sas_port[i]->phy_list.next,
87 struct asd_sas_phy, port_phy_el);
88 j = 0;
89 while (sha->sas_phy[j]) {
90 if (sha->sas_phy[j] == phy)
91 break;
92 j++;
Jeff Garzikb5762942007-10-25 20:58:22 -040093 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -040094 break;
Jeff Garzikb5762942007-10-25 20:58:22 -040095 }
Andy Yan20b09c22009-05-08 17:46:40 -040096 i++;
97 }
98 hi = j/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
99 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
100
101 return mvi;
102
103}
104
105/* FIXME */
106int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
107{
108 unsigned long i = 0, j = 0, n = 0, num = 0;
Andy Yan9870d9a2009-05-11 22:19:25 +0800109 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
110 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -0400111 struct sas_ha_struct *sha = dev->port->ha;
112
113 while (sha->sas_port[i]) {
114 if (sha->sas_port[i] == dev->port) {
115 struct asd_sas_phy *phy;
116 list_for_each_entry(phy,
117 &sha->sas_port[i]->phy_list, port_phy_el) {
118 j = 0;
119 while (sha->sas_phy[j]) {
120 if (sha->sas_phy[j] == phy)
121 break;
122 j++;
123 }
124 phyno[n] = (j >= mvi->chip->n_phy) ?
125 (j - mvi->chip->n_phy) : j;
126 num++;
127 n++;
128 }
129 break;
130 }
131 i++;
132 }
133 return num;
134}
135
Xiangliang Yu534ff102011-05-24 22:26:50 +0800136struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
137 u8 reg_set)
138{
139 u32 dev_no;
140 for (dev_no = 0; dev_no < MVS_MAX_DEVICES; dev_no++) {
141 if (mvi->devices[dev_no].taskfileset == MVS_ID_NOT_MAPPED)
142 continue;
143
144 if (mvi->devices[dev_no].taskfileset == reg_set)
145 return &mvi->devices[dev_no];
146 }
147 return NULL;
148}
149
Andy Yan20b09c22009-05-08 17:46:40 -0400150static inline void mvs_free_reg_set(struct mvs_info *mvi,
151 struct mvs_device *dev)
152{
153 if (!dev) {
154 mv_printk("device has been free.\n");
155 return;
156 }
Andy Yan20b09c22009-05-08 17:46:40 -0400157 if (dev->taskfileset == MVS_ID_NOT_MAPPED)
158 return;
159 MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
160}
161
162static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
163 struct mvs_device *dev)
164{
165 if (dev->taskfileset != MVS_ID_NOT_MAPPED)
166 return 0;
167 return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
168}
169
170void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
171{
172 u32 no;
173 for_each_phy(phy_mask, phy_mask, no) {
174 if (!(phy_mask & 1))
175 continue;
176 MVS_CHIP_DISP->phy_reset(mvi, no, hard);
177 }
178}
179
180/* FIXME: locking? */
181int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
182 void *funcdata)
183{
184 int rc = 0, phy_id = sas_phy->id;
185 u32 tmp, i = 0, hi;
186 struct sas_ha_struct *sha = sas_phy->ha;
187 struct mvs_info *mvi = NULL;
188
189 while (sha->sas_phy[i]) {
190 if (sha->sas_phy[i] == sas_phy)
191 break;
192 i++;
193 }
194 hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
195 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
196
197 switch (func) {
198 case PHY_FUNC_SET_LINK_RATE:
199 MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
200 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400201
202 case PHY_FUNC_HARD_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400203 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400204 if (tmp & PHY_RST_HARD)
205 break;
Xiangliang Yua4632aa2011-05-24 22:36:02 +0800206 MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_HARD_RESET);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400207 break;
208
209 case PHY_FUNC_LINK_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400210 MVS_CHIP_DISP->phy_enable(mvi, phy_id);
Xiangliang Yua4632aa2011-05-24 22:36:02 +0800211 MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_SOFT_RESET);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400212 break;
213
214 case PHY_FUNC_DISABLE:
Andy Yan20b09c22009-05-08 17:46:40 -0400215 MVS_CHIP_DISP->phy_disable(mvi, phy_id);
216 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400217 case PHY_FUNC_RELEASE_SPINUP_HOLD:
218 default:
219 rc = -EOPNOTSUPP;
Jeff Garzikb5762942007-10-25 20:58:22 -0400220 }
Andy Yan20b09c22009-05-08 17:46:40 -0400221 msleep(200);
Jeff Garzikb5762942007-10-25 20:58:22 -0400222 return rc;
223}
224
Andy Yan20b09c22009-05-08 17:46:40 -0400225void __devinit mvs_set_sas_addr(struct mvs_info *mvi, int port_id,
226 u32 off_lo, u32 off_hi, u64 sas_addr)
227{
228 u32 lo = (u32)sas_addr;
229 u32 hi = (u32)(sas_addr>>32);
230
231 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_lo);
232 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, lo);
233 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_hi);
234 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, hi);
235}
236
Ke Wei8f261aa2008-02-23 21:15:27 +0800237static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
238{
239 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -0400240 struct asd_sas_phy *sas_phy = &phy->sas_phy;
241 struct sas_ha_struct *sas_ha;
Ke Wei8f261aa2008-02-23 21:15:27 +0800242 if (!phy->phy_attached)
243 return;
244
Andy Yan20b09c22009-05-08 17:46:40 -0400245 if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
246 && phy->phy_type & PORT_TYPE_SAS) {
247 return;
248 }
249
250 sas_ha = mvi->sas;
251 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
252
Ke Weiee1f1c22008-03-27 14:53:47 +0800253 if (sas_phy->phy) {
254 struct sas_phy *sphy = sas_phy->phy;
255
256 sphy->negotiated_linkrate = sas_phy->linkrate;
257 sphy->minimum_linkrate = phy->minimum_linkrate;
258 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
259 sphy->maximum_linkrate = phy->maximum_linkrate;
Andy Yan20b09c22009-05-08 17:46:40 -0400260 sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
Ke Weiee1f1c22008-03-27 14:53:47 +0800261 }
262
Ke Wei8f261aa2008-02-23 21:15:27 +0800263 if (phy->phy_type & PORT_TYPE_SAS) {
264 struct sas_identify_frame *id;
265
266 id = (struct sas_identify_frame *)phy->frame_rcvd;
267 id->dev_type = phy->identify.device_type;
268 id->initiator_bits = SAS_PROTOCOL_ALL;
269 id->target_bits = phy->identify.target_port_protocols;
270 } else if (phy->phy_type & PORT_TYPE_SATA) {
Andy Yan20b09c22009-05-08 17:46:40 -0400271 /*Nothing*/
Ke Wei8f261aa2008-02-23 21:15:27 +0800272 }
Andy Yan20b09c22009-05-08 17:46:40 -0400273 mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
274
275 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
276
277 mvi->sas->notify_port_event(sas_phy,
Ke Wei8f261aa2008-02-23 21:15:27 +0800278 PORTE_BYTES_DMAED);
279}
280
Andy Yan20b09c22009-05-08 17:46:40 -0400281int mvs_slave_alloc(struct scsi_device *scsi_dev)
282{
283 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
284 if (dev_is_sata(dev)) {
285 /* We don't need to rescan targets
286 * if REPORT_LUNS request is failed
287 */
288 if (scsi_dev->lun > 0)
289 return -ENXIO;
290 scsi_dev->tagged_supported = 1;
291 }
292
293 return sas_slave_alloc(scsi_dev);
294}
295
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400296int mvs_slave_configure(struct scsi_device *sdev)
Ke Wei8f261aa2008-02-23 21:15:27 +0800297{
Ke Weiee1f1c22008-03-27 14:53:47 +0800298 struct domain_device *dev = sdev_to_domain_dev(sdev);
299 int ret = sas_slave_configure(sdev);
Ke Wei8f261aa2008-02-23 21:15:27 +0800300
Ke Weiee1f1c22008-03-27 14:53:47 +0800301 if (ret)
302 return ret;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800303 if (!dev_is_sata(dev)) {
304 sas_change_queue_depth(sdev,
305 MVS_QUEUE_SIZE,
306 SCSI_QDEPTH_DEFAULT);
Ke Weiee1f1c22008-03-27 14:53:47 +0800307 }
308 return 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400309}
310
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400311void mvs_scan_start(struct Scsi_Host *shost)
Jeff Garzikb5762942007-10-25 20:58:22 -0400312{
Andy Yan20b09c22009-05-08 17:46:40 -0400313 int i, j;
314 unsigned short core_nr;
315 struct mvs_info *mvi;
316 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800317 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
Jeff Garzikb5762942007-10-25 20:58:22 -0400318
Andy Yan20b09c22009-05-08 17:46:40 -0400319 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
320
321 for (j = 0; j < core_nr; j++) {
322 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
323 for (i = 0; i < mvi->chip->n_phy; ++i)
324 mvs_bytes_dmaed(mvi, i);
Jeff Garzikb5762942007-10-25 20:58:22 -0400325 }
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800326 mvs_prv->scan_finished = 1;
Jeff Garzikb5762942007-10-25 20:58:22 -0400327}
328
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400329int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
Jeff Garzikb5762942007-10-25 20:58:22 -0400330{
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800331 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
332 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
333
334 if (mvs_prv->scan_finished == 0)
Ke Wei8f261aa2008-02-23 21:15:27 +0800335 return 0;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800336
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400337 scsi_flush_work(shost);
338 return 1;
Jeff Garzikb5762942007-10-25 20:58:22 -0400339}
340
Ke Wei8f261aa2008-02-23 21:15:27 +0800341static int mvs_task_prep_smp(struct mvs_info *mvi,
342 struct mvs_task_exec_info *tei)
Jeff Garzikb5762942007-10-25 20:58:22 -0400343{
Ke Wei8f261aa2008-02-23 21:15:27 +0800344 int elem, rc, i;
345 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400346 struct mvs_cmd_hdr *hdr = tei->hdr;
Andy Yan20b09c22009-05-08 17:46:40 -0400347 struct domain_device *dev = task->dev;
348 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400349 struct scatterlist *sg_req, *sg_resp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800350 u32 req_len, resp_len, tag = tei->tag;
351 void *buf_tmp;
352 u8 *buf_oaf;
353 dma_addr_t buf_tmp_dma;
Andy Yan20b09c22009-05-08 17:46:40 -0400354 void *buf_prd;
Ke Wei8f261aa2008-02-23 21:15:27 +0800355 struct mvs_slot_info *slot = &mvi->slot_info[tag];
Ke Wei8f261aa2008-02-23 21:15:27 +0800356 u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800357
Jeff Garzikb5762942007-10-25 20:58:22 -0400358 /*
359 * DMA-map SMP request, response buffers
360 */
Ke Wei8f261aa2008-02-23 21:15:27 +0800361 sg_req = &task->smp_task.smp_req;
Andy Yan20b09c22009-05-08 17:46:40 -0400362 elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400363 if (!elem)
364 return -ENOMEM;
365 req_len = sg_dma_len(sg_req);
366
Ke Wei8f261aa2008-02-23 21:15:27 +0800367 sg_resp = &task->smp_task.smp_resp;
Andy Yan20b09c22009-05-08 17:46:40 -0400368 elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400369 if (!elem) {
370 rc = -ENOMEM;
371 goto err_out;
372 }
Andy Yan20b09c22009-05-08 17:46:40 -0400373 resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400374
375 /* must be in dwords */
376 if ((req_len & 0x3) || (resp_len & 0x3)) {
377 rc = -EINVAL;
378 goto err_out_2;
379 }
380
381 /*
Ke Wei8f261aa2008-02-23 21:15:27 +0800382 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
Jeff Garzikb5762942007-10-25 20:58:22 -0400383 */
384
Andy Yan20b09c22009-05-08 17:46:40 -0400385 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800386 buf_tmp = slot->buf;
387 buf_tmp_dma = slot->buf_dma;
Jeff Garzikb5762942007-10-25 20:58:22 -0400388
Ke Wei8f261aa2008-02-23 21:15:27 +0800389 hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
Ke Wei8f261aa2008-02-23 21:15:27 +0800390
391 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
392 buf_oaf = buf_tmp;
393 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
394
395 buf_tmp += MVS_OAF_SZ;
396 buf_tmp_dma += MVS_OAF_SZ;
397
Andy Yan20b09c22009-05-08 17:46:40 -0400398 /* region 3: PRD table *********************************** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800399 buf_prd = buf_tmp;
400 if (tei->n_elem)
401 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
402 else
403 hdr->prd_tbl = 0;
404
Andy Yan20b09c22009-05-08 17:46:40 -0400405 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Ke Wei8f261aa2008-02-23 21:15:27 +0800406 buf_tmp += i;
407 buf_tmp_dma += i;
408
409 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
410 slot->response = buf_tmp;
411 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400412 if (mvi->flags & MVF_FLAG_SOC)
413 hdr->reserved[0] = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800414
415 /*
416 * Fill in TX ring and command slot header
417 */
418 slot->tx = mvi->tx_prod;
419 mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
420 TXQ_MODE_I | tag |
421 (sas_port->phy_mask << TXQ_PHY_SHIFT));
422
423 hdr->flags |= flags;
424 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
Jeff Garzikb5762942007-10-25 20:58:22 -0400425 hdr->tags = cpu_to_le32(tag);
426 hdr->data_len = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400427
Ke Wei8f261aa2008-02-23 21:15:27 +0800428 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400429 /* initiator, SMP, ftype 1h */
430 buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
431 buf_oaf[1] = dev->linkrate & 0xf;
Ke Wei8f261aa2008-02-23 21:15:27 +0800432 *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
Andy Yan20b09c22009-05-08 17:46:40 -0400433 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Ke Wei8f261aa2008-02-23 21:15:27 +0800434
435 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400436 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Ke Wei8f261aa2008-02-23 21:15:27 +0800437
Jeff Garzikb5762942007-10-25 20:58:22 -0400438 return 0;
439
440err_out_2:
Andy Yan20b09c22009-05-08 17:46:40 -0400441 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400442 PCI_DMA_FROMDEVICE);
443err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400444 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400445 PCI_DMA_TODEVICE);
446 return rc;
447}
448
Ke Wei4e52fc02008-03-27 14:54:50 +0800449static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
Ke Wei8f261aa2008-02-23 21:15:27 +0800450{
Ke Wei8f261aa2008-02-23 21:15:27 +0800451 struct ata_queued_cmd *qc = task->uldd_task;
452
Ke Wei4e52fc02008-03-27 14:54:50 +0800453 if (qc) {
454 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
455 qc->tf.command == ATA_CMD_FPDMA_READ) {
456 *tag = qc->tag;
457 return 1;
458 }
459 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800460
Ke Wei4e52fc02008-03-27 14:54:50 +0800461 return 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800462}
463
Jeff Garzikb5762942007-10-25 20:58:22 -0400464static int mvs_task_prep_ata(struct mvs_info *mvi,
465 struct mvs_task_exec_info *tei)
466{
467 struct sas_task *task = tei->task;
468 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400469 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400470 struct mvs_cmd_hdr *hdr = tei->hdr;
471 struct asd_sas_port *sas_port = dev->port;
Ke Wei8f261aa2008-02-23 21:15:27 +0800472 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400473 void *buf_prd;
474 u32 tag = tei->tag, hdr_tag;
475 u32 flags, del_q;
Jeff Garzikb5762942007-10-25 20:58:22 -0400476 void *buf_tmp;
477 u8 *buf_cmd, *buf_oaf;
478 dma_addr_t buf_tmp_dma;
Ke Wei8f261aa2008-02-23 21:15:27 +0800479 u32 i, req_len, resp_len;
480 const u32 max_resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400481
Andy Yan20b09c22009-05-08 17:46:40 -0400482 if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
483 mv_dprintk("Have not enough regiset for dev %d.\n",
484 mvi_dev->device_id);
Ke Wei8f261aa2008-02-23 21:15:27 +0800485 return -EBUSY;
Andy Yan20b09c22009-05-08 17:46:40 -0400486 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800487 slot = &mvi->slot_info[tag];
488 slot->tx = mvi->tx_prod;
Andy Yan20b09c22009-05-08 17:46:40 -0400489 del_q = TXQ_MODE_I | tag |
490 (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
491 (sas_port->phy_mask << TXQ_PHY_SHIFT) |
492 (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
493 mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
Jeff Garzikb5762942007-10-25 20:58:22 -0400494
Andy Yan20b09c22009-05-08 17:46:40 -0400495 if (task->data_dir == DMA_FROM_DEVICE)
496 flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
497 else
498 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800499
Jeff Garzikb5762942007-10-25 20:58:22 -0400500 if (task->ata_task.use_ncq)
501 flags |= MCH_FPDMA;
Ke Wei8f261aa2008-02-23 21:15:27 +0800502 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
503 if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
504 flags |= MCH_ATAPI;
505 }
506
Jeff Garzikb5762942007-10-25 20:58:22 -0400507 /* FIXME: fill in port multiplier number */
508
509 hdr->flags = cpu_to_le32(flags);
Ke Wei8f261aa2008-02-23 21:15:27 +0800510
511 /* FIXME: the low order order 5 bits for the TAG if enable NCQ */
Andy Yan20b09c22009-05-08 17:46:40 -0400512 if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
513 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
Ke Wei4e52fc02008-03-27 14:54:50 +0800514 else
Andy Yan20b09c22009-05-08 17:46:40 -0400515 hdr_tag = tag;
516
517 hdr->tags = cpu_to_le32(hdr_tag);
518
Jeff Garzikb5762942007-10-25 20:58:22 -0400519 hdr->data_len = cpu_to_le32(task->total_xfer_len);
520
521 /*
522 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
523 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400524
Ke Wei8f261aa2008-02-23 21:15:27 +0800525 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
526 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400527 buf_tmp_dma = slot->buf_dma;
528
529 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
530
531 buf_tmp += MVS_ATA_CMD_SZ;
532 buf_tmp_dma += MVS_ATA_CMD_SZ;
533
Ke Wei8f261aa2008-02-23 21:15:27 +0800534 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400535 /* used for STP. unused for SATA? */
536 buf_oaf = buf_tmp;
537 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
538
539 buf_tmp += MVS_OAF_SZ;
540 buf_tmp_dma += MVS_OAF_SZ;
541
Ke Wei8f261aa2008-02-23 21:15:27 +0800542 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400543 buf_prd = buf_tmp;
Andy Yan20b09c22009-05-08 17:46:40 -0400544
Ke Wei8f261aa2008-02-23 21:15:27 +0800545 if (tei->n_elem)
546 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
547 else
548 hdr->prd_tbl = 0;
Andy Yan20b09c22009-05-08 17:46:40 -0400549 i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
Jeff Garzikb5762942007-10-25 20:58:22 -0400550
Jeff Garzikb5762942007-10-25 20:58:22 -0400551 buf_tmp += i;
552 buf_tmp_dma += i;
553
Ke Wei8f261aa2008-02-23 21:15:27 +0800554 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400555 /* FIXME: probably unused, for SATA. kept here just in case
556 * we get a STP/SATA error information record
557 */
558 slot->response = buf_tmp;
559 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400560 if (mvi->flags & MVF_FLAG_SOC)
561 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400562
Ke Wei8f261aa2008-02-23 21:15:27 +0800563 req_len = sizeof(struct host_to_dev_fis);
Jeff Garzikb5762942007-10-25 20:58:22 -0400564 resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800565 sizeof(struct mvs_err_info) - i;
Jeff Garzikb5762942007-10-25 20:58:22 -0400566
567 /* request, response lengths */
Ke Wei8f261aa2008-02-23 21:15:27 +0800568 resp_len = min(resp_len, max_resp_len);
Jeff Garzikb5762942007-10-25 20:58:22 -0400569 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
570
Andy Yan20b09c22009-05-08 17:46:40 -0400571 if (likely(!task->ata_task.device_control_reg_update))
572 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
Jeff Garzikb5762942007-10-25 20:58:22 -0400573 /* fill in command FIS and ATAPI CDB */
Ke Wei8f261aa2008-02-23 21:15:27 +0800574 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
575 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
576 memcpy(buf_cmd + STP_ATAPI_CMD,
577 task->ata_task.atapi_packet, 16);
578
579 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400580 /* initiator, STP, ftype 1h */
581 buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
582 buf_oaf[1] = dev->linkrate & 0xf;
583 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
584 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400585
586 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400587 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800588
Andy Yan20b09c22009-05-08 17:46:40 -0400589 if (task->data_dir == DMA_FROM_DEVICE)
Xiangliang Yu8882f082011-05-24 22:33:11 +0800590 MVS_CHIP_DISP->dma_fix(mvi, sas_port->phy_mask,
Andy Yan20b09c22009-05-08 17:46:40 -0400591 TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800592
Jeff Garzikb5762942007-10-25 20:58:22 -0400593 return 0;
594}
595
596static int mvs_task_prep_ssp(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -0400597 struct mvs_task_exec_info *tei, int is_tmf,
598 struct mvs_tmf_task *tmf)
Jeff Garzikb5762942007-10-25 20:58:22 -0400599{
600 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400601 struct mvs_cmd_hdr *hdr = tei->hdr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800602 struct mvs_port *port = tei->port;
Andy Yan20b09c22009-05-08 17:46:40 -0400603 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400604 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -0400605 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400606 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400607 void *buf_prd;
Jeff Garzikb5762942007-10-25 20:58:22 -0400608 struct ssp_frame_hdr *ssp_hdr;
609 void *buf_tmp;
610 u8 *buf_cmd, *buf_oaf, fburst = 0;
611 dma_addr_t buf_tmp_dma;
612 u32 flags;
Ke Wei8f261aa2008-02-23 21:15:27 +0800613 u32 resp_len, req_len, i, tag = tei->tag;
614 const u32 max_resp_len = SB_RFB_MAX;
Andy Yan20b09c22009-05-08 17:46:40 -0400615 u32 phy_mask;
Jeff Garzikb5762942007-10-25 20:58:22 -0400616
617 slot = &mvi->slot_info[tag];
618
Andy Yan20b09c22009-05-08 17:46:40 -0400619 phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
620 sas_port->phy_mask) & TXQ_PHY_MASK;
621
Ke Wei8f261aa2008-02-23 21:15:27 +0800622 slot->tx = mvi->tx_prod;
623 mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
624 (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
Ke Wei4e52fc02008-03-27 14:54:50 +0800625 (phy_mask << TXQ_PHY_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400626
627 flags = MCH_RETRY;
628 if (task->ssp_task.enable_first_burst) {
629 flags |= MCH_FBURST;
630 fburst = (1 << 7);
631 }
Andy Yan2b288132009-05-11 20:01:55 +0800632 if (is_tmf)
633 flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800634 else
635 flags |= (MCH_SSP_FR_CMD << MCH_SSP_FR_TYPE_SHIFT);
636
Andy Yan2b288132009-05-11 20:01:55 +0800637 hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400638 hdr->tags = cpu_to_le32(tag);
639 hdr->data_len = cpu_to_le32(task->total_xfer_len);
640
641 /*
642 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
643 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400644
Ke Wei8f261aa2008-02-23 21:15:27 +0800645 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
646 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400647 buf_tmp_dma = slot->buf_dma;
648
649 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
650
651 buf_tmp += MVS_SSP_CMD_SZ;
652 buf_tmp_dma += MVS_SSP_CMD_SZ;
653
Ke Wei8f261aa2008-02-23 21:15:27 +0800654 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400655 buf_oaf = buf_tmp;
656 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
657
658 buf_tmp += MVS_OAF_SZ;
659 buf_tmp_dma += MVS_OAF_SZ;
660
Ke Wei8f261aa2008-02-23 21:15:27 +0800661 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400662 buf_prd = buf_tmp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800663 if (tei->n_elem)
664 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
665 else
666 hdr->prd_tbl = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400667
Andy Yan20b09c22009-05-08 17:46:40 -0400668 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Jeff Garzikb5762942007-10-25 20:58:22 -0400669 buf_tmp += i;
670 buf_tmp_dma += i;
671
Ke Wei8f261aa2008-02-23 21:15:27 +0800672 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400673 slot->response = buf_tmp;
674 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400675 if (mvi->flags & MVF_FLAG_SOC)
676 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400677
Jeff Garzikb5762942007-10-25 20:58:22 -0400678 resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800679 sizeof(struct mvs_err_info) - i;
680 resp_len = min(resp_len, max_resp_len);
681
682 req_len = sizeof(struct ssp_frame_hdr) + 28;
Jeff Garzikb5762942007-10-25 20:58:22 -0400683
684 /* request, response lengths */
685 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
686
687 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400688 /* initiator, SSP, ftype 1h */
689 buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
690 buf_oaf[1] = dev->linkrate & 0xf;
691 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
692 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400693
Ke Wei8f261aa2008-02-23 21:15:27 +0800694 /* fill in SSP frame header (Command Table.SSP frame header) */
695 ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
Andy Yan20b09c22009-05-08 17:46:40 -0400696
697 if (is_tmf)
698 ssp_hdr->frame_type = SSP_TASK;
699 else
700 ssp_hdr->frame_type = SSP_COMMAND;
701
702 memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
Jeff Garzikb5762942007-10-25 20:58:22 -0400703 HASHED_SAS_ADDR_SIZE);
704 memcpy(ssp_hdr->hashed_src_addr,
Andy Yan20b09c22009-05-08 17:46:40 -0400705 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400706 ssp_hdr->tag = cpu_to_be16(tag);
707
Andy Yan20b09c22009-05-08 17:46:40 -0400708 /* fill in IU for TASK and Command Frame */
Jeff Garzikb5762942007-10-25 20:58:22 -0400709 buf_cmd += sizeof(*ssp_hdr);
710 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
Jeff Garzikb5762942007-10-25 20:58:22 -0400711
Andy Yan20b09c22009-05-08 17:46:40 -0400712 if (ssp_hdr->frame_type != SSP_TASK) {
713 buf_cmd[9] = fburst | task->ssp_task.task_attr |
714 (task->ssp_task.task_prio << 3);
715 memcpy(buf_cmd + 12, &task->ssp_task.cdb, 16);
716 } else{
717 buf_cmd[10] = tmf->tmf;
718 switch (tmf->tmf) {
719 case TMF_ABORT_TASK:
720 case TMF_QUERY_TASK:
721 buf_cmd[12] =
722 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
723 buf_cmd[13] =
724 tmf->tag_of_task_to_be_managed & 0xff;
725 break;
726 default:
727 break;
728 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400729 }
Andy Yan20b09c22009-05-08 17:46:40 -0400730 /* fill in PRD (scatter/gather) table, if any */
731 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Jeff Garzikb5762942007-10-25 20:58:22 -0400732 return 0;
733}
734
Andy Yan20b09c22009-05-08 17:46:40 -0400735#define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == NO_DEVICE)))
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700736static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
737 struct mvs_tmf_task *tmf, int *pass)
Jeff Garzikb5762942007-10-25 20:58:22 -0400738{
Ke Wei8f261aa2008-02-23 21:15:27 +0800739 struct domain_device *dev = task->dev;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700740 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400741 struct mvs_task_exec_info tei;
Ke Wei4e52fc02008-03-27 14:54:50 +0800742 struct mvs_slot_info *slot;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700743 u32 tag = 0xdeadbeef, n_elem = 0;
744 int rc = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400745
Andy Yan20b09c22009-05-08 17:46:40 -0400746 if (!dev->port) {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700747 struct task_status_struct *tsm = &task->task_status;
Andy Yan20b09c22009-05-08 17:46:40 -0400748
749 tsm->resp = SAS_TASK_UNDELIVERED;
750 tsm->stat = SAS_PHY_DOWN;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700751 /*
752 * libsas will use dev->port, should
753 * not call task_done for sata
754 */
Srinivas9dc9fd92010-02-15 00:00:00 -0600755 if (dev->dev_type != SATA_DEV)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700756 task->task_done(task);
757 return rc;
Andy Yan20b09c22009-05-08 17:46:40 -0400758 }
759
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700760 if (DEV_IS_GONE(mvi_dev)) {
761 if (mvi_dev)
762 mv_dprintk("device %d not ready.\n",
763 mvi_dev->device_id);
764 else
765 mv_dprintk("device %016llx not ready.\n",
766 SAS_ADDR(dev->sas_addr));
Andy Yan20b09c22009-05-08 17:46:40 -0400767
768 rc = SAS_PHY_DOWN;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700769 return rc;
770 }
771 tei.port = dev->port->lldd_port;
772 if (tei.port && !tei.port->port_attached && !tmf) {
773 if (sas_protocol_ata(task->task_proto)) {
774 struct task_status_struct *ts = &task->task_status;
775 mv_dprintk("SATA/STP port %d does not attach"
776 "device.\n", dev->port->id);
777 ts->resp = SAS_TASK_COMPLETE;
778 ts->stat = SAS_PHY_DOWN;
Andy Yan20b09c22009-05-08 17:46:40 -0400779
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700780 task->task_done(task);
Jeff Garzikb5762942007-10-25 20:58:22 -0400781
Ke Wei8f261aa2008-02-23 21:15:27 +0800782 } else {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700783 struct task_status_struct *ts = &task->task_status;
784 mv_dprintk("SAS port %d does not attach"
785 "device.\n", dev->port->id);
786 ts->resp = SAS_TASK_UNDELIVERED;
787 ts->stat = SAS_PHY_DOWN;
788 task->task_done(task);
Ke Wei8f261aa2008-02-23 21:15:27 +0800789 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700790 return rc;
791 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400792
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700793 if (!sas_protocol_ata(task->task_proto)) {
794 if (task->num_scatter) {
795 n_elem = dma_map_sg(mvi->dev,
796 task->scatter,
797 task->num_scatter,
798 task->data_dir);
799 if (!n_elem) {
800 rc = -ENOMEM;
801 goto prep_out;
802 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800803 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700804 } else {
805 n_elem = task->num_scatter;
806 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400807
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700808 rc = mvs_tag_alloc(mvi, &tag);
809 if (rc)
810 goto err_out;
Jeff Garzikb5762942007-10-25 20:58:22 -0400811
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700812 slot = &mvi->slot_info[tag];
Srinivas9dc9fd92010-02-15 00:00:00 -0600813
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700814 task->lldd_task = NULL;
815 slot->n_elem = n_elem;
816 slot->slot_tag = tag;
Jeff Garzikb5762942007-10-25 20:58:22 -0400817
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700818 slot->buf = pci_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
819 if (!slot->buf)
820 goto err_out_tag;
821 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
822
823 tei.task = task;
824 tei.hdr = &mvi->slot[tag];
825 tei.tag = tag;
826 tei.n_elem = n_elem;
827 switch (task->task_proto) {
828 case SAS_PROTOCOL_SMP:
829 rc = mvs_task_prep_smp(mvi, &tei);
830 break;
831 case SAS_PROTOCOL_SSP:
832 rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
833 break;
834 case SAS_PROTOCOL_SATA:
835 case SAS_PROTOCOL_STP:
836 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
837 rc = mvs_task_prep_ata(mvi, &tei);
838 break;
839 default:
840 dev_printk(KERN_ERR, mvi->dev,
841 "unknown sas_task proto: 0x%x\n",
842 task->task_proto);
843 rc = -EINVAL;
844 break;
845 }
846
847 if (rc) {
848 mv_dprintk("rc is %x\n", rc);
849 goto err_out_slot_buf;
850 }
851 slot->task = task;
852 slot->port = tei.port;
853 task->lldd_task = slot;
854 list_add_tail(&slot->entry, &tei.port->list);
855 spin_lock(&task->task_state_lock);
856 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
857 spin_unlock(&task->task_state_lock);
858
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700859 mvi_dev->running_req++;
860 ++(*pass);
861 mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
862
863 return rc;
864
865err_out_slot_buf:
866 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
Jeff Garzikb5762942007-10-25 20:58:22 -0400867err_out_tag:
Ke Wei8f261aa2008-02-23 21:15:27 +0800868 mvs_tag_free(mvi, tag);
Jeff Garzikb5762942007-10-25 20:58:22 -0400869err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400870
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700871 dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
872 if (!sas_protocol_ata(task->task_proto))
Ke Wei8f261aa2008-02-23 21:15:27 +0800873 if (n_elem)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700874 dma_unmap_sg(mvi->dev, task->scatter, n_elem,
875 task->data_dir);
876prep_out:
877 return rc;
878}
879
880static struct mvs_task_list *mvs_task_alloc_list(int *num, gfp_t gfp_flags)
881{
882 struct mvs_task_list *first = NULL;
883
884 for (; *num > 0; --*num) {
885 struct mvs_task_list *mvs_list = kmem_cache_zalloc(mvs_task_list_cache, gfp_flags);
886
887 if (!mvs_list)
888 break;
889
890 INIT_LIST_HEAD(&mvs_list->list);
891 if (!first)
892 first = mvs_list;
893 else
894 list_add_tail(&mvs_list->list, &first->list);
895
896 }
897
898 return first;
899}
900
901static inline void mvs_task_free_list(struct mvs_task_list *mvs_list)
902{
903 LIST_HEAD(list);
904 struct list_head *pos, *a;
905 struct mvs_task_list *mlist = NULL;
906
907 __list_add(&list, mvs_list->list.prev, &mvs_list->list);
908
909 list_for_each_safe(pos, a, &list) {
910 list_del_init(pos);
911 mlist = list_entry(pos, struct mvs_task_list, list);
912 kmem_cache_free(mvs_task_list_cache, mlist);
913 }
914}
915
916static int mvs_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
917 struct completion *completion, int is_tmf,
918 struct mvs_tmf_task *tmf)
919{
920 struct domain_device *dev = task->dev;
921 struct mvs_info *mvi = NULL;
922 u32 rc = 0;
923 u32 pass = 0;
924 unsigned long flags = 0;
925
926 mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
927
928 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
929 spin_unlock_irq(dev->sata_dev.ap->lock);
930
931 spin_lock_irqsave(&mvi->lock, flags);
932 rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
933 if (rc)
934 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
935
936 if (likely(pass))
937 MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
938 (MVS_CHIP_SLOT_SZ - 1));
Andy Yan0b84b702009-05-11 20:05:26 +0800939 spin_unlock_irqrestore(&mvi->lock, flags);
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700940
941 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
942 spin_lock_irq(dev->sata_dev.ap->lock);
943
944 return rc;
945}
946
947static int mvs_collector_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
948 struct completion *completion, int is_tmf,
949 struct mvs_tmf_task *tmf)
950{
951 struct domain_device *dev = task->dev;
952 struct mvs_prv_info *mpi = dev->port->ha->lldd_ha;
953 struct mvs_info *mvi = NULL;
954 struct sas_task *t = task;
955 struct mvs_task_list *mvs_list = NULL, *a;
956 LIST_HEAD(q);
957 int pass[2] = {0};
958 u32 rc = 0;
959 u32 n = num;
960 unsigned long flags = 0;
961
962 mvs_list = mvs_task_alloc_list(&n, gfp_flags);
963 if (n) {
964 printk(KERN_ERR "%s: mvs alloc list failed.\n", __func__);
965 rc = -ENOMEM;
966 goto free_list;
967 }
968
969 __list_add(&q, mvs_list->list.prev, &mvs_list->list);
970
971 list_for_each_entry(a, &q, list) {
972 a->task = t;
973 t = list_entry(t->list.next, struct sas_task, list);
974 }
975
976 list_for_each_entry(a, &q , list) {
977
978 t = a->task;
979 mvi = ((struct mvs_device *)t->dev->lldd_dev)->mvi_info;
980
981 spin_lock_irqsave(&mvi->lock, flags);
982 rc = mvs_task_prep(t, mvi, is_tmf, tmf, &pass[mvi->id]);
983 if (rc)
984 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
985 spin_unlock_irqrestore(&mvi->lock, flags);
986 }
987
988 if (likely(pass[0]))
989 MVS_CHIP_DISP->start_delivery(mpi->mvi[0],
990 (mpi->mvi[0]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
991
992 if (likely(pass[1]))
993 MVS_CHIP_DISP->start_delivery(mpi->mvi[1],
994 (mpi->mvi[1]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
995
996 list_del_init(&q);
997
998free_list:
999 if (mvs_list)
1000 mvs_task_free_list(mvs_list);
1001
Jeff Garzikb5762942007-10-25 20:58:22 -04001002 return rc;
1003}
1004
Andy Yan20b09c22009-05-08 17:46:40 -04001005int mvs_queue_command(struct sas_task *task, const int num,
1006 gfp_t gfp_flags)
1007{
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001008 struct mvs_device *mvi_dev = task->dev->lldd_dev;
1009 struct sas_ha_struct *sas = mvi_dev->mvi_info->sas;
1010
1011 if (sas->lldd_max_execute_num < 2)
1012 return mvs_task_exec(task, num, gfp_flags, NULL, 0, NULL);
1013 else
1014 return mvs_collector_task_exec(task, num, gfp_flags, NULL, 0, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04001015}
1016
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001017static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
Ke Wei8f261aa2008-02-23 21:15:27 +08001018{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001019 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1020 mvs_tag_clear(mvi, slot_idx);
1021}
Ke Wei8f261aa2008-02-23 21:15:27 +08001022
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001023static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
1024 struct mvs_slot_info *slot, u32 slot_idx)
1025{
Andy Yan20b09c22009-05-08 17:46:40 -04001026 if (!slot->task)
1027 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001028 if (!sas_protocol_ata(task->task_proto))
1029 if (slot->n_elem)
Andy Yan20b09c22009-05-08 17:46:40 -04001030 dma_unmap_sg(mvi->dev, task->scatter,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001031 slot->n_elem, task->data_dir);
Ke Wei8f261aa2008-02-23 21:15:27 +08001032
Ke Wei8f261aa2008-02-23 21:15:27 +08001033 switch (task->task_proto) {
1034 case SAS_PROTOCOL_SMP:
Andy Yan20b09c22009-05-08 17:46:40 -04001035 dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001036 PCI_DMA_FROMDEVICE);
Andy Yan20b09c22009-05-08 17:46:40 -04001037 dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001038 PCI_DMA_TODEVICE);
Ke Wei8f261aa2008-02-23 21:15:27 +08001039 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001040
Ke Wei8f261aa2008-02-23 21:15:27 +08001041 case SAS_PROTOCOL_SATA:
1042 case SAS_PROTOCOL_STP:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001043 case SAS_PROTOCOL_SSP:
Ke Wei8f261aa2008-02-23 21:15:27 +08001044 default:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001045 /* do nothing */
Ke Wei8f261aa2008-02-23 21:15:27 +08001046 break;
1047 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001048
1049 if (slot->buf) {
1050 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
1051 slot->buf = NULL;
1052 }
Andy Yan20b09c22009-05-08 17:46:40 -04001053 list_del_init(&slot->entry);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001054 task->lldd_task = NULL;
1055 slot->task = NULL;
1056 slot->port = NULL;
Andy Yan20b09c22009-05-08 17:46:40 -04001057 slot->slot_tag = 0xFFFFFFFF;
1058 mvs_slot_free(mvi, slot_idx);
Ke Wei8f261aa2008-02-23 21:15:27 +08001059}
1060
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001061static void mvs_update_wideport(struct mvs_info *mvi, int phy_no)
Ke Wei8f261aa2008-02-23 21:15:27 +08001062{
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001063 struct mvs_phy *phy = &mvi->phy[phy_no];
Ke Wei8f261aa2008-02-23 21:15:27 +08001064 struct mvs_port *port = phy->port;
1065 int j, no;
1066
Andy Yan20b09c22009-05-08 17:46:40 -04001067 for_each_phy(port->wide_port_phymap, j, no) {
1068 if (j & 1) {
1069 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1070 PHYR_WIDE_PORT);
1071 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
Ke Wei8f261aa2008-02-23 21:15:27 +08001072 port->wide_port_phymap);
1073 } else {
Andy Yan20b09c22009-05-08 17:46:40 -04001074 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1075 PHYR_WIDE_PORT);
1076 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1077 0);
Ke Wei8f261aa2008-02-23 21:15:27 +08001078 }
Andy Yan20b09c22009-05-08 17:46:40 -04001079 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001080}
1081
1082static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
1083{
1084 u32 tmp;
1085 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001086 struct mvs_port *port = phy->port;
Ke Wei8f261aa2008-02-23 21:15:27 +08001087
Andy Yan20b09c22009-05-08 17:46:40 -04001088 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001089 if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
Ke Wei963829e2008-03-27 14:55:23 +08001090 if (!port)
Ke Wei8f261aa2008-02-23 21:15:27 +08001091 phy->phy_attached = 1;
1092 return tmp;
1093 }
1094
Ke Wei8f261aa2008-02-23 21:15:27 +08001095 if (port) {
1096 if (phy->phy_type & PORT_TYPE_SAS) {
1097 port->wide_port_phymap &= ~(1U << i);
1098 if (!port->wide_port_phymap)
1099 port->port_attached = 0;
1100 mvs_update_wideport(mvi, i);
1101 } else if (phy->phy_type & PORT_TYPE_SATA)
1102 port->port_attached = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +08001103 phy->port = NULL;
1104 phy->phy_attached = 0;
1105 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1106 }
1107 return 0;
1108}
1109
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001110static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
1111{
1112 u32 *s = (u32 *) buf;
1113
1114 if (!s)
1115 return NULL;
1116
Andy Yan20b09c22009-05-08 17:46:40 -04001117 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001118 s[3] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001119
Andy Yan20b09c22009-05-08 17:46:40 -04001120 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001121 s[2] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001122
Andy Yan20b09c22009-05-08 17:46:40 -04001123 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001124 s[1] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001125
Andy Yan20b09c22009-05-08 17:46:40 -04001126 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001127 s[0] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Andy Yan20b09c22009-05-08 17:46:40 -04001128
1129 /* Workaround: take some ATAPI devices for ATA */
1130 if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1131 s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001132
Andy Yanf9da3be2009-05-14 20:41:21 -04001133 return s;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001134}
1135
1136static u32 mvs_is_sig_fis_received(u32 irq_status)
1137{
1138 return irq_status & PHYEV_SIG_FIS;
1139}
1140
Xiangliang Yu8882f082011-05-24 22:33:11 +08001141static void mvs_sig_remove_timer(struct mvs_phy *phy)
1142{
1143 if (phy->timer.function)
1144 del_timer(&phy->timer);
1145 phy->timer.function = NULL;
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)) {
Xiangliang Yu8882f082011-05-24 22:33:11 +08001170 mvs_sig_remove_timer(phy);
Andy Yan20b09c22009-05-08 17:46:40 -04001171 phy->phy_attached = 1;
1172 phy->att_dev_sas_addr =
1173 i + mvi->id * mvi->chip->n_phy;
1174 if (oob_done)
1175 sas_phy->oob_mode = SATA_OOB_MODE;
1176 phy->frame_rcvd_size =
1177 sizeof(struct dev_to_host_fis);
Andy Yanf9da3be2009-05-14 20:41:21 -04001178 mvs_get_d2h_reg(mvi, i, id);
Andy Yan20b09c22009-05-08 17:46:40 -04001179 } else {
1180 u32 tmp;
1181 dev_printk(KERN_DEBUG, mvi->dev,
1182 "Phy%d : No sig fis\n", i);
1183 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1184 MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1185 tmp | PHYEV_SIG_FIS);
1186 phy->phy_attached = 0;
1187 phy->phy_type &= ~PORT_TYPE_SATA;
Andy Yan20b09c22009-05-08 17:46:40 -04001188 goto out_done;
1189 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001190 } else if (phy->phy_type & PORT_TYPE_SAS
Andy Yan20b09c22009-05-08 17:46:40 -04001191 || 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 }
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001213 mv_dprintk("phy %d attach dev info is %x\n",
Andy Yan20b09c22009-05-08 17:46:40 -04001214 i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001215 mv_dprintk("phy %d attach sas addr is %llx\n",
Andy Yan20b09c22009-05-08 17:46:40 -04001216 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];
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001240 if (i >= mvi->chip->n_phy)
1241 port = &mvi->port[i - mvi->chip->n_phy];
Andy Yan20b09c22009-05-08 17:46:40 -04001242 else
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001243 port = &mvi->port[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001244 if (lock)
1245 spin_lock_irqsave(&mvi->lock, flags);
1246 port->port_attached = 1;
1247 phy->port = port;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001248 sas_port->lldd_port = port;
Andy Yan20b09c22009-05-08 17:46:40 -04001249 if (phy->phy_type & PORT_TYPE_SAS) {
1250 port->wide_port_phymap = sas_port->phy_mask;
1251 mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1252 mvs_update_wideport(mvi, sas_phy->id);
1253 }
1254 if (lock)
1255 spin_unlock_irqrestore(&mvi->lock, flags);
1256}
1257
1258static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1259{
Srinivas9dc9fd92010-02-15 00:00:00 -06001260 struct domain_device *dev;
1261 struct mvs_phy *phy = sas_phy->lldd_phy;
1262 struct mvs_info *mvi = phy->mvi;
1263 struct asd_sas_port *port = sas_phy->port;
1264 int phy_no = 0;
1265
1266 while (phy != &mvi->phy[phy_no]) {
1267 phy_no++;
1268 if (phy_no >= MVS_MAX_PHYS)
1269 return;
1270 }
1271 list_for_each_entry(dev, &port->dev_list, dev_list_node)
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001272 mvs_do_release_task(phy->mvi, phy_no, dev);
Srinivas9dc9fd92010-02-15 00:00:00 -06001273
Andy Yan20b09c22009-05-08 17:46:40 -04001274}
1275
1276
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001277void mvs_port_formed(struct asd_sas_phy *sas_phy)
Ke Wei8f261aa2008-02-23 21:15:27 +08001278{
Andy Yan20b09c22009-05-08 17:46:40 -04001279 mvs_port_notify_formed(sas_phy, 1);
1280}
Ke Wei8f261aa2008-02-23 21:15:27 +08001281
Andy Yan20b09c22009-05-08 17:46:40 -04001282void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1283{
1284 mvs_port_notify_deformed(sas_phy, 1);
1285}
1286
1287struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
1288{
1289 u32 dev;
1290 for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
1291 if (mvi->devices[dev].dev_type == NO_DEVICE) {
1292 mvi->devices[dev].device_id = dev;
1293 return &mvi->devices[dev];
1294 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001295 }
Andy Yan20b09c22009-05-08 17:46:40 -04001296
1297 if (dev == MVS_MAX_DEVICES)
1298 mv_printk("max support %d devices, ignore ..\n",
1299 MVS_MAX_DEVICES);
1300
1301 return NULL;
1302}
1303
1304void mvs_free_dev(struct mvs_device *mvi_dev)
1305{
1306 u32 id = mvi_dev->device_id;
1307 memset(mvi_dev, 0, sizeof(*mvi_dev));
1308 mvi_dev->device_id = id;
1309 mvi_dev->dev_type = NO_DEVICE;
1310 mvi_dev->dev_status = MVS_DEV_NORMAL;
1311 mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1312}
1313
1314int mvs_dev_found_notify(struct domain_device *dev, int lock)
1315{
1316 unsigned long flags = 0;
1317 int res = 0;
1318 struct mvs_info *mvi = NULL;
1319 struct domain_device *parent_dev = dev->parent;
1320 struct mvs_device *mvi_device;
1321
1322 mvi = mvs_find_dev_mvi(dev);
1323
1324 if (lock)
1325 spin_lock_irqsave(&mvi->lock, flags);
1326
1327 mvi_device = mvs_alloc_dev(mvi);
1328 if (!mvi_device) {
1329 res = -1;
1330 goto found_out;
1331 }
Andy Yanf9da3be2009-05-14 20:41:21 -04001332 dev->lldd_dev = mvi_device;
Srinivas9dc9fd92010-02-15 00:00:00 -06001333 mvi_device->dev_status = MVS_DEV_NORMAL;
Andy Yan20b09c22009-05-08 17:46:40 -04001334 mvi_device->dev_type = dev->dev_type;
Andy Yan9870d9a2009-05-11 22:19:25 +08001335 mvi_device->mvi_info = mvi;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001336 mvi_device->sas_device = dev;
Andy Yan20b09c22009-05-08 17:46:40 -04001337 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1338 int phy_id;
1339 u8 phy_num = parent_dev->ex_dev.num_phys;
1340 struct ex_phy *phy;
1341 for (phy_id = 0; phy_id < phy_num; phy_id++) {
1342 phy = &parent_dev->ex_dev.ex_phy[phy_id];
1343 if (SAS_ADDR(phy->attached_sas_addr) ==
1344 SAS_ADDR(dev->sas_addr)) {
1345 mvi_device->attached_phy = phy_id;
1346 break;
1347 }
1348 }
1349
1350 if (phy_id == phy_num) {
1351 mv_printk("Error: no attached dev:%016llx"
1352 "at ex:%016llx.\n",
1353 SAS_ADDR(dev->sas_addr),
1354 SAS_ADDR(parent_dev->sas_addr));
1355 res = -1;
1356 }
1357 }
1358
1359found_out:
1360 if (lock)
1361 spin_unlock_irqrestore(&mvi->lock, flags);
1362 return res;
1363}
1364
1365int mvs_dev_found(struct domain_device *dev)
1366{
1367 return mvs_dev_found_notify(dev, 1);
1368}
1369
Srinivas9dc9fd92010-02-15 00:00:00 -06001370void mvs_dev_gone_notify(struct domain_device *dev)
Andy Yan20b09c22009-05-08 17:46:40 -04001371{
1372 unsigned long flags = 0;
Andy Yanf9da3be2009-05-14 20:41:21 -04001373 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001374 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001375
Srinivas9dc9fd92010-02-15 00:00:00 -06001376 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001377
1378 if (mvi_dev) {
1379 mv_dprintk("found dev[%d:%x] is gone.\n",
1380 mvi_dev->device_id, mvi_dev->dev_type);
Srinivas9dc9fd92010-02-15 00:00:00 -06001381 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001382 mvs_free_reg_set(mvi, mvi_dev);
1383 mvs_free_dev(mvi_dev);
1384 } else {
1385 mv_dprintk("found dev has gone.\n");
1386 }
1387 dev->lldd_dev = NULL;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001388 mvi_dev->sas_device = NULL;
Andy Yan20b09c22009-05-08 17:46:40 -04001389
Srinivas9dc9fd92010-02-15 00:00:00 -06001390 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001391}
1392
1393
1394void mvs_dev_gone(struct domain_device *dev)
1395{
Srinivas9dc9fd92010-02-15 00:00:00 -06001396 mvs_dev_gone_notify(dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001397}
1398
1399static struct sas_task *mvs_alloc_task(void)
1400{
1401 struct sas_task *task = kzalloc(sizeof(struct sas_task), GFP_KERNEL);
1402
1403 if (task) {
1404 INIT_LIST_HEAD(&task->list);
1405 spin_lock_init(&task->task_state_lock);
1406 task->task_state_flags = SAS_TASK_STATE_PENDING;
1407 init_timer(&task->timer);
1408 init_completion(&task->completion);
1409 }
1410 return task;
1411}
1412
1413static void mvs_free_task(struct sas_task *task)
1414{
1415 if (task) {
1416 BUG_ON(!list_empty(&task->list));
1417 kfree(task);
1418 }
1419}
1420
1421static void mvs_task_done(struct sas_task *task)
1422{
1423 if (!del_timer(&task->timer))
1424 return;
1425 complete(&task->completion);
1426}
1427
1428static void mvs_tmf_timedout(unsigned long data)
1429{
1430 struct sas_task *task = (struct sas_task *)data;
1431
1432 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1433 complete(&task->completion);
1434}
1435
1436/* XXX */
1437#define MVS_TASK_TIMEOUT 20
1438static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1439 void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1440{
1441 int res, retry;
1442 struct sas_task *task = NULL;
1443
1444 for (retry = 0; retry < 3; retry++) {
1445 task = mvs_alloc_task();
1446 if (!task)
1447 return -ENOMEM;
1448
1449 task->dev = dev;
1450 task->task_proto = dev->tproto;
1451
1452 memcpy(&task->ssp_task, parameter, para_len);
1453 task->task_done = mvs_task_done;
1454
1455 task->timer.data = (unsigned long) task;
1456 task->timer.function = mvs_tmf_timedout;
1457 task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1458 add_timer(&task->timer);
1459
Andy Yan0b84b702009-05-11 20:05:26 +08001460 res = mvs_task_exec(task, 1, GFP_KERNEL, NULL, 1, tmf);
Andy Yan20b09c22009-05-08 17:46:40 -04001461
1462 if (res) {
1463 del_timer(&task->timer);
1464 mv_printk("executing internel task failed:%d\n", res);
1465 goto ex_err;
1466 }
1467
1468 wait_for_completion(&task->completion);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001469 res = TMF_RESP_FUNC_FAILED;
Andy Yan20b09c22009-05-08 17:46:40 -04001470 /* Even TMF timed out, return direct. */
1471 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1472 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1473 mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1474 goto ex_err;
1475 }
1476 }
1477
1478 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -05001479 task->task_status.stat == SAM_STAT_GOOD) {
Andy Yan20b09c22009-05-08 17:46:40 -04001480 res = TMF_RESP_FUNC_COMPLETE;
1481 break;
1482 }
1483
1484 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1485 task->task_status.stat == SAS_DATA_UNDERRUN) {
1486 /* no error, but return the number of bytes of
1487 * underrun */
1488 res = task->task_status.residual;
1489 break;
1490 }
1491
1492 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1493 task->task_status.stat == SAS_DATA_OVERRUN) {
1494 mv_dprintk("blocked task error.\n");
1495 res = -EMSGSIZE;
1496 break;
1497 } else {
1498 mv_dprintk(" task to dev %016llx response: 0x%x "
1499 "status 0x%x\n",
1500 SAS_ADDR(dev->sas_addr),
1501 task->task_status.resp,
1502 task->task_status.stat);
1503 mvs_free_task(task);
1504 task = NULL;
1505
1506 }
1507 }
1508ex_err:
1509 BUG_ON(retry == 3 && task != NULL);
1510 if (task != NULL)
1511 mvs_free_task(task);
1512 return res;
1513}
1514
1515static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1516 u8 *lun, struct mvs_tmf_task *tmf)
1517{
1518 struct sas_ssp_task ssp_task;
Andy Yan20b09c22009-05-08 17:46:40 -04001519 if (!(dev->tproto & SAS_PROTOCOL_SSP))
1520 return TMF_RESP_FUNC_ESUPP;
1521
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001522 memcpy(ssp_task.LUN, lun, 8);
Andy Yan20b09c22009-05-08 17:46:40 -04001523
1524 return mvs_exec_internal_tmf_task(dev, &ssp_task,
1525 sizeof(ssp_task), tmf);
1526}
1527
1528
1529/* Standard mandates link reset for ATA (type 0)
1530 and hard reset for SSP (type 1) , only for RECOVERY */
1531static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1532{
1533 int rc;
1534 struct sas_phy *phy = sas_find_local_phy(dev);
1535 int reset_type = (dev->dev_type == SATA_DEV ||
1536 (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1537 rc = sas_phy_reset(phy, reset_type);
1538 msleep(2000);
1539 return rc;
1540}
1541
1542/* mandatory SAM-3 */
1543int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1544{
1545 unsigned long flags;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001546 int rc = TMF_RESP_FUNC_FAILED;
Andy Yan20b09c22009-05-08 17:46:40 -04001547 struct mvs_tmf_task tmf_task;
Andy Yanf9da3be2009-05-14 20:41:21 -04001548 struct mvs_device * mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001549 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001550
1551 tmf_task.tmf = TMF_LU_RESET;
1552 mvi_dev->dev_status = MVS_DEV_EH;
1553 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1554 if (rc == TMF_RESP_FUNC_COMPLETE) {
Andy Yan20b09c22009-05-08 17:46:40 -04001555 spin_lock_irqsave(&mvi->lock, flags);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001556 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001557 spin_unlock_irqrestore(&mvi->lock, flags);
1558 }
1559 /* If failed, fall-through I_T_Nexus reset */
1560 mv_printk("%s for device[%x]:rc= %d\n", __func__,
1561 mvi_dev->device_id, rc);
1562 return rc;
Jeff Garzikb5762942007-10-25 20:58:22 -04001563}
1564
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001565int mvs_I_T_nexus_reset(struct domain_device *dev)
Ke Weie9ff91b2008-03-27 14:55:33 +08001566{
Andy Yan20b09c22009-05-08 17:46:40 -04001567 unsigned long flags;
Srinivas9dc9fd92010-02-15 00:00:00 -06001568 int rc = TMF_RESP_FUNC_FAILED;
1569 struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001570 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001571
1572 if (mvi_dev->dev_status != MVS_DEV_EH)
1573 return TMF_RESP_FUNC_COMPLETE;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001574 else
1575 mvi_dev->dev_status = MVS_DEV_NORMAL;
Andy Yan20b09c22009-05-08 17:46:40 -04001576 rc = mvs_debug_I_T_nexus_reset(dev);
1577 mv_printk("%s for device[%x]:rc= %d\n",
1578 __func__, mvi_dev->device_id, rc);
1579
1580 /* housekeeper */
Andy Yan20b09c22009-05-08 17:46:40 -04001581 spin_lock_irqsave(&mvi->lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001582 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001583 spin_unlock_irqrestore(&mvi->lock, flags);
1584
1585 return rc;
1586}
1587/* optional SAM-3 */
1588int mvs_query_task(struct sas_task *task)
1589{
1590 u32 tag;
1591 struct scsi_lun lun;
1592 struct mvs_tmf_task tmf_task;
1593 int rc = TMF_RESP_FUNC_FAILED;
1594
1595 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1596 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1597 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001598 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1599 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001600
1601 int_to_scsilun(cmnd->device->lun, &lun);
1602 rc = mvs_find_tag(mvi, task, &tag);
1603 if (rc == 0) {
1604 rc = TMF_RESP_FUNC_FAILED;
1605 return rc;
1606 }
1607
1608 tmf_task.tmf = TMF_QUERY_TASK;
1609 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1610
1611 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1612 switch (rc) {
1613 /* The task is still in Lun, release it then */
1614 case TMF_RESP_FUNC_SUCC:
1615 /* The task is not in Lun or failed, reset the phy */
1616 case TMF_RESP_FUNC_FAILED:
1617 case TMF_RESP_FUNC_COMPLETE:
1618 break;
1619 }
1620 }
1621 mv_printk("%s:rc= %d\n", __func__, rc);
1622 return rc;
1623}
1624
1625/* mandatory SAM-3, still need free task/slot info */
1626int mvs_abort_task(struct sas_task *task)
1627{
1628 struct scsi_lun lun;
1629 struct mvs_tmf_task tmf_task;
1630 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001631 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
Jiri Slaby24ae1632010-06-22 13:42:02 +02001632 struct mvs_info *mvi;
Andy Yan20b09c22009-05-08 17:46:40 -04001633 int rc = TMF_RESP_FUNC_FAILED;
1634 unsigned long flags;
1635 u32 tag;
Andy Yan9870d9a2009-05-11 22:19:25 +08001636
Srinivas9dc9fd92010-02-15 00:00:00 -06001637 if (!mvi_dev) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001638 mv_printk("Device has removed\n");
1639 return TMF_RESP_FUNC_FAILED;
Srinivas9dc9fd92010-02-15 00:00:00 -06001640 }
1641
Jiri Slaby24ae1632010-06-22 13:42:02 +02001642 mvi = mvi_dev->mvi_info;
1643
Andy Yan20b09c22009-05-08 17:46:40 -04001644 spin_lock_irqsave(&task->task_state_lock, flags);
1645 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1646 spin_unlock_irqrestore(&task->task_state_lock, flags);
1647 rc = TMF_RESP_FUNC_COMPLETE;
1648 goto out;
1649 }
1650 spin_unlock_irqrestore(&task->task_state_lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001651 mvi_dev->dev_status = MVS_DEV_EH;
Andy Yan20b09c22009-05-08 17:46:40 -04001652 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1653 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1654
1655 int_to_scsilun(cmnd->device->lun, &lun);
1656 rc = mvs_find_tag(mvi, task, &tag);
1657 if (rc == 0) {
1658 mv_printk("No such tag in %s\n", __func__);
1659 rc = TMF_RESP_FUNC_FAILED;
1660 return rc;
1661 }
1662
1663 tmf_task.tmf = TMF_ABORT_TASK;
1664 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1665
1666 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1667
1668 /* if successful, clear the task and callback forwards.*/
1669 if (rc == TMF_RESP_FUNC_COMPLETE) {
1670 u32 slot_no;
1671 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -04001672
1673 if (task->lldd_task) {
Andy Yanf9da3be2009-05-14 20:41:21 -04001674 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -04001675 slot_no = (u32) (slot - mvi->slot_info);
Srinivas9dc9fd92010-02-15 00:00:00 -06001676 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001677 mvs_slot_complete(mvi, slot_no, 1);
Srinivas9dc9fd92010-02-15 00:00:00 -06001678 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001679 }
1680 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001681
Andy Yan20b09c22009-05-08 17:46:40 -04001682 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1683 task->task_proto & SAS_PROTOCOL_STP) {
1684 /* to do free register_set */
Srinivas9dc9fd92010-02-15 00:00:00 -06001685 if (SATA_DEV == dev->dev_type) {
1686 struct mvs_slot_info *slot = task->lldd_task;
Srinivas9dc9fd92010-02-15 00:00:00 -06001687 u32 slot_idx = (u32)(slot - mvi->slot_info);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001688 mv_dprintk("mvs_abort_task() mvi=%p task=%p "
Srinivas9dc9fd92010-02-15 00:00:00 -06001689 "slot=%p slot_idx=x%x\n",
1690 mvi, task, slot, slot_idx);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001691 mvs_tmf_timedout((unsigned long)task);
Srinivas9dc9fd92010-02-15 00:00:00 -06001692 mvs_slot_task_free(mvi, task, slot, slot_idx);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001693 rc = TMF_RESP_FUNC_COMPLETE;
1694 goto out;
Srinivas9dc9fd92010-02-15 00:00:00 -06001695 }
Andy Yan20b09c22009-05-08 17:46:40 -04001696
1697 }
1698out:
1699 if (rc != TMF_RESP_FUNC_COMPLETE)
1700 mv_printk("%s:rc= %d\n", __func__, rc);
1701 return rc;
1702}
1703
1704int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1705{
1706 int rc = TMF_RESP_FUNC_FAILED;
1707 struct mvs_tmf_task tmf_task;
1708
1709 tmf_task.tmf = TMF_ABORT_TASK_SET;
1710 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1711
1712 return rc;
1713}
1714
1715int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1716{
1717 int rc = TMF_RESP_FUNC_FAILED;
1718 struct mvs_tmf_task tmf_task;
1719
1720 tmf_task.tmf = TMF_CLEAR_ACA;
1721 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1722
1723 return rc;
1724}
1725
1726int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1727{
1728 int rc = TMF_RESP_FUNC_FAILED;
1729 struct mvs_tmf_task tmf_task;
1730
1731 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1732 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1733
1734 return rc;
Ke Weie9ff91b2008-03-27 14:55:33 +08001735}
1736
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001737static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1738 u32 slot_idx, int err)
1739{
Andy Yanf9da3be2009-05-14 20:41:21 -04001740 struct mvs_device *mvi_dev = task->dev->lldd_dev;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001741 struct task_status_struct *tstat = &task->task_status;
1742 struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
James Bottomleydf64d3c2010-07-27 15:51:13 -05001743 int stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001744
Andy Yan20b09c22009-05-08 17:46:40 -04001745
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001746 resp->frame_len = sizeof(struct dev_to_host_fis);
1747 memcpy(&resp->ending_fis[0],
Andy Yan20b09c22009-05-08 17:46:40 -04001748 SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001749 sizeof(struct dev_to_host_fis));
1750 tstat->buf_valid_size = sizeof(*resp);
Srinivas9dc9fd92010-02-15 00:00:00 -06001751 if (unlikely(err)) {
1752 if (unlikely(err & CMD_ISS_STPD))
1753 stat = SAS_OPEN_REJECT;
1754 else
1755 stat = SAS_PROTO_RESPONSE;
1756 }
1757
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001758 return stat;
1759}
1760
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001761void mvs_set_sense(u8 *buffer, int len, int d_sense,
1762 int key, int asc, int ascq)
1763{
1764 memset(buffer, 0, len);
1765
1766 if (d_sense) {
1767 /* Descriptor format */
1768 if (len < 4) {
1769 mv_printk("Length %d of sense buffer too small to "
1770 "fit sense %x:%x:%x", len, key, asc, ascq);
1771 }
1772
1773 buffer[0] = 0x72; /* Response Code */
1774 if (len > 1)
1775 buffer[1] = key; /* Sense Key */
1776 if (len > 2)
1777 buffer[2] = asc; /* ASC */
1778 if (len > 3)
1779 buffer[3] = ascq; /* ASCQ */
1780 } else {
1781 if (len < 14) {
1782 mv_printk("Length %d of sense buffer too small to "
1783 "fit sense %x:%x:%x", len, key, asc, ascq);
1784 }
1785
1786 buffer[0] = 0x70; /* Response Code */
1787 if (len > 2)
1788 buffer[2] = key; /* Sense Key */
1789 if (len > 7)
1790 buffer[7] = 0x0a; /* Additional Sense Length */
1791 if (len > 12)
1792 buffer[12] = asc; /* ASC */
1793 if (len > 13)
1794 buffer[13] = ascq; /* ASCQ */
1795 }
1796
1797 return;
1798}
1799
1800void mvs_fill_ssp_resp_iu(struct ssp_response_iu *iu,
1801 u8 key, u8 asc, u8 asc_q)
1802{
1803 iu->datapres = 2;
1804 iu->response_data_len = 0;
1805 iu->sense_data_len = 17;
1806 iu->status = 02;
1807 mvs_set_sense(iu->sense_data, 17, 0,
1808 key, asc, asc_q);
1809}
1810
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001811static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1812 u32 slot_idx)
1813{
1814 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
Andy Yan20b09c22009-05-08 17:46:40 -04001815 int stat;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001816 u32 err_dw0 = le32_to_cpu(*(u32 *)slot->response);
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001817 u32 err_dw1 = le32_to_cpu(*((u32 *)slot->response + 1));
Andy Yan20b09c22009-05-08 17:46:40 -04001818 u32 tfs = 0;
1819 enum mvs_port_type type = PORT_TYPE_SAS;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001820
Andy Yan20b09c22009-05-08 17:46:40 -04001821 if (err_dw0 & CMD_ISS_STPD)
1822 MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1823
1824 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1825
James Bottomleydf64d3c2010-07-27 15:51:13 -05001826 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001827 switch (task->task_proto) {
1828 case SAS_PROTOCOL_SSP:
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001829 {
Andy Yan20b09c22009-05-08 17:46:40 -04001830 stat = SAS_ABORTED_TASK;
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001831 if ((err_dw0 & NO_DEST) || err_dw1 & bit(31)) {
1832 struct ssp_response_iu *iu = slot->response +
1833 sizeof(struct mvs_err_info);
1834 mvs_fill_ssp_resp_iu(iu, NOT_READY, 0x04, 01);
1835 sas_ssp_task_response(mvi->dev, task, iu);
1836 stat = SAM_STAT_CHECK_CONDITION;
1837 }
1838 if (err_dw1 & bit(31))
1839 mv_printk("reuse same slot, retry command.\n");
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001840 break;
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001841 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001842 case SAS_PROTOCOL_SMP:
James Bottomleydf64d3c2010-07-27 15:51:13 -05001843 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001844 break;
Andy Yan20b09c22009-05-08 17:46:40 -04001845
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001846 case SAS_PROTOCOL_SATA:
1847 case SAS_PROTOCOL_STP:
1848 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
Andy Yan20b09c22009-05-08 17:46:40 -04001849 {
Andy Yan20b09c22009-05-08 17:46:40 -04001850 task->ata_task.use_ncq = 0;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001851 stat = SAS_PROTO_RESPONSE;
Srinivas9dc9fd92010-02-15 00:00:00 -06001852 mvs_sata_done(mvi, task, slot_idx, err_dw0);
Andy Yan20b09c22009-05-08 17:46:40 -04001853 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001854 break;
1855 default:
1856 break;
1857 }
1858
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001859 return stat;
1860}
1861
Andy Yan20b09c22009-05-08 17:46:40 -04001862int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001863{
1864 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1865 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1866 struct sas_task *task = slot->task;
Andy Yan20b09c22009-05-08 17:46:40 -04001867 struct mvs_device *mvi_dev = NULL;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001868 struct task_status_struct *tstat;
Srinivas9dc9fd92010-02-15 00:00:00 -06001869 struct domain_device *dev;
1870 u32 aborted;
Andy Yan20b09c22009-05-08 17:46:40 -04001871
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001872 void *to;
Andy Yan20b09c22009-05-08 17:46:40 -04001873 enum exec_status sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001874
Srinivas9dc9fd92010-02-15 00:00:00 -06001875 if (unlikely(!task || !task->lldd_task || !task->dev))
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001876 return -1;
1877
Andy Yan20b09c22009-05-08 17:46:40 -04001878 tstat = &task->task_status;
Srinivas9dc9fd92010-02-15 00:00:00 -06001879 dev = task->dev;
1880 mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -04001881
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001882 spin_lock(&task->task_state_lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001883 task->task_state_flags &=
1884 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1885 task->task_state_flags |= SAS_TASK_STATE_DONE;
1886 /* race condition*/
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001887 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001888 spin_unlock(&task->task_state_lock);
1889
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001890 memset(tstat, 0, sizeof(*tstat));
1891 tstat->resp = SAS_TASK_COMPLETE;
1892
Andy Yan20b09c22009-05-08 17:46:40 -04001893 if (unlikely(aborted)) {
1894 tstat->stat = SAS_ABORTED_TASK;
Srinivas9dc9fd92010-02-15 00:00:00 -06001895 if (mvi_dev && mvi_dev->running_req)
1896 mvi_dev->running_req--;
Andy Yan20b09c22009-05-08 17:46:40 -04001897 if (sas_protocol_ata(task->task_proto))
1898 mvs_free_reg_set(mvi, mvi_dev);
1899
1900 mvs_slot_task_free(mvi, task, slot, slot_idx);
1901 return -1;
1902 }
1903
Srinivas9dc9fd92010-02-15 00:00:00 -06001904 if (unlikely(!mvi_dev || flags)) {
1905 if (!mvi_dev)
1906 mv_dprintk("port has not device.\n");
Andy Yan20b09c22009-05-08 17:46:40 -04001907 tstat->stat = SAS_PHY_DOWN;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001908 goto out;
1909 }
1910
1911 /* error info record present */
1912 if (unlikely((rx_desc & RXQ_ERR) && (*(u64 *) slot->response))) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001913 mv_dprintk("port %d slot %d rx_desc %X has error info"
1914 "%016llX.\n", slot->port->sas_port.id, slot_idx,
1915 rx_desc, (u64)(*(u64 *)slot->response));
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001916 tstat->stat = mvs_slot_err(mvi, task, slot_idx);
Srinivas9dc9fd92010-02-15 00:00:00 -06001917 tstat->resp = SAS_TASK_COMPLETE;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001918 goto out;
1919 }
1920
1921 switch (task->task_proto) {
1922 case SAS_PROTOCOL_SSP:
1923 /* hw says status == 0, datapres == 0 */
1924 if (rx_desc & RXQ_GOOD) {
James Bottomleydf64d3c2010-07-27 15:51:13 -05001925 tstat->stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001926 tstat->resp = SAS_TASK_COMPLETE;
1927 }
1928 /* response frame present */
1929 else if (rx_desc & RXQ_RSP) {
Andy Yan20b09c22009-05-08 17:46:40 -04001930 struct ssp_response_iu *iu = slot->response +
1931 sizeof(struct mvs_err_info);
1932 sas_ssp_task_response(mvi->dev, task, iu);
1933 } else
James Bottomleydf64d3c2010-07-27 15:51:13 -05001934 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001935 break;
1936
1937 case SAS_PROTOCOL_SMP: {
1938 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
James Bottomleydf64d3c2010-07-27 15:51:13 -05001939 tstat->stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001940 to = kmap_atomic(sg_page(sg_resp), KM_IRQ0);
1941 memcpy(to + sg_resp->offset,
1942 slot->response + sizeof(struct mvs_err_info),
1943 sg_dma_len(sg_resp));
1944 kunmap_atomic(to, KM_IRQ0);
1945 break;
1946 }
1947
1948 case SAS_PROTOCOL_SATA:
1949 case SAS_PROTOCOL_STP:
1950 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
1951 tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
1952 break;
1953 }
1954
1955 default:
James Bottomleydf64d3c2010-07-27 15:51:13 -05001956 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001957 break;
1958 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001959 if (!slot->port->port_attached) {
1960 mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
1961 tstat->stat = SAS_PHY_DOWN;
1962 }
1963
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001964
1965out:
Srinivas9dc9fd92010-02-15 00:00:00 -06001966 if (mvi_dev && mvi_dev->running_req) {
1967 mvi_dev->running_req--;
1968 if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
Andy Yan0f980a82009-05-11 21:49:52 +08001969 mvs_free_reg_set(mvi, mvi_dev);
1970 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001971 mvs_slot_task_free(mvi, task, slot, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04001972 sts = tstat->stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001973
1974 spin_unlock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001975 if (task->task_done)
1976 task->task_done(task);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001977
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001978 spin_lock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001979
1980 return sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001981}
1982
Srinivas9dc9fd92010-02-15 00:00:00 -06001983void mvs_do_release_task(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -04001984 int phy_no, struct domain_device *dev)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001985{
Srinivas9dc9fd92010-02-15 00:00:00 -06001986 u32 slot_idx;
Andy Yan20b09c22009-05-08 17:46:40 -04001987 struct mvs_phy *phy;
1988 struct mvs_port *port;
1989 struct mvs_slot_info *slot, *slot2;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001990
Andy Yan20b09c22009-05-08 17:46:40 -04001991 phy = &mvi->phy[phy_no];
1992 port = phy->port;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001993 if (!port)
1994 return;
Srinivas9dc9fd92010-02-15 00:00:00 -06001995 /* clean cmpl queue in case request is already finished */
1996 mvs_int_rx(mvi, false);
1997
1998
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001999
Andy Yan20b09c22009-05-08 17:46:40 -04002000 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
2001 struct sas_task *task;
2002 slot_idx = (u32) (slot - mvi->slot_info);
2003 task = slot->task;
2004
2005 if (dev && task->dev != dev)
2006 continue;
2007
2008 mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
2009 slot_idx, slot->slot_tag, task);
Srinivas9dc9fd92010-02-15 00:00:00 -06002010 MVS_CHIP_DISP->command_active(mvi, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04002011
2012 mvs_slot_complete(mvi, slot_idx, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002013 }
2014}
2015
Srinivas9dc9fd92010-02-15 00:00:00 -06002016void mvs_release_task(struct mvs_info *mvi,
2017 struct domain_device *dev)
2018{
2019 int i, phyno[WIDE_PORT_MAX_PHY], num;
2020 /* housekeeper */
2021 num = mvs_find_dev_phyno(dev, phyno);
2022 for (i = 0; i < num; i++)
2023 mvs_do_release_task(mvi, phyno[i], dev);
2024}
2025
Andy Yan20b09c22009-05-08 17:46:40 -04002026static void mvs_phy_disconnected(struct mvs_phy *phy)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002027{
Andy Yan20b09c22009-05-08 17:46:40 -04002028 phy->phy_attached = 0;
2029 phy->att_dev_info = 0;
2030 phy->att_dev_sas_addr = 0;
2031}
2032
2033static void mvs_work_queue(struct work_struct *work)
2034{
2035 struct delayed_work *dw = container_of(work, struct delayed_work, work);
2036 struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
2037 struct mvs_info *mvi = mwq->mvi;
2038 unsigned long flags;
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002039 u32 phy_no = (unsigned long) mwq->data;
2040 struct sas_ha_struct *sas_ha = mvi->sas;
2041 struct mvs_phy *phy = &mvi->phy[phy_no];
2042 struct asd_sas_phy *sas_phy = &phy->sas_phy;
Andy Yan20b09c22009-05-08 17:46:40 -04002043
2044 spin_lock_irqsave(&mvi->lock, flags);
2045 if (mwq->handler & PHY_PLUG_EVENT) {
Andy Yan20b09c22009-05-08 17:46:40 -04002046
2047 if (phy->phy_event & PHY_PLUG_OUT) {
2048 u32 tmp;
2049 struct sas_identify_frame *id;
2050 id = (struct sas_identify_frame *)phy->frame_rcvd;
2051 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
2052 phy->phy_event &= ~PHY_PLUG_OUT;
2053 if (!(tmp & PHY_READY_MASK)) {
2054 sas_phy_disconnected(sas_phy);
2055 mvs_phy_disconnected(phy);
2056 sas_ha->notify_phy_event(sas_phy,
2057 PHYE_LOSS_OF_SIGNAL);
2058 mv_dprintk("phy%d Removed Device\n", phy_no);
2059 } else {
2060 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2061 mvs_update_phyinfo(mvi, phy_no, 1);
2062 mvs_bytes_dmaed(mvi, phy_no);
2063 mvs_port_notify_formed(sas_phy, 0);
2064 mv_dprintk("phy%d Attached Device\n", phy_no);
2065 }
2066 }
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002067 } else if (mwq->handler & EXP_BRCT_CHG) {
2068 phy->phy_event &= ~EXP_BRCT_CHG;
2069 sas_ha->notify_port_event(sas_phy,
2070 PORTE_BROADCAST_RCVD);
2071 mv_dprintk("phy%d Got Broadcast Change\n", phy_no);
Andy Yan20b09c22009-05-08 17:46:40 -04002072 }
2073 list_del(&mwq->entry);
2074 spin_unlock_irqrestore(&mvi->lock, flags);
2075 kfree(mwq);
2076}
2077
2078static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
2079{
2080 struct mvs_wq *mwq;
2081 int ret = 0;
2082
2083 mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
2084 if (mwq) {
2085 mwq->mvi = mvi;
2086 mwq->data = data;
2087 mwq->handler = handler;
2088 MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
2089 list_add_tail(&mwq->entry, &mvi->wq_list);
2090 schedule_delayed_work(&mwq->work_q, HZ * 2);
2091 } else
2092 ret = -ENOMEM;
2093
2094 return ret;
2095}
2096
2097static void mvs_sig_time_out(unsigned long tphy)
2098{
2099 struct mvs_phy *phy = (struct mvs_phy *)tphy;
2100 struct mvs_info *mvi = phy->mvi;
2101 u8 phy_no;
2102
2103 for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
2104 if (&mvi->phy[phy_no] == phy) {
2105 mv_dprintk("Get signature time out, reset phy %d\n",
2106 phy_no+mvi->id*mvi->chip->n_phy);
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002107 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_HARD_RESET);
Andy Yan20b09c22009-05-08 17:46:40 -04002108 }
2109 }
2110}
2111
Andy Yan20b09c22009-05-08 17:46:40 -04002112void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
2113{
2114 u32 tmp;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002115 struct mvs_phy *phy = &mvi->phy[phy_no];
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002116
Andy Yan20b09c22009-05-08 17:46:40 -04002117 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002118 MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
2119 mv_dprintk("phy %d ctrl sts=0x%08X.\n", phy_no+mvi->id*mvi->chip->n_phy,
Andy Yan20b09c22009-05-08 17:46:40 -04002120 MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002121 mv_dprintk("phy %d irq sts = 0x%08X\n", phy_no+mvi->id*mvi->chip->n_phy,
Andy Yan20b09c22009-05-08 17:46:40 -04002122 phy->irq_status);
2123
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002124 /*
2125 * events is port event now ,
2126 * we need check the interrupt status which belongs to per port.
2127 */
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002128
Srinivas9dc9fd92010-02-15 00:00:00 -06002129 if (phy->irq_status & PHYEV_DCDR_ERR) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002130 mv_dprintk("phy %d STP decoding error.\n",
Srinivas9dc9fd92010-02-15 00:00:00 -06002131 phy_no + mvi->id*mvi->chip->n_phy);
2132 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002133
Andy Yan20b09c22009-05-08 17:46:40 -04002134 if (phy->irq_status & PHYEV_POOF) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002135 mdelay(500);
Andy Yan20b09c22009-05-08 17:46:40 -04002136 if (!(phy->phy_event & PHY_PLUG_OUT)) {
2137 int dev_sata = phy->phy_type & PORT_TYPE_SATA;
2138 int ready;
Srinivas9dc9fd92010-02-15 00:00:00 -06002139 mvs_do_release_task(mvi, phy_no, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04002140 phy->phy_event |= PHY_PLUG_OUT;
Srinivas9dc9fd92010-02-15 00:00:00 -06002141 MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
Andy Yan20b09c22009-05-08 17:46:40 -04002142 mvs_handle_event(mvi,
2143 (void *)(unsigned long)phy_no,
2144 PHY_PLUG_EVENT);
2145 ready = mvs_is_phy_ready(mvi, phy_no);
Andy Yan20b09c22009-05-08 17:46:40 -04002146 if (ready || dev_sata) {
2147 if (MVS_CHIP_DISP->stp_reset)
2148 MVS_CHIP_DISP->stp_reset(mvi,
2149 phy_no);
2150 else
2151 MVS_CHIP_DISP->phy_reset(mvi,
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002152 phy_no, MVS_SOFT_RESET);
Andy Yan20b09c22009-05-08 17:46:40 -04002153 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002154 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002155 }
2156 }
Andy Yan20b09c22009-05-08 17:46:40 -04002157
2158 if (phy->irq_status & PHYEV_COMWAKE) {
2159 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2160 MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2161 tmp | PHYEV_SIG_FIS);
2162 if (phy->timer.function == NULL) {
2163 phy->timer.data = (unsigned long)phy;
2164 phy->timer.function = mvs_sig_time_out;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002165 phy->timer.expires = jiffies + 5*HZ;
Andy Yan20b09c22009-05-08 17:46:40 -04002166 add_timer(&phy->timer);
2167 }
2168 }
2169 if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2170 phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
Andy Yan20b09c22009-05-08 17:46:40 -04002171 mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2172 if (phy->phy_status) {
2173 mdelay(10);
2174 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2175 if (phy->phy_type & PORT_TYPE_SATA) {
2176 tmp = MVS_CHIP_DISP->read_port_irq_mask(
2177 mvi, phy_no);
2178 tmp &= ~PHYEV_SIG_FIS;
2179 MVS_CHIP_DISP->write_port_irq_mask(mvi,
2180 phy_no, tmp);
2181 }
2182 mvs_update_phyinfo(mvi, phy_no, 0);
Srinivas9dc9fd92010-02-15 00:00:00 -06002183 if (phy->phy_type & PORT_TYPE_SAS) {
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002184 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_PHY_TUNE);
Srinivas9dc9fd92010-02-15 00:00:00 -06002185 mdelay(10);
2186 }
2187
Andy Yan20b09c22009-05-08 17:46:40 -04002188 mvs_bytes_dmaed(mvi, phy_no);
2189 /* whether driver is going to handle hot plug */
2190 if (phy->phy_event & PHY_PLUG_OUT) {
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002191 mvs_port_notify_formed(&phy->sas_phy, 0);
Andy Yan20b09c22009-05-08 17:46:40 -04002192 phy->phy_event &= ~PHY_PLUG_OUT;
2193 }
2194 } else {
2195 mv_dprintk("plugin interrupt but phy%d is gone\n",
2196 phy_no + mvi->id*mvi->chip->n_phy);
2197 }
2198 } else if (phy->irq_status & PHYEV_BROAD_CH) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002199 mv_dprintk("phy %d broadcast change.\n",
Andy Yan20b09c22009-05-08 17:46:40 -04002200 phy_no + mvi->id*mvi->chip->n_phy);
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002201 mvs_handle_event(mvi, (void *)(unsigned long)phy_no,
2202 EXP_BRCT_CHG);
Andy Yan20b09c22009-05-08 17:46:40 -04002203 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002204}
2205
Andy Yan20b09c22009-05-08 17:46:40 -04002206int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002207{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002208 u32 rx_prod_idx, rx_desc;
2209 bool attn = false;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002210
2211 /* the first dword in the RX ring is special: it contains
2212 * a mirror of the hardware's RX producer index, so that
2213 * we don't have to stall the CPU reading that register.
2214 * The actual RX ring is offset by one dword, due to this.
2215 */
2216 rx_prod_idx = mvi->rx_cons;
2217 mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2218 if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
2219 return 0;
2220
2221 /* The CMPL_Q may come late, read from register and try again
2222 * note: if coalescing is enabled,
2223 * it will need to read from register every time for sure
2224 */
Andy Yan20b09c22009-05-08 17:46:40 -04002225 if (unlikely(mvi->rx_cons == rx_prod_idx))
2226 mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002227
2228 if (mvi->rx_cons == rx_prod_idx)
2229 return 0;
2230
2231 while (mvi->rx_cons != rx_prod_idx) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002232 /* increment our internal RX consumer pointer */
2233 rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002234 rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2235
2236 if (likely(rx_desc & RXQ_DONE))
2237 mvs_slot_complete(mvi, rx_desc, 0);
2238 if (rx_desc & RXQ_ATTN) {
2239 attn = true;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002240 } else if (rx_desc & RXQ_ERR) {
2241 if (!(rx_desc & RXQ_DONE))
2242 mvs_slot_complete(mvi, rx_desc, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002243 } else if (rx_desc & RXQ_SLOT_RESET) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002244 mvs_slot_free(mvi, rx_desc);
2245 }
2246 }
2247
2248 if (attn && self_clear)
Andy Yan20b09c22009-05-08 17:46:40 -04002249 MVS_CHIP_DISP->int_full(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002250 return 0;
2251}
2252