]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/hisi_sas/hisi_sas_main.c
scsi: hisi_sas: Terminate STP reject quickly for v2 hw
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / hisi_sas / hisi_sas_main.c
1 /*
2 * Copyright (c) 2015 Linaro Ltd.
3 * Copyright (c) 2015 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 */
11
12 #include "hisi_sas.h"
13 #define DRV_NAME "hisi_sas"
14
15 #define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17
18 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
20 static int
21 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
24 static int hisi_sas_softreset_ata_disk(struct domain_device *device);
25 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
26 void *funcdata);
27 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
28 struct domain_device *device);
29 static void hisi_sas_dev_gone(struct domain_device *device);
30
31 u8 hisi_sas_get_ata_protocol(struct host_to_dev_fis *fis, int direction)
32 {
33 switch (fis->command) {
34 case ATA_CMD_FPDMA_WRITE:
35 case ATA_CMD_FPDMA_READ:
36 case ATA_CMD_FPDMA_RECV:
37 case ATA_CMD_FPDMA_SEND:
38 case ATA_CMD_NCQ_NON_DATA:
39 return HISI_SAS_SATA_PROTOCOL_FPDMA;
40
41 case ATA_CMD_DOWNLOAD_MICRO:
42 case ATA_CMD_ID_ATA:
43 case ATA_CMD_PMP_READ:
44 case ATA_CMD_READ_LOG_EXT:
45 case ATA_CMD_PIO_READ:
46 case ATA_CMD_PIO_READ_EXT:
47 case ATA_CMD_PMP_WRITE:
48 case ATA_CMD_WRITE_LOG_EXT:
49 case ATA_CMD_PIO_WRITE:
50 case ATA_CMD_PIO_WRITE_EXT:
51 return HISI_SAS_SATA_PROTOCOL_PIO;
52
53 case ATA_CMD_DSM:
54 case ATA_CMD_DOWNLOAD_MICRO_DMA:
55 case ATA_CMD_PMP_READ_DMA:
56 case ATA_CMD_PMP_WRITE_DMA:
57 case ATA_CMD_READ:
58 case ATA_CMD_READ_EXT:
59 case ATA_CMD_READ_LOG_DMA_EXT:
60 case ATA_CMD_READ_STREAM_DMA_EXT:
61 case ATA_CMD_TRUSTED_RCV_DMA:
62 case ATA_CMD_TRUSTED_SND_DMA:
63 case ATA_CMD_WRITE:
64 case ATA_CMD_WRITE_EXT:
65 case ATA_CMD_WRITE_FUA_EXT:
66 case ATA_CMD_WRITE_QUEUED:
67 case ATA_CMD_WRITE_LOG_DMA_EXT:
68 case ATA_CMD_WRITE_STREAM_DMA_EXT:
69 case ATA_CMD_ZAC_MGMT_IN:
70 return HISI_SAS_SATA_PROTOCOL_DMA;
71
72 case ATA_CMD_CHK_POWER:
73 case ATA_CMD_DEV_RESET:
74 case ATA_CMD_EDD:
75 case ATA_CMD_FLUSH:
76 case ATA_CMD_FLUSH_EXT:
77 case ATA_CMD_VERIFY:
78 case ATA_CMD_VERIFY_EXT:
79 case ATA_CMD_SET_FEATURES:
80 case ATA_CMD_STANDBY:
81 case ATA_CMD_STANDBYNOW1:
82 case ATA_CMD_ZAC_MGMT_OUT:
83 return HISI_SAS_SATA_PROTOCOL_NONDATA;
84 default:
85 {
86 if (fis->command == ATA_CMD_SET_MAX) {
87 switch (fis->features) {
88 case ATA_SET_MAX_PASSWD:
89 case ATA_SET_MAX_LOCK:
90 return HISI_SAS_SATA_PROTOCOL_PIO;
91
92 case ATA_SET_MAX_PASSWD_DMA:
93 case ATA_SET_MAX_UNLOCK_DMA:
94 return HISI_SAS_SATA_PROTOCOL_DMA;
95
96 default:
97 return HISI_SAS_SATA_PROTOCOL_NONDATA;
98 }
99 }
100 if (direction == DMA_NONE)
101 return HISI_SAS_SATA_PROTOCOL_NONDATA;
102 return HISI_SAS_SATA_PROTOCOL_PIO;
103 }
104 }
105 }
106 EXPORT_SYMBOL_GPL(hisi_sas_get_ata_protocol);
107
108 void hisi_sas_sata_done(struct sas_task *task,
109 struct hisi_sas_slot *slot)
110 {
111 struct task_status_struct *ts = &task->task_status;
112 struct ata_task_resp *resp = (struct ata_task_resp *)ts->buf;
113 struct hisi_sas_status_buffer *status_buf =
114 hisi_sas_status_buf_addr_mem(slot);
115 u8 *iu = &status_buf->iu[0];
116 struct dev_to_host_fis *d2h = (struct dev_to_host_fis *)iu;
117
118 resp->frame_len = sizeof(struct dev_to_host_fis);
119 memcpy(&resp->ending_fis[0], d2h, sizeof(struct dev_to_host_fis));
120
121 ts->buf_valid_size = sizeof(*resp);
122 }
123 EXPORT_SYMBOL_GPL(hisi_sas_sata_done);
124
125 int hisi_sas_get_ncq_tag(struct sas_task *task, u32 *tag)
126 {
127 struct ata_queued_cmd *qc = task->uldd_task;
128
129 if (qc) {
130 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
131 qc->tf.command == ATA_CMD_FPDMA_READ) {
132 *tag = qc->tag;
133 return 1;
134 }
135 }
136 return 0;
137 }
138 EXPORT_SYMBOL_GPL(hisi_sas_get_ncq_tag);
139
140 /*
141 * This function assumes linkrate mask fits in 8 bits, which it
142 * does for all HW versions supported.
143 */
144 u8 hisi_sas_get_prog_phy_linkrate_mask(enum sas_linkrate max)
145 {
146 u16 rate = 0;
147 int i;
148
149 max -= SAS_LINK_RATE_1_5_GBPS;
150 for (i = 0; i <= max; i++)
151 rate |= 1 << (i * 2);
152 return rate;
153 }
154 EXPORT_SYMBOL_GPL(hisi_sas_get_prog_phy_linkrate_mask);
155
156 static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
157 {
158 return device->port->ha->lldd_ha;
159 }
160
161 struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
162 {
163 return container_of(sas_port, struct hisi_sas_port, sas_port);
164 }
165 EXPORT_SYMBOL_GPL(to_hisi_sas_port);
166
167 void hisi_sas_stop_phys(struct hisi_hba *hisi_hba)
168 {
169 int phy_no;
170
171 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++)
172 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
173 }
174 EXPORT_SYMBOL_GPL(hisi_sas_stop_phys);
175
176 static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
177 {
178 void *bitmap = hisi_hba->slot_index_tags;
179
180 clear_bit(slot_idx, bitmap);
181 }
182
183 static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
184 {
185 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
186 }
187
188 static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
189 {
190 void *bitmap = hisi_hba->slot_index_tags;
191
192 set_bit(slot_idx, bitmap);
193 }
194
195 static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
196 {
197 unsigned int index;
198 void *bitmap = hisi_hba->slot_index_tags;
199
200 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
201 hisi_hba->last_slot_index + 1);
202 if (index >= hisi_hba->slot_index_count) {
203 index = find_next_zero_bit(bitmap, hisi_hba->slot_index_count,
204 0);
205 if (index >= hisi_hba->slot_index_count)
206 return -SAS_QUEUE_FULL;
207 }
208 hisi_sas_slot_index_set(hisi_hba, index);
209 *slot_idx = index;
210 hisi_hba->last_slot_index = index;
211
212 return 0;
213 }
214
215 static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
216 {
217 int i;
218
219 for (i = 0; i < hisi_hba->slot_index_count; ++i)
220 hisi_sas_slot_index_clear(hisi_hba, i);
221 }
222
223 void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
224 struct hisi_sas_slot *slot)
225 {
226 struct hisi_sas_dq *dq = &hisi_hba->dq[slot->dlvry_queue];
227 unsigned long flags;
228
229 if (task) {
230 struct device *dev = hisi_hba->dev;
231
232 if (!task->lldd_task)
233 return;
234
235 task->lldd_task = NULL;
236
237 if (!sas_protocol_ata(task->task_proto))
238 if (slot->n_elem)
239 dma_unmap_sg(dev, task->scatter,
240 task->num_scatter,
241 task->data_dir);
242 }
243
244 if (slot->buf)
245 dma_pool_free(hisi_hba->buffer_pool, slot->buf, slot->buf_dma);
246
247 spin_lock_irqsave(&dq->lock, flags);
248 list_del_init(&slot->entry);
249 spin_unlock_irqrestore(&dq->lock, flags);
250 slot->buf = NULL;
251 slot->task = NULL;
252 slot->port = NULL;
253 spin_lock_irqsave(&hisi_hba->lock, flags);
254 hisi_sas_slot_index_free(hisi_hba, slot->idx);
255 spin_unlock_irqrestore(&hisi_hba->lock, flags);
256
257 /* slot memory is fully zeroed when it is reused */
258 }
259 EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
260
261 static void hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
262 struct hisi_sas_slot *slot)
263 {
264 hisi_hba->hw->prep_smp(hisi_hba, slot);
265 }
266
267 static void hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
268 struct hisi_sas_slot *slot)
269 {
270 hisi_hba->hw->prep_ssp(hisi_hba, slot);
271 }
272
273 static void hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
274 struct hisi_sas_slot *slot)
275 {
276 hisi_hba->hw->prep_stp(hisi_hba, slot);
277 }
278
279 static void hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
280 struct hisi_sas_slot *slot,
281 int device_id, int abort_flag, int tag_to_abort)
282 {
283 hisi_hba->hw->prep_abort(hisi_hba, slot,
284 device_id, abort_flag, tag_to_abort);
285 }
286
287 /*
288 * This function will issue an abort TMF regardless of whether the
289 * task is in the sdev or not. Then it will do the task complete
290 * cleanup and callbacks.
291 */
292 static void hisi_sas_slot_abort(struct work_struct *work)
293 {
294 struct hisi_sas_slot *abort_slot =
295 container_of(work, struct hisi_sas_slot, abort_slot);
296 struct sas_task *task = abort_slot->task;
297 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
298 struct scsi_cmnd *cmnd = task->uldd_task;
299 struct hisi_sas_tmf_task tmf_task;
300 struct scsi_lun lun;
301 struct device *dev = hisi_hba->dev;
302 int tag = abort_slot->idx;
303
304 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
305 dev_err(dev, "cannot abort slot for non-ssp task\n");
306 goto out;
307 }
308
309 int_to_scsilun(cmnd->device->lun, &lun);
310 tmf_task.tmf = TMF_ABORT_TASK;
311 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
312
313 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
314 out:
315 /* Do cleanup for this task */
316 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
317 if (task->task_done)
318 task->task_done(task);
319 }
320
321 static int hisi_sas_task_prep(struct sas_task *task,
322 struct hisi_sas_dq **dq_pointer,
323 bool is_tmf, struct hisi_sas_tmf_task *tmf,
324 int *pass)
325 {
326 struct domain_device *device = task->dev;
327 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
328 struct hisi_sas_device *sas_dev = device->lldd_dev;
329 struct hisi_sas_port *port;
330 struct hisi_sas_slot *slot;
331 struct hisi_sas_cmd_hdr *cmd_hdr_base;
332 struct asd_sas_port *sas_port = device->port;
333 struct device *dev = hisi_hba->dev;
334 int dlvry_queue_slot, dlvry_queue, rc, slot_idx;
335 int n_elem = 0, n_elem_req = 0, n_elem_resp = 0;
336 unsigned long flags, flags_dq;
337 struct hisi_sas_dq *dq;
338 int wr_q_index;
339
340 if (!sas_port) {
341 struct task_status_struct *ts = &task->task_status;
342
343 ts->resp = SAS_TASK_UNDELIVERED;
344 ts->stat = SAS_PHY_DOWN;
345 /*
346 * libsas will use dev->port, should
347 * not call task_done for sata
348 */
349 if (device->dev_type != SAS_SATA_DEV)
350 task->task_done(task);
351 return -ECOMM;
352 }
353
354 if (DEV_IS_GONE(sas_dev)) {
355 if (sas_dev)
356 dev_info(dev, "task prep: device %d not ready\n",
357 sas_dev->device_id);
358 else
359 dev_info(dev, "task prep: device %016llx not ready\n",
360 SAS_ADDR(device->sas_addr));
361
362 return -ECOMM;
363 }
364
365 *dq_pointer = dq = sas_dev->dq;
366
367 port = to_hisi_sas_port(sas_port);
368 if (port && !port->port_attached) {
369 dev_info(dev, "task prep: %s port%d not attach device\n",
370 (dev_is_sata(device)) ?
371 "SATA/STP" : "SAS",
372 device->port->id);
373
374 return -ECOMM;
375 }
376
377 if (!sas_protocol_ata(task->task_proto)) {
378 unsigned int req_len, resp_len;
379
380 if (task->num_scatter) {
381 n_elem = dma_map_sg(dev, task->scatter,
382 task->num_scatter, task->data_dir);
383 if (!n_elem) {
384 rc = -ENOMEM;
385 goto prep_out;
386 }
387 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
388 n_elem_req = dma_map_sg(dev, &task->smp_task.smp_req,
389 1, DMA_TO_DEVICE);
390 if (!n_elem_req) {
391 rc = -ENOMEM;
392 goto prep_out;
393 }
394 req_len = sg_dma_len(&task->smp_task.smp_req);
395 if (req_len & 0x3) {
396 rc = -EINVAL;
397 goto err_out_dma_unmap;
398 }
399 n_elem_resp = dma_map_sg(dev, &task->smp_task.smp_resp,
400 1, DMA_FROM_DEVICE);
401 if (!n_elem_resp) {
402 rc = -ENOMEM;
403 goto err_out_dma_unmap;
404 }
405 resp_len = sg_dma_len(&task->smp_task.smp_resp);
406 if (resp_len & 0x3) {
407 rc = -EINVAL;
408 goto err_out_dma_unmap;
409 }
410 }
411 } else
412 n_elem = task->num_scatter;
413
414 if (n_elem > HISI_SAS_SGE_PAGE_CNT) {
415 dev_err(dev, "task prep: n_elem(%d) > HISI_SAS_SGE_PAGE_CNT",
416 n_elem);
417 rc = -EINVAL;
418 goto err_out_dma_unmap;
419 }
420
421 spin_lock_irqsave(&hisi_hba->lock, flags);
422 if (hisi_hba->hw->slot_index_alloc)
423 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
424 device);
425 else
426 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
427 spin_unlock_irqrestore(&hisi_hba->lock, flags);
428 if (rc)
429 goto err_out_dma_unmap;
430
431 slot = &hisi_hba->slot_info[slot_idx];
432 memset(slot, 0, sizeof(struct hisi_sas_slot));
433
434 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
435 GFP_ATOMIC, &slot->buf_dma);
436 if (!slot->buf) {
437 rc = -ENOMEM;
438 goto err_out_tag;
439 }
440
441 spin_lock_irqsave(&dq->lock, flags_dq);
442 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
443 if (wr_q_index < 0) {
444 spin_unlock_irqrestore(&dq->lock, flags_dq);
445 goto err_out_buf;
446 }
447
448 list_add_tail(&slot->delivery, &dq->list);
449 spin_unlock_irqrestore(&dq->lock, flags_dq);
450
451 dlvry_queue = dq->id;
452 dlvry_queue_slot = wr_q_index;
453
454 slot->idx = slot_idx;
455 slot->n_elem = n_elem;
456 slot->dlvry_queue = dlvry_queue;
457 slot->dlvry_queue_slot = dlvry_queue_slot;
458 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
459 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
460 slot->task = task;
461 slot->port = port;
462 slot->tmf = tmf;
463 slot->is_internal = is_tmf;
464 task->lldd_task = slot;
465 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
466
467 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
468 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
469 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
470
471 switch (task->task_proto) {
472 case SAS_PROTOCOL_SMP:
473 hisi_sas_task_prep_smp(hisi_hba, slot);
474 break;
475 case SAS_PROTOCOL_SSP:
476 hisi_sas_task_prep_ssp(hisi_hba, slot);
477 break;
478 case SAS_PROTOCOL_SATA:
479 case SAS_PROTOCOL_STP:
480 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
481 hisi_sas_task_prep_ata(hisi_hba, slot);
482 break;
483 default:
484 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
485 task->task_proto);
486 break;
487 }
488
489 spin_lock_irqsave(&dq->lock, flags);
490 list_add_tail(&slot->entry, &sas_dev->list);
491 spin_unlock_irqrestore(&dq->lock, flags);
492 spin_lock_irqsave(&task->task_state_lock, flags);
493 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
494 spin_unlock_irqrestore(&task->task_state_lock, flags);
495
496 ++(*pass);
497 slot->ready = 1;
498
499 return 0;
500
501 err_out_buf:
502 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
503 slot->buf_dma);
504 err_out_tag:
505 spin_lock_irqsave(&hisi_hba->lock, flags);
506 hisi_sas_slot_index_free(hisi_hba, slot_idx);
507 spin_unlock_irqrestore(&hisi_hba->lock, flags);
508 err_out_dma_unmap:
509 if (!sas_protocol_ata(task->task_proto)) {
510 if (task->num_scatter) {
511 dma_unmap_sg(dev, task->scatter, task->num_scatter,
512 task->data_dir);
513 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
514 if (n_elem_req)
515 dma_unmap_sg(dev, &task->smp_task.smp_req,
516 1, DMA_TO_DEVICE);
517 if (n_elem_resp)
518 dma_unmap_sg(dev, &task->smp_task.smp_resp,
519 1, DMA_FROM_DEVICE);
520 }
521 }
522 prep_out:
523 dev_err(dev, "task prep: failed[%d]!\n", rc);
524 return rc;
525 }
526
527 static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
528 bool is_tmf, struct hisi_sas_tmf_task *tmf)
529 {
530 u32 rc;
531 u32 pass = 0;
532 unsigned long flags;
533 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
534 struct device *dev = hisi_hba->dev;
535 struct hisi_sas_dq *dq = NULL;
536
537 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
538 return -EINVAL;
539
540 /* protect task_prep and start_delivery sequence */
541 rc = hisi_sas_task_prep(task, &dq, is_tmf, tmf, &pass);
542 if (rc)
543 dev_err(dev, "task exec: failed[%d]!\n", rc);
544
545 if (likely(pass)) {
546 spin_lock_irqsave(&dq->lock, flags);
547 hisi_hba->hw->start_delivery(dq);
548 spin_unlock_irqrestore(&dq->lock, flags);
549 }
550
551 return rc;
552 }
553
554 static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
555 {
556 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
557 struct asd_sas_phy *sas_phy = &phy->sas_phy;
558 struct sas_ha_struct *sas_ha;
559
560 if (!phy->phy_attached)
561 return;
562
563 sas_ha = &hisi_hba->sha;
564 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
565
566 if (sas_phy->phy) {
567 struct sas_phy *sphy = sas_phy->phy;
568
569 sphy->negotiated_linkrate = sas_phy->linkrate;
570 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
571 sphy->maximum_linkrate_hw =
572 hisi_hba->hw->phy_get_max_linkrate();
573 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
574 sphy->minimum_linkrate = phy->minimum_linkrate;
575
576 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
577 sphy->maximum_linkrate = phy->maximum_linkrate;
578 }
579
580 if (phy->phy_type & PORT_TYPE_SAS) {
581 struct sas_identify_frame *id;
582
583 id = (struct sas_identify_frame *)phy->frame_rcvd;
584 id->dev_type = phy->identify.device_type;
585 id->initiator_bits = SAS_PROTOCOL_ALL;
586 id->target_bits = phy->identify.target_port_protocols;
587 } else if (phy->phy_type & PORT_TYPE_SATA) {
588 /*Nothing*/
589 }
590
591 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
592 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
593 }
594
595 static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
596 {
597 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
598 struct hisi_sas_device *sas_dev = NULL;
599 unsigned long flags;
600 int last = hisi_hba->last_dev_id;
601 int first = (hisi_hba->last_dev_id + 1) % HISI_SAS_MAX_DEVICES;
602 int i;
603
604 spin_lock_irqsave(&hisi_hba->lock, flags);
605 for (i = first; i != last; i %= HISI_SAS_MAX_DEVICES) {
606 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
607 int queue = i % hisi_hba->queue_count;
608 struct hisi_sas_dq *dq = &hisi_hba->dq[queue];
609
610 hisi_hba->devices[i].device_id = i;
611 sas_dev = &hisi_hba->devices[i];
612 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
613 sas_dev->dev_type = device->dev_type;
614 sas_dev->hisi_hba = hisi_hba;
615 sas_dev->sas_device = device;
616 sas_dev->dq = dq;
617 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
618 break;
619 }
620 i++;
621 }
622 hisi_hba->last_dev_id = i;
623 spin_unlock_irqrestore(&hisi_hba->lock, flags);
624
625 return sas_dev;
626 }
627
628 #define HISI_SAS_SRST_ATA_DISK_CNT 3
629 static int hisi_sas_init_device(struct domain_device *device)
630 {
631 int rc = TMF_RESP_FUNC_COMPLETE;
632 struct scsi_lun lun;
633 struct hisi_sas_tmf_task tmf_task;
634 int retry = HISI_SAS_SRST_ATA_DISK_CNT;
635 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
636
637 switch (device->dev_type) {
638 case SAS_END_DEVICE:
639 int_to_scsilun(0, &lun);
640
641 tmf_task.tmf = TMF_CLEAR_TASK_SET;
642 rc = hisi_sas_debug_issue_ssp_tmf(device, lun.scsi_lun,
643 &tmf_task);
644 if (rc == TMF_RESP_FUNC_COMPLETE)
645 hisi_sas_release_task(hisi_hba, device);
646 break;
647 case SAS_SATA_DEV:
648 case SAS_SATA_PM:
649 case SAS_SATA_PM_PORT:
650 case SAS_SATA_PENDING:
651 while (retry-- > 0) {
652 rc = hisi_sas_softreset_ata_disk(device);
653 if (!rc)
654 break;
655 }
656 break;
657 default:
658 break;
659 }
660
661 return rc;
662 }
663
664 static int hisi_sas_dev_found(struct domain_device *device)
665 {
666 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
667 struct domain_device *parent_dev = device->parent;
668 struct hisi_sas_device *sas_dev;
669 struct device *dev = hisi_hba->dev;
670 int rc;
671
672 if (hisi_hba->hw->alloc_dev)
673 sas_dev = hisi_hba->hw->alloc_dev(device);
674 else
675 sas_dev = hisi_sas_alloc_dev(device);
676 if (!sas_dev) {
677 dev_err(dev, "fail alloc dev: max support %d devices\n",
678 HISI_SAS_MAX_DEVICES);
679 return -EINVAL;
680 }
681
682 device->lldd_dev = sas_dev;
683 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
684
685 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
686 int phy_no;
687 u8 phy_num = parent_dev->ex_dev.num_phys;
688 struct ex_phy *phy;
689
690 for (phy_no = 0; phy_no < phy_num; phy_no++) {
691 phy = &parent_dev->ex_dev.ex_phy[phy_no];
692 if (SAS_ADDR(phy->attached_sas_addr) ==
693 SAS_ADDR(device->sas_addr)) {
694 sas_dev->attached_phy = phy_no;
695 break;
696 }
697 }
698
699 if (phy_no == phy_num) {
700 dev_info(dev, "dev found: no attached "
701 "dev:%016llx at ex:%016llx\n",
702 SAS_ADDR(device->sas_addr),
703 SAS_ADDR(parent_dev->sas_addr));
704 rc = -EINVAL;
705 goto err_out;
706 }
707 }
708
709 dev_info(dev, "dev[%d:%x] found\n",
710 sas_dev->device_id, sas_dev->dev_type);
711
712 rc = hisi_sas_init_device(device);
713 if (rc)
714 goto err_out;
715 return 0;
716
717 err_out:
718 hisi_sas_dev_gone(device);
719 return rc;
720 }
721
722 int hisi_sas_slave_configure(struct scsi_device *sdev)
723 {
724 struct domain_device *dev = sdev_to_domain_dev(sdev);
725 int ret = sas_slave_configure(sdev);
726
727 if (ret)
728 return ret;
729 if (!dev_is_sata(dev))
730 sas_change_queue_depth(sdev, 64);
731
732 return 0;
733 }
734 EXPORT_SYMBOL_GPL(hisi_sas_slave_configure);
735
736 void hisi_sas_scan_start(struct Scsi_Host *shost)
737 {
738 struct hisi_hba *hisi_hba = shost_priv(shost);
739
740 hisi_hba->hw->phys_init(hisi_hba);
741 }
742 EXPORT_SYMBOL_GPL(hisi_sas_scan_start);
743
744 int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
745 {
746 struct hisi_hba *hisi_hba = shost_priv(shost);
747 struct sas_ha_struct *sha = &hisi_hba->sha;
748
749 /* Wait for PHY up interrupt to occur */
750 if (time < HZ)
751 return 0;
752
753 sas_drain_work(sha);
754 return 1;
755 }
756 EXPORT_SYMBOL_GPL(hisi_sas_scan_finished);
757
758 static void hisi_sas_phyup_work(struct work_struct *work)
759 {
760 struct hisi_sas_phy *phy =
761 container_of(work, typeof(*phy), works[HISI_PHYE_PHY_UP]);
762 struct hisi_hba *hisi_hba = phy->hisi_hba;
763 struct asd_sas_phy *sas_phy = &phy->sas_phy;
764 int phy_no = sas_phy->id;
765
766 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
767 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
768 }
769
770 static void hisi_sas_linkreset_work(struct work_struct *work)
771 {
772 struct hisi_sas_phy *phy =
773 container_of(work, typeof(*phy), works[HISI_PHYE_LINK_RESET]);
774 struct asd_sas_phy *sas_phy = &phy->sas_phy;
775
776 hisi_sas_control_phy(sas_phy, PHY_FUNC_LINK_RESET, NULL);
777 }
778
779 static const work_func_t hisi_sas_phye_fns[HISI_PHYES_NUM] = {
780 [HISI_PHYE_PHY_UP] = hisi_sas_phyup_work,
781 [HISI_PHYE_LINK_RESET] = hisi_sas_linkreset_work,
782 };
783
784 bool hisi_sas_notify_phy_event(struct hisi_sas_phy *phy,
785 enum hisi_sas_phy_event event)
786 {
787 struct hisi_hba *hisi_hba = phy->hisi_hba;
788
789 if (WARN_ON(event >= HISI_PHYES_NUM))
790 return false;
791
792 return queue_work(hisi_hba->wq, &phy->works[event]);
793 }
794 EXPORT_SYMBOL_GPL(hisi_sas_notify_phy_event);
795
796 static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
797 {
798 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
799 struct asd_sas_phy *sas_phy = &phy->sas_phy;
800 int i;
801
802 phy->hisi_hba = hisi_hba;
803 phy->port = NULL;
804 phy->minimum_linkrate = SAS_LINK_RATE_1_5_GBPS;
805 phy->maximum_linkrate = hisi_hba->hw->phy_get_max_linkrate();
806 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
807 sas_phy->class = SAS;
808 sas_phy->iproto = SAS_PROTOCOL_ALL;
809 sas_phy->tproto = 0;
810 sas_phy->type = PHY_TYPE_PHYSICAL;
811 sas_phy->role = PHY_ROLE_INITIATOR;
812 sas_phy->oob_mode = OOB_NOT_CONNECTED;
813 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
814 sas_phy->id = phy_no;
815 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
816 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
817 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
818 sas_phy->lldd_phy = phy;
819
820 for (i = 0; i < HISI_PHYES_NUM; i++)
821 INIT_WORK(&phy->works[i], hisi_sas_phye_fns[i]);
822 }
823
824 static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
825 {
826 struct sas_ha_struct *sas_ha = sas_phy->ha;
827 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
828 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
829 struct asd_sas_port *sas_port = sas_phy->port;
830 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
831 unsigned long flags;
832
833 if (!sas_port)
834 return;
835
836 spin_lock_irqsave(&hisi_hba->lock, flags);
837 port->port_attached = 1;
838 port->id = phy->port_id;
839 phy->port = port;
840 sas_port->lldd_port = port;
841 spin_unlock_irqrestore(&hisi_hba->lock, flags);
842 }
843
844 static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
845 struct hisi_sas_slot *slot)
846 {
847 if (task) {
848 unsigned long flags;
849 struct task_status_struct *ts;
850
851 ts = &task->task_status;
852
853 ts->resp = SAS_TASK_COMPLETE;
854 ts->stat = SAS_ABORTED_TASK;
855 spin_lock_irqsave(&task->task_state_lock, flags);
856 task->task_state_flags &=
857 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
858 task->task_state_flags |= SAS_TASK_STATE_DONE;
859 spin_unlock_irqrestore(&task->task_state_lock, flags);
860 }
861
862 hisi_sas_slot_task_free(hisi_hba, task, slot);
863 }
864
865 /* hisi_hba.lock should be locked */
866 static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
867 struct domain_device *device)
868 {
869 struct hisi_sas_slot *slot, *slot2;
870 struct hisi_sas_device *sas_dev = device->lldd_dev;
871
872 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
873 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
874 }
875
876 void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
877 {
878 struct hisi_sas_device *sas_dev;
879 struct domain_device *device;
880 int i;
881
882 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
883 sas_dev = &hisi_hba->devices[i];
884 device = sas_dev->sas_device;
885
886 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
887 !device)
888 continue;
889
890 hisi_sas_release_task(hisi_hba, device);
891 }
892 }
893 EXPORT_SYMBOL_GPL(hisi_sas_release_tasks);
894
895 static void hisi_sas_dereg_device(struct hisi_hba *hisi_hba,
896 struct domain_device *device)
897 {
898 if (hisi_hba->hw->dereg_device)
899 hisi_hba->hw->dereg_device(hisi_hba, device);
900 }
901
902 static void hisi_sas_dev_gone(struct domain_device *device)
903 {
904 struct hisi_sas_device *sas_dev = device->lldd_dev;
905 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
906 struct device *dev = hisi_hba->dev;
907
908 dev_info(dev, "dev[%d:%x] is gone\n",
909 sas_dev->device_id, sas_dev->dev_type);
910
911 if (!test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
912 hisi_sas_internal_task_abort(hisi_hba, device,
913 HISI_SAS_INT_ABT_DEV, 0);
914
915 hisi_sas_dereg_device(hisi_hba, device);
916
917 hisi_hba->hw->clear_itct(hisi_hba, sas_dev);
918 device->lldd_dev = NULL;
919 }
920
921 if (hisi_hba->hw->free_device)
922 hisi_hba->hw->free_device(sas_dev);
923 sas_dev->dev_type = SAS_PHY_UNUSED;
924 }
925
926 static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
927 {
928 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
929 }
930
931 static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
932 void *funcdata)
933 {
934 struct sas_ha_struct *sas_ha = sas_phy->ha;
935 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
936 int phy_no = sas_phy->id;
937
938 switch (func) {
939 case PHY_FUNC_HARD_RESET:
940 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
941 break;
942
943 case PHY_FUNC_LINK_RESET:
944 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
945 msleep(100);
946 hisi_hba->hw->phy_start(hisi_hba, phy_no);
947 break;
948
949 case PHY_FUNC_DISABLE:
950 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
951 break;
952
953 case PHY_FUNC_SET_LINK_RATE:
954 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
955 break;
956 case PHY_FUNC_GET_EVENTS:
957 if (hisi_hba->hw->get_events) {
958 hisi_hba->hw->get_events(hisi_hba, phy_no);
959 break;
960 }
961 /* fallthru */
962 case PHY_FUNC_RELEASE_SPINUP_HOLD:
963 default:
964 return -EOPNOTSUPP;
965 }
966 return 0;
967 }
968
969 static void hisi_sas_task_done(struct sas_task *task)
970 {
971 if (!del_timer(&task->slow_task->timer))
972 return;
973 complete(&task->slow_task->completion);
974 }
975
976 static void hisi_sas_tmf_timedout(struct timer_list *t)
977 {
978 struct sas_task_slow *slow = from_timer(slow, t, timer);
979 struct sas_task *task = slow->task;
980 unsigned long flags;
981
982 spin_lock_irqsave(&task->task_state_lock, flags);
983 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
984 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
985 spin_unlock_irqrestore(&task->task_state_lock, flags);
986
987 complete(&task->slow_task->completion);
988 }
989
990 #define TASK_TIMEOUT 20
991 #define TASK_RETRY 3
992 #define INTERNAL_ABORT_TIMEOUT 6
993 static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
994 void *parameter, u32 para_len,
995 struct hisi_sas_tmf_task *tmf)
996 {
997 struct hisi_sas_device *sas_dev = device->lldd_dev;
998 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
999 struct device *dev = hisi_hba->dev;
1000 struct sas_task *task;
1001 int res, retry;
1002
1003 for (retry = 0; retry < TASK_RETRY; retry++) {
1004 task = sas_alloc_slow_task(GFP_KERNEL);
1005 if (!task)
1006 return -ENOMEM;
1007
1008 task->dev = device;
1009 task->task_proto = device->tproto;
1010
1011 if (dev_is_sata(device)) {
1012 task->ata_task.device_control_reg_update = 1;
1013 memcpy(&task->ata_task.fis, parameter, para_len);
1014 } else {
1015 memcpy(&task->ssp_task, parameter, para_len);
1016 }
1017 task->task_done = hisi_sas_task_done;
1018
1019 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1020 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
1021 add_timer(&task->slow_task->timer);
1022
1023 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
1024
1025 if (res) {
1026 del_timer(&task->slow_task->timer);
1027 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
1028 res);
1029 goto ex_err;
1030 }
1031
1032 wait_for_completion(&task->slow_task->completion);
1033 res = TMF_RESP_FUNC_FAILED;
1034 /* Even TMF timed out, return direct. */
1035 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1036 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1037 struct hisi_sas_slot *slot = task->lldd_task;
1038
1039 dev_err(dev, "abort tmf: TMF task timeout and not done\n");
1040 if (slot)
1041 slot->task = NULL;
1042
1043 goto ex_err;
1044 } else
1045 dev_err(dev, "abort tmf: TMF task timeout\n");
1046 }
1047
1048 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1049 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1050 res = TMF_RESP_FUNC_COMPLETE;
1051 break;
1052 }
1053
1054 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1055 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1056 res = TMF_RESP_FUNC_SUCC;
1057 break;
1058 }
1059
1060 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1061 task->task_status.stat == SAS_DATA_UNDERRUN) {
1062 /* no error, but return the number of bytes of
1063 * underrun
1064 */
1065 dev_warn(dev, "abort tmf: task to dev %016llx "
1066 "resp: 0x%x sts 0x%x underrun\n",
1067 SAS_ADDR(device->sas_addr),
1068 task->task_status.resp,
1069 task->task_status.stat);
1070 res = task->task_status.residual;
1071 break;
1072 }
1073
1074 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1075 task->task_status.stat == SAS_DATA_OVERRUN) {
1076 dev_warn(dev, "abort tmf: blocked task error\n");
1077 res = -EMSGSIZE;
1078 break;
1079 }
1080
1081 dev_warn(dev, "abort tmf: task to dev "
1082 "%016llx resp: 0x%x status 0x%x\n",
1083 SAS_ADDR(device->sas_addr), task->task_status.resp,
1084 task->task_status.stat);
1085 sas_free_task(task);
1086 task = NULL;
1087 }
1088 ex_err:
1089 if (retry == TASK_RETRY)
1090 dev_warn(dev, "abort tmf: executing internal task failed!\n");
1091 sas_free_task(task);
1092 return res;
1093 }
1094
1095 static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
1096 bool reset, int pmp, u8 *fis)
1097 {
1098 struct ata_taskfile tf;
1099
1100 ata_tf_init(dev, &tf);
1101 if (reset)
1102 tf.ctl |= ATA_SRST;
1103 else
1104 tf.ctl &= ~ATA_SRST;
1105 tf.command = ATA_CMD_DEV_RESET;
1106 ata_tf_to_fis(&tf, pmp, 0, fis);
1107 }
1108
1109 static int hisi_sas_softreset_ata_disk(struct domain_device *device)
1110 {
1111 u8 fis[20] = {0};
1112 struct ata_port *ap = device->sata_dev.ap;
1113 struct ata_link *link;
1114 int rc = TMF_RESP_FUNC_FAILED;
1115 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1116 struct device *dev = hisi_hba->dev;
1117 int s = sizeof(struct host_to_dev_fis);
1118
1119 ata_for_each_link(link, ap, EDGE) {
1120 int pmp = sata_srst_pmp(link);
1121
1122 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1123 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
1124 if (rc != TMF_RESP_FUNC_COMPLETE)
1125 break;
1126 }
1127
1128 if (rc == TMF_RESP_FUNC_COMPLETE) {
1129 ata_for_each_link(link, ap, EDGE) {
1130 int pmp = sata_srst_pmp(link);
1131
1132 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
1133 rc = hisi_sas_exec_internal_tmf_task(device, fis,
1134 s, NULL);
1135 if (rc != TMF_RESP_FUNC_COMPLETE)
1136 dev_err(dev, "ata disk de-reset failed\n");
1137 }
1138 } else {
1139 dev_err(dev, "ata disk reset failed\n");
1140 }
1141
1142 if (rc == TMF_RESP_FUNC_COMPLETE)
1143 hisi_sas_release_task(hisi_hba, device);
1144
1145 return rc;
1146 }
1147
1148 static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
1149 u8 *lun, struct hisi_sas_tmf_task *tmf)
1150 {
1151 struct sas_ssp_task ssp_task;
1152
1153 if (!(device->tproto & SAS_PROTOCOL_SSP))
1154 return TMF_RESP_FUNC_ESUPP;
1155
1156 memcpy(ssp_task.LUN, lun, 8);
1157
1158 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
1159 sizeof(ssp_task), tmf);
1160 }
1161
1162 static void hisi_sas_refresh_port_id(struct hisi_hba *hisi_hba)
1163 {
1164 u32 state = hisi_hba->hw->get_phys_state(hisi_hba);
1165 int i;
1166
1167 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1168 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1169 struct domain_device *device = sas_dev->sas_device;
1170 struct asd_sas_port *sas_port;
1171 struct hisi_sas_port *port;
1172 struct hisi_sas_phy *phy = NULL;
1173 struct asd_sas_phy *sas_phy;
1174
1175 if ((sas_dev->dev_type == SAS_PHY_UNUSED)
1176 || !device || !device->port)
1177 continue;
1178
1179 sas_port = device->port;
1180 port = to_hisi_sas_port(sas_port);
1181
1182 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el)
1183 if (state & BIT(sas_phy->id)) {
1184 phy = sas_phy->lldd_phy;
1185 break;
1186 }
1187
1188 if (phy) {
1189 port->id = phy->port_id;
1190
1191 /* Update linkrate of directly attached device. */
1192 if (!device->parent)
1193 device->linkrate = phy->sas_phy.linkrate;
1194
1195 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
1196 } else
1197 port->id = 0xff;
1198 }
1199 }
1200
1201 static void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1202 u32 state)
1203 {
1204 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1205 struct asd_sas_port *_sas_port = NULL;
1206 int phy_no;
1207
1208 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1209 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1210 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1211 struct asd_sas_port *sas_port = sas_phy->port;
1212 bool do_port_check = !!(_sas_port != sas_port);
1213
1214 if (!sas_phy->phy->enabled)
1215 continue;
1216
1217 /* Report PHY state change to libsas */
1218 if (state & BIT(phy_no)) {
1219 if (do_port_check && sas_port && sas_port->port_dev) {
1220 struct domain_device *dev = sas_port->port_dev;
1221
1222 _sas_port = sas_port;
1223
1224 if (DEV_IS_EXPANDER(dev->dev_type))
1225 sas_ha->notify_port_event(sas_phy,
1226 PORTE_BROADCAST_RCVD);
1227 }
1228 } else if (old_state & (1 << phy_no))
1229 /* PHY down but was up before */
1230 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1231
1232 }
1233 }
1234
1235 static void hisi_sas_reset_init_all_devices(struct hisi_hba *hisi_hba)
1236 {
1237 struct hisi_sas_device *sas_dev;
1238 struct domain_device *device;
1239 int i;
1240
1241 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1242 sas_dev = &hisi_hba->devices[i];
1243 device = sas_dev->sas_device;
1244
1245 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1246 continue;
1247
1248 hisi_sas_init_device(device);
1249 }
1250 }
1251
1252 static void hisi_sas_send_ata_reset_each_phy(struct hisi_hba *hisi_hba,
1253 struct asd_sas_port *sas_port,
1254 struct domain_device *device)
1255 {
1256 struct hisi_sas_tmf_task tmf_task = { .force_phy = 1 };
1257 struct ata_port *ap = device->sata_dev.ap;
1258 struct device *dev = hisi_hba->dev;
1259 int s = sizeof(struct host_to_dev_fis);
1260 int rc = TMF_RESP_FUNC_FAILED;
1261 struct asd_sas_phy *sas_phy;
1262 struct ata_link *link;
1263 u8 fis[20] = {0};
1264 u32 state;
1265
1266 state = hisi_hba->hw->get_phys_state(hisi_hba);
1267 list_for_each_entry(sas_phy, &sas_port->phy_list, port_phy_el) {
1268 if (!(state & BIT(sas_phy->id)))
1269 continue;
1270
1271 ata_for_each_link(link, ap, EDGE) {
1272 int pmp = sata_srst_pmp(link);
1273
1274 tmf_task.phy_id = sas_phy->id;
1275 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
1276 rc = hisi_sas_exec_internal_tmf_task(device, fis, s,
1277 &tmf_task);
1278 if (rc != TMF_RESP_FUNC_COMPLETE) {
1279 dev_err(dev, "phy%d ata reset failed rc=%d\n",
1280 sas_phy->id, rc);
1281 break;
1282 }
1283 }
1284 }
1285 }
1286
1287 static void hisi_sas_terminate_stp_reject(struct hisi_hba *hisi_hba)
1288 {
1289 struct device *dev = hisi_hba->dev;
1290 int port_no, rc, i;
1291
1292 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1293 struct hisi_sas_device *sas_dev = &hisi_hba->devices[i];
1294 struct domain_device *device = sas_dev->sas_device;
1295
1296 if ((sas_dev->dev_type == SAS_PHY_UNUSED) || !device)
1297 continue;
1298
1299 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1300 HISI_SAS_INT_ABT_DEV, 0);
1301 if (rc < 0)
1302 dev_err(dev, "STP reject: abort dev failed %d\n", rc);
1303 }
1304
1305 for (port_no = 0; port_no < hisi_hba->n_phy; port_no++) {
1306 struct hisi_sas_port *port = &hisi_hba->port[port_no];
1307 struct asd_sas_port *sas_port = &port->sas_port;
1308 struct domain_device *port_dev = sas_port->port_dev;
1309 struct domain_device *device;
1310
1311 if (!port_dev || !DEV_IS_EXPANDER(port_dev->dev_type))
1312 continue;
1313
1314 /* Try to find a SATA device */
1315 list_for_each_entry(device, &sas_port->dev_list,
1316 dev_list_node) {
1317 if (dev_is_sata(device)) {
1318 hisi_sas_send_ata_reset_each_phy(hisi_hba,
1319 sas_port,
1320 device);
1321 break;
1322 }
1323 }
1324 }
1325 }
1326
1327 static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
1328 {
1329 struct device *dev = hisi_hba->dev;
1330 struct Scsi_Host *shost = hisi_hba->shost;
1331 u32 old_state, state;
1332 int rc;
1333
1334 if (!hisi_hba->hw->soft_reset)
1335 return -1;
1336
1337 if (test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags))
1338 return -1;
1339
1340 dev_info(dev, "controller resetting...\n");
1341 old_state = hisi_hba->hw->get_phys_state(hisi_hba);
1342
1343 scsi_block_requests(shost);
1344 hisi_hba->hw->wait_cmds_complete_timeout(hisi_hba, 100, 5000);
1345
1346 if (timer_pending(&hisi_hba->timer))
1347 del_timer_sync(&hisi_hba->timer);
1348
1349 set_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1350 rc = hisi_hba->hw->soft_reset(hisi_hba);
1351 if (rc) {
1352 dev_warn(dev, "controller reset failed (%d)\n", rc);
1353 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1354 scsi_unblock_requests(shost);
1355 goto out;
1356 }
1357
1358 clear_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags);
1359
1360 /* Init and wait for PHYs to come up and all libsas event finished. */
1361 hisi_hba->hw->phys_init(hisi_hba);
1362 msleep(1000);
1363 hisi_sas_refresh_port_id(hisi_hba);
1364
1365 if (hisi_hba->reject_stp_links_msk)
1366 hisi_sas_terminate_stp_reject(hisi_hba);
1367 hisi_sas_reset_init_all_devices(hisi_hba);
1368 scsi_unblock_requests(shost);
1369
1370 state = hisi_hba->hw->get_phys_state(hisi_hba);
1371 hisi_sas_rescan_topology(hisi_hba, old_state, state);
1372 dev_info(dev, "controller reset complete\n");
1373
1374 out:
1375 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
1376
1377 return rc;
1378 }
1379
1380 static int hisi_sas_abort_task(struct sas_task *task)
1381 {
1382 struct scsi_lun lun;
1383 struct hisi_sas_tmf_task tmf_task;
1384 struct domain_device *device = task->dev;
1385 struct hisi_sas_device *sas_dev = device->lldd_dev;
1386 struct hisi_hba *hisi_hba;
1387 struct device *dev;
1388 int rc = TMF_RESP_FUNC_FAILED;
1389 unsigned long flags;
1390
1391 if (!sas_dev)
1392 return TMF_RESP_FUNC_FAILED;
1393
1394 hisi_hba = dev_to_hisi_hba(task->dev);
1395 dev = hisi_hba->dev;
1396
1397 spin_lock_irqsave(&task->task_state_lock, flags);
1398 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1399 spin_unlock_irqrestore(&task->task_state_lock, flags);
1400 rc = TMF_RESP_FUNC_COMPLETE;
1401 goto out;
1402 }
1403 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1404 spin_unlock_irqrestore(&task->task_state_lock, flags);
1405
1406 sas_dev->dev_status = HISI_SAS_DEV_EH;
1407 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1408 struct scsi_cmnd *cmnd = task->uldd_task;
1409 struct hisi_sas_slot *slot = task->lldd_task;
1410 u32 tag = slot->idx;
1411 int rc2;
1412
1413 int_to_scsilun(cmnd->device->lun, &lun);
1414 tmf_task.tmf = TMF_ABORT_TASK;
1415 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1416
1417 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
1418 &tmf_task);
1419
1420 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
1421 HISI_SAS_INT_ABT_CMD, tag);
1422 if (rc2 < 0) {
1423 dev_err(dev, "abort task: internal abort (%d)\n", rc2);
1424 return TMF_RESP_FUNC_FAILED;
1425 }
1426
1427 /*
1428 * If the TMF finds that the IO is not in the device and also
1429 * the internal abort does not succeed, then it is safe to
1430 * free the slot.
1431 * Note: if the internal abort succeeds then the slot
1432 * will have already been completed
1433 */
1434 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
1435 if (task->lldd_task)
1436 hisi_sas_do_release_task(hisi_hba, task, slot);
1437 }
1438 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1439 task->task_proto & SAS_PROTOCOL_STP) {
1440 if (task->dev->dev_type == SAS_SATA_DEV) {
1441 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1442 HISI_SAS_INT_ABT_DEV, 0);
1443 if (rc < 0) {
1444 dev_err(dev, "abort task: internal abort failed\n");
1445 goto out;
1446 }
1447 hisi_sas_dereg_device(hisi_hba, device);
1448 rc = hisi_sas_softreset_ata_disk(device);
1449 }
1450 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
1451 /* SMP */
1452 struct hisi_sas_slot *slot = task->lldd_task;
1453 u32 tag = slot->idx;
1454
1455 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1456 HISI_SAS_INT_ABT_CMD, tag);
1457 if (((rc < 0) || (rc == TMF_RESP_FUNC_FAILED)) &&
1458 task->lldd_task)
1459 hisi_sas_do_release_task(hisi_hba, task, slot);
1460 }
1461
1462 out:
1463 if (rc != TMF_RESP_FUNC_COMPLETE)
1464 dev_notice(dev, "abort task: rc=%d\n", rc);
1465 return rc;
1466 }
1467
1468 static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
1469 {
1470 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1471 struct device *dev = hisi_hba->dev;
1472 struct hisi_sas_tmf_task tmf_task;
1473 int rc = TMF_RESP_FUNC_FAILED;
1474
1475 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1476 HISI_SAS_INT_ABT_DEV, 0);
1477 if (rc < 0) {
1478 dev_err(dev, "abort task set: internal abort rc=%d\n", rc);
1479 return TMF_RESP_FUNC_FAILED;
1480 }
1481 hisi_sas_dereg_device(hisi_hba, device);
1482
1483 tmf_task.tmf = TMF_ABORT_TASK_SET;
1484 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1485
1486 if (rc == TMF_RESP_FUNC_COMPLETE)
1487 hisi_sas_release_task(hisi_hba, device);
1488
1489 return rc;
1490 }
1491
1492 static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
1493 {
1494 int rc = TMF_RESP_FUNC_FAILED;
1495 struct hisi_sas_tmf_task tmf_task;
1496
1497 tmf_task.tmf = TMF_CLEAR_ACA;
1498 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1499
1500 return rc;
1501 }
1502
1503 static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1504 {
1505 struct sas_phy *phy = sas_get_local_phy(device);
1506 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1507 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1508 rc = sas_phy_reset(phy, reset_type);
1509 sas_put_local_phy(phy);
1510 msleep(2000);
1511 return rc;
1512 }
1513
1514 static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1515 {
1516 struct hisi_sas_device *sas_dev = device->lldd_dev;
1517 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1518 struct device *dev = hisi_hba->dev;
1519 int rc = TMF_RESP_FUNC_FAILED;
1520
1521 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1522 return TMF_RESP_FUNC_FAILED;
1523 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1524
1525 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1526 HISI_SAS_INT_ABT_DEV, 0);
1527 if (rc < 0) {
1528 dev_err(dev, "I_T nexus reset: internal abort (%d)\n", rc);
1529 return TMF_RESP_FUNC_FAILED;
1530 }
1531 hisi_sas_dereg_device(hisi_hba, device);
1532
1533 rc = hisi_sas_debug_I_T_nexus_reset(device);
1534
1535 if ((rc == TMF_RESP_FUNC_COMPLETE) || (rc == -ENODEV))
1536 hisi_sas_release_task(hisi_hba, device);
1537
1538 return rc;
1539 }
1540
1541 static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1542 {
1543 struct hisi_sas_device *sas_dev = device->lldd_dev;
1544 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1545 struct device *dev = hisi_hba->dev;
1546 int rc = TMF_RESP_FUNC_FAILED;
1547
1548 sas_dev->dev_status = HISI_SAS_DEV_EH;
1549 if (dev_is_sata(device)) {
1550 struct sas_phy *phy;
1551
1552 /* Clear internal IO and then hardreset */
1553 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1554 HISI_SAS_INT_ABT_DEV, 0);
1555 if (rc < 0) {
1556 dev_err(dev, "lu_reset: internal abort failed\n");
1557 goto out;
1558 }
1559 hisi_sas_dereg_device(hisi_hba, device);
1560
1561 phy = sas_get_local_phy(device);
1562
1563 rc = sas_phy_reset(phy, 1);
1564
1565 if (rc == 0)
1566 hisi_sas_release_task(hisi_hba, device);
1567 sas_put_local_phy(phy);
1568 } else {
1569 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1570
1571 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1572 HISI_SAS_INT_ABT_DEV, 0);
1573 if (rc < 0) {
1574 dev_err(dev, "lu_reset: internal abort failed\n");
1575 goto out;
1576 }
1577 hisi_sas_dereg_device(hisi_hba, device);
1578
1579 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1580 if (rc == TMF_RESP_FUNC_COMPLETE)
1581 hisi_sas_release_task(hisi_hba, device);
1582 }
1583 out:
1584 if (rc != TMF_RESP_FUNC_COMPLETE)
1585 dev_err(dev, "lu_reset: for device[%d]:rc= %d\n",
1586 sas_dev->device_id, rc);
1587 return rc;
1588 }
1589
1590 static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1591 {
1592 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1593 HISI_SAS_DECLARE_RST_WORK_ON_STACK(r);
1594
1595 queue_work(hisi_hba->wq, &r.work);
1596 wait_for_completion(r.completion);
1597 if (r.done)
1598 return TMF_RESP_FUNC_COMPLETE;
1599
1600 return TMF_RESP_FUNC_FAILED;
1601 }
1602
1603 static int hisi_sas_query_task(struct sas_task *task)
1604 {
1605 struct scsi_lun lun;
1606 struct hisi_sas_tmf_task tmf_task;
1607 int rc = TMF_RESP_FUNC_FAILED;
1608
1609 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1610 struct scsi_cmnd *cmnd = task->uldd_task;
1611 struct domain_device *device = task->dev;
1612 struct hisi_sas_slot *slot = task->lldd_task;
1613 u32 tag = slot->idx;
1614
1615 int_to_scsilun(cmnd->device->lun, &lun);
1616 tmf_task.tmf = TMF_QUERY_TASK;
1617 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1618
1619 rc = hisi_sas_debug_issue_ssp_tmf(device,
1620 lun.scsi_lun,
1621 &tmf_task);
1622 switch (rc) {
1623 /* The task is still in Lun, release it then */
1624 case TMF_RESP_FUNC_SUCC:
1625 /* The task is not in Lun or failed, reset the phy */
1626 case TMF_RESP_FUNC_FAILED:
1627 case TMF_RESP_FUNC_COMPLETE:
1628 break;
1629 default:
1630 rc = TMF_RESP_FUNC_FAILED;
1631 break;
1632 }
1633 }
1634 return rc;
1635 }
1636
1637 static int
1638 hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, int device_id,
1639 struct sas_task *task, int abort_flag,
1640 int task_tag)
1641 {
1642 struct domain_device *device = task->dev;
1643 struct hisi_sas_device *sas_dev = device->lldd_dev;
1644 struct device *dev = hisi_hba->dev;
1645 struct hisi_sas_port *port;
1646 struct hisi_sas_slot *slot;
1647 struct asd_sas_port *sas_port = device->port;
1648 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1649 struct hisi_sas_dq *dq = sas_dev->dq;
1650 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1651 unsigned long flags, flags_dq = 0;
1652 int wr_q_index;
1653
1654 if (unlikely(test_bit(HISI_SAS_REJECT_CMD_BIT, &hisi_hba->flags)))
1655 return -EINVAL;
1656
1657 if (!device->port)
1658 return -1;
1659
1660 port = to_hisi_sas_port(sas_port);
1661
1662 /* simply get a slot and send abort command */
1663 spin_lock_irqsave(&hisi_hba->lock, flags);
1664 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1665 if (rc) {
1666 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1667 goto err_out;
1668 }
1669 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1670
1671 slot = &hisi_hba->slot_info[slot_idx];
1672 memset(slot, 0, sizeof(struct hisi_sas_slot));
1673
1674 slot->buf = dma_pool_alloc(hisi_hba->buffer_pool,
1675 GFP_ATOMIC, &slot->buf_dma);
1676 if (!slot->buf) {
1677 rc = -ENOMEM;
1678 goto err_out_tag;
1679 }
1680
1681 spin_lock_irqsave(&dq->lock, flags_dq);
1682 wr_q_index = hisi_hba->hw->get_free_slot(hisi_hba, dq);
1683 if (wr_q_index < 0) {
1684 spin_unlock_irqrestore(&dq->lock, flags_dq);
1685 goto err_out_buf;
1686 }
1687 list_add_tail(&slot->delivery, &dq->list);
1688 spin_unlock_irqrestore(&dq->lock, flags_dq);
1689
1690 dlvry_queue = dq->id;
1691 dlvry_queue_slot = wr_q_index;
1692
1693 slot->idx = slot_idx;
1694 slot->n_elem = n_elem;
1695 slot->dlvry_queue = dlvry_queue;
1696 slot->dlvry_queue_slot = dlvry_queue_slot;
1697 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1698 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1699 slot->task = task;
1700 slot->port = port;
1701 slot->is_internal = true;
1702 task->lldd_task = slot;
1703
1704 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1705 memset(hisi_sas_cmd_hdr_addr_mem(slot), 0, HISI_SAS_COMMAND_TABLE_SZ);
1706 memset(hisi_sas_status_buf_addr_mem(slot), 0, HISI_SAS_STATUS_BUF_SZ);
1707
1708 hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1709 abort_flag, task_tag);
1710
1711 spin_lock_irqsave(&task->task_state_lock, flags);
1712 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1713 spin_unlock_irqrestore(&task->task_state_lock, flags);
1714
1715 slot->ready = 1;
1716 /* send abort command to the chip */
1717 spin_lock_irqsave(&dq->lock, flags);
1718 list_add_tail(&slot->entry, &sas_dev->list);
1719 hisi_hba->hw->start_delivery(dq);
1720 spin_unlock_irqrestore(&dq->lock, flags);
1721
1722 return 0;
1723
1724 err_out_buf:
1725 dma_pool_free(hisi_hba->buffer_pool, slot->buf,
1726 slot->buf_dma);
1727 err_out_tag:
1728 spin_lock_irqsave(&hisi_hba->lock, flags);
1729 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1730 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1731 err_out:
1732 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1733
1734 return rc;
1735 }
1736
1737 /**
1738 * hisi_sas_internal_task_abort -- execute an internal
1739 * abort command for single IO command or a device
1740 * @hisi_hba: host controller struct
1741 * @device: domain device
1742 * @abort_flag: mode of operation, device or single IO
1743 * @tag: tag of IO to be aborted (only relevant to single
1744 * IO mode)
1745 */
1746 static int
1747 hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1748 struct domain_device *device,
1749 int abort_flag, int tag)
1750 {
1751 struct sas_task *task;
1752 struct hisi_sas_device *sas_dev = device->lldd_dev;
1753 struct device *dev = hisi_hba->dev;
1754 int res;
1755
1756 /*
1757 * The interface is not realized means this HW don't support internal
1758 * abort, or don't need to do internal abort. Then here, we return
1759 * TMF_RESP_FUNC_FAILED and let other steps go on, which depends that
1760 * the internal abort has been executed and returned CQ.
1761 */
1762 if (!hisi_hba->hw->prep_abort)
1763 return TMF_RESP_FUNC_FAILED;
1764
1765 task = sas_alloc_slow_task(GFP_KERNEL);
1766 if (!task)
1767 return -ENOMEM;
1768
1769 task->dev = device;
1770 task->task_proto = device->tproto;
1771 task->task_done = hisi_sas_task_done;
1772 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1773 task->slow_task->timer.expires = jiffies + INTERNAL_ABORT_TIMEOUT*HZ;
1774 add_timer(&task->slow_task->timer);
1775
1776 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1777 task, abort_flag, tag);
1778 if (res) {
1779 del_timer(&task->slow_task->timer);
1780 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1781 res);
1782 goto exit;
1783 }
1784 wait_for_completion(&task->slow_task->completion);
1785 res = TMF_RESP_FUNC_FAILED;
1786
1787 /* Internal abort timed out */
1788 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1789 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1790 struct hisi_sas_slot *slot = task->lldd_task;
1791
1792 if (slot)
1793 slot->task = NULL;
1794 dev_err(dev, "internal task abort: timeout and not done.\n");
1795 res = -EIO;
1796 goto exit;
1797 } else
1798 dev_err(dev, "internal task abort: timeout.\n");
1799 }
1800
1801 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1802 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1803 res = TMF_RESP_FUNC_COMPLETE;
1804 goto exit;
1805 }
1806
1807 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1808 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1809 res = TMF_RESP_FUNC_SUCC;
1810 goto exit;
1811 }
1812
1813 exit:
1814 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
1815 "resp: 0x%x sts 0x%x\n",
1816 SAS_ADDR(device->sas_addr),
1817 task,
1818 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1819 task->task_status.stat);
1820 sas_free_task(task);
1821
1822 return res;
1823 }
1824
1825 static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1826 {
1827 hisi_sas_port_notify_formed(sas_phy);
1828 }
1829
1830 static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1831 {
1832 }
1833
1834 static int hisi_sas_write_gpio(struct sas_ha_struct *sha, u8 reg_type,
1835 u8 reg_index, u8 reg_count, u8 *write_data)
1836 {
1837 struct hisi_hba *hisi_hba = sha->lldd_ha;
1838
1839 if (!hisi_hba->hw->write_gpio)
1840 return -EOPNOTSUPP;
1841
1842 return hisi_hba->hw->write_gpio(hisi_hba, reg_type,
1843 reg_index, reg_count, write_data);
1844 }
1845
1846 static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1847 {
1848 phy->phy_attached = 0;
1849 phy->phy_type = 0;
1850 phy->port = NULL;
1851 }
1852
1853 void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1854 {
1855 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1856 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1857 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1858
1859 if (rdy) {
1860 /* Phy down but ready */
1861 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1862 hisi_sas_port_notify_formed(sas_phy);
1863 } else {
1864 struct hisi_sas_port *port = phy->port;
1865
1866 /* Phy down and not ready */
1867 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1868 sas_phy_disconnected(sas_phy);
1869
1870 if (port) {
1871 if (phy->phy_type & PORT_TYPE_SAS) {
1872 int port_id = port->id;
1873
1874 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1875 port_id))
1876 port->port_attached = 0;
1877 } else if (phy->phy_type & PORT_TYPE_SATA)
1878 port->port_attached = 0;
1879 }
1880 hisi_sas_phy_disconnected(phy);
1881 }
1882 }
1883 EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1884
1885 void hisi_sas_kill_tasklets(struct hisi_hba *hisi_hba)
1886 {
1887 int i;
1888
1889 for (i = 0; i < hisi_hba->queue_count; i++) {
1890 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1891
1892 tasklet_kill(&cq->tasklet);
1893 }
1894 }
1895 EXPORT_SYMBOL_GPL(hisi_sas_kill_tasklets);
1896
1897 struct scsi_transport_template *hisi_sas_stt;
1898 EXPORT_SYMBOL_GPL(hisi_sas_stt);
1899
1900 struct device_attribute *host_attrs[] = {
1901 &dev_attr_phy_event_threshold,
1902 NULL,
1903 };
1904 EXPORT_SYMBOL_GPL(host_attrs);
1905
1906 static struct sas_domain_function_template hisi_sas_transport_ops = {
1907 .lldd_dev_found = hisi_sas_dev_found,
1908 .lldd_dev_gone = hisi_sas_dev_gone,
1909 .lldd_execute_task = hisi_sas_queue_command,
1910 .lldd_control_phy = hisi_sas_control_phy,
1911 .lldd_abort_task = hisi_sas_abort_task,
1912 .lldd_abort_task_set = hisi_sas_abort_task_set,
1913 .lldd_clear_aca = hisi_sas_clear_aca,
1914 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1915 .lldd_lu_reset = hisi_sas_lu_reset,
1916 .lldd_query_task = hisi_sas_query_task,
1917 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
1918 .lldd_port_formed = hisi_sas_port_formed,
1919 .lldd_port_deformed = hisi_sas_port_deformed,
1920 .lldd_write_gpio = hisi_sas_write_gpio,
1921 };
1922
1923 void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1924 {
1925 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1926
1927 for (i = 0; i < hisi_hba->queue_count; i++) {
1928 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1929 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1930
1931 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1932 memset(hisi_hba->cmd_hdr[i], 0, s);
1933 dq->wr_point = 0;
1934
1935 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1936 memset(hisi_hba->complete_hdr[i], 0, s);
1937 cq->rd_point = 0;
1938 }
1939
1940 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1941 memset(hisi_hba->initial_fis, 0, s);
1942
1943 s = max_command_entries * sizeof(struct hisi_sas_iost);
1944 memset(hisi_hba->iost, 0, s);
1945
1946 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1947 memset(hisi_hba->breakpoint, 0, s);
1948
1949 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
1950 memset(hisi_hba->sata_breakpoint, 0, s);
1951 }
1952 EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1953
1954 int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1955 {
1956 struct device *dev = hisi_hba->dev;
1957 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1958
1959 spin_lock_init(&hisi_hba->lock);
1960 for (i = 0; i < hisi_hba->n_phy; i++) {
1961 hisi_sas_phy_init(hisi_hba, i);
1962 hisi_hba->port[i].port_attached = 0;
1963 hisi_hba->port[i].id = -1;
1964 }
1965
1966 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1967 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1968 hisi_hba->devices[i].device_id = i;
1969 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1970 }
1971
1972 for (i = 0; i < hisi_hba->queue_count; i++) {
1973 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1974 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1975
1976 /* Completion queue structure */
1977 cq->id = i;
1978 cq->hisi_hba = hisi_hba;
1979
1980 /* Delivery queue structure */
1981 spin_lock_init(&dq->lock);
1982 INIT_LIST_HEAD(&dq->list);
1983 dq->id = i;
1984 dq->hisi_hba = hisi_hba;
1985
1986 /* Delivery queue */
1987 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1988 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1989 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1990 if (!hisi_hba->cmd_hdr[i])
1991 goto err_out;
1992
1993 /* Completion queue */
1994 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1995 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1996 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1997 if (!hisi_hba->complete_hdr[i])
1998 goto err_out;
1999 }
2000
2001 s = sizeof(struct hisi_sas_slot_buf_table);
2002 hisi_hba->buffer_pool = dma_pool_create("dma_buffer", dev, s, 16, 0);
2003 if (!hisi_hba->buffer_pool)
2004 goto err_out;
2005
2006 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2007 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
2008 GFP_KERNEL);
2009 if (!hisi_hba->itct)
2010 goto err_out;
2011
2012 memset(hisi_hba->itct, 0, s);
2013
2014 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
2015 sizeof(struct hisi_sas_slot),
2016 GFP_KERNEL);
2017 if (!hisi_hba->slot_info)
2018 goto err_out;
2019
2020 s = max_command_entries * sizeof(struct hisi_sas_iost);
2021 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
2022 GFP_KERNEL);
2023 if (!hisi_hba->iost)
2024 goto err_out;
2025
2026 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2027 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
2028 &hisi_hba->breakpoint_dma, GFP_KERNEL);
2029 if (!hisi_hba->breakpoint)
2030 goto err_out;
2031
2032 hisi_hba->slot_index_count = max_command_entries;
2033 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
2034 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
2035 if (!hisi_hba->slot_index_tags)
2036 goto err_out;
2037
2038 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2039 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
2040 &hisi_hba->initial_fis_dma, GFP_KERNEL);
2041 if (!hisi_hba->initial_fis)
2042 goto err_out;
2043
2044 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2045 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
2046 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
2047 if (!hisi_hba->sata_breakpoint)
2048 goto err_out;
2049 hisi_sas_init_mem(hisi_hba);
2050
2051 hisi_sas_slot_index_init(hisi_hba);
2052
2053 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
2054 if (!hisi_hba->wq) {
2055 dev_err(dev, "sas_alloc: failed to create workqueue\n");
2056 goto err_out;
2057 }
2058
2059 return 0;
2060 err_out:
2061 return -ENOMEM;
2062 }
2063 EXPORT_SYMBOL_GPL(hisi_sas_alloc);
2064
2065 void hisi_sas_free(struct hisi_hba *hisi_hba)
2066 {
2067 struct device *dev = hisi_hba->dev;
2068 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
2069
2070 for (i = 0; i < hisi_hba->queue_count; i++) {
2071 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
2072 if (hisi_hba->cmd_hdr[i])
2073 dma_free_coherent(dev, s,
2074 hisi_hba->cmd_hdr[i],
2075 hisi_hba->cmd_hdr_dma[i]);
2076
2077 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
2078 if (hisi_hba->complete_hdr[i])
2079 dma_free_coherent(dev, s,
2080 hisi_hba->complete_hdr[i],
2081 hisi_hba->complete_hdr_dma[i]);
2082 }
2083
2084 dma_pool_destroy(hisi_hba->buffer_pool);
2085
2086 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
2087 if (hisi_hba->itct)
2088 dma_free_coherent(dev, s,
2089 hisi_hba->itct, hisi_hba->itct_dma);
2090
2091 s = max_command_entries * sizeof(struct hisi_sas_iost);
2092 if (hisi_hba->iost)
2093 dma_free_coherent(dev, s,
2094 hisi_hba->iost, hisi_hba->iost_dma);
2095
2096 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
2097 if (hisi_hba->breakpoint)
2098 dma_free_coherent(dev, s,
2099 hisi_hba->breakpoint,
2100 hisi_hba->breakpoint_dma);
2101
2102
2103 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
2104 if (hisi_hba->initial_fis)
2105 dma_free_coherent(dev, s,
2106 hisi_hba->initial_fis,
2107 hisi_hba->initial_fis_dma);
2108
2109 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_sata_breakpoint);
2110 if (hisi_hba->sata_breakpoint)
2111 dma_free_coherent(dev, s,
2112 hisi_hba->sata_breakpoint,
2113 hisi_hba->sata_breakpoint_dma);
2114
2115 if (hisi_hba->wq)
2116 destroy_workqueue(hisi_hba->wq);
2117 }
2118 EXPORT_SYMBOL_GPL(hisi_sas_free);
2119
2120 void hisi_sas_rst_work_handler(struct work_struct *work)
2121 {
2122 struct hisi_hba *hisi_hba =
2123 container_of(work, struct hisi_hba, rst_work);
2124
2125 hisi_sas_controller_reset(hisi_hba);
2126 }
2127 EXPORT_SYMBOL_GPL(hisi_sas_rst_work_handler);
2128
2129 void hisi_sas_sync_rst_work_handler(struct work_struct *work)
2130 {
2131 struct hisi_sas_rst *rst =
2132 container_of(work, struct hisi_sas_rst, work);
2133
2134 if (!hisi_sas_controller_reset(rst->hisi_hba))
2135 rst->done = true;
2136 complete(rst->completion);
2137 }
2138 EXPORT_SYMBOL_GPL(hisi_sas_sync_rst_work_handler);
2139
2140 int hisi_sas_get_fw_info(struct hisi_hba *hisi_hba)
2141 {
2142 struct device *dev = hisi_hba->dev;
2143 struct platform_device *pdev = hisi_hba->platform_dev;
2144 struct device_node *np = pdev ? pdev->dev.of_node : NULL;
2145 struct clk *refclk;
2146
2147 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
2148 SAS_ADDR_SIZE)) {
2149 dev_err(dev, "could not get property sas-addr\n");
2150 return -ENOENT;
2151 }
2152
2153 if (np) {
2154 /*
2155 * These properties are only required for platform device-based
2156 * controller with DT firmware.
2157 */
2158 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
2159 "hisilicon,sas-syscon");
2160 if (IS_ERR(hisi_hba->ctrl)) {
2161 dev_err(dev, "could not get syscon\n");
2162 return -ENOENT;
2163 }
2164
2165 if (device_property_read_u32(dev, "ctrl-reset-reg",
2166 &hisi_hba->ctrl_reset_reg)) {
2167 dev_err(dev,
2168 "could not get property ctrl-reset-reg\n");
2169 return -ENOENT;
2170 }
2171
2172 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
2173 &hisi_hba->ctrl_reset_sts_reg)) {
2174 dev_err(dev,
2175 "could not get property ctrl-reset-sts-reg\n");
2176 return -ENOENT;
2177 }
2178
2179 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
2180 &hisi_hba->ctrl_clock_ena_reg)) {
2181 dev_err(dev,
2182 "could not get property ctrl-clock-ena-reg\n");
2183 return -ENOENT;
2184 }
2185 }
2186
2187 refclk = devm_clk_get(dev, NULL);
2188 if (IS_ERR(refclk))
2189 dev_dbg(dev, "no ref clk property\n");
2190 else
2191 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
2192
2193 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy)) {
2194 dev_err(dev, "could not get property phy-count\n");
2195 return -ENOENT;
2196 }
2197
2198 if (device_property_read_u32(dev, "queue-count",
2199 &hisi_hba->queue_count)) {
2200 dev_err(dev, "could not get property queue-count\n");
2201 return -ENOENT;
2202 }
2203
2204 return 0;
2205 }
2206 EXPORT_SYMBOL_GPL(hisi_sas_get_fw_info);
2207
2208 static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
2209 const struct hisi_sas_hw *hw)
2210 {
2211 struct resource *res;
2212 struct Scsi_Host *shost;
2213 struct hisi_hba *hisi_hba;
2214 struct device *dev = &pdev->dev;
2215
2216 shost = scsi_host_alloc(hw->sht, sizeof(*hisi_hba));
2217 if (!shost) {
2218 dev_err(dev, "scsi host alloc failed\n");
2219 return NULL;
2220 }
2221 hisi_hba = shost_priv(shost);
2222
2223 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
2224 hisi_hba->hw = hw;
2225 hisi_hba->dev = dev;
2226 hisi_hba->platform_dev = pdev;
2227 hisi_hba->shost = shost;
2228 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
2229
2230 timer_setup(&hisi_hba->timer, NULL, 0);
2231
2232 if (hisi_sas_get_fw_info(hisi_hba) < 0)
2233 goto err_out;
2234
2235 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
2236 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
2237 dev_err(dev, "No usable DMA addressing method\n");
2238 goto err_out;
2239 }
2240
2241 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2242 hisi_hba->regs = devm_ioremap_resource(dev, res);
2243 if (IS_ERR(hisi_hba->regs))
2244 goto err_out;
2245
2246 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2247 if (res) {
2248 hisi_hba->sgpio_regs = devm_ioremap_resource(dev, res);
2249 if (IS_ERR(hisi_hba->sgpio_regs))
2250 goto err_out;
2251 }
2252
2253 if (hisi_sas_alloc(hisi_hba, shost)) {
2254 hisi_sas_free(hisi_hba);
2255 goto err_out;
2256 }
2257
2258 return shost;
2259 err_out:
2260 scsi_host_put(shost);
2261 dev_err(dev, "shost alloc failed\n");
2262 return NULL;
2263 }
2264
2265 void hisi_sas_init_add(struct hisi_hba *hisi_hba)
2266 {
2267 int i;
2268
2269 for (i = 0; i < hisi_hba->n_phy; i++)
2270 memcpy(&hisi_hba->phy[i].dev_sas_addr,
2271 hisi_hba->sas_addr,
2272 SAS_ADDR_SIZE);
2273 }
2274 EXPORT_SYMBOL_GPL(hisi_sas_init_add);
2275
2276 int hisi_sas_probe(struct platform_device *pdev,
2277 const struct hisi_sas_hw *hw)
2278 {
2279 struct Scsi_Host *shost;
2280 struct hisi_hba *hisi_hba;
2281 struct device *dev = &pdev->dev;
2282 struct asd_sas_phy **arr_phy;
2283 struct asd_sas_port **arr_port;
2284 struct sas_ha_struct *sha;
2285 int rc, phy_nr, port_nr, i;
2286
2287 shost = hisi_sas_shost_alloc(pdev, hw);
2288 if (!shost)
2289 return -ENOMEM;
2290
2291 sha = SHOST_TO_SAS_HA(shost);
2292 hisi_hba = shost_priv(shost);
2293 platform_set_drvdata(pdev, sha);
2294
2295 phy_nr = port_nr = hisi_hba->n_phy;
2296
2297 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
2298 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
2299 if (!arr_phy || !arr_port) {
2300 rc = -ENOMEM;
2301 goto err_out_ha;
2302 }
2303
2304 sha->sas_phy = arr_phy;
2305 sha->sas_port = arr_port;
2306 sha->lldd_ha = hisi_hba;
2307
2308 shost->transportt = hisi_sas_stt;
2309 shost->max_id = HISI_SAS_MAX_DEVICES;
2310 shost->max_lun = ~0;
2311 shost->max_channel = 1;
2312 shost->max_cmd_len = 16;
2313 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
2314 shost->can_queue = hisi_hba->hw->max_command_entries;
2315 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
2316
2317 sha->sas_ha_name = DRV_NAME;
2318 sha->dev = hisi_hba->dev;
2319 sha->lldd_module = THIS_MODULE;
2320 sha->sas_addr = &hisi_hba->sas_addr[0];
2321 sha->num_phys = hisi_hba->n_phy;
2322 sha->core.shost = hisi_hba->shost;
2323
2324 for (i = 0; i < hisi_hba->n_phy; i++) {
2325 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
2326 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
2327 }
2328
2329 hisi_sas_init_add(hisi_hba);
2330
2331 rc = scsi_add_host(shost, &pdev->dev);
2332 if (rc)
2333 goto err_out_ha;
2334
2335 rc = sas_register_ha(sha);
2336 if (rc)
2337 goto err_out_register_ha;
2338
2339 rc = hisi_hba->hw->hw_init(hisi_hba);
2340 if (rc)
2341 goto err_out_register_ha;
2342
2343 scsi_scan_host(shost);
2344
2345 return 0;
2346
2347 err_out_register_ha:
2348 scsi_remove_host(shost);
2349 err_out_ha:
2350 hisi_sas_free(hisi_hba);
2351 scsi_host_put(shost);
2352 return rc;
2353 }
2354 EXPORT_SYMBOL_GPL(hisi_sas_probe);
2355
2356 int hisi_sas_remove(struct platform_device *pdev)
2357 {
2358 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
2359 struct hisi_hba *hisi_hba = sha->lldd_ha;
2360 struct Scsi_Host *shost = sha->core.shost;
2361
2362 if (timer_pending(&hisi_hba->timer))
2363 del_timer(&hisi_hba->timer);
2364
2365 sas_unregister_ha(sha);
2366 sas_remove_host(sha->core.shost);
2367
2368 hisi_sas_free(hisi_hba);
2369 scsi_host_put(shost);
2370 return 0;
2371 }
2372 EXPORT_SYMBOL_GPL(hisi_sas_remove);
2373
2374 static __init int hisi_sas_init(void)
2375 {
2376 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
2377 if (!hisi_sas_stt)
2378 return -ENOMEM;
2379
2380 return 0;
2381 }
2382
2383 static __exit void hisi_sas_exit(void)
2384 {
2385 sas_release_transport(hisi_sas_stt);
2386 }
2387
2388 module_init(hisi_sas_init);
2389 module_exit(hisi_sas_exit);
2390
2391 MODULE_LICENSE("GPL");
2392 MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
2393 MODULE_DESCRIPTION("HISILICON SAS controller driver");
2394 MODULE_ALIAS("platform:" DRV_NAME);