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