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