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