blob: f720d3ced85177bafc896fd728cccb0efa0b46bf [file] [log] [blame]
John Garrye8899fa2015-11-18 00:50:30 +08001/*
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
John Garry42e7a692015-11-18 00:50:49 +080015#define DEV_IS_GONE(dev) \
16 ((!dev) || (dev->dev_type == SAS_PHY_UNUSED))
17
John Garrycac9b2a2016-02-25 17:42:11 +080018static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
19 u8 *lun, struct hisi_sas_tmf_task *tmf);
John Garry441c2742016-08-24 19:05:47 +080020static int
21hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
22 struct domain_device *device,
23 int abort_flag, int tag);
Xiang Chen7c594f02017-03-23 01:25:20 +080024static int hisi_sas_softreset_ata_disk(struct domain_device *device);
John Garrycac9b2a2016-02-25 17:42:11 +080025
John Garry42e7a692015-11-18 00:50:49 +080026static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
27{
28 return device->port->ha->lldd_ha;
29}
30
John Garry2e244f02017-03-23 01:25:17 +080031struct 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}
35EXPORT_SYMBOL_GPL(to_hisi_sas_port);
36
John Garry257efd12015-11-18 00:50:36 +080037static 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
John Garry42e7a692015-11-18 00:50:49 +080044static 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
49static 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
56static 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
John Garry257efd12015-11-18 00:50:36 +080069static 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}
John Garry27a3f222015-11-18 00:50:50 +080076
77void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
78 struct hisi_sas_slot *slot)
79{
John Garry27a3f222015-11-18 00:50:50 +080080
John Garryd3c4dd42017-04-10 21:22:00 +080081 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;
John Garry27a3f222015-11-18 00:50:50 +080085
John Garryd3c4dd42017-04-10 21:22:00 +080086 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 }
John Garry27a3f222015-11-18 00:50:50 +080096
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);
John Garry27a3f222015-11-18 00:50:50 +0800110 slot->task = NULL;
111 slot->port = NULL;
112 hisi_sas_slot_index_free(hisi_hba, slot->idx);
John Garryd3c4dd42017-04-10 21:22:00 +0800113
John Garry59ba49f2016-09-06 23:36:14 +0800114 /* slot memory is fully zeroed when it is reused */
John Garry27a3f222015-11-18 00:50:50 +0800115}
116EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
117
John Garry66ee9992015-11-18 00:50:54 +0800118static 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
John Garry42e7a692015-11-18 00:50:49 +0800124static 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
John Garry6f2ff1a2016-01-26 02:47:20 +0800131static 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
John Garry441c2742016-08-24 19:05:47 +0800137static 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
John Garrycac9b2a2016-02-25 17:42:11 +0800145/*
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 */
150static 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;
John Garrycac9b2a2016-02-25 17:42:11 +0800158 struct scsi_lun lun;
159 struct device *dev = &hisi_hba->pdev->dev;
160 int tag = abort_slot->idx;
John Garryda7b66e2017-01-03 20:24:50 +0800161 unsigned long flags;
John Garrycac9b2a2016-02-25 17:42:11 +0800162
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);
173out:
174 /* Do cleanup for this task */
John Garryda7b66e2017-01-03 20:24:50 +0800175 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800176 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
John Garryda7b66e2017-01-03 20:24:50 +0800177 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800178 if (task->task_done)
179 task->task_done(task);
John Garrycac9b2a2016-02-25 17:42:11 +0800180}
181
John Garry42e7a692015-11-18 00:50:49 +0800182static 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;
John Garry2e244f02017-03-23 01:25:17 +0800191 struct asd_sas_port *sas_port = device->port;
John Garry42e7a692015-11-18 00:50:49 +0800192 struct device *dev = &hisi_hba->pdev->dev;
193 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
John Garry54c9dd22017-03-23 01:25:29 +0800194 unsigned long flags;
John Garry42e7a692015-11-18 00:50:49 +0800195
John Garry2e244f02017-03-23 01:25:17 +0800196 if (!sas_port) {
John Garry42e7a692015-11-18 00:50:49 +0800197 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);
John Garryddabca22017-03-23 01:25:22 +0800207 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800208 }
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
John Garryddabca22017-03-23 01:25:22 +0800218 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800219 }
John Garry2e244f02017-03-23 01:25:17 +0800220
221 port = to_hisi_sas_port(sas_port);
John Garry9859f242016-08-24 19:05:52 +0800222 if (port && !port->port_attached) {
John Garry09fe9ec2016-09-06 23:36:18 +0800223 dev_info(dev, "task prep: %s port%d not attach device\n",
Xiang Chen6073b772017-03-23 01:25:38 +0800224 (dev_is_sata(device)) ?
John Garry09fe9ec2016-09-06 23:36:18 +0800225 "SATA/STP" : "SAS",
226 device->port->id);
John Garry42e7a692015-11-18 00:50:49 +0800227
John Garry09fe9ec2016-09-06 23:36:18 +0800228 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800229 }
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
John Garry685b6d62016-04-15 21:36:36 +0800243 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);
John Garry42e7a692015-11-18 00:50:49 +0800248 if (rc)
249 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +0800250 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
251 &dlvry_queue, &dlvry_queue_slot);
John Garry42e7a692015-11-18 00:50:49 +0800252 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;
John Garrycac9b2a2016-02-25 17:42:11 +0800267 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
John Garry42e7a692015-11-18 00:50:49 +0800268
269 slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool,
270 GFP_ATOMIC,
271 &slot->status_buffer_dma);
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300272 if (!slot->status_buffer) {
273 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800274 goto err_out_slot_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300275 }
John Garry42e7a692015-11-18 00:50:49 +0800276 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);
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300281 if (!slot->command_table) {
282 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800283 goto err_out_status_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300284 }
John Garry42e7a692015-11-18 00:50:49 +0800285 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) {
John Garry66ee9992015-11-18 00:50:54 +0800289 case SAS_PROTOCOL_SMP:
290 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
291 break;
John Garry42e7a692015-11-18 00:50:49 +0800292 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:
John Garry6f2ff1a2016-01-26 02:47:20 +0800298 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
299 break;
John Garry42e7a692015-11-18 00:50:49 +0800300 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
John Garry405314d2017-03-23 01:25:21 +0800314 list_add_tail(&slot->entry, &sas_dev->list);
John Garry54c9dd22017-03-23 01:25:29 +0800315 spin_lock_irqsave(&task->task_state_lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800316 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
John Garry54c9dd22017-03-23 01:25:29 +0800317 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry42e7a692015-11-18 00:50:49 +0800318
319 hisi_hba->slot_prep = slot;
320
John Garryf696cc32016-11-07 20:48:39 +0800321 atomic64_inc(&sas_dev->running_req);
John Garry42e7a692015-11-18 00:50:49 +0800322 ++(*pass);
323
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300324 return 0;
John Garry42e7a692015-11-18 00:50:49 +0800325
326err_out_sge:
327 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
328 slot->sge_page_dma);
329err_out_command_table:
330 dma_pool_free(hisi_hba->command_table_pool, slot->command_table,
331 slot->command_table_dma);
332err_out_status_buf:
333 dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer,
334 slot->status_buffer_dma);
335err_out_slot_buf:
336 /* Nothing to be done */
337err_out_tag:
338 hisi_sas_slot_index_free(hisi_hba, slot_idx);
339err_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);
345prep_out:
346 return rc;
347}
348
349static 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
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800358 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
359 return -EINVAL;
360
John Garry42e7a692015-11-18 00:50:49 +0800361 /* 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}
John Garry257efd12015-11-18 00:50:36 +0800373
John Garry66139922015-11-18 00:50:48 +0800374static 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;
John Garry66139922015-11-18 00:50:48 +0800390 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
Xiang Chen2ae75782016-11-07 20:48:40 +0800391 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;
John Garry66139922015-11-18 00:50:48 +0800398 }
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
John Garryabda97c2015-11-18 00:50:51 +0800415static 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;
John Garry405314d2017-03-23 01:25:21 +0800430 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
John Garryabda97c2015-11-18 00:50:51 +0800431 break;
432 }
433 }
434 spin_unlock(&hisi_hba->lock);
435
436 return sas_dev;
437}
438
439static 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
John Garry685b6d62016-04-15 21:36:36 +0800446 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);
John Garryabda97c2015-11-18 00:50:51 +0800450 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
John Garry31eec8a2016-02-25 17:42:14 +0800485static 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
John Garry701f75e2015-11-18 00:50:55 +0800498static void hisi_sas_scan_start(struct Scsi_Host *shost)
499{
500 struct hisi_hba *hisi_hba = shost_priv(shost);
John Garry701f75e2015-11-18 00:50:55 +0800501
John Garry396b8042017-03-23 01:25:19 +0800502 hisi_hba->hw->phys_init(hisi_hba);
John Garry701f75e2015-11-18 00:50:55 +0800503}
504
505static 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
John Garry396b8042017-03-23 01:25:19 +0800510 /* Wait for PHY up interrupt to occur */
511 if (time < HZ)
John Garry701f75e2015-11-18 00:50:55 +0800512 return 0;
513
514 sas_drain_work(sha);
515 return 1;
516}
517
John Garry66139922015-11-18 00:50:48 +0800518static 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}
John Garry976867e2015-11-18 00:50:42 +0800529
530static 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;
John Garry66139922015-11-18 00:50:48 +0800551
552 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
John Garry976867e2015-11-18 00:50:42 +0800553}
554
John Garry184a4632015-11-18 00:50:52 +0800555static 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;
John Garry2e244f02017-03-23 01:25:17 +0800561 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
John Garry184a4632015-11-18 00:50:52 +0800562 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
John Garryd3c4dd42017-04-10 21:22:00 +0800575static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, struct sas_task *task,
John Garry405314d2017-03-23 01:25:21 +0800576 struct hisi_sas_slot *slot)
John Garry184a4632015-11-18 00:50:52 +0800577{
John Garryd3c4dd42017-04-10 21:22:00 +0800578 if (task) {
579 unsigned long flags;
580 struct task_status_struct *ts;
John Garry184a4632015-11-18 00:50:52 +0800581
John Garryd3c4dd42017-04-10 21:22:00 +0800582 ts = &task->task_status;
John Garry184a4632015-11-18 00:50:52 +0800583
John Garryd3c4dd42017-04-10 21:22:00 +0800584 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 }
John Garry184a4632015-11-18 00:50:52 +0800592
John Garry405314d2017-03-23 01:25:21 +0800593 hisi_sas_slot_task_free(hisi_hba, task, slot);
John Garry184a4632015-11-18 00:50:52 +0800594}
595
John Garry405314d2017-03-23 01:25:21 +0800596/* hisi_hba.lock should be locked */
John Garry184a4632015-11-18 00:50:52 +0800597static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
598 struct domain_device *device)
599{
John Garry405314d2017-03-23 01:25:21 +0800600 struct hisi_sas_slot *slot, *slot2;
601 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry184a4632015-11-18 00:50:52 +0800602
John Garry405314d2017-03-23 01:25:21 +0800603 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
604 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
John Garry184a4632015-11-18 00:50:52 +0800605}
606
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800607static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
608{
John Garry405314d2017-03-23 01:25:21 +0800609 struct hisi_sas_device *sas_dev;
610 struct domain_device *device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800611 int i;
612
John Garry405314d2017-03-23 01:25:21 +0800613 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
614 sas_dev = &hisi_hba->devices[i];
615 device = sas_dev->sas_device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800616
John Garry405314d2017-03-23 01:25:21 +0800617 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
618 !device)
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800619 continue;
John Garry405314d2017-03-23 01:25:21 +0800620
621 hisi_sas_release_task(hisi_hba, device);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800622 }
623}
624
John Garryabda97c2015-11-18 00:50:51 +0800625static 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
John Garry40f27022016-08-24 19:05:48 +0800635 hisi_sas_internal_task_abort(hisi_hba, device,
636 HISI_SAS_INT_ABT_DEV, 0);
637
John Garryabda97c2015-11-18 00:50:51 +0800638 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}
John Garry42e7a692015-11-18 00:50:49 +0800645
646static 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
John Garrye4189d52015-11-18 00:50:57 +0800651static 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:
John Garryb4c67a62017-03-23 01:25:23 +0800664 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
665 msleep(100);
John Garrye4189d52015-11-18 00:50:57 +0800666 hisi_hba->hw->phy_enable(hisi_hba, phy_no);
John Garrye4189d52015-11-18 00:50:57 +0800667 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:
Xiang Chen2ae75782016-11-07 20:48:40 +0800674 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
675 break;
676
John Garrye4189d52015-11-18 00:50:57 +0800677 case PHY_FUNC_RELEASE_SPINUP_HOLD:
678 default:
679 return -EOPNOTSUPP;
680 }
681 return 0;
682}
John Garry184a4632015-11-18 00:50:52 +0800683
John Garry0efff302015-11-18 00:50:56 +0800684static 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
691static void hisi_sas_tmf_timedout(unsigned long data)
692{
693 struct sas_task *task = (struct sas_task *)data;
694
695 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
696 complete(&task->slow_task->completion);
697}
698
699#define TASK_TIMEOUT 20
700#define TASK_RETRY 3
701static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
702 void *parameter, u32 para_len,
703 struct hisi_sas_tmf_task *tmf)
704{
705 struct hisi_sas_device *sas_dev = device->lldd_dev;
706 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
707 struct device *dev = &hisi_hba->pdev->dev;
708 struct sas_task *task;
709 int res, retry;
710
711 for (retry = 0; retry < TASK_RETRY; retry++) {
712 task = sas_alloc_slow_task(GFP_KERNEL);
713 if (!task)
714 return -ENOMEM;
715
716 task->dev = device;
717 task->task_proto = device->tproto;
718
Xiang Chen7c594f02017-03-23 01:25:20 +0800719 if (dev_is_sata(device)) {
720 task->ata_task.device_control_reg_update = 1;
721 memcpy(&task->ata_task.fis, parameter, para_len);
722 } else {
723 memcpy(&task->ssp_task, parameter, para_len);
724 }
John Garry0efff302015-11-18 00:50:56 +0800725 task->task_done = hisi_sas_task_done;
726
727 task->slow_task->timer.data = (unsigned long) task;
728 task->slow_task->timer.function = hisi_sas_tmf_timedout;
729 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
730 add_timer(&task->slow_task->timer);
731
732 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
733
734 if (res) {
735 del_timer(&task->slow_task->timer);
736 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
737 res);
738 goto ex_err;
739 }
740
741 wait_for_completion(&task->slow_task->completion);
742 res = TMF_RESP_FUNC_FAILED;
743 /* Even TMF timed out, return direct. */
744 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
745 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
John Garryd3c4dd42017-04-10 21:22:00 +0800746 struct hisi_sas_slot *slot = task->lldd_task;
747
Xiang Chen7c594f02017-03-23 01:25:20 +0800748 dev_err(dev, "abort tmf: TMF task timeout\n");
John Garryd3c4dd42017-04-10 21:22:00 +0800749 if (slot)
750 slot->task = NULL;
751
John Garry0efff302015-11-18 00:50:56 +0800752 goto ex_err;
753 }
754 }
755
756 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry1af1b802016-02-25 17:42:10 +0800757 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
John Garry0efff302015-11-18 00:50:56 +0800758 res = TMF_RESP_FUNC_COMPLETE;
759 break;
760 }
761
762 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry4ffde482016-08-24 19:05:53 +0800763 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
764 res = TMF_RESP_FUNC_SUCC;
765 break;
766 }
767
768 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry0efff302015-11-18 00:50:56 +0800769 task->task_status.stat == SAS_DATA_UNDERRUN) {
770 /* no error, but return the number of bytes of
771 * underrun
772 */
773 dev_warn(dev, "abort tmf: task to dev %016llx "
774 "resp: 0x%x sts 0x%x underrun\n",
775 SAS_ADDR(device->sas_addr),
776 task->task_status.resp,
777 task->task_status.stat);
778 res = task->task_status.residual;
779 break;
780 }
781
782 if (task->task_status.resp == SAS_TASK_COMPLETE &&
783 task->task_status.stat == SAS_DATA_OVERRUN) {
784 dev_warn(dev, "abort tmf: blocked task error\n");
785 res = -EMSGSIZE;
786 break;
787 }
788
789 dev_warn(dev, "abort tmf: task to dev "
790 "%016llx resp: 0x%x status 0x%x\n",
791 SAS_ADDR(device->sas_addr), task->task_status.resp,
792 task->task_status.stat);
793 sas_free_task(task);
794 task = NULL;
795 }
796ex_err:
Xiang Chend2d7e7a2016-11-07 20:48:34 +0800797 if (retry == TASK_RETRY)
798 dev_warn(dev, "abort tmf: executing internal task failed!\n");
John Garry0efff302015-11-18 00:50:56 +0800799 sas_free_task(task);
800 return res;
801}
802
Xiang Chen7c594f02017-03-23 01:25:20 +0800803static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
804 bool reset, int pmp, u8 *fis)
805{
806 struct ata_taskfile tf;
807
808 ata_tf_init(dev, &tf);
809 if (reset)
810 tf.ctl |= ATA_SRST;
811 else
812 tf.ctl &= ~ATA_SRST;
813 tf.command = ATA_CMD_DEV_RESET;
814 ata_tf_to_fis(&tf, pmp, 0, fis);
815}
816
817static int hisi_sas_softreset_ata_disk(struct domain_device *device)
818{
819 u8 fis[20] = {0};
820 struct ata_port *ap = device->sata_dev.ap;
821 struct ata_link *link;
822 int rc = TMF_RESP_FUNC_FAILED;
823 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
824 struct device *dev = &hisi_hba->pdev->dev;
825 int s = sizeof(struct host_to_dev_fis);
826 unsigned long flags;
827
828 ata_for_each_link(link, ap, EDGE) {
829 int pmp = sata_srst_pmp(link);
830
831 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
832 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
833 if (rc != TMF_RESP_FUNC_COMPLETE)
834 break;
835 }
836
837 if (rc == TMF_RESP_FUNC_COMPLETE) {
838 ata_for_each_link(link, ap, EDGE) {
839 int pmp = sata_srst_pmp(link);
840
841 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
842 rc = hisi_sas_exec_internal_tmf_task(device, fis,
843 s, NULL);
844 if (rc != TMF_RESP_FUNC_COMPLETE)
845 dev_err(dev, "ata disk de-reset failed\n");
846 }
847 } else {
848 dev_err(dev, "ata disk reset failed\n");
849 }
850
851 if (rc == TMF_RESP_FUNC_COMPLETE) {
852 spin_lock_irqsave(&hisi_hba->lock, flags);
853 hisi_sas_release_task(hisi_hba, device);
854 spin_unlock_irqrestore(&hisi_hba->lock, flags);
855 }
856
857 return rc;
858}
859
John Garry0efff302015-11-18 00:50:56 +0800860static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
861 u8 *lun, struct hisi_sas_tmf_task *tmf)
862{
863 struct sas_ssp_task ssp_task;
864
865 if (!(device->tproto & SAS_PROTOCOL_SSP))
866 return TMF_RESP_FUNC_ESUPP;
867
868 memcpy(ssp_task.LUN, lun, 8);
869
870 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
871 sizeof(ssp_task), tmf);
872}
873
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800874static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
875{
876 int rc;
877
878 if (!hisi_hba->hw->soft_reset)
879 return -1;
880
881 if (!test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
882 struct device *dev = &hisi_hba->pdev->dev;
883 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
884 unsigned long flags;
885
886 dev_dbg(dev, "controller reset begins!\n");
887 scsi_block_requests(hisi_hba->shost);
888 rc = hisi_hba->hw->soft_reset(hisi_hba);
889 if (rc) {
890 dev_warn(dev, "controller reset failed (%d)\n", rc);
891 goto out;
892 }
893 spin_lock_irqsave(&hisi_hba->lock, flags);
894 hisi_sas_release_tasks(hisi_hba);
895 spin_unlock_irqrestore(&hisi_hba->lock, flags);
896
897 sas_ha->notify_ha_event(sas_ha, HAE_RESET);
898 dev_dbg(dev, "controller reset successful!\n");
899 } else
900 return -1;
901
902out:
903 scsi_unblock_requests(hisi_hba->shost);
904 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
905 return rc;
906}
907
John Garry0efff302015-11-18 00:50:56 +0800908static int hisi_sas_abort_task(struct sas_task *task)
909{
910 struct scsi_lun lun;
911 struct hisi_sas_tmf_task tmf_task;
912 struct domain_device *device = task->dev;
913 struct hisi_sas_device *sas_dev = device->lldd_dev;
914 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
915 struct device *dev = &hisi_hba->pdev->dev;
916 int rc = TMF_RESP_FUNC_FAILED;
917 unsigned long flags;
918
919 if (!sas_dev) {
920 dev_warn(dev, "Device has been removed\n");
921 return TMF_RESP_FUNC_FAILED;
922 }
923
John Garry0efff302015-11-18 00:50:56 +0800924 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
John Garry0efff302015-11-18 00:50:56 +0800925 rc = TMF_RESP_FUNC_COMPLETE;
926 goto out;
927 }
928
John Garry0efff302015-11-18 00:50:56 +0800929 sas_dev->dev_status = HISI_SAS_DEV_EH;
930 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
931 struct scsi_cmnd *cmnd = task->uldd_task;
932 struct hisi_sas_slot *slot = task->lldd_task;
933 u32 tag = slot->idx;
John Garryc35279f2017-03-23 01:25:25 +0800934 int rc2;
John Garry0efff302015-11-18 00:50:56 +0800935
936 int_to_scsilun(cmnd->device->lun, &lun);
937 tmf_task.tmf = TMF_ABORT_TASK;
938 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
939
940 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
941 &tmf_task);
942
John Garryc35279f2017-03-23 01:25:25 +0800943 rc2 = hisi_sas_internal_task_abort(hisi_hba, device,
944 HISI_SAS_INT_ABT_CMD, tag);
945 /*
946 * If the TMF finds that the IO is not in the device and also
947 * the internal abort does not succeed, then it is safe to
948 * free the slot.
949 * Note: if the internal abort succeeds then the slot
950 * will have already been completed
951 */
952 if (rc == TMF_RESP_FUNC_COMPLETE && rc2 != TMF_RESP_FUNC_SUCC) {
John Garry0efff302015-11-18 00:50:56 +0800953 if (task->lldd_task) {
John Garry0efff302015-11-18 00:50:56 +0800954 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garryc35279f2017-03-23 01:25:25 +0800955 hisi_sas_do_release_task(hisi_hba, task, slot);
John Garry0efff302015-11-18 00:50:56 +0800956 spin_unlock_irqrestore(&hisi_hba->lock, flags);
957 }
958 }
John Garry0efff302015-11-18 00:50:56 +0800959 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
960 task->task_proto & SAS_PROTOCOL_STP) {
961 if (task->dev->dev_type == SAS_SATA_DEV) {
John Garrydc8a49c2016-08-24 19:05:49 +0800962 hisi_sas_internal_task_abort(hisi_hba, device,
963 HISI_SAS_INT_ABT_DEV, 0);
Xiang Chen7c594f02017-03-23 01:25:20 +0800964 rc = hisi_sas_softreset_ata_disk(device);
John Garry0efff302015-11-18 00:50:56 +0800965 }
Gustavo A. R. Silvaeb045e02017-05-22 13:00:29 -0500966 } else if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SMP) {
John Garrydc8a49c2016-08-24 19:05:49 +0800967 /* SMP */
968 struct hisi_sas_slot *slot = task->lldd_task;
969 u32 tag = slot->idx;
John Garry0efff302015-11-18 00:50:56 +0800970
Xiang Chenccbfe5a2017-03-23 01:25:36 +0800971 rc = hisi_sas_internal_task_abort(hisi_hba, device,
972 HISI_SAS_INT_ABT_CMD, tag);
973 if (rc == TMF_RESP_FUNC_FAILED) {
974 spin_lock_irqsave(&hisi_hba->lock, flags);
975 hisi_sas_do_release_task(hisi_hba, task, slot);
976 spin_unlock_irqrestore(&hisi_hba->lock, flags);
977 }
John Garry0efff302015-11-18 00:50:56 +0800978 }
979
980out:
981 if (rc != TMF_RESP_FUNC_COMPLETE)
982 dev_notice(dev, "abort task: rc=%d\n", rc);
983 return rc;
984}
985
986static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
987{
988 struct hisi_sas_tmf_task tmf_task;
989 int rc = TMF_RESP_FUNC_FAILED;
990
991 tmf_task.tmf = TMF_ABORT_TASK_SET;
992 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
993
994 return rc;
995}
996
997static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
998{
999 int rc = TMF_RESP_FUNC_FAILED;
1000 struct hisi_sas_tmf_task tmf_task;
1001
1002 tmf_task.tmf = TMF_CLEAR_ACA;
1003 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1004
1005 return rc;
1006}
1007
1008static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1009{
1010 struct sas_phy *phy = sas_get_local_phy(device);
1011 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1012 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1013 rc = sas_phy_reset(phy, reset_type);
1014 sas_put_local_phy(phy);
1015 msleep(2000);
1016 return rc;
1017}
1018
1019static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1020{
1021 struct hisi_sas_device *sas_dev = device->lldd_dev;
1022 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1023 unsigned long flags;
1024 int rc = TMF_RESP_FUNC_FAILED;
1025
1026 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1027 return TMF_RESP_FUNC_FAILED;
1028 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1029
1030 rc = hisi_sas_debug_I_T_nexus_reset(device);
1031
Xiang Chen61312432017-03-23 01:25:28 +08001032 if (rc == TMF_RESP_FUNC_COMPLETE) {
1033 spin_lock_irqsave(&hisi_hba->lock, flags);
1034 hisi_sas_release_task(hisi_hba, device);
1035 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1036 }
1037 return rc;
John Garry0efff302015-11-18 00:50:56 +08001038}
1039
1040static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1041{
John Garry0efff302015-11-18 00:50:56 +08001042 struct hisi_sas_device *sas_dev = device->lldd_dev;
1043 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1044 struct device *dev = &hisi_hba->pdev->dev;
1045 unsigned long flags;
1046 int rc = TMF_RESP_FUNC_FAILED;
1047
John Garry0efff302015-11-18 00:50:56 +08001048 sas_dev->dev_status = HISI_SAS_DEV_EH;
John Garry055945d2017-03-23 01:25:26 +08001049 if (dev_is_sata(device)) {
1050 struct sas_phy *phy;
John Garry0efff302015-11-18 00:50:56 +08001051
John Garry055945d2017-03-23 01:25:26 +08001052 /* Clear internal IO and then hardreset */
1053 rc = hisi_sas_internal_task_abort(hisi_hba, device,
1054 HISI_SAS_INT_ABT_DEV, 0);
1055 if (rc == TMF_RESP_FUNC_FAILED)
1056 goto out;
1057
1058 phy = sas_get_local_phy(device);
1059
1060 rc = sas_phy_reset(phy, 1);
1061
1062 if (rc == 0) {
1063 spin_lock_irqsave(&hisi_hba->lock, flags);
1064 hisi_sas_release_task(hisi_hba, device);
1065 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1066 }
1067 sas_put_local_phy(phy);
1068 } else {
1069 struct hisi_sas_tmf_task tmf_task = { .tmf = TMF_LU_RESET };
1070
1071 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1072 if (rc == TMF_RESP_FUNC_COMPLETE) {
1073 spin_lock_irqsave(&hisi_hba->lock, flags);
1074 hisi_sas_release_task(hisi_hba, device);
1075 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1076 }
1077 }
1078out:
John Garry14d3f392017-03-23 01:25:37 +08001079 if (rc != TMF_RESP_FUNC_COMPLETE)
1080 dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
1081 sas_dev->device_id, rc);
John Garry0efff302015-11-18 00:50:56 +08001082 return rc;
1083}
1084
John Garry8b05ad62017-03-23 01:25:35 +08001085static int hisi_sas_clear_nexus_ha(struct sas_ha_struct *sas_ha)
1086{
1087 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
1088
1089 return hisi_sas_controller_reset(hisi_hba);
1090}
1091
John Garry0efff302015-11-18 00:50:56 +08001092static int hisi_sas_query_task(struct sas_task *task)
1093{
1094 struct scsi_lun lun;
1095 struct hisi_sas_tmf_task tmf_task;
1096 int rc = TMF_RESP_FUNC_FAILED;
1097
1098 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1099 struct scsi_cmnd *cmnd = task->uldd_task;
1100 struct domain_device *device = task->dev;
1101 struct hisi_sas_slot *slot = task->lldd_task;
1102 u32 tag = slot->idx;
1103
1104 int_to_scsilun(cmnd->device->lun, &lun);
1105 tmf_task.tmf = TMF_QUERY_TASK;
1106 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1107
1108 rc = hisi_sas_debug_issue_ssp_tmf(device,
1109 lun.scsi_lun,
1110 &tmf_task);
1111 switch (rc) {
1112 /* The task is still in Lun, release it then */
1113 case TMF_RESP_FUNC_SUCC:
1114 /* The task is not in Lun or failed, reset the phy */
1115 case TMF_RESP_FUNC_FAILED:
1116 case TMF_RESP_FUNC_COMPLETE:
1117 break;
Xiang Chen997ee432016-11-07 20:48:35 +08001118 default:
1119 rc = TMF_RESP_FUNC_FAILED;
1120 break;
John Garry0efff302015-11-18 00:50:56 +08001121 }
1122 }
1123 return rc;
1124}
1125
John Garry441c2742016-08-24 19:05:47 +08001126static int
1127hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
1128 struct sas_task *task, int abort_flag,
1129 int task_tag)
1130{
1131 struct domain_device *device = task->dev;
1132 struct hisi_sas_device *sas_dev = device->lldd_dev;
1133 struct device *dev = &hisi_hba->pdev->dev;
1134 struct hisi_sas_port *port;
1135 struct hisi_sas_slot *slot;
John Garry2e244f02017-03-23 01:25:17 +08001136 struct asd_sas_port *sas_port = device->port;
John Garry441c2742016-08-24 19:05:47 +08001137 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1138 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
John Garry54c9dd22017-03-23 01:25:29 +08001139 unsigned long flags;
John Garry441c2742016-08-24 19:05:47 +08001140
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001141 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
1142 return -EINVAL;
1143
John Garry441c2742016-08-24 19:05:47 +08001144 if (!device->port)
1145 return -1;
1146
John Garry2e244f02017-03-23 01:25:17 +08001147 port = to_hisi_sas_port(sas_port);
John Garry441c2742016-08-24 19:05:47 +08001148
1149 /* simply get a slot and send abort command */
1150 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1151 if (rc)
1152 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +08001153 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
1154 &dlvry_queue, &dlvry_queue_slot);
John Garry441c2742016-08-24 19:05:47 +08001155 if (rc)
1156 goto err_out_tag;
1157
1158 slot = &hisi_hba->slot_info[slot_idx];
1159 memset(slot, 0, sizeof(struct hisi_sas_slot));
1160
1161 slot->idx = slot_idx;
1162 slot->n_elem = n_elem;
1163 slot->dlvry_queue = dlvry_queue;
1164 slot->dlvry_queue_slot = dlvry_queue_slot;
1165 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1166 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1167 slot->task = task;
1168 slot->port = port;
1169 task->lldd_task = slot;
1170
1171 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1172
1173 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1174 abort_flag, task_tag);
1175 if (rc)
1176 goto err_out_tag;
1177
John Garry405314d2017-03-23 01:25:21 +08001178
1179 list_add_tail(&slot->entry, &sas_dev->list);
John Garry54c9dd22017-03-23 01:25:29 +08001180 spin_lock_irqsave(&task->task_state_lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001181 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
John Garry54c9dd22017-03-23 01:25:29 +08001182 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry441c2742016-08-24 19:05:47 +08001183
1184 hisi_hba->slot_prep = slot;
1185
John Garryf696cc32016-11-07 20:48:39 +08001186 atomic64_inc(&sas_dev->running_req);
1187
John Garry441c2742016-08-24 19:05:47 +08001188 /* send abort command to our chip */
1189 hisi_hba->hw->start_delivery(hisi_hba);
1190
1191 return 0;
1192
1193err_out_tag:
1194 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1195err_out:
1196 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1197
1198 return rc;
1199}
1200
1201/**
1202 * hisi_sas_internal_task_abort -- execute an internal
1203 * abort command for single IO command or a device
1204 * @hisi_hba: host controller struct
1205 * @device: domain device
1206 * @abort_flag: mode of operation, device or single IO
1207 * @tag: tag of IO to be aborted (only relevant to single
1208 * IO mode)
1209 */
1210static int
1211hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1212 struct domain_device *device,
1213 int abort_flag, int tag)
1214{
1215 struct sas_task *task;
1216 struct hisi_sas_device *sas_dev = device->lldd_dev;
1217 struct device *dev = &hisi_hba->pdev->dev;
1218 int res;
1219 unsigned long flags;
1220
1221 if (!hisi_hba->hw->prep_abort)
1222 return -EOPNOTSUPP;
1223
1224 task = sas_alloc_slow_task(GFP_KERNEL);
1225 if (!task)
1226 return -ENOMEM;
1227
1228 task->dev = device;
1229 task->task_proto = device->tproto;
1230 task->task_done = hisi_sas_task_done;
1231 task->slow_task->timer.data = (unsigned long)task;
1232 task->slow_task->timer.function = hisi_sas_tmf_timedout;
John Garry0844a3f2017-04-10 21:21:59 +08001233 task->slow_task->timer.expires = jiffies + msecs_to_jiffies(110);
John Garry441c2742016-08-24 19:05:47 +08001234 add_timer(&task->slow_task->timer);
1235
1236 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1237 spin_lock_irqsave(&hisi_hba->lock, flags);
1238 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1239 task, abort_flag, tag);
1240 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1241 if (res) {
1242 del_timer(&task->slow_task->timer);
1243 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1244 res);
1245 goto exit;
1246 }
1247 wait_for_completion(&task->slow_task->completion);
1248 res = TMF_RESP_FUNC_FAILED;
1249
1250 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1251 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1252 res = TMF_RESP_FUNC_COMPLETE;
1253 goto exit;
1254 }
1255
John Garryc35279f2017-03-23 01:25:25 +08001256 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1257 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1258 res = TMF_RESP_FUNC_SUCC;
1259 goto exit;
1260 }
1261
John Garry6fcdda82017-03-23 01:25:30 +08001262 /* Internal abort timed out */
John Garry441c2742016-08-24 19:05:47 +08001263 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1264 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1265 dev_err(dev, "internal task abort: timeout.\n");
John Garry441c2742016-08-24 19:05:47 +08001266 }
1267 }
1268
1269exit:
John Garry297d7302017-01-20 20:45:22 +08001270 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
John Garry441c2742016-08-24 19:05:47 +08001271 "resp: 0x%x sts 0x%x\n",
1272 SAS_ADDR(device->sas_addr),
1273 task,
1274 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1275 task->task_status.stat);
1276 sas_free_task(task);
1277
1278 return res;
1279}
1280
John Garry184a4632015-11-18 00:50:52 +08001281static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1282{
1283 hisi_sas_port_notify_formed(sas_phy);
1284}
1285
John Garry184a4632015-11-18 00:50:52 +08001286static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1287{
1288 phy->phy_attached = 0;
1289 phy->phy_type = 0;
1290 phy->port = NULL;
1291}
1292
1293void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1294{
1295 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1296 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1297 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1298
1299 if (rdy) {
1300 /* Phy down but ready */
1301 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1302 hisi_sas_port_notify_formed(sas_phy);
1303 } else {
1304 struct hisi_sas_port *port = phy->port;
1305
1306 /* Phy down and not ready */
1307 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1308 sas_phy_disconnected(sas_phy);
1309
1310 if (port) {
1311 if (phy->phy_type & PORT_TYPE_SAS) {
1312 int port_id = port->id;
1313
1314 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1315 port_id))
1316 port->port_attached = 0;
1317 } else if (phy->phy_type & PORT_TYPE_SATA)
1318 port->port_attached = 0;
1319 }
1320 hisi_sas_phy_disconnected(phy);
1321 }
1322}
1323EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1324
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001325void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1326 u32 state)
1327{
1328 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1329 int phy_no;
1330
1331 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1332 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1333 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1334 struct asd_sas_port *sas_port = sas_phy->port;
1335 struct domain_device *dev;
1336
1337 if (sas_phy->enabled) {
1338 /* Report PHY state change to libsas */
1339 if (state & (1 << phy_no))
1340 continue;
1341
1342 if (old_state & (1 << phy_no))
1343 /* PHY down but was up before */
1344 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1345 }
1346 if (!sas_port)
1347 continue;
1348 dev = sas_port->port_dev;
1349
1350 if (DEV_IS_EXPANDER(dev->dev_type))
1351 sas_ha->notify_phy_event(sas_phy, PORTE_BROADCAST_RCVD);
1352 }
1353}
1354EXPORT_SYMBOL_GPL(hisi_sas_rescan_topology);
1355
John Garrye8899fa2015-11-18 00:50:30 +08001356static struct scsi_transport_template *hisi_sas_stt;
1357
John Garry7eb78692015-11-18 00:50:31 +08001358static struct scsi_host_template hisi_sas_sht = {
1359 .module = THIS_MODULE,
1360 .name = DRV_NAME,
1361 .queuecommand = sas_queuecommand,
1362 .target_alloc = sas_target_alloc,
John Garry31eec8a2016-02-25 17:42:14 +08001363 .slave_configure = hisi_sas_slave_configure,
John Garry701f75e2015-11-18 00:50:55 +08001364 .scan_finished = hisi_sas_scan_finished,
1365 .scan_start = hisi_sas_scan_start,
John Garry7eb78692015-11-18 00:50:31 +08001366 .change_queue_depth = sas_change_queue_depth,
1367 .bios_param = sas_bios_param,
1368 .can_queue = 1,
1369 .this_id = -1,
1370 .sg_tablesize = SG_ALL,
1371 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1372 .use_clustering = ENABLE_CLUSTERING,
1373 .eh_device_reset_handler = sas_eh_device_reset_handler,
1374 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
1375 .target_destroy = sas_target_destroy,
1376 .ioctl = sas_ioctl,
1377};
1378
John Garrye8899fa2015-11-18 00:50:30 +08001379static struct sas_domain_function_template hisi_sas_transport_ops = {
John Garryabda97c2015-11-18 00:50:51 +08001380 .lldd_dev_found = hisi_sas_dev_found,
1381 .lldd_dev_gone = hisi_sas_dev_gone,
John Garry42e7a692015-11-18 00:50:49 +08001382 .lldd_execute_task = hisi_sas_queue_command,
John Garrye4189d52015-11-18 00:50:57 +08001383 .lldd_control_phy = hisi_sas_control_phy,
John Garry0efff302015-11-18 00:50:56 +08001384 .lldd_abort_task = hisi_sas_abort_task,
1385 .lldd_abort_task_set = hisi_sas_abort_task_set,
1386 .lldd_clear_aca = hisi_sas_clear_aca,
1387 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1388 .lldd_lu_reset = hisi_sas_lu_reset,
1389 .lldd_query_task = hisi_sas_query_task,
John Garry8b05ad62017-03-23 01:25:35 +08001390 .lldd_clear_nexus_ha = hisi_sas_clear_nexus_ha,
John Garry184a4632015-11-18 00:50:52 +08001391 .lldd_port_formed = hisi_sas_port_formed,
John Garrye8899fa2015-11-18 00:50:30 +08001392};
1393
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001394void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1395{
1396 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1397
1398 for (i = 0; i < hisi_hba->queue_count; i++) {
1399 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1400 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1401
1402 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1403 memset(hisi_hba->cmd_hdr[i], 0, s);
1404 dq->wr_point = 0;
1405
1406 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1407 memset(hisi_hba->complete_hdr[i], 0, s);
1408 cq->rd_point = 0;
1409 }
1410
1411 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1412 memset(hisi_hba->initial_fis, 0, s);
1413
1414 s = max_command_entries * sizeof(struct hisi_sas_iost);
1415 memset(hisi_hba->iost, 0, s);
1416
1417 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1418 memset(hisi_hba->breakpoint, 0, s);
1419
1420 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1421 memset(hisi_hba->sata_breakpoint, 0, s);
1422}
1423EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1424
John Garry6be6de12015-11-18 00:50:34 +08001425static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1426{
John Garry6be6de12015-11-18 00:50:34 +08001427 struct platform_device *pdev = hisi_hba->pdev;
1428 struct device *dev = &pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001429 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +08001430
John Garryfa42d802015-11-18 00:50:43 +08001431 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +08001432 for (i = 0; i < hisi_hba->n_phy; i++) {
1433 hisi_sas_phy_init(hisi_hba, i);
1434 hisi_hba->port[i].port_attached = 0;
1435 hisi_hba->port[i].id = -1;
John Garry976867e2015-11-18 00:50:42 +08001436 }
1437
John Garryaf740db2015-11-18 00:50:41 +08001438 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1439 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1440 hisi_hba->devices[i].device_id = i;
1441 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1442 }
1443
John Garry6be6de12015-11-18 00:50:34 +08001444 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +08001445 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
John Garry4fde02a2016-09-06 23:36:12 +08001446 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
John Garry9101a072015-11-18 00:50:37 +08001447
1448 /* Completion queue structure */
1449 cq->id = i;
1450 cq->hisi_hba = hisi_hba;
1451
John Garry4fde02a2016-09-06 23:36:12 +08001452 /* Delivery queue structure */
1453 dq->id = i;
1454 dq->hisi_hba = hisi_hba;
1455
John Garry6be6de12015-11-18 00:50:34 +08001456 /* Delivery queue */
1457 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1458 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1459 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1460 if (!hisi_hba->cmd_hdr[i])
1461 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001462
1463 /* Completion queue */
1464 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1465 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1466 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1467 if (!hisi_hba->complete_hdr[i])
1468 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001469 }
1470
1471 s = HISI_SAS_STATUS_BUF_SZ;
1472 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1473 dev, s, 16, 0);
1474 if (!hisi_hba->status_buffer_pool)
1475 goto err_out;
1476
1477 s = HISI_SAS_COMMAND_TABLE_SZ;
1478 hisi_hba->command_table_pool = dma_pool_create("command_table",
1479 dev, s, 16, 0);
1480 if (!hisi_hba->command_table_pool)
1481 goto err_out;
1482
1483 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1484 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1485 GFP_KERNEL);
1486 if (!hisi_hba->itct)
1487 goto err_out;
1488
1489 memset(hisi_hba->itct, 0, s);
1490
John Garrya8d547b2016-01-26 02:47:03 +08001491 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
John Garry6be6de12015-11-18 00:50:34 +08001492 sizeof(struct hisi_sas_slot),
1493 GFP_KERNEL);
1494 if (!hisi_hba->slot_info)
1495 goto err_out;
1496
John Garrya8d547b2016-01-26 02:47:03 +08001497 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry6be6de12015-11-18 00:50:34 +08001498 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1499 GFP_KERNEL);
1500 if (!hisi_hba->iost)
1501 goto err_out;
1502
John Garrya8d547b2016-01-26 02:47:03 +08001503 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001504 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1505 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1506 if (!hisi_hba->breakpoint)
1507 goto err_out;
1508
John Garrya8d547b2016-01-26 02:47:03 +08001509 hisi_hba->slot_index_count = max_command_entries;
John Garry433f5692016-09-06 23:36:15 +08001510 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
John Garry257efd12015-11-18 00:50:36 +08001511 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1512 if (!hisi_hba->slot_index_tags)
1513 goto err_out;
1514
John Garry6be6de12015-11-18 00:50:34 +08001515 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1516 sizeof(struct hisi_sas_sge_page), 16, 0);
1517 if (!hisi_hba->sge_page_pool)
1518 goto err_out;
1519
1520 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1521 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1522 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1523 if (!hisi_hba->initial_fis)
1524 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001525
John Garrya8d547b2016-01-26 02:47:03 +08001526 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry6be6de12015-11-18 00:50:34 +08001527 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1528 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1529 if (!hisi_hba->sata_breakpoint)
1530 goto err_out;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001531 hisi_sas_init_mem(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001532
John Garry257efd12015-11-18 00:50:36 +08001533 hisi_sas_slot_index_init(hisi_hba);
1534
John Garry7e9080e2015-11-18 00:50:40 +08001535 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1536 if (!hisi_hba->wq) {
1537 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1538 goto err_out;
1539 }
1540
John Garry6be6de12015-11-18 00:50:34 +08001541 return 0;
1542err_out:
1543 return -ENOMEM;
1544}
1545
John Garry89d53322015-11-18 00:50:35 +08001546static void hisi_sas_free(struct hisi_hba *hisi_hba)
1547{
1548 struct device *dev = &hisi_hba->pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001549 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry89d53322015-11-18 00:50:35 +08001550
1551 for (i = 0; i < hisi_hba->queue_count; i++) {
1552 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1553 if (hisi_hba->cmd_hdr[i])
1554 dma_free_coherent(dev, s,
1555 hisi_hba->cmd_hdr[i],
1556 hisi_hba->cmd_hdr_dma[i]);
1557
1558 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1559 if (hisi_hba->complete_hdr[i])
1560 dma_free_coherent(dev, s,
1561 hisi_hba->complete_hdr[i],
1562 hisi_hba->complete_hdr_dma[i]);
1563 }
1564
1565 dma_pool_destroy(hisi_hba->status_buffer_pool);
1566 dma_pool_destroy(hisi_hba->command_table_pool);
1567 dma_pool_destroy(hisi_hba->sge_page_pool);
1568
1569 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1570 if (hisi_hba->itct)
1571 dma_free_coherent(dev, s,
1572 hisi_hba->itct, hisi_hba->itct_dma);
1573
John Garrya8d547b2016-01-26 02:47:03 +08001574 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry89d53322015-11-18 00:50:35 +08001575 if (hisi_hba->iost)
1576 dma_free_coherent(dev, s,
1577 hisi_hba->iost, hisi_hba->iost_dma);
1578
John Garrya8d547b2016-01-26 02:47:03 +08001579 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001580 if (hisi_hba->breakpoint)
1581 dma_free_coherent(dev, s,
1582 hisi_hba->breakpoint,
1583 hisi_hba->breakpoint_dma);
1584
1585
1586 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1587 if (hisi_hba->initial_fis)
1588 dma_free_coherent(dev, s,
1589 hisi_hba->initial_fis,
1590 hisi_hba->initial_fis_dma);
1591
John Garrya8d547b2016-01-26 02:47:03 +08001592 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry89d53322015-11-18 00:50:35 +08001593 if (hisi_hba->sata_breakpoint)
1594 dma_free_coherent(dev, s,
1595 hisi_hba->sata_breakpoint,
1596 hisi_hba->sata_breakpoint_dma);
1597
John Garry7e9080e2015-11-18 00:50:40 +08001598 if (hisi_hba->wq)
1599 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +08001600}
John Garry6be6de12015-11-18 00:50:34 +08001601
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001602static void hisi_sas_rst_work_handler(struct work_struct *work)
1603{
1604 struct hisi_hba *hisi_hba =
1605 container_of(work, struct hisi_hba, rst_work);
1606
1607 hisi_sas_controller_reset(hisi_hba);
1608}
1609
John Garry7eb78692015-11-18 00:50:31 +08001610static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1611 const struct hisi_sas_hw *hw)
1612{
John Garrye26b2f42015-11-18 00:50:32 +08001613 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +08001614 struct Scsi_Host *shost;
1615 struct hisi_hba *hisi_hba;
1616 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +08001617 struct device_node *np = pdev->dev.of_node;
John Garry3bc45af2016-10-04 19:11:11 +08001618 struct clk *refclk;
John Garry7eb78692015-11-18 00:50:31 +08001619
1620 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
Xiaofei Tand37a0082016-11-29 23:45:57 +08001621 if (!shost) {
1622 dev_err(dev, "scsi host alloc failed\n");
1623 return NULL;
1624 }
John Garry7eb78692015-11-18 00:50:31 +08001625 hisi_hba = shost_priv(shost);
1626
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001627 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
John Garry7eb78692015-11-18 00:50:31 +08001628 hisi_hba->hw = hw;
1629 hisi_hba->pdev = pdev;
1630 hisi_hba->shost = shost;
1631 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1632
John Garryfa42d802015-11-18 00:50:43 +08001633 init_timer(&hisi_hba->timer);
1634
John Garry4d558c72016-02-04 02:26:08 +08001635 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1636 SAS_ADDR_SIZE))
John Garrye26b2f42015-11-18 00:50:32 +08001637 goto err_out;
1638
John Garry4d558c72016-02-04 02:26:08 +08001639 if (np) {
1640 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1641 "hisilicon,sas-syscon");
1642 if (IS_ERR(hisi_hba->ctrl))
1643 goto err_out;
1644
1645 if (device_property_read_u32(dev, "ctrl-reset-reg",
1646 &hisi_hba->ctrl_reset_reg))
1647 goto err_out;
1648
1649 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1650 &hisi_hba->ctrl_reset_sts_reg))
1651 goto err_out;
1652
1653 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1654 &hisi_hba->ctrl_clock_ena_reg))
1655 goto err_out;
1656 }
1657
John Garry3bc45af2016-10-04 19:11:11 +08001658 refclk = devm_clk_get(&pdev->dev, NULL);
1659 if (IS_ERR(refclk))
John Garry87e287c2017-01-20 20:45:20 +08001660 dev_dbg(dev, "no ref clk property\n");
John Garry3bc45af2016-10-04 19:11:11 +08001661 else
1662 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1663
John Garry4d558c72016-02-04 02:26:08 +08001664 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
John Garrye26b2f42015-11-18 00:50:32 +08001665 goto err_out;
1666
John Garry4d558c72016-02-04 02:26:08 +08001667 if (device_property_read_u32(dev, "queue-count",
1668 &hisi_hba->queue_count))
John Garrye26b2f42015-11-18 00:50:32 +08001669 goto err_out;
1670
John Garrya6f2c7f2016-09-06 23:36:19 +08001671 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1672 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1673 dev_err(dev, "No usable DMA addressing method\n");
1674 goto err_out;
1675 }
1676
John Garrye26b2f42015-11-18 00:50:32 +08001677 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1678 hisi_hba->regs = devm_ioremap_resource(dev, res);
1679 if (IS_ERR(hisi_hba->regs))
1680 goto err_out;
1681
John Garry89d53322015-11-18 00:50:35 +08001682 if (hisi_sas_alloc(hisi_hba, shost)) {
1683 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001684 goto err_out;
John Garry89d53322015-11-18 00:50:35 +08001685 }
John Garry6be6de12015-11-18 00:50:34 +08001686
John Garry7eb78692015-11-18 00:50:31 +08001687 return shost;
1688err_out:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001689 kfree(shost);
John Garry7eb78692015-11-18 00:50:31 +08001690 dev_err(dev, "shost alloc failed\n");
1691 return NULL;
1692}
1693
John Garry5d742422015-11-18 00:50:38 +08001694static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1695{
1696 int i;
1697
1698 for (i = 0; i < hisi_hba->n_phy; i++)
1699 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1700 hisi_hba->sas_addr,
1701 SAS_ADDR_SIZE);
1702}
1703
John Garry7eb78692015-11-18 00:50:31 +08001704int hisi_sas_probe(struct platform_device *pdev,
1705 const struct hisi_sas_hw *hw)
1706{
1707 struct Scsi_Host *shost;
1708 struct hisi_hba *hisi_hba;
1709 struct device *dev = &pdev->dev;
1710 struct asd_sas_phy **arr_phy;
1711 struct asd_sas_port **arr_port;
1712 struct sas_ha_struct *sha;
1713 int rc, phy_nr, port_nr, i;
1714
1715 shost = hisi_sas_shost_alloc(pdev, hw);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001716 if (!shost)
1717 return -ENOMEM;
John Garry7eb78692015-11-18 00:50:31 +08001718
1719 sha = SHOST_TO_SAS_HA(shost);
1720 hisi_hba = shost_priv(shost);
1721 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +08001722
John Garry7eb78692015-11-18 00:50:31 +08001723 phy_nr = port_nr = hisi_hba->n_phy;
1724
1725 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1726 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001727 if (!arr_phy || !arr_port) {
1728 rc = -ENOMEM;
1729 goto err_out_ha;
1730 }
John Garry7eb78692015-11-18 00:50:31 +08001731
1732 sha->sas_phy = arr_phy;
1733 sha->sas_port = arr_port;
John Garry7eb78692015-11-18 00:50:31 +08001734 sha->lldd_ha = hisi_hba;
1735
1736 shost->transportt = hisi_sas_stt;
1737 shost->max_id = HISI_SAS_MAX_DEVICES;
1738 shost->max_lun = ~0;
1739 shost->max_channel = 1;
1740 shost->max_cmd_len = 16;
1741 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
John Garrya8d547b2016-01-26 02:47:03 +08001742 shost->can_queue = hisi_hba->hw->max_command_entries;
1743 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
John Garry7eb78692015-11-18 00:50:31 +08001744
1745 sha->sas_ha_name = DRV_NAME;
1746 sha->dev = &hisi_hba->pdev->dev;
1747 sha->lldd_module = THIS_MODULE;
1748 sha->sas_addr = &hisi_hba->sas_addr[0];
1749 sha->num_phys = hisi_hba->n_phy;
1750 sha->core.shost = hisi_hba->shost;
1751
1752 for (i = 0; i < hisi_hba->n_phy; i++) {
1753 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1754 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1755 }
1756
John Garry5d742422015-11-18 00:50:38 +08001757 hisi_sas_init_add(hisi_hba);
1758
John Garry7eb78692015-11-18 00:50:31 +08001759 rc = scsi_add_host(shost, &pdev->dev);
1760 if (rc)
1761 goto err_out_ha;
1762
1763 rc = sas_register_ha(sha);
1764 if (rc)
1765 goto err_out_register_ha;
1766
Xiang Chen0757f042017-01-20 20:45:23 +08001767 rc = hisi_hba->hw->hw_init(hisi_hba);
1768 if (rc)
1769 goto err_out_register_ha;
1770
John Garry7eb78692015-11-18 00:50:31 +08001771 scsi_scan_host(shost);
1772
1773 return 0;
1774
1775err_out_register_ha:
1776 scsi_remove_host(shost);
1777err_out_ha:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001778 hisi_sas_free(hisi_hba);
John Garry7eb78692015-11-18 00:50:31 +08001779 kfree(shost);
1780 return rc;
1781}
1782EXPORT_SYMBOL_GPL(hisi_sas_probe);
1783
John Garry89d53322015-11-18 00:50:35 +08001784int hisi_sas_remove(struct platform_device *pdev)
1785{
1786 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1787 struct hisi_hba *hisi_hba = sha->lldd_ha;
Xiaofei Tand37a0082016-11-29 23:45:57 +08001788 struct Scsi_Host *shost = sha->core.shost;
John Garry89d53322015-11-18 00:50:35 +08001789
John Garry89d53322015-11-18 00:50:35 +08001790 sas_unregister_ha(sha);
1791 sas_remove_host(sha->core.shost);
1792
1793 hisi_sas_free(hisi_hba);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001794 kfree(shost);
John Garry89d53322015-11-18 00:50:35 +08001795 return 0;
1796}
1797EXPORT_SYMBOL_GPL(hisi_sas_remove);
1798
John Garrye8899fa2015-11-18 00:50:30 +08001799static __init int hisi_sas_init(void)
1800{
1801 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1802
1803 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1804 if (!hisi_sas_stt)
1805 return -ENOMEM;
1806
1807 return 0;
1808}
1809
1810static __exit void hisi_sas_exit(void)
1811{
1812 sas_release_transport(hisi_sas_stt);
1813}
1814
1815module_init(hisi_sas_init);
1816module_exit(hisi_sas_exit);
1817
1818MODULE_VERSION(DRV_VERSION);
1819MODULE_LICENSE("GPL");
1820MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1821MODULE_DESCRIPTION("HISILICON SAS controller driver");
1822MODULE_ALIAS("platform:" DRV_NAME);