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