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