1 // SPDX-License-Identifier: GPL-2.0-only
3 * Aic94xx SAS/SATA driver SCB management.
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
6 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
10 #include <scsi/scsi_host.h>
13 #include "aic94xx_reg.h"
14 #include "aic94xx_hwi.h"
15 #include "aic94xx_seq.h"
17 #include "aic94xx_dump.h"
19 /* ---------- EMPTY SCB ---------- */
23 #define PRIMITIVE_RECVD 0x08
24 #define PHY_EVENT 0x10
25 #define LINK_RESET_ERROR 0x18
26 #define TIMER_EVENT 0x20
27 #define REQ_TASK_ABORT 0xF0
28 #define REQ_DEVICE_RESET 0xF1
29 #define SIGNAL_NCQ_ERROR 0xF2
30 #define CLEAR_NCQ_ERROR 0xF3
32 #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE \
33 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
36 static void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
38 struct sas_phy *sas_phy = phy->sas_phy.phy;
40 switch (oob_mode & 7) {
42 /* FIXME: sas transport class doesn't have this */
43 phy->sas_phy.linkrate = SAS_LINK_RATE_6_0_GBPS;
44 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
47 phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
48 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
51 phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
52 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
55 sas_phy->negotiated_linkrate = phy->sas_phy.linkrate;
56 sas_phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
57 sas_phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
58 sas_phy->maximum_linkrate = phy->phy_desc->max_sas_lrate;
59 sas_phy->minimum_linkrate = phy->phy_desc->min_sas_lrate;
61 if (oob_mode & SAS_MODE)
62 phy->sas_phy.oob_mode = SAS_OOB_MODE;
63 else if (oob_mode & SATA_MODE)
64 phy->sas_phy.oob_mode = SATA_OOB_MODE;
67 static void asd_phy_event_tasklet(struct asd_ascb *ascb,
68 struct done_list_struct *dl)
70 struct asd_ha_struct *asd_ha = ascb->ha;
71 int phy_id = dl->status_block[0] & DL_PHY_MASK;
72 struct asd_phy *phy = &asd_ha->phys[phy_id];
74 u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
75 u8 oob_mode = dl->status_block[2];
78 case CURRENT_LOSS_OF_SIGNAL:
79 /* directly attached device was removed */
80 ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
81 asd_turn_led(asd_ha, phy_id, 0);
82 sas_phy_disconnected(&phy->sas_phy);
83 sas_notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL,
86 case CURRENT_OOB_DONE:
87 /* hot plugged device */
88 asd_turn_led(asd_ha, phy_id, 1);
89 get_lrate_mode(phy, oob_mode);
90 ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
91 phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
92 sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE, GFP_ATOMIC);
94 case CURRENT_SPINUP_HOLD:
95 /* hot plug SATA, no COMWAKE sent */
96 asd_turn_led(asd_ha, phy_id, 1);
97 sas_notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD,
100 case CURRENT_GTO_TIMEOUT:
101 case CURRENT_OOB_ERROR:
102 ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
103 dl->status_block[1]);
104 asd_turn_led(asd_ha, phy_id, 0);
105 sas_phy_disconnected(&phy->sas_phy);
106 sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR, GFP_ATOMIC);
111 /* If phys are enabled sparsely, this will do the right thing. */
112 static unsigned ord_phy(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
114 u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
117 for_each_phy(enabled_mask, enabled_mask, i) {
118 if (&asd_ha->phys[i] == phy)
126 * asd_get_attached_sas_addr -- extract/generate attached SAS address
127 * @phy: pointer to asd_phy
128 * @sas_addr: pointer to buffer where the SAS address is to be written
130 * This function extracts the SAS address from an IDENTIFY frame
131 * received. If OOB is SATA, then a SAS address is generated from the
134 * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
137 static void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
139 if (phy->sas_phy.frame_rcvd[0] == 0x34
140 && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
141 struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
142 /* FIS device-to-host */
143 u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
145 addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
146 *(__be64 *)sas_addr = cpu_to_be64(addr);
148 struct sas_identify_frame *idframe =
149 (void *) phy->sas_phy.frame_rcvd;
150 memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
154 static void asd_form_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
157 struct asd_port *free_port = NULL;
158 struct asd_port *port;
159 struct asd_sas_phy *sas_phy = &phy->sas_phy;
162 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
163 if (!phy->asd_port) {
164 for (i = 0; i < ASD_MAX_PHYS; i++) {
165 port = &asd_ha->asd_ports[i];
167 /* Check for wide port */
168 if (port->num_phys > 0 &&
169 memcmp(port->sas_addr, sas_phy->sas_addr,
170 SAS_ADDR_SIZE) == 0 &&
171 memcmp(port->attached_sas_addr,
172 sas_phy->attached_sas_addr,
173 SAS_ADDR_SIZE) == 0) {
177 /* Find a free port */
178 if (port->num_phys == 0 && free_port == NULL) {
183 /* Use a free port if this doesn't form a wide port */
184 if (i >= ASD_MAX_PHYS) {
187 memcpy(port->sas_addr, sas_phy->sas_addr,
189 memcpy(port->attached_sas_addr,
190 sas_phy->attached_sas_addr,
194 port->phy_mask |= (1U << sas_phy->id);
195 phy->asd_port = port;
197 ASD_DPRINTK("%s: updating phy_mask 0x%x for phy%d\n",
198 __func__, phy->asd_port->phy_mask, sas_phy->id);
199 asd_update_port_links(asd_ha, phy);
200 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
203 static void asd_deform_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
205 struct asd_port *port = phy->asd_port;
206 struct asd_sas_phy *sas_phy = &phy->sas_phy;
209 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
212 port->phy_mask &= ~(1U << sas_phy->id);
213 phy->asd_port = NULL;
215 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
218 static void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
219 struct done_list_struct *dl,
220 int edb_id, int phy_id)
223 int edb_el = edb_id + ascb->edb_index;
224 struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
225 struct asd_phy *phy = &ascb->ha->phys[phy_id];
226 u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
228 size = min(size, (u16) sizeof(phy->frame_rcvd));
230 spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
231 memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
232 phy->sas_phy.frame_rcvd_size = size;
233 asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
234 spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
235 asd_dump_frame_rcvd(phy, dl);
236 asd_form_port(ascb->ha, phy);
237 sas_notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED, GFP_ATOMIC);
240 static void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
241 struct done_list_struct *dl,
244 struct asd_ha_struct *asd_ha = ascb->ha;
245 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
246 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
247 struct asd_phy *phy = &asd_ha->phys[phy_id];
248 u8 lr_error = dl->status_block[1];
249 u8 retries_left = dl->status_block[2];
253 ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
256 ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
259 ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
262 ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
265 ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
270 asd_turn_led(asd_ha, phy_id, 0);
271 sas_phy_disconnected(sas_phy);
272 asd_deform_port(asd_ha, phy);
273 sas_notify_port_event(sas_phy, PORTE_LINK_RESET_ERR, GFP_ATOMIC);
275 if (retries_left == 0) {
277 struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
280 asd_printk("%s: out of memory\n", __func__);
283 ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
285 asd_build_control_phy(cp, phy_id, ENABLE_PHY);
286 if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
293 static void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
294 struct done_list_struct *dl,
298 struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
299 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
300 struct asd_ha_struct *asd_ha = ascb->ha;
301 struct asd_phy *phy = &asd_ha->phys[phy_id];
302 u8 reg = dl->status_block[1];
303 u32 cont = dl->status_block[2] << ((reg & 3)*8);
307 case LmPRMSTAT0BYTE0:
313 ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
315 spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
316 sas_phy->sas_prim = ffs(cont);
317 spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
318 sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD,
323 ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
327 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
332 case LmPRMSTAT1BYTE0:
335 ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
337 /* The sequencer disables all phys on that port.
338 * We have to re-enable the phys ourselves. */
339 asd_deform_port(asd_ha, phy);
340 sas_notify_port_event(sas_phy, PORTE_HARD_RESET,
345 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
351 ASD_DPRINTK("unknown primitive register:0x%x\n",
352 dl->status_block[1]);
358 * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
359 * @ascb: pointer to Empty SCB
360 * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
362 * After an EDB has been invalidated, if all EDBs in this ESCB have been
363 * invalidated, the ESCB is posted back to the sequencer.
364 * Context is tasklet/IRQ.
366 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
368 struct asd_seq_data *seq = &ascb->ha->seq;
369 struct empty_scb *escb = &ascb->scb->escb;
370 struct sg_el *eb = &escb->eb[edb_id];
371 struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
373 memset(edb->vaddr, 0, ASD_EDB_SIZE);
374 eb->flags |= ELEMENT_NOT_VALID;
377 if (escb->num_valid == 0) {
379 /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
380 "dma_handle: 0x%08llx, next: 0x%08llx, "
381 "index:%d, opcode:0x%02x\n",
383 (u64)ascb->dma_scb.dma_handle,
384 le64_to_cpu(ascb->scb->header.next_scb),
385 le16_to_cpu(ascb->scb->header.index),
386 ascb->scb->header.opcode);
388 escb->num_valid = ASD_EDBS_PER_SCB;
389 for (i = 0; i < ASD_EDBS_PER_SCB; i++)
390 escb->eb[i].flags = 0;
391 if (!list_empty(&ascb->list))
392 list_del_init(&ascb->list);
393 i = asd_post_escb_list(ascb->ha, ascb, 1);
395 asd_printk("couldn't post escb, err:%d\n", i);
399 static void escb_tasklet_complete(struct asd_ascb *ascb,
400 struct done_list_struct *dl)
402 struct asd_ha_struct *asd_ha = ascb->ha;
403 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
404 int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
405 u8 sb_opcode = dl->status_block[0];
406 int phy_id = sb_opcode & DL_PHY_MASK;
407 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
408 struct asd_phy *phy = &asd_ha->phys[phy_id];
410 if (edb > 6 || edb < 0) {
411 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
413 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
415 ASD_DPRINTK("escb: vaddr: 0x%p, "
416 "dma_handle: 0x%llx, next: 0x%llx, "
417 "index:%d, opcode:0x%02x\n",
419 (unsigned long long)ascb->dma_scb.dma_handle,
421 le64_to_cpu(ascb->scb->header.next_scb),
422 le16_to_cpu(ascb->scb->header.index),
423 ascb->scb->header.opcode);
426 /* Catch these before we mask off the sb_opcode bits */
428 case REQ_TASK_ABORT: {
429 struct asd_ascb *a, *b;
431 struct domain_device *failed_dev = NULL;
433 ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
434 __func__, dl->status_block[3]);
437 * Find the task that caused the abort and abort it first.
438 * The sequencer won't put anything on the done list until
441 tc_abort = *((u16*)(&dl->status_block[1]));
442 tc_abort = le16_to_cpu(tc_abort);
444 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
445 struct sas_task *task = a->uldd_task;
447 if (a->tc_index != tc_abort)
451 failed_dev = task->dev;
452 sas_task_abort(task);
454 ASD_DPRINTK("R_T_A for non TASK scb 0x%x\n",
455 a->scb->header.opcode);
461 ASD_DPRINTK("%s: Can't find task (tc=%d) to abort!\n",
467 * Now abort everything else for that device (hba?) so
468 * that the EH will wake up and do something.
470 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
471 struct sas_task *task = a->uldd_task;
474 task->dev == failed_dev &&
475 a->tc_index != tc_abort)
476 sas_task_abort(task);
481 case REQ_DEVICE_RESET: {
485 struct sas_task *last_dev_task = NULL;
487 conn_handle = *((u16*)(&dl->status_block[1]));
488 conn_handle = le16_to_cpu(conn_handle);
490 ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __func__,
491 dl->status_block[3]);
493 /* Find the last pending task for the device... */
494 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
496 struct domain_device *dev;
497 struct sas_task *task = a->uldd_task;
503 x = (unsigned long)dev->lldd_dev;
504 if (x == conn_handle)
505 last_dev_task = task;
508 if (!last_dev_task) {
509 ASD_DPRINTK("%s: Device reset for idle device %d?\n",
510 __func__, conn_handle);
514 /* ...and set the reset flag */
515 spin_lock_irqsave(&last_dev_task->task_state_lock, flags);
516 last_dev_task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
517 spin_unlock_irqrestore(&last_dev_task->task_state_lock, flags);
519 /* Kill all pending tasks for the device */
520 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
522 struct domain_device *dev;
523 struct sas_task *task = a->uldd_task;
529 x = (unsigned long)dev->lldd_dev;
530 if (x == conn_handle)
531 sas_task_abort(task);
536 case SIGNAL_NCQ_ERROR:
537 ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __func__);
539 case CLEAR_NCQ_ERROR:
540 ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __func__);
544 sb_opcode &= ~DL_PHY_MASK;
548 ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __func__, phy_id);
549 asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
551 case PRIMITIVE_RECVD:
552 ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __func__,
554 asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
557 ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __func__, phy_id);
558 asd_phy_event_tasklet(ascb, dl);
560 case LINK_RESET_ERROR:
561 ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __func__,
563 asd_link_reset_err_tasklet(ascb, dl, phy_id);
566 ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
568 asd_turn_led(asd_ha, phy_id, 0);
569 /* the device is gone */
570 sas_phy_disconnected(sas_phy);
571 asd_deform_port(asd_ha, phy);
572 sas_notify_port_event(sas_phy, PORTE_TIMER_EVENT, GFP_ATOMIC);
575 ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __func__,
577 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
579 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
581 ASD_DPRINTK("escb: vaddr: 0x%p, "
582 "dma_handle: 0x%llx, next: 0x%llx, "
583 "index:%d, opcode:0x%02x\n",
585 (unsigned long long)ascb->dma_scb.dma_handle,
587 le64_to_cpu(ascb->scb->header.next_scb),
588 le16_to_cpu(ascb->scb->header.index),
589 ascb->scb->header.opcode);
594 asd_invalidate_edb(ascb, edb);
597 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
599 struct asd_seq_data *seq = &asd_ha->seq;
602 for (i = 0; i < seq->num_escbs; i++)
603 seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
605 ASD_DPRINTK("posting %d escbs\n", i);
606 return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
609 /* ---------- CONTROL PHY ---------- */
611 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE \
612 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
616 * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
617 * @ascb: pointer to an ascb
618 * @dl: pointer to the done list entry
620 * This function completes a CONTROL PHY scb and frees the ascb.
622 * - an LED blinks if there is IO though it,
623 * - if a device is connected to the LED, it is lit,
624 * - if no device is connected to the LED, is is dimmed (off).
626 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
627 struct done_list_struct *dl)
629 struct asd_ha_struct *asd_ha = ascb->ha;
630 struct scb *scb = ascb->scb;
631 struct control_phy *control_phy = &scb->control_phy;
632 u8 phy_id = control_phy->phy_id;
633 struct asd_phy *phy = &ascb->ha->phys[phy_id];
635 u8 status = dl->status_block[0];
636 u8 oob_status = dl->status_block[1];
637 u8 oob_mode = dl->status_block[2];
638 /* u8 oob_signals= dl->status_block[3]; */
641 ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
642 __func__, phy_id, status);
646 switch (control_phy->sub_func) {
648 asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
649 asd_turn_led(asd_ha, phy_id, 0);
650 asd_control_led(asd_ha, phy_id, 0);
651 ASD_DPRINTK("%s: disable phy%d\n", __func__, phy_id);
655 asd_control_led(asd_ha, phy_id, 1);
656 if (oob_status & CURRENT_OOB_DONE) {
657 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
658 get_lrate_mode(phy, oob_mode);
659 asd_turn_led(asd_ha, phy_id, 1);
660 ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
661 __func__, phy_id,phy->sas_phy.linkrate,
662 phy->sas_phy.iproto);
663 } else if (oob_status & CURRENT_SPINUP_HOLD) {
664 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
665 asd_turn_led(asd_ha, phy_id, 1);
666 ASD_DPRINTK("%s: phy%d, spinup hold\n", __func__,
668 } else if (oob_status & CURRENT_ERR_MASK) {
669 asd_turn_led(asd_ha, phy_id, 0);
670 ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
671 __func__, phy_id, oob_status);
672 } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
673 | CURRENT_DEVICE_PRESENT)) {
674 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
675 asd_turn_led(asd_ha, phy_id, 1);
676 ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
679 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
680 asd_turn_led(asd_ha, phy_id, 0);
681 ASD_DPRINTK("%s: phy%d: no device present: "
683 __func__, phy_id, oob_status);
686 case RELEASE_SPINUP_HOLD:
688 case EXECUTE_HARD_RESET:
689 ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __func__,
690 phy_id, control_phy->sub_func);
694 ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __func__,
695 phy_id, control_phy->sub_func);
702 static void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
704 /* disable all speeds, then enable defaults */
705 *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
706 | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
708 switch (pd->max_sas_lrate) {
709 case SAS_LINK_RATE_6_0_GBPS:
710 *speed_mask &= ~SAS_SPEED_60_DIS;
713 case SAS_LINK_RATE_3_0_GBPS:
714 *speed_mask &= ~SAS_SPEED_30_DIS;
716 case SAS_LINK_RATE_1_5_GBPS:
717 *speed_mask &= ~SAS_SPEED_15_DIS;
720 switch (pd->min_sas_lrate) {
721 case SAS_LINK_RATE_6_0_GBPS:
722 *speed_mask |= SAS_SPEED_30_DIS;
724 case SAS_LINK_RATE_3_0_GBPS:
725 *speed_mask |= SAS_SPEED_15_DIS;
728 case SAS_LINK_RATE_1_5_GBPS:
733 switch (pd->max_sata_lrate) {
734 case SAS_LINK_RATE_3_0_GBPS:
735 *speed_mask &= ~SATA_SPEED_30_DIS;
738 case SAS_LINK_RATE_1_5_GBPS:
739 *speed_mask &= ~SATA_SPEED_15_DIS;
742 switch (pd->min_sata_lrate) {
743 case SAS_LINK_RATE_3_0_GBPS:
744 *speed_mask |= SATA_SPEED_15_DIS;
747 case SAS_LINK_RATE_1_5_GBPS:
754 * asd_build_control_phy -- build a CONTROL PHY SCB
755 * @ascb: pointer to an ascb
756 * @phy_id: phy id to control, integer
757 * @subfunc: subfunction, what to actually to do the phy
759 * This function builds a CONTROL PHY scb. No allocation of any kind
760 * is performed. @ascb is allocated with the list function.
761 * The caller can override the ascb->tasklet_complete to point
762 * to its own callback function. It must call asd_ascb_free()
763 * at its tasklet complete function.
764 * See the default implementation.
766 void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
768 struct asd_phy *phy = &ascb->ha->phys[phy_id];
769 struct scb *scb = ascb->scb;
770 struct control_phy *control_phy = &scb->control_phy;
772 scb->header.opcode = CONTROL_PHY;
773 control_phy->phy_id = (u8) phy_id;
774 control_phy->sub_func = subfunc;
777 case EXECUTE_HARD_RESET: /* 0x81 */
778 case ENABLE_PHY: /* 0x01 */
779 /* decide hot plug delay */
780 control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
782 /* decide speed mask */
783 set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
785 /* initiator port settings are in the hi nibble */
786 if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
787 control_phy->port_type = SAS_PROTOCOL_ALL << 4;
788 else if (phy->sas_phy.role == PHY_ROLE_TARGET)
789 control_phy->port_type = SAS_PROTOCOL_ALL;
791 control_phy->port_type =
792 (SAS_PROTOCOL_ALL << 4) | SAS_PROTOCOL_ALL;
794 /* link reset retries, this should be nominal */
795 control_phy->link_reset_retries = 10;
798 case RELEASE_SPINUP_HOLD: /* 0x02 */
799 /* decide the func_mask */
800 control_phy->func_mask = FUNCTION_MASK_DEFAULT;
801 if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
802 control_phy->func_mask &= ~SPINUP_HOLD_DIS;
804 control_phy->func_mask |= SPINUP_HOLD_DIS;
807 control_phy->conn_handle = cpu_to_le16(0xFFFF);
809 ascb->tasklet_complete = control_phy_tasklet_complete;
812 /* ---------- INITIATE LINK ADM TASK ---------- */
816 static void link_adm_tasklet_complete(struct asd_ascb *ascb,
817 struct done_list_struct *dl)
819 u8 opcode = dl->opcode;
820 struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
821 u8 phy_id = link_adm->phy_id;
823 if (opcode != TC_NO_ERROR) {
824 asd_printk("phy%d: link adm task 0x%x completed with error "
825 "0x%x\n", phy_id, link_adm->sub_func, opcode);
827 ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
828 phy_id, link_adm->sub_func, opcode);
833 void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
836 struct scb *scb = ascb->scb;
837 struct initiate_link_adm *link_adm = &scb->link_adm;
839 scb->header.opcode = INITIATE_LINK_ADM_TASK;
841 link_adm->phy_id = phy_id;
842 link_adm->sub_func = subfunc;
843 link_adm->conn_handle = cpu_to_le16(0xFFFF);
845 ascb->tasklet_complete = link_adm_tasklet_complete;
850 /* ---------- SCB timer ---------- */
853 * asd_ascb_timedout -- called when a pending SCB's timer has expired
854 * @t: Timer context used to fetch the SCB
856 * This is the default timeout function which does the most necessary.
857 * Upper layers can implement their own timeout function, say to free
858 * resources they have with this SCB, and then call this one at the
859 * end of their timeout function. To do this, one should initialize
860 * the ascb->timer.{function, expires} prior to calling the post
861 * function. The timer is started by the post function.
863 void asd_ascb_timedout(struct timer_list *t)
865 struct asd_ascb *ascb = from_timer(ascb, t, timer);
866 struct asd_seq_data *seq = &ascb->ha->seq;
869 ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
871 spin_lock_irqsave(&seq->pend_q_lock, flags);
873 list_del_init(&ascb->list);
874 spin_unlock_irqrestore(&seq->pend_q_lock, flags);
879 /* ---------- CONTROL PHY ---------- */
881 /* Given the spec value, return a driver value. */
882 static const int phy_func_table[] = {
883 [PHY_FUNC_NOP] = PHY_NO_OP,
884 [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
885 [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
886 [PHY_FUNC_DISABLE] = DISABLE_PHY,
887 [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
890 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
892 struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
893 struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
894 struct asd_ascb *ascb;
895 struct sas_phy_linkrates *rates;
899 case PHY_FUNC_CLEAR_ERROR_LOG:
900 case PHY_FUNC_GET_EVENTS:
902 case PHY_FUNC_SET_LINK_RATE:
904 if (rates->minimum_linkrate) {
905 pd->min_sas_lrate = rates->minimum_linkrate;
906 pd->min_sata_lrate = rates->minimum_linkrate;
908 if (rates->maximum_linkrate) {
909 pd->max_sas_lrate = rates->maximum_linkrate;
910 pd->max_sata_lrate = rates->maximum_linkrate;
912 func = PHY_FUNC_LINK_RESET;
918 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
922 asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
923 res = asd_post_ascb_list(asd_ha, ascb , 1);