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