cxlflash: Superpipe support
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / cxlflash / lunmgt.c
1 /*
2  * CXL Flash Device Driver
3  *
4  * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5  *             Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
6  *
7  * Copyright (C) 2015 IBM Corporation
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version
12  * 2 of the License, or (at your option) any later version.
13  */
14
15 #include <misc/cxl.h>
16 #include <asm/unaligned.h>
17
18 #include <scsi/scsi_host.h>
19 #include <uapi/scsi/cxlflash_ioctl.h>
20
21 #include "sislite.h"
22 #include "common.h"
23 #include "superpipe.h"
24
25 /**
26  * create_local() - allocate and initialize a local LUN information structure
27  * @sdev:       SCSI device associated with LUN.
28  * @wwid:       World Wide Node Name for LUN.
29  *
30  * Return: Allocated local llun_info structure on success, NULL on failure
31  */
32 static struct llun_info *create_local(struct scsi_device *sdev, u8 *wwid)
33 {
34         struct llun_info *lli = NULL;
35
36         lli = kzalloc(sizeof(*lli), GFP_KERNEL);
37         if (unlikely(!lli)) {
38                 pr_err("%s: could not allocate lli\n", __func__);
39                 goto out;
40         }
41
42         lli->sdev = sdev;
43         lli->newly_created = true;
44         lli->host_no = sdev->host->host_no;
45
46         memcpy(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
47 out:
48         return lli;
49 }
50
51 /**
52  * create_global() - allocate and initialize a global LUN information structure
53  * @sdev:       SCSI device associated with LUN.
54  * @wwid:       World Wide Node Name for LUN.
55  *
56  * Return: Allocated global glun_info structure on success, NULL on failure
57  */
58 static struct glun_info *create_global(struct scsi_device *sdev, u8 *wwid)
59 {
60         struct glun_info *gli = NULL;
61
62         gli = kzalloc(sizeof(*gli), GFP_KERNEL);
63         if (unlikely(!gli)) {
64                 pr_err("%s: could not allocate gli\n", __func__);
65                 goto out;
66         }
67
68         mutex_init(&gli->mutex);
69         memcpy(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
70 out:
71         return gli;
72 }
73
74 /**
75  * refresh_local() - find and update local LUN information structure by WWID
76  * @cfg:        Internal structure associated with the host.
77  * @wwid:       WWID associated with LUN.
78  *
79  * When the LUN is found, mark it by updating it's newly_created field.
80  *
81  * Return: Found local lun_info structure on success, NULL on failure
82  * If a LUN with the WWID is found in the list, refresh it's state.
83  */
84 static struct llun_info *refresh_local(struct cxlflash_cfg *cfg, u8 *wwid)
85 {
86         struct llun_info *lli, *temp;
87
88         list_for_each_entry_safe(lli, temp, &cfg->lluns, list)
89                 if (!memcmp(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN)) {
90                         lli->newly_created = false;
91                         return lli;
92                 }
93
94         return NULL;
95 }
96
97 /**
98  * lookup_global() - find a global LUN information structure by WWID
99  * @wwid:       WWID associated with LUN.
100  *
101  * Return: Found global lun_info structure on success, NULL on failure
102  */
103 static struct glun_info *lookup_global(u8 *wwid)
104 {
105         struct glun_info *gli, *temp;
106
107         list_for_each_entry_safe(gli, temp, &global.gluns, list)
108                 if (!memcmp(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN))
109                         return gli;
110
111         return NULL;
112 }
113
114 /**
115  * find_and_create_lun() - find or create a local LUN information structure
116  * @sdev:       SCSI device associated with LUN.
117  * @wwid:       WWID associated with LUN.
118  *
119  * The LUN is kept both in a local list (per adapter) and in a global list
120  * (across all adapters). Certain attributes of the LUN are local to the
121  * adapter (such as index, port selection mask etc.).
122  * The block allocation map is shared across all adapters (i.e. associated
123  * wih the global list). Since different attributes are associated with
124  * the per adapter and global entries, allocate two separate structures for each
125  * LUN (one local, one global).
126  *
127  * Keep a pointer back from the local to the global entry.
128  *
129  * Return: Found/Allocated local lun_info structure on success, NULL on failure
130  */
131 static struct llun_info *find_and_create_lun(struct scsi_device *sdev, u8 *wwid)
132 {
133         struct llun_info *lli = NULL;
134         struct glun_info *gli = NULL;
135         struct Scsi_Host *shost = sdev->host;
136         struct cxlflash_cfg *cfg = shost_priv(shost);
137
138         mutex_lock(&global.mutex);
139         if (unlikely(!wwid))
140                 goto out;
141
142         lli = refresh_local(cfg, wwid);
143         if (lli)
144                 goto out;
145
146         lli = create_local(sdev, wwid);
147         if (unlikely(!lli))
148                 goto out;
149
150         gli = lookup_global(wwid);
151         if (gli) {
152                 lli->parent = gli;
153                 list_add(&lli->list, &cfg->lluns);
154                 goto out;
155         }
156
157         gli = create_global(sdev, wwid);
158         if (unlikely(!gli)) {
159                 kfree(lli);
160                 lli = NULL;
161                 goto out;
162         }
163
164         lli->parent = gli;
165         list_add(&lli->list, &cfg->lluns);
166
167         list_add(&gli->list, &global.gluns);
168
169 out:
170         mutex_unlock(&global.mutex);
171         pr_debug("%s: returning %p\n", __func__, lli);
172         return lli;
173 }
174
175 /**
176  * cxlflash_term_local_luns() - Delete all entries from local LUN list, free.
177  * @cfg:        Internal structure associated with the host.
178  */
179 void cxlflash_term_local_luns(struct cxlflash_cfg *cfg)
180 {
181         struct llun_info *lli, *temp;
182
183         mutex_lock(&global.mutex);
184         list_for_each_entry_safe(lli, temp, &cfg->lluns, list) {
185                 list_del(&lli->list);
186                 kfree(lli);
187         }
188         mutex_unlock(&global.mutex);
189 }
190
191 /**
192  * cxlflash_list_init() - initializes the global LUN list
193  */
194 void cxlflash_list_init(void)
195 {
196         INIT_LIST_HEAD(&global.gluns);
197         mutex_init(&global.mutex);
198         global.err_page = NULL;
199 }
200
201 /**
202  * cxlflash_term_global_luns() - frees resources associated with global LUN list
203  */
204 void cxlflash_term_global_luns(void)
205 {
206         struct glun_info *gli, *temp;
207
208         mutex_lock(&global.mutex);
209         list_for_each_entry_safe(gli, temp, &global.gluns, list) {
210                 list_del(&gli->list);
211                 kfree(gli);
212         }
213         mutex_unlock(&global.mutex);
214 }
215
216 /**
217  * cxlflash_manage_lun() - handles LUN management activities
218  * @sdev:       SCSI device associated with LUN.
219  * @manage:     Manage ioctl data structure.
220  *
221  * This routine is used to notify the driver about a LUN's WWID and associate
222  * SCSI devices (sdev) with a global LUN instance. Additionally it serves to
223  * change a LUN's operating mode: legacy or superpipe.
224  *
225  * Return: 0 on success, -errno on failure
226  */
227 int cxlflash_manage_lun(struct scsi_device *sdev,
228                         struct dk_cxlflash_manage_lun *manage)
229 {
230         int rc = 0;
231         struct llun_info *lli = NULL;
232         u64 flags = manage->hdr.flags;
233         u32 chan = sdev->channel;
234
235         lli = find_and_create_lun(sdev, manage->wwid);
236         pr_debug("%s: ENTER: WWID = %016llX%016llX, flags = %016llX li = %p\n",
237                  __func__, get_unaligned_le64(&manage->wwid[0]),
238                  get_unaligned_le64(&manage->wwid[8]),
239                  manage->hdr.flags, lli);
240         if (unlikely(!lli)) {
241                 rc = -ENOMEM;
242                 goto out;
243         }
244
245         if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) {
246                 if (lli->newly_created)
247                         lli->port_sel = CHAN2PORT(chan);
248                 else
249                         lli->port_sel = BOTH_PORTS;
250                 /* Store off lun in unpacked, AFU-friendly format */
251                 lli->lun_id[chan] = lun_to_lunid(sdev->lun);
252                 sdev->hostdata = lli;
253         } else if (flags & DK_CXLFLASH_MANAGE_LUN_DISABLE_SUPERPIPE) {
254                 if (lli->parent->mode != MODE_NONE)
255                         rc = -EBUSY;
256                 else
257                         sdev->hostdata = NULL;
258         }
259
260 out:
261         pr_debug("%s: returning rc=%d\n", __func__, rc);
262         return rc;
263 }