Commit | Line | Data |
---|---|---|
dbf9bfe6 | 1 | /* |
f5860992 | 2 | * PMC-Sierra PM8001/8081/8088/8089 SAS/SATA based host adapters driver |
dbf9bfe6 | 3 | * |
4 | * Copyright (c) 2008-2009 USI Co., Ltd. | |
5 | * All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions, and the following disclaimer, | |
12 | * without modification. | |
13 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer | |
14 | * substantially similar to the "NO WARRANTY" disclaimer below | |
15 | * ("Disclaimer") and any redistribution must be conditioned upon | |
16 | * including a substantially similar Disclaimer requirement for further | |
17 | * binary redistribution. | |
18 | * 3. Neither the names of the above-listed copyright holders nor the names | |
19 | * of any contributors may be used to endorse or promote products derived | |
20 | * from this software without specific prior written permission. | |
21 | * | |
22 | * Alternatively, this software may be distributed under the terms of the | |
23 | * GNU General Public License ("GPL") version 2 as published by the Free | |
24 | * Software Foundation. | |
25 | * | |
26 | * NO WARRANTY | |
27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR | |
30 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
31 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
32 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
33 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
34 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
35 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
36 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
37 | * POSSIBILITY OF SUCH DAMAGES. | |
38 | * | |
39 | */ | |
40 | ||
5a0e3ad6 | 41 | #include <linux/slab.h> |
dbf9bfe6 | 42 | #include "pm8001_sas.h" |
43 | ||
44 | /** | |
45 | * pm8001_find_tag - from sas task to find out tag that belongs to this task | |
46 | * @task: the task sent to the LLDD | |
47 | * @tag: the found tag associated with the task | |
48 | */ | |
49 | static int pm8001_find_tag(struct sas_task *task, u32 *tag) | |
50 | { | |
51 | if (task->lldd_task) { | |
52 | struct pm8001_ccb_info *ccb; | |
53 | ccb = task->lldd_task; | |
54 | *tag = ccb->ccb_tag; | |
55 | return 1; | |
56 | } | |
57 | return 0; | |
58 | } | |
59 | ||
60 | /** | |
ef300544 | 61 | * pm8001_tag_free - free the no more needed tag |
dbf9bfe6 | 62 | * @pm8001_ha: our hba struct |
63 | * @tag: the found tag associated with the task | |
64 | */ | |
c6b9ef57 | 65 | void pm8001_tag_free(struct pm8001_hba_info *pm8001_ha, u32 tag) |
dbf9bfe6 | 66 | { |
67 | void *bitmap = pm8001_ha->tags; | |
ef300544 | 68 | clear_bit(tag, bitmap); |
dbf9bfe6 | 69 | } |
70 | ||
71 | /** | |
72 | * pm8001_tag_alloc - allocate a empty tag for task used. | |
73 | * @pm8001_ha: our hba struct | |
74 | * @tag_out: the found empty tag . | |
75 | */ | |
76 | inline int pm8001_tag_alloc(struct pm8001_hba_info *pm8001_ha, u32 *tag_out) | |
77 | { | |
ef300544 | 78 | unsigned int tag; |
dbf9bfe6 | 79 | void *bitmap = pm8001_ha->tags; |
646cdf00 | 80 | unsigned long flags; |
dbf9bfe6 | 81 | |
646cdf00 | 82 | spin_lock_irqsave(&pm8001_ha->bitmap_lock, flags); |
ef300544 | 83 | tag = find_first_zero_bit(bitmap, pm8001_ha->tags_num); |
646cdf00 TH |
84 | if (tag >= pm8001_ha->tags_num) { |
85 | spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags); | |
dbf9bfe6 | 86 | return -SAS_QUEUE_FULL; |
646cdf00 | 87 | } |
ef300544 | 88 | set_bit(tag, bitmap); |
646cdf00 | 89 | spin_unlock_irqrestore(&pm8001_ha->bitmap_lock, flags); |
dbf9bfe6 | 90 | *tag_out = tag; |
91 | return 0; | |
92 | } | |
93 | ||
94 | void pm8001_tag_init(struct pm8001_hba_info *pm8001_ha) | |
95 | { | |
96 | int i; | |
97 | for (i = 0; i < pm8001_ha->tags_num; ++i) | |
ef300544 | 98 | pm8001_tag_free(pm8001_ha, i); |
dbf9bfe6 | 99 | } |
100 | ||
101 | /** | |
102 | * pm8001_mem_alloc - allocate memory for pm8001. | |
103 | * @pdev: pci device. | |
104 | * @virt_addr: the allocated virtual address | |
105 | * @pphys_addr_hi: the physical address high byte address. | |
106 | * @pphys_addr_lo: the physical address low byte address. | |
107 | * @mem_size: memory size. | |
108 | */ | |
109 | int pm8001_mem_alloc(struct pci_dev *pdev, void **virt_addr, | |
110 | dma_addr_t *pphys_addr, u32 *pphys_addr_hi, | |
111 | u32 *pphys_addr_lo, u32 mem_size, u32 align) | |
112 | { | |
113 | caddr_t mem_virt_alloc; | |
114 | dma_addr_t mem_dma_handle; | |
115 | u64 phys_align; | |
116 | u64 align_offset = 0; | |
117 | if (align) | |
118 | align_offset = (dma_addr_t)align - 1; | |
7c845eb5 JP |
119 | mem_virt_alloc = pci_zalloc_consistent(pdev, mem_size + align, |
120 | &mem_dma_handle); | |
dbf9bfe6 | 121 | if (!mem_virt_alloc) { |
122 | pm8001_printk("memory allocation error\n"); | |
123 | return -1; | |
124 | } | |
dbf9bfe6 | 125 | *pphys_addr = mem_dma_handle; |
126 | phys_align = (*pphys_addr + align_offset) & ~align_offset; | |
127 | *virt_addr = (void *)mem_virt_alloc + phys_align - *pphys_addr; | |
128 | *pphys_addr_hi = upper_32_bits(phys_align); | |
129 | *pphys_addr_lo = lower_32_bits(phys_align); | |
130 | return 0; | |
131 | } | |
132 | /** | |
133 | * pm8001_find_ha_by_dev - from domain device which come from sas layer to | |
134 | * find out our hba struct. | |
135 | * @dev: the domain device which from sas layer. | |
136 | */ | |
137 | static | |
138 | struct pm8001_hba_info *pm8001_find_ha_by_dev(struct domain_device *dev) | |
139 | { | |
140 | struct sas_ha_struct *sha = dev->port->ha; | |
141 | struct pm8001_hba_info *pm8001_ha = sha->lldd_ha; | |
142 | return pm8001_ha; | |
143 | } | |
144 | ||
145 | /** | |
146 | * pm8001_phy_control - this function should be registered to | |
147 | * sas_domain_function_template to provide libsas used, note: this is just | |
148 | * control the HBA phy rather than other expander phy if you want control | |
149 | * other phy, you should use SMP command. | |
150 | * @sas_phy: which phy in HBA phys. | |
151 | * @func: the operation. | |
152 | * @funcdata: always NULL. | |
153 | */ | |
154 | int pm8001_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func, | |
155 | void *funcdata) | |
156 | { | |
157 | int rc = 0, phy_id = sas_phy->id; | |
158 | struct pm8001_hba_info *pm8001_ha = NULL; | |
159 | struct sas_phy_linkrates *rates; | |
160 | DECLARE_COMPLETION_ONSTACK(completion); | |
d95d0001 | 161 | unsigned long flags; |
dbf9bfe6 | 162 | pm8001_ha = sas_phy->ha->lldd_ha; |
163 | pm8001_ha->phy[phy_id].enable_completion = &completion; | |
164 | switch (func) { | |
165 | case PHY_FUNC_SET_LINK_RATE: | |
166 | rates = funcdata; | |
167 | if (rates->minimum_linkrate) { | |
168 | pm8001_ha->phy[phy_id].minimum_linkrate = | |
169 | rates->minimum_linkrate; | |
170 | } | |
171 | if (rates->maximum_linkrate) { | |
172 | pm8001_ha->phy[phy_id].maximum_linkrate = | |
173 | rates->maximum_linkrate; | |
174 | } | |
175 | if (pm8001_ha->phy[phy_id].phy_state == 0) { | |
176 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); | |
177 | wait_for_completion(&completion); | |
178 | } | |
179 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, | |
180 | PHY_LINK_RESET); | |
181 | break; | |
182 | case PHY_FUNC_HARD_RESET: | |
183 | if (pm8001_ha->phy[phy_id].phy_state == 0) { | |
184 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); | |
185 | wait_for_completion(&completion); | |
186 | } | |
187 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, | |
188 | PHY_HARD_RESET); | |
189 | break; | |
190 | case PHY_FUNC_LINK_RESET: | |
191 | if (pm8001_ha->phy[phy_id].phy_state == 0) { | |
192 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, phy_id); | |
193 | wait_for_completion(&completion); | |
194 | } | |
195 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, | |
196 | PHY_LINK_RESET); | |
197 | break; | |
198 | case PHY_FUNC_RELEASE_SPINUP_HOLD: | |
199 | PM8001_CHIP_DISP->phy_ctl_req(pm8001_ha, phy_id, | |
200 | PHY_LINK_RESET); | |
201 | break; | |
202 | case PHY_FUNC_DISABLE: | |
203 | PM8001_CHIP_DISP->phy_stop_req(pm8001_ha, phy_id); | |
204 | break; | |
d95d0001 MS |
205 | case PHY_FUNC_GET_EVENTS: |
206 | spin_lock_irqsave(&pm8001_ha->lock, flags); | |
f5860992 S |
207 | if (pm8001_ha->chip_id == chip_8001) { |
208 | if (-1 == pm8001_bar4_shift(pm8001_ha, | |
d95d0001 | 209 | (phy_id < 4) ? 0x30000 : 0x40000)) { |
f5860992 S |
210 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
211 | return -EINVAL; | |
212 | } | |
d95d0001 MS |
213 | } |
214 | { | |
215 | struct sas_phy *phy = sas_phy->phy; | |
216 | uint32_t *qp = (uint32_t *)(((char *) | |
217 | pm8001_ha->io_mem[2].memvirtaddr) | |
218 | + 0x1034 + (0x4000 * (phy_id & 3))); | |
219 | ||
220 | phy->invalid_dword_count = qp[0]; | |
221 | phy->running_disparity_error_count = qp[1]; | |
222 | phy->loss_of_dword_sync_count = qp[3]; | |
223 | phy->phy_reset_problem_count = qp[4]; | |
224 | } | |
f5860992 S |
225 | if (pm8001_ha->chip_id == chip_8001) |
226 | pm8001_bar4_shift(pm8001_ha, 0); | |
d95d0001 MS |
227 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); |
228 | return 0; | |
dbf9bfe6 | 229 | default: |
d95d0001 | 230 | rc = -EOPNOTSUPP; |
dbf9bfe6 | 231 | } |
232 | msleep(300); | |
233 | return rc; | |
234 | } | |
235 | ||
dbf9bfe6 | 236 | /** |
237 | * pm8001_scan_start - we should enable all HBA phys by sending the phy_start | |
238 | * command to HBA. | |
239 | * @shost: the scsi host data. | |
240 | */ | |
241 | void pm8001_scan_start(struct Scsi_Host *shost) | |
242 | { | |
243 | int i; | |
244 | struct pm8001_hba_info *pm8001_ha; | |
245 | struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost); | |
246 | pm8001_ha = sha->lldd_ha; | |
f5860992 S |
247 | /* SAS_RE_INITIALIZATION not available in SPCv/ve */ |
248 | if (pm8001_ha->chip_id == chip_8001) | |
249 | PM8001_CHIP_DISP->sas_re_init_req(pm8001_ha); | |
dbf9bfe6 | 250 | for (i = 0; i < pm8001_ha->chip->n_phy; ++i) |
251 | PM8001_CHIP_DISP->phy_start_req(pm8001_ha, i); | |
252 | } | |
253 | ||
254 | int pm8001_scan_finished(struct Scsi_Host *shost, unsigned long time) | |
255 | { | |
b1124cd3 DW |
256 | struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost); |
257 | ||
dbf9bfe6 | 258 | /* give the phy enabling interrupt event time to come in (1s |
259 | * is empirically about all it takes) */ | |
260 | if (time < HZ) | |
261 | return 0; | |
262 | /* Wait for discovery to finish */ | |
b1124cd3 | 263 | sas_drain_work(ha); |
dbf9bfe6 | 264 | return 1; |
265 | } | |
266 | ||
267 | /** | |
268 | * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task | |
269 | * @pm8001_ha: our hba card information | |
270 | * @ccb: the ccb which attached to smp task | |
271 | */ | |
272 | static int pm8001_task_prep_smp(struct pm8001_hba_info *pm8001_ha, | |
273 | struct pm8001_ccb_info *ccb) | |
274 | { | |
275 | return PM8001_CHIP_DISP->smp_req(pm8001_ha, ccb); | |
276 | } | |
277 | ||
278 | u32 pm8001_get_ncq_tag(struct sas_task *task, u32 *tag) | |
279 | { | |
280 | struct ata_queued_cmd *qc = task->uldd_task; | |
281 | if (qc) { | |
282 | if (qc->tf.command == ATA_CMD_FPDMA_WRITE || | |
283 | qc->tf.command == ATA_CMD_FPDMA_READ) { | |
284 | *tag = qc->tag; | |
285 | return 1; | |
286 | } | |
287 | } | |
288 | return 0; | |
289 | } | |
290 | ||
291 | /** | |
292 | * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task | |
293 | * @pm8001_ha: our hba card information | |
294 | * @ccb: the ccb which attached to sata task | |
295 | */ | |
296 | static int pm8001_task_prep_ata(struct pm8001_hba_info *pm8001_ha, | |
297 | struct pm8001_ccb_info *ccb) | |
298 | { | |
299 | return PM8001_CHIP_DISP->sata_req(pm8001_ha, ccb); | |
300 | } | |
301 | ||
302 | /** | |
303 | * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data | |
304 | * @pm8001_ha: our hba card information | |
305 | * @ccb: the ccb which attached to TM | |
306 | * @tmf: the task management IU | |
307 | */ | |
308 | static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info *pm8001_ha, | |
309 | struct pm8001_ccb_info *ccb, struct pm8001_tmf_task *tmf) | |
310 | { | |
311 | return PM8001_CHIP_DISP->ssp_tm_req(pm8001_ha, ccb, tmf); | |
312 | } | |
313 | ||
314 | /** | |
315 | * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task | |
316 | * @pm8001_ha: our hba card information | |
317 | * @ccb: the ccb which attached to ssp task | |
318 | */ | |
319 | static int pm8001_task_prep_ssp(struct pm8001_hba_info *pm8001_ha, | |
320 | struct pm8001_ccb_info *ccb) | |
321 | { | |
322 | return PM8001_CHIP_DISP->ssp_io_req(pm8001_ha, ccb); | |
323 | } | |
11e16364 | 324 | |
1cc943ae | 325 | /* Find the local port id that's attached to this device */ |
326 | static int sas_find_local_port_id(struct domain_device *dev) | |
327 | { | |
328 | struct domain_device *pdev = dev->parent; | |
329 | ||
330 | /* Directly attached device */ | |
331 | if (!pdev) | |
332 | return dev->port->id; | |
333 | while (pdev) { | |
334 | struct domain_device *pdev_p = pdev->parent; | |
335 | if (!pdev_p) | |
336 | return pdev->port->id; | |
337 | pdev = pdev->parent; | |
338 | } | |
339 | return 0; | |
340 | } | |
341 | ||
dbf9bfe6 | 342 | /** |
97ee2088 | 343 | * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware. |
dbf9bfe6 | 344 | * @task: the task to be execute. |
345 | * @num: if can_queue great than 1, the task can be queued up. for SMP task, | |
346 | * we always execute one one time. | |
347 | * @gfp_flags: gfp_flags. | |
97ee2088 | 348 | * @is_tmf: if it is task management task. |
dbf9bfe6 | 349 | * @tmf: the task management IU |
350 | */ | |
351 | #define DEV_IS_GONE(pm8001_dev) \ | |
aa9f8328 | 352 | ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED))) |
79855d17 | 353 | static int pm8001_task_exec(struct sas_task *task, |
dbf9bfe6 | 354 | gfp_t gfp_flags, int is_tmf, struct pm8001_tmf_task *tmf) |
355 | { | |
356 | struct domain_device *dev = task->dev; | |
357 | struct pm8001_hba_info *pm8001_ha; | |
358 | struct pm8001_device *pm8001_dev; | |
1cc943ae | 359 | struct pm8001_port *port = NULL; |
dbf9bfe6 | 360 | struct sas_task *t = task; |
361 | struct pm8001_ccb_info *ccb; | |
362 | u32 tag = 0xdeadbeef, rc, n_elem = 0; | |
312d3e56 | 363 | unsigned long flags = 0; |
dbf9bfe6 | 364 | |
365 | if (!dev->port) { | |
366 | struct task_status_struct *tsm = &t->task_status; | |
367 | tsm->resp = SAS_TASK_UNDELIVERED; | |
368 | tsm->stat = SAS_PHY_DOWN; | |
aa9f8328 | 369 | if (dev->dev_type != SAS_SATA_DEV) |
dbf9bfe6 | 370 | t->task_done(t); |
371 | return 0; | |
372 | } | |
373 | pm8001_ha = pm8001_find_ha_by_dev(task->dev); | |
374 | PM8001_IO_DBG(pm8001_ha, pm8001_printk("pm8001_task_exec device \n ")); | |
375 | spin_lock_irqsave(&pm8001_ha->lock, flags); | |
376 | do { | |
377 | dev = t->dev; | |
378 | pm8001_dev = dev->lldd_dev; | |
1cc943ae | 379 | port = &pm8001_ha->port[sas_find_local_port_id(dev)]; |
b90b378a | 380 | if (DEV_IS_GONE(pm8001_dev) || !port->port_attached) { |
1cc943ae | 381 | if (sas_protocol_ata(t->task_proto)) { |
382 | struct task_status_struct *ts = &t->task_status; | |
383 | ts->resp = SAS_TASK_UNDELIVERED; | |
384 | ts->stat = SAS_PHY_DOWN; | |
385 | ||
386 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
1cc943ae | 387 | t->task_done(t); |
1cc943ae | 388 | spin_lock_irqsave(&pm8001_ha->lock, flags); |
1cc943ae | 389 | continue; |
390 | } else { | |
391 | struct task_status_struct *ts = &t->task_status; | |
392 | ts->resp = SAS_TASK_UNDELIVERED; | |
393 | ts->stat = SAS_PHY_DOWN; | |
394 | t->task_done(t); | |
1cc943ae | 395 | continue; |
396 | } | |
397 | } | |
dbf9bfe6 | 398 | rc = pm8001_tag_alloc(pm8001_ha, &tag); |
399 | if (rc) | |
400 | goto err_out; | |
401 | ccb = &pm8001_ha->ccb_info[tag]; | |
402 | ||
403 | if (!sas_protocol_ata(t->task_proto)) { | |
404 | if (t->num_scatter) { | |
405 | n_elem = dma_map_sg(pm8001_ha->dev, | |
406 | t->scatter, | |
407 | t->num_scatter, | |
408 | t->data_dir); | |
409 | if (!n_elem) { | |
410 | rc = -ENOMEM; | |
97ee2088 | 411 | goto err_out_tag; |
dbf9bfe6 | 412 | } |
413 | } | |
414 | } else { | |
415 | n_elem = t->num_scatter; | |
416 | } | |
417 | ||
97ee2088 | 418 | t->lldd_task = ccb; |
dbf9bfe6 | 419 | ccb->n_elem = n_elem; |
420 | ccb->ccb_tag = tag; | |
421 | ccb->task = t; | |
aed97b8b | 422 | ccb->device = pm8001_dev; |
dbf9bfe6 | 423 | switch (t->task_proto) { |
424 | case SAS_PROTOCOL_SMP: | |
425 | rc = pm8001_task_prep_smp(pm8001_ha, ccb); | |
426 | break; | |
427 | case SAS_PROTOCOL_SSP: | |
428 | if (is_tmf) | |
429 | rc = pm8001_task_prep_ssp_tm(pm8001_ha, | |
430 | ccb, tmf); | |
431 | else | |
432 | rc = pm8001_task_prep_ssp(pm8001_ha, ccb); | |
433 | break; | |
434 | case SAS_PROTOCOL_SATA: | |
435 | case SAS_PROTOCOL_STP: | |
dbf9bfe6 | 436 | rc = pm8001_task_prep_ata(pm8001_ha, ccb); |
437 | break; | |
438 | default: | |
439 | dev_printk(KERN_ERR, pm8001_ha->dev, | |
440 | "unknown sas_task proto: 0x%x\n", | |
441 | t->task_proto); | |
442 | rc = -EINVAL; | |
443 | break; | |
444 | } | |
445 | ||
446 | if (rc) { | |
447 | PM8001_IO_DBG(pm8001_ha, | |
448 | pm8001_printk("rc is %x\n", rc)); | |
449 | goto err_out_tag; | |
450 | } | |
dbf9bfe6 | 451 | /* TODO: select normal or high priority */ |
452 | spin_lock(&t->task_state_lock); | |
453 | t->task_state_flags |= SAS_TASK_AT_INITIATOR; | |
454 | spin_unlock(&t->task_state_lock); | |
455 | pm8001_dev->running_req++; | |
79855d17 | 456 | } while (0); |
dbf9bfe6 | 457 | rc = 0; |
458 | goto out_done; | |
459 | ||
460 | err_out_tag: | |
461 | pm8001_tag_free(pm8001_ha, tag); | |
462 | err_out: | |
463 | dev_printk(KERN_ERR, pm8001_ha->dev, "pm8001 exec failed[%d]!\n", rc); | |
464 | if (!sas_protocol_ata(t->task_proto)) | |
465 | if (n_elem) | |
466 | dma_unmap_sg(pm8001_ha->dev, t->scatter, n_elem, | |
467 | t->data_dir); | |
468 | out_done: | |
469 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
470 | return rc; | |
471 | } | |
472 | ||
473 | /** | |
474 | * pm8001_queue_command - register for upper layer used, all IO commands sent | |
475 | * to HBA are from this interface. | |
476 | * @task: the task to be execute. | |
dbf9bfe6 | 477 | * @gfp_flags: gfp_flags |
478 | */ | |
79855d17 | 479 | int pm8001_queue_command(struct sas_task *task, gfp_t gfp_flags) |
dbf9bfe6 | 480 | { |
79855d17 | 481 | return pm8001_task_exec(task, gfp_flags, 0, NULL); |
dbf9bfe6 | 482 | } |
483 | ||
dbf9bfe6 | 484 | /** |
485 | * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb. | |
486 | * @pm8001_ha: our hba card information | |
487 | * @ccb: the ccb which attached to ssp task | |
488 | * @task: the task to be free. | |
489 | * @ccb_idx: ccb index. | |
490 | */ | |
491 | void pm8001_ccb_task_free(struct pm8001_hba_info *pm8001_ha, | |
492 | struct sas_task *task, struct pm8001_ccb_info *ccb, u32 ccb_idx) | |
493 | { | |
494 | if (!ccb->task) | |
495 | return; | |
496 | if (!sas_protocol_ata(task->task_proto)) | |
497 | if (ccb->n_elem) | |
498 | dma_unmap_sg(pm8001_ha->dev, task->scatter, | |
499 | task->num_scatter, task->data_dir); | |
500 | ||
501 | switch (task->task_proto) { | |
502 | case SAS_PROTOCOL_SMP: | |
503 | dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_resp, 1, | |
504 | PCI_DMA_FROMDEVICE); | |
505 | dma_unmap_sg(pm8001_ha->dev, &task->smp_task.smp_req, 1, | |
506 | PCI_DMA_TODEVICE); | |
507 | break; | |
508 | ||
509 | case SAS_PROTOCOL_SATA: | |
510 | case SAS_PROTOCOL_STP: | |
511 | case SAS_PROTOCOL_SSP: | |
512 | default: | |
513 | /* do nothing */ | |
514 | break; | |
515 | } | |
516 | task->lldd_task = NULL; | |
517 | ccb->task = NULL; | |
518 | ccb->ccb_tag = 0xFFFFFFFF; | |
5954d738 | 519 | ccb->open_retry = 0; |
ef300544 | 520 | pm8001_tag_free(pm8001_ha, ccb_idx); |
dbf9bfe6 | 521 | } |
522 | ||
523 | /** | |
97ee2088 | 524 | * pm8001_alloc_dev - find a empty pm8001_device |
dbf9bfe6 | 525 | * @pm8001_ha: our hba card information |
526 | */ | |
527 | struct pm8001_device *pm8001_alloc_dev(struct pm8001_hba_info *pm8001_ha) | |
528 | { | |
529 | u32 dev; | |
530 | for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) { | |
aa9f8328 | 531 | if (pm8001_ha->devices[dev].dev_type == SAS_PHY_UNUSED) { |
dbf9bfe6 | 532 | pm8001_ha->devices[dev].id = dev; |
533 | return &pm8001_ha->devices[dev]; | |
534 | } | |
535 | } | |
536 | if (dev == PM8001_MAX_DEVICES) { | |
537 | PM8001_FAIL_DBG(pm8001_ha, | |
538 | pm8001_printk("max support %d devices, ignore ..\n", | |
539 | PM8001_MAX_DEVICES)); | |
540 | } | |
541 | return NULL; | |
542 | } | |
c6b9ef57 S |
543 | /** |
544 | * pm8001_find_dev - find a matching pm8001_device | |
545 | * @pm8001_ha: our hba card information | |
546 | */ | |
547 | struct pm8001_device *pm8001_find_dev(struct pm8001_hba_info *pm8001_ha, | |
548 | u32 device_id) | |
549 | { | |
550 | u32 dev; | |
551 | for (dev = 0; dev < PM8001_MAX_DEVICES; dev++) { | |
552 | if (pm8001_ha->devices[dev].device_id == device_id) | |
553 | return &pm8001_ha->devices[dev]; | |
554 | } | |
555 | if (dev == PM8001_MAX_DEVICES) { | |
556 | PM8001_FAIL_DBG(pm8001_ha, pm8001_printk("NO MATCHING " | |
557 | "DEVICE FOUND !!!\n")); | |
558 | } | |
559 | return NULL; | |
560 | } | |
dbf9bfe6 | 561 | |
562 | static void pm8001_free_dev(struct pm8001_device *pm8001_dev) | |
563 | { | |
564 | u32 id = pm8001_dev->id; | |
565 | memset(pm8001_dev, 0, sizeof(*pm8001_dev)); | |
566 | pm8001_dev->id = id; | |
aa9f8328 | 567 | pm8001_dev->dev_type = SAS_PHY_UNUSED; |
dbf9bfe6 | 568 | pm8001_dev->device_id = PM8001_MAX_DEVICES; |
569 | pm8001_dev->sas_device = NULL; | |
570 | } | |
571 | ||
572 | /** | |
97ee2088 | 573 | * pm8001_dev_found_notify - libsas notify a device is found. |
574 | * @dev: the device structure which sas layer used. | |
575 | * | |
576 | * when libsas find a sas domain device, it should tell the LLDD that | |
577 | * device is found, and then LLDD register this device to HBA firmware | |
578 | * by the command "OPC_INB_REG_DEV", after that the HBA will assign a | |
579 | * device ID(according to device's sas address) and returned it to LLDD. From | |
dbf9bfe6 | 580 | * now on, we communicate with HBA FW with the device ID which HBA assigned |
3ad2f3fb | 581 | * rather than sas address. it is the necessary step for our HBA but it is |
dbf9bfe6 | 582 | * the optional for other HBA driver. |
dbf9bfe6 | 583 | */ |
584 | static int pm8001_dev_found_notify(struct domain_device *dev) | |
585 | { | |
586 | unsigned long flags = 0; | |
587 | int res = 0; | |
588 | struct pm8001_hba_info *pm8001_ha = NULL; | |
589 | struct domain_device *parent_dev = dev->parent; | |
590 | struct pm8001_device *pm8001_device; | |
591 | DECLARE_COMPLETION_ONSTACK(completion); | |
592 | u32 flag = 0; | |
593 | pm8001_ha = pm8001_find_ha_by_dev(dev); | |
594 | spin_lock_irqsave(&pm8001_ha->lock, flags); | |
595 | ||
596 | pm8001_device = pm8001_alloc_dev(pm8001_ha); | |
dbf9bfe6 | 597 | if (!pm8001_device) { |
598 | res = -1; | |
599 | goto found_out; | |
600 | } | |
f01f4e6a | 601 | pm8001_device->sas_device = dev; |
dbf9bfe6 | 602 | dev->lldd_dev = pm8001_device; |
603 | pm8001_device->dev_type = dev->dev_type; | |
604 | pm8001_device->dcompletion = &completion; | |
605 | if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) { | |
606 | int phy_id; | |
607 | struct ex_phy *phy; | |
608 | for (phy_id = 0; phy_id < parent_dev->ex_dev.num_phys; | |
609 | phy_id++) { | |
610 | phy = &parent_dev->ex_dev.ex_phy[phy_id]; | |
611 | if (SAS_ADDR(phy->attached_sas_addr) | |
612 | == SAS_ADDR(dev->sas_addr)) { | |
613 | pm8001_device->attached_phy = phy_id; | |
614 | break; | |
615 | } | |
616 | } | |
617 | if (phy_id == parent_dev->ex_dev.num_phys) { | |
618 | PM8001_FAIL_DBG(pm8001_ha, | |
619 | pm8001_printk("Error: no attached dev:%016llx" | |
620 | " at ex:%016llx.\n", SAS_ADDR(dev->sas_addr), | |
621 | SAS_ADDR(parent_dev->sas_addr))); | |
622 | res = -1; | |
623 | } | |
624 | } else { | |
aa9f8328 | 625 | if (dev->dev_type == SAS_SATA_DEV) { |
dbf9bfe6 | 626 | pm8001_device->attached_phy = |
627 | dev->rphy->identify.phy_identifier; | |
628 | flag = 1; /* directly sata*/ | |
629 | } | |
630 | } /*register this device to HBA*/ | |
6fbc7692 | 631 | PM8001_DISC_DBG(pm8001_ha, pm8001_printk("Found device\n")); |
dbf9bfe6 | 632 | PM8001_CHIP_DISP->reg_dev_req(pm8001_ha, pm8001_device, flag); |
633 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
634 | wait_for_completion(&completion); | |
aa9f8328 | 635 | if (dev->dev_type == SAS_END_DEVICE) |
dbf9bfe6 | 636 | msleep(50); |
5c4fb76a | 637 | pm8001_ha->flags = PM8001F_RUN_TIME; |
dbf9bfe6 | 638 | return 0; |
639 | found_out: | |
640 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
641 | return res; | |
642 | } | |
643 | ||
644 | int pm8001_dev_found(struct domain_device *dev) | |
645 | { | |
646 | return pm8001_dev_found_notify(dev); | |
647 | } | |
648 | ||
c6b9ef57 | 649 | void pm8001_task_done(struct sas_task *task) |
dbf9bfe6 | 650 | { |
f0bf750c | 651 | if (!del_timer(&task->slow_task->timer)) |
dbf9bfe6 | 652 | return; |
f0bf750c | 653 | complete(&task->slow_task->completion); |
dbf9bfe6 | 654 | } |
655 | ||
656 | static void pm8001_tmf_timedout(unsigned long data) | |
657 | { | |
658 | struct sas_task *task = (struct sas_task *)data; | |
659 | ||
660 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; | |
f0bf750c | 661 | complete(&task->slow_task->completion); |
dbf9bfe6 | 662 | } |
663 | ||
664 | #define PM8001_TASK_TIMEOUT 20 | |
665 | /** | |
97ee2088 | 666 | * pm8001_exec_internal_tmf_task - execute some task management commands. |
dbf9bfe6 | 667 | * @dev: the wanted device. |
668 | * @tmf: which task management wanted to be take. | |
669 | * @para_len: para_len. | |
670 | * @parameter: ssp task parameter. | |
97ee2088 | 671 | * |
672 | * when errors or exception happened, we may want to do something, for example | |
673 | * abort the issued task which result in this execption, it is done by calling | |
674 | * this function, note it is also with the task execute interface. | |
dbf9bfe6 | 675 | */ |
676 | static int pm8001_exec_internal_tmf_task(struct domain_device *dev, | |
677 | void *parameter, u32 para_len, struct pm8001_tmf_task *tmf) | |
678 | { | |
679 | int res, retry; | |
680 | struct sas_task *task = NULL; | |
681 | struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); | |
e912457b AKS |
682 | struct pm8001_device *pm8001_dev = dev->lldd_dev; |
683 | DECLARE_COMPLETION_ONSTACK(completion_setstate); | |
dbf9bfe6 | 684 | |
685 | for (retry = 0; retry < 3; retry++) { | |
f0bf750c | 686 | task = sas_alloc_slow_task(GFP_KERNEL); |
dbf9bfe6 | 687 | if (!task) |
688 | return -ENOMEM; | |
689 | ||
690 | task->dev = dev; | |
691 | task->task_proto = dev->tproto; | |
692 | memcpy(&task->ssp_task, parameter, para_len); | |
693 | task->task_done = pm8001_task_done; | |
f0bf750c DW |
694 | task->slow_task->timer.data = (unsigned long)task; |
695 | task->slow_task->timer.function = pm8001_tmf_timedout; | |
696 | task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT*HZ; | |
697 | add_timer(&task->slow_task->timer); | |
dbf9bfe6 | 698 | |
79855d17 | 699 | res = pm8001_task_exec(task, GFP_KERNEL, 1, tmf); |
dbf9bfe6 | 700 | |
701 | if (res) { | |
f0bf750c | 702 | del_timer(&task->slow_task->timer); |
dbf9bfe6 | 703 | PM8001_FAIL_DBG(pm8001_ha, |
704 | pm8001_printk("Executing internal task " | |
705 | "failed\n")); | |
706 | goto ex_err; | |
707 | } | |
f0bf750c | 708 | wait_for_completion(&task->slow_task->completion); |
e912457b AKS |
709 | if (pm8001_ha->chip_id != chip_8001) { |
710 | pm8001_dev->setds_completion = &completion_setstate; | |
711 | PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, | |
712 | pm8001_dev, 0x01); | |
713 | wait_for_completion(&completion_setstate); | |
714 | } | |
dbf9bfe6 | 715 | res = -TMF_RESP_FUNC_FAILED; |
716 | /* Even TMF timed out, return direct. */ | |
717 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { | |
718 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { | |
719 | PM8001_FAIL_DBG(pm8001_ha, | |
720 | pm8001_printk("TMF task[%x]timeout.\n", | |
721 | tmf->tmf)); | |
722 | goto ex_err; | |
723 | } | |
724 | } | |
725 | ||
726 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
df64d3ca | 727 | task->task_status.stat == SAM_STAT_GOOD) { |
dbf9bfe6 | 728 | res = TMF_RESP_FUNC_COMPLETE; |
729 | break; | |
730 | } | |
731 | ||
732 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
733 | task->task_status.stat == SAS_DATA_UNDERRUN) { | |
734 | /* no error, but return the number of bytes of | |
735 | * underrun */ | |
736 | res = task->task_status.residual; | |
737 | break; | |
738 | } | |
739 | ||
740 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
741 | task->task_status.stat == SAS_DATA_OVERRUN) { | |
742 | PM8001_FAIL_DBG(pm8001_ha, | |
743 | pm8001_printk("Blocked task error.\n")); | |
744 | res = -EMSGSIZE; | |
745 | break; | |
746 | } else { | |
97ee2088 | 747 | PM8001_EH_DBG(pm8001_ha, |
748 | pm8001_printk(" Task to dev %016llx response:" | |
749 | "0x%x status 0x%x\n", | |
dbf9bfe6 | 750 | SAS_ADDR(dev->sas_addr), |
751 | task->task_status.resp, | |
752 | task->task_status.stat)); | |
4fcf812c | 753 | sas_free_task(task); |
dbf9bfe6 | 754 | task = NULL; |
755 | } | |
756 | } | |
757 | ex_err: | |
758 | BUG_ON(retry == 3 && task != NULL); | |
4fcf812c | 759 | sas_free_task(task); |
dbf9bfe6 | 760 | return res; |
761 | } | |
762 | ||
763 | static int | |
764 | pm8001_exec_internal_task_abort(struct pm8001_hba_info *pm8001_ha, | |
765 | struct pm8001_device *pm8001_dev, struct domain_device *dev, u32 flag, | |
766 | u32 task_tag) | |
767 | { | |
768 | int res, retry; | |
97ee2088 | 769 | u32 ccb_tag; |
dbf9bfe6 | 770 | struct pm8001_ccb_info *ccb; |
771 | struct sas_task *task = NULL; | |
772 | ||
773 | for (retry = 0; retry < 3; retry++) { | |
f0bf750c | 774 | task = sas_alloc_slow_task(GFP_KERNEL); |
dbf9bfe6 | 775 | if (!task) |
776 | return -ENOMEM; | |
777 | ||
778 | task->dev = dev; | |
779 | task->task_proto = dev->tproto; | |
780 | task->task_done = pm8001_task_done; | |
f0bf750c DW |
781 | task->slow_task->timer.data = (unsigned long)task; |
782 | task->slow_task->timer.function = pm8001_tmf_timedout; | |
783 | task->slow_task->timer.expires = jiffies + PM8001_TASK_TIMEOUT * HZ; | |
784 | add_timer(&task->slow_task->timer); | |
dbf9bfe6 | 785 | |
97ee2088 | 786 | res = pm8001_tag_alloc(pm8001_ha, &ccb_tag); |
787 | if (res) | |
788 | return res; | |
dbf9bfe6 | 789 | ccb = &pm8001_ha->ccb_info[ccb_tag]; |
790 | ccb->device = pm8001_dev; | |
791 | ccb->ccb_tag = ccb_tag; | |
792 | ccb->task = task; | |
793 | ||
794 | res = PM8001_CHIP_DISP->task_abort(pm8001_ha, | |
795 | pm8001_dev, flag, task_tag, ccb_tag); | |
796 | ||
797 | if (res) { | |
f0bf750c | 798 | del_timer(&task->slow_task->timer); |
dbf9bfe6 | 799 | PM8001_FAIL_DBG(pm8001_ha, |
800 | pm8001_printk("Executing internal task " | |
801 | "failed\n")); | |
802 | goto ex_err; | |
803 | } | |
f0bf750c | 804 | wait_for_completion(&task->slow_task->completion); |
dbf9bfe6 | 805 | res = TMF_RESP_FUNC_FAILED; |
806 | /* Even TMF timed out, return direct. */ | |
807 | if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) { | |
808 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { | |
809 | PM8001_FAIL_DBG(pm8001_ha, | |
810 | pm8001_printk("TMF task timeout.\n")); | |
811 | goto ex_err; | |
812 | } | |
813 | } | |
814 | ||
815 | if (task->task_status.resp == SAS_TASK_COMPLETE && | |
df64d3ca | 816 | task->task_status.stat == SAM_STAT_GOOD) { |
dbf9bfe6 | 817 | res = TMF_RESP_FUNC_COMPLETE; |
818 | break; | |
819 | ||
820 | } else { | |
97ee2088 | 821 | PM8001_EH_DBG(pm8001_ha, |
dbf9bfe6 | 822 | pm8001_printk(" Task to dev %016llx response: " |
823 | "0x%x status 0x%x\n", | |
824 | SAS_ADDR(dev->sas_addr), | |
825 | task->task_status.resp, | |
826 | task->task_status.stat)); | |
4fcf812c | 827 | sas_free_task(task); |
dbf9bfe6 | 828 | task = NULL; |
829 | } | |
830 | } | |
831 | ex_err: | |
832 | BUG_ON(retry == 3 && task != NULL); | |
4fcf812c | 833 | sas_free_task(task); |
dbf9bfe6 | 834 | return res; |
835 | } | |
836 | ||
837 | /** | |
838 | * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify" | |
839 | * @dev: the device structure which sas layer used. | |
840 | */ | |
841 | static void pm8001_dev_gone_notify(struct domain_device *dev) | |
842 | { | |
843 | unsigned long flags = 0; | |
dbf9bfe6 | 844 | struct pm8001_hba_info *pm8001_ha; |
845 | struct pm8001_device *pm8001_dev = dev->lldd_dev; | |
2471b894 | 846 | |
dbf9bfe6 | 847 | pm8001_ha = pm8001_find_ha_by_dev(dev); |
848 | spin_lock_irqsave(&pm8001_ha->lock, flags); | |
dbf9bfe6 | 849 | if (pm8001_dev) { |
2471b894 DC |
850 | u32 device_id = pm8001_dev->device_id; |
851 | ||
dbf9bfe6 | 852 | PM8001_DISC_DBG(pm8001_ha, |
853 | pm8001_printk("found dev[%d:%x] is gone.\n", | |
854 | pm8001_dev->device_id, pm8001_dev->dev_type)); | |
855 | if (pm8001_dev->running_req) { | |
856 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
857 | pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , | |
858 | dev, 1, 0); | |
859 | spin_lock_irqsave(&pm8001_ha->lock, flags); | |
860 | } | |
861 | PM8001_CHIP_DISP->dereg_dev_req(pm8001_ha, device_id); | |
862 | pm8001_free_dev(pm8001_dev); | |
863 | } else { | |
864 | PM8001_DISC_DBG(pm8001_ha, | |
865 | pm8001_printk("Found dev has gone.\n")); | |
866 | } | |
867 | dev->lldd_dev = NULL; | |
868 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
869 | } | |
870 | ||
871 | void pm8001_dev_gone(struct domain_device *dev) | |
872 | { | |
873 | pm8001_dev_gone_notify(dev); | |
874 | } | |
875 | ||
876 | static int pm8001_issue_ssp_tmf(struct domain_device *dev, | |
877 | u8 *lun, struct pm8001_tmf_task *tmf) | |
878 | { | |
879 | struct sas_ssp_task ssp_task; | |
880 | if (!(dev->tproto & SAS_PROTOCOL_SSP)) | |
881 | return TMF_RESP_FUNC_ESUPP; | |
882 | ||
883 | strncpy((u8 *)&ssp_task.LUN, lun, 8); | |
884 | return pm8001_exec_internal_tmf_task(dev, &ssp_task, sizeof(ssp_task), | |
885 | tmf); | |
886 | } | |
887 | ||
5954d738 MS |
888 | /* retry commands by ha, by task and/or by device */ |
889 | void pm8001_open_reject_retry( | |
890 | struct pm8001_hba_info *pm8001_ha, | |
891 | struct sas_task *task_to_close, | |
892 | struct pm8001_device *device_to_close) | |
893 | { | |
894 | int i; | |
895 | unsigned long flags; | |
896 | ||
897 | if (pm8001_ha == NULL) | |
898 | return; | |
899 | ||
900 | spin_lock_irqsave(&pm8001_ha->lock, flags); | |
901 | ||
902 | for (i = 0; i < PM8001_MAX_CCB; i++) { | |
903 | struct sas_task *task; | |
904 | struct task_status_struct *ts; | |
905 | struct pm8001_device *pm8001_dev; | |
906 | unsigned long flags1; | |
907 | u32 tag; | |
908 | struct pm8001_ccb_info *ccb = &pm8001_ha->ccb_info[i]; | |
909 | ||
910 | pm8001_dev = ccb->device; | |
aa9f8328 | 911 | if (!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)) |
5954d738 MS |
912 | continue; |
913 | if (!device_to_close) { | |
914 | uintptr_t d = (uintptr_t)pm8001_dev | |
915 | - (uintptr_t)&pm8001_ha->devices; | |
916 | if (((d % sizeof(*pm8001_dev)) != 0) | |
917 | || ((d / sizeof(*pm8001_dev)) >= PM8001_MAX_DEVICES)) | |
918 | continue; | |
919 | } else if (pm8001_dev != device_to_close) | |
920 | continue; | |
921 | tag = ccb->ccb_tag; | |
922 | if (!tag || (tag == 0xFFFFFFFF)) | |
923 | continue; | |
924 | task = ccb->task; | |
925 | if (!task || !task->task_done) | |
926 | continue; | |
927 | if (task_to_close && (task != task_to_close)) | |
928 | continue; | |
929 | ts = &task->task_status; | |
930 | ts->resp = SAS_TASK_COMPLETE; | |
931 | /* Force the midlayer to retry */ | |
932 | ts->stat = SAS_OPEN_REJECT; | |
933 | ts->open_rej_reason = SAS_OREJ_RSVD_RETRY; | |
934 | if (pm8001_dev) | |
935 | pm8001_dev->running_req--; | |
936 | spin_lock_irqsave(&task->task_state_lock, flags1); | |
937 | task->task_state_flags &= ~SAS_TASK_STATE_PENDING; | |
938 | task->task_state_flags &= ~SAS_TASK_AT_INITIATOR; | |
939 | task->task_state_flags |= SAS_TASK_STATE_DONE; | |
940 | if (unlikely((task->task_state_flags | |
941 | & SAS_TASK_STATE_ABORTED))) { | |
942 | spin_unlock_irqrestore(&task->task_state_lock, | |
943 | flags1); | |
944 | pm8001_ccb_task_free(pm8001_ha, task, ccb, tag); | |
945 | } else { | |
946 | spin_unlock_irqrestore(&task->task_state_lock, | |
947 | flags1); | |
948 | pm8001_ccb_task_free(pm8001_ha, task, ccb, tag); | |
949 | mb();/* in order to force CPU ordering */ | |
950 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
951 | task->task_done(task); | |
952 | spin_lock_irqsave(&pm8001_ha->lock, flags); | |
953 | } | |
954 | } | |
955 | ||
956 | spin_unlock_irqrestore(&pm8001_ha->lock, flags); | |
957 | } | |
958 | ||
dbf9bfe6 | 959 | /** |
960 | * Standard mandates link reset for ATA (type 0) and hard reset for | |
961 | * SSP (type 1) , only for RECOVERY | |
962 | */ | |
963 | int pm8001_I_T_nexus_reset(struct domain_device *dev) | |
964 | { | |
965 | int rc = TMF_RESP_FUNC_FAILED; | |
966 | struct pm8001_device *pm8001_dev; | |
967 | struct pm8001_hba_info *pm8001_ha; | |
968 | struct sas_phy *phy; | |
e7db8229 | 969 | |
dbf9bfe6 | 970 | if (!dev || !dev->lldd_dev) |
e7db8229 | 971 | return -ENODEV; |
dbf9bfe6 | 972 | |
973 | pm8001_dev = dev->lldd_dev; | |
974 | pm8001_ha = pm8001_find_ha_by_dev(dev); | |
f41a0c44 | 975 | phy = sas_get_local_phy(dev); |
dbf9bfe6 | 976 | |
977 | if (dev_is_sata(dev)) { | |
978 | DECLARE_COMPLETION_ONSTACK(completion_setstate); | |
f41a0c44 DW |
979 | if (scsi_is_sas_phy_local(phy)) { |
980 | rc = 0; | |
981 | goto out; | |
982 | } | |
dbf9bfe6 | 983 | rc = sas_phy_reset(phy, 1); |
984 | msleep(2000); | |
985 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , | |
986 | dev, 1, 0); | |
987 | pm8001_dev->setds_completion = &completion_setstate; | |
988 | rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, | |
989 | pm8001_dev, 0x01); | |
990 | wait_for_completion(&completion_setstate); | |
f41a0c44 DW |
991 | } else { |
992 | rc = sas_phy_reset(phy, 1); | |
993 | msleep(2000); | |
dbf9bfe6 | 994 | } |
995 | PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n", | |
996 | pm8001_dev->device_id, rc)); | |
f41a0c44 DW |
997 | out: |
998 | sas_put_local_phy(phy); | |
dbf9bfe6 | 999 | return rc; |
1000 | } | |
1001 | ||
a6cb3d01 S |
1002 | /* |
1003 | * This function handle the IT_NEXUS_XXX event or completion | |
1004 | * status code for SSP/SATA/SMP I/O request. | |
1005 | */ | |
1006 | int pm8001_I_T_nexus_event_handler(struct domain_device *dev) | |
1007 | { | |
1008 | int rc = TMF_RESP_FUNC_FAILED; | |
1009 | struct pm8001_device *pm8001_dev; | |
1010 | struct pm8001_hba_info *pm8001_ha; | |
1011 | struct sas_phy *phy; | |
1012 | u32 device_id = 0; | |
1013 | ||
1014 | if (!dev || !dev->lldd_dev) | |
1015 | return -1; | |
1016 | ||
1017 | pm8001_dev = dev->lldd_dev; | |
1018 | device_id = pm8001_dev->device_id; | |
1019 | pm8001_ha = pm8001_find_ha_by_dev(dev); | |
1020 | ||
1021 | PM8001_EH_DBG(pm8001_ha, | |
1022 | pm8001_printk("I_T_Nexus handler invoked !!")); | |
1023 | ||
1024 | phy = sas_get_local_phy(dev); | |
1025 | ||
1026 | if (dev_is_sata(dev)) { | |
1027 | DECLARE_COMPLETION_ONSTACK(completion_setstate); | |
1028 | if (scsi_is_sas_phy_local(phy)) { | |
1029 | rc = 0; | |
1030 | goto out; | |
1031 | } | |
1032 | /* send internal ssp/sata/smp abort command to FW */ | |
1033 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , | |
1034 | dev, 1, 0); | |
1035 | msleep(100); | |
1036 | ||
1037 | /* deregister the target device */ | |
1038 | pm8001_dev_gone_notify(dev); | |
1039 | msleep(200); | |
1040 | ||
1041 | /*send phy reset to hard reset target */ | |
1042 | rc = sas_phy_reset(phy, 1); | |
1043 | msleep(2000); | |
1044 | pm8001_dev->setds_completion = &completion_setstate; | |
1045 | ||
1046 | wait_for_completion(&completion_setstate); | |
1047 | } else { | |
1048 | /* send internal ssp/sata/smp abort command to FW */ | |
1049 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , | |
1050 | dev, 1, 0); | |
1051 | msleep(100); | |
1052 | ||
1053 | /* deregister the target device */ | |
1054 | pm8001_dev_gone_notify(dev); | |
1055 | msleep(200); | |
1056 | ||
1057 | /*send phy reset to hard reset target */ | |
1058 | rc = sas_phy_reset(phy, 1); | |
1059 | msleep(2000); | |
1060 | } | |
1061 | PM8001_EH_DBG(pm8001_ha, pm8001_printk(" for device[%x]:rc=%d\n", | |
1062 | pm8001_dev->device_id, rc)); | |
1063 | out: | |
1064 | sas_put_local_phy(phy); | |
1065 | ||
1066 | return rc; | |
1067 | } | |
dbf9bfe6 | 1068 | /* mandatory SAM-3, the task reset the specified LUN*/ |
1069 | int pm8001_lu_reset(struct domain_device *dev, u8 *lun) | |
1070 | { | |
1071 | int rc = TMF_RESP_FUNC_FAILED; | |
1072 | struct pm8001_tmf_task tmf_task; | |
1073 | struct pm8001_device *pm8001_dev = dev->lldd_dev; | |
1074 | struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); | |
34a9b81b | 1075 | DECLARE_COMPLETION_ONSTACK(completion_setstate); |
dbf9bfe6 | 1076 | if (dev_is_sata(dev)) { |
f41a0c44 | 1077 | struct sas_phy *phy = sas_get_local_phy(dev); |
dbf9bfe6 | 1078 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev , |
1079 | dev, 1, 0); | |
1080 | rc = sas_phy_reset(phy, 1); | |
f41a0c44 | 1081 | sas_put_local_phy(phy); |
34a9b81b | 1082 | pm8001_dev->setds_completion = &completion_setstate; |
dbf9bfe6 | 1083 | rc = PM8001_CHIP_DISP->set_dev_state_req(pm8001_ha, |
1084 | pm8001_dev, 0x01); | |
34a9b81b | 1085 | wait_for_completion(&completion_setstate); |
dbf9bfe6 | 1086 | } else { |
1087 | tmf_task.tmf = TMF_LU_RESET; | |
1088 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); | |
1089 | } | |
1090 | /* If failed, fall-through I_T_Nexus reset */ | |
1091 | PM8001_EH_DBG(pm8001_ha, pm8001_printk("for device[%x]:rc=%d\n", | |
1092 | pm8001_dev->device_id, rc)); | |
1093 | return rc; | |
1094 | } | |
1095 | ||
1096 | /* optional SAM-3 */ | |
1097 | int pm8001_query_task(struct sas_task *task) | |
1098 | { | |
1099 | u32 tag = 0xdeadbeef; | |
1100 | int i = 0; | |
1101 | struct scsi_lun lun; | |
1102 | struct pm8001_tmf_task tmf_task; | |
1103 | int rc = TMF_RESP_FUNC_FAILED; | |
1104 | if (unlikely(!task || !task->lldd_task || !task->dev)) | |
1105 | return rc; | |
1106 | ||
1107 | if (task->task_proto & SAS_PROTOCOL_SSP) { | |
1108 | struct scsi_cmnd *cmnd = task->uldd_task; | |
1109 | struct domain_device *dev = task->dev; | |
1110 | struct pm8001_hba_info *pm8001_ha = | |
1111 | pm8001_find_ha_by_dev(dev); | |
1112 | ||
1113 | int_to_scsilun(cmnd->device->lun, &lun); | |
1114 | rc = pm8001_find_tag(task, &tag); | |
1115 | if (rc == 0) { | |
1116 | rc = TMF_RESP_FUNC_FAILED; | |
1117 | return rc; | |
1118 | } | |
1119 | PM8001_EH_DBG(pm8001_ha, pm8001_printk("Query:[")); | |
1120 | for (i = 0; i < 16; i++) | |
1121 | printk(KERN_INFO "%02x ", cmnd->cmnd[i]); | |
1122 | printk(KERN_INFO "]\n"); | |
1123 | tmf_task.tmf = TMF_QUERY_TASK; | |
1124 | tmf_task.tag_of_task_to_be_managed = tag; | |
1125 | ||
1126 | rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task); | |
1127 | switch (rc) { | |
1128 | /* The task is still in Lun, release it then */ | |
1129 | case TMF_RESP_FUNC_SUCC: | |
1130 | PM8001_EH_DBG(pm8001_ha, | |
6fbc7692 MS |
1131 | pm8001_printk("The task is still in Lun\n")); |
1132 | break; | |
dbf9bfe6 | 1133 | /* The task is not in Lun or failed, reset the phy */ |
1134 | case TMF_RESP_FUNC_FAILED: | |
1135 | case TMF_RESP_FUNC_COMPLETE: | |
1136 | PM8001_EH_DBG(pm8001_ha, | |
1137 | pm8001_printk("The task is not in Lun or failed," | |
6fbc7692 | 1138 | " reset the phy\n")); |
dbf9bfe6 | 1139 | break; |
1140 | } | |
1141 | } | |
1142 | pm8001_printk(":rc= %d\n", rc); | |
1143 | return rc; | |
1144 | } | |
1145 | ||
1146 | /* mandatory SAM-3, still need free task/ccb info, abord the specified task */ | |
1147 | int pm8001_abort_task(struct sas_task *task) | |
1148 | { | |
1149 | unsigned long flags; | |
1150 | u32 tag = 0xdeadbeef; | |
1151 | u32 device_id; | |
1152 | struct domain_device *dev ; | |
1153 | struct pm8001_hba_info *pm8001_ha = NULL; | |
1154 | struct pm8001_ccb_info *ccb; | |
1155 | struct scsi_lun lun; | |
1156 | struct pm8001_device *pm8001_dev; | |
1157 | struct pm8001_tmf_task tmf_task; | |
1158 | int rc = TMF_RESP_FUNC_FAILED; | |
1159 | if (unlikely(!task || !task->lldd_task || !task->dev)) | |
1160 | return rc; | |
1161 | spin_lock_irqsave(&task->task_state_lock, flags); | |
1162 | if (task->task_state_flags & SAS_TASK_STATE_DONE) { | |
1163 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
1164 | rc = TMF_RESP_FUNC_COMPLETE; | |
1165 | goto out; | |
1166 | } | |
1167 | spin_unlock_irqrestore(&task->task_state_lock, flags); | |
1168 | if (task->task_proto & SAS_PROTOCOL_SSP) { | |
1169 | struct scsi_cmnd *cmnd = task->uldd_task; | |
1170 | dev = task->dev; | |
1171 | ccb = task->lldd_task; | |
1172 | pm8001_dev = dev->lldd_dev; | |
1173 | pm8001_ha = pm8001_find_ha_by_dev(dev); | |
1174 | int_to_scsilun(cmnd->device->lun, &lun); | |
1175 | rc = pm8001_find_tag(task, &tag); | |
1176 | if (rc == 0) { | |
1177 | printk(KERN_INFO "No such tag in %s\n", __func__); | |
1178 | rc = TMF_RESP_FUNC_FAILED; | |
1179 | return rc; | |
1180 | } | |
1181 | device_id = pm8001_dev->device_id; | |
1182 | PM8001_EH_DBG(pm8001_ha, | |
97ee2088 | 1183 | pm8001_printk("abort io to deviceid= %d\n", device_id)); |
1184 | tmf_task.tmf = TMF_ABORT_TASK; | |
dbf9bfe6 | 1185 | tmf_task.tag_of_task_to_be_managed = tag; |
1186 | rc = pm8001_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task); | |
97ee2088 | 1187 | pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, |
dbf9bfe6 | 1188 | pm8001_dev->sas_device, 0, tag); |
1189 | } else if (task->task_proto & SAS_PROTOCOL_SATA || | |
1190 | task->task_proto & SAS_PROTOCOL_STP) { | |
1191 | dev = task->dev; | |
1192 | pm8001_dev = dev->lldd_dev; | |
1193 | pm8001_ha = pm8001_find_ha_by_dev(dev); | |
1194 | rc = pm8001_find_tag(task, &tag); | |
1195 | if (rc == 0) { | |
1196 | printk(KERN_INFO "No such tag in %s\n", __func__); | |
1197 | rc = TMF_RESP_FUNC_FAILED; | |
1198 | return rc; | |
1199 | } | |
1200 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, | |
1201 | pm8001_dev->sas_device, 0, tag); | |
1202 | } else if (task->task_proto & SAS_PROTOCOL_SMP) { | |
1203 | /* SMP */ | |
1204 | dev = task->dev; | |
1205 | pm8001_dev = dev->lldd_dev; | |
1206 | pm8001_ha = pm8001_find_ha_by_dev(dev); | |
1207 | rc = pm8001_find_tag(task, &tag); | |
1208 | if (rc == 0) { | |
1209 | printk(KERN_INFO "No such tag in %s\n", __func__); | |
1210 | rc = TMF_RESP_FUNC_FAILED; | |
1211 | return rc; | |
1212 | } | |
1213 | rc = pm8001_exec_internal_task_abort(pm8001_ha, pm8001_dev, | |
1214 | pm8001_dev->sas_device, 0, tag); | |
1215 | ||
1216 | } | |
1217 | out: | |
1218 | if (rc != TMF_RESP_FUNC_COMPLETE) | |
1219 | pm8001_printk("rc= %d\n", rc); | |
1220 | return rc; | |
1221 | } | |
1222 | ||
1223 | int pm8001_abort_task_set(struct domain_device *dev, u8 *lun) | |
1224 | { | |
1225 | int rc = TMF_RESP_FUNC_FAILED; | |
1226 | struct pm8001_tmf_task tmf_task; | |
1227 | ||
1228 | tmf_task.tmf = TMF_ABORT_TASK_SET; | |
1229 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); | |
1230 | return rc; | |
1231 | } | |
1232 | ||
1233 | int pm8001_clear_aca(struct domain_device *dev, u8 *lun) | |
1234 | { | |
1235 | int rc = TMF_RESP_FUNC_FAILED; | |
1236 | struct pm8001_tmf_task tmf_task; | |
1237 | ||
1238 | tmf_task.tmf = TMF_CLEAR_ACA; | |
1239 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); | |
1240 | ||
1241 | return rc; | |
1242 | } | |
1243 | ||
1244 | int pm8001_clear_task_set(struct domain_device *dev, u8 *lun) | |
1245 | { | |
1246 | int rc = TMF_RESP_FUNC_FAILED; | |
1247 | struct pm8001_tmf_task tmf_task; | |
1248 | struct pm8001_device *pm8001_dev = dev->lldd_dev; | |
1249 | struct pm8001_hba_info *pm8001_ha = pm8001_find_ha_by_dev(dev); | |
1250 | ||
1251 | PM8001_EH_DBG(pm8001_ha, | |
1252 | pm8001_printk("I_T_L_Q clear task set[%x]\n", | |
1253 | pm8001_dev->device_id)); | |
1254 | tmf_task.tmf = TMF_CLEAR_TASK_SET; | |
1255 | rc = pm8001_issue_ssp_tmf(dev, lun, &tmf_task); | |
1256 | return rc; | |
1257 | } | |
1258 |