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