xref: /openbmc/linux/drivers/scsi/mvsas/mv_sas.c (revision 12eb4683)
1 /*
2  * Marvell 88SE64xx/88SE94xx main function
3  *
4  * Copyright 2007 Red Hat, Inc.
5  * Copyright 2008 Marvell. <kewei@marvell.com>
6  * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
7  *
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 */
25 
26 #include "mv_sas.h"
27 
28 static 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;
32 		slot = task->lldd_task;
33 		*tag = slot->slot_tag;
34 		return 1;
35 	}
36 	return 0;
37 }
38 
39 void mvs_tag_clear(struct mvs_info *mvi, u32 tag)
40 {
41 	void *bitmap = mvi->tags;
42 	clear_bit(tag, bitmap);
43 }
44 
45 void mvs_tag_free(struct mvs_info *mvi, u32 tag)
46 {
47 	mvs_tag_clear(mvi, tag);
48 }
49 
50 void mvs_tag_set(struct mvs_info *mvi, unsigned int tag)
51 {
52 	void *bitmap = mvi->tags;
53 	set_bit(tag, bitmap);
54 }
55 
56 inline int mvs_tag_alloc(struct mvs_info *mvi, u32 *tag_out)
57 {
58 	unsigned int index, tag;
59 	void *bitmap = mvi->tags;
60 
61 	index = find_first_zero_bit(bitmap, mvi->tags_num);
62 	tag = index;
63 	if (tag >= mvi->tags_num)
64 		return -SAS_QUEUE_FULL;
65 	mvs_tag_set(mvi, tag);
66 	*tag_out = tag;
67 	return 0;
68 }
69 
70 void mvs_tag_init(struct mvs_info *mvi)
71 {
72 	int i;
73 	for (i = 0; i < mvi->tags_num; ++i)
74 		mvs_tag_clear(mvi, i);
75 }
76 
77 struct 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;
83 
84 	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++;
93 			}
94 			break;
95 		}
96 		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 int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
106 {
107 	unsigned long i = 0, j = 0, n = 0, num = 0;
108 	struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
109 	struct mvs_info *mvi = mvi_dev->mvi_info;
110 	struct sas_ha_struct *sha = dev->port->ha;
111 
112 	while (sha->sas_port[i]) {
113 		if (sha->sas_port[i] == dev->port) {
114 			struct asd_sas_phy *phy;
115 			list_for_each_entry(phy,
116 				&sha->sas_port[i]->phy_list, port_phy_el) {
117 				j = 0;
118 				while (sha->sas_phy[j]) {
119 					if (sha->sas_phy[j] == phy)
120 						break;
121 					j++;
122 				}
123 				phyno[n] = (j >= mvi->chip->n_phy) ?
124 					(j - mvi->chip->n_phy) : j;
125 				num++;
126 				n++;
127 			}
128 			break;
129 		}
130 		i++;
131 	}
132 	return num;
133 }
134 
135 struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
136 						u8 reg_set)
137 {
138 	u32 dev_no;
139 	for (dev_no = 0; dev_no < MVS_MAX_DEVICES; dev_no++) {
140 		if (mvi->devices[dev_no].taskfileset == MVS_ID_NOT_MAPPED)
141 			continue;
142 
143 		if (mvi->devices[dev_no].taskfileset == reg_set)
144 			return &mvi->devices[dev_no];
145 	}
146 	return NULL;
147 }
148 
149 static inline void mvs_free_reg_set(struct mvs_info *mvi,
150 				struct mvs_device *dev)
151 {
152 	if (!dev) {
153 		mv_printk("device has been free.\n");
154 		return;
155 	}
156 	if (dev->taskfileset == MVS_ID_NOT_MAPPED)
157 		return;
158 	MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
159 }
160 
161 static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
162 				struct mvs_device *dev)
163 {
164 	if (dev->taskfileset != MVS_ID_NOT_MAPPED)
165 		return 0;
166 	return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
167 }
168 
169 void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
170 {
171 	u32 no;
172 	for_each_phy(phy_mask, phy_mask, no) {
173 		if (!(phy_mask & 1))
174 			continue;
175 		MVS_CHIP_DISP->phy_reset(mvi, no, hard);
176 	}
177 }
178 
179 int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
180 			void *funcdata)
181 {
182 	int rc = 0, phy_id = sas_phy->id;
183 	u32 tmp, i = 0, hi;
184 	struct sas_ha_struct *sha = sas_phy->ha;
185 	struct mvs_info *mvi = NULL;
186 
187 	while (sha->sas_phy[i]) {
188 		if (sha->sas_phy[i] == sas_phy)
189 			break;
190 		i++;
191 	}
192 	hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
193 	mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
194 
195 	switch (func) {
196 	case PHY_FUNC_SET_LINK_RATE:
197 		MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
198 		break;
199 
200 	case PHY_FUNC_HARD_RESET:
201 		tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
202 		if (tmp & PHY_RST_HARD)
203 			break;
204 		MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_HARD_RESET);
205 		break;
206 
207 	case PHY_FUNC_LINK_RESET:
208 		MVS_CHIP_DISP->phy_enable(mvi, phy_id);
209 		MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_SOFT_RESET);
210 		break;
211 
212 	case PHY_FUNC_DISABLE:
213 		MVS_CHIP_DISP->phy_disable(mvi, phy_id);
214 		break;
215 	case PHY_FUNC_RELEASE_SPINUP_HOLD:
216 	default:
217 		rc = -ENOSYS;
218 	}
219 	msleep(200);
220 	return rc;
221 }
222 
223 void mvs_set_sas_addr(struct mvs_info *mvi, int port_id, u32 off_lo,
224 		      u32 off_hi, u64 sas_addr)
225 {
226 	u32 lo = (u32)sas_addr;
227 	u32 hi = (u32)(sas_addr>>32);
228 
229 	MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_lo);
230 	MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, lo);
231 	MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_hi);
232 	MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, hi);
233 }
234 
235 static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
236 {
237 	struct mvs_phy *phy = &mvi->phy[i];
238 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
239 	struct sas_ha_struct *sas_ha;
240 	if (!phy->phy_attached)
241 		return;
242 
243 	if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
244 		&& phy->phy_type & PORT_TYPE_SAS) {
245 		return;
246 	}
247 
248 	sas_ha = mvi->sas;
249 	sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
250 
251 	if (sas_phy->phy) {
252 		struct sas_phy *sphy = sas_phy->phy;
253 
254 		sphy->negotiated_linkrate = sas_phy->linkrate;
255 		sphy->minimum_linkrate = phy->minimum_linkrate;
256 		sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
257 		sphy->maximum_linkrate = phy->maximum_linkrate;
258 		sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
259 	}
260 
261 	if (phy->phy_type & PORT_TYPE_SAS) {
262 		struct sas_identify_frame *id;
263 
264 		id = (struct sas_identify_frame *)phy->frame_rcvd;
265 		id->dev_type = phy->identify.device_type;
266 		id->initiator_bits = SAS_PROTOCOL_ALL;
267 		id->target_bits = phy->identify.target_port_protocols;
268 
269 		/* direct attached SAS device */
270 		if (phy->att_dev_info & PORT_SSP_TRGT_MASK) {
271 			MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_PHY_STAT);
272 			MVS_CHIP_DISP->write_port_cfg_data(mvi, i, 0x00);
273 		}
274 	} else if (phy->phy_type & PORT_TYPE_SATA) {
275 		/*Nothing*/
276 	}
277 	mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
278 
279 	sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
280 
281 	mvi->sas->notify_port_event(sas_phy,
282 				   PORTE_BYTES_DMAED);
283 }
284 
285 void mvs_scan_start(struct Scsi_Host *shost)
286 {
287 	int i, j;
288 	unsigned short core_nr;
289 	struct mvs_info *mvi;
290 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
291 	struct mvs_prv_info *mvs_prv = sha->lldd_ha;
292 
293 	core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
294 
295 	for (j = 0; j < core_nr; j++) {
296 		mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
297 		for (i = 0; i < mvi->chip->n_phy; ++i)
298 			mvs_bytes_dmaed(mvi, i);
299 	}
300 	mvs_prv->scan_finished = 1;
301 }
302 
303 int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
304 {
305 	struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
306 	struct mvs_prv_info *mvs_prv = sha->lldd_ha;
307 
308 	if (mvs_prv->scan_finished == 0)
309 		return 0;
310 
311 	sas_drain_work(sha);
312 	return 1;
313 }
314 
315 static int mvs_task_prep_smp(struct mvs_info *mvi,
316 			     struct mvs_task_exec_info *tei)
317 {
318 	int elem, rc, i;
319 	struct sas_ha_struct *sha = mvi->sas;
320 	struct sas_task *task = tei->task;
321 	struct mvs_cmd_hdr *hdr = tei->hdr;
322 	struct domain_device *dev = task->dev;
323 	struct asd_sas_port *sas_port = dev->port;
324 	struct sas_phy *sphy = dev->phy;
325 	struct asd_sas_phy *sas_phy = sha->sas_phy[sphy->number];
326 	struct scatterlist *sg_req, *sg_resp;
327 	u32 req_len, resp_len, tag = tei->tag;
328 	void *buf_tmp;
329 	u8 *buf_oaf;
330 	dma_addr_t buf_tmp_dma;
331 	void *buf_prd;
332 	struct mvs_slot_info *slot = &mvi->slot_info[tag];
333 	u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
334 
335 	/*
336 	 * DMA-map SMP request, response buffers
337 	 */
338 	sg_req = &task->smp_task.smp_req;
339 	elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
340 	if (!elem)
341 		return -ENOMEM;
342 	req_len = sg_dma_len(sg_req);
343 
344 	sg_resp = &task->smp_task.smp_resp;
345 	elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
346 	if (!elem) {
347 		rc = -ENOMEM;
348 		goto err_out;
349 	}
350 	resp_len = SB_RFB_MAX;
351 
352 	/* must be in dwords */
353 	if ((req_len & 0x3) || (resp_len & 0x3)) {
354 		rc = -EINVAL;
355 		goto err_out_2;
356 	}
357 
358 	/*
359 	 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
360 	 */
361 
362 	/* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
363 	buf_tmp = slot->buf;
364 	buf_tmp_dma = slot->buf_dma;
365 
366 	hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
367 
368 	/* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
369 	buf_oaf = buf_tmp;
370 	hdr->open_frame = cpu_to_le64(buf_tmp_dma);
371 
372 	buf_tmp += MVS_OAF_SZ;
373 	buf_tmp_dma += MVS_OAF_SZ;
374 
375 	/* region 3: PRD table *********************************** */
376 	buf_prd = buf_tmp;
377 	if (tei->n_elem)
378 		hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
379 	else
380 		hdr->prd_tbl = 0;
381 
382 	i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
383 	buf_tmp += i;
384 	buf_tmp_dma += i;
385 
386 	/* region 4: status buffer (larger the PRD, smaller this buf) ****** */
387 	slot->response = buf_tmp;
388 	hdr->status_buf = cpu_to_le64(buf_tmp_dma);
389 	if (mvi->flags & MVF_FLAG_SOC)
390 		hdr->reserved[0] = 0;
391 
392 	/*
393 	 * Fill in TX ring and command slot header
394 	 */
395 	slot->tx = mvi->tx_prod;
396 	mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
397 					TXQ_MODE_I | tag |
398 					(MVS_PHY_ID << TXQ_PHY_SHIFT));
399 
400 	hdr->flags |= flags;
401 	hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
402 	hdr->tags = cpu_to_le32(tag);
403 	hdr->data_len = 0;
404 
405 	/* generate open address frame hdr (first 12 bytes) */
406 	/* initiator, SMP, ftype 1h */
407 	buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
408 	buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
409 	*(u16 *)(buf_oaf + 2) = 0xFFFF;		/* SAS SPEC */
410 	memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
411 
412 	/* fill in PRD (scatter/gather) table, if any */
413 	MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
414 
415 	return 0;
416 
417 err_out_2:
418 	dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
419 		     PCI_DMA_FROMDEVICE);
420 err_out:
421 	dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
422 		     PCI_DMA_TODEVICE);
423 	return rc;
424 }
425 
426 static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
427 {
428 	struct ata_queued_cmd *qc = task->uldd_task;
429 
430 	if (qc) {
431 		if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
432 			qc->tf.command == ATA_CMD_FPDMA_READ) {
433 			*tag = qc->tag;
434 			return 1;
435 		}
436 	}
437 
438 	return 0;
439 }
440 
441 static int mvs_task_prep_ata(struct mvs_info *mvi,
442 			     struct mvs_task_exec_info *tei)
443 {
444 	struct sas_ha_struct *sha = mvi->sas;
445 	struct sas_task *task = tei->task;
446 	struct domain_device *dev = task->dev;
447 	struct mvs_device *mvi_dev = dev->lldd_dev;
448 	struct mvs_cmd_hdr *hdr = tei->hdr;
449 	struct asd_sas_port *sas_port = dev->port;
450 	struct sas_phy *sphy = dev->phy;
451 	struct asd_sas_phy *sas_phy = sha->sas_phy[sphy->number];
452 	struct mvs_slot_info *slot;
453 	void *buf_prd;
454 	u32 tag = tei->tag, hdr_tag;
455 	u32 flags, del_q;
456 	void *buf_tmp;
457 	u8 *buf_cmd, *buf_oaf;
458 	dma_addr_t buf_tmp_dma;
459 	u32 i, req_len, resp_len;
460 	const u32 max_resp_len = SB_RFB_MAX;
461 
462 	if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
463 		mv_dprintk("Have not enough regiset for dev %d.\n",
464 			mvi_dev->device_id);
465 		return -EBUSY;
466 	}
467 	slot = &mvi->slot_info[tag];
468 	slot->tx = mvi->tx_prod;
469 	del_q = TXQ_MODE_I | tag |
470 		(TXQ_CMD_STP << TXQ_CMD_SHIFT) |
471 		(MVS_PHY_ID << TXQ_PHY_SHIFT) |
472 		(mvi_dev->taskfileset << TXQ_SRS_SHIFT);
473 	mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
474 
475 	if (task->data_dir == DMA_FROM_DEVICE)
476 		flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
477 	else
478 		flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
479 
480 	if (task->ata_task.use_ncq)
481 		flags |= MCH_FPDMA;
482 	if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
483 		if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
484 			flags |= MCH_ATAPI;
485 	}
486 
487 	hdr->flags = cpu_to_le32(flags);
488 
489 	if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
490 		task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
491 	else
492 		hdr_tag = tag;
493 
494 	hdr->tags = cpu_to_le32(hdr_tag);
495 
496 	hdr->data_len = cpu_to_le32(task->total_xfer_len);
497 
498 	/*
499 	 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
500 	 */
501 
502 	/* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
503 	buf_cmd = buf_tmp = slot->buf;
504 	buf_tmp_dma = slot->buf_dma;
505 
506 	hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
507 
508 	buf_tmp += MVS_ATA_CMD_SZ;
509 	buf_tmp_dma += MVS_ATA_CMD_SZ;
510 
511 	/* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
512 	/* used for STP.  unused for SATA? */
513 	buf_oaf = buf_tmp;
514 	hdr->open_frame = cpu_to_le64(buf_tmp_dma);
515 
516 	buf_tmp += MVS_OAF_SZ;
517 	buf_tmp_dma += MVS_OAF_SZ;
518 
519 	/* region 3: PRD table ********************************************* */
520 	buf_prd = buf_tmp;
521 
522 	if (tei->n_elem)
523 		hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
524 	else
525 		hdr->prd_tbl = 0;
526 	i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
527 
528 	buf_tmp += i;
529 	buf_tmp_dma += i;
530 
531 	/* region 4: status buffer (larger the PRD, smaller this buf) ****** */
532 	slot->response = buf_tmp;
533 	hdr->status_buf = cpu_to_le64(buf_tmp_dma);
534 	if (mvi->flags & MVF_FLAG_SOC)
535 		hdr->reserved[0] = 0;
536 
537 	req_len = sizeof(struct host_to_dev_fis);
538 	resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
539 	    sizeof(struct mvs_err_info) - i;
540 
541 	/* request, response lengths */
542 	resp_len = min(resp_len, max_resp_len);
543 	hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
544 
545 	if (likely(!task->ata_task.device_control_reg_update))
546 		task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
547 	/* fill in command FIS and ATAPI CDB */
548 	memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
549 	if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
550 		memcpy(buf_cmd + STP_ATAPI_CMD,
551 			task->ata_task.atapi_packet, 16);
552 
553 	/* generate open address frame hdr (first 12 bytes) */
554 	/* initiator, STP, ftype 1h */
555 	buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
556 	buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
557 	*(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
558 	memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
559 
560 	/* fill in PRD (scatter/gather) table, if any */
561 	MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
562 
563 	if (task->data_dir == DMA_FROM_DEVICE)
564 		MVS_CHIP_DISP->dma_fix(mvi, sas_port->phy_mask,
565 				TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
566 
567 	return 0;
568 }
569 
570 static int mvs_task_prep_ssp(struct mvs_info *mvi,
571 			     struct mvs_task_exec_info *tei, int is_tmf,
572 			     struct mvs_tmf_task *tmf)
573 {
574 	struct sas_task *task = tei->task;
575 	struct mvs_cmd_hdr *hdr = tei->hdr;
576 	struct mvs_port *port = tei->port;
577 	struct domain_device *dev = task->dev;
578 	struct mvs_device *mvi_dev = dev->lldd_dev;
579 	struct asd_sas_port *sas_port = dev->port;
580 	struct mvs_slot_info *slot;
581 	void *buf_prd;
582 	struct ssp_frame_hdr *ssp_hdr;
583 	void *buf_tmp;
584 	u8 *buf_cmd, *buf_oaf, fburst = 0;
585 	dma_addr_t buf_tmp_dma;
586 	u32 flags;
587 	u32 resp_len, req_len, i, tag = tei->tag;
588 	const u32 max_resp_len = SB_RFB_MAX;
589 	u32 phy_mask;
590 
591 	slot = &mvi->slot_info[tag];
592 
593 	phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
594 		sas_port->phy_mask) & TXQ_PHY_MASK;
595 
596 	slot->tx = mvi->tx_prod;
597 	mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
598 				(TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
599 				(phy_mask << TXQ_PHY_SHIFT));
600 
601 	flags = MCH_RETRY;
602 	if (task->ssp_task.enable_first_burst) {
603 		flags |= MCH_FBURST;
604 		fburst = (1 << 7);
605 	}
606 	if (is_tmf)
607 		flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
608 	else
609 		flags |= (MCH_SSP_FR_CMD << MCH_SSP_FR_TYPE_SHIFT);
610 
611 	hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
612 	hdr->tags = cpu_to_le32(tag);
613 	hdr->data_len = cpu_to_le32(task->total_xfer_len);
614 
615 	/*
616 	 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
617 	 */
618 
619 	/* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
620 	buf_cmd = buf_tmp = slot->buf;
621 	buf_tmp_dma = slot->buf_dma;
622 
623 	hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
624 
625 	buf_tmp += MVS_SSP_CMD_SZ;
626 	buf_tmp_dma += MVS_SSP_CMD_SZ;
627 
628 	/* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
629 	buf_oaf = buf_tmp;
630 	hdr->open_frame = cpu_to_le64(buf_tmp_dma);
631 
632 	buf_tmp += MVS_OAF_SZ;
633 	buf_tmp_dma += MVS_OAF_SZ;
634 
635 	/* region 3: PRD table ********************************************* */
636 	buf_prd = buf_tmp;
637 	if (tei->n_elem)
638 		hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
639 	else
640 		hdr->prd_tbl = 0;
641 
642 	i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
643 	buf_tmp += i;
644 	buf_tmp_dma += i;
645 
646 	/* region 4: status buffer (larger the PRD, smaller this buf) ****** */
647 	slot->response = buf_tmp;
648 	hdr->status_buf = cpu_to_le64(buf_tmp_dma);
649 	if (mvi->flags & MVF_FLAG_SOC)
650 		hdr->reserved[0] = 0;
651 
652 	resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
653 	    sizeof(struct mvs_err_info) - i;
654 	resp_len = min(resp_len, max_resp_len);
655 
656 	req_len = sizeof(struct ssp_frame_hdr) + 28;
657 
658 	/* request, response lengths */
659 	hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
660 
661 	/* generate open address frame hdr (first 12 bytes) */
662 	/* initiator, SSP, ftype 1h */
663 	buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
664 	buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
665 	*(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
666 	memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
667 
668 	/* fill in SSP frame header (Command Table.SSP frame header) */
669 	ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
670 
671 	if (is_tmf)
672 		ssp_hdr->frame_type = SSP_TASK;
673 	else
674 		ssp_hdr->frame_type = SSP_COMMAND;
675 
676 	memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
677 	       HASHED_SAS_ADDR_SIZE);
678 	memcpy(ssp_hdr->hashed_src_addr,
679 	       dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
680 	ssp_hdr->tag = cpu_to_be16(tag);
681 
682 	/* fill in IU for TASK and Command Frame */
683 	buf_cmd += sizeof(*ssp_hdr);
684 	memcpy(buf_cmd, &task->ssp_task.LUN, 8);
685 
686 	if (ssp_hdr->frame_type != SSP_TASK) {
687 		buf_cmd[9] = fburst | task->ssp_task.task_attr |
688 				(task->ssp_task.task_prio << 3);
689 		memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
690 		       task->ssp_task.cmd->cmd_len);
691 	} else{
692 		buf_cmd[10] = tmf->tmf;
693 		switch (tmf->tmf) {
694 		case TMF_ABORT_TASK:
695 		case TMF_QUERY_TASK:
696 			buf_cmd[12] =
697 				(tmf->tag_of_task_to_be_managed >> 8) & 0xff;
698 			buf_cmd[13] =
699 				tmf->tag_of_task_to_be_managed & 0xff;
700 			break;
701 		default:
702 			break;
703 		}
704 	}
705 	/* fill in PRD (scatter/gather) table, if any */
706 	MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
707 	return 0;
708 }
709 
710 #define	DEV_IS_GONE(mvi_dev)	((!mvi_dev || (mvi_dev->dev_type == SAS_PHY_UNUSED)))
711 static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
712 				struct mvs_tmf_task *tmf, int *pass)
713 {
714 	struct domain_device *dev = task->dev;
715 	struct mvs_device *mvi_dev = dev->lldd_dev;
716 	struct mvs_task_exec_info tei;
717 	struct mvs_slot_info *slot;
718 	u32 tag = 0xdeadbeef, n_elem = 0;
719 	int rc = 0;
720 
721 	if (!dev->port) {
722 		struct task_status_struct *tsm = &task->task_status;
723 
724 		tsm->resp = SAS_TASK_UNDELIVERED;
725 		tsm->stat = SAS_PHY_DOWN;
726 		/*
727 		 * libsas will use dev->port, should
728 		 * not call task_done for sata
729 		 */
730 		if (dev->dev_type != SAS_SATA_DEV)
731 			task->task_done(task);
732 		return rc;
733 	}
734 
735 	if (DEV_IS_GONE(mvi_dev)) {
736 		if (mvi_dev)
737 			mv_dprintk("device %d not ready.\n",
738 				mvi_dev->device_id);
739 		else
740 			mv_dprintk("device %016llx not ready.\n",
741 				SAS_ADDR(dev->sas_addr));
742 
743 			rc = SAS_PHY_DOWN;
744 			return rc;
745 	}
746 	tei.port = dev->port->lldd_port;
747 	if (tei.port && !tei.port->port_attached && !tmf) {
748 		if (sas_protocol_ata(task->task_proto)) {
749 			struct task_status_struct *ts = &task->task_status;
750 			mv_dprintk("SATA/STP port %d does not attach"
751 					"device.\n", dev->port->id);
752 			ts->resp = SAS_TASK_COMPLETE;
753 			ts->stat = SAS_PHY_DOWN;
754 
755 			task->task_done(task);
756 
757 		} else {
758 			struct task_status_struct *ts = &task->task_status;
759 			mv_dprintk("SAS port %d does not attach"
760 				"device.\n", dev->port->id);
761 			ts->resp = SAS_TASK_UNDELIVERED;
762 			ts->stat = SAS_PHY_DOWN;
763 			task->task_done(task);
764 		}
765 		return rc;
766 	}
767 
768 	if (!sas_protocol_ata(task->task_proto)) {
769 		if (task->num_scatter) {
770 			n_elem = dma_map_sg(mvi->dev,
771 					    task->scatter,
772 					    task->num_scatter,
773 					    task->data_dir);
774 			if (!n_elem) {
775 				rc = -ENOMEM;
776 				goto prep_out;
777 			}
778 		}
779 	} else {
780 		n_elem = task->num_scatter;
781 	}
782 
783 	rc = mvs_tag_alloc(mvi, &tag);
784 	if (rc)
785 		goto err_out;
786 
787 	slot = &mvi->slot_info[tag];
788 
789 	task->lldd_task = NULL;
790 	slot->n_elem = n_elem;
791 	slot->slot_tag = tag;
792 
793 	slot->buf = pci_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
794 	if (!slot->buf)
795 		goto err_out_tag;
796 	memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
797 
798 	tei.task = task;
799 	tei.hdr = &mvi->slot[tag];
800 	tei.tag = tag;
801 	tei.n_elem = n_elem;
802 	switch (task->task_proto) {
803 	case SAS_PROTOCOL_SMP:
804 		rc = mvs_task_prep_smp(mvi, &tei);
805 		break;
806 	case SAS_PROTOCOL_SSP:
807 		rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
808 		break;
809 	case SAS_PROTOCOL_SATA:
810 	case SAS_PROTOCOL_STP:
811 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
812 		rc = mvs_task_prep_ata(mvi, &tei);
813 		break;
814 	default:
815 		dev_printk(KERN_ERR, mvi->dev,
816 			"unknown sas_task proto: 0x%x\n",
817 			task->task_proto);
818 		rc = -EINVAL;
819 		break;
820 	}
821 
822 	if (rc) {
823 		mv_dprintk("rc is %x\n", rc);
824 		goto err_out_slot_buf;
825 	}
826 	slot->task = task;
827 	slot->port = tei.port;
828 	task->lldd_task = slot;
829 	list_add_tail(&slot->entry, &tei.port->list);
830 	spin_lock(&task->task_state_lock);
831 	task->task_state_flags |= SAS_TASK_AT_INITIATOR;
832 	spin_unlock(&task->task_state_lock);
833 
834 	mvi_dev->running_req++;
835 	++(*pass);
836 	mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
837 
838 	return rc;
839 
840 err_out_slot_buf:
841 	pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
842 err_out_tag:
843 	mvs_tag_free(mvi, tag);
844 err_out:
845 
846 	dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
847 	if (!sas_protocol_ata(task->task_proto))
848 		if (n_elem)
849 			dma_unmap_sg(mvi->dev, task->scatter, n_elem,
850 				     task->data_dir);
851 prep_out:
852 	return rc;
853 }
854 
855 static struct mvs_task_list *mvs_task_alloc_list(int *num, gfp_t gfp_flags)
856 {
857 	struct mvs_task_list *first = NULL;
858 
859 	for (; *num > 0; --*num) {
860 		struct mvs_task_list *mvs_list = kmem_cache_zalloc(mvs_task_list_cache, gfp_flags);
861 
862 		if (!mvs_list)
863 			break;
864 
865 		INIT_LIST_HEAD(&mvs_list->list);
866 		if (!first)
867 			first = mvs_list;
868 		else
869 			list_add_tail(&mvs_list->list, &first->list);
870 
871 	}
872 
873 	return first;
874 }
875 
876 static inline void mvs_task_free_list(struct mvs_task_list *mvs_list)
877 {
878 	LIST_HEAD(list);
879 	struct list_head *pos, *a;
880 	struct mvs_task_list *mlist = NULL;
881 
882 	__list_add(&list, mvs_list->list.prev, &mvs_list->list);
883 
884 	list_for_each_safe(pos, a, &list) {
885 		list_del_init(pos);
886 		mlist = list_entry(pos, struct mvs_task_list, list);
887 		kmem_cache_free(mvs_task_list_cache, mlist);
888 	}
889 }
890 
891 static int mvs_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
892 				struct completion *completion, int is_tmf,
893 				struct mvs_tmf_task *tmf)
894 {
895 	struct mvs_info *mvi = NULL;
896 	u32 rc = 0;
897 	u32 pass = 0;
898 	unsigned long flags = 0;
899 
900 	mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
901 
902 	spin_lock_irqsave(&mvi->lock, flags);
903 	rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
904 	if (rc)
905 		dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
906 
907 	if (likely(pass))
908 			MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
909 				(MVS_CHIP_SLOT_SZ - 1));
910 	spin_unlock_irqrestore(&mvi->lock, flags);
911 
912 	return rc;
913 }
914 
915 static int mvs_collector_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
916 				struct completion *completion, int is_tmf,
917 				struct mvs_tmf_task *tmf)
918 {
919 	struct domain_device *dev = task->dev;
920 	struct mvs_prv_info *mpi = dev->port->ha->lldd_ha;
921 	struct mvs_info *mvi = NULL;
922 	struct sas_task *t = task;
923 	struct mvs_task_list *mvs_list = NULL, *a;
924 	LIST_HEAD(q);
925 	int pass[2] = {0};
926 	u32 rc = 0;
927 	u32 n = num;
928 	unsigned long flags = 0;
929 
930 	mvs_list = mvs_task_alloc_list(&n, gfp_flags);
931 	if (n) {
932 		printk(KERN_ERR "%s: mvs alloc list failed.\n", __func__);
933 		rc = -ENOMEM;
934 		goto free_list;
935 	}
936 
937 	__list_add(&q, mvs_list->list.prev, &mvs_list->list);
938 
939 	list_for_each_entry(a, &q, list) {
940 		a->task = t;
941 		t = list_entry(t->list.next, struct sas_task, list);
942 	}
943 
944 	list_for_each_entry(a, &q , list) {
945 
946 		t = a->task;
947 		mvi = ((struct mvs_device *)t->dev->lldd_dev)->mvi_info;
948 
949 		spin_lock_irqsave(&mvi->lock, flags);
950 		rc = mvs_task_prep(t, mvi, is_tmf, tmf, &pass[mvi->id]);
951 		if (rc)
952 			dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
953 		spin_unlock_irqrestore(&mvi->lock, flags);
954 	}
955 
956 	if (likely(pass[0]))
957 			MVS_CHIP_DISP->start_delivery(mpi->mvi[0],
958 				(mpi->mvi[0]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
959 
960 	if (likely(pass[1]))
961 			MVS_CHIP_DISP->start_delivery(mpi->mvi[1],
962 				(mpi->mvi[1]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
963 
964 	list_del_init(&q);
965 
966 free_list:
967 	if (mvs_list)
968 		mvs_task_free_list(mvs_list);
969 
970 	return rc;
971 }
972 
973 int mvs_queue_command(struct sas_task *task, const int num,
974 			gfp_t gfp_flags)
975 {
976 	struct mvs_device *mvi_dev = task->dev->lldd_dev;
977 	struct sas_ha_struct *sas = mvi_dev->mvi_info->sas;
978 
979 	if (sas->lldd_max_execute_num < 2)
980 		return mvs_task_exec(task, num, gfp_flags, NULL, 0, NULL);
981 	else
982 		return mvs_collector_task_exec(task, num, gfp_flags, NULL, 0, NULL);
983 }
984 
985 static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
986 {
987 	u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
988 	mvs_tag_clear(mvi, slot_idx);
989 }
990 
991 static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
992 			  struct mvs_slot_info *slot, u32 slot_idx)
993 {
994 	if (!slot->task)
995 		return;
996 	if (!sas_protocol_ata(task->task_proto))
997 		if (slot->n_elem)
998 			dma_unmap_sg(mvi->dev, task->scatter,
999 				     slot->n_elem, task->data_dir);
1000 
1001 	switch (task->task_proto) {
1002 	case SAS_PROTOCOL_SMP:
1003 		dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
1004 			     PCI_DMA_FROMDEVICE);
1005 		dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
1006 			     PCI_DMA_TODEVICE);
1007 		break;
1008 
1009 	case SAS_PROTOCOL_SATA:
1010 	case SAS_PROTOCOL_STP:
1011 	case SAS_PROTOCOL_SSP:
1012 	default:
1013 		/* do nothing */
1014 		break;
1015 	}
1016 
1017 	if (slot->buf) {
1018 		pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
1019 		slot->buf = NULL;
1020 	}
1021 	list_del_init(&slot->entry);
1022 	task->lldd_task = NULL;
1023 	slot->task = NULL;
1024 	slot->port = NULL;
1025 	slot->slot_tag = 0xFFFFFFFF;
1026 	mvs_slot_free(mvi, slot_idx);
1027 }
1028 
1029 static void mvs_update_wideport(struct mvs_info *mvi, int phy_no)
1030 {
1031 	struct mvs_phy *phy = &mvi->phy[phy_no];
1032 	struct mvs_port *port = phy->port;
1033 	int j, no;
1034 
1035 	for_each_phy(port->wide_port_phymap, j, no) {
1036 		if (j & 1) {
1037 			MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1038 						PHYR_WIDE_PORT);
1039 			MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1040 						port->wide_port_phymap);
1041 		} else {
1042 			MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1043 						PHYR_WIDE_PORT);
1044 			MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1045 						0);
1046 		}
1047 	}
1048 }
1049 
1050 static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
1051 {
1052 	u32 tmp;
1053 	struct mvs_phy *phy = &mvi->phy[i];
1054 	struct mvs_port *port = phy->port;
1055 
1056 	tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
1057 	if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
1058 		if (!port)
1059 			phy->phy_attached = 1;
1060 		return tmp;
1061 	}
1062 
1063 	if (port) {
1064 		if (phy->phy_type & PORT_TYPE_SAS) {
1065 			port->wide_port_phymap &= ~(1U << i);
1066 			if (!port->wide_port_phymap)
1067 				port->port_attached = 0;
1068 			mvs_update_wideport(mvi, i);
1069 		} else if (phy->phy_type & PORT_TYPE_SATA)
1070 			port->port_attached = 0;
1071 		phy->port = NULL;
1072 		phy->phy_attached = 0;
1073 		phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1074 	}
1075 	return 0;
1076 }
1077 
1078 static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
1079 {
1080 	u32 *s = (u32 *) buf;
1081 
1082 	if (!s)
1083 		return NULL;
1084 
1085 	MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
1086 	s[3] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1087 
1088 	MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
1089 	s[2] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1090 
1091 	MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
1092 	s[1] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1093 
1094 	MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
1095 	s[0] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1096 
1097 	if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1098 		s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
1099 
1100 	return s;
1101 }
1102 
1103 static u32 mvs_is_sig_fis_received(u32 irq_status)
1104 {
1105 	return irq_status & PHYEV_SIG_FIS;
1106 }
1107 
1108 static void mvs_sig_remove_timer(struct mvs_phy *phy)
1109 {
1110 	if (phy->timer.function)
1111 		del_timer(&phy->timer);
1112 	phy->timer.function = NULL;
1113 }
1114 
1115 void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
1116 {
1117 	struct mvs_phy *phy = &mvi->phy[i];
1118 	struct sas_identify_frame *id;
1119 
1120 	id = (struct sas_identify_frame *)phy->frame_rcvd;
1121 
1122 	if (get_st) {
1123 		phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
1124 		phy->phy_status = mvs_is_phy_ready(mvi, i);
1125 	}
1126 
1127 	if (phy->phy_status) {
1128 		int oob_done = 0;
1129 		struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
1130 
1131 		oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
1132 
1133 		MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
1134 		if (phy->phy_type & PORT_TYPE_SATA) {
1135 			phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
1136 			if (mvs_is_sig_fis_received(phy->irq_status)) {
1137 				mvs_sig_remove_timer(phy);
1138 				phy->phy_attached = 1;
1139 				phy->att_dev_sas_addr =
1140 					i + mvi->id * mvi->chip->n_phy;
1141 				if (oob_done)
1142 					sas_phy->oob_mode = SATA_OOB_MODE;
1143 				phy->frame_rcvd_size =
1144 				    sizeof(struct dev_to_host_fis);
1145 				mvs_get_d2h_reg(mvi, i, id);
1146 			} else {
1147 				u32 tmp;
1148 				dev_printk(KERN_DEBUG, mvi->dev,
1149 					"Phy%d : No sig fis\n", i);
1150 				tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1151 				MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1152 						tmp | PHYEV_SIG_FIS);
1153 				phy->phy_attached = 0;
1154 				phy->phy_type &= ~PORT_TYPE_SATA;
1155 				goto out_done;
1156 			}
1157 		}	else if (phy->phy_type & PORT_TYPE_SAS
1158 			|| phy->att_dev_info & PORT_SSP_INIT_MASK) {
1159 			phy->phy_attached = 1;
1160 			phy->identify.device_type =
1161 				phy->att_dev_info & PORT_DEV_TYPE_MASK;
1162 
1163 			if (phy->identify.device_type == SAS_END_DEVICE)
1164 				phy->identify.target_port_protocols =
1165 							SAS_PROTOCOL_SSP;
1166 			else if (phy->identify.device_type != SAS_PHY_UNUSED)
1167 				phy->identify.target_port_protocols =
1168 							SAS_PROTOCOL_SMP;
1169 			if (oob_done)
1170 				sas_phy->oob_mode = SAS_OOB_MODE;
1171 			phy->frame_rcvd_size =
1172 			    sizeof(struct sas_identify_frame);
1173 		}
1174 		memcpy(sas_phy->attached_sas_addr,
1175 			&phy->att_dev_sas_addr, SAS_ADDR_SIZE);
1176 
1177 		if (MVS_CHIP_DISP->phy_work_around)
1178 			MVS_CHIP_DISP->phy_work_around(mvi, i);
1179 	}
1180 	mv_dprintk("phy %d attach dev info is %x\n",
1181 		i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
1182 	mv_dprintk("phy %d attach sas addr is %llx\n",
1183 		i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
1184 out_done:
1185 	if (get_st)
1186 		MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
1187 }
1188 
1189 static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
1190 {
1191 	struct sas_ha_struct *sas_ha = sas_phy->ha;
1192 	struct mvs_info *mvi = NULL; int i = 0, hi;
1193 	struct mvs_phy *phy = sas_phy->lldd_phy;
1194 	struct asd_sas_port *sas_port = sas_phy->port;
1195 	struct mvs_port *port;
1196 	unsigned long flags = 0;
1197 	if (!sas_port)
1198 		return;
1199 
1200 	while (sas_ha->sas_phy[i]) {
1201 		if (sas_ha->sas_phy[i] == sas_phy)
1202 			break;
1203 		i++;
1204 	}
1205 	hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
1206 	mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
1207 	if (i >= mvi->chip->n_phy)
1208 		port = &mvi->port[i - mvi->chip->n_phy];
1209 	else
1210 		port = &mvi->port[i];
1211 	if (lock)
1212 		spin_lock_irqsave(&mvi->lock, flags);
1213 	port->port_attached = 1;
1214 	phy->port = port;
1215 	sas_port->lldd_port = port;
1216 	if (phy->phy_type & PORT_TYPE_SAS) {
1217 		port->wide_port_phymap = sas_port->phy_mask;
1218 		mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1219 		mvs_update_wideport(mvi, sas_phy->id);
1220 
1221 		/* direct attached SAS device */
1222 		if (phy->att_dev_info & PORT_SSP_TRGT_MASK) {
1223 			MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_PHY_STAT);
1224 			MVS_CHIP_DISP->write_port_cfg_data(mvi, i, 0x04);
1225 		}
1226 	}
1227 	if (lock)
1228 		spin_unlock_irqrestore(&mvi->lock, flags);
1229 }
1230 
1231 static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1232 {
1233 	struct domain_device *dev;
1234 	struct mvs_phy *phy = sas_phy->lldd_phy;
1235 	struct mvs_info *mvi = phy->mvi;
1236 	struct asd_sas_port *port = sas_phy->port;
1237 	int phy_no = 0;
1238 
1239 	while (phy != &mvi->phy[phy_no]) {
1240 		phy_no++;
1241 		if (phy_no >= MVS_MAX_PHYS)
1242 			return;
1243 	}
1244 	list_for_each_entry(dev, &port->dev_list, dev_list_node)
1245 		mvs_do_release_task(phy->mvi, phy_no, dev);
1246 
1247 }
1248 
1249 
1250 void mvs_port_formed(struct asd_sas_phy *sas_phy)
1251 {
1252 	mvs_port_notify_formed(sas_phy, 1);
1253 }
1254 
1255 void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1256 {
1257 	mvs_port_notify_deformed(sas_phy, 1);
1258 }
1259 
1260 struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
1261 {
1262 	u32 dev;
1263 	for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
1264 		if (mvi->devices[dev].dev_type == SAS_PHY_UNUSED) {
1265 			mvi->devices[dev].device_id = dev;
1266 			return &mvi->devices[dev];
1267 		}
1268 	}
1269 
1270 	if (dev == MVS_MAX_DEVICES)
1271 		mv_printk("max support %d devices, ignore ..\n",
1272 			MVS_MAX_DEVICES);
1273 
1274 	return NULL;
1275 }
1276 
1277 void mvs_free_dev(struct mvs_device *mvi_dev)
1278 {
1279 	u32 id = mvi_dev->device_id;
1280 	memset(mvi_dev, 0, sizeof(*mvi_dev));
1281 	mvi_dev->device_id = id;
1282 	mvi_dev->dev_type = SAS_PHY_UNUSED;
1283 	mvi_dev->dev_status = MVS_DEV_NORMAL;
1284 	mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1285 }
1286 
1287 int mvs_dev_found_notify(struct domain_device *dev, int lock)
1288 {
1289 	unsigned long flags = 0;
1290 	int res = 0;
1291 	struct mvs_info *mvi = NULL;
1292 	struct domain_device *parent_dev = dev->parent;
1293 	struct mvs_device *mvi_device;
1294 
1295 	mvi = mvs_find_dev_mvi(dev);
1296 
1297 	if (lock)
1298 		spin_lock_irqsave(&mvi->lock, flags);
1299 
1300 	mvi_device = mvs_alloc_dev(mvi);
1301 	if (!mvi_device) {
1302 		res = -1;
1303 		goto found_out;
1304 	}
1305 	dev->lldd_dev = mvi_device;
1306 	mvi_device->dev_status = MVS_DEV_NORMAL;
1307 	mvi_device->dev_type = dev->dev_type;
1308 	mvi_device->mvi_info = mvi;
1309 	mvi_device->sas_device = dev;
1310 	if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1311 		int phy_id;
1312 		u8 phy_num = parent_dev->ex_dev.num_phys;
1313 		struct ex_phy *phy;
1314 		for (phy_id = 0; phy_id < phy_num; phy_id++) {
1315 			phy = &parent_dev->ex_dev.ex_phy[phy_id];
1316 			if (SAS_ADDR(phy->attached_sas_addr) ==
1317 				SAS_ADDR(dev->sas_addr)) {
1318 				mvi_device->attached_phy = phy_id;
1319 				break;
1320 			}
1321 		}
1322 
1323 		if (phy_id == phy_num) {
1324 			mv_printk("Error: no attached dev:%016llx"
1325 				"at ex:%016llx.\n",
1326 				SAS_ADDR(dev->sas_addr),
1327 				SAS_ADDR(parent_dev->sas_addr));
1328 			res = -1;
1329 		}
1330 	}
1331 
1332 found_out:
1333 	if (lock)
1334 		spin_unlock_irqrestore(&mvi->lock, flags);
1335 	return res;
1336 }
1337 
1338 int mvs_dev_found(struct domain_device *dev)
1339 {
1340 	return mvs_dev_found_notify(dev, 1);
1341 }
1342 
1343 void mvs_dev_gone_notify(struct domain_device *dev)
1344 {
1345 	unsigned long flags = 0;
1346 	struct mvs_device *mvi_dev = dev->lldd_dev;
1347 	struct mvs_info *mvi = mvi_dev->mvi_info;
1348 
1349 	spin_lock_irqsave(&mvi->lock, flags);
1350 
1351 	if (mvi_dev) {
1352 		mv_dprintk("found dev[%d:%x] is gone.\n",
1353 			mvi_dev->device_id, mvi_dev->dev_type);
1354 		mvs_release_task(mvi, dev);
1355 		mvs_free_reg_set(mvi, mvi_dev);
1356 		mvs_free_dev(mvi_dev);
1357 	} else {
1358 		mv_dprintk("found dev has gone.\n");
1359 	}
1360 	dev->lldd_dev = NULL;
1361 	mvi_dev->sas_device = NULL;
1362 
1363 	spin_unlock_irqrestore(&mvi->lock, flags);
1364 }
1365 
1366 
1367 void mvs_dev_gone(struct domain_device *dev)
1368 {
1369 	mvs_dev_gone_notify(dev);
1370 }
1371 
1372 static void mvs_task_done(struct sas_task *task)
1373 {
1374 	if (!del_timer(&task->slow_task->timer))
1375 		return;
1376 	complete(&task->slow_task->completion);
1377 }
1378 
1379 static void mvs_tmf_timedout(unsigned long data)
1380 {
1381 	struct sas_task *task = (struct sas_task *)data;
1382 
1383 	task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1384 	complete(&task->slow_task->completion);
1385 }
1386 
1387 #define MVS_TASK_TIMEOUT 20
1388 static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1389 			void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1390 {
1391 	int res, retry;
1392 	struct sas_task *task = NULL;
1393 
1394 	for (retry = 0; retry < 3; retry++) {
1395 		task = sas_alloc_slow_task(GFP_KERNEL);
1396 		if (!task)
1397 			return -ENOMEM;
1398 
1399 		task->dev = dev;
1400 		task->task_proto = dev->tproto;
1401 
1402 		memcpy(&task->ssp_task, parameter, para_len);
1403 		task->task_done = mvs_task_done;
1404 
1405 		task->slow_task->timer.data = (unsigned long) task;
1406 		task->slow_task->timer.function = mvs_tmf_timedout;
1407 		task->slow_task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1408 		add_timer(&task->slow_task->timer);
1409 
1410 		res = mvs_task_exec(task, 1, GFP_KERNEL, NULL, 1, tmf);
1411 
1412 		if (res) {
1413 			del_timer(&task->slow_task->timer);
1414 			mv_printk("executing internal task failed:%d\n", res);
1415 			goto ex_err;
1416 		}
1417 
1418 		wait_for_completion(&task->slow_task->completion);
1419 		res = TMF_RESP_FUNC_FAILED;
1420 		/* Even TMF timed out, return direct. */
1421 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1422 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1423 				mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1424 				goto ex_err;
1425 			}
1426 		}
1427 
1428 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1429 		    task->task_status.stat == SAM_STAT_GOOD) {
1430 			res = TMF_RESP_FUNC_COMPLETE;
1431 			break;
1432 		}
1433 
1434 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1435 		      task->task_status.stat == SAS_DATA_UNDERRUN) {
1436 			/* no error, but return the number of bytes of
1437 			 * underrun */
1438 			res = task->task_status.residual;
1439 			break;
1440 		}
1441 
1442 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1443 		      task->task_status.stat == SAS_DATA_OVERRUN) {
1444 			mv_dprintk("blocked task error.\n");
1445 			res = -EMSGSIZE;
1446 			break;
1447 		} else {
1448 			mv_dprintk(" task to dev %016llx response: 0x%x "
1449 				    "status 0x%x\n",
1450 				    SAS_ADDR(dev->sas_addr),
1451 				    task->task_status.resp,
1452 				    task->task_status.stat);
1453 			sas_free_task(task);
1454 			task = NULL;
1455 
1456 		}
1457 	}
1458 ex_err:
1459 	BUG_ON(retry == 3 && task != NULL);
1460 	sas_free_task(task);
1461 	return res;
1462 }
1463 
1464 static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1465 				u8 *lun, struct mvs_tmf_task *tmf)
1466 {
1467 	struct sas_ssp_task ssp_task;
1468 	if (!(dev->tproto & SAS_PROTOCOL_SSP))
1469 		return TMF_RESP_FUNC_ESUPP;
1470 
1471 	memcpy(ssp_task.LUN, lun, 8);
1472 
1473 	return mvs_exec_internal_tmf_task(dev, &ssp_task,
1474 				sizeof(ssp_task), tmf);
1475 }
1476 
1477 
1478 /*  Standard mandates link reset for ATA  (type 0)
1479     and hard reset for SSP (type 1) , only for RECOVERY */
1480 static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1481 {
1482 	int rc;
1483 	struct sas_phy *phy = sas_get_local_phy(dev);
1484 	int reset_type = (dev->dev_type == SAS_SATA_DEV ||
1485 			(dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1486 	rc = sas_phy_reset(phy, reset_type);
1487 	sas_put_local_phy(phy);
1488 	msleep(2000);
1489 	return rc;
1490 }
1491 
1492 /* mandatory SAM-3 */
1493 int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1494 {
1495 	unsigned long flags;
1496 	int rc = TMF_RESP_FUNC_FAILED;
1497 	struct mvs_tmf_task tmf_task;
1498 	struct mvs_device * mvi_dev = dev->lldd_dev;
1499 	struct mvs_info *mvi = mvi_dev->mvi_info;
1500 
1501 	tmf_task.tmf = TMF_LU_RESET;
1502 	mvi_dev->dev_status = MVS_DEV_EH;
1503 	rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1504 	if (rc == TMF_RESP_FUNC_COMPLETE) {
1505 		spin_lock_irqsave(&mvi->lock, flags);
1506 		mvs_release_task(mvi, dev);
1507 		spin_unlock_irqrestore(&mvi->lock, flags);
1508 	}
1509 	/* If failed, fall-through I_T_Nexus reset */
1510 	mv_printk("%s for device[%x]:rc= %d\n", __func__,
1511 			mvi_dev->device_id, rc);
1512 	return rc;
1513 }
1514 
1515 int mvs_I_T_nexus_reset(struct domain_device *dev)
1516 {
1517 	unsigned long flags;
1518 	int rc = TMF_RESP_FUNC_FAILED;
1519     struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
1520 	struct mvs_info *mvi = mvi_dev->mvi_info;
1521 
1522 	if (mvi_dev->dev_status != MVS_DEV_EH)
1523 		return TMF_RESP_FUNC_COMPLETE;
1524 	else
1525 		mvi_dev->dev_status = MVS_DEV_NORMAL;
1526 	rc = mvs_debug_I_T_nexus_reset(dev);
1527 	mv_printk("%s for device[%x]:rc= %d\n",
1528 		__func__, mvi_dev->device_id, rc);
1529 
1530 	spin_lock_irqsave(&mvi->lock, flags);
1531 	mvs_release_task(mvi, dev);
1532 	spin_unlock_irqrestore(&mvi->lock, flags);
1533 
1534 	return rc;
1535 }
1536 /* optional SAM-3 */
1537 int mvs_query_task(struct sas_task *task)
1538 {
1539 	u32 tag;
1540 	struct scsi_lun lun;
1541 	struct mvs_tmf_task tmf_task;
1542 	int rc = TMF_RESP_FUNC_FAILED;
1543 
1544 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1545 		struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1546 		struct domain_device *dev = task->dev;
1547 		struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1548 		struct mvs_info *mvi = mvi_dev->mvi_info;
1549 
1550 		int_to_scsilun(cmnd->device->lun, &lun);
1551 		rc = mvs_find_tag(mvi, task, &tag);
1552 		if (rc == 0) {
1553 			rc = TMF_RESP_FUNC_FAILED;
1554 			return rc;
1555 		}
1556 
1557 		tmf_task.tmf = TMF_QUERY_TASK;
1558 		tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1559 
1560 		rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1561 		switch (rc) {
1562 		/* The task is still in Lun, release it then */
1563 		case TMF_RESP_FUNC_SUCC:
1564 		/* The task is not in Lun or failed, reset the phy */
1565 		case TMF_RESP_FUNC_FAILED:
1566 		case TMF_RESP_FUNC_COMPLETE:
1567 			break;
1568 		}
1569 	}
1570 	mv_printk("%s:rc= %d\n", __func__, rc);
1571 	return rc;
1572 }
1573 
1574 /*  mandatory SAM-3, still need free task/slot info */
1575 int mvs_abort_task(struct sas_task *task)
1576 {
1577 	struct scsi_lun lun;
1578 	struct mvs_tmf_task tmf_task;
1579 	struct domain_device *dev = task->dev;
1580 	struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1581 	struct mvs_info *mvi;
1582 	int rc = TMF_RESP_FUNC_FAILED;
1583 	unsigned long flags;
1584 	u32 tag;
1585 
1586 	if (!mvi_dev) {
1587 		mv_printk("Device has removed\n");
1588 		return TMF_RESP_FUNC_FAILED;
1589 	}
1590 
1591 	mvi = mvi_dev->mvi_info;
1592 
1593 	spin_lock_irqsave(&task->task_state_lock, flags);
1594 	if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1595 		spin_unlock_irqrestore(&task->task_state_lock, flags);
1596 		rc = TMF_RESP_FUNC_COMPLETE;
1597 		goto out;
1598 	}
1599 	spin_unlock_irqrestore(&task->task_state_lock, flags);
1600 	mvi_dev->dev_status = MVS_DEV_EH;
1601 	if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1602 		struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1603 
1604 		int_to_scsilun(cmnd->device->lun, &lun);
1605 		rc = mvs_find_tag(mvi, task, &tag);
1606 		if (rc == 0) {
1607 			mv_printk("No such tag in %s\n", __func__);
1608 			rc = TMF_RESP_FUNC_FAILED;
1609 			return rc;
1610 		}
1611 
1612 		tmf_task.tmf = TMF_ABORT_TASK;
1613 		tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1614 
1615 		rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1616 
1617 		/* if successful, clear the task and callback forwards.*/
1618 		if (rc == TMF_RESP_FUNC_COMPLETE) {
1619 			u32 slot_no;
1620 			struct mvs_slot_info *slot;
1621 
1622 			if (task->lldd_task) {
1623 				slot = task->lldd_task;
1624 				slot_no = (u32) (slot - mvi->slot_info);
1625 				spin_lock_irqsave(&mvi->lock, flags);
1626 				mvs_slot_complete(mvi, slot_no, 1);
1627 				spin_unlock_irqrestore(&mvi->lock, flags);
1628 			}
1629 		}
1630 
1631 	} else if (task->task_proto & SAS_PROTOCOL_SATA ||
1632 		task->task_proto & SAS_PROTOCOL_STP) {
1633 		if (SAS_SATA_DEV == dev->dev_type) {
1634 			struct mvs_slot_info *slot = task->lldd_task;
1635 			u32 slot_idx = (u32)(slot - mvi->slot_info);
1636 			mv_dprintk("mvs_abort_task() mvi=%p task=%p "
1637 				   "slot=%p slot_idx=x%x\n",
1638 				   mvi, task, slot, slot_idx);
1639 			task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1640 			mvs_slot_task_free(mvi, task, slot, slot_idx);
1641 			rc = TMF_RESP_FUNC_COMPLETE;
1642 			goto out;
1643 		}
1644 
1645 	}
1646 out:
1647 	if (rc != TMF_RESP_FUNC_COMPLETE)
1648 		mv_printk("%s:rc= %d\n", __func__, rc);
1649 	return rc;
1650 }
1651 
1652 int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1653 {
1654 	int rc = TMF_RESP_FUNC_FAILED;
1655 	struct mvs_tmf_task tmf_task;
1656 
1657 	tmf_task.tmf = TMF_ABORT_TASK_SET;
1658 	rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1659 
1660 	return rc;
1661 }
1662 
1663 int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1664 {
1665 	int rc = TMF_RESP_FUNC_FAILED;
1666 	struct mvs_tmf_task tmf_task;
1667 
1668 	tmf_task.tmf = TMF_CLEAR_ACA;
1669 	rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1670 
1671 	return rc;
1672 }
1673 
1674 int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1675 {
1676 	int rc = TMF_RESP_FUNC_FAILED;
1677 	struct mvs_tmf_task tmf_task;
1678 
1679 	tmf_task.tmf = TMF_CLEAR_TASK_SET;
1680 	rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1681 
1682 	return rc;
1683 }
1684 
1685 static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1686 			u32 slot_idx, int err)
1687 {
1688 	struct mvs_device *mvi_dev = task->dev->lldd_dev;
1689 	struct task_status_struct *tstat = &task->task_status;
1690 	struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
1691 	int stat = SAM_STAT_GOOD;
1692 
1693 
1694 	resp->frame_len = sizeof(struct dev_to_host_fis);
1695 	memcpy(&resp->ending_fis[0],
1696 	       SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
1697 	       sizeof(struct dev_to_host_fis));
1698 	tstat->buf_valid_size = sizeof(*resp);
1699 	if (unlikely(err)) {
1700 		if (unlikely(err & CMD_ISS_STPD))
1701 			stat = SAS_OPEN_REJECT;
1702 		else
1703 			stat = SAS_PROTO_RESPONSE;
1704        }
1705 
1706 	return stat;
1707 }
1708 
1709 void mvs_set_sense(u8 *buffer, int len, int d_sense,
1710 		int key, int asc, int ascq)
1711 {
1712 	memset(buffer, 0, len);
1713 
1714 	if (d_sense) {
1715 		/* Descriptor format */
1716 		if (len < 4) {
1717 			mv_printk("Length %d of sense buffer too small to "
1718 				"fit sense %x:%x:%x", len, key, asc, ascq);
1719 		}
1720 
1721 		buffer[0] = 0x72;		/* Response Code	*/
1722 		if (len > 1)
1723 			buffer[1] = key;	/* Sense Key */
1724 		if (len > 2)
1725 			buffer[2] = asc;	/* ASC	*/
1726 		if (len > 3)
1727 			buffer[3] = ascq;	/* ASCQ	*/
1728 	} else {
1729 		if (len < 14) {
1730 			mv_printk("Length %d of sense buffer too small to "
1731 				"fit sense %x:%x:%x", len, key, asc, ascq);
1732 		}
1733 
1734 		buffer[0] = 0x70;		/* Response Code	*/
1735 		if (len > 2)
1736 			buffer[2] = key;	/* Sense Key */
1737 		if (len > 7)
1738 			buffer[7] = 0x0a;	/* Additional Sense Length */
1739 		if (len > 12)
1740 			buffer[12] = asc;	/* ASC */
1741 		if (len > 13)
1742 			buffer[13] = ascq; /* ASCQ */
1743 	}
1744 
1745 	return;
1746 }
1747 
1748 void mvs_fill_ssp_resp_iu(struct ssp_response_iu *iu,
1749 				u8 key, u8 asc, u8 asc_q)
1750 {
1751 	iu->datapres = 2;
1752 	iu->response_data_len = 0;
1753 	iu->sense_data_len = 17;
1754 	iu->status = 02;
1755 	mvs_set_sense(iu->sense_data, 17, 0,
1756 			key, asc, asc_q);
1757 }
1758 
1759 static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1760 			 u32 slot_idx)
1761 {
1762 	struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1763 	int stat;
1764 	u32 err_dw0 = le32_to_cpu(*(u32 *)slot->response);
1765 	u32 err_dw1 = le32_to_cpu(*((u32 *)slot->response + 1));
1766 	u32 tfs = 0;
1767 	enum mvs_port_type type = PORT_TYPE_SAS;
1768 
1769 	if (err_dw0 & CMD_ISS_STPD)
1770 		MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1771 
1772 	MVS_CHIP_DISP->command_active(mvi, slot_idx);
1773 
1774 	stat = SAM_STAT_CHECK_CONDITION;
1775 	switch (task->task_proto) {
1776 	case SAS_PROTOCOL_SSP:
1777 	{
1778 		stat = SAS_ABORTED_TASK;
1779 		if ((err_dw0 & NO_DEST) || err_dw1 & bit(31)) {
1780 			struct ssp_response_iu *iu = slot->response +
1781 				sizeof(struct mvs_err_info);
1782 			mvs_fill_ssp_resp_iu(iu, NOT_READY, 0x04, 01);
1783 			sas_ssp_task_response(mvi->dev, task, iu);
1784 			stat = SAM_STAT_CHECK_CONDITION;
1785 		}
1786 		if (err_dw1 & bit(31))
1787 			mv_printk("reuse same slot, retry command.\n");
1788 		break;
1789 	}
1790 	case SAS_PROTOCOL_SMP:
1791 		stat = SAM_STAT_CHECK_CONDITION;
1792 		break;
1793 
1794 	case SAS_PROTOCOL_SATA:
1795 	case SAS_PROTOCOL_STP:
1796 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
1797 	{
1798 		task->ata_task.use_ncq = 0;
1799 		stat = SAS_PROTO_RESPONSE;
1800 		mvs_sata_done(mvi, task, slot_idx, err_dw0);
1801 	}
1802 		break;
1803 	default:
1804 		break;
1805 	}
1806 
1807 	return stat;
1808 }
1809 
1810 int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
1811 {
1812 	u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1813 	struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1814 	struct sas_task *task = slot->task;
1815 	struct mvs_device *mvi_dev = NULL;
1816 	struct task_status_struct *tstat;
1817 	struct domain_device *dev;
1818 	u32 aborted;
1819 
1820 	void *to;
1821 	enum exec_status sts;
1822 
1823 	if (unlikely(!task || !task->lldd_task || !task->dev))
1824 		return -1;
1825 
1826 	tstat = &task->task_status;
1827 	dev = task->dev;
1828 	mvi_dev = dev->lldd_dev;
1829 
1830 	spin_lock(&task->task_state_lock);
1831 	task->task_state_flags &=
1832 		~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1833 	task->task_state_flags |= SAS_TASK_STATE_DONE;
1834 	/* race condition*/
1835 	aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
1836 	spin_unlock(&task->task_state_lock);
1837 
1838 	memset(tstat, 0, sizeof(*tstat));
1839 	tstat->resp = SAS_TASK_COMPLETE;
1840 
1841 	if (unlikely(aborted)) {
1842 		tstat->stat = SAS_ABORTED_TASK;
1843 		if (mvi_dev && mvi_dev->running_req)
1844 			mvi_dev->running_req--;
1845 		if (sas_protocol_ata(task->task_proto))
1846 			mvs_free_reg_set(mvi, mvi_dev);
1847 
1848 		mvs_slot_task_free(mvi, task, slot, slot_idx);
1849 		return -1;
1850 	}
1851 
1852 	/* when no device attaching, go ahead and complete by error handling*/
1853 	if (unlikely(!mvi_dev || flags)) {
1854 		if (!mvi_dev)
1855 			mv_dprintk("port has not device.\n");
1856 		tstat->stat = SAS_PHY_DOWN;
1857 		goto out;
1858 	}
1859 
1860 	/*
1861 	 * error info record present; slot->response is 32 bit aligned but may
1862 	 * not be 64 bit aligned, so check for zero in two 32 bit reads
1863 	 */
1864 	if (unlikely((rx_desc & RXQ_ERR)
1865 		     && (*((u32 *)slot->response)
1866 			 || *(((u32 *)slot->response) + 1)))) {
1867 		mv_dprintk("port %d slot %d rx_desc %X has error info"
1868 			"%016llX.\n", slot->port->sas_port.id, slot_idx,
1869 			 rx_desc, get_unaligned_le64(slot->response));
1870 		tstat->stat = mvs_slot_err(mvi, task, slot_idx);
1871 		tstat->resp = SAS_TASK_COMPLETE;
1872 		goto out;
1873 	}
1874 
1875 	switch (task->task_proto) {
1876 	case SAS_PROTOCOL_SSP:
1877 		/* hw says status == 0, datapres == 0 */
1878 		if (rx_desc & RXQ_GOOD) {
1879 			tstat->stat = SAM_STAT_GOOD;
1880 			tstat->resp = SAS_TASK_COMPLETE;
1881 		}
1882 		/* response frame present */
1883 		else if (rx_desc & RXQ_RSP) {
1884 			struct ssp_response_iu *iu = slot->response +
1885 						sizeof(struct mvs_err_info);
1886 			sas_ssp_task_response(mvi->dev, task, iu);
1887 		} else
1888 			tstat->stat = SAM_STAT_CHECK_CONDITION;
1889 		break;
1890 
1891 	case SAS_PROTOCOL_SMP: {
1892 			struct scatterlist *sg_resp = &task->smp_task.smp_resp;
1893 			tstat->stat = SAM_STAT_GOOD;
1894 			to = kmap_atomic(sg_page(sg_resp));
1895 			memcpy(to + sg_resp->offset,
1896 				slot->response + sizeof(struct mvs_err_info),
1897 				sg_dma_len(sg_resp));
1898 			kunmap_atomic(to);
1899 			break;
1900 		}
1901 
1902 	case SAS_PROTOCOL_SATA:
1903 	case SAS_PROTOCOL_STP:
1904 	case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
1905 			tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
1906 			break;
1907 		}
1908 
1909 	default:
1910 		tstat->stat = SAM_STAT_CHECK_CONDITION;
1911 		break;
1912 	}
1913 	if (!slot->port->port_attached) {
1914 		mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
1915 		tstat->stat = SAS_PHY_DOWN;
1916 	}
1917 
1918 
1919 out:
1920 	if (mvi_dev && mvi_dev->running_req) {
1921 		mvi_dev->running_req--;
1922 		if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
1923 			mvs_free_reg_set(mvi, mvi_dev);
1924 	}
1925 	mvs_slot_task_free(mvi, task, slot, slot_idx);
1926 	sts = tstat->stat;
1927 
1928 	spin_unlock(&mvi->lock);
1929 	if (task->task_done)
1930 		task->task_done(task);
1931 
1932 	spin_lock(&mvi->lock);
1933 
1934 	return sts;
1935 }
1936 
1937 void mvs_do_release_task(struct mvs_info *mvi,
1938 		int phy_no, struct domain_device *dev)
1939 {
1940 	u32 slot_idx;
1941 	struct mvs_phy *phy;
1942 	struct mvs_port *port;
1943 	struct mvs_slot_info *slot, *slot2;
1944 
1945 	phy = &mvi->phy[phy_no];
1946 	port = phy->port;
1947 	if (!port)
1948 		return;
1949 	/* clean cmpl queue in case request is already finished */
1950 	mvs_int_rx(mvi, false);
1951 
1952 
1953 
1954 	list_for_each_entry_safe(slot, slot2, &port->list, entry) {
1955 		struct sas_task *task;
1956 		slot_idx = (u32) (slot - mvi->slot_info);
1957 		task = slot->task;
1958 
1959 		if (dev && task->dev != dev)
1960 			continue;
1961 
1962 		mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
1963 			slot_idx, slot->slot_tag, task);
1964 		MVS_CHIP_DISP->command_active(mvi, slot_idx);
1965 
1966 		mvs_slot_complete(mvi, slot_idx, 1);
1967 	}
1968 }
1969 
1970 void mvs_release_task(struct mvs_info *mvi,
1971 		      struct domain_device *dev)
1972 {
1973 	int i, phyno[WIDE_PORT_MAX_PHY], num;
1974 	num = mvs_find_dev_phyno(dev, phyno);
1975 	for (i = 0; i < num; i++)
1976 		mvs_do_release_task(mvi, phyno[i], dev);
1977 }
1978 
1979 static void mvs_phy_disconnected(struct mvs_phy *phy)
1980 {
1981 	phy->phy_attached = 0;
1982 	phy->att_dev_info = 0;
1983 	phy->att_dev_sas_addr = 0;
1984 }
1985 
1986 static void mvs_work_queue(struct work_struct *work)
1987 {
1988 	struct delayed_work *dw = container_of(work, struct delayed_work, work);
1989 	struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
1990 	struct mvs_info *mvi = mwq->mvi;
1991 	unsigned long flags;
1992 	u32 phy_no = (unsigned long) mwq->data;
1993 	struct sas_ha_struct *sas_ha = mvi->sas;
1994 	struct mvs_phy *phy = &mvi->phy[phy_no];
1995 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1996 
1997 	spin_lock_irqsave(&mvi->lock, flags);
1998 	if (mwq->handler & PHY_PLUG_EVENT) {
1999 
2000 		if (phy->phy_event & PHY_PLUG_OUT) {
2001 			u32 tmp;
2002 			struct sas_identify_frame *id;
2003 			id = (struct sas_identify_frame *)phy->frame_rcvd;
2004 			tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
2005 			phy->phy_event &= ~PHY_PLUG_OUT;
2006 			if (!(tmp & PHY_READY_MASK)) {
2007 				sas_phy_disconnected(sas_phy);
2008 				mvs_phy_disconnected(phy);
2009 				sas_ha->notify_phy_event(sas_phy,
2010 					PHYE_LOSS_OF_SIGNAL);
2011 				mv_dprintk("phy%d Removed Device\n", phy_no);
2012 			} else {
2013 				MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2014 				mvs_update_phyinfo(mvi, phy_no, 1);
2015 				mvs_bytes_dmaed(mvi, phy_no);
2016 				mvs_port_notify_formed(sas_phy, 0);
2017 				mv_dprintk("phy%d Attached Device\n", phy_no);
2018 			}
2019 		}
2020 	} else if (mwq->handler & EXP_BRCT_CHG) {
2021 		phy->phy_event &= ~EXP_BRCT_CHG;
2022 		sas_ha->notify_port_event(sas_phy,
2023 				PORTE_BROADCAST_RCVD);
2024 		mv_dprintk("phy%d Got Broadcast Change\n", phy_no);
2025 	}
2026 	list_del(&mwq->entry);
2027 	spin_unlock_irqrestore(&mvi->lock, flags);
2028 	kfree(mwq);
2029 }
2030 
2031 static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
2032 {
2033 	struct mvs_wq *mwq;
2034 	int ret = 0;
2035 
2036 	mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
2037 	if (mwq) {
2038 		mwq->mvi = mvi;
2039 		mwq->data = data;
2040 		mwq->handler = handler;
2041 		MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
2042 		list_add_tail(&mwq->entry, &mvi->wq_list);
2043 		schedule_delayed_work(&mwq->work_q, HZ * 2);
2044 	} else
2045 		ret = -ENOMEM;
2046 
2047 	return ret;
2048 }
2049 
2050 static void mvs_sig_time_out(unsigned long tphy)
2051 {
2052 	struct mvs_phy *phy = (struct mvs_phy *)tphy;
2053 	struct mvs_info *mvi = phy->mvi;
2054 	u8 phy_no;
2055 
2056 	for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
2057 		if (&mvi->phy[phy_no] == phy) {
2058 			mv_dprintk("Get signature time out, reset phy %d\n",
2059 				phy_no+mvi->id*mvi->chip->n_phy);
2060 			MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_HARD_RESET);
2061 		}
2062 	}
2063 }
2064 
2065 void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
2066 {
2067 	u32 tmp;
2068 	struct mvs_phy *phy = &mvi->phy[phy_no];
2069 
2070 	phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
2071 	MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
2072 	mv_dprintk("phy %d ctrl sts=0x%08X.\n", phy_no+mvi->id*mvi->chip->n_phy,
2073 		MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
2074 	mv_dprintk("phy %d irq sts = 0x%08X\n", phy_no+mvi->id*mvi->chip->n_phy,
2075 		phy->irq_status);
2076 
2077 	/*
2078 	* events is port event now ,
2079 	* we need check the interrupt status which belongs to per port.
2080 	*/
2081 
2082 	if (phy->irq_status & PHYEV_DCDR_ERR) {
2083 		mv_dprintk("phy %d STP decoding error.\n",
2084 		phy_no + mvi->id*mvi->chip->n_phy);
2085 	}
2086 
2087 	if (phy->irq_status & PHYEV_POOF) {
2088 		mdelay(500);
2089 		if (!(phy->phy_event & PHY_PLUG_OUT)) {
2090 			int dev_sata = phy->phy_type & PORT_TYPE_SATA;
2091 			int ready;
2092 			mvs_do_release_task(mvi, phy_no, NULL);
2093 			phy->phy_event |= PHY_PLUG_OUT;
2094 			MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
2095 			mvs_handle_event(mvi,
2096 				(void *)(unsigned long)phy_no,
2097 				PHY_PLUG_EVENT);
2098 			ready = mvs_is_phy_ready(mvi, phy_no);
2099 			if (ready || dev_sata) {
2100 				if (MVS_CHIP_DISP->stp_reset)
2101 					MVS_CHIP_DISP->stp_reset(mvi,
2102 							phy_no);
2103 				else
2104 					MVS_CHIP_DISP->phy_reset(mvi,
2105 							phy_no, MVS_SOFT_RESET);
2106 				return;
2107 			}
2108 		}
2109 	}
2110 
2111 	if (phy->irq_status & PHYEV_COMWAKE) {
2112 		tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2113 		MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2114 					tmp | PHYEV_SIG_FIS);
2115 		if (phy->timer.function == NULL) {
2116 			phy->timer.data = (unsigned long)phy;
2117 			phy->timer.function = mvs_sig_time_out;
2118 			phy->timer.expires = jiffies + 5*HZ;
2119 			add_timer(&phy->timer);
2120 		}
2121 	}
2122 	if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2123 		phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
2124 		mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2125 		if (phy->phy_status) {
2126 			mdelay(10);
2127 			MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2128 			if (phy->phy_type & PORT_TYPE_SATA) {
2129 				tmp = MVS_CHIP_DISP->read_port_irq_mask(
2130 						mvi, phy_no);
2131 				tmp &= ~PHYEV_SIG_FIS;
2132 				MVS_CHIP_DISP->write_port_irq_mask(mvi,
2133 							phy_no, tmp);
2134 			}
2135 			mvs_update_phyinfo(mvi, phy_no, 0);
2136 			if (phy->phy_type & PORT_TYPE_SAS) {
2137 				MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_PHY_TUNE);
2138 				mdelay(10);
2139 			}
2140 
2141 			mvs_bytes_dmaed(mvi, phy_no);
2142 			/* whether driver is going to handle hot plug */
2143 			if (phy->phy_event & PHY_PLUG_OUT) {
2144 				mvs_port_notify_formed(&phy->sas_phy, 0);
2145 				phy->phy_event &= ~PHY_PLUG_OUT;
2146 			}
2147 		} else {
2148 			mv_dprintk("plugin interrupt but phy%d is gone\n",
2149 				phy_no + mvi->id*mvi->chip->n_phy);
2150 		}
2151 	} else if (phy->irq_status & PHYEV_BROAD_CH) {
2152 		mv_dprintk("phy %d broadcast change.\n",
2153 			phy_no + mvi->id*mvi->chip->n_phy);
2154 		mvs_handle_event(mvi, (void *)(unsigned long)phy_no,
2155 				EXP_BRCT_CHG);
2156 	}
2157 }
2158 
2159 int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
2160 {
2161 	u32 rx_prod_idx, rx_desc;
2162 	bool attn = false;
2163 
2164 	/* the first dword in the RX ring is special: it contains
2165 	 * a mirror of the hardware's RX producer index, so that
2166 	 * we don't have to stall the CPU reading that register.
2167 	 * The actual RX ring is offset by one dword, due to this.
2168 	 */
2169 	rx_prod_idx = mvi->rx_cons;
2170 	mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2171 	if (mvi->rx_cons == 0xfff)	/* h/w hasn't touched RX ring yet */
2172 		return 0;
2173 
2174 	/* The CMPL_Q may come late, read from register and try again
2175 	* note: if coalescing is enabled,
2176 	* it will need to read from register every time for sure
2177 	*/
2178 	if (unlikely(mvi->rx_cons == rx_prod_idx))
2179 		mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
2180 
2181 	if (mvi->rx_cons == rx_prod_idx)
2182 		return 0;
2183 
2184 	while (mvi->rx_cons != rx_prod_idx) {
2185 		/* increment our internal RX consumer pointer */
2186 		rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
2187 		rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2188 
2189 		if (likely(rx_desc & RXQ_DONE))
2190 			mvs_slot_complete(mvi, rx_desc, 0);
2191 		if (rx_desc & RXQ_ATTN) {
2192 			attn = true;
2193 		} else if (rx_desc & RXQ_ERR) {
2194 			if (!(rx_desc & RXQ_DONE))
2195 				mvs_slot_complete(mvi, rx_desc, 0);
2196 		} else if (rx_desc & RXQ_SLOT_RESET) {
2197 			mvs_slot_free(mvi, rx_desc);
2198 		}
2199 	}
2200 
2201 	if (attn && self_clear)
2202 		MVS_CHIP_DISP->int_full(mvi);
2203 	return 0;
2204 }
2205 
2206