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