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