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