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