blob: cbaef90e584d3be1453cef46967573be4251fbda [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);
John Garrycac9b2a2016-02-25 17:42:11 +080024
John Garry42e7a692015-11-18 00:50:49 +080025static struct hisi_hba *dev_to_hisi_hba(struct domain_device *device)
26{
27 return device->port->ha->lldd_ha;
28}
29
John Garry2e244f02017-03-23 01:25:17 +080030struct hisi_sas_port *to_hisi_sas_port(struct asd_sas_port *sas_port)
31{
32 return container_of(sas_port, struct hisi_sas_port, sas_port);
33}
34EXPORT_SYMBOL_GPL(to_hisi_sas_port);
35
John Garry257efd12015-11-18 00:50:36 +080036static void hisi_sas_slot_index_clear(struct hisi_hba *hisi_hba, int slot_idx)
37{
38 void *bitmap = hisi_hba->slot_index_tags;
39
40 clear_bit(slot_idx, bitmap);
41}
42
John Garry42e7a692015-11-18 00:50:49 +080043static void hisi_sas_slot_index_free(struct hisi_hba *hisi_hba, int slot_idx)
44{
45 hisi_sas_slot_index_clear(hisi_hba, slot_idx);
46}
47
48static void hisi_sas_slot_index_set(struct hisi_hba *hisi_hba, int slot_idx)
49{
50 void *bitmap = hisi_hba->slot_index_tags;
51
52 set_bit(slot_idx, bitmap);
53}
54
55static int hisi_sas_slot_index_alloc(struct hisi_hba *hisi_hba, int *slot_idx)
56{
57 unsigned int index;
58 void *bitmap = hisi_hba->slot_index_tags;
59
60 index = find_first_zero_bit(bitmap, hisi_hba->slot_index_count);
61 if (index >= hisi_hba->slot_index_count)
62 return -SAS_QUEUE_FULL;
63 hisi_sas_slot_index_set(hisi_hba, index);
64 *slot_idx = index;
65 return 0;
66}
67
John Garry257efd12015-11-18 00:50:36 +080068static void hisi_sas_slot_index_init(struct hisi_hba *hisi_hba)
69{
70 int i;
71
72 for (i = 0; i < hisi_hba->slot_index_count; ++i)
73 hisi_sas_slot_index_clear(hisi_hba, i);
74}
John Garry27a3f222015-11-18 00:50:50 +080075
76void hisi_sas_slot_task_free(struct hisi_hba *hisi_hba, struct sas_task *task,
77 struct hisi_sas_slot *slot)
78{
79 struct device *dev = &hisi_hba->pdev->dev;
Xiang Chen13c59902017-01-20 20:45:24 +080080 struct domain_device *device = task->dev;
81 struct hisi_sas_device *sas_dev = device->lldd_dev;
John Garry27a3f222015-11-18 00:50:50 +080082
83 if (!slot->task)
84 return;
85
86 if (!sas_protocol_ata(task->task_proto))
87 if (slot->n_elem)
88 dma_unmap_sg(dev, task->scatter, slot->n_elem,
89 task->data_dir);
90
91 if (slot->command_table)
92 dma_pool_free(hisi_hba->command_table_pool,
93 slot->command_table, slot->command_table_dma);
94
95 if (slot->status_buffer)
96 dma_pool_free(hisi_hba->status_buffer_pool,
97 slot->status_buffer, slot->status_buffer_dma);
98
99 if (slot->sge_page)
100 dma_pool_free(hisi_hba->sge_page_pool, slot->sge_page,
101 slot->sge_page_dma);
102
103 list_del_init(&slot->entry);
104 task->lldd_task = NULL;
105 slot->task = NULL;
106 slot->port = NULL;
107 hisi_sas_slot_index_free(hisi_hba, slot->idx);
Xiang Chen13c59902017-01-20 20:45:24 +0800108 if (sas_dev)
109 atomic64_dec(&sas_dev->running_req);
John Garry59ba49f2016-09-06 23:36:14 +0800110 /* slot memory is fully zeroed when it is reused */
John Garry27a3f222015-11-18 00:50:50 +0800111}
112EXPORT_SYMBOL_GPL(hisi_sas_slot_task_free);
113
John Garry66ee9992015-11-18 00:50:54 +0800114static int hisi_sas_task_prep_smp(struct hisi_hba *hisi_hba,
115 struct hisi_sas_slot *slot)
116{
117 return hisi_hba->hw->prep_smp(hisi_hba, slot);
118}
119
John Garry42e7a692015-11-18 00:50:49 +0800120static int hisi_sas_task_prep_ssp(struct hisi_hba *hisi_hba,
121 struct hisi_sas_slot *slot, int is_tmf,
122 struct hisi_sas_tmf_task *tmf)
123{
124 return hisi_hba->hw->prep_ssp(hisi_hba, slot, is_tmf, tmf);
125}
126
John Garry6f2ff1a2016-01-26 02:47:20 +0800127static int hisi_sas_task_prep_ata(struct hisi_hba *hisi_hba,
128 struct hisi_sas_slot *slot)
129{
130 return hisi_hba->hw->prep_stp(hisi_hba, slot);
131}
132
John Garry441c2742016-08-24 19:05:47 +0800133static int hisi_sas_task_prep_abort(struct hisi_hba *hisi_hba,
134 struct hisi_sas_slot *slot,
135 int device_id, int abort_flag, int tag_to_abort)
136{
137 return hisi_hba->hw->prep_abort(hisi_hba, slot,
138 device_id, abort_flag, tag_to_abort);
139}
140
John Garrycac9b2a2016-02-25 17:42:11 +0800141/*
142 * This function will issue an abort TMF regardless of whether the
143 * task is in the sdev or not. Then it will do the task complete
144 * cleanup and callbacks.
145 */
146static void hisi_sas_slot_abort(struct work_struct *work)
147{
148 struct hisi_sas_slot *abort_slot =
149 container_of(work, struct hisi_sas_slot, abort_slot);
150 struct sas_task *task = abort_slot->task;
151 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
152 struct scsi_cmnd *cmnd = task->uldd_task;
153 struct hisi_sas_tmf_task tmf_task;
John Garrycac9b2a2016-02-25 17:42:11 +0800154 struct scsi_lun lun;
155 struct device *dev = &hisi_hba->pdev->dev;
156 int tag = abort_slot->idx;
John Garryda7b66e2017-01-03 20:24:50 +0800157 unsigned long flags;
John Garrycac9b2a2016-02-25 17:42:11 +0800158
159 if (!(task->task_proto & SAS_PROTOCOL_SSP)) {
160 dev_err(dev, "cannot abort slot for non-ssp task\n");
161 goto out;
162 }
163
164 int_to_scsilun(cmnd->device->lun, &lun);
165 tmf_task.tmf = TMF_ABORT_TASK;
166 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
167
168 hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun, &tmf_task);
169out:
170 /* Do cleanup for this task */
John Garryda7b66e2017-01-03 20:24:50 +0800171 spin_lock_irqsave(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800172 hisi_sas_slot_task_free(hisi_hba, task, abort_slot);
John Garryda7b66e2017-01-03 20:24:50 +0800173 spin_unlock_irqrestore(&hisi_hba->lock, flags);
John Garrycac9b2a2016-02-25 17:42:11 +0800174 if (task->task_done)
175 task->task_done(task);
John Garrycac9b2a2016-02-25 17:42:11 +0800176}
177
John Garry42e7a692015-11-18 00:50:49 +0800178static int hisi_sas_task_prep(struct sas_task *task, struct hisi_hba *hisi_hba,
179 int is_tmf, struct hisi_sas_tmf_task *tmf,
180 int *pass)
181{
182 struct domain_device *device = task->dev;
183 struct hisi_sas_device *sas_dev = device->lldd_dev;
184 struct hisi_sas_port *port;
185 struct hisi_sas_slot *slot;
186 struct hisi_sas_cmd_hdr *cmd_hdr_base;
John Garry2e244f02017-03-23 01:25:17 +0800187 struct asd_sas_port *sas_port = device->port;
John Garry42e7a692015-11-18 00:50:49 +0800188 struct device *dev = &hisi_hba->pdev->dev;
189 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
190
John Garry2e244f02017-03-23 01:25:17 +0800191 if (!sas_port) {
John Garry42e7a692015-11-18 00:50:49 +0800192 struct task_status_struct *ts = &task->task_status;
193
194 ts->resp = SAS_TASK_UNDELIVERED;
195 ts->stat = SAS_PHY_DOWN;
196 /*
197 * libsas will use dev->port, should
198 * not call task_done for sata
199 */
200 if (device->dev_type != SAS_SATA_DEV)
201 task->task_done(task);
202 return 0;
203 }
204
205 if (DEV_IS_GONE(sas_dev)) {
206 if (sas_dev)
207 dev_info(dev, "task prep: device %llu not ready\n",
208 sas_dev->device_id);
209 else
210 dev_info(dev, "task prep: device %016llx not ready\n",
211 SAS_ADDR(device->sas_addr));
212
213 rc = SAS_PHY_DOWN;
214 return rc;
215 }
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
310 list_add_tail(&slot->entry, &port->list);
311 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;
426 break;
427 }
428 }
429 spin_unlock(&hisi_hba->lock);
430
431 return sas_dev;
432}
433
434static int hisi_sas_dev_found(struct domain_device *device)
435{
436 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
437 struct domain_device *parent_dev = device->parent;
438 struct hisi_sas_device *sas_dev;
439 struct device *dev = &hisi_hba->pdev->dev;
440
John Garry685b6d62016-04-15 21:36:36 +0800441 if (hisi_hba->hw->alloc_dev)
442 sas_dev = hisi_hba->hw->alloc_dev(device);
443 else
444 sas_dev = hisi_sas_alloc_dev(device);
John Garryabda97c2015-11-18 00:50:51 +0800445 if (!sas_dev) {
446 dev_err(dev, "fail alloc dev: max support %d devices\n",
447 HISI_SAS_MAX_DEVICES);
448 return -EINVAL;
449 }
450
451 device->lldd_dev = sas_dev;
452 hisi_hba->hw->setup_itct(hisi_hba, sas_dev);
453
454 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
455 int phy_no;
456 u8 phy_num = parent_dev->ex_dev.num_phys;
457 struct ex_phy *phy;
458
459 for (phy_no = 0; phy_no < phy_num; phy_no++) {
460 phy = &parent_dev->ex_dev.ex_phy[phy_no];
461 if (SAS_ADDR(phy->attached_sas_addr) ==
462 SAS_ADDR(device->sas_addr)) {
463 sas_dev->attached_phy = phy_no;
464 break;
465 }
466 }
467
468 if (phy_no == phy_num) {
469 dev_info(dev, "dev found: no attached "
470 "dev:%016llx at ex:%016llx\n",
471 SAS_ADDR(device->sas_addr),
472 SAS_ADDR(parent_dev->sas_addr));
473 return -EINVAL;
474 }
475 }
476
477 return 0;
478}
479
John Garry31eec8a2016-02-25 17:42:14 +0800480static int hisi_sas_slave_configure(struct scsi_device *sdev)
481{
482 struct domain_device *dev = sdev_to_domain_dev(sdev);
483 int ret = sas_slave_configure(sdev);
484
485 if (ret)
486 return ret;
487 if (!dev_is_sata(dev))
488 sas_change_queue_depth(sdev, 64);
489
490 return 0;
491}
492
John Garry701f75e2015-11-18 00:50:55 +0800493static void hisi_sas_scan_start(struct Scsi_Host *shost)
494{
495 struct hisi_hba *hisi_hba = shost_priv(shost);
496 int i;
497
498 for (i = 0; i < hisi_hba->n_phy; ++i)
499 hisi_sas_bytes_dmaed(hisi_hba, i);
500
501 hisi_hba->scan_finished = 1;
502}
503
504static int hisi_sas_scan_finished(struct Scsi_Host *shost, unsigned long time)
505{
506 struct hisi_hba *hisi_hba = shost_priv(shost);
507 struct sas_ha_struct *sha = &hisi_hba->sha;
508
509 if (hisi_hba->scan_finished == 0)
510 return 0;
511
512 sas_drain_work(sha);
513 return 1;
514}
515
John Garry66139922015-11-18 00:50:48 +0800516static void hisi_sas_phyup_work(struct work_struct *work)
517{
518 struct hisi_sas_phy *phy =
519 container_of(work, struct hisi_sas_phy, phyup_ws);
520 struct hisi_hba *hisi_hba = phy->hisi_hba;
521 struct asd_sas_phy *sas_phy = &phy->sas_phy;
522 int phy_no = sas_phy->id;
523
524 hisi_hba->hw->sl_notify(hisi_hba, phy_no); /* This requires a sleep */
525 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
526}
John Garry976867e2015-11-18 00:50:42 +0800527
528static void hisi_sas_phy_init(struct hisi_hba *hisi_hba, int phy_no)
529{
530 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
531 struct asd_sas_phy *sas_phy = &phy->sas_phy;
532
533 phy->hisi_hba = hisi_hba;
534 phy->port = NULL;
535 init_timer(&phy->timer);
536 sas_phy->enabled = (phy_no < hisi_hba->n_phy) ? 1 : 0;
537 sas_phy->class = SAS;
538 sas_phy->iproto = SAS_PROTOCOL_ALL;
539 sas_phy->tproto = 0;
540 sas_phy->type = PHY_TYPE_PHYSICAL;
541 sas_phy->role = PHY_ROLE_INITIATOR;
542 sas_phy->oob_mode = OOB_NOT_CONNECTED;
543 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
544 sas_phy->id = phy_no;
545 sas_phy->sas_addr = &hisi_hba->sas_addr[0];
546 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
547 sas_phy->ha = (struct sas_ha_struct *)hisi_hba->shost->hostdata;
548 sas_phy->lldd_phy = phy;
John Garry66139922015-11-18 00:50:48 +0800549
550 INIT_WORK(&phy->phyup_ws, hisi_sas_phyup_work);
John Garry976867e2015-11-18 00:50:42 +0800551}
552
John Garry184a4632015-11-18 00:50:52 +0800553static void hisi_sas_port_notify_formed(struct asd_sas_phy *sas_phy)
554{
555 struct sas_ha_struct *sas_ha = sas_phy->ha;
556 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
557 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
558 struct asd_sas_port *sas_port = sas_phy->port;
John Garry2e244f02017-03-23 01:25:17 +0800559 struct hisi_sas_port *port = to_hisi_sas_port(sas_port);
John Garry184a4632015-11-18 00:50:52 +0800560 unsigned long flags;
561
562 if (!sas_port)
563 return;
564
565 spin_lock_irqsave(&hisi_hba->lock, flags);
566 port->port_attached = 1;
567 port->id = phy->port_id;
568 phy->port = port;
569 sas_port->lldd_port = port;
570 spin_unlock_irqrestore(&hisi_hba->lock, flags);
571}
572
573static void hisi_sas_do_release_task(struct hisi_hba *hisi_hba, int phy_no,
574 struct domain_device *device)
575{
576 struct hisi_sas_phy *phy;
577 struct hisi_sas_port *port;
578 struct hisi_sas_slot *slot, *slot2;
579 struct device *dev = &hisi_hba->pdev->dev;
580
581 phy = &hisi_hba->phy[phy_no];
582 port = phy->port;
583 if (!port)
584 return;
585
586 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
587 struct sas_task *task;
588
589 task = slot->task;
590 if (device && task->dev != device)
591 continue;
592
593 dev_info(dev, "Release slot [%d:%d], task [%p]:\n",
594 slot->dlvry_queue, slot->dlvry_queue_slot, task);
595 hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
596 }
597}
598
599static void hisi_sas_port_notify_deformed(struct asd_sas_phy *sas_phy)
600{
601 struct domain_device *device;
602 struct hisi_sas_phy *phy = sas_phy->lldd_phy;
603 struct asd_sas_port *sas_port = sas_phy->port;
604
605 list_for_each_entry(device, &sas_port->dev_list, dev_list_node)
606 hisi_sas_do_release_task(phy->hisi_hba, sas_phy->id, device);
607}
608
609static void hisi_sas_release_task(struct hisi_hba *hisi_hba,
610 struct domain_device *device)
611{
612 struct asd_sas_port *port = device->port;
613 struct asd_sas_phy *sas_phy;
614
615 list_for_each_entry(sas_phy, &port->phy_list, port_phy_el)
616 hisi_sas_do_release_task(hisi_hba, sas_phy->id, device);
617}
618
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800619static void hisi_sas_release_tasks(struct hisi_hba *hisi_hba)
620{
621 int i;
622
623 for (i = 0; i < HISI_SAS_MAX_PHYS; i++) {
624 struct hisi_sas_phy *phy = &hisi_hba->phy[i];
625 struct asd_sas_phy *sas_phy = &phy->sas_phy;
626
627 if (!sas_phy->port)
628 continue;
629 hisi_sas_port_notify_deformed(sas_phy);
630 }
631}
632
John Garryabda97c2015-11-18 00:50:51 +0800633static void hisi_sas_dev_gone(struct domain_device *device)
634{
635 struct hisi_sas_device *sas_dev = device->lldd_dev;
636 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
637 struct device *dev = &hisi_hba->pdev->dev;
638 u64 dev_id = sas_dev->device_id;
639
640 dev_info(dev, "found dev[%lld:%x] is gone\n",
641 sas_dev->device_id, sas_dev->dev_type);
642
John Garry40f27022016-08-24 19:05:48 +0800643 hisi_sas_internal_task_abort(hisi_hba, device,
644 HISI_SAS_INT_ABT_DEV, 0);
645
John Garryabda97c2015-11-18 00:50:51 +0800646 hisi_hba->hw->free_device(hisi_hba, sas_dev);
647 device->lldd_dev = NULL;
648 memset(sas_dev, 0, sizeof(*sas_dev));
649 sas_dev->device_id = dev_id;
650 sas_dev->dev_type = SAS_PHY_UNUSED;
651 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
652}
John Garry42e7a692015-11-18 00:50:49 +0800653
654static int hisi_sas_queue_command(struct sas_task *task, gfp_t gfp_flags)
655{
656 return hisi_sas_task_exec(task, gfp_flags, 0, NULL);
657}
658
John Garrye4189d52015-11-18 00:50:57 +0800659static int hisi_sas_control_phy(struct asd_sas_phy *sas_phy, enum phy_func func,
660 void *funcdata)
661{
662 struct sas_ha_struct *sas_ha = sas_phy->ha;
663 struct hisi_hba *hisi_hba = sas_ha->lldd_ha;
664 int phy_no = sas_phy->id;
665
666 switch (func) {
667 case PHY_FUNC_HARD_RESET:
668 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
669 break;
670
671 case PHY_FUNC_LINK_RESET:
672 hisi_hba->hw->phy_enable(hisi_hba, phy_no);
673 hisi_hba->hw->phy_hard_reset(hisi_hba, phy_no);
674 break;
675
676 case PHY_FUNC_DISABLE:
677 hisi_hba->hw->phy_disable(hisi_hba, phy_no);
678 break;
679
680 case PHY_FUNC_SET_LINK_RATE:
Xiang Chen2ae75782016-11-07 20:48:40 +0800681 hisi_hba->hw->phy_set_linkrate(hisi_hba, phy_no, funcdata);
682 break;
683
John Garrye4189d52015-11-18 00:50:57 +0800684 case PHY_FUNC_RELEASE_SPINUP_HOLD:
685 default:
686 return -EOPNOTSUPP;
687 }
688 return 0;
689}
John Garry184a4632015-11-18 00:50:52 +0800690
John Garry0efff302015-11-18 00:50:56 +0800691static void hisi_sas_task_done(struct sas_task *task)
692{
693 if (!del_timer(&task->slow_task->timer))
694 return;
695 complete(&task->slow_task->completion);
696}
697
698static void hisi_sas_tmf_timedout(unsigned long data)
699{
700 struct sas_task *task = (struct sas_task *)data;
701
702 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
703 complete(&task->slow_task->completion);
704}
705
706#define TASK_TIMEOUT 20
707#define TASK_RETRY 3
708static int hisi_sas_exec_internal_tmf_task(struct domain_device *device,
709 void *parameter, u32 para_len,
710 struct hisi_sas_tmf_task *tmf)
711{
712 struct hisi_sas_device *sas_dev = device->lldd_dev;
713 struct hisi_hba *hisi_hba = sas_dev->hisi_hba;
714 struct device *dev = &hisi_hba->pdev->dev;
715 struct sas_task *task;
716 int res, retry;
717
718 for (retry = 0; retry < TASK_RETRY; retry++) {
719 task = sas_alloc_slow_task(GFP_KERNEL);
720 if (!task)
721 return -ENOMEM;
722
723 task->dev = device;
724 task->task_proto = device->tproto;
725
726 memcpy(&task->ssp_task, parameter, para_len);
727 task->task_done = hisi_sas_task_done;
728
729 task->slow_task->timer.data = (unsigned long) task;
730 task->slow_task->timer.function = hisi_sas_tmf_timedout;
731 task->slow_task->timer.expires = jiffies + TASK_TIMEOUT*HZ;
732 add_timer(&task->slow_task->timer);
733
734 res = hisi_sas_task_exec(task, GFP_KERNEL, 1, tmf);
735
736 if (res) {
737 del_timer(&task->slow_task->timer);
738 dev_err(dev, "abort tmf: executing internal task failed: %d\n",
739 res);
740 goto ex_err;
741 }
742
743 wait_for_completion(&task->slow_task->completion);
744 res = TMF_RESP_FUNC_FAILED;
745 /* Even TMF timed out, return direct. */
746 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
747 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
748 dev_err(dev, "abort tmf: TMF task[%d] timeout\n",
749 tmf->tag_of_task_to_be_managed);
750 if (task->lldd_task) {
751 struct hisi_sas_slot *slot =
752 task->lldd_task;
753
754 hisi_sas_slot_task_free(hisi_hba,
755 task, slot);
756 }
757
758 goto ex_err;
759 }
760 }
761
762 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry1af1b802016-02-25 17:42:10 +0800763 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
John Garry0efff302015-11-18 00:50:56 +0800764 res = TMF_RESP_FUNC_COMPLETE;
765 break;
766 }
767
768 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry4ffde482016-08-24 19:05:53 +0800769 task->task_status.stat == TMF_RESP_FUNC_SUCC) {
770 res = TMF_RESP_FUNC_SUCC;
771 break;
772 }
773
774 if (task->task_status.resp == SAS_TASK_COMPLETE &&
John Garry0efff302015-11-18 00:50:56 +0800775 task->task_status.stat == SAS_DATA_UNDERRUN) {
776 /* no error, but return the number of bytes of
777 * underrun
778 */
779 dev_warn(dev, "abort tmf: task to dev %016llx "
780 "resp: 0x%x sts 0x%x underrun\n",
781 SAS_ADDR(device->sas_addr),
782 task->task_status.resp,
783 task->task_status.stat);
784 res = task->task_status.residual;
785 break;
786 }
787
788 if (task->task_status.resp == SAS_TASK_COMPLETE &&
789 task->task_status.stat == SAS_DATA_OVERRUN) {
790 dev_warn(dev, "abort tmf: blocked task error\n");
791 res = -EMSGSIZE;
792 break;
793 }
794
795 dev_warn(dev, "abort tmf: task to dev "
796 "%016llx resp: 0x%x status 0x%x\n",
797 SAS_ADDR(device->sas_addr), task->task_status.resp,
798 task->task_status.stat);
799 sas_free_task(task);
800 task = NULL;
801 }
802ex_err:
Xiang Chend2d7e7a2016-11-07 20:48:34 +0800803 if (retry == TASK_RETRY)
804 dev_warn(dev, "abort tmf: executing internal task failed!\n");
John Garry0efff302015-11-18 00:50:56 +0800805 sas_free_task(task);
806 return res;
807}
808
809static int hisi_sas_debug_issue_ssp_tmf(struct domain_device *device,
810 u8 *lun, struct hisi_sas_tmf_task *tmf)
811{
812 struct sas_ssp_task ssp_task;
813
814 if (!(device->tproto & SAS_PROTOCOL_SSP))
815 return TMF_RESP_FUNC_ESUPP;
816
817 memcpy(ssp_task.LUN, lun, 8);
818
819 return hisi_sas_exec_internal_tmf_task(device, &ssp_task,
820 sizeof(ssp_task), tmf);
821}
822
Xiang Chen06ec0fb2017-03-23 01:25:18 +0800823static int hisi_sas_controller_reset(struct hisi_hba *hisi_hba)
824{
825 int rc;
826
827 if (!hisi_hba->hw->soft_reset)
828 return -1;
829
830 if (!test_and_set_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)) {
831 struct device *dev = &hisi_hba->pdev->dev;
832 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
833 unsigned long flags;
834
835 dev_dbg(dev, "controller reset begins!\n");
836 scsi_block_requests(hisi_hba->shost);
837 rc = hisi_hba->hw->soft_reset(hisi_hba);
838 if (rc) {
839 dev_warn(dev, "controller reset failed (%d)\n", rc);
840 goto out;
841 }
842 spin_lock_irqsave(&hisi_hba->lock, flags);
843 hisi_sas_release_tasks(hisi_hba);
844 spin_unlock_irqrestore(&hisi_hba->lock, flags);
845
846 sas_ha->notify_ha_event(sas_ha, HAE_RESET);
847 dev_dbg(dev, "controller reset successful!\n");
848 } else
849 return -1;
850
851out:
852 scsi_unblock_requests(hisi_hba->shost);
853 clear_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags);
854 return rc;
855}
856
John Garry0efff302015-11-18 00:50:56 +0800857static int hisi_sas_abort_task(struct sas_task *task)
858{
859 struct scsi_lun lun;
860 struct hisi_sas_tmf_task tmf_task;
861 struct domain_device *device = task->dev;
862 struct hisi_sas_device *sas_dev = device->lldd_dev;
863 struct hisi_hba *hisi_hba = dev_to_hisi_hba(task->dev);
864 struct device *dev = &hisi_hba->pdev->dev;
865 int rc = TMF_RESP_FUNC_FAILED;
866 unsigned long flags;
867
868 if (!sas_dev) {
869 dev_warn(dev, "Device has been removed\n");
870 return TMF_RESP_FUNC_FAILED;
871 }
872
873 spin_lock_irqsave(&task->task_state_lock, flags);
874 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
875 spin_unlock_irqrestore(&task->task_state_lock, flags);
876 rc = TMF_RESP_FUNC_COMPLETE;
877 goto out;
878 }
879
880 spin_unlock_irqrestore(&task->task_state_lock, flags);
881 sas_dev->dev_status = HISI_SAS_DEV_EH;
882 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
883 struct scsi_cmnd *cmnd = task->uldd_task;
884 struct hisi_sas_slot *slot = task->lldd_task;
885 u32 tag = slot->idx;
886
887 int_to_scsilun(cmnd->device->lun, &lun);
888 tmf_task.tmf = TMF_ABORT_TASK;
889 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
890
891 rc = hisi_sas_debug_issue_ssp_tmf(task->dev, lun.scsi_lun,
892 &tmf_task);
893
894 /* if successful, clear the task and callback forwards.*/
895 if (rc == TMF_RESP_FUNC_COMPLETE) {
896 if (task->lldd_task) {
897 struct hisi_sas_slot *slot;
898
899 slot = &hisi_hba->slot_info
900 [tmf_task.tag_of_task_to_be_managed];
901 spin_lock_irqsave(&hisi_hba->lock, flags);
902 hisi_hba->hw->slot_complete(hisi_hba, slot, 1);
903 spin_unlock_irqrestore(&hisi_hba->lock, flags);
904 }
905 }
906
John Garrydc8a49c2016-08-24 19:05:49 +0800907 hisi_sas_internal_task_abort(hisi_hba, device,
908 HISI_SAS_INT_ABT_CMD, tag);
John Garry0efff302015-11-18 00:50:56 +0800909 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
910 task->task_proto & SAS_PROTOCOL_STP) {
911 if (task->dev->dev_type == SAS_SATA_DEV) {
John Garrydc8a49c2016-08-24 19:05:49 +0800912 hisi_sas_internal_task_abort(hisi_hba, device,
913 HISI_SAS_INT_ABT_DEV, 0);
John Garry0efff302015-11-18 00:50:56 +0800914 rc = TMF_RESP_FUNC_COMPLETE;
John Garry0efff302015-11-18 00:50:56 +0800915 }
John Garrydc8a49c2016-08-24 19:05:49 +0800916 } else if (task->task_proto & SAS_PROTOCOL_SMP) {
917 /* SMP */
918 struct hisi_sas_slot *slot = task->lldd_task;
919 u32 tag = slot->idx;
John Garry0efff302015-11-18 00:50:56 +0800920
John Garrydc8a49c2016-08-24 19:05:49 +0800921 hisi_sas_internal_task_abort(hisi_hba, device,
922 HISI_SAS_INT_ABT_CMD, tag);
John Garry0efff302015-11-18 00:50:56 +0800923 }
924
925out:
926 if (rc != TMF_RESP_FUNC_COMPLETE)
927 dev_notice(dev, "abort task: rc=%d\n", rc);
928 return rc;
929}
930
931static int hisi_sas_abort_task_set(struct domain_device *device, u8 *lun)
932{
933 struct hisi_sas_tmf_task tmf_task;
934 int rc = TMF_RESP_FUNC_FAILED;
935
936 tmf_task.tmf = TMF_ABORT_TASK_SET;
937 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
938
939 return rc;
940}
941
942static int hisi_sas_clear_aca(struct domain_device *device, u8 *lun)
943{
944 int rc = TMF_RESP_FUNC_FAILED;
945 struct hisi_sas_tmf_task tmf_task;
946
947 tmf_task.tmf = TMF_CLEAR_ACA;
948 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
949
950 return rc;
951}
952
953static int hisi_sas_debug_I_T_nexus_reset(struct domain_device *device)
954{
955 struct sas_phy *phy = sas_get_local_phy(device);
956 int rc, reset_type = (device->dev_type == SAS_SATA_DEV ||
957 (device->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
958 rc = sas_phy_reset(phy, reset_type);
959 sas_put_local_phy(phy);
960 msleep(2000);
961 return rc;
962}
963
964static int hisi_sas_I_T_nexus_reset(struct domain_device *device)
965{
966 struct hisi_sas_device *sas_dev = device->lldd_dev;
967 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
968 unsigned long flags;
969 int rc = TMF_RESP_FUNC_FAILED;
970
971 if (sas_dev->dev_status != HISI_SAS_DEV_EH)
972 return TMF_RESP_FUNC_FAILED;
973 sas_dev->dev_status = HISI_SAS_DEV_NORMAL;
974
975 rc = hisi_sas_debug_I_T_nexus_reset(device);
976
977 spin_lock_irqsave(&hisi_hba->lock, flags);
978 hisi_sas_release_task(hisi_hba, device);
979 spin_unlock_irqrestore(&hisi_hba->lock, flags);
980
981 return 0;
982}
983
984static int hisi_sas_lu_reset(struct domain_device *device, u8 *lun)
985{
986 struct hisi_sas_tmf_task tmf_task;
987 struct hisi_sas_device *sas_dev = device->lldd_dev;
988 struct hisi_hba *hisi_hba = dev_to_hisi_hba(device);
989 struct device *dev = &hisi_hba->pdev->dev;
990 unsigned long flags;
991 int rc = TMF_RESP_FUNC_FAILED;
992
993 tmf_task.tmf = TMF_LU_RESET;
994 sas_dev->dev_status = HISI_SAS_DEV_EH;
995 rc = hisi_sas_debug_issue_ssp_tmf(device, lun, &tmf_task);
996 if (rc == TMF_RESP_FUNC_COMPLETE) {
997 spin_lock_irqsave(&hisi_hba->lock, flags);
998 hisi_sas_release_task(hisi_hba, device);
999 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1000 }
1001
1002 /* If failed, fall-through I_T_Nexus reset */
1003 dev_err(dev, "lu_reset: for device[%llx]:rc= %d\n",
1004 sas_dev->device_id, rc);
1005 return rc;
1006}
1007
1008static int hisi_sas_query_task(struct sas_task *task)
1009{
1010 struct scsi_lun lun;
1011 struct hisi_sas_tmf_task tmf_task;
1012 int rc = TMF_RESP_FUNC_FAILED;
1013
1014 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1015 struct scsi_cmnd *cmnd = task->uldd_task;
1016 struct domain_device *device = task->dev;
1017 struct hisi_sas_slot *slot = task->lldd_task;
1018 u32 tag = slot->idx;
1019
1020 int_to_scsilun(cmnd->device->lun, &lun);
1021 tmf_task.tmf = TMF_QUERY_TASK;
1022 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1023
1024 rc = hisi_sas_debug_issue_ssp_tmf(device,
1025 lun.scsi_lun,
1026 &tmf_task);
1027 switch (rc) {
1028 /* The task is still in Lun, release it then */
1029 case TMF_RESP_FUNC_SUCC:
1030 /* The task is not in Lun or failed, reset the phy */
1031 case TMF_RESP_FUNC_FAILED:
1032 case TMF_RESP_FUNC_COMPLETE:
1033 break;
Xiang Chen997ee432016-11-07 20:48:35 +08001034 default:
1035 rc = TMF_RESP_FUNC_FAILED;
1036 break;
John Garry0efff302015-11-18 00:50:56 +08001037 }
1038 }
1039 return rc;
1040}
1041
John Garry441c2742016-08-24 19:05:47 +08001042static int
1043hisi_sas_internal_abort_task_exec(struct hisi_hba *hisi_hba, u64 device_id,
1044 struct sas_task *task, int abort_flag,
1045 int task_tag)
1046{
1047 struct domain_device *device = task->dev;
1048 struct hisi_sas_device *sas_dev = device->lldd_dev;
1049 struct device *dev = &hisi_hba->pdev->dev;
1050 struct hisi_sas_port *port;
1051 struct hisi_sas_slot *slot;
John Garry2e244f02017-03-23 01:25:17 +08001052 struct asd_sas_port *sas_port = device->port;
John Garry441c2742016-08-24 19:05:47 +08001053 struct hisi_sas_cmd_hdr *cmd_hdr_base;
1054 int dlvry_queue_slot, dlvry_queue, n_elem = 0, rc, slot_idx;
1055
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001056 if (unlikely(test_bit(HISI_SAS_RESET_BIT, &hisi_hba->flags)))
1057 return -EINVAL;
1058
John Garry441c2742016-08-24 19:05:47 +08001059 if (!device->port)
1060 return -1;
1061
John Garry2e244f02017-03-23 01:25:17 +08001062 port = to_hisi_sas_port(sas_port);
John Garry441c2742016-08-24 19:05:47 +08001063
1064 /* simply get a slot and send abort command */
1065 rc = hisi_sas_slot_index_alloc(hisi_hba, &slot_idx);
1066 if (rc)
1067 goto err_out;
Xiang Chenc70f1fb2016-11-07 20:48:31 +08001068 rc = hisi_hba->hw->get_free_slot(hisi_hba, sas_dev->device_id,
1069 &dlvry_queue, &dlvry_queue_slot);
John Garry441c2742016-08-24 19:05:47 +08001070 if (rc)
1071 goto err_out_tag;
1072
1073 slot = &hisi_hba->slot_info[slot_idx];
1074 memset(slot, 0, sizeof(struct hisi_sas_slot));
1075
1076 slot->idx = slot_idx;
1077 slot->n_elem = n_elem;
1078 slot->dlvry_queue = dlvry_queue;
1079 slot->dlvry_queue_slot = dlvry_queue_slot;
1080 cmd_hdr_base = hisi_hba->cmd_hdr[dlvry_queue];
1081 slot->cmd_hdr = &cmd_hdr_base[dlvry_queue_slot];
1082 slot->task = task;
1083 slot->port = port;
1084 task->lldd_task = slot;
1085
1086 memset(slot->cmd_hdr, 0, sizeof(struct hisi_sas_cmd_hdr));
1087
1088 rc = hisi_sas_task_prep_abort(hisi_hba, slot, device_id,
1089 abort_flag, task_tag);
1090 if (rc)
1091 goto err_out_tag;
1092
1093 /* Port structure is static for the HBA, so
1094 * even if the port is deformed it is ok
1095 * to reference.
1096 */
1097 list_add_tail(&slot->entry, &port->list);
1098 spin_lock(&task->task_state_lock);
1099 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
1100 spin_unlock(&task->task_state_lock);
1101
1102 hisi_hba->slot_prep = slot;
1103
John Garryf696cc32016-11-07 20:48:39 +08001104 atomic64_inc(&sas_dev->running_req);
1105
John Garry441c2742016-08-24 19:05:47 +08001106 /* send abort command to our chip */
1107 hisi_hba->hw->start_delivery(hisi_hba);
1108
1109 return 0;
1110
1111err_out_tag:
1112 hisi_sas_slot_index_free(hisi_hba, slot_idx);
1113err_out:
1114 dev_err(dev, "internal abort task prep: failed[%d]!\n", rc);
1115
1116 return rc;
1117}
1118
1119/**
1120 * hisi_sas_internal_task_abort -- execute an internal
1121 * abort command for single IO command or a device
1122 * @hisi_hba: host controller struct
1123 * @device: domain device
1124 * @abort_flag: mode of operation, device or single IO
1125 * @tag: tag of IO to be aborted (only relevant to single
1126 * IO mode)
1127 */
1128static int
1129hisi_sas_internal_task_abort(struct hisi_hba *hisi_hba,
1130 struct domain_device *device,
1131 int abort_flag, int tag)
1132{
1133 struct sas_task *task;
1134 struct hisi_sas_device *sas_dev = device->lldd_dev;
1135 struct device *dev = &hisi_hba->pdev->dev;
1136 int res;
1137 unsigned long flags;
1138
1139 if (!hisi_hba->hw->prep_abort)
1140 return -EOPNOTSUPP;
1141
1142 task = sas_alloc_slow_task(GFP_KERNEL);
1143 if (!task)
1144 return -ENOMEM;
1145
1146 task->dev = device;
1147 task->task_proto = device->tproto;
1148 task->task_done = hisi_sas_task_done;
1149 task->slow_task->timer.data = (unsigned long)task;
1150 task->slow_task->timer.function = hisi_sas_tmf_timedout;
1151 task->slow_task->timer.expires = jiffies + 20*HZ;
1152 add_timer(&task->slow_task->timer);
1153
1154 /* Lock as we are alloc'ing a slot, which cannot be interrupted */
1155 spin_lock_irqsave(&hisi_hba->lock, flags);
1156 res = hisi_sas_internal_abort_task_exec(hisi_hba, sas_dev->device_id,
1157 task, abort_flag, tag);
1158 spin_unlock_irqrestore(&hisi_hba->lock, flags);
1159 if (res) {
1160 del_timer(&task->slow_task->timer);
1161 dev_err(dev, "internal task abort: executing internal task failed: %d\n",
1162 res);
1163 goto exit;
1164 }
1165 wait_for_completion(&task->slow_task->completion);
1166 res = TMF_RESP_FUNC_FAILED;
1167
1168 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1169 task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1170 res = TMF_RESP_FUNC_COMPLETE;
1171 goto exit;
1172 }
1173
1174 /* TMF timed out, return direct. */
1175 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1176 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1177 dev_err(dev, "internal task abort: timeout.\n");
1178 if (task->lldd_task) {
1179 struct hisi_sas_slot *slot = task->lldd_task;
1180
1181 hisi_sas_slot_task_free(hisi_hba, task, slot);
1182 }
1183 }
1184 }
1185
1186exit:
John Garry297d7302017-01-20 20:45:22 +08001187 dev_dbg(dev, "internal task abort: task to dev %016llx task=%p "
John Garry441c2742016-08-24 19:05:47 +08001188 "resp: 0x%x sts 0x%x\n",
1189 SAS_ADDR(device->sas_addr),
1190 task,
1191 task->task_status.resp, /* 0 is complete, -1 is undelivered */
1192 task->task_status.stat);
1193 sas_free_task(task);
1194
1195 return res;
1196}
1197
John Garry184a4632015-11-18 00:50:52 +08001198static void hisi_sas_port_formed(struct asd_sas_phy *sas_phy)
1199{
1200 hisi_sas_port_notify_formed(sas_phy);
1201}
1202
1203static void hisi_sas_port_deformed(struct asd_sas_phy *sas_phy)
1204{
1205 hisi_sas_port_notify_deformed(sas_phy);
1206}
1207
1208static void hisi_sas_phy_disconnected(struct hisi_sas_phy *phy)
1209{
1210 phy->phy_attached = 0;
1211 phy->phy_type = 0;
1212 phy->port = NULL;
1213}
1214
1215void hisi_sas_phy_down(struct hisi_hba *hisi_hba, int phy_no, int rdy)
1216{
1217 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1218 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1219 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1220
1221 if (rdy) {
1222 /* Phy down but ready */
1223 hisi_sas_bytes_dmaed(hisi_hba, phy_no);
1224 hisi_sas_port_notify_formed(sas_phy);
1225 } else {
1226 struct hisi_sas_port *port = phy->port;
1227
1228 /* Phy down and not ready */
1229 sas_ha->notify_phy_event(sas_phy, PHYE_LOSS_OF_SIGNAL);
1230 sas_phy_disconnected(sas_phy);
1231
1232 if (port) {
1233 if (phy->phy_type & PORT_TYPE_SAS) {
1234 int port_id = port->id;
1235
1236 if (!hisi_hba->hw->get_wideport_bitmap(hisi_hba,
1237 port_id))
1238 port->port_attached = 0;
1239 } else if (phy->phy_type & PORT_TYPE_SATA)
1240 port->port_attached = 0;
1241 }
1242 hisi_sas_phy_disconnected(phy);
1243 }
1244}
1245EXPORT_SYMBOL_GPL(hisi_sas_phy_down);
1246
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001247void hisi_sas_rescan_topology(struct hisi_hba *hisi_hba, u32 old_state,
1248 u32 state)
1249{
1250 struct sas_ha_struct *sas_ha = &hisi_hba->sha;
1251 int phy_no;
1252
1253 for (phy_no = 0; phy_no < hisi_hba->n_phy; phy_no++) {
1254 struct hisi_sas_phy *phy = &hisi_hba->phy[phy_no];
1255 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1256 struct asd_sas_port *sas_port = sas_phy->port;
1257 struct domain_device *dev;
1258
1259 if (sas_phy->enabled) {
1260 /* Report PHY state change to libsas */
1261 if (state & (1 << phy_no))
1262 continue;
1263
1264 if (old_state & (1 << phy_no))
1265 /* PHY down but was up before */
1266 hisi_sas_phy_down(hisi_hba, phy_no, 0);
1267 }
1268 if (!sas_port)
1269 continue;
1270 dev = sas_port->port_dev;
1271
1272 if (DEV_IS_EXPANDER(dev->dev_type))
1273 sas_ha->notify_phy_event(sas_phy, PORTE_BROADCAST_RCVD);
1274 }
1275}
1276EXPORT_SYMBOL_GPL(hisi_sas_rescan_topology);
1277
John Garrye8899fa2015-11-18 00:50:30 +08001278static struct scsi_transport_template *hisi_sas_stt;
1279
John Garry7eb78692015-11-18 00:50:31 +08001280static struct scsi_host_template hisi_sas_sht = {
1281 .module = THIS_MODULE,
1282 .name = DRV_NAME,
1283 .queuecommand = sas_queuecommand,
1284 .target_alloc = sas_target_alloc,
John Garry31eec8a2016-02-25 17:42:14 +08001285 .slave_configure = hisi_sas_slave_configure,
John Garry701f75e2015-11-18 00:50:55 +08001286 .scan_finished = hisi_sas_scan_finished,
1287 .scan_start = hisi_sas_scan_start,
John Garry7eb78692015-11-18 00:50:31 +08001288 .change_queue_depth = sas_change_queue_depth,
1289 .bios_param = sas_bios_param,
1290 .can_queue = 1,
1291 .this_id = -1,
1292 .sg_tablesize = SG_ALL,
1293 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
1294 .use_clustering = ENABLE_CLUSTERING,
1295 .eh_device_reset_handler = sas_eh_device_reset_handler,
1296 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
1297 .target_destroy = sas_target_destroy,
1298 .ioctl = sas_ioctl,
1299};
1300
John Garrye8899fa2015-11-18 00:50:30 +08001301static struct sas_domain_function_template hisi_sas_transport_ops = {
John Garryabda97c2015-11-18 00:50:51 +08001302 .lldd_dev_found = hisi_sas_dev_found,
1303 .lldd_dev_gone = hisi_sas_dev_gone,
John Garry42e7a692015-11-18 00:50:49 +08001304 .lldd_execute_task = hisi_sas_queue_command,
John Garrye4189d52015-11-18 00:50:57 +08001305 .lldd_control_phy = hisi_sas_control_phy,
John Garry0efff302015-11-18 00:50:56 +08001306 .lldd_abort_task = hisi_sas_abort_task,
1307 .lldd_abort_task_set = hisi_sas_abort_task_set,
1308 .lldd_clear_aca = hisi_sas_clear_aca,
1309 .lldd_I_T_nexus_reset = hisi_sas_I_T_nexus_reset,
1310 .lldd_lu_reset = hisi_sas_lu_reset,
1311 .lldd_query_task = hisi_sas_query_task,
John Garry184a4632015-11-18 00:50:52 +08001312 .lldd_port_formed = hisi_sas_port_formed,
1313 .lldd_port_deformed = hisi_sas_port_deformed,
John Garrye8899fa2015-11-18 00:50:30 +08001314};
1315
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001316void hisi_sas_init_mem(struct hisi_hba *hisi_hba)
1317{
1318 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
1319
1320 for (i = 0; i < hisi_hba->queue_count; i++) {
1321 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
1322 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
1323
1324 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1325 memset(hisi_hba->cmd_hdr[i], 0, s);
1326 dq->wr_point = 0;
1327
1328 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1329 memset(hisi_hba->complete_hdr[i], 0, s);
1330 cq->rd_point = 0;
1331 }
1332
1333 s = sizeof(struct hisi_sas_initial_fis) * hisi_hba->n_phy;
1334 memset(hisi_hba->initial_fis, 0, s);
1335
1336 s = max_command_entries * sizeof(struct hisi_sas_iost);
1337 memset(hisi_hba->iost, 0, s);
1338
1339 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
1340 memset(hisi_hba->breakpoint, 0, s);
1341
1342 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
1343 memset(hisi_hba->sata_breakpoint, 0, s);
1344}
1345EXPORT_SYMBOL_GPL(hisi_sas_init_mem);
1346
John Garry6be6de12015-11-18 00:50:34 +08001347static int hisi_sas_alloc(struct hisi_hba *hisi_hba, struct Scsi_Host *shost)
1348{
John Garry6be6de12015-11-18 00:50:34 +08001349 struct platform_device *pdev = hisi_hba->pdev;
1350 struct device *dev = &pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001351 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry6be6de12015-11-18 00:50:34 +08001352
John Garryfa42d802015-11-18 00:50:43 +08001353 spin_lock_init(&hisi_hba->lock);
John Garry976867e2015-11-18 00:50:42 +08001354 for (i = 0; i < hisi_hba->n_phy; i++) {
1355 hisi_sas_phy_init(hisi_hba, i);
1356 hisi_hba->port[i].port_attached = 0;
1357 hisi_hba->port[i].id = -1;
1358 INIT_LIST_HEAD(&hisi_hba->port[i].list);
1359 }
1360
John Garryaf740db2015-11-18 00:50:41 +08001361 for (i = 0; i < HISI_SAS_MAX_DEVICES; i++) {
1362 hisi_hba->devices[i].dev_type = SAS_PHY_UNUSED;
1363 hisi_hba->devices[i].device_id = i;
1364 hisi_hba->devices[i].dev_status = HISI_SAS_DEV_NORMAL;
1365 }
1366
John Garry6be6de12015-11-18 00:50:34 +08001367 for (i = 0; i < hisi_hba->queue_count; i++) {
John Garry9101a072015-11-18 00:50:37 +08001368 struct hisi_sas_cq *cq = &hisi_hba->cq[i];
John Garry4fde02a2016-09-06 23:36:12 +08001369 struct hisi_sas_dq *dq = &hisi_hba->dq[i];
John Garry9101a072015-11-18 00:50:37 +08001370
1371 /* Completion queue structure */
1372 cq->id = i;
1373 cq->hisi_hba = hisi_hba;
1374
John Garry4fde02a2016-09-06 23:36:12 +08001375 /* Delivery queue structure */
1376 dq->id = i;
1377 dq->hisi_hba = hisi_hba;
1378
John Garry6be6de12015-11-18 00:50:34 +08001379 /* Delivery queue */
1380 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1381 hisi_hba->cmd_hdr[i] = dma_alloc_coherent(dev, s,
1382 &hisi_hba->cmd_hdr_dma[i], GFP_KERNEL);
1383 if (!hisi_hba->cmd_hdr[i])
1384 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001385
1386 /* Completion queue */
1387 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1388 hisi_hba->complete_hdr[i] = dma_alloc_coherent(dev, s,
1389 &hisi_hba->complete_hdr_dma[i], GFP_KERNEL);
1390 if (!hisi_hba->complete_hdr[i])
1391 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001392 }
1393
1394 s = HISI_SAS_STATUS_BUF_SZ;
1395 hisi_hba->status_buffer_pool = dma_pool_create("status_buffer",
1396 dev, s, 16, 0);
1397 if (!hisi_hba->status_buffer_pool)
1398 goto err_out;
1399
1400 s = HISI_SAS_COMMAND_TABLE_SZ;
1401 hisi_hba->command_table_pool = dma_pool_create("command_table",
1402 dev, s, 16, 0);
1403 if (!hisi_hba->command_table_pool)
1404 goto err_out;
1405
1406 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1407 hisi_hba->itct = dma_alloc_coherent(dev, s, &hisi_hba->itct_dma,
1408 GFP_KERNEL);
1409 if (!hisi_hba->itct)
1410 goto err_out;
1411
1412 memset(hisi_hba->itct, 0, s);
1413
John Garrya8d547b2016-01-26 02:47:03 +08001414 hisi_hba->slot_info = devm_kcalloc(dev, max_command_entries,
John Garry6be6de12015-11-18 00:50:34 +08001415 sizeof(struct hisi_sas_slot),
1416 GFP_KERNEL);
1417 if (!hisi_hba->slot_info)
1418 goto err_out;
1419
John Garrya8d547b2016-01-26 02:47:03 +08001420 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry6be6de12015-11-18 00:50:34 +08001421 hisi_hba->iost = dma_alloc_coherent(dev, s, &hisi_hba->iost_dma,
1422 GFP_KERNEL);
1423 if (!hisi_hba->iost)
1424 goto err_out;
1425
John Garrya8d547b2016-01-26 02:47:03 +08001426 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry6be6de12015-11-18 00:50:34 +08001427 hisi_hba->breakpoint = dma_alloc_coherent(dev, s,
1428 &hisi_hba->breakpoint_dma, GFP_KERNEL);
1429 if (!hisi_hba->breakpoint)
1430 goto err_out;
1431
John Garrya8d547b2016-01-26 02:47:03 +08001432 hisi_hba->slot_index_count = max_command_entries;
John Garry433f5692016-09-06 23:36:15 +08001433 s = hisi_hba->slot_index_count / BITS_PER_BYTE;
John Garry257efd12015-11-18 00:50:36 +08001434 hisi_hba->slot_index_tags = devm_kzalloc(dev, s, GFP_KERNEL);
1435 if (!hisi_hba->slot_index_tags)
1436 goto err_out;
1437
John Garry6be6de12015-11-18 00:50:34 +08001438 hisi_hba->sge_page_pool = dma_pool_create("status_sge", dev,
1439 sizeof(struct hisi_sas_sge_page), 16, 0);
1440 if (!hisi_hba->sge_page_pool)
1441 goto err_out;
1442
1443 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1444 hisi_hba->initial_fis = dma_alloc_coherent(dev, s,
1445 &hisi_hba->initial_fis_dma, GFP_KERNEL);
1446 if (!hisi_hba->initial_fis)
1447 goto err_out;
John Garry6be6de12015-11-18 00:50:34 +08001448
John Garrya8d547b2016-01-26 02:47:03 +08001449 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry6be6de12015-11-18 00:50:34 +08001450 hisi_hba->sata_breakpoint = dma_alloc_coherent(dev, s,
1451 &hisi_hba->sata_breakpoint_dma, GFP_KERNEL);
1452 if (!hisi_hba->sata_breakpoint)
1453 goto err_out;
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001454 hisi_sas_init_mem(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001455
John Garry257efd12015-11-18 00:50:36 +08001456 hisi_sas_slot_index_init(hisi_hba);
1457
John Garry7e9080e2015-11-18 00:50:40 +08001458 hisi_hba->wq = create_singlethread_workqueue(dev_name(dev));
1459 if (!hisi_hba->wq) {
1460 dev_err(dev, "sas_alloc: failed to create workqueue\n");
1461 goto err_out;
1462 }
1463
John Garry6be6de12015-11-18 00:50:34 +08001464 return 0;
1465err_out:
1466 return -ENOMEM;
1467}
1468
John Garry89d53322015-11-18 00:50:35 +08001469static void hisi_sas_free(struct hisi_hba *hisi_hba)
1470{
1471 struct device *dev = &hisi_hba->pdev->dev;
John Garrya8d547b2016-01-26 02:47:03 +08001472 int i, s, max_command_entries = hisi_hba->hw->max_command_entries;
John Garry89d53322015-11-18 00:50:35 +08001473
1474 for (i = 0; i < hisi_hba->queue_count; i++) {
1475 s = sizeof(struct hisi_sas_cmd_hdr) * HISI_SAS_QUEUE_SLOTS;
1476 if (hisi_hba->cmd_hdr[i])
1477 dma_free_coherent(dev, s,
1478 hisi_hba->cmd_hdr[i],
1479 hisi_hba->cmd_hdr_dma[i]);
1480
1481 s = hisi_hba->hw->complete_hdr_size * HISI_SAS_QUEUE_SLOTS;
1482 if (hisi_hba->complete_hdr[i])
1483 dma_free_coherent(dev, s,
1484 hisi_hba->complete_hdr[i],
1485 hisi_hba->complete_hdr_dma[i]);
1486 }
1487
1488 dma_pool_destroy(hisi_hba->status_buffer_pool);
1489 dma_pool_destroy(hisi_hba->command_table_pool);
1490 dma_pool_destroy(hisi_hba->sge_page_pool);
1491
1492 s = HISI_SAS_MAX_ITCT_ENTRIES * sizeof(struct hisi_sas_itct);
1493 if (hisi_hba->itct)
1494 dma_free_coherent(dev, s,
1495 hisi_hba->itct, hisi_hba->itct_dma);
1496
John Garrya8d547b2016-01-26 02:47:03 +08001497 s = max_command_entries * sizeof(struct hisi_sas_iost);
John Garry89d53322015-11-18 00:50:35 +08001498 if (hisi_hba->iost)
1499 dma_free_coherent(dev, s,
1500 hisi_hba->iost, hisi_hba->iost_dma);
1501
John Garrya8d547b2016-01-26 02:47:03 +08001502 s = max_command_entries * sizeof(struct hisi_sas_breakpoint);
John Garry89d53322015-11-18 00:50:35 +08001503 if (hisi_hba->breakpoint)
1504 dma_free_coherent(dev, s,
1505 hisi_hba->breakpoint,
1506 hisi_hba->breakpoint_dma);
1507
1508
1509 s = sizeof(struct hisi_sas_initial_fis) * HISI_SAS_MAX_PHYS;
1510 if (hisi_hba->initial_fis)
1511 dma_free_coherent(dev, s,
1512 hisi_hba->initial_fis,
1513 hisi_hba->initial_fis_dma);
1514
John Garrya8d547b2016-01-26 02:47:03 +08001515 s = max_command_entries * sizeof(struct hisi_sas_breakpoint) * 2;
John Garry89d53322015-11-18 00:50:35 +08001516 if (hisi_hba->sata_breakpoint)
1517 dma_free_coherent(dev, s,
1518 hisi_hba->sata_breakpoint,
1519 hisi_hba->sata_breakpoint_dma);
1520
John Garry7e9080e2015-11-18 00:50:40 +08001521 if (hisi_hba->wq)
1522 destroy_workqueue(hisi_hba->wq);
John Garry89d53322015-11-18 00:50:35 +08001523}
John Garry6be6de12015-11-18 00:50:34 +08001524
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001525static void hisi_sas_rst_work_handler(struct work_struct *work)
1526{
1527 struct hisi_hba *hisi_hba =
1528 container_of(work, struct hisi_hba, rst_work);
1529
1530 hisi_sas_controller_reset(hisi_hba);
1531}
1532
John Garry7eb78692015-11-18 00:50:31 +08001533static struct Scsi_Host *hisi_sas_shost_alloc(struct platform_device *pdev,
1534 const struct hisi_sas_hw *hw)
1535{
John Garrye26b2f42015-11-18 00:50:32 +08001536 struct resource *res;
John Garry7eb78692015-11-18 00:50:31 +08001537 struct Scsi_Host *shost;
1538 struct hisi_hba *hisi_hba;
1539 struct device *dev = &pdev->dev;
John Garrye26b2f42015-11-18 00:50:32 +08001540 struct device_node *np = pdev->dev.of_node;
John Garry3bc45af2016-10-04 19:11:11 +08001541 struct clk *refclk;
John Garry7eb78692015-11-18 00:50:31 +08001542
1543 shost = scsi_host_alloc(&hisi_sas_sht, sizeof(*hisi_hba));
Xiaofei Tand37a0082016-11-29 23:45:57 +08001544 if (!shost) {
1545 dev_err(dev, "scsi host alloc failed\n");
1546 return NULL;
1547 }
John Garry7eb78692015-11-18 00:50:31 +08001548 hisi_hba = shost_priv(shost);
1549
Xiang Chen06ec0fb2017-03-23 01:25:18 +08001550 INIT_WORK(&hisi_hba->rst_work, hisi_sas_rst_work_handler);
John Garry7eb78692015-11-18 00:50:31 +08001551 hisi_hba->hw = hw;
1552 hisi_hba->pdev = pdev;
1553 hisi_hba->shost = shost;
1554 SHOST_TO_SAS_HA(shost) = &hisi_hba->sha;
1555
John Garryfa42d802015-11-18 00:50:43 +08001556 init_timer(&hisi_hba->timer);
1557
John Garry4d558c72016-02-04 02:26:08 +08001558 if (device_property_read_u8_array(dev, "sas-addr", hisi_hba->sas_addr,
1559 SAS_ADDR_SIZE))
John Garrye26b2f42015-11-18 00:50:32 +08001560 goto err_out;
1561
John Garry4d558c72016-02-04 02:26:08 +08001562 if (np) {
1563 hisi_hba->ctrl = syscon_regmap_lookup_by_phandle(np,
1564 "hisilicon,sas-syscon");
1565 if (IS_ERR(hisi_hba->ctrl))
1566 goto err_out;
1567
1568 if (device_property_read_u32(dev, "ctrl-reset-reg",
1569 &hisi_hba->ctrl_reset_reg))
1570 goto err_out;
1571
1572 if (device_property_read_u32(dev, "ctrl-reset-sts-reg",
1573 &hisi_hba->ctrl_reset_sts_reg))
1574 goto err_out;
1575
1576 if (device_property_read_u32(dev, "ctrl-clock-ena-reg",
1577 &hisi_hba->ctrl_clock_ena_reg))
1578 goto err_out;
1579 }
1580
John Garry3bc45af2016-10-04 19:11:11 +08001581 refclk = devm_clk_get(&pdev->dev, NULL);
1582 if (IS_ERR(refclk))
John Garry87e287c2017-01-20 20:45:20 +08001583 dev_dbg(dev, "no ref clk property\n");
John Garry3bc45af2016-10-04 19:11:11 +08001584 else
1585 hisi_hba->refclk_frequency_mhz = clk_get_rate(refclk) / 1000000;
1586
John Garry4d558c72016-02-04 02:26:08 +08001587 if (device_property_read_u32(dev, "phy-count", &hisi_hba->n_phy))
John Garrye26b2f42015-11-18 00:50:32 +08001588 goto err_out;
1589
John Garry4d558c72016-02-04 02:26:08 +08001590 if (device_property_read_u32(dev, "queue-count",
1591 &hisi_hba->queue_count))
John Garrye26b2f42015-11-18 00:50:32 +08001592 goto err_out;
1593
John Garrya6f2c7f2016-09-06 23:36:19 +08001594 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)) &&
1595 dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
1596 dev_err(dev, "No usable DMA addressing method\n");
1597 goto err_out;
1598 }
1599
John Garrye26b2f42015-11-18 00:50:32 +08001600 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1601 hisi_hba->regs = devm_ioremap_resource(dev, res);
1602 if (IS_ERR(hisi_hba->regs))
1603 goto err_out;
1604
John Garry89d53322015-11-18 00:50:35 +08001605 if (hisi_sas_alloc(hisi_hba, shost)) {
1606 hisi_sas_free(hisi_hba);
John Garry6be6de12015-11-18 00:50:34 +08001607 goto err_out;
John Garry89d53322015-11-18 00:50:35 +08001608 }
John Garry6be6de12015-11-18 00:50:34 +08001609
John Garry7eb78692015-11-18 00:50:31 +08001610 return shost;
1611err_out:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001612 kfree(shost);
John Garry7eb78692015-11-18 00:50:31 +08001613 dev_err(dev, "shost alloc failed\n");
1614 return NULL;
1615}
1616
John Garry5d742422015-11-18 00:50:38 +08001617static void hisi_sas_init_add(struct hisi_hba *hisi_hba)
1618{
1619 int i;
1620
1621 for (i = 0; i < hisi_hba->n_phy; i++)
1622 memcpy(&hisi_hba->phy[i].dev_sas_addr,
1623 hisi_hba->sas_addr,
1624 SAS_ADDR_SIZE);
1625}
1626
John Garry7eb78692015-11-18 00:50:31 +08001627int hisi_sas_probe(struct platform_device *pdev,
1628 const struct hisi_sas_hw *hw)
1629{
1630 struct Scsi_Host *shost;
1631 struct hisi_hba *hisi_hba;
1632 struct device *dev = &pdev->dev;
1633 struct asd_sas_phy **arr_phy;
1634 struct asd_sas_port **arr_port;
1635 struct sas_ha_struct *sha;
1636 int rc, phy_nr, port_nr, i;
1637
1638 shost = hisi_sas_shost_alloc(pdev, hw);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001639 if (!shost)
1640 return -ENOMEM;
John Garry7eb78692015-11-18 00:50:31 +08001641
1642 sha = SHOST_TO_SAS_HA(shost);
1643 hisi_hba = shost_priv(shost);
1644 platform_set_drvdata(pdev, sha);
John Garry50cb9162015-11-18 00:50:39 +08001645
John Garry7eb78692015-11-18 00:50:31 +08001646 phy_nr = port_nr = hisi_hba->n_phy;
1647
1648 arr_phy = devm_kcalloc(dev, phy_nr, sizeof(void *), GFP_KERNEL);
1649 arr_port = devm_kcalloc(dev, port_nr, sizeof(void *), GFP_KERNEL);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001650 if (!arr_phy || !arr_port) {
1651 rc = -ENOMEM;
1652 goto err_out_ha;
1653 }
John Garry7eb78692015-11-18 00:50:31 +08001654
1655 sha->sas_phy = arr_phy;
1656 sha->sas_port = arr_port;
John Garry7eb78692015-11-18 00:50:31 +08001657 sha->lldd_ha = hisi_hba;
1658
1659 shost->transportt = hisi_sas_stt;
1660 shost->max_id = HISI_SAS_MAX_DEVICES;
1661 shost->max_lun = ~0;
1662 shost->max_channel = 1;
1663 shost->max_cmd_len = 16;
1664 shost->sg_tablesize = min_t(u16, SG_ALL, HISI_SAS_SGE_PAGE_CNT);
John Garrya8d547b2016-01-26 02:47:03 +08001665 shost->can_queue = hisi_hba->hw->max_command_entries;
1666 shost->cmd_per_lun = hisi_hba->hw->max_command_entries;
John Garry7eb78692015-11-18 00:50:31 +08001667
1668 sha->sas_ha_name = DRV_NAME;
1669 sha->dev = &hisi_hba->pdev->dev;
1670 sha->lldd_module = THIS_MODULE;
1671 sha->sas_addr = &hisi_hba->sas_addr[0];
1672 sha->num_phys = hisi_hba->n_phy;
1673 sha->core.shost = hisi_hba->shost;
1674
1675 for (i = 0; i < hisi_hba->n_phy; i++) {
1676 sha->sas_phy[i] = &hisi_hba->phy[i].sas_phy;
1677 sha->sas_port[i] = &hisi_hba->port[i].sas_port;
1678 }
1679
John Garry5d742422015-11-18 00:50:38 +08001680 hisi_sas_init_add(hisi_hba);
1681
John Garry7eb78692015-11-18 00:50:31 +08001682 rc = scsi_add_host(shost, &pdev->dev);
1683 if (rc)
1684 goto err_out_ha;
1685
1686 rc = sas_register_ha(sha);
1687 if (rc)
1688 goto err_out_register_ha;
1689
Xiang Chen0757f042017-01-20 20:45:23 +08001690 rc = hisi_hba->hw->hw_init(hisi_hba);
1691 if (rc)
1692 goto err_out_register_ha;
1693
John Garry7eb78692015-11-18 00:50:31 +08001694 scsi_scan_host(shost);
1695
1696 return 0;
1697
1698err_out_register_ha:
1699 scsi_remove_host(shost);
1700err_out_ha:
Xiaofei Tand37a0082016-11-29 23:45:57 +08001701 hisi_sas_free(hisi_hba);
John Garry7eb78692015-11-18 00:50:31 +08001702 kfree(shost);
1703 return rc;
1704}
1705EXPORT_SYMBOL_GPL(hisi_sas_probe);
1706
John Garry89d53322015-11-18 00:50:35 +08001707int hisi_sas_remove(struct platform_device *pdev)
1708{
1709 struct sas_ha_struct *sha = platform_get_drvdata(pdev);
1710 struct hisi_hba *hisi_hba = sha->lldd_ha;
Xiaofei Tand37a0082016-11-29 23:45:57 +08001711 struct Scsi_Host *shost = sha->core.shost;
John Garry89d53322015-11-18 00:50:35 +08001712
1713 scsi_remove_host(sha->core.shost);
1714 sas_unregister_ha(sha);
1715 sas_remove_host(sha->core.shost);
1716
1717 hisi_sas_free(hisi_hba);
Xiaofei Tand37a0082016-11-29 23:45:57 +08001718 kfree(shost);
John Garry89d53322015-11-18 00:50:35 +08001719 return 0;
1720}
1721EXPORT_SYMBOL_GPL(hisi_sas_remove);
1722
John Garrye8899fa2015-11-18 00:50:30 +08001723static __init int hisi_sas_init(void)
1724{
1725 pr_info("hisi_sas: driver version %s\n", DRV_VERSION);
1726
1727 hisi_sas_stt = sas_domain_attach_transport(&hisi_sas_transport_ops);
1728 if (!hisi_sas_stt)
1729 return -ENOMEM;
1730
1731 return 0;
1732}
1733
1734static __exit void hisi_sas_exit(void)
1735{
1736 sas_release_transport(hisi_sas_stt);
1737}
1738
1739module_init(hisi_sas_init);
1740module_exit(hisi_sas_exit);
1741
1742MODULE_VERSION(DRV_VERSION);
1743MODULE_LICENSE("GPL");
1744MODULE_AUTHOR("John Garry <john.garry@huawei.com>");
1745MODULE_DESCRIPTION("HISILICON SAS controller driver");
1746MODULE_ALIAS("platform:" DRV_NAME);