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