1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Aic94xx SAS/SATA driver SCB management.
4 *
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
6 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 */
8
9 #include <linux/gfp.h>
10 #include <scsi/scsi_host.h>
11
12 #include "aic94xx.h"
13 #include "aic94xx_reg.h"
14 #include "aic94xx_hwi.h"
15 #include "aic94xx_seq.h"
16
17 #include "aic94xx_dump.h"
18
19 /* ---------- EMPTY SCB ---------- */
20
21 #define DL_PHY_MASK 7
22 #define BYTES_DMAED 0
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
31
32 #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE \
33 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
34 | CURRENT_OOB_ERROR)
35
get_lrate_mode(struct asd_phy * phy,u8 oob_mode)36 static void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
37 {
38 struct sas_phy *sas_phy = phy->sas_phy.phy;
39
40 switch (oob_mode & 7) {
41 case PHY_SPEED_60:
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;
45 break;
46 case PHY_SPEED_30:
47 phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
48 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
49 break;
50 case PHY_SPEED_15:
51 phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
52 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
53 break;
54 }
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;
60
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;
65 }
66
asd_phy_event_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl)67 static void asd_phy_event_tasklet(struct asd_ascb *ascb,
68 struct done_list_struct *dl)
69 {
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];
73
74 u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
75 u8 oob_mode = dl->status_block[2];
76
77 switch (oob_status) {
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);
84 break;
85 case CURRENT_OOB_DONE:
86 /* hot plugged device */
87 asd_turn_led(asd_ha, phy_id, 1);
88 get_lrate_mode(phy, oob_mode);
89 ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
90 phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
91 sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE);
92 break;
93 case CURRENT_SPINUP_HOLD:
94 /* hot plug SATA, no COMWAKE sent */
95 asd_turn_led(asd_ha, phy_id, 1);
96 sas_notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD);
97 break;
98 case CURRENT_GTO_TIMEOUT:
99 case CURRENT_OOB_ERROR:
100 ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
101 dl->status_block[1]);
102 asd_turn_led(asd_ha, phy_id, 0);
103 sas_phy_disconnected(&phy->sas_phy);
104 sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR);
105 break;
106 }
107 }
108
109 /* If phys are enabled sparsely, this will do the right thing. */
ord_phy(struct asd_ha_struct * asd_ha,struct asd_phy * phy)110 static unsigned ord_phy(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
111 {
112 u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
113 int i, k = 0;
114
115 for_each_phy(enabled_mask, enabled_mask, i) {
116 if (&asd_ha->phys[i] == phy)
117 return k;
118 k++;
119 }
120 return 0;
121 }
122
123 /**
124 * asd_get_attached_sas_addr -- extract/generate attached SAS address
125 * @phy: pointer to asd_phy
126 * @sas_addr: pointer to buffer where the SAS address is to be written
127 *
128 * This function extracts the SAS address from an IDENTIFY frame
129 * received. If OOB is SATA, then a SAS address is generated from the
130 * HA tables.
131 *
132 * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
133 * buffer.
134 */
asd_get_attached_sas_addr(struct asd_phy * phy,u8 * sas_addr)135 static void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
136 {
137 if (phy->sas_phy.frame_rcvd[0] == 0x34
138 && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
139 struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
140 /* FIS device-to-host */
141 u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
142
143 addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
144 *(__be64 *)sas_addr = cpu_to_be64(addr);
145 } else {
146 struct sas_identify_frame *idframe =
147 (void *) phy->sas_phy.frame_rcvd;
148 memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
149 }
150 }
151
asd_form_port(struct asd_ha_struct * asd_ha,struct asd_phy * phy)152 static void asd_form_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
153 {
154 int i;
155 struct asd_port *free_port = NULL;
156 struct asd_port *port;
157 struct asd_sas_phy *sas_phy = &phy->sas_phy;
158 unsigned long flags;
159
160 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
161 if (!phy->asd_port) {
162 for (i = 0; i < ASD_MAX_PHYS; i++) {
163 port = &asd_ha->asd_ports[i];
164
165 /* Check for wide port */
166 if (port->num_phys > 0 &&
167 memcmp(port->sas_addr, sas_phy->sas_addr,
168 SAS_ADDR_SIZE) == 0 &&
169 memcmp(port->attached_sas_addr,
170 sas_phy->attached_sas_addr,
171 SAS_ADDR_SIZE) == 0) {
172 break;
173 }
174
175 /* Find a free port */
176 if (port->num_phys == 0 && free_port == NULL) {
177 free_port = port;
178 }
179 }
180
181 /* Use a free port if this doesn't form a wide port */
182 if (i >= ASD_MAX_PHYS) {
183 port = free_port;
184 BUG_ON(!port);
185 memcpy(port->sas_addr, sas_phy->sas_addr,
186 SAS_ADDR_SIZE);
187 memcpy(port->attached_sas_addr,
188 sas_phy->attached_sas_addr,
189 SAS_ADDR_SIZE);
190 }
191 port->num_phys++;
192 port->phy_mask |= (1U << sas_phy->id);
193 phy->asd_port = port;
194 }
195 ASD_DPRINTK("%s: updating phy_mask 0x%x for phy%d\n",
196 __func__, phy->asd_port->phy_mask, sas_phy->id);
197 asd_update_port_links(asd_ha, phy);
198 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
199 }
200
asd_deform_port(struct asd_ha_struct * asd_ha,struct asd_phy * phy)201 static void asd_deform_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
202 {
203 struct asd_port *port = phy->asd_port;
204 struct asd_sas_phy *sas_phy = &phy->sas_phy;
205 unsigned long flags;
206
207 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
208 if (port) {
209 port->num_phys--;
210 port->phy_mask &= ~(1U << sas_phy->id);
211 phy->asd_port = NULL;
212 }
213 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
214 }
215
asd_bytes_dmaed_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl,int edb_id,int phy_id)216 static void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
217 struct done_list_struct *dl,
218 int edb_id, int phy_id)
219 {
220 unsigned long flags;
221 int edb_el = edb_id + ascb->edb_index;
222 struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
223 struct asd_phy *phy = &ascb->ha->phys[phy_id];
224 u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
225
226 size = min(size, (u16) sizeof(phy->frame_rcvd));
227
228 spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
229 memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
230 phy->sas_phy.frame_rcvd_size = size;
231 asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
232 spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
233 asd_dump_frame_rcvd(phy, dl);
234 asd_form_port(ascb->ha, phy);
235 sas_notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
236 }
237
asd_link_reset_err_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl,int phy_id)238 static void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
239 struct done_list_struct *dl,
240 int phy_id)
241 {
242 struct asd_ha_struct *asd_ha = ascb->ha;
243 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
244 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
245 struct asd_phy *phy = &asd_ha->phys[phy_id];
246 u8 lr_error = dl->status_block[1];
247 u8 retries_left = dl->status_block[2];
248
249 switch (lr_error) {
250 case 0:
251 ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
252 break;
253 case 1:
254 ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
255 break;
256 case 2:
257 ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
258 break;
259 case 3:
260 ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
261 break;
262 default:
263 ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
264 phy_id, lr_error);
265 break;
266 }
267
268 asd_turn_led(asd_ha, phy_id, 0);
269 sas_phy_disconnected(sas_phy);
270 asd_deform_port(asd_ha, phy);
271 sas_notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
272
273 if (retries_left == 0) {
274 int num = 1;
275 struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
276 GFP_ATOMIC);
277 if (!cp) {
278 asd_printk("%s: out of memory\n", __func__);
279 goto out;
280 }
281 ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
282 phy_id);
283 asd_build_control_phy(cp, phy_id, ENABLE_PHY);
284 if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
285 asd_ascb_free(cp);
286 }
287 out:
288 ;
289 }
290
asd_primitive_rcvd_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl,int phy_id)291 static void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
292 struct done_list_struct *dl,
293 int phy_id)
294 {
295 unsigned long flags;
296 struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
297 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
298 struct asd_ha_struct *asd_ha = ascb->ha;
299 struct asd_phy *phy = &asd_ha->phys[phy_id];
300 u8 reg = dl->status_block[1];
301 u32 cont = dl->status_block[2] << ((reg & 3)*8);
302
303 reg &= ~3;
304 switch (reg) {
305 case LmPRMSTAT0BYTE0:
306 switch (cont) {
307 case LmBROADCH:
308 case LmBROADRVCH0:
309 case LmBROADRVCH1:
310 case LmBROADSES:
311 ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
312 phy_id, cont);
313 spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
314 sas_phy->sas_prim = ffs(cont);
315 spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
316 sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
317 break;
318
319 case LmUNKNOWNP:
320 ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
321 break;
322
323 default:
324 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
325 phy_id, reg, cont);
326 break;
327 }
328 break;
329 case LmPRMSTAT1BYTE0:
330 switch (cont) {
331 case LmHARDRST:
332 ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
333 phy_id);
334 /* The sequencer disables all phys on that port.
335 * We have to re-enable the phys ourselves. */
336 asd_deform_port(asd_ha, phy);
337 sas_notify_port_event(sas_phy, PORTE_HARD_RESET);
338 break;
339
340 default:
341 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
342 phy_id, reg, cont);
343 break;
344 }
345 break;
346 default:
347 ASD_DPRINTK("unknown primitive register:0x%x\n",
348 dl->status_block[1]);
349 break;
350 }
351 }
352
353 /**
354 * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
355 * @ascb: pointer to Empty SCB
356 * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
357 *
358 * After an EDB has been invalidated, if all EDBs in this ESCB have been
359 * invalidated, the ESCB is posted back to the sequencer.
360 * Context is tasklet/IRQ.
361 */
asd_invalidate_edb(struct asd_ascb * ascb,int edb_id)362 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
363 {
364 struct asd_seq_data *seq = &ascb->ha->seq;
365 struct empty_scb *escb = &ascb->scb->escb;
366 struct sg_el *eb = &escb->eb[edb_id];
367 struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
368
369 memset(edb->vaddr, 0, ASD_EDB_SIZE);
370 eb->flags |= ELEMENT_NOT_VALID;
371 escb->num_valid--;
372
373 if (escb->num_valid == 0) {
374 int i;
375 /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
376 "dma_handle: 0x%08llx, next: 0x%08llx, "
377 "index:%d, opcode:0x%02x\n",
378 ascb->dma_scb.vaddr,
379 (u64)ascb->dma_scb.dma_handle,
380 le64_to_cpu(ascb->scb->header.next_scb),
381 le16_to_cpu(ascb->scb->header.index),
382 ascb->scb->header.opcode);
383 */
384 escb->num_valid = ASD_EDBS_PER_SCB;
385 for (i = 0; i < ASD_EDBS_PER_SCB; i++)
386 escb->eb[i].flags = 0;
387 if (!list_empty(&ascb->list))
388 list_del_init(&ascb->list);
389 i = asd_post_escb_list(ascb->ha, ascb, 1);
390 if (i)
391 asd_printk("couldn't post escb, err:%d\n", i);
392 }
393 }
394
escb_tasklet_complete(struct asd_ascb * ascb,struct done_list_struct * dl)395 static void escb_tasklet_complete(struct asd_ascb *ascb,
396 struct done_list_struct *dl)
397 {
398 struct asd_ha_struct *asd_ha = ascb->ha;
399 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
400 int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
401 u8 sb_opcode = dl->status_block[0];
402 int phy_id = sb_opcode & DL_PHY_MASK;
403 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
404 struct asd_phy *phy = &asd_ha->phys[phy_id];
405
406 if (edb > 6 || edb < 0) {
407 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
408 edb, dl->opcode);
409 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
410 sb_opcode, phy_id);
411 ASD_DPRINTK("escb: vaddr: 0x%p, "
412 "dma_handle: 0x%llx, next: 0x%llx, "
413 "index:%d, opcode:0x%02x\n",
414 ascb->dma_scb.vaddr,
415 (unsigned long long)ascb->dma_scb.dma_handle,
416 (unsigned long long)
417 le64_to_cpu(ascb->scb->header.next_scb),
418 le16_to_cpu(ascb->scb->header.index),
419 ascb->scb->header.opcode);
420 }
421
422 /* Catch these before we mask off the sb_opcode bits */
423 switch (sb_opcode) {
424 case REQ_TASK_ABORT: {
425 struct asd_ascb *a, *b;
426 u16 tc_abort;
427 struct domain_device *failed_dev = NULL;
428
429 ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
430 __func__, dl->status_block[3]);
431
432 /*
433 * Find the task that caused the abort and abort it first.
434 * The sequencer won't put anything on the done list until
435 * that happens.
436 */
437 tc_abort = *((u16*)(&dl->status_block[1]));
438 tc_abort = le16_to_cpu(tc_abort);
439
440 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
441 struct sas_task *task = a->uldd_task;
442
443 if (a->tc_index != tc_abort)
444 continue;
445
446 if (task) {
447 failed_dev = task->dev;
448 sas_task_abort(task);
449 } else {
450 ASD_DPRINTK("R_T_A for non TASK scb 0x%x\n",
451 a->scb->header.opcode);
452 }
453 break;
454 }
455
456 if (!failed_dev) {
457 ASD_DPRINTK("%s: Can't find task (tc=%d) to abort!\n",
458 __func__, tc_abort);
459 goto out;
460 }
461
462 /*
463 * Now abort everything else for that device (hba?) so
464 * that the EH will wake up and do something.
465 */
466 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
467 struct sas_task *task = a->uldd_task;
468
469 if (task &&
470 task->dev == failed_dev &&
471 a->tc_index != tc_abort)
472 sas_task_abort(task);
473 }
474
475 goto out;
476 }
477 case REQ_DEVICE_RESET: {
478 struct asd_ascb *a;
479 u16 conn_handle;
480 unsigned long flags;
481 struct sas_task *last_dev_task = NULL;
482
483 conn_handle = *((u16*)(&dl->status_block[1]));
484 conn_handle = le16_to_cpu(conn_handle);
485
486 ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __func__,
487 dl->status_block[3]);
488
489 /* Find the last pending task for the device... */
490 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
491 u16 x;
492 struct domain_device *dev;
493 struct sas_task *task = a->uldd_task;
494
495 if (!task)
496 continue;
497 dev = task->dev;
498
499 x = (unsigned long)dev->lldd_dev;
500 if (x == conn_handle)
501 last_dev_task = task;
502 }
503
504 if (!last_dev_task) {
505 ASD_DPRINTK("%s: Device reset for idle device %d?\n",
506 __func__, conn_handle);
507 goto out;
508 }
509
510 /* ...and set the reset flag */
511 spin_lock_irqsave(&last_dev_task->task_state_lock, flags);
512 last_dev_task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
513 spin_unlock_irqrestore(&last_dev_task->task_state_lock, flags);
514
515 /* Kill all pending tasks for the device */
516 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
517 u16 x;
518 struct domain_device *dev;
519 struct sas_task *task = a->uldd_task;
520
521 if (!task)
522 continue;
523 dev = task->dev;
524
525 x = (unsigned long)dev->lldd_dev;
526 if (x == conn_handle)
527 sas_task_abort(task);
528 }
529
530 goto out;
531 }
532 case SIGNAL_NCQ_ERROR:
533 ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __func__);
534 goto out;
535 case CLEAR_NCQ_ERROR:
536 ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __func__);
537 goto out;
538 }
539
540 sb_opcode &= ~DL_PHY_MASK;
541
542 switch (sb_opcode) {
543 case BYTES_DMAED:
544 ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __func__, phy_id);
545 asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
546 break;
547 case PRIMITIVE_RECVD:
548 ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __func__,
549 phy_id);
550 asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
551 break;
552 case PHY_EVENT:
553 ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __func__, phy_id);
554 asd_phy_event_tasklet(ascb, dl);
555 break;
556 case LINK_RESET_ERROR:
557 ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __func__,
558 phy_id);
559 asd_link_reset_err_tasklet(ascb, dl, phy_id);
560 break;
561 case TIMER_EVENT:
562 ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
563 __func__, phy_id);
564 asd_turn_led(asd_ha, phy_id, 0);
565 /* the device is gone */
566 sas_phy_disconnected(sas_phy);
567 asd_deform_port(asd_ha, phy);
568 sas_notify_port_event(sas_phy, PORTE_TIMER_EVENT);
569 break;
570 default:
571 ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __func__,
572 phy_id, sb_opcode);
573 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
574 edb, dl->opcode);
575 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
576 sb_opcode, phy_id);
577 ASD_DPRINTK("escb: vaddr: 0x%p, "
578 "dma_handle: 0x%llx, next: 0x%llx, "
579 "index:%d, opcode:0x%02x\n",
580 ascb->dma_scb.vaddr,
581 (unsigned long long)ascb->dma_scb.dma_handle,
582 (unsigned long long)
583 le64_to_cpu(ascb->scb->header.next_scb),
584 le16_to_cpu(ascb->scb->header.index),
585 ascb->scb->header.opcode);
586
587 break;
588 }
589 out:
590 asd_invalidate_edb(ascb, edb);
591 }
592
asd_init_post_escbs(struct asd_ha_struct * asd_ha)593 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
594 {
595 struct asd_seq_data *seq = &asd_ha->seq;
596 int i;
597
598 for (i = 0; i < seq->num_escbs; i++)
599 seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
600
601 ASD_DPRINTK("posting %d escbs\n", i);
602 return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
603 }
604
605 /* ---------- CONTROL PHY ---------- */
606
607 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE \
608 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
609 | CURRENT_OOB_ERROR)
610
611 /**
612 * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
613 * @ascb: pointer to an ascb
614 * @dl: pointer to the done list entry
615 *
616 * This function completes a CONTROL PHY scb and frees the ascb.
617 * A note on LEDs:
618 * - an LED blinks if there is IO though it,
619 * - if a device is connected to the LED, it is lit,
620 * - if no device is connected to the LED, is is dimmed (off).
621 */
control_phy_tasklet_complete(struct asd_ascb * ascb,struct done_list_struct * dl)622 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
623 struct done_list_struct *dl)
624 {
625 struct asd_ha_struct *asd_ha = ascb->ha;
626 struct scb *scb = ascb->scb;
627 struct control_phy *control_phy = &scb->control_phy;
628 u8 phy_id = control_phy->phy_id;
629 struct asd_phy *phy = &ascb->ha->phys[phy_id];
630
631 u8 status = dl->status_block[0];
632 u8 oob_status = dl->status_block[1];
633 u8 oob_mode = dl->status_block[2];
634 /* u8 oob_signals= dl->status_block[3]; */
635
636 if (status != 0) {
637 ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
638 __func__, phy_id, status);
639 goto out;
640 }
641
642 switch (control_phy->sub_func) {
643 case DISABLE_PHY:
644 asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
645 asd_turn_led(asd_ha, phy_id, 0);
646 asd_control_led(asd_ha, phy_id, 0);
647 ASD_DPRINTK("%s: disable phy%d\n", __func__, phy_id);
648 break;
649
650 case ENABLE_PHY:
651 asd_control_led(asd_ha, phy_id, 1);
652 if (oob_status & CURRENT_OOB_DONE) {
653 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
654 get_lrate_mode(phy, oob_mode);
655 asd_turn_led(asd_ha, phy_id, 1);
656 ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
657 __func__, phy_id,phy->sas_phy.linkrate,
658 phy->sas_phy.iproto);
659 } else if (oob_status & CURRENT_SPINUP_HOLD) {
660 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
661 asd_turn_led(asd_ha, phy_id, 1);
662 ASD_DPRINTK("%s: phy%d, spinup hold\n", __func__,
663 phy_id);
664 } else if (oob_status & CURRENT_ERR_MASK) {
665 asd_turn_led(asd_ha, phy_id, 0);
666 ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
667 __func__, phy_id, oob_status);
668 } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
669 | CURRENT_DEVICE_PRESENT)) {
670 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
671 asd_turn_led(asd_ha, phy_id, 1);
672 ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
673 __func__, phy_id);
674 } else {
675 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
676 asd_turn_led(asd_ha, phy_id, 0);
677 ASD_DPRINTK("%s: phy%d: no device present: "
678 "oob_status:0x%x\n",
679 __func__, phy_id, oob_status);
680 }
681 break;
682 case RELEASE_SPINUP_HOLD:
683 case PHY_NO_OP:
684 case EXECUTE_HARD_RESET:
685 ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __func__,
686 phy_id, control_phy->sub_func);
687 /* XXX finish */
688 break;
689 default:
690 ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __func__,
691 phy_id, control_phy->sub_func);
692 break;
693 }
694 out:
695 asd_ascb_free(ascb);
696 }
697
set_speed_mask(u8 * speed_mask,struct asd_phy_desc * pd)698 static void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
699 {
700 /* disable all speeds, then enable defaults */
701 *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
702 | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
703
704 switch (pd->max_sas_lrate) {
705 case SAS_LINK_RATE_6_0_GBPS:
706 *speed_mask &= ~SAS_SPEED_60_DIS;
707 fallthrough;
708 default:
709 case SAS_LINK_RATE_3_0_GBPS:
710 *speed_mask &= ~SAS_SPEED_30_DIS;
711 fallthrough;
712 case SAS_LINK_RATE_1_5_GBPS:
713 *speed_mask &= ~SAS_SPEED_15_DIS;
714 }
715
716 switch (pd->min_sas_lrate) {
717 case SAS_LINK_RATE_6_0_GBPS:
718 *speed_mask |= SAS_SPEED_30_DIS;
719 fallthrough;
720 case SAS_LINK_RATE_3_0_GBPS:
721 *speed_mask |= SAS_SPEED_15_DIS;
722 default:
723 case SAS_LINK_RATE_1_5_GBPS:
724 /* nothing to do */
725 ;
726 }
727
728 switch (pd->max_sata_lrate) {
729 case SAS_LINK_RATE_3_0_GBPS:
730 *speed_mask &= ~SATA_SPEED_30_DIS;
731 fallthrough;
732 default:
733 case SAS_LINK_RATE_1_5_GBPS:
734 *speed_mask &= ~SATA_SPEED_15_DIS;
735 }
736
737 switch (pd->min_sata_lrate) {
738 case SAS_LINK_RATE_3_0_GBPS:
739 *speed_mask |= SATA_SPEED_15_DIS;
740 default:
741 case SAS_LINK_RATE_1_5_GBPS:
742 /* nothing to do */
743 ;
744 }
745 }
746
747 /**
748 * asd_build_control_phy -- build a CONTROL PHY SCB
749 * @ascb: pointer to an ascb
750 * @phy_id: phy id to control, integer
751 * @subfunc: subfunction, what to actually to do the phy
752 *
753 * This function builds a CONTROL PHY scb. No allocation of any kind
754 * is performed. @ascb is allocated with the list function.
755 * The caller can override the ascb->tasklet_complete to point
756 * to its own callback function. It must call asd_ascb_free()
757 * at its tasklet complete function.
758 * See the default implementation.
759 */
asd_build_control_phy(struct asd_ascb * ascb,int phy_id,u8 subfunc)760 void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
761 {
762 struct asd_phy *phy = &ascb->ha->phys[phy_id];
763 struct scb *scb = ascb->scb;
764 struct control_phy *control_phy = &scb->control_phy;
765
766 scb->header.opcode = CONTROL_PHY;
767 control_phy->phy_id = (u8) phy_id;
768 control_phy->sub_func = subfunc;
769
770 switch (subfunc) {
771 case EXECUTE_HARD_RESET: /* 0x81 */
772 case ENABLE_PHY: /* 0x01 */
773 /* decide hot plug delay */
774 control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
775
776 /* decide speed mask */
777 set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
778
779 /* initiator port settings are in the hi nibble */
780 if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
781 control_phy->port_type = SAS_PROTOCOL_ALL << 4;
782 else if (phy->sas_phy.role == PHY_ROLE_TARGET)
783 control_phy->port_type = SAS_PROTOCOL_ALL;
784 else
785 control_phy->port_type =
786 (SAS_PROTOCOL_ALL << 4) | SAS_PROTOCOL_ALL;
787
788 /* link reset retries, this should be nominal */
789 control_phy->link_reset_retries = 10;
790 fallthrough;
791
792 case RELEASE_SPINUP_HOLD: /* 0x02 */
793 /* decide the func_mask */
794 control_phy->func_mask = FUNCTION_MASK_DEFAULT;
795 if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
796 control_phy->func_mask &= ~SPINUP_HOLD_DIS;
797 else
798 control_phy->func_mask |= SPINUP_HOLD_DIS;
799 }
800
801 control_phy->conn_handle = cpu_to_le16(0xFFFF);
802
803 ascb->tasklet_complete = control_phy_tasklet_complete;
804 }
805
806 /* ---------- INITIATE LINK ADM TASK ---------- */
807
808 #if 0
809
810 static void link_adm_tasklet_complete(struct asd_ascb *ascb,
811 struct done_list_struct *dl)
812 {
813 u8 opcode = dl->opcode;
814 struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
815 u8 phy_id = link_adm->phy_id;
816
817 if (opcode != TC_NO_ERROR) {
818 asd_printk("phy%d: link adm task 0x%x completed with error "
819 "0x%x\n", phy_id, link_adm->sub_func, opcode);
820 }
821 ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
822 phy_id, link_adm->sub_func, opcode);
823
824 asd_ascb_free(ascb);
825 }
826
827 void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
828 u8 subfunc)
829 {
830 struct scb *scb = ascb->scb;
831 struct initiate_link_adm *link_adm = &scb->link_adm;
832
833 scb->header.opcode = INITIATE_LINK_ADM_TASK;
834
835 link_adm->phy_id = phy_id;
836 link_adm->sub_func = subfunc;
837 link_adm->conn_handle = cpu_to_le16(0xFFFF);
838
839 ascb->tasklet_complete = link_adm_tasklet_complete;
840 }
841
842 #endif /* 0 */
843
844 /* ---------- SCB timer ---------- */
845
846 /**
847 * asd_ascb_timedout -- called when a pending SCB's timer has expired
848 * @t: Timer context used to fetch the SCB
849 *
850 * This is the default timeout function which does the most necessary.
851 * Upper layers can implement their own timeout function, say to free
852 * resources they have with this SCB, and then call this one at the
853 * end of their timeout function. To do this, one should initialize
854 * the ascb->timer.{function, expires} prior to calling the post
855 * function. The timer is started by the post function.
856 */
asd_ascb_timedout(struct timer_list * t)857 void asd_ascb_timedout(struct timer_list *t)
858 {
859 struct asd_ascb *ascb = from_timer(ascb, t, timer);
860 struct asd_seq_data *seq = &ascb->ha->seq;
861 unsigned long flags;
862
863 ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
864
865 spin_lock_irqsave(&seq->pend_q_lock, flags);
866 seq->pending--;
867 list_del_init(&ascb->list);
868 spin_unlock_irqrestore(&seq->pend_q_lock, flags);
869
870 asd_ascb_free(ascb);
871 }
872
873 /* ---------- CONTROL PHY ---------- */
874
875 /* Given the spec value, return a driver value. */
876 static const int phy_func_table[] = {
877 [PHY_FUNC_NOP] = PHY_NO_OP,
878 [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
879 [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
880 [PHY_FUNC_DISABLE] = DISABLE_PHY,
881 [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
882 };
883
asd_control_phy(struct asd_sas_phy * phy,enum phy_func func,void * arg)884 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
885 {
886 struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
887 struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
888 struct asd_ascb *ascb;
889 struct sas_phy_linkrates *rates;
890 int res = 1;
891
892 switch (func) {
893 case PHY_FUNC_CLEAR_ERROR_LOG:
894 case PHY_FUNC_GET_EVENTS:
895 return -ENOSYS;
896 case PHY_FUNC_SET_LINK_RATE:
897 rates = arg;
898 if (rates->minimum_linkrate) {
899 pd->min_sas_lrate = rates->minimum_linkrate;
900 pd->min_sata_lrate = rates->minimum_linkrate;
901 }
902 if (rates->maximum_linkrate) {
903 pd->max_sas_lrate = rates->maximum_linkrate;
904 pd->max_sata_lrate = rates->maximum_linkrate;
905 }
906 func = PHY_FUNC_LINK_RESET;
907 break;
908 default:
909 break;
910 }
911
912 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
913 if (!ascb)
914 return -ENOMEM;
915
916 asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
917 res = asd_post_ascb_list(asd_ha, ascb , 1);
918 if (res)
919 asd_ascb_free(ascb);
920
921 return res;
922 }
923