2 * Marvell 88SE64xx/88SE94xx main function
4 * Copyright 2007 Red Hat, Inc.
5 * Copyright 2008 Marvell. <kewei@marvell.com>
6 * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
8 * This file is licensed under GPLv2.
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
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.
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
28 static int mvs_find_tag(struct mvs_info *mvi, struct sas_task *task, u32 *tag)
30 if (task->lldd_task) {
31 struct mvs_slot_info *slot;
32 slot = task->lldd_task;
33 *tag = slot->slot_tag;
39 void mvs_tag_clear(struct mvs_info *mvi, u32 tag)
41 void *bitmap = mvi->tags;
42 clear_bit(tag, bitmap);
45 void mvs_tag_free(struct mvs_info *mvi, u32 tag)
47 mvs_tag_clear(mvi, tag);
50 void mvs_tag_set(struct mvs_info *mvi, unsigned int tag)
52 void *bitmap = mvi->tags;
56 inline int mvs_tag_alloc(struct mvs_info *mvi, u32 *tag_out)
58 unsigned int index, tag;
59 void *bitmap = mvi->tags;
61 index = find_first_zero_bit(bitmap, mvi->tags_num);
63 if (tag >= mvi->tags_num)
64 return -SAS_QUEUE_FULL;
65 mvs_tag_set(mvi, tag);
70 void mvs_tag_init(struct mvs_info *mvi)
73 for (i = 0; i < mvi->tags_num; ++i)
74 mvs_tag_clear(mvi, i);
77 struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
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;
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);
89 while (sha->sas_phy[j]) {
90 if (sha->sas_phy[j] == phy)
98 hi = j/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
99 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
105 int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
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;
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) {
118 while (sha->sas_phy[j]) {
119 if (sha->sas_phy[j] == phy)
123 phyno[n] = (j >= mvi->chip->n_phy) ?
124 (j - mvi->chip->n_phy) : j;
135 struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
139 for (dev_no = 0; dev_no < MVS_MAX_DEVICES; dev_no++) {
140 if (mvi->devices[dev_no].taskfileset == MVS_ID_NOT_MAPPED)
143 if (mvi->devices[dev_no].taskfileset == reg_set)
144 return &mvi->devices[dev_no];
149 static inline void mvs_free_reg_set(struct mvs_info *mvi,
150 struct mvs_device *dev)
153 mv_printk("device has been free.\n");
156 if (dev->taskfileset == MVS_ID_NOT_MAPPED)
158 MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
161 static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
162 struct mvs_device *dev)
164 if (dev->taskfileset != MVS_ID_NOT_MAPPED)
166 return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
169 void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
172 for_each_phy(phy_mask, phy_mask, no) {
175 MVS_CHIP_DISP->phy_reset(mvi, no, hard);
179 int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
182 int rc = 0, phy_id = sas_phy->id;
184 struct sas_ha_struct *sha = sas_phy->ha;
185 struct mvs_info *mvi = NULL;
187 while (sha->sas_phy[i]) {
188 if (sha->sas_phy[i] == sas_phy)
192 hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
193 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
196 case PHY_FUNC_SET_LINK_RATE:
197 MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
200 case PHY_FUNC_HARD_RESET:
201 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
202 if (tmp & PHY_RST_HARD)
204 MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_HARD_RESET);
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);
212 case PHY_FUNC_DISABLE:
213 MVS_CHIP_DISP->phy_disable(mvi, phy_id);
215 case PHY_FUNC_RELEASE_SPINUP_HOLD:
223 void __devinit mvs_set_sas_addr(struct mvs_info *mvi, int port_id,
224 u32 off_lo, u32 off_hi, u64 sas_addr)
226 u32 lo = (u32)sas_addr;
227 u32 hi = (u32)(sas_addr>>32);
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);
235 static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
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)
243 if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
244 && phy->phy_type & PORT_TYPE_SAS) {
249 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
252 struct sas_phy *sphy = sas_phy->phy;
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();
261 if (phy->phy_type & PORT_TYPE_SAS) {
262 struct sas_identify_frame *id;
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;
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);
274 } else if (phy->phy_type & PORT_TYPE_SATA) {
277 mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
279 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
281 mvi->sas->notify_port_event(sas_phy,
285 void mvs_scan_start(struct Scsi_Host *shost)
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;
293 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
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);
300 mvs_prv->scan_finished = 1;
303 int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
305 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
306 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
308 if (mvs_prv->scan_finished == 0)
311 scsi_flush_work(shost);
315 static int mvs_task_prep_smp(struct mvs_info *mvi,
316 struct mvs_task_exec_info *tei)
319 struct sas_task *task = tei->task;
320 struct mvs_cmd_hdr *hdr = tei->hdr;
321 struct domain_device *dev = task->dev;
322 struct asd_sas_port *sas_port = dev->port;
323 struct scatterlist *sg_req, *sg_resp;
324 u32 req_len, resp_len, tag = tei->tag;
327 dma_addr_t buf_tmp_dma;
329 struct mvs_slot_info *slot = &mvi->slot_info[tag];
330 u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
333 * DMA-map SMP request, response buffers
335 sg_req = &task->smp_task.smp_req;
336 elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
339 req_len = sg_dma_len(sg_req);
341 sg_resp = &task->smp_task.smp_resp;
342 elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
347 resp_len = SB_RFB_MAX;
349 /* must be in dwords */
350 if ((req_len & 0x3) || (resp_len & 0x3)) {
356 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
359 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
361 buf_tmp_dma = slot->buf_dma;
363 hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
365 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
367 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
369 buf_tmp += MVS_OAF_SZ;
370 buf_tmp_dma += MVS_OAF_SZ;
372 /* region 3: PRD table *********************************** */
375 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
379 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
383 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
384 slot->response = buf_tmp;
385 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
386 if (mvi->flags & MVF_FLAG_SOC)
387 hdr->reserved[0] = 0;
390 * Fill in TX ring and command slot header
392 slot->tx = mvi->tx_prod;
393 mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
395 (sas_port->phy_mask << TXQ_PHY_SHIFT));
398 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
399 hdr->tags = cpu_to_le32(tag);
402 /* generate open address frame hdr (first 12 bytes) */
403 /* initiator, SMP, ftype 1h */
404 buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
405 buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
406 *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
407 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
409 /* fill in PRD (scatter/gather) table, if any */
410 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
415 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
418 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
423 static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
425 struct ata_queued_cmd *qc = task->uldd_task;
428 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
429 qc->tf.command == ATA_CMD_FPDMA_READ) {
438 static int mvs_task_prep_ata(struct mvs_info *mvi,
439 struct mvs_task_exec_info *tei)
441 struct sas_task *task = tei->task;
442 struct domain_device *dev = task->dev;
443 struct mvs_device *mvi_dev = dev->lldd_dev;
444 struct mvs_cmd_hdr *hdr = tei->hdr;
445 struct asd_sas_port *sas_port = dev->port;
446 struct mvs_slot_info *slot;
448 u32 tag = tei->tag, hdr_tag;
451 u8 *buf_cmd, *buf_oaf;
452 dma_addr_t buf_tmp_dma;
453 u32 i, req_len, resp_len;
454 const u32 max_resp_len = SB_RFB_MAX;
456 if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
457 mv_dprintk("Have not enough regiset for dev %d.\n",
461 slot = &mvi->slot_info[tag];
462 slot->tx = mvi->tx_prod;
463 del_q = TXQ_MODE_I | tag |
464 (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
465 (sas_port->phy_mask << TXQ_PHY_SHIFT) |
466 (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
467 mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
469 if (task->data_dir == DMA_FROM_DEVICE)
470 flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
472 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
474 if (task->ata_task.use_ncq)
476 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
477 if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
481 hdr->flags = cpu_to_le32(flags);
483 if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
484 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
488 hdr->tags = cpu_to_le32(hdr_tag);
490 hdr->data_len = cpu_to_le32(task->total_xfer_len);
493 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
496 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
497 buf_cmd = buf_tmp = slot->buf;
498 buf_tmp_dma = slot->buf_dma;
500 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
502 buf_tmp += MVS_ATA_CMD_SZ;
503 buf_tmp_dma += MVS_ATA_CMD_SZ;
505 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
506 /* used for STP. unused for SATA? */
508 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
510 buf_tmp += MVS_OAF_SZ;
511 buf_tmp_dma += MVS_OAF_SZ;
513 /* region 3: PRD table ********************************************* */
517 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
520 i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
525 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
526 slot->response = buf_tmp;
527 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
528 if (mvi->flags & MVF_FLAG_SOC)
529 hdr->reserved[0] = 0;
531 req_len = sizeof(struct host_to_dev_fis);
532 resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
533 sizeof(struct mvs_err_info) - i;
535 /* request, response lengths */
536 resp_len = min(resp_len, max_resp_len);
537 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
539 if (likely(!task->ata_task.device_control_reg_update))
540 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
541 /* fill in command FIS and ATAPI CDB */
542 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
543 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
544 memcpy(buf_cmd + STP_ATAPI_CMD,
545 task->ata_task.atapi_packet, 16);
547 /* generate open address frame hdr (first 12 bytes) */
548 /* initiator, STP, ftype 1h */
549 buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
550 buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
551 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
552 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
554 /* fill in PRD (scatter/gather) table, if any */
555 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
557 if (task->data_dir == DMA_FROM_DEVICE)
558 MVS_CHIP_DISP->dma_fix(mvi, sas_port->phy_mask,
559 TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
564 static int mvs_task_prep_ssp(struct mvs_info *mvi,
565 struct mvs_task_exec_info *tei, int is_tmf,
566 struct mvs_tmf_task *tmf)
568 struct sas_task *task = tei->task;
569 struct mvs_cmd_hdr *hdr = tei->hdr;
570 struct mvs_port *port = tei->port;
571 struct domain_device *dev = task->dev;
572 struct mvs_device *mvi_dev = dev->lldd_dev;
573 struct asd_sas_port *sas_port = dev->port;
574 struct mvs_slot_info *slot;
576 struct ssp_frame_hdr *ssp_hdr;
578 u8 *buf_cmd, *buf_oaf, fburst = 0;
579 dma_addr_t buf_tmp_dma;
581 u32 resp_len, req_len, i, tag = tei->tag;
582 const u32 max_resp_len = SB_RFB_MAX;
585 slot = &mvi->slot_info[tag];
587 phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
588 sas_port->phy_mask) & TXQ_PHY_MASK;
590 slot->tx = mvi->tx_prod;
591 mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
592 (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
593 (phy_mask << TXQ_PHY_SHIFT));
596 if (task->ssp_task.enable_first_burst) {
601 flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
603 flags |= (MCH_SSP_FR_CMD << MCH_SSP_FR_TYPE_SHIFT);
605 hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
606 hdr->tags = cpu_to_le32(tag);
607 hdr->data_len = cpu_to_le32(task->total_xfer_len);
610 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
613 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
614 buf_cmd = buf_tmp = slot->buf;
615 buf_tmp_dma = slot->buf_dma;
617 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
619 buf_tmp += MVS_SSP_CMD_SZ;
620 buf_tmp_dma += MVS_SSP_CMD_SZ;
622 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
624 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
626 buf_tmp += MVS_OAF_SZ;
627 buf_tmp_dma += MVS_OAF_SZ;
629 /* region 3: PRD table ********************************************* */
632 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
636 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
640 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
641 slot->response = buf_tmp;
642 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
643 if (mvi->flags & MVF_FLAG_SOC)
644 hdr->reserved[0] = 0;
646 resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
647 sizeof(struct mvs_err_info) - i;
648 resp_len = min(resp_len, max_resp_len);
650 req_len = sizeof(struct ssp_frame_hdr) + 28;
652 /* request, response lengths */
653 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
655 /* generate open address frame hdr (first 12 bytes) */
656 /* initiator, SSP, ftype 1h */
657 buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
658 buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
659 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
660 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
662 /* fill in SSP frame header (Command Table.SSP frame header) */
663 ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
666 ssp_hdr->frame_type = SSP_TASK;
668 ssp_hdr->frame_type = SSP_COMMAND;
670 memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
671 HASHED_SAS_ADDR_SIZE);
672 memcpy(ssp_hdr->hashed_src_addr,
673 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
674 ssp_hdr->tag = cpu_to_be16(tag);
676 /* fill in IU for TASK and Command Frame */
677 buf_cmd += sizeof(*ssp_hdr);
678 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
680 if (ssp_hdr->frame_type != SSP_TASK) {
681 buf_cmd[9] = fburst | task->ssp_task.task_attr |
682 (task->ssp_task.task_prio << 3);
683 memcpy(buf_cmd + 12, &task->ssp_task.cdb, 16);
685 buf_cmd[10] = tmf->tmf;
690 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
692 tmf->tag_of_task_to_be_managed & 0xff;
698 /* fill in PRD (scatter/gather) table, if any */
699 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
703 #define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == NO_DEVICE)))
704 static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
705 struct mvs_tmf_task *tmf, int *pass)
707 struct domain_device *dev = task->dev;
708 struct mvs_device *mvi_dev = dev->lldd_dev;
709 struct mvs_task_exec_info tei;
710 struct mvs_slot_info *slot;
711 u32 tag = 0xdeadbeef, n_elem = 0;
715 struct task_status_struct *tsm = &task->task_status;
717 tsm->resp = SAS_TASK_UNDELIVERED;
718 tsm->stat = SAS_PHY_DOWN;
720 * libsas will use dev->port, should
721 * not call task_done for sata
723 if (dev->dev_type != SATA_DEV)
724 task->task_done(task);
728 if (DEV_IS_GONE(mvi_dev)) {
730 mv_dprintk("device %d not ready.\n",
733 mv_dprintk("device %016llx not ready.\n",
734 SAS_ADDR(dev->sas_addr));
739 tei.port = dev->port->lldd_port;
740 if (tei.port && !tei.port->port_attached && !tmf) {
741 if (sas_protocol_ata(task->task_proto)) {
742 struct task_status_struct *ts = &task->task_status;
743 mv_dprintk("SATA/STP port %d does not attach"
744 "device.\n", dev->port->id);
745 ts->resp = SAS_TASK_COMPLETE;
746 ts->stat = SAS_PHY_DOWN;
748 task->task_done(task);
751 struct task_status_struct *ts = &task->task_status;
752 mv_dprintk("SAS port %d does not attach"
753 "device.\n", dev->port->id);
754 ts->resp = SAS_TASK_UNDELIVERED;
755 ts->stat = SAS_PHY_DOWN;
756 task->task_done(task);
761 if (!sas_protocol_ata(task->task_proto)) {
762 if (task->num_scatter) {
763 n_elem = dma_map_sg(mvi->dev,
773 n_elem = task->num_scatter;
776 rc = mvs_tag_alloc(mvi, &tag);
780 slot = &mvi->slot_info[tag];
782 task->lldd_task = NULL;
783 slot->n_elem = n_elem;
784 slot->slot_tag = tag;
786 slot->buf = pci_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
789 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
792 tei.hdr = &mvi->slot[tag];
795 switch (task->task_proto) {
796 case SAS_PROTOCOL_SMP:
797 rc = mvs_task_prep_smp(mvi, &tei);
799 case SAS_PROTOCOL_SSP:
800 rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
802 case SAS_PROTOCOL_SATA:
803 case SAS_PROTOCOL_STP:
804 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
805 rc = mvs_task_prep_ata(mvi, &tei);
808 dev_printk(KERN_ERR, mvi->dev,
809 "unknown sas_task proto: 0x%x\n",
816 mv_dprintk("rc is %x\n", rc);
817 goto err_out_slot_buf;
820 slot->port = tei.port;
821 task->lldd_task = slot;
822 list_add_tail(&slot->entry, &tei.port->list);
823 spin_lock(&task->task_state_lock);
824 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
825 spin_unlock(&task->task_state_lock);
827 mvi_dev->running_req++;
829 mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
834 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
836 mvs_tag_free(mvi, tag);
839 dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
840 if (!sas_protocol_ata(task->task_proto))
842 dma_unmap_sg(mvi->dev, task->scatter, n_elem,
848 static struct mvs_task_list *mvs_task_alloc_list(int *num, gfp_t gfp_flags)
850 struct mvs_task_list *first = NULL;
852 for (; *num > 0; --*num) {
853 struct mvs_task_list *mvs_list = kmem_cache_zalloc(mvs_task_list_cache, gfp_flags);
858 INIT_LIST_HEAD(&mvs_list->list);
862 list_add_tail(&mvs_list->list, &first->list);
869 static inline void mvs_task_free_list(struct mvs_task_list *mvs_list)
872 struct list_head *pos, *a;
873 struct mvs_task_list *mlist = NULL;
875 __list_add(&list, mvs_list->list.prev, &mvs_list->list);
877 list_for_each_safe(pos, a, &list) {
879 mlist = list_entry(pos, struct mvs_task_list, list);
880 kmem_cache_free(mvs_task_list_cache, mlist);
884 static int mvs_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
885 struct completion *completion, int is_tmf,
886 struct mvs_tmf_task *tmf)
888 struct domain_device *dev = task->dev;
889 struct mvs_info *mvi = NULL;
892 unsigned long flags = 0;
894 mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
896 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
897 spin_unlock_irq(dev->sata_dev.ap->lock);
899 spin_lock_irqsave(&mvi->lock, flags);
900 rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
902 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
905 MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
906 (MVS_CHIP_SLOT_SZ - 1));
907 spin_unlock_irqrestore(&mvi->lock, flags);
909 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
910 spin_lock_irq(dev->sata_dev.ap->lock);
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)
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;
928 unsigned long flags = 0;
930 mvs_list = mvs_task_alloc_list(&n, gfp_flags);
932 printk(KERN_ERR "%s: mvs alloc list failed.\n", __func__);
937 __list_add(&q, mvs_list->list.prev, &mvs_list->list);
939 list_for_each_entry(a, &q, list) {
941 t = list_entry(t->list.next, struct sas_task, list);
944 list_for_each_entry(a, &q , list) {
947 mvi = ((struct mvs_device *)t->dev->lldd_dev)->mvi_info;
949 spin_lock_irqsave(&mvi->lock, flags);
950 rc = mvs_task_prep(t, mvi, is_tmf, tmf, &pass[mvi->id]);
952 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
953 spin_unlock_irqrestore(&mvi->lock, flags);
957 MVS_CHIP_DISP->start_delivery(mpi->mvi[0],
958 (mpi->mvi[0]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
961 MVS_CHIP_DISP->start_delivery(mpi->mvi[1],
962 (mpi->mvi[1]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
968 mvs_task_free_list(mvs_list);
973 int mvs_queue_command(struct sas_task *task, const int num,
976 struct mvs_device *mvi_dev = task->dev->lldd_dev;
977 struct sas_ha_struct *sas = mvi_dev->mvi_info->sas;
979 if (sas->lldd_max_execute_num < 2)
980 return mvs_task_exec(task, num, gfp_flags, NULL, 0, NULL);
982 return mvs_collector_task_exec(task, num, gfp_flags, NULL, 0, NULL);
985 static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
987 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
988 mvs_tag_clear(mvi, slot_idx);
991 static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
992 struct mvs_slot_info *slot, u32 slot_idx)
998 if (!sas_protocol_ata(task->task_proto))
1000 dma_unmap_sg(mvi->dev, task->scatter,
1001 slot->n_elem, task->data_dir);
1003 switch (task->task_proto) {
1004 case SAS_PROTOCOL_SMP:
1005 dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
1006 PCI_DMA_FROMDEVICE);
1007 dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
1011 case SAS_PROTOCOL_SATA:
1012 case SAS_PROTOCOL_STP:
1013 case SAS_PROTOCOL_SSP:
1020 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
1023 list_del_init(&slot->entry);
1024 task->lldd_task = NULL;
1027 slot->slot_tag = 0xFFFFFFFF;
1028 mvs_slot_free(mvi, slot_idx);
1031 static void mvs_update_wideport(struct mvs_info *mvi, int phy_no)
1033 struct mvs_phy *phy = &mvi->phy[phy_no];
1034 struct mvs_port *port = phy->port;
1037 for_each_phy(port->wide_port_phymap, j, no) {
1039 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1041 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1042 port->wide_port_phymap);
1044 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1046 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1052 static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
1055 struct mvs_phy *phy = &mvi->phy[i];
1056 struct mvs_port *port = phy->port;
1058 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
1059 if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
1061 phy->phy_attached = 1;
1066 if (phy->phy_type & PORT_TYPE_SAS) {
1067 port->wide_port_phymap &= ~(1U << i);
1068 if (!port->wide_port_phymap)
1069 port->port_attached = 0;
1070 mvs_update_wideport(mvi, i);
1071 } else if (phy->phy_type & PORT_TYPE_SATA)
1072 port->port_attached = 0;
1074 phy->phy_attached = 0;
1075 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1080 static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
1082 u32 *s = (u32 *) buf;
1087 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
1088 s[3] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1090 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
1091 s[2] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1093 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
1094 s[1] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1096 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
1097 s[0] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1099 if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1100 s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
1105 static u32 mvs_is_sig_fis_received(u32 irq_status)
1107 return irq_status & PHYEV_SIG_FIS;
1110 static void mvs_sig_remove_timer(struct mvs_phy *phy)
1112 if (phy->timer.function)
1113 del_timer(&phy->timer);
1114 phy->timer.function = NULL;
1117 void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
1119 struct mvs_phy *phy = &mvi->phy[i];
1120 struct sas_identify_frame *id;
1122 id = (struct sas_identify_frame *)phy->frame_rcvd;
1125 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
1126 phy->phy_status = mvs_is_phy_ready(mvi, i);
1129 if (phy->phy_status) {
1131 struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
1133 oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
1135 MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
1136 if (phy->phy_type & PORT_TYPE_SATA) {
1137 phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
1138 if (mvs_is_sig_fis_received(phy->irq_status)) {
1139 mvs_sig_remove_timer(phy);
1140 phy->phy_attached = 1;
1141 phy->att_dev_sas_addr =
1142 i + mvi->id * mvi->chip->n_phy;
1144 sas_phy->oob_mode = SATA_OOB_MODE;
1145 phy->frame_rcvd_size =
1146 sizeof(struct dev_to_host_fis);
1147 mvs_get_d2h_reg(mvi, i, id);
1150 dev_printk(KERN_DEBUG, mvi->dev,
1151 "Phy%d : No sig fis\n", i);
1152 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1153 MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1154 tmp | PHYEV_SIG_FIS);
1155 phy->phy_attached = 0;
1156 phy->phy_type &= ~PORT_TYPE_SATA;
1159 } else if (phy->phy_type & PORT_TYPE_SAS
1160 || phy->att_dev_info & PORT_SSP_INIT_MASK) {
1161 phy->phy_attached = 1;
1162 phy->identify.device_type =
1163 phy->att_dev_info & PORT_DEV_TYPE_MASK;
1165 if (phy->identify.device_type == SAS_END_DEV)
1166 phy->identify.target_port_protocols =
1168 else if (phy->identify.device_type != NO_DEVICE)
1169 phy->identify.target_port_protocols =
1172 sas_phy->oob_mode = SAS_OOB_MODE;
1173 phy->frame_rcvd_size =
1174 sizeof(struct sas_identify_frame);
1176 memcpy(sas_phy->attached_sas_addr,
1177 &phy->att_dev_sas_addr, SAS_ADDR_SIZE);
1179 if (MVS_CHIP_DISP->phy_work_around)
1180 MVS_CHIP_DISP->phy_work_around(mvi, i);
1182 mv_dprintk("phy %d attach dev info is %x\n",
1183 i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
1184 mv_dprintk("phy %d attach sas addr is %llx\n",
1185 i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
1188 MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
1191 static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
1193 struct sas_ha_struct *sas_ha = sas_phy->ha;
1194 struct mvs_info *mvi = NULL; int i = 0, hi;
1195 struct mvs_phy *phy = sas_phy->lldd_phy;
1196 struct asd_sas_port *sas_port = sas_phy->port;
1197 struct mvs_port *port;
1198 unsigned long flags = 0;
1202 while (sas_ha->sas_phy[i]) {
1203 if (sas_ha->sas_phy[i] == sas_phy)
1207 hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
1208 mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
1209 if (i >= mvi->chip->n_phy)
1210 port = &mvi->port[i - mvi->chip->n_phy];
1212 port = &mvi->port[i];
1214 spin_lock_irqsave(&mvi->lock, flags);
1215 port->port_attached = 1;
1217 sas_port->lldd_port = port;
1218 if (phy->phy_type & PORT_TYPE_SAS) {
1219 port->wide_port_phymap = sas_port->phy_mask;
1220 mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1221 mvs_update_wideport(mvi, sas_phy->id);
1223 /* direct attached SAS device */
1224 if (phy->att_dev_info & PORT_SSP_TRGT_MASK) {
1225 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_PHY_STAT);
1226 MVS_CHIP_DISP->write_port_cfg_data(mvi, i, 0x04);
1230 spin_unlock_irqrestore(&mvi->lock, flags);
1233 static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1235 struct domain_device *dev;
1236 struct mvs_phy *phy = sas_phy->lldd_phy;
1237 struct mvs_info *mvi = phy->mvi;
1238 struct asd_sas_port *port = sas_phy->port;
1241 while (phy != &mvi->phy[phy_no]) {
1243 if (phy_no >= MVS_MAX_PHYS)
1246 list_for_each_entry(dev, &port->dev_list, dev_list_node)
1247 mvs_do_release_task(phy->mvi, phy_no, dev);
1252 void mvs_port_formed(struct asd_sas_phy *sas_phy)
1254 mvs_port_notify_formed(sas_phy, 1);
1257 void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1259 mvs_port_notify_deformed(sas_phy, 1);
1262 struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
1265 for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
1266 if (mvi->devices[dev].dev_type == NO_DEVICE) {
1267 mvi->devices[dev].device_id = dev;
1268 return &mvi->devices[dev];
1272 if (dev == MVS_MAX_DEVICES)
1273 mv_printk("max support %d devices, ignore ..\n",
1279 void mvs_free_dev(struct mvs_device *mvi_dev)
1281 u32 id = mvi_dev->device_id;
1282 memset(mvi_dev, 0, sizeof(*mvi_dev));
1283 mvi_dev->device_id = id;
1284 mvi_dev->dev_type = NO_DEVICE;
1285 mvi_dev->dev_status = MVS_DEV_NORMAL;
1286 mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1289 int mvs_dev_found_notify(struct domain_device *dev, int lock)
1291 unsigned long flags = 0;
1293 struct mvs_info *mvi = NULL;
1294 struct domain_device *parent_dev = dev->parent;
1295 struct mvs_device *mvi_device;
1297 mvi = mvs_find_dev_mvi(dev);
1300 spin_lock_irqsave(&mvi->lock, flags);
1302 mvi_device = mvs_alloc_dev(mvi);
1307 dev->lldd_dev = mvi_device;
1308 mvi_device->dev_status = MVS_DEV_NORMAL;
1309 mvi_device->dev_type = dev->dev_type;
1310 mvi_device->mvi_info = mvi;
1311 mvi_device->sas_device = dev;
1312 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1314 u8 phy_num = parent_dev->ex_dev.num_phys;
1316 for (phy_id = 0; phy_id < phy_num; phy_id++) {
1317 phy = &parent_dev->ex_dev.ex_phy[phy_id];
1318 if (SAS_ADDR(phy->attached_sas_addr) ==
1319 SAS_ADDR(dev->sas_addr)) {
1320 mvi_device->attached_phy = phy_id;
1325 if (phy_id == phy_num) {
1326 mv_printk("Error: no attached dev:%016llx"
1328 SAS_ADDR(dev->sas_addr),
1329 SAS_ADDR(parent_dev->sas_addr));
1336 spin_unlock_irqrestore(&mvi->lock, flags);
1340 int mvs_dev_found(struct domain_device *dev)
1342 return mvs_dev_found_notify(dev, 1);
1345 void mvs_dev_gone_notify(struct domain_device *dev)
1347 unsigned long flags = 0;
1348 struct mvs_device *mvi_dev = dev->lldd_dev;
1349 struct mvs_info *mvi = mvi_dev->mvi_info;
1351 spin_lock_irqsave(&mvi->lock, flags);
1354 mv_dprintk("found dev[%d:%x] is gone.\n",
1355 mvi_dev->device_id, mvi_dev->dev_type);
1356 mvs_release_task(mvi, dev);
1357 mvs_free_reg_set(mvi, mvi_dev);
1358 mvs_free_dev(mvi_dev);
1360 mv_dprintk("found dev has gone.\n");
1362 dev->lldd_dev = NULL;
1363 mvi_dev->sas_device = NULL;
1365 spin_unlock_irqrestore(&mvi->lock, flags);
1369 void mvs_dev_gone(struct domain_device *dev)
1371 mvs_dev_gone_notify(dev);
1374 static void mvs_task_done(struct sas_task *task)
1376 if (!del_timer(&task->timer))
1378 complete(&task->completion);
1381 static void mvs_tmf_timedout(unsigned long data)
1383 struct sas_task *task = (struct sas_task *)data;
1385 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1386 complete(&task->completion);
1389 #define MVS_TASK_TIMEOUT 20
1390 static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1391 void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1394 struct sas_task *task = NULL;
1396 for (retry = 0; retry < 3; retry++) {
1397 task = sas_alloc_task(GFP_KERNEL);
1402 task->task_proto = dev->tproto;
1404 memcpy(&task->ssp_task, parameter, para_len);
1405 task->task_done = mvs_task_done;
1407 task->timer.data = (unsigned long) task;
1408 task->timer.function = mvs_tmf_timedout;
1409 task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1410 add_timer(&task->timer);
1412 res = mvs_task_exec(task, 1, GFP_KERNEL, NULL, 1, tmf);
1415 del_timer(&task->timer);
1416 mv_printk("executing internel task failed:%d\n", res);
1420 wait_for_completion(&task->completion);
1421 res = TMF_RESP_FUNC_FAILED;
1422 /* Even TMF timed out, return direct. */
1423 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1424 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1425 mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1430 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1431 task->task_status.stat == SAM_STAT_GOOD) {
1432 res = TMF_RESP_FUNC_COMPLETE;
1436 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1437 task->task_status.stat == SAS_DATA_UNDERRUN) {
1438 /* no error, but return the number of bytes of
1440 res = task->task_status.residual;
1444 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1445 task->task_status.stat == SAS_DATA_OVERRUN) {
1446 mv_dprintk("blocked task error.\n");
1450 mv_dprintk(" task to dev %016llx response: 0x%x "
1452 SAS_ADDR(dev->sas_addr),
1453 task->task_status.resp,
1454 task->task_status.stat);
1455 sas_free_task(task);
1461 BUG_ON(retry == 3 && task != NULL);
1462 sas_free_task(task);
1466 static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1467 u8 *lun, struct mvs_tmf_task *tmf)
1469 struct sas_ssp_task ssp_task;
1470 if (!(dev->tproto & SAS_PROTOCOL_SSP))
1471 return TMF_RESP_FUNC_ESUPP;
1473 memcpy(ssp_task.LUN, lun, 8);
1475 return mvs_exec_internal_tmf_task(dev, &ssp_task,
1476 sizeof(ssp_task), tmf);
1480 /* Standard mandates link reset for ATA (type 0)
1481 and hard reset for SSP (type 1) , only for RECOVERY */
1482 static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1485 struct sas_phy *phy = sas_find_local_phy(dev);
1486 int reset_type = (dev->dev_type == SATA_DEV ||
1487 (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1488 rc = sas_phy_reset(phy, reset_type);
1493 /* mandatory SAM-3 */
1494 int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1496 unsigned long flags;
1497 int rc = TMF_RESP_FUNC_FAILED;
1498 struct mvs_tmf_task tmf_task;
1499 struct mvs_device * mvi_dev = dev->lldd_dev;
1500 struct mvs_info *mvi = mvi_dev->mvi_info;
1502 tmf_task.tmf = TMF_LU_RESET;
1503 mvi_dev->dev_status = MVS_DEV_EH;
1504 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1505 if (rc == TMF_RESP_FUNC_COMPLETE) {
1506 spin_lock_irqsave(&mvi->lock, flags);
1507 mvs_release_task(mvi, dev);
1508 spin_unlock_irqrestore(&mvi->lock, flags);
1510 /* If failed, fall-through I_T_Nexus reset */
1511 mv_printk("%s for device[%x]:rc= %d\n", __func__,
1512 mvi_dev->device_id, rc);
1516 int mvs_I_T_nexus_reset(struct domain_device *dev)
1518 unsigned long flags;
1519 int rc = TMF_RESP_FUNC_FAILED;
1520 struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
1521 struct mvs_info *mvi = mvi_dev->mvi_info;
1523 if (mvi_dev->dev_status != MVS_DEV_EH)
1524 return TMF_RESP_FUNC_COMPLETE;
1526 mvi_dev->dev_status = MVS_DEV_NORMAL;
1527 rc = mvs_debug_I_T_nexus_reset(dev);
1528 mv_printk("%s for device[%x]:rc= %d\n",
1529 __func__, mvi_dev->device_id, rc);
1531 spin_lock_irqsave(&mvi->lock, flags);
1532 mvs_release_task(mvi, dev);
1533 spin_unlock_irqrestore(&mvi->lock, flags);
1537 /* optional SAM-3 */
1538 int mvs_query_task(struct sas_task *task)
1541 struct scsi_lun lun;
1542 struct mvs_tmf_task tmf_task;
1543 int rc = TMF_RESP_FUNC_FAILED;
1545 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1546 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1547 struct domain_device *dev = task->dev;
1548 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1549 struct mvs_info *mvi = mvi_dev->mvi_info;
1551 int_to_scsilun(cmnd->device->lun, &lun);
1552 rc = mvs_find_tag(mvi, task, &tag);
1554 rc = TMF_RESP_FUNC_FAILED;
1558 tmf_task.tmf = TMF_QUERY_TASK;
1559 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1561 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1563 /* The task is still in Lun, release it then */
1564 case TMF_RESP_FUNC_SUCC:
1565 /* The task is not in Lun or failed, reset the phy */
1566 case TMF_RESP_FUNC_FAILED:
1567 case TMF_RESP_FUNC_COMPLETE:
1571 mv_printk("%s:rc= %d\n", __func__, rc);
1575 /* mandatory SAM-3, still need free task/slot info */
1576 int mvs_abort_task(struct sas_task *task)
1578 struct scsi_lun lun;
1579 struct mvs_tmf_task tmf_task;
1580 struct domain_device *dev = task->dev;
1581 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1582 struct mvs_info *mvi;
1583 int rc = TMF_RESP_FUNC_FAILED;
1584 unsigned long flags;
1588 mv_printk("Device has removed\n");
1589 return TMF_RESP_FUNC_FAILED;
1592 mvi = mvi_dev->mvi_info;
1594 spin_lock_irqsave(&task->task_state_lock, flags);
1595 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1596 spin_unlock_irqrestore(&task->task_state_lock, flags);
1597 rc = TMF_RESP_FUNC_COMPLETE;
1600 spin_unlock_irqrestore(&task->task_state_lock, flags);
1601 mvi_dev->dev_status = MVS_DEV_EH;
1602 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1603 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1605 int_to_scsilun(cmnd->device->lun, &lun);
1606 rc = mvs_find_tag(mvi, task, &tag);
1608 mv_printk("No such tag in %s\n", __func__);
1609 rc = TMF_RESP_FUNC_FAILED;
1613 tmf_task.tmf = TMF_ABORT_TASK;
1614 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1616 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1618 /* if successful, clear the task and callback forwards.*/
1619 if (rc == TMF_RESP_FUNC_COMPLETE) {
1621 struct mvs_slot_info *slot;
1623 if (task->lldd_task) {
1624 slot = task->lldd_task;
1625 slot_no = (u32) (slot - mvi->slot_info);
1626 spin_lock_irqsave(&mvi->lock, flags);
1627 mvs_slot_complete(mvi, slot_no, 1);
1628 spin_unlock_irqrestore(&mvi->lock, flags);
1632 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1633 task->task_proto & SAS_PROTOCOL_STP) {
1634 if (SATA_DEV == dev->dev_type) {
1635 struct mvs_slot_info *slot = task->lldd_task;
1636 u32 slot_idx = (u32)(slot - mvi->slot_info);
1637 mv_dprintk("mvs_abort_task() mvi=%p task=%p "
1638 "slot=%p slot_idx=x%x\n",
1639 mvi, task, slot, slot_idx);
1640 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1641 mvs_slot_task_free(mvi, task, slot, slot_idx);
1642 rc = TMF_RESP_FUNC_COMPLETE;
1648 if (rc != TMF_RESP_FUNC_COMPLETE)
1649 mv_printk("%s:rc= %d\n", __func__, rc);
1653 int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1655 int rc = TMF_RESP_FUNC_FAILED;
1656 struct mvs_tmf_task tmf_task;
1658 tmf_task.tmf = TMF_ABORT_TASK_SET;
1659 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1664 int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1666 int rc = TMF_RESP_FUNC_FAILED;
1667 struct mvs_tmf_task tmf_task;
1669 tmf_task.tmf = TMF_CLEAR_ACA;
1670 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1675 int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1677 int rc = TMF_RESP_FUNC_FAILED;
1678 struct mvs_tmf_task tmf_task;
1680 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1681 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1686 static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1687 u32 slot_idx, int err)
1689 struct mvs_device *mvi_dev = task->dev->lldd_dev;
1690 struct task_status_struct *tstat = &task->task_status;
1691 struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
1692 int stat = SAM_STAT_GOOD;
1695 resp->frame_len = sizeof(struct dev_to_host_fis);
1696 memcpy(&resp->ending_fis[0],
1697 SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
1698 sizeof(struct dev_to_host_fis));
1699 tstat->buf_valid_size = sizeof(*resp);
1700 if (unlikely(err)) {
1701 if (unlikely(err & CMD_ISS_STPD))
1702 stat = SAS_OPEN_REJECT;
1704 stat = SAS_PROTO_RESPONSE;
1710 void mvs_set_sense(u8 *buffer, int len, int d_sense,
1711 int key, int asc, int ascq)
1713 memset(buffer, 0, len);
1716 /* Descriptor format */
1718 mv_printk("Length %d of sense buffer too small to "
1719 "fit sense %x:%x:%x", len, key, asc, ascq);
1722 buffer[0] = 0x72; /* Response Code */
1724 buffer[1] = key; /* Sense Key */
1726 buffer[2] = asc; /* ASC */
1728 buffer[3] = ascq; /* ASCQ */
1731 mv_printk("Length %d of sense buffer too small to "
1732 "fit sense %x:%x:%x", len, key, asc, ascq);
1735 buffer[0] = 0x70; /* Response Code */
1737 buffer[2] = key; /* Sense Key */
1739 buffer[7] = 0x0a; /* Additional Sense Length */
1741 buffer[12] = asc; /* ASC */
1743 buffer[13] = ascq; /* ASCQ */
1749 void mvs_fill_ssp_resp_iu(struct ssp_response_iu *iu,
1750 u8 key, u8 asc, u8 asc_q)
1753 iu->response_data_len = 0;
1754 iu->sense_data_len = 17;
1756 mvs_set_sense(iu->sense_data, 17, 0,
1760 static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1763 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1765 u32 err_dw0 = le32_to_cpu(*(u32 *)slot->response);
1766 u32 err_dw1 = le32_to_cpu(*((u32 *)slot->response + 1));
1768 enum mvs_port_type type = PORT_TYPE_SAS;
1770 if (err_dw0 & CMD_ISS_STPD)
1771 MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1773 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1775 stat = SAM_STAT_CHECK_CONDITION;
1776 switch (task->task_proto) {
1777 case SAS_PROTOCOL_SSP:
1779 stat = SAS_ABORTED_TASK;
1780 if ((err_dw0 & NO_DEST) || err_dw1 & bit(31)) {
1781 struct ssp_response_iu *iu = slot->response +
1782 sizeof(struct mvs_err_info);
1783 mvs_fill_ssp_resp_iu(iu, NOT_READY, 0x04, 01);
1784 sas_ssp_task_response(mvi->dev, task, iu);
1785 stat = SAM_STAT_CHECK_CONDITION;
1787 if (err_dw1 & bit(31))
1788 mv_printk("reuse same slot, retry command.\n");
1791 case SAS_PROTOCOL_SMP:
1792 stat = SAM_STAT_CHECK_CONDITION;
1795 case SAS_PROTOCOL_SATA:
1796 case SAS_PROTOCOL_STP:
1797 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
1799 task->ata_task.use_ncq = 0;
1800 stat = SAS_PROTO_RESPONSE;
1801 mvs_sata_done(mvi, task, slot_idx, err_dw0);
1811 int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
1813 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1814 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1815 struct sas_task *task = slot->task;
1816 struct mvs_device *mvi_dev = NULL;
1817 struct task_status_struct *tstat;
1818 struct domain_device *dev;
1822 enum exec_status sts;
1824 if (unlikely(!task || !task->lldd_task || !task->dev))
1827 tstat = &task->task_status;
1829 mvi_dev = dev->lldd_dev;
1831 spin_lock(&task->task_state_lock);
1832 task->task_state_flags &=
1833 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1834 task->task_state_flags |= SAS_TASK_STATE_DONE;
1836 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
1837 spin_unlock(&task->task_state_lock);
1839 memset(tstat, 0, sizeof(*tstat));
1840 tstat->resp = SAS_TASK_COMPLETE;
1842 if (unlikely(aborted)) {
1843 tstat->stat = SAS_ABORTED_TASK;
1844 if (mvi_dev && mvi_dev->running_req)
1845 mvi_dev->running_req--;
1846 if (sas_protocol_ata(task->task_proto))
1847 mvs_free_reg_set(mvi, mvi_dev);
1849 mvs_slot_task_free(mvi, task, slot, slot_idx);
1853 /* when no device attaching, go ahead and complete by error handling*/
1854 if (unlikely(!mvi_dev || flags)) {
1856 mv_dprintk("port has not device.\n");
1857 tstat->stat = SAS_PHY_DOWN;
1861 /* error info record present */
1862 if (unlikely((rx_desc & RXQ_ERR) && (*(u64 *) slot->response))) {
1863 mv_dprintk("port %d slot %d rx_desc %X has error info"
1864 "%016llX.\n", slot->port->sas_port.id, slot_idx,
1865 rx_desc, (u64)(*(u64 *)slot->response));
1866 tstat->stat = mvs_slot_err(mvi, task, slot_idx);
1867 tstat->resp = SAS_TASK_COMPLETE;
1871 switch (task->task_proto) {
1872 case SAS_PROTOCOL_SSP:
1873 /* hw says status == 0, datapres == 0 */
1874 if (rx_desc & RXQ_GOOD) {
1875 tstat->stat = SAM_STAT_GOOD;
1876 tstat->resp = SAS_TASK_COMPLETE;
1878 /* response frame present */
1879 else if (rx_desc & RXQ_RSP) {
1880 struct ssp_response_iu *iu = slot->response +
1881 sizeof(struct mvs_err_info);
1882 sas_ssp_task_response(mvi->dev, task, iu);
1884 tstat->stat = SAM_STAT_CHECK_CONDITION;
1887 case SAS_PROTOCOL_SMP: {
1888 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
1889 tstat->stat = SAM_STAT_GOOD;
1890 to = kmap_atomic(sg_page(sg_resp), KM_IRQ0);
1891 memcpy(to + sg_resp->offset,
1892 slot->response + sizeof(struct mvs_err_info),
1893 sg_dma_len(sg_resp));
1894 kunmap_atomic(to, KM_IRQ0);
1898 case SAS_PROTOCOL_SATA:
1899 case SAS_PROTOCOL_STP:
1900 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
1901 tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
1906 tstat->stat = SAM_STAT_CHECK_CONDITION;
1909 if (!slot->port->port_attached) {
1910 mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
1911 tstat->stat = SAS_PHY_DOWN;
1916 if (mvi_dev && mvi_dev->running_req) {
1917 mvi_dev->running_req--;
1918 if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
1919 mvs_free_reg_set(mvi, mvi_dev);
1921 mvs_slot_task_free(mvi, task, slot, slot_idx);
1924 spin_unlock(&mvi->lock);
1925 if (task->task_done)
1926 task->task_done(task);
1928 spin_lock(&mvi->lock);
1933 void mvs_do_release_task(struct mvs_info *mvi,
1934 int phy_no, struct domain_device *dev)
1937 struct mvs_phy *phy;
1938 struct mvs_port *port;
1939 struct mvs_slot_info *slot, *slot2;
1941 phy = &mvi->phy[phy_no];
1945 /* clean cmpl queue in case request is already finished */
1946 mvs_int_rx(mvi, false);
1950 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
1951 struct sas_task *task;
1952 slot_idx = (u32) (slot - mvi->slot_info);
1955 if (dev && task->dev != dev)
1958 mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
1959 slot_idx, slot->slot_tag, task);
1960 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1962 mvs_slot_complete(mvi, slot_idx, 1);
1966 void mvs_release_task(struct mvs_info *mvi,
1967 struct domain_device *dev)
1969 int i, phyno[WIDE_PORT_MAX_PHY], num;
1970 num = mvs_find_dev_phyno(dev, phyno);
1971 for (i = 0; i < num; i++)
1972 mvs_do_release_task(mvi, phyno[i], dev);
1975 static void mvs_phy_disconnected(struct mvs_phy *phy)
1977 phy->phy_attached = 0;
1978 phy->att_dev_info = 0;
1979 phy->att_dev_sas_addr = 0;
1982 static void mvs_work_queue(struct work_struct *work)
1984 struct delayed_work *dw = container_of(work, struct delayed_work, work);
1985 struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
1986 struct mvs_info *mvi = mwq->mvi;
1987 unsigned long flags;
1988 u32 phy_no = (unsigned long) mwq->data;
1989 struct sas_ha_struct *sas_ha = mvi->sas;
1990 struct mvs_phy *phy = &mvi->phy[phy_no];
1991 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1993 spin_lock_irqsave(&mvi->lock, flags);
1994 if (mwq->handler & PHY_PLUG_EVENT) {
1996 if (phy->phy_event & PHY_PLUG_OUT) {
1998 struct sas_identify_frame *id;
1999 id = (struct sas_identify_frame *)phy->frame_rcvd;
2000 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
2001 phy->phy_event &= ~PHY_PLUG_OUT;
2002 if (!(tmp & PHY_READY_MASK)) {
2003 sas_phy_disconnected(sas_phy);
2004 mvs_phy_disconnected(phy);
2005 sas_ha->notify_phy_event(sas_phy,
2006 PHYE_LOSS_OF_SIGNAL);
2007 mv_dprintk("phy%d Removed Device\n", phy_no);
2009 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2010 mvs_update_phyinfo(mvi, phy_no, 1);
2011 mvs_bytes_dmaed(mvi, phy_no);
2012 mvs_port_notify_formed(sas_phy, 0);
2013 mv_dprintk("phy%d Attached Device\n", phy_no);
2016 } else if (mwq->handler & EXP_BRCT_CHG) {
2017 phy->phy_event &= ~EXP_BRCT_CHG;
2018 sas_ha->notify_port_event(sas_phy,
2019 PORTE_BROADCAST_RCVD);
2020 mv_dprintk("phy%d Got Broadcast Change\n", phy_no);
2022 list_del(&mwq->entry);
2023 spin_unlock_irqrestore(&mvi->lock, flags);
2027 static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
2032 mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
2036 mwq->handler = handler;
2037 MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
2038 list_add_tail(&mwq->entry, &mvi->wq_list);
2039 schedule_delayed_work(&mwq->work_q, HZ * 2);
2046 static void mvs_sig_time_out(unsigned long tphy)
2048 struct mvs_phy *phy = (struct mvs_phy *)tphy;
2049 struct mvs_info *mvi = phy->mvi;
2052 for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
2053 if (&mvi->phy[phy_no] == phy) {
2054 mv_dprintk("Get signature time out, reset phy %d\n",
2055 phy_no+mvi->id*mvi->chip->n_phy);
2056 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_HARD_RESET);
2061 void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
2064 struct mvs_phy *phy = &mvi->phy[phy_no];
2066 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
2067 MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
2068 mv_dprintk("phy %d ctrl sts=0x%08X.\n", phy_no+mvi->id*mvi->chip->n_phy,
2069 MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
2070 mv_dprintk("phy %d irq sts = 0x%08X\n", phy_no+mvi->id*mvi->chip->n_phy,
2074 * events is port event now ,
2075 * we need check the interrupt status which belongs to per port.
2078 if (phy->irq_status & PHYEV_DCDR_ERR) {
2079 mv_dprintk("phy %d STP decoding error.\n",
2080 phy_no + mvi->id*mvi->chip->n_phy);
2083 if (phy->irq_status & PHYEV_POOF) {
2085 if (!(phy->phy_event & PHY_PLUG_OUT)) {
2086 int dev_sata = phy->phy_type & PORT_TYPE_SATA;
2088 mvs_do_release_task(mvi, phy_no, NULL);
2089 phy->phy_event |= PHY_PLUG_OUT;
2090 MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
2091 mvs_handle_event(mvi,
2092 (void *)(unsigned long)phy_no,
2094 ready = mvs_is_phy_ready(mvi, phy_no);
2095 if (ready || dev_sata) {
2096 if (MVS_CHIP_DISP->stp_reset)
2097 MVS_CHIP_DISP->stp_reset(mvi,
2100 MVS_CHIP_DISP->phy_reset(mvi,
2101 phy_no, MVS_SOFT_RESET);
2107 if (phy->irq_status & PHYEV_COMWAKE) {
2108 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2109 MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2110 tmp | PHYEV_SIG_FIS);
2111 if (phy->timer.function == NULL) {
2112 phy->timer.data = (unsigned long)phy;
2113 phy->timer.function = mvs_sig_time_out;
2114 phy->timer.expires = jiffies + 5*HZ;
2115 add_timer(&phy->timer);
2118 if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2119 phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
2120 mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2121 if (phy->phy_status) {
2123 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2124 if (phy->phy_type & PORT_TYPE_SATA) {
2125 tmp = MVS_CHIP_DISP->read_port_irq_mask(
2127 tmp &= ~PHYEV_SIG_FIS;
2128 MVS_CHIP_DISP->write_port_irq_mask(mvi,
2131 mvs_update_phyinfo(mvi, phy_no, 0);
2132 if (phy->phy_type & PORT_TYPE_SAS) {
2133 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_PHY_TUNE);
2137 mvs_bytes_dmaed(mvi, phy_no);
2138 /* whether driver is going to handle hot plug */
2139 if (phy->phy_event & PHY_PLUG_OUT) {
2140 mvs_port_notify_formed(&phy->sas_phy, 0);
2141 phy->phy_event &= ~PHY_PLUG_OUT;
2144 mv_dprintk("plugin interrupt but phy%d is gone\n",
2145 phy_no + mvi->id*mvi->chip->n_phy);
2147 } else if (phy->irq_status & PHYEV_BROAD_CH) {
2148 mv_dprintk("phy %d broadcast change.\n",
2149 phy_no + mvi->id*mvi->chip->n_phy);
2150 mvs_handle_event(mvi, (void *)(unsigned long)phy_no,
2155 int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
2157 u32 rx_prod_idx, rx_desc;
2160 /* the first dword in the RX ring is special: it contains
2161 * a mirror of the hardware's RX producer index, so that
2162 * we don't have to stall the CPU reading that register.
2163 * The actual RX ring is offset by one dword, due to this.
2165 rx_prod_idx = mvi->rx_cons;
2166 mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2167 if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
2170 /* The CMPL_Q may come late, read from register and try again
2171 * note: if coalescing is enabled,
2172 * it will need to read from register every time for sure
2174 if (unlikely(mvi->rx_cons == rx_prod_idx))
2175 mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
2177 if (mvi->rx_cons == rx_prod_idx)
2180 while (mvi->rx_cons != rx_prod_idx) {
2181 /* increment our internal RX consumer pointer */
2182 rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
2183 rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2185 if (likely(rx_desc & RXQ_DONE))
2186 mvs_slot_complete(mvi, rx_desc, 0);
2187 if (rx_desc & RXQ_ATTN) {
2189 } else if (rx_desc & RXQ_ERR) {
2190 if (!(rx_desc & RXQ_DONE))
2191 mvs_slot_complete(mvi, rx_desc, 0);
2192 } else if (rx_desc & RXQ_SLOT_RESET) {
2193 mvs_slot_free(mvi, rx_desc);
2197 if (attn && self_clear)
2198 MVS_CHIP_DISP->int_full(mvi);