blob: 00068d25ba48450819e94ea28cf6969d1cb41f0b [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{
80 struct device *dev = &hisi_hba->pdev->dev;
Xiang Chen13c59902017-01-20 20:45:24 +080081 struct domain_device *device = task->dev;
82 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry27a3f222015-11-18 00:50:50 +080083
84 if (!slot->task)
85 return;
86
87 if (!sas_protocol_ata(task->task_proto))
88 if (slot->n_elem)
89 dma_unmap_sg(dev, task->scatter, slot->n_elem,
90 task->data_dir);
91
92 if (slot->command_table)
93 dma_pool_free(hisi_hba->command_table_pool,
94 slot->command_table, slot->command_table_dma);
95
96 if (slot->status_buffer)
97 dma_pool_free(hisi_hba->status_buffer_pool,
98 slot->status_buffer, slot->status_buffer_dma);
99
100 if (slot->sge_page)
101 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
102 slot->sge_page_dma);
103
104 list_del_init(&slot->entry);
105 task->lldd_task = NULL;
106 slot->task = NULL;
107 slot->port = NULL;
108 hisi_sas_slot_index_free(hisi_hba, slot->idx);
Xiang Chen13c59902017-01-20 20:45:24 +0800109 if (sas_dev)
110 atomic64_dec(&sas_dev->running_req);
John Garry59ba49f2016-09-06 23:36:14 +0800111 /* slot memory is fully zeroed when it is reused */
John Garry27a3f222015-11-18 00:50:50 +0800112}
113EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
114
John Garry66ee9992015-11-18 00:50:54 +0800115static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
116 struct hisi_sas_slot *slot)
117{
118 return hisi_hba->hw->prep_smp(hisi_hba, slot);
119}
120
John Garry42e7a692015-11-18 00:50:49 +0800121static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
122 struct hisi_sas_slot *slot, int is_tmf,
123 struct hisi_sas_tmf_task *tmf)
124{
125 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
126}
127
John Garry6f2ff1a2016-01-26 02:47:20 +0800128static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
129 struct hisi_sas_slot *slot)
130{
131 return hisi_hba->hw->prep_stp(hisi_hba, slot);
132}
133
John Garry441c2742016-08-24 19:05:47 +0800134static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
135 struct hisi_sas_slot *slot,
136 int device_id, int abort_flag, int tag_to_abort)
137{
138 return hisi_hba->hw->prep_abort(hisi_hba, slot,
139 device_id, abort_flag, tag_to_abort);
140}
141
John Garrycac9b2a2016-02-25 17:42:11 +0800142/*
143 * This function will issue an abort TMF regardless of whether the
144 * task is in the sdev or not. Then it will do the task complete
145 * cleanup and callbacks.
146 */
147static void hisi_sas_slot_abort(struct work_struct *work)
148{
149 struct hisi_sas_slot *abort_slot =
150 container_of(work, struct hisi_sas_slot, abort_slot);
151 struct sas_task *task = abort_slot->task;
152 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
153 struct scsi_cmnd *cmnd = task->uldd_task;
154 struct hisi_sas_tmf_task tmf_task;
John Garrycac9b2a2016-02-25 17:42:11 +0800155 struct scsi_lun lun;
156 struct device *dev = &hisi_hba->pdev->dev;
157 int tag = abort_slot->idx;
John Garryda7b66e2017-01-03 20:24:50 +0800158 unsigned long flags;
John Garrycac9b2a2016-02-25 17:42:11 +0800159
160 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
161 dev_err(dev, "cannot abort slot for non-ssp task\n");
162 goto out;
163 }
164
165 int_to_scsilun(cmnd->device->lun, &lun);
166 tmf_task.tmf = TMF_ABORT_TASK;
167 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
168
169 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
170out:
171 /* Do cleanup for this task */
John Garryda7b66e2017-01-03 20:24:50 +0800172 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800173 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
John Garryda7b66e2017-01-03 20:24:50 +0800174 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800175 if (task->task_done)
176 task->task_done(task);
John Garrycac9b2a2016-02-25 17:42:11 +0800177}
178
John Garry42e7a692015-11-18 00:50:49 +0800179static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba,
180 int is_tmf, struct hisi_sas_tmf_task *tmf,
181 int *pass)
182{
183 struct domain_device *device = task->dev;
184 struct hisi_sas_device *sas_dev = device->lldd_dev;
185 struct hisi_sas_port *port;
186 struct hisi_sas_slot *slot;
187 struct hisi_sas_cmd_hdr *cmd_hdr_base;
John Garry2e244f02017-03-23 01:25:17 +0800188 struct asd_sas_port *sas_port = device->port;
John Garry42e7a692015-11-18 00:50:49 +0800189 struct device *dev = &hisi_hba->pdev->dev;
190 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
191
John Garry2e244f02017-03-23 01:25:17 +0800192 if (!sas_port) {
John Garry42e7a692015-11-18 00:50:49 +0800193 struct task_status_struct *ts = &task->task_status;
194
195 ts->resp = SAS_TASK_UNDELIVERED;
196 ts->stat = SAS_PHY_DOWN;
197 /*
198 * libsas will use dev->port, should
199 * not call task_done for sata
200 */
201 if (device->dev_type != SAS_SATA_DEV)
202 task->task_done(task);
John Garryddabca22017-03-23 01:25:22 +0800203 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800204 }
205
206 if (DEV_IS_GONE(sas_dev)) {
207 if (sas_dev)
208 dev_info(dev, "task prep: device %llu not ready\n",
209 sas_dev->device_id);
210 else
211 dev_info(dev, "task prep: device %016llx not ready\n",
212 SAS_ADDR(device->sas_addr));
213
John Garryddabca22017-03-23 01:25:22 +0800214 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800215 }
John Garry2e244f02017-03-23 01:25:17 +0800216
217 port = to_hisi_sas_port(sas_port);
John Garry9859f242016-08-24 19:05:52 +0800218 if (port && !port->port_attached) {
John Garry09fe9ec2016-09-06 23:36:18 +0800219 dev_info(dev, "task prep: %s port%d not attach device\n",
220 (sas_protocol_ata(task->task_proto)) ?
221 "SATA/STP" : "SAS",
222 device->port->id);
John Garry42e7a692015-11-18 00:50:49 +0800223
John Garry09fe9ec2016-09-06 23:36:18 +0800224 return SAS_PHY_DOWN;
John Garry42e7a692015-11-18 00:50:49 +0800225 }
226
227 if (!sas_protocol_ata(task->task_proto)) {
228 if (task->num_scatter) {
229 n_elem = dma_map_sg(dev, task->scatter,
230 task->num_scatter, task->data_dir);
231 if (!n_elem) {
232 rc = -ENOMEM;
233 goto prep_out;
234 }
235 }
236 } else
237 n_elem = task->num_scatter;
238
John Garry685b6d62016-04-15 21:36:36 +0800239 if (hisi_hba->hw->slot_index_alloc)
240 rc = hisi_hba->hw->slot_index_alloc(hisi_hba, &slot_idx,
241 device);
242 else
243 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
John Garry42e7a692015-11-18 00:50:49 +0800244 if (rc)
245 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +0800246 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
247 &dlvry_queue, &dlvry_queue_slot);
John Garry42e7a692015-11-18 00:50:49 +0800248 if (rc)
249 goto err_out_tag;
250
251 slot = &hisi_hba->slot_info[slot_idx];
252 memset(slot, 0, sizeof(struct hisi_sas_slot));
253
254 slot->idx = slot_idx;
255 slot->n_elem = n_elem;
256 slot->dlvry_queue = dlvry_queue;
257 slot->dlvry_queue_slot = dlvry_queue_slot;
258 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
259 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
260 slot->task = task;
261 slot->port = port;
262 task->lldd_task = slot;
John Garrycac9b2a2016-02-25 17:42:11 +0800263 INIT_WORK(&slot->abort_slot, hisi_sas_slot_abort);
John Garry42e7a692015-11-18 00:50:49 +0800264
265 slot->status_buffer = dma_pool_alloc(hisi_hba->status_buffer_pool,
266 GFP_ATOMIC,
267 &slot->status_buffer_dma);
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300268 if (!slot->status_buffer) {
269 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800270 goto err_out_slot_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300271 }
John Garry42e7a692015-11-18 00:50:49 +0800272 memset(slot->status_buffer, 0, HISI_SAS_STATUS_BUF_SZ);
273
274 slot->command_table = dma_pool_alloc(hisi_hba->command_table_pool,
275 GFP_ATOMIC,
276 &slot->command_table_dma);
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300277 if (!slot->command_table) {
278 rc = -ENOMEM;
John Garry42e7a692015-11-18 00:50:49 +0800279 goto err_out_status_buf;
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300280 }
John Garry42e7a692015-11-18 00:50:49 +0800281 memset(slot->command_table, 0, HISI_SAS_COMMAND_TABLE_SZ);
282 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
283
284 switch (task->task_proto) {
John Garry66ee9992015-11-18 00:50:54 +0800285 case SAS_PROTOCOL_SMP:
286 rc = hisi_sas_task_prep_smp(hisi_hba, slot);
287 break;
John Garry42e7a692015-11-18 00:50:49 +0800288 case SAS_PROTOCOL_SSP:
289 rc = hisi_sas_task_prep_ssp(hisi_hba, slot, is_tmf, tmf);
290 break;
291 case SAS_PROTOCOL_SATA:
292 case SAS_PROTOCOL_STP:
293 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
John Garry6f2ff1a2016-01-26 02:47:20 +0800294 rc = hisi_sas_task_prep_ata(hisi_hba, slot);
295 break;
John Garry42e7a692015-11-18 00:50:49 +0800296 default:
297 dev_err(dev, "task prep: unknown/unsupported proto (0x%x)\n",
298 task->task_proto);
299 rc = -EINVAL;
300 break;
301 }
302
303 if (rc) {
304 dev_err(dev, "task prep: rc = 0x%x\n", rc);
305 if (slot->sge_page)
306 goto err_out_sge;
307 goto err_out_command_table;
308 }
309
John Garry405314d2017-03-23 01:25:21 +0800310 list_add_tail(&slot->entry, &sas_dev->list);
John Garry42e7a692015-11-18 00:50:49 +0800311 spin_lock(&task->task_state_lock);
312 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
313 spin_unlock(&task->task_state_lock);
314
315 hisi_hba->slot_prep = slot;
316
John Garryf696cc32016-11-07 20:48:39 +0800317 atomic64_inc(&sas_dev->running_req);
John Garry42e7a692015-11-18 00:50:49 +0800318 ++(*pass);
319
Dan Carpenter9c9d18e2015-12-09 13:48:36 +0300320 return 0;
John Garry42e7a692015-11-18 00:50:49 +0800321
322err_out_sge:
323 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
324 slot->sge_page_dma);
325err_out_command_table:
326 dma_pool_free(hisi_hba->command_table_pool, slot->command_table,
327 slot->command_table_dma);
328err_out_status_buf:
329 dma_pool_free(hisi_hba->status_buffer_pool, slot->status_buffer,
330 slot->status_buffer_dma);
331err_out_slot_buf:
332 /* Nothing to be done */
333err_out_tag:
334 hisi_sas_slot_index_free(hisi_hba, slot_idx);
335err_out:
336 dev_err(dev, "task prep: failed[%d]!\n", rc);
337 if (!sas_protocol_ata(task->task_proto))
338 if (n_elem)
339 dma_unmap_sg(dev, task->scatter, n_elem,
340 task->data_dir);
341prep_out:
342 return rc;
343}
344
345static int hisi_sas_task_exec(struct sas_task *task, gfp_t gfp_flags,
346 int is_tmf, struct hisi_sas_tmf_task *tmf)
347{
348 u32 rc;
349 u32 pass = 0;
350 unsigned long flags;
351 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
352 struct device *dev = &hisi_hba->pdev->dev;
353
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800354 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
355 return -EINVAL;
356
John Garry42e7a692015-11-18 00:50:49 +0800357 /* protect task_prep and start_delivery sequence */
358 spin_lock_irqsave(&hisi_hba->lock, flags);
359 rc = hisi_sas_task_prep(task, hisi_hba, is_tmf, tmf, &pass);
360 if (rc)
361 dev_err(dev, "task exec: failed[%d]!\n", rc);
362
363 if (likely(pass))
364 hisi_hba->hw->start_delivery(hisi_hba);
365 spin_unlock_irqrestore(&hisi_hba->lock, flags);
366
367 return rc;
368}
John Garry257efd12015-11-18 00:50:36 +0800369
John Garry66139922015-11-18 00:50:48 +0800370static void hisi_sas_bytes_dmaed(struct hisi_hba *hisi_hba, int phy_no)
371{
372 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
373 struct asd_sas_phy *sas_phy = &phy->sas_phy;
374 struct sas_ha_struct *sas_ha;
375
376 if (!phy->phy_attached)
377 return;
378
379 sas_ha = &hisi_hba->sha;
380 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
381
382 if (sas_phy->phy) {
383 struct sas_phy *sphy = sas_phy->phy;
384
385 sphy->negotiated_linkrate = sas_phy->linkrate;
John Garry66139922015-11-18 00:50:48 +0800386 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
Xiang Chen2ae75782016-11-07 20:48:40 +0800387 sphy->maximum_linkrate_hw =
388 hisi_hba->hw->phy_get_max_linkrate();
389 if (sphy->minimum_linkrate == SAS_LINK_RATE_UNKNOWN)
390 sphy->minimum_linkrate = phy->minimum_linkrate;
391
392 if (sphy->maximum_linkrate == SAS_LINK_RATE_UNKNOWN)
393 sphy->maximum_linkrate = phy->maximum_linkrate;
John Garry66139922015-11-18 00:50:48 +0800394 }
395
396 if (phy->phy_type & PORT_TYPE_SAS) {
397 struct sas_identify_frame *id;
398
399 id = (struct sas_identify_frame *)phy->frame_rcvd;
400 id->dev_type = phy->identify.device_type;
401 id->initiator_bits = SAS_PROTOCOL_ALL;
402 id->target_bits = phy->identify.target_port_protocols;
403 } else if (phy->phy_type & PORT_TYPE_SATA) {
404 /*Nothing*/
405 }
406
407 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
408 sas_ha->notify_port_event(sas_phy, PORTE_BYTES_DMAED);
409}
410
John Garryabda97c2015-11-18 00:50:51 +0800411static struct hisi_sas_device *hisi_sas_alloc_dev(struct domain_device *device)
412{
413 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
414 struct hisi_sas_device *sas_dev = NULL;
415 int i;
416
417 spin_lock(&hisi_hba->lock);
418 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
419 if (hisi_hba->devices[i].dev_type == SAS_PHY_UNUSED) {
420 hisi_hba->devices[i].device_id = i;
421 sas_dev = &hisi_hba->devices[i];
422 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
423 sas_dev->dev_type = device->dev_type;
424 sas_dev->hisi_hba = hisi_hba;
425 sas_dev->sas_device = device;
John Garry405314d2017-03-23 01:25:21 +0800426 INIT_LIST_HEAD(&hisi_hba->devices[i].list);
John Garryabda97c2015-11-18 00:50:51 +0800427 break;
428 }
429 }
430 spin_unlock(&hisi_hba->lock);
431
432 return sas_dev;
433}
434
435static int hisi_sas_dev_found(struct domain_device *device)
436{
437 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
438 struct domain_device *parent_dev = device->parent;
439 struct hisi_sas_device *sas_dev;
440 struct device *dev = &hisi_hba->pdev->dev;
441
John Garry685b6d62016-04-15 21:36:36 +0800442 if (hisi_hba->hw->alloc_dev)
443 sas_dev = hisi_hba->hw->alloc_dev(device);
444 else
445 sas_dev = hisi_sas_alloc_dev(device);
John Garryabda97c2015-11-18 00:50:51 +0800446 if (!sas_dev) {
447 dev_err(dev, "fail alloc dev: max support %d devices\n",
448 HISI_SAS_MAX_DEVICES);
449 return -EINVAL;
450 }
451
452 device->lldd_dev = sas_dev;
453 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
454
455 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
456 int phy_no;
457 u8 phy_num = parent_dev->ex_dev.num_phys;
458 struct ex_phy *phy;
459
460 for (phy_no = 0; phy_no < phy_num; phy_no++) {
461 phy = &parent_dev->ex_dev.ex_phy[phy_no];
462 if (SAS_ADDR(phy->attached_sas_addr) ==
463 SAS_ADDR(device->sas_addr)) {
464 sas_dev->attached_phy = phy_no;
465 break;
466 }
467 }
468
469 if (phy_no == phy_num) {
470 dev_info(dev, "dev found: no attached "
471 "dev:%016llx at ex:%016llx\n",
472 SAS_ADDR(device->sas_addr),
473 SAS_ADDR(parent_dev->sas_addr));
474 return -EINVAL;
475 }
476 }
477
478 return 0;
479}
480
John Garry31eec8a2016-02-25 17:42:14 +0800481static int hisi_sas_slave_configure(struct scsi_device *sdev)
482{
483 struct domain_device *dev = sdev_to_domain_dev(sdev);
484 int ret = sas_slave_configure(sdev);
485
486 if (ret)
487 return ret;
488 if (!dev_is_sata(dev))
489 sas_change_queue_depth(sdev, 64);
490
491 return 0;
492}
493
John Garry701f75e2015-11-18 00:50:55 +0800494static void hisi_sas_scan_start(struct Scsi_Host *shost)
495{
496 struct hisi_hba *hisi_hba = shost_priv(shost);
John Garry701f75e2015-11-18 00:50:55 +0800497
John Garry396b8042017-03-23 01:25:19 +0800498 hisi_hba->hw->phys_init(hisi_hba);
John Garry701f75e2015-11-18 00:50:55 +0800499}
500
501static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
502{
503 struct hisi_hba *hisi_hba = shost_priv(shost);
504 struct sas_ha_struct *sha = &hisi_hba->sha;
505
John Garry396b8042017-03-23 01:25:19 +0800506 /* Wait for PHY up interrupt to occur */
507 if (time < HZ)
John Garry701f75e2015-11-18 00:50:55 +0800508 return 0;
509
510 sas_drain_work(sha);
511 return 1;
512}
513
John Garry66139922015-11-18 00:50:48 +0800514static void hisi_sas_phyup_work(struct work_struct *work)
515{
516 struct hisi_sas_phy *phy =
517 container_of(work, struct hisi_sas_phy, phyup_ws);
518 struct hisi_hba *hisi_hba = phy->hisi_hba;
519 struct asd_sas_phy *sas_phy = &phy->sas_phy;
520 int phy_no = sas_phy->id;
521
522 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
523 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
524}
John Garry976867e2015-11-18 00:50:42 +0800525
526static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
527{
528 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
529 struct asd_sas_phy *sas_phy = &phy->sas_phy;
530
531 phy->hisi_hba = hisi_hba;
532 phy->port = NULL;
533 init_timer(&phy->timer);
534 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
535 sas_phy->class = SAS;
536 sas_phy->iproto = SAS_PROTOCOL_ALL;
537 sas_phy->tproto = 0;
538 sas_phy->type = PHY_TYPE_PHYSICAL;
539 sas_phy->role = PHY_ROLE_INITIATOR;
540 sas_phy->oob_mode = OOB_NOT_CONNECTED;
541 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
542 sas_phy->id = phy_no;
543 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
544 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
545 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
546 sas_phy->lldd_phy = phy;
John Garry66139922015-11-18 00:50:48 +0800547
548 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
John Garry976867e2015-11-18 00:50:42 +0800549}
550
John Garry184a4632015-11-18 00:50:52 +0800551static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
552{
553 struct sas_ha_struct *sas_ha = sas_phy->ha;
554 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
555 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
556 struct asd_sas_port *sas_port = sas_phy->port;
John Garry2e244f02017-03-23 01:25:17 +0800557 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
John Garry184a4632015-11-18 00:50:52 +0800558 unsigned long flags;
559
560 if (!sas_port)
561 return;
562
563 spin_lock_irqsave(&hisi_hba->lock, flags);
564 port->port_attached = 1;
565 port->id = phy->port_id;
566 phy->port = port;
567 sas_port->lldd_port = port;
568 spin_unlock_irqrestore(&hisi_hba->lock, flags);
569}
570
John Garry405314d2017-03-23 01:25:21 +0800571static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba,
572 struct sas_task *task,
573 struct hisi_sas_slot *slot)
John Garry184a4632015-11-18 00:50:52 +0800574{
John Garry405314d2017-03-23 01:25:21 +0800575 struct task_status_struct *ts;
576 unsigned long flags;
John Garry184a4632015-11-18 00:50:52 +0800577
John Garry405314d2017-03-23 01:25:21 +0800578 if (!task)
John Garry184a4632015-11-18 00:50:52 +0800579 return;
580
John Garry405314d2017-03-23 01:25:21 +0800581 ts = &task->task_status;
John Garry184a4632015-11-18 00:50:52 +0800582
John Garry405314d2017-03-23 01:25:21 +0800583 ts->resp = SAS_TASK_COMPLETE;
584 ts->stat = SAS_ABORTED_TASK;
585 spin_lock_irqsave(&task->task_state_lock, flags);
586 task->task_state_flags &=
587 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
588 task->task_state_flags |= SAS_TASK_STATE_DONE;
589 spin_unlock_irqrestore(&task->task_state_lock, flags);
John Garry184a4632015-11-18 00:50:52 +0800590
John Garry405314d2017-03-23 01:25:21 +0800591 hisi_sas_slot_task_free(hisi_hba, task, slot);
John Garry184a4632015-11-18 00:50:52 +0800592}
593
John Garry405314d2017-03-23 01:25:21 +0800594/* hisi_hba.lock should be locked */
John Garry184a4632015-11-18 00:50:52 +0800595static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
596 struct domain_device *device)
597{
John Garry405314d2017-03-23 01:25:21 +0800598 struct hisi_sas_slot *slot, *slot2;
599 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry184a4632015-11-18 00:50:52 +0800600
John Garry405314d2017-03-23 01:25:21 +0800601 list_for_each_entry_safe(slot, slot2, &sas_dev->list, entry)
602 hisi_sas_do_release_task(hisi_hba, slot->task, slot);
John Garry184a4632015-11-18 00:50:52 +0800603}
604
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800605static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
606{
John Garry405314d2017-03-23 01:25:21 +0800607 struct hisi_sas_device *sas_dev;
608 struct domain_device *device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800609 int i;
610
John Garry405314d2017-03-23 01:25:21 +0800611 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
612 sas_dev = &hisi_hba->devices[i];
613 device = sas_dev->sas_device;
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800614
John Garry405314d2017-03-23 01:25:21 +0800615 if ((sas_dev->dev_type == SAS_PHY_UNUSED) ||
616 !device)
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800617 continue;
John Garry405314d2017-03-23 01:25:21 +0800618
619 hisi_sas_release_task(hisi_hba, device);
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800620 }
621}
622
John Garryabda97c2015-11-18 00:50:51 +0800623static void hisi_sas_dev_gone(struct domain_device *device)
624{
625 struct hisi_sas_device *sas_dev = device->lldd_dev;
626 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
627 struct device *dev = &hisi_hba->pdev->dev;
628 u64 dev_id = sas_dev->device_id;
629
630 dev_info(dev, "found dev[%lld:%x] is gone\n",
631 sas_dev->device_id, sas_dev->dev_type);
632
John Garry40f27022016-08-24 19:05:48 +0800633 hisi_sas_internal_task_abort(hisi_hba, device,
634 HISI_SAS_INT_ABT_DEV, 0);
635
John Garryabda97c2015-11-18 00:50:51 +0800636 hisi_hba->hw->free_device(hisi_hba, sas_dev);
637 device->lldd_dev = NULL;
638 memset(sas_dev, 0, sizeof(*sas_dev));
639 sas_dev->device_id = dev_id;
640 sas_dev->dev_type = SAS_PHY_UNUSED;
641 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
642}
John Garry42e7a692015-11-18 00:50:49 +0800643
644static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
645{
646 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
647}
648
John Garrye4189d52015-11-18 00:50:57 +0800649static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
650 void *funcdata)
651{
652 struct sas_ha_struct *sas_ha = sas_phy->ha;
653 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
654 int phy_no = sas_phy->id;
655
656 switch (func) {
657 case PHY_FUNC_HARD_RESET:
658 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
659 break;
660
661 case PHY_FUNC_LINK_RESET:
John Garryb4c67a62017-03-23 01:25:23 +0800662 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
663 msleep(100);
John Garrye4189d52015-11-18 00:50:57 +0800664 hisi_hba->hw->phy_enable(hisi_hba, phy_no);
John Garrye4189d52015-11-18 00:50:57 +0800665 break;
666
667 case PHY_FUNC_DISABLE:
668 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
669 break;
670
671 case PHY_FUNC_SET_LINK_RATE:
Xiang Chen2ae75782016-11-07 20:48:40 +0800672 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
673 break;
674
John Garrye4189d52015-11-18 00:50:57 +0800675 case PHY_FUNC_RELEASE_SPINUP_HOLD:
676 default:
677 return -EOPNOTSUPP;
678 }
679 return 0;
680}
John Garry184a4632015-11-18 00:50:52 +0800681
John Garry0efff302015-11-18 00:50:56 +0800682static void hisi_sas_task_done(struct sas_task *task)
683{
684 if (!del_timer(&task->slow_task->timer))
685 return;
686 complete(&task->slow_task->completion);
687}
688
689static void hisi_sas_tmf_timedout(unsigned long data)
690{
691 struct sas_task *task = (struct sas_task *)data;
692
693 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
694 complete(&task->slow_task->completion);
695}
696
697#define TASK_TIMEOUT 20
698#define TASK_RETRY 3
699static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
700 void *parameter, u32 para_len,
701 struct hisi_sas_tmf_task *tmf)
702{
703 struct hisi_sas_device *sas_dev = device->lldd_dev;
704 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
705 struct device *dev = &hisi_hba->pdev->dev;
706 struct sas_task *task;
707 int res, retry;
708
709 for (retry = 0; retry < TASK_RETRY; retry++) {
710 task = sas_alloc_slow_task(GFP_KERNEL);
711 if (!task)
712 return -ENOMEM;
713
714 task->dev = device;
715 task->task_proto = device->tproto;
716
Xiang Chen7c594f02017-03-23 01:25:20 +0800717 if (dev_is_sata(device)) {
718 task->ata_task.device_control_reg_update = 1;
719 memcpy(&task->ata_task.fis, parameter, para_len);
720 } else {
721 memcpy(&task->ssp_task, parameter, para_len);
722 }
John Garry0efff302015-11-18 00:50:56 +0800723 task->task_done = hisi_sas_task_done;
724
725 task->slow_task->timer.data = (unsigned long) task;
726 task->slow_task->timer.function = hisi_sas_tmf_timedout;
727 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
728 add_timer(&task->slow_task->timer);
729
730 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
731
732 if (res) {
733 del_timer(&task->slow_task->timer);
734 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
735 res);
736 goto ex_err;
737 }
738
739 wait_for_completion(&task->slow_task->completion);
740 res = TMF_RESP_FUNC_FAILED;
741 /* Even TMF timed out, return direct. */
742 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
743 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
Xiang Chen7c594f02017-03-23 01:25:20 +0800744 dev_err(dev, "abort tmf: TMF task timeout\n");
John Garry0efff302015-11-18 00:50:56 +0800745 if (task->lldd_task) {
746 struct hisi_sas_slot *slot =
747 task->lldd_task;
748
749 hisi_sas_slot_task_free(hisi_hba,
750 task, slot);
751 }
752
753 goto ex_err;
754 }
755 }
756
757 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry1af1b802016-02-25 17:42:10 +0800758 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
John Garry0efff302015-11-18 00:50:56 +0800759 res = TMF_RESP_FUNC_COMPLETE;
760 break;
761 }
762
763 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry4ffde482016-08-24 19:05:53 +0800764 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
765 res = TMF_RESP_FUNC_SUCC;
766 break;
767 }
768
769 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry0efff302015-11-18 00:50:56 +0800770 task->task_status.stat == SAS_DATA_UNDERRUN) {
771 /* no error, but return the number of bytes of
772 * underrun
773 */
774 dev_warn(dev, "abort tmf: task to dev %016llx "
775 "resp: 0x%x sts 0x%x underrun\n",
776 SAS_ADDR(device->sas_addr),
777 task->task_status.resp,
778 task->task_status.stat);
779 res = task->task_status.residual;
780 break;
781 }
782
783 if (task->task_status.resp == SAS_TASK_COMPLETE &&
784 task->task_status.stat == SAS_DATA_OVERRUN) {
785 dev_warn(dev, "abort tmf: blocked task error\n");
786 res = -EMSGSIZE;
787 break;
788 }
789
790 dev_warn(dev, "abort tmf: task to dev "
791 "%016llx resp: 0x%x status 0x%x\n",
792 SAS_ADDR(device->sas_addr), task->task_status.resp,
793 task->task_status.stat);
794 sas_free_task(task);
795 task = NULL;
796 }
797ex_err:
Xiang Chend2d7e7a2016-11-07 20:48:34 +0800798 if (retry == TASK_RETRY)
799 dev_warn(dev, "abort tmf: executing internal task failed!\n");
John Garry0efff302015-11-18 00:50:56 +0800800 sas_free_task(task);
801 return res;
802}
803
Xiang Chen7c594f02017-03-23 01:25:20 +0800804static void hisi_sas_fill_ata_reset_cmd(struct ata_device *dev,
805 bool reset, int pmp, u8 *fis)
806{
807 struct ata_taskfile tf;
808
809 ata_tf_init(dev, &tf);
810 if (reset)
811 tf.ctl |= ATA_SRST;
812 else
813 tf.ctl &= ~ATA_SRST;
814 tf.command = ATA_CMD_DEV_RESET;
815 ata_tf_to_fis(&tf, pmp, 0, fis);
816}
817
818static int hisi_sas_softreset_ata_disk(struct domain_device *device)
819{
820 u8 fis[20] = {0};
821 struct ata_port *ap = device->sata_dev.ap;
822 struct ata_link *link;
823 int rc = TMF_RESP_FUNC_FAILED;
824 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
825 struct device *dev = &hisi_hba->pdev->dev;
826 int s = sizeof(struct host_to_dev_fis);
827 unsigned long flags;
828
829 ata_for_each_link(link, ap, EDGE) {
830 int pmp = sata_srst_pmp(link);
831
832 hisi_sas_fill_ata_reset_cmd(link->device, 1, pmp, fis);
833 rc = hisi_sas_exec_internal_tmf_task(device, fis, s, NULL);
834 if (rc != TMF_RESP_FUNC_COMPLETE)
835 break;
836 }
837
838 if (rc == TMF_RESP_FUNC_COMPLETE) {
839 ata_for_each_link(link, ap, EDGE) {
840 int pmp = sata_srst_pmp(link);
841
842 hisi_sas_fill_ata_reset_cmd(link->device, 0, pmp, fis);
843 rc = hisi_sas_exec_internal_tmf_task(device, fis,
844 s, NULL);
845 if (rc != TMF_RESP_FUNC_COMPLETE)
846 dev_err(dev, "ata disk de-reset failed\n");
847 }
848 } else {
849 dev_err(dev, "ata disk reset failed\n");
850 }
851
852 if (rc == TMF_RESP_FUNC_COMPLETE) {
853 spin_lock_irqsave(&hisi_hba->lock, flags);
854 hisi_sas_release_task(hisi_hba, device);
855 spin_unlock_irqrestore(&hisi_hba->lock, flags);
856 }
857
858 return rc;
859}
860
John Garry0efff302015-11-18 00:50:56 +0800861static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
862 u8 *lun, struct hisi_sas_tmf_task *tmf)
863{
864 struct sas_ssp_task ssp_task;
865
866 if (!(device->tproto & SAS_PROTOCOL_SSP))
867 return TMF_RESP_FUNC_ESUPP;
868
869 memcpy(ssp_task.LUN, lun, 8);
870
871 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
872 sizeof(ssp_task), tmf);
873}
874
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800875static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
876{
877 int rc;
878
879 if (!hisi_hba->hw->soft_reset)
880 return -1;
881
882 if (!test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
883 struct device *dev = &hisi_hba->pdev->dev;
884 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
885 unsigned long flags;
886
887 dev_dbg(dev, "controller reset begins!\n");
888 scsi_block_requests(hisi_hba->shost);
889 rc = hisi_hba->hw->soft_reset(hisi_hba);
890 if (rc) {
891 dev_warn(dev, "controller reset failed (%d)\n", rc);
892 goto out;
893 }
894 spin_lock_irqsave(&hisi_hba->lock, flags);
895 hisi_sas_release_tasks(hisi_hba);
896 spin_unlock_irqrestore(&hisi_hba->lock, flags);
897
898 sas_ha->notify_ha_event(sas_ha, HAE_RESET);
899 dev_dbg(dev, "controller reset successful!\n");
900 } else
901 return -1;
902
903out:
904 scsi_unblock_requests(hisi_hba->shost);
905 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
906 return rc;
907}
908
John Garry0efff302015-11-18 00:50:56 +0800909static int hisi_sas_abort_task(struct sas_task *task)
910{
911 struct scsi_lun lun;
912 struct hisi_sas_tmf_task tmf_task;
913 struct domain_device *device = task->dev;
914 struct hisi_sas_device *sas_dev = device->lldd_dev;
915 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
916 struct device *dev = &hisi_hba->pdev->dev;
917 int rc = TMF_RESP_FUNC_FAILED;
918 unsigned long flags;
919
920 if (!sas_dev) {
921 dev_warn(dev, "Device has been removed\n");
922 return TMF_RESP_FUNC_FAILED;
923 }
924
925 spin_lock_irqsave(&task->task_state_lock, flags);
926 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
927 spin_unlock_irqrestore(&task->task_state_lock, flags);
928 rc = TMF_RESP_FUNC_COMPLETE;
929 goto out;
930 }
931
932 spin_unlock_irqrestore(&task->task_state_lock, flags);
933 sas_dev->dev_status = HISI_SAS_DEV_EH;
934 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
935 struct scsi_cmnd *cmnd = task->uldd_task;
936 struct hisi_sas_slot *slot = task->lldd_task;
937 u32 tag = slot->idx;
938
939 int_to_scsilun(cmnd->device->lun, &lun);
940 tmf_task.tmf = TMF_ABORT_TASK;
941 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
942
943 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
944 &tmf_task);
945
946 /* if successful, clear the task and callback forwards.*/
947 if (rc == TMF_RESP_FUNC_COMPLETE) {
948 if (task->lldd_task) {
949 struct hisi_sas_slot *slot;
950
951 slot = &hisi_hba->slot_info
952 [tmf_task.tag_of_task_to_be_managed];
953 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garry405314d2017-03-23 01:25:21 +0800954 hisi_hba->hw->slot_complete(hisi_hba, slot);
John Garry0efff302015-11-18 00:50:56 +0800955 spin_unlock_irqrestore(&hisi_hba->lock, flags);
956 }
957 }
958
John Garrydc8a49c2016-08-24 19:05:49 +0800959 hisi_sas_internal_task_abort(hisi_hba, device,
960 HISI_SAS_INT_ABT_CMD, tag);
John Garry0efff302015-11-18 00:50:56 +0800961 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
962 task->task_proto & SAS_PROTOCOL_STP) {
963 if (task->dev->dev_type == SAS_SATA_DEV) {
John Garrydc8a49c2016-08-24 19:05:49 +0800964 hisi_sas_internal_task_abort(hisi_hba, device,
965 HISI_SAS_INT_ABT_DEV, 0);
Xiang Chen7c594f02017-03-23 01:25:20 +0800966 rc = hisi_sas_softreset_ata_disk(device);
John Garry0efff302015-11-18 00:50:56 +0800967 }
John Garrydc8a49c2016-08-24 19:05:49 +0800968 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
969 /* SMP */
970 struct hisi_sas_slot *slot = task->lldd_task;
971 u32 tag = slot->idx;
John Garry0efff302015-11-18 00:50:56 +0800972
John Garrydc8a49c2016-08-24 19:05:49 +0800973 hisi_sas_internal_task_abort(hisi_hba, device,
974 HISI_SAS_INT_ABT_CMD, tag);
John Garry0efff302015-11-18 00:50:56 +0800975 }
976
977out:
978 if (rc != TMF_RESP_FUNC_COMPLETE)
979 dev_notice(dev, "abort task: rc=%d\n", rc);
980 return rc;
981}
982
983static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
984{
985 struct hisi_sas_tmf_task tmf_task;
986 int rc = TMF_RESP_FUNC_FAILED;
987
988 tmf_task.tmf = TMF_ABORT_TASK_SET;
989 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
990
991 return rc;
992}
993
994static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
995{
996 int rc = TMF_RESP_FUNC_FAILED;
997 struct hisi_sas_tmf_task tmf_task;
998
999 tmf_task.tmf = TMF_CLEAR_ACA;
1000 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1001
1002 return rc;
1003}
1004
1005static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
1006{
1007 struct sas_phy *phy = sas_get_local_phy(device);
1008 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
1009 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1010 rc = sas_phy_reset(phy, reset_type);
1011 sas_put_local_phy(phy);
1012 msleep(2000);
1013 return rc;
1014}
1015
1016static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
1017{
1018 struct hisi_sas_device *sas_dev = device->lldd_dev;
1019 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1020 unsigned long flags;
1021 int rc = TMF_RESP_FUNC_FAILED;
1022
1023 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
1024 return TMF_RESP_FUNC_FAILED;
1025 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
1026
1027 rc = hisi_sas_debug_I_T_nexus_reset(device);
1028
1029 spin_lock_irqsave(&hisi_hba->lock, flags);
1030 hisi_sas_release_task(hisi_hba, device);
1031 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1032
1033 return 0;
1034}
1035
1036static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
1037{
1038 struct hisi_sas_tmf_task tmf_task;
1039 struct hisi_sas_device *sas_dev = device->lldd_dev;
1040 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
1041 struct device *dev = &hisi_hba->pdev->dev;
1042 unsigned long flags;
1043 int rc = TMF_RESP_FUNC_FAILED;
1044
1045 tmf_task.tmf = TMF_LU_RESET;
1046 sas_dev->dev_status = HISI_SAS_DEV_EH;
1047 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
1048 if (rc == TMF_RESP_FUNC_COMPLETE) {
1049 spin_lock_irqsave(&hisi_hba->lock, flags);
1050 hisi_sas_release_task(hisi_hba, device);
1051 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1052 }
1053
1054 /* If failed, fall-through I_T_Nexus reset */
1055 dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
1056 sas_dev->device_id, rc);
1057 return rc;
1058}
1059
1060static int hisi_sas_query_task(struct sas_task *task)
1061{
1062 struct scsi_lun lun;
1063 struct hisi_sas_tmf_task tmf_task;
1064 int rc = TMF_RESP_FUNC_FAILED;
1065
1066 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1067 struct scsi_cmnd *cmnd = task->uldd_task;
1068 struct domain_device *device = task->dev;
1069 struct hisi_sas_slot *slot = task->lldd_task;
1070 u32 tag = slot->idx;
1071
1072 int_to_scsilun(cmnd->device->lun, &lun);
1073 tmf_task.tmf = TMF_QUERY_TASK;
1074 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1075
1076 rc = hisi_sas_debug_issue_ssp_tmf(device,
1077 lun.scsi_lun,
1078 &tmf_task);
1079 switch (rc) {
1080 /* The task is still in Lun, release it then */
1081 case TMF_RESP_FUNC_SUCC:
1082 /* The task is not in Lun or failed, reset the phy */
1083 case TMF_RESP_FUNC_FAILED:
1084 case TMF_RESP_FUNC_COMPLETE:
1085 break;
Xiang Chen997ee432016-11-07 20:48:35 +08001086 default:
1087 rc = TMF_RESP_FUNC_FAILED;
1088 break;
John Garry0efff302015-11-18 00:50:56 +08001089 }
1090 }
1091 return rc;
1092}
1093
John Garry441c2742016-08-24 19:05:47 +08001094static int
1095hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
1096 struct sas_task *task, int abort_flag,
1097 int task_tag)
1098{
1099 struct domain_device *device = task->dev;
1100 struct hisi_sas_device *sas_dev = device->lldd_dev;
1101 struct device *dev = &hisi_hba->pdev->dev;
1102 struct hisi_sas_port *port;
1103 struct hisi_sas_slot *slot;
John Garry2e244f02017-03-23 01:25:17 +08001104 struct asd_sas_port *sas_port = device->port;
John Garry441c2742016-08-24 19:05:47 +08001105 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1106 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1107
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001108 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
1109 return -EINVAL;
1110
John Garry441c2742016-08-24 19:05:47 +08001111 if (!device->port)
1112 return -1;
1113
John Garry2e244f02017-03-23 01:25:17 +08001114 port = to_hisi_sas_port(sas_port);
John Garry441c2742016-08-24 19:05:47 +08001115
1116 /* simply get a slot and send abort command */
1117 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1118 if (rc)
1119 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +08001120 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
1121 &dlvry_queue, &dlvry_queue_slot);
John Garry441c2742016-08-24 19:05:47 +08001122 if (rc)
1123 goto err_out_tag;
1124
1125 slot = &hisi_hba->slot_info[slot_idx];
1126 memset(slot, 0, sizeof(struct hisi_sas_slot));
1127
1128 slot->idx = slot_idx;
1129 slot->n_elem = n_elem;
1130 slot->dlvry_queue = dlvry_queue;
1131 slot->dlvry_queue_slot = dlvry_queue_slot;
1132 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1133 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1134 slot->task = task;
1135 slot->port = port;
1136 task->lldd_task = slot;
1137
1138 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1139
1140 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1141 abort_flag, task_tag);
1142 if (rc)
1143 goto err_out_tag;
1144
John Garry405314d2017-03-23 01:25:21 +08001145
1146 list_add_tail(&slot->entry, &sas_dev->list);
John Garry441c2742016-08-24 19:05:47 +08001147 spin_lock(&task->task_state_lock);
1148 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1149 spin_unlock(&task->task_state_lock);
1150
1151 hisi_hba->slot_prep = slot;
1152
John Garryf696cc32016-11-07 20:48:39 +08001153 atomic64_inc(&sas_dev->running_req);
1154
John Garry441c2742016-08-24 19:05:47 +08001155 /* send abort command to our chip */
1156 hisi_hba->hw->start_delivery(hisi_hba);
1157
1158 return 0;
1159
1160err_out_tag:
1161 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1162err_out:
1163 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1164
1165 return rc;
1166}
1167
1168/**
1169 * hisi_sas_internal_task_abort -- execute an internal
1170 * abort command for single IO command or a device
1171 * @hisi_hba: host controller struct
1172 * @device: domain device
1173 * @abort_flag: mode of operation, device or single IO
1174 * @tag: tag of IO to be aborted (only relevant to single
1175 * IO mode)
1176 */
1177static int
1178hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1179 struct domain_device *device,
1180 int abort_flag, int tag)
1181{
1182 struct sas_task *task;
1183 struct hisi_sas_device *sas_dev = device->lldd_dev;
1184 struct device *dev = &hisi_hba->pdev->dev;
1185 int res;
1186 unsigned long flags;
1187
1188 if (!hisi_hba->hw->prep_abort)
1189 return -EOPNOTSUPP;
1190
1191 task = sas_alloc_slow_task(GFP_KERNEL);
1192 if (!task)
1193 return -ENOMEM;
1194
1195 task->dev = device;
1196 task->task_proto = device->tproto;
1197 task->task_done = hisi_sas_task_done;
1198 task->slow_task->timer.data = (unsigned long)task;
1199 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1200 task->slow_task->timer.expires = jiffies + 20*HZ;
1201 add_timer(&task->slow_task->timer);
1202
1203 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1204 spin_lock_irqsave(&hisi_hba->lock, flags);
1205 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1206 task, abort_flag, tag);
1207 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1208 if (res) {
1209 del_timer(&task->slow_task->timer);
1210 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1211 res);
1212 goto exit;
1213 }
1214 wait_for_completion(&task->slow_task->completion);
1215 res = TMF_RESP_FUNC_FAILED;
1216
1217 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1218 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1219 res = TMF_RESP_FUNC_COMPLETE;
1220 goto exit;
1221 }
1222
1223 /* TMF timed out, return direct. */
1224 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1225 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1226 dev_err(dev, "internal task abort: timeout.\n");
1227 if (task->lldd_task) {
1228 struct hisi_sas_slot *slot = task->lldd_task;
1229
1230 hisi_sas_slot_task_free(hisi_hba, task, slot);
1231 }
1232 }
1233 }
1234
1235exit:
John Garry297d7302017-01-20 20:45:22 +08001236 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
John Garry441c2742016-08-24 19:05:47 +08001237 "resp: 0x%x sts 0x%x\n",
1238 SAS_ADDR(device->sas_addr),
1239 task,
1240 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1241 task->task_status.stat);
1242 sas_free_task(task);
1243
1244 return res;
1245}
1246
John Garry184a4632015-11-18 00:50:52 +08001247static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1248{
1249 hisi_sas_port_notify_formed(sas_phy);
1250}
1251
John Garry184a4632015-11-18 00:50:52 +08001252static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1253{
1254 phy->phy_attached = 0;
1255 phy->phy_type = 0;
1256 phy->port = NULL;
1257}
1258
1259void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1260{
1261 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1262 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1263 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1264
1265 if (rdy) {
1266 /* Phy down but ready */
1267 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1268 hisi_sas_port_notify_formed(sas_phy);
1269 } else {
1270 struct hisi_sas_port *port = phy->port;
1271
1272 /* Phy down and not ready */
1273 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1274 sas_phy_disconnected(sas_phy);
1275
1276 if (port) {
1277 if (phy->phy_type & PORT_TYPE_SAS) {
1278 int port_id = port->id;
1279
1280 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1281 port_id))
1282 port->port_attached = 0;
1283 } else if (phy->phy_type & PORT_TYPE_SATA)
1284 port->port_attached = 0;
1285 }
1286 hisi_sas_phy_disconnected(phy);
1287 }
1288}
1289EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1290
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001291void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1292 u32 state)
1293{
1294 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1295 int phy_no;
1296
1297 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1298 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1299 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1300 struct asd_sas_port *sas_port = sas_phy->port;
1301 struct domain_device *dev;
1302
1303 if (sas_phy->enabled) {
1304 /* Report PHY state change to libsas */
1305 if (state & (1 << phy_no))
1306 continue;
1307
1308 if (old_state & (1 << phy_no))
1309 /* PHY down but was up before */
1310 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1311 }
1312 if (!sas_port)
1313 continue;
1314 dev = sas_port->port_dev;
1315
1316 if (DEV_IS_EXPANDER(dev->dev_type))
1317 sas_ha->notify_phy_event(sas_phy, PORTE_BROADCAST_RCVD);
1318 }
1319}
1320EXPORT_SYMBOL_GPL(hisi_sas_rescan_topology);
1321
John Garrye8899fa2015-11-18 00:50:30 +08001322static struct scsi_transport_template *hisi_sas_stt;
1323
John Garry7eb78692015-11-18 00:50:31 +08001324static struct scsi_host_template hisi_sas_sht = {
1325 .module = THIS_MODULE,
1326 .name = DRV_NAME,
1327 .queuecommand = sas_queuecommand,
1328 .target_alloc = sas_target_alloc,
John Garry31eec8a2016-02-25 17:42:14 +08001329 .slave_configure = hisi_sas_slave_configure,
John Garry701f75e2015-11-18 00:50:55 +08001330 .scan_finished = hisi_sas_scan_finished,
1331 .scan_start = hisi_sas_scan_start,
John Garry7eb78692015-11-18 00:50:31 +08001332 .change_queue_depth = sas_change_queue_depth,
1333 .bios_param = sas_bios_param,
1334 .can_queue = 1,
1335 .this_id = -1,
1336 .sg_tablesize = SG_ALL,
1337 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1338 .use_clustering = ENABLE_CLUSTERING,
1339 .eh_device_reset_handler = sas_eh_device_reset_handler,
1340 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
1341 .target_destroy = sas_target_destroy,
1342 .ioctl = sas_ioctl,
1343};
1344
John Garrye8899fa2015-11-18 00:50:30 +08001345static struct sas_domain_function_template hisi_sas_transport_ops = {
John Garryabda97c2015-11-18 00:50:51 +08001346 .lldd_dev_found = hisi_sas_dev_found,
1347 .lldd_dev_gone = hisi_sas_dev_gone,
John Garry42e7a692015-11-18 00:50:49 +08001348 .lldd_execute_task = hisi_sas_queue_command,
John Garrye4189d52015-11-18 00:50:57 +08001349 .lldd_control_phy = hisi_sas_control_phy,
John Garry0efff302015-11-18 00:50:56 +08001350 .lldd_abort_task = hisi_sas_abort_task,
1351 .lldd_abort_task_set = hisi_sas_abort_task_set,
1352 .lldd_clear_aca = hisi_sas_clear_aca,
1353 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1354 .lldd_lu_reset = hisi_sas_lu_reset,
1355 .lldd_query_task = hisi_sas_query_task,
John Garry184a4632015-11-18 00:50:52 +08001356 .lldd_port_formed = hisi_sas_port_formed,
John Garrye8899fa2015-11-18 00:50:30 +08001357};
1358
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001359void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1360{
1361 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1362
1363 for (i = 0; i < hisi_hba->queue_count; i++) {
1364 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1365 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1366
1367 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1368 memset(hisi_hba->cmd_hdr[i], 0, s);
1369 dq->wr_point = 0;
1370
1371 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1372 memset(hisi_hba->complete_hdr[i], 0, s);
1373 cq->rd_point = 0;
1374 }
1375
1376 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1377 memset(hisi_hba->initial_fis, 0, s);
1378
1379 s = max_command_entries * sizeof(struct hisi_sas_iost);
1380 memset(hisi_hba->iost, 0, s);
1381
1382 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1383 memset(hisi_hba->breakpoint, 0, s);
1384
1385 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1386 memset(hisi_hba->sata_breakpoint, 0, s);
1387}
1388EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1389
John Garry6be6de12015-11-18 00:50:34 +08001390static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1391{
John Garry6be6de12015-11-18 00:50:34 +08001392 struct platform_device *pdev = hisi_hba->pdev;
1393 struct device *dev = &pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001394 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +08001395
John Garryfa42d802015-11-18 00:50:43 +08001396 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +08001397 for (i = 0; i < hisi_hba->n_phy; i++) {
1398 hisi_sas_phy_init(hisi_hba, i);
1399 hisi_hba->port[i].port_attached = 0;
1400 hisi_hba->port[i].id = -1;
John Garry976867e2015-11-18 00:50:42 +08001401 }
1402
John Garryaf740db2015-11-18 00:50:41 +08001403 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1404 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1405 hisi_hba->devices[i].device_id = i;
1406 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1407 }
1408
John Garry6be6de12015-11-18 00:50:34 +08001409 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +08001410 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
John Garry4fde02a2016-09-06 23:36:12 +08001411 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
John Garry9101a072015-11-18 00:50:37 +08001412
1413 /* Completion queue structure */
1414 cq->id = i;
1415 cq->hisi_hba = hisi_hba;
1416
John Garry4fde02a2016-09-06 23:36:12 +08001417 /* Delivery queue structure */
1418 dq->id = i;
1419 dq->hisi_hba = hisi_hba;
1420
John Garry6be6de12015-11-18 00:50:34 +08001421 /* Delivery queue */
1422 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1423 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1424 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1425 if (!hisi_hba->cmd_hdr[i])
1426 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001427
1428 /* Completion queue */
1429 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1430 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1431 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1432 if (!hisi_hba->complete_hdr[i])
1433 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001434 }
1435
1436 s = HISI_SAS_STATUS_BUF_SZ;
1437 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1438 dev, s, 16, 0);
1439 if (!hisi_hba->status_buffer_pool)
1440 goto err_out;
1441
1442 s = HISI_SAS_COMMAND_TABLE_SZ;
1443 hisi_hba->command_table_pool = dma_pool_create("command_table",
1444 dev, s, 16, 0);
1445 if (!hisi_hba->command_table_pool)
1446 goto err_out;
1447
1448 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1449 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1450 GFP_KERNEL);
1451 if (!hisi_hba->itct)
1452 goto err_out;
1453
1454 memset(hisi_hba->itct, 0, s);
1455
John Garrya8d547b2016-01-26 02:47:03 +08001456 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
John Garry6be6de12015-11-18 00:50:34 +08001457 sizeof(struct hisi_sas_slot),
1458 GFP_KERNEL);
1459 if (!hisi_hba->slot_info)
1460 goto err_out;
1461
John Garrya8d547b2016-01-26 02:47:03 +08001462 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry6be6de12015-11-18 00:50:34 +08001463 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1464 GFP_KERNEL);
1465 if (!hisi_hba->iost)
1466 goto err_out;
1467
John Garrya8d547b2016-01-26 02:47:03 +08001468 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001469 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1470 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1471 if (!hisi_hba->breakpoint)
1472 goto err_out;
1473
John Garrya8d547b2016-01-26 02:47:03 +08001474 hisi_hba->slot_index_count = max_command_entries;
John Garry433f5692016-09-06 23:36:15 +08001475 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
John Garry257efd12015-11-18 00:50:36 +08001476 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1477 if (!hisi_hba->slot_index_tags)
1478 goto err_out;
1479
John Garry6be6de12015-11-18 00:50:34 +08001480 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1481 sizeof(struct hisi_sas_sge_page), 16, 0);
1482 if (!hisi_hba->sge_page_pool)
1483 goto err_out;
1484
1485 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1486 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1487 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1488 if (!hisi_hba->initial_fis)
1489 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001490
John Garrya8d547b2016-01-26 02:47:03 +08001491 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry6be6de12015-11-18 00:50:34 +08001492 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1493 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1494 if (!hisi_hba->sata_breakpoint)
1495 goto err_out;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001496 hisi_sas_init_mem(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001497
John Garry257efd12015-11-18 00:50:36 +08001498 hisi_sas_slot_index_init(hisi_hba);
1499
John Garry7e9080e2015-11-18 00:50:40 +08001500 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1501 if (!hisi_hba->wq) {
1502 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1503 goto err_out;
1504 }
1505
John Garry6be6de12015-11-18 00:50:34 +08001506 return 0;
1507err_out:
1508 return -ENOMEM;
1509}
1510
John Garry89d53322015-11-18 00:50:35 +08001511static void hisi_sas_free(struct hisi_hba *hisi_hba)
1512{
1513 struct device *dev = &hisi_hba->pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001514 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry89d53322015-11-18 00:50:35 +08001515
1516 for (i = 0; i < hisi_hba->queue_count; i++) {
1517 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1518 if (hisi_hba->cmd_hdr[i])
1519 dma_free_coherent(dev, s,
1520 hisi_hba->cmd_hdr[i],
1521 hisi_hba->cmd_hdr_dma[i]);
1522
1523 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1524 if (hisi_hba->complete_hdr[i])
1525 dma_free_coherent(dev, s,
1526 hisi_hba->complete_hdr[i],
1527 hisi_hba->complete_hdr_dma[i]);
1528 }
1529
1530 dma_pool_destroy(hisi_hba->status_buffer_pool);
1531 dma_pool_destroy(hisi_hba->command_table_pool);
1532 dma_pool_destroy(hisi_hba->sge_page_pool);
1533
1534 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1535 if (hisi_hba->itct)
1536 dma_free_coherent(dev, s,
1537 hisi_hba->itct, hisi_hba->itct_dma);
1538
John Garrya8d547b2016-01-26 02:47:03 +08001539 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry89d53322015-11-18 00:50:35 +08001540 if (hisi_hba->iost)
1541 dma_free_coherent(dev, s,
1542 hisi_hba->iost, hisi_hba->iost_dma);
1543
John Garrya8d547b2016-01-26 02:47:03 +08001544 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001545 if (hisi_hba->breakpoint)
1546 dma_free_coherent(dev, s,
1547 hisi_hba->breakpoint,
1548 hisi_hba->breakpoint_dma);
1549
1550
1551 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1552 if (hisi_hba->initial_fis)
1553 dma_free_coherent(dev, s,
1554 hisi_hba->initial_fis,
1555 hisi_hba->initial_fis_dma);
1556
John Garrya8d547b2016-01-26 02:47:03 +08001557 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry89d53322015-11-18 00:50:35 +08001558 if (hisi_hba->sata_breakpoint)
1559 dma_free_coherent(dev, s,
1560 hisi_hba->sata_breakpoint,
1561 hisi_hba->sata_breakpoint_dma);
1562
John Garry7e9080e2015-11-18 00:50:40 +08001563 if (hisi_hba->wq)
1564 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +08001565}
John Garry6be6de12015-11-18 00:50:34 +08001566
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001567static void hisi_sas_rst_work_handler(struct work_struct *work)
1568{
1569 struct hisi_hba *hisi_hba =
1570 container_of(work, struct hisi_hba, rst_work);
1571
1572 hisi_sas_controller_reset(hisi_hba);
1573}
1574
John Garry7eb78692015-11-18 00:50:31 +08001575static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1576 const struct hisi_sas_hw *hw)
1577{
John Garrye26b2f42015-11-18 00:50:32 +08001578 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +08001579 struct Scsi_Host *shost;
1580 struct hisi_hba *hisi_hba;
1581 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +08001582 struct device_node *np = pdev->dev.of_node;
John Garry3bc45af2016-10-04 19:11:11 +08001583 struct clk *refclk;
John Garry7eb78692015-11-18 00:50:31 +08001584
1585 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
Xiaofei Tand37a0082016-11-29 23:45:57 +08001586 if (!shost) {
1587 dev_err(dev, "scsi host alloc failed\n");
1588 return NULL;
1589 }
John Garry7eb78692015-11-18 00:50:31 +08001590 hisi_hba = shost_priv(shost);
1591
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001592 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
John Garry7eb78692015-11-18 00:50:31 +08001593 hisi_hba->hw = hw;
1594 hisi_hba->pdev = pdev;
1595 hisi_hba->shost = shost;
1596 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1597
John Garryfa42d802015-11-18 00:50:43 +08001598 init_timer(&hisi_hba->timer);
1599
John Garry4d558c72016-02-04 02:26:08 +08001600 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1601 SAS_ADDR_SIZE))
John Garrye26b2f42015-11-18 00:50:32 +08001602 goto err_out;
1603
John Garry4d558c72016-02-04 02:26:08 +08001604 if (np) {
1605 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1606 "hisilicon,sas-syscon");
1607 if (IS_ERR(hisi_hba->ctrl))
1608 goto err_out;
1609
1610 if (device_property_read_u32(dev, "ctrl-reset-reg",
1611 &hisi_hba->ctrl_reset_reg))
1612 goto err_out;
1613
1614 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1615 &hisi_hba->ctrl_reset_sts_reg))
1616 goto err_out;
1617
1618 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1619 &hisi_hba->ctrl_clock_ena_reg))
1620 goto err_out;
1621 }
1622
John Garry3bc45af2016-10-04 19:11:11 +08001623 refclk = devm_clk_get(&pdev->dev, NULL);
1624 if (IS_ERR(refclk))
John Garry87e287c2017-01-20 20:45:20 +08001625 dev_dbg(dev, "no ref clk property\n");
John Garry3bc45af2016-10-04 19:11:11 +08001626 else
1627 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1628
John Garry4d558c72016-02-04 02:26:08 +08001629 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
John Garrye26b2f42015-11-18 00:50:32 +08001630 goto err_out;
1631
John Garry4d558c72016-02-04 02:26:08 +08001632 if (device_property_read_u32(dev, "queue-count",
1633 &hisi_hba->queue_count))
John Garrye26b2f42015-11-18 00:50:32 +08001634 goto err_out;
1635
John Garrya6f2c7f2016-09-06 23:36:19 +08001636 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1637 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1638 dev_err(dev, "No usable DMA addressing method\n");
1639 goto err_out;
1640 }
1641
John Garrye26b2f42015-11-18 00:50:32 +08001642 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1643 hisi_hba->regs = devm_ioremap_resource(dev, res);
1644 if (IS_ERR(hisi_hba->regs))
1645 goto err_out;
1646
John Garry89d53322015-11-18 00:50:35 +08001647 if (hisi_sas_alloc(hisi_hba, shost)) {
1648 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001649 goto err_out;
John Garry89d53322015-11-18 00:50:35 +08001650 }
John Garry6be6de12015-11-18 00:50:34 +08001651
John Garry7eb78692015-11-18 00:50:31 +08001652 return shost;
1653err_out:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001654 kfree(shost);
John Garry7eb78692015-11-18 00:50:31 +08001655 dev_err(dev, "shost alloc failed\n");
1656 return NULL;
1657}
1658
John Garry5d742422015-11-18 00:50:38 +08001659static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1660{
1661 int i;
1662
1663 for (i = 0; i < hisi_hba->n_phy; i++)
1664 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1665 hisi_hba->sas_addr,
1666 SAS_ADDR_SIZE);
1667}
1668
John Garry7eb78692015-11-18 00:50:31 +08001669int hisi_sas_probe(struct platform_device *pdev,
1670 const struct hisi_sas_hw *hw)
1671{
1672 struct Scsi_Host *shost;
1673 struct hisi_hba *hisi_hba;
1674 struct device *dev = &pdev->dev;
1675 struct asd_sas_phy **arr_phy;
1676 struct asd_sas_port **arr_port;
1677 struct sas_ha_struct *sha;
1678 int rc, phy_nr, port_nr, i;
1679
1680 shost = hisi_sas_shost_alloc(pdev, hw);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001681 if (!shost)
1682 return -ENOMEM;
John Garry7eb78692015-11-18 00:50:31 +08001683
1684 sha = SHOST_TO_SAS_HA(shost);
1685 hisi_hba = shost_priv(shost);
1686 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +08001687
John Garry7eb78692015-11-18 00:50:31 +08001688 phy_nr = port_nr = hisi_hba->n_phy;
1689
1690 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1691 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001692 if (!arr_phy || !arr_port) {
1693 rc = -ENOMEM;
1694 goto err_out_ha;
1695 }
John Garry7eb78692015-11-18 00:50:31 +08001696
1697 sha->sas_phy = arr_phy;
1698 sha->sas_port = arr_port;
John Garry7eb78692015-11-18 00:50:31 +08001699 sha->lldd_ha = hisi_hba;
1700
1701 shost->transportt = hisi_sas_stt;
1702 shost->max_id = HISI_SAS_MAX_DEVICES;
1703 shost->max_lun = ~0;
1704 shost->max_channel = 1;
1705 shost->max_cmd_len = 16;
1706 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
John Garrya8d547b2016-01-26 02:47:03 +08001707 shost->can_queue = hisi_hba->hw->max_command_entries;
1708 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
John Garry7eb78692015-11-18 00:50:31 +08001709
1710 sha->sas_ha_name = DRV_NAME;
1711 sha->dev = &hisi_hba->pdev->dev;
1712 sha->lldd_module = THIS_MODULE;
1713 sha->sas_addr = &hisi_hba->sas_addr[0];
1714 sha->num_phys = hisi_hba->n_phy;
1715 sha->core.shost = hisi_hba->shost;
1716
1717 for (i = 0; i < hisi_hba->n_phy; i++) {
1718 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1719 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1720 }
1721
John Garry5d742422015-11-18 00:50:38 +08001722 hisi_sas_init_add(hisi_hba);
1723
John Garry7eb78692015-11-18 00:50:31 +08001724 rc = scsi_add_host(shost, &pdev->dev);
1725 if (rc)
1726 goto err_out_ha;
1727
1728 rc = sas_register_ha(sha);
1729 if (rc)
1730 goto err_out_register_ha;
1731
Xiang Chen0757f042017-01-20 20:45:23 +08001732 rc = hisi_hba->hw->hw_init(hisi_hba);
1733 if (rc)
1734 goto err_out_register_ha;
1735
John Garry7eb78692015-11-18 00:50:31 +08001736 scsi_scan_host(shost);
1737
1738 return 0;
1739
1740err_out_register_ha:
1741 scsi_remove_host(shost);
1742err_out_ha:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001743 hisi_sas_free(hisi_hba);
John Garry7eb78692015-11-18 00:50:31 +08001744 kfree(shost);
1745 return rc;
1746}
1747EXPORT_SYMBOL_GPL(hisi_sas_probe);
1748
John Garry89d53322015-11-18 00:50:35 +08001749int hisi_sas_remove(struct platform_device *pdev)
1750{
1751 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1752 struct hisi_hba *hisi_hba = sha->lldd_ha;
Xiaofei Tand37a0082016-11-29 23:45:57 +08001753 struct Scsi_Host *shost = sha->core.shost;
John Garry89d53322015-11-18 00:50:35 +08001754
1755 scsi_remove_host(sha->core.shost);
1756 sas_unregister_ha(sha);
1757 sas_remove_host(sha->core.shost);
1758
1759 hisi_sas_free(hisi_hba);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001760 kfree(shost);
John Garry89d53322015-11-18 00:50:35 +08001761 return 0;
1762}
1763EXPORT_SYMBOL_GPL(hisi_sas_remove);
1764
John Garrye8899fa2015-11-18 00:50:30 +08001765static __init int hisi_sas_init(void)
1766{
1767 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1768
1769 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1770 if (!hisi_sas_stt)
1771 return -ENOMEM;
1772
1773 return 0;
1774}
1775
1776static __exit void hisi_sas_exit(void)
1777{
1778 sas_release_transport(hisi_sas_stt);
1779}
1780
1781module_init(hisi_sas_init);
1782module_exit(hisi_sas_exit);
1783
1784MODULE_VERSION(DRV_VERSION);
1785MODULE_LICENSE("GPL");
1786MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1787MODULE_DESCRIPTION("HISILICON SAS controller driver");
1788MODULE_ALIAS("platform:" DRV_NAME);