1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/delay.h>
16#include <linux/file.h>
17#include <linux/syscalls.h>
18#include <misc/cxl.h>
19#include <asm/unaligned.h>
20
21#include <scsi/scsi.h>
22#include <scsi/scsi_host.h>
23#include <scsi/scsi_cmnd.h>
24#include <scsi/scsi_eh.h>
25#include <uapi/scsi/cxlflash_ioctl.h>
26
27#include "sislite.h"
28#include "common.h"
29#include "vlun.h"
30#include "superpipe.h"
31
32struct cxlflash_global global;
33
34
35
36
37
38
39static void marshal_rele_to_resize(struct dk_cxlflash_release *release,
40 struct dk_cxlflash_resize *resize)
41{
42 resize->hdr = release->hdr;
43 resize->context_id = release->context_id;
44 resize->rsrc_handle = release->rsrc_handle;
45}
46
47
48
49
50
51
52static void marshal_det_to_rele(struct dk_cxlflash_detach *detach,
53 struct dk_cxlflash_release *release)
54{
55 release->hdr = detach->hdr;
56 release->context_id = detach->context_id;
57}
58
59
60
61
62void cxlflash_free_errpage(void)
63{
64
65 mutex_lock(&global.mutex);
66 if (global.err_page) {
67 __free_page(global.err_page);
68 global.err_page = NULL;
69 }
70 mutex_unlock(&global.mutex);
71}
72
73
74
75
76
77
78
79
80
81
82void cxlflash_stop_term_user_contexts(struct cxlflash_cfg *cfg)
83{
84 struct device *dev = &cfg->dev->dev;
85 int i, found;
86
87 cxlflash_mark_contexts_error(cfg);
88
89 while (true) {
90 found = false;
91
92 for (i = 0; i < MAX_CONTEXT; i++)
93 if (cfg->ctx_tbl[i]) {
94 found = true;
95 break;
96 }
97
98 if (!found && list_empty(&cfg->ctx_err_recovery))
99 return;
100
101 dev_dbg(dev, "%s: Wait for user contexts to quiesce...\n",
102 __func__);
103 wake_up_all(&cfg->reset_waitq);
104 ssleep(1);
105 }
106}
107
108
109
110
111
112
113
114
115
116static struct ctx_info *find_error_context(struct cxlflash_cfg *cfg, u64 rctxid,
117 struct file *file)
118{
119 struct ctx_info *ctxi;
120
121 list_for_each_entry(ctxi, &cfg->ctx_err_recovery, list)
122 if ((ctxi->ctxid == rctxid) || (ctxi->file == file))
123 return ctxi;
124
125 return NULL;
126}
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143struct ctx_info *get_context(struct cxlflash_cfg *cfg, u64 rctxid,
144 void *arg, enum ctx_ctrl ctx_ctrl)
145{
146 struct device *dev = &cfg->dev->dev;
147 struct ctx_info *ctxi = NULL;
148 struct lun_access *lun_access = NULL;
149 struct file *file = NULL;
150 struct llun_info *lli = arg;
151 u64 ctxid = DECODE_CTXID(rctxid);
152 int rc;
153 pid_t pid = current->tgid, ctxpid = 0;
154
155 if (ctx_ctrl & CTX_CTRL_FILE) {
156 lli = NULL;
157 file = (struct file *)arg;
158 }
159
160 if (ctx_ctrl & CTX_CTRL_CLONE)
161 pid = current->parent->tgid;
162
163 if (likely(ctxid < MAX_CONTEXT)) {
164 while (true) {
165 mutex_lock(&cfg->ctx_tbl_list_mutex);
166 ctxi = cfg->ctx_tbl[ctxid];
167 if (ctxi)
168 if ((file && (ctxi->file != file)) ||
169 (!file && (ctxi->ctxid != rctxid)))
170 ctxi = NULL;
171
172 if ((ctx_ctrl & CTX_CTRL_ERR) ||
173 (!ctxi && (ctx_ctrl & CTX_CTRL_ERR_FALLBACK)))
174 ctxi = find_error_context(cfg, rctxid, file);
175 if (!ctxi) {
176 mutex_unlock(&cfg->ctx_tbl_list_mutex);
177 goto out;
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192 rc = mutex_trylock(&ctxi->mutex);
193 mutex_unlock(&cfg->ctx_tbl_list_mutex);
194 if (rc)
195 break;
196 }
197
198 if (ctxi->unavail)
199 goto denied;
200
201 ctxpid = ctxi->pid;
202 if (likely(!(ctx_ctrl & CTX_CTRL_NOPID)))
203 if (pid != ctxpid)
204 goto denied;
205
206 if (lli) {
207 list_for_each_entry(lun_access, &ctxi->luns, list)
208 if (lun_access->lli == lli)
209 goto out;
210 goto denied;
211 }
212 }
213
214out:
215 dev_dbg(dev, "%s: rctxid=%016llx ctxinfo=%p ctxpid=%u pid=%u "
216 "ctx_ctrl=%u\n", __func__, rctxid, ctxi, ctxpid, pid,
217 ctx_ctrl);
218
219 return ctxi;
220
221denied:
222 mutex_unlock(&ctxi->mutex);
223 ctxi = NULL;
224 goto out;
225}
226
227
228
229
230
231
232
233void put_context(struct ctx_info *ctxi)
234{
235 mutex_unlock(&ctxi->mutex);
236}
237
238
239
240
241
242
243
244
245
246
247
248
249static int afu_attach(struct cxlflash_cfg *cfg, struct ctx_info *ctxi)
250{
251 struct device *dev = &cfg->dev->dev;
252 struct afu *afu = cfg->afu;
253 struct sisl_ctrl_map __iomem *ctrl_map = ctxi->ctrl_map;
254 int rc = 0;
255 u64 val;
256
257
258 readq_be(&ctrl_map->mbox_r);
259 val = (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD);
260 writeq_be(val, &ctrl_map->ctx_cap);
261 val = readq_be(&ctrl_map->ctx_cap);
262 if (val != (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD)) {
263 dev_err(dev, "%s: ctx may be closed val=%016llx\n",
264 __func__, val);
265 rc = -EAGAIN;
266 goto out;
267 }
268
269
270 writeq_be((u64)ctxi->rht_start, &ctrl_map->rht_start);
271 val = SISL_RHT_CNT_ID((u64)MAX_RHT_PER_CONTEXT, (u64)(afu->ctx_hndl));
272 writeq_be(val, &ctrl_map->rht_cnt_id);
273out:
274 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
275 return rc;
276}
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303static int read_cap16(struct scsi_device *sdev, struct llun_info *lli)
304{
305 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
306 struct device *dev = &cfg->dev->dev;
307 struct glun_info *gli = lli->parent;
308 struct scsi_sense_hdr sshdr;
309 u8 *cmd_buf = NULL;
310 u8 *scsi_cmd = NULL;
311 u8 *sense_buf = NULL;
312 int rc = 0;
313 int result = 0;
314 int retry_cnt = 0;
315 u32 to = CMD_TIMEOUT * HZ;
316
317retry:
318 cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL);
319 scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL);
320 sense_buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
321 if (unlikely(!cmd_buf || !scsi_cmd || !sense_buf)) {
322 rc = -ENOMEM;
323 goto out;
324 }
325
326 scsi_cmd[0] = SERVICE_ACTION_IN_16;
327 scsi_cmd[1] = SAI_READ_CAPACITY_16;
328 put_unaligned_be32(CMD_BUFSIZE, &scsi_cmd[10]);
329
330 dev_dbg(dev, "%s: %ssending cmd(%02x)\n", __func__,
331 retry_cnt ? "re" : "", scsi_cmd[0]);
332
333
334 up_read(&cfg->ioctl_rwsem);
335 result = scsi_execute(sdev, scsi_cmd, DMA_FROM_DEVICE, cmd_buf,
336 CMD_BUFSIZE, sense_buf, &sshdr, to, CMD_RETRIES,
337 0, 0, NULL);
338 down_read(&cfg->ioctl_rwsem);
339 rc = check_state(cfg);
340 if (rc) {
341 dev_err(dev, "%s: Failed state result=%08x\n",
342 __func__, result);
343 rc = -ENODEV;
344 goto out;
345 }
346
347 if (driver_byte(result) == DRIVER_SENSE) {
348 result &= ~(0xFF<<24);
349 if (result & SAM_STAT_CHECK_CONDITION) {
350 switch (sshdr.sense_key) {
351 case NO_SENSE:
352 case RECOVERED_ERROR:
353
354 case NOT_READY:
355 result &= ~SAM_STAT_CHECK_CONDITION;
356 break;
357 case UNIT_ATTENTION:
358 switch (sshdr.asc) {
359 case 0x29:
360
361 case 0x2A:
362 case 0x3F:
363
364 if (retry_cnt++ < 1) {
365 kfree(cmd_buf);
366 kfree(scsi_cmd);
367 kfree(sense_buf);
368 goto retry;
369 }
370 }
371 break;
372 default:
373 break;
374 }
375 }
376 }
377
378 if (result) {
379 dev_err(dev, "%s: command failed, result=%08x\n",
380 __func__, result);
381 rc = -EIO;
382 goto out;
383 }
384
385
386
387
388
389
390 mutex_lock(&gli->mutex);
391 gli->max_lba = be64_to_cpu(*((__be64 *)&cmd_buf[0]));
392 gli->blk_len = be32_to_cpu(*((__be32 *)&cmd_buf[8]));
393 mutex_unlock(&gli->mutex);
394
395out:
396 kfree(cmd_buf);
397 kfree(scsi_cmd);
398 kfree(sense_buf);
399
400 dev_dbg(dev, "%s: maxlba=%lld blklen=%d rc=%d\n",
401 __func__, gli->max_lba, gli->blk_len, rc);
402 return rc;
403}
404
405
406
407
408
409
410
411
412
413struct sisl_rht_entry *get_rhte(struct ctx_info *ctxi, res_hndl_t rhndl,
414 struct llun_info *lli)
415{
416 struct cxlflash_cfg *cfg = ctxi->cfg;
417 struct device *dev = &cfg->dev->dev;
418 struct sisl_rht_entry *rhte = NULL;
419
420 if (unlikely(!ctxi->rht_start)) {
421 dev_dbg(dev, "%s: Context does not have allocated RHT\n",
422 __func__);
423 goto out;
424 }
425
426 if (unlikely(rhndl >= MAX_RHT_PER_CONTEXT)) {
427 dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
428 __func__, rhndl);
429 goto out;
430 }
431
432 if (unlikely(ctxi->rht_lun[rhndl] != lli)) {
433 dev_dbg(dev, "%s: Bad resource handle LUN rhndl=%d\n",
434 __func__, rhndl);
435 goto out;
436 }
437
438 rhte = &ctxi->rht_start[rhndl];
439 if (unlikely(rhte->nmask == 0)) {
440 dev_dbg(dev, "%s: Unopened resource handle rhndl=%d\n",
441 __func__, rhndl);
442 rhte = NULL;
443 goto out;
444 }
445
446out:
447 return rhte;
448}
449
450
451
452
453
454
455
456
457struct sisl_rht_entry *rhte_checkout(struct ctx_info *ctxi,
458 struct llun_info *lli)
459{
460 struct cxlflash_cfg *cfg = ctxi->cfg;
461 struct device *dev = &cfg->dev->dev;
462 struct sisl_rht_entry *rhte = NULL;
463 int i;
464
465
466 for (i = 0; i < MAX_RHT_PER_CONTEXT; i++)
467 if (ctxi->rht_start[i].nmask == 0) {
468 rhte = &ctxi->rht_start[i];
469 ctxi->rht_out++;
470 break;
471 }
472
473 if (likely(rhte))
474 ctxi->rht_lun[i] = lli;
475
476 dev_dbg(dev, "%s: returning rhte=%p index=%d\n", __func__, rhte, i);
477 return rhte;
478}
479
480
481
482
483
484
485void rhte_checkin(struct ctx_info *ctxi,
486 struct sisl_rht_entry *rhte)
487{
488 u32 rsrc_handle = rhte - ctxi->rht_start;
489
490 rhte->nmask = 0;
491 rhte->fp = 0;
492 ctxi->rht_out--;
493 ctxi->rht_lun[rsrc_handle] = NULL;
494 ctxi->rht_needs_ws[rsrc_handle] = false;
495}
496
497
498
499
500
501
502
503
504static void rht_format1(struct sisl_rht_entry *rhte, u64 lun_id, u32 perm,
505 u32 port_sel)
506{
507
508
509
510
511
512 struct sisl_rht_entry_f1 dummy = { 0 };
513 struct sisl_rht_entry_f1 *rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
514
515 memset(rhte_f1, 0, sizeof(*rhte_f1));
516 rhte_f1->fp = SISL_RHT_FP(1U, 0);
517 dma_wmb();
518
519 rhte_f1->lun_id = lun_id;
520 dma_wmb();
521
522
523
524
525
526
527 dummy.valid = 0x80;
528 dummy.fp = SISL_RHT_FP(1U, perm);
529 dummy.port_sel = port_sel;
530 rhte_f1->dw = dummy.dw;
531
532 dma_wmb();
533}
534
535
536
537
538
539
540
541
542
543int cxlflash_lun_attach(struct glun_info *gli, enum lun_mode mode, bool locked)
544{
545 int rc = 0;
546
547 if (!locked)
548 mutex_lock(&gli->mutex);
549
550 if (gli->mode == MODE_NONE)
551 gli->mode = mode;
552 else if (gli->mode != mode) {
553 pr_debug("%s: gli_mode=%d requested_mode=%d\n",
554 __func__, gli->mode, mode);
555 rc = -EINVAL;
556 goto out;
557 }
558
559 gli->users++;
560 WARN_ON(gli->users <= 0);
561out:
562 pr_debug("%s: Returning rc=%d gli->mode=%u gli->users=%u\n",
563 __func__, rc, gli->mode, gli->users);
564 if (!locked)
565 mutex_unlock(&gli->mutex);
566 return rc;
567}
568
569
570
571
572
573
574
575
576
577
578
579void cxlflash_lun_detach(struct glun_info *gli)
580{
581 mutex_lock(&gli->mutex);
582 WARN_ON(gli->mode == MODE_NONE);
583 if (--gli->users == 0) {
584 gli->mode = MODE_NONE;
585 cxlflash_ba_terminate(&gli->blka.ba_lun);
586 }
587 pr_debug("%s: gli->users=%u\n", __func__, gli->users);
588 WARN_ON(gli->users < 0);
589 mutex_unlock(&gli->mutex);
590}
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607int _cxlflash_disk_release(struct scsi_device *sdev,
608 struct ctx_info *ctxi,
609 struct dk_cxlflash_release *release)
610{
611 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
612 struct device *dev = &cfg->dev->dev;
613 struct llun_info *lli = sdev->hostdata;
614 struct glun_info *gli = lli->parent;
615 struct afu *afu = cfg->afu;
616 bool put_ctx = false;
617
618 struct dk_cxlflash_resize size;
619 res_hndl_t rhndl = release->rsrc_handle;
620
621 int rc = 0;
622 u64 ctxid = DECODE_CTXID(release->context_id),
623 rctxid = release->context_id;
624
625 struct sisl_rht_entry *rhte;
626 struct sisl_rht_entry_f1 *rhte_f1;
627
628 dev_dbg(dev, "%s: ctxid=%llu rhndl=%llu gli->mode=%u gli->users=%u\n",
629 __func__, ctxid, release->rsrc_handle, gli->mode, gli->users);
630
631 if (!ctxi) {
632 ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
633 if (unlikely(!ctxi)) {
634 dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
635 __func__, ctxid);
636 rc = -EINVAL;
637 goto out;
638 }
639
640 put_ctx = true;
641 }
642
643 rhte = get_rhte(ctxi, rhndl, lli);
644 if (unlikely(!rhte)) {
645 dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
646 __func__, rhndl);
647 rc = -EINVAL;
648 goto out;
649 }
650
651
652
653
654
655
656
657
658 switch (gli->mode) {
659 case MODE_VIRTUAL:
660 marshal_rele_to_resize(release, &size);
661 size.req_size = 0;
662 rc = _cxlflash_vlun_resize(sdev, ctxi, &size);
663 if (rc) {
664 dev_dbg(dev, "%s: resize failed rc %d\n", __func__, rc);
665 goto out;
666 }
667
668 break;
669 case MODE_PHYSICAL:
670
671
672
673
674
675 rhte_f1 = (struct sisl_rht_entry_f1 *)rhte;
676
677 rhte_f1->valid = 0;
678 dma_wmb();
679
680 rhte_f1->lun_id = 0;
681 dma_wmb();
682
683 rhte_f1->dw = 0;
684 dma_wmb();
685
686 if (!ctxi->err_recovery_active)
687 cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC);
688 break;
689 default:
690 WARN(1, "Unsupported LUN mode!");
691 goto out;
692 }
693
694 rhte_checkin(ctxi, rhte);
695 cxlflash_lun_detach(gli);
696
697out:
698 if (put_ctx)
699 put_context(ctxi);
700 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
701 return rc;
702}
703
704int cxlflash_disk_release(struct scsi_device *sdev,
705 struct dk_cxlflash_release *release)
706{
707 return _cxlflash_disk_release(sdev, NULL, release);
708}
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723static void destroy_context(struct cxlflash_cfg *cfg,
724 struct ctx_info *ctxi)
725{
726 struct afu *afu = cfg->afu;
727
728 if (ctxi->initialized) {
729 WARN_ON(!list_empty(&ctxi->luns));
730
731
732 if (afu->afu_map && ctxi->ctrl_map) {
733 writeq_be(0, &ctxi->ctrl_map->rht_start);
734 writeq_be(0, &ctxi->ctrl_map->rht_cnt_id);
735 writeq_be(0, &ctxi->ctrl_map->ctx_cap);
736 }
737 }
738
739
740 free_page((ulong)ctxi->rht_start);
741 kfree(ctxi->rht_needs_ws);
742 kfree(ctxi->rht_lun);
743 kfree(ctxi);
744}
745
746
747
748
749
750
751
752static struct ctx_info *create_context(struct cxlflash_cfg *cfg)
753{
754 struct device *dev = &cfg->dev->dev;
755 struct ctx_info *ctxi = NULL;
756 struct llun_info **lli = NULL;
757 u8 *ws = NULL;
758 struct sisl_rht_entry *rhte;
759
760 ctxi = kzalloc(sizeof(*ctxi), GFP_KERNEL);
761 lli = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*lli)), GFP_KERNEL);
762 ws = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*ws)), GFP_KERNEL);
763 if (unlikely(!ctxi || !lli || !ws)) {
764 dev_err(dev, "%s: Unable to allocate context\n", __func__);
765 goto err;
766 }
767
768 rhte = (struct sisl_rht_entry *)get_zeroed_page(GFP_KERNEL);
769 if (unlikely(!rhte)) {
770 dev_err(dev, "%s: Unable to allocate RHT\n", __func__);
771 goto err;
772 }
773
774 ctxi->rht_lun = lli;
775 ctxi->rht_needs_ws = ws;
776 ctxi->rht_start = rhte;
777out:
778 return ctxi;
779
780err:
781 kfree(ws);
782 kfree(lli);
783 kfree(ctxi);
784 ctxi = NULL;
785 goto out;
786}
787
788
789
790
791
792
793
794
795
796
797static void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg,
798 struct cxl_context *ctx, int ctxid, struct file *file,
799 u32 perms)
800{
801 struct afu *afu = cfg->afu;
802
803 ctxi->rht_perms = perms;
804 ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
805 ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
806 ctxi->pid = current->tgid;
807 ctxi->ctx = ctx;
808 ctxi->cfg = cfg;
809 ctxi->file = file;
810 ctxi->initialized = true;
811 mutex_init(&ctxi->mutex);
812 kref_init(&ctxi->kref);
813 INIT_LIST_HEAD(&ctxi->luns);
814 INIT_LIST_HEAD(&ctxi->list);
815}
816
817
818
819
820
821
822
823
824
825static void remove_context(struct kref *kref)
826{
827 struct ctx_info *ctxi = container_of(kref, struct ctx_info, kref);
828 struct cxlflash_cfg *cfg = ctxi->cfg;
829 u64 ctxid = DECODE_CTXID(ctxi->ctxid);
830
831
832 WARN_ON(!mutex_is_locked(&ctxi->mutex));
833 ctxi->unavail = true;
834 mutex_unlock(&ctxi->mutex);
835 mutex_lock(&cfg->ctx_tbl_list_mutex);
836 mutex_lock(&ctxi->mutex);
837
838 if (!list_empty(&ctxi->list))
839 list_del(&ctxi->list);
840 cfg->ctx_tbl[ctxid] = NULL;
841 mutex_unlock(&cfg->ctx_tbl_list_mutex);
842 mutex_unlock(&ctxi->mutex);
843
844
845 destroy_context(cfg, ctxi);
846}
847
848
849
850
851
852
853
854
855
856
857
858
859
860static int _cxlflash_disk_detach(struct scsi_device *sdev,
861 struct ctx_info *ctxi,
862 struct dk_cxlflash_detach *detach)
863{
864 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
865 struct device *dev = &cfg->dev->dev;
866 struct llun_info *lli = sdev->hostdata;
867 struct lun_access *lun_access, *t;
868 struct dk_cxlflash_release rel;
869 bool put_ctx = false;
870
871 int i;
872 int rc = 0;
873 u64 ctxid = DECODE_CTXID(detach->context_id),
874 rctxid = detach->context_id;
875
876 dev_dbg(dev, "%s: ctxid=%llu\n", __func__, ctxid);
877
878 if (!ctxi) {
879 ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
880 if (unlikely(!ctxi)) {
881 dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
882 __func__, ctxid);
883 rc = -EINVAL;
884 goto out;
885 }
886
887 put_ctx = true;
888 }
889
890
891 if (ctxi->rht_out) {
892 marshal_det_to_rele(detach, &rel);
893 for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
894 if (ctxi->rht_lun[i] == lli) {
895 rel.rsrc_handle = i;
896 _cxlflash_disk_release(sdev, ctxi, &rel);
897 }
898
899
900 if (ctxi->rht_out == 0)
901 break;
902 }
903 }
904
905
906 list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
907 if (lun_access->lli == lli) {
908 list_del(&lun_access->list);
909 kfree(lun_access);
910 lun_access = NULL;
911 break;
912 }
913
914
915
916
917
918 if (kref_put(&ctxi->kref, remove_context))
919 put_ctx = false;
920 scsi_device_put(sdev);
921out:
922 if (put_ctx)
923 put_context(ctxi);
924 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
925 return rc;
926}
927
928static int cxlflash_disk_detach(struct scsi_device *sdev,
929 struct dk_cxlflash_detach *detach)
930{
931 return _cxlflash_disk_detach(sdev, NULL, detach);
932}
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956static int cxlflash_cxl_release(struct inode *inode, struct file *file)
957{
958 struct cxl_context *ctx = cxl_fops_get_context(file);
959 struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
960 cxl_fops);
961 struct device *dev = &cfg->dev->dev;
962 struct ctx_info *ctxi = NULL;
963 struct dk_cxlflash_detach detach = { { 0 }, 0 };
964 struct lun_access *lun_access, *t;
965 enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
966 int ctxid;
967
968 ctxid = cxl_process_element(ctx);
969 if (unlikely(ctxid < 0)) {
970 dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
971 __func__, ctx, ctxid);
972 goto out;
973 }
974
975 ctxi = get_context(cfg, ctxid, file, ctrl);
976 if (unlikely(!ctxi)) {
977 ctxi = get_context(cfg, ctxid, file, ctrl | CTX_CTRL_CLONE);
978 if (!ctxi) {
979 dev_dbg(dev, "%s: ctxid=%d already free\n",
980 __func__, ctxid);
981 goto out_release;
982 }
983
984 dev_dbg(dev, "%s: Another process owns ctxid=%d\n",
985 __func__, ctxid);
986 put_context(ctxi);
987 goto out;
988 }
989
990 dev_dbg(dev, "%s: close for ctxid=%d\n", __func__, ctxid);
991
992 detach.context_id = ctxi->ctxid;
993 list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
994 _cxlflash_disk_detach(lun_access->sdev, ctxi, &detach);
995out_release:
996 cxl_fd_release(inode, file);
997out:
998 dev_dbg(dev, "%s: returning\n", __func__);
999 return 0;
1000}
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010static void unmap_context(struct ctx_info *ctxi)
1011{
1012 unmap_mapping_range(ctxi->file->f_mapping, 0, 0, 1);
1013}
1014
1015
1016
1017
1018
1019
1020
1021static struct page *get_err_page(struct cxlflash_cfg *cfg)
1022{
1023 struct page *err_page = global.err_page;
1024 struct device *dev = &cfg->dev->dev;
1025
1026 if (unlikely(!err_page)) {
1027 err_page = alloc_page(GFP_KERNEL);
1028 if (unlikely(!err_page)) {
1029 dev_err(dev, "%s: Unable to allocate err_page\n",
1030 __func__);
1031 goto out;
1032 }
1033
1034 memset(page_address(err_page), -1, PAGE_SIZE);
1035
1036
1037 mutex_lock(&global.mutex);
1038 if (likely(!global.err_page))
1039 global.err_page = err_page;
1040 else {
1041 __free_page(err_page);
1042 err_page = global.err_page;
1043 }
1044 mutex_unlock(&global.mutex);
1045 }
1046
1047out:
1048 dev_dbg(dev, "%s: returning err_page=%p\n", __func__, err_page);
1049 return err_page;
1050}
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067static int cxlflash_mmap_fault(struct vm_fault *vmf)
1068{
1069 struct vm_area_struct *vma = vmf->vma;
1070 struct file *file = vma->vm_file;
1071 struct cxl_context *ctx = cxl_fops_get_context(file);
1072 struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
1073 cxl_fops);
1074 struct device *dev = &cfg->dev->dev;
1075 struct ctx_info *ctxi = NULL;
1076 struct page *err_page = NULL;
1077 enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
1078 int rc = 0;
1079 int ctxid;
1080
1081 ctxid = cxl_process_element(ctx);
1082 if (unlikely(ctxid < 0)) {
1083 dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
1084 __func__, ctx, ctxid);
1085 goto err;
1086 }
1087
1088 ctxi = get_context(cfg, ctxid, file, ctrl);
1089 if (unlikely(!ctxi)) {
1090 dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
1091 goto err;
1092 }
1093
1094 dev_dbg(dev, "%s: fault for context %d\n", __func__, ctxid);
1095
1096 if (likely(!ctxi->err_recovery_active)) {
1097 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1098 rc = ctxi->cxl_mmap_vmops->fault(vmf);
1099 } else {
1100 dev_dbg(dev, "%s: err recovery active, use err_page\n",
1101 __func__);
1102
1103 err_page = get_err_page(cfg);
1104 if (unlikely(!err_page)) {
1105 dev_err(dev, "%s: Could not get err_page\n", __func__);
1106 rc = VM_FAULT_RETRY;
1107 goto out;
1108 }
1109
1110 get_page(err_page);
1111 vmf->page = err_page;
1112 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
1113 }
1114
1115out:
1116 if (likely(ctxi))
1117 put_context(ctxi);
1118 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1119 return rc;
1120
1121err:
1122 rc = VM_FAULT_SIGBUS;
1123 goto out;
1124}
1125
1126
1127
1128
1129static const struct vm_operations_struct cxlflash_mmap_vmops = {
1130 .fault = cxlflash_mmap_fault,
1131};
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142static int cxlflash_cxl_mmap(struct file *file, struct vm_area_struct *vma)
1143{
1144 struct cxl_context *ctx = cxl_fops_get_context(file);
1145 struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
1146 cxl_fops);
1147 struct device *dev = &cfg->dev->dev;
1148 struct ctx_info *ctxi = NULL;
1149 enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
1150 int ctxid;
1151 int rc = 0;
1152
1153 ctxid = cxl_process_element(ctx);
1154 if (unlikely(ctxid < 0)) {
1155 dev_err(dev, "%s: Context %p was closed ctxid=%d\n",
1156 __func__, ctx, ctxid);
1157 rc = -EIO;
1158 goto out;
1159 }
1160
1161 ctxi = get_context(cfg, ctxid, file, ctrl);
1162 if (unlikely(!ctxi)) {
1163 dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid);
1164 rc = -EIO;
1165 goto out;
1166 }
1167
1168 dev_dbg(dev, "%s: mmap for context %d\n", __func__, ctxid);
1169
1170 rc = cxl_fd_mmap(file, vma);
1171 if (likely(!rc)) {
1172
1173 ctxi->cxl_mmap_vmops = vma->vm_ops;
1174 vma->vm_ops = &cxlflash_mmap_vmops;
1175 }
1176
1177out:
1178 if (likely(ctxi))
1179 put_context(ctxi);
1180 return rc;
1181}
1182
1183const struct file_operations cxlflash_cxl_fops = {
1184 .owner = THIS_MODULE,
1185 .mmap = cxlflash_cxl_mmap,
1186 .release = cxlflash_cxl_release,
1187};
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198int cxlflash_mark_contexts_error(struct cxlflash_cfg *cfg)
1199{
1200 int i, rc = 0;
1201 struct ctx_info *ctxi = NULL;
1202
1203 mutex_lock(&cfg->ctx_tbl_list_mutex);
1204
1205 for (i = 0; i < MAX_CONTEXT; i++) {
1206 ctxi = cfg->ctx_tbl[i];
1207 if (ctxi) {
1208 mutex_lock(&ctxi->mutex);
1209 cfg->ctx_tbl[i] = NULL;
1210 list_add(&ctxi->list, &cfg->ctx_err_recovery);
1211 ctxi->err_recovery_active = true;
1212 ctxi->ctrl_map = NULL;
1213 unmap_context(ctxi);
1214 mutex_unlock(&ctxi->mutex);
1215 }
1216 }
1217
1218 mutex_unlock(&cfg->ctx_tbl_list_mutex);
1219 return rc;
1220}
1221
1222
1223
1224
1225static const struct file_operations null_fops = {
1226 .owner = THIS_MODULE,
1227};
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242int check_state(struct cxlflash_cfg *cfg)
1243{
1244 struct device *dev = &cfg->dev->dev;
1245 int rc = 0;
1246
1247retry:
1248 switch (cfg->state) {
1249 case STATE_RESET:
1250 dev_dbg(dev, "%s: Reset state, going to wait...\n", __func__);
1251 up_read(&cfg->ioctl_rwsem);
1252 rc = wait_event_interruptible(cfg->reset_waitq,
1253 cfg->state != STATE_RESET);
1254 down_read(&cfg->ioctl_rwsem);
1255 if (unlikely(rc))
1256 break;
1257 goto retry;
1258 case STATE_FAILTERM:
1259 dev_dbg(dev, "%s: Failed/Terminating\n", __func__);
1260 rc = -ENODEV;
1261 break;
1262 default:
1263 break;
1264 }
1265
1266 return rc;
1267}
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281static int cxlflash_disk_attach(struct scsi_device *sdev,
1282 struct dk_cxlflash_attach *attach)
1283{
1284 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1285 struct device *dev = &cfg->dev->dev;
1286 struct afu *afu = cfg->afu;
1287 struct llun_info *lli = sdev->hostdata;
1288 struct glun_info *gli = lli->parent;
1289 struct cxl_ioctl_start_work *work;
1290 struct ctx_info *ctxi = NULL;
1291 struct lun_access *lun_access = NULL;
1292 int rc = 0;
1293 u32 perms;
1294 int ctxid = -1;
1295 u64 flags = 0UL;
1296 u64 rctxid = 0UL;
1297 struct file *file = NULL;
1298
1299 struct cxl_context *ctx = NULL;
1300
1301 int fd = -1;
1302
1303 if (attach->num_interrupts > 4) {
1304 dev_dbg(dev, "%s: Cannot support this many interrupts %llu\n",
1305 __func__, attach->num_interrupts);
1306 rc = -EINVAL;
1307 goto out;
1308 }
1309
1310 if (gli->max_lba == 0) {
1311 dev_dbg(dev, "%s: No capacity info for LUN=%016llx\n",
1312 __func__, lli->lun_id[sdev->channel]);
1313 rc = read_cap16(sdev, lli);
1314 if (rc) {
1315 dev_err(dev, "%s: Invalid device rc=%d\n",
1316 __func__, rc);
1317 rc = -ENODEV;
1318 goto out;
1319 }
1320 dev_dbg(dev, "%s: LBA = %016llx\n", __func__, gli->max_lba);
1321 dev_dbg(dev, "%s: BLK_LEN = %08x\n", __func__, gli->blk_len);
1322 }
1323
1324 if (attach->hdr.flags & DK_CXLFLASH_ATTACH_REUSE_CONTEXT) {
1325 rctxid = attach->context_id;
1326 ctxi = get_context(cfg, rctxid, NULL, 0);
1327 if (!ctxi) {
1328 dev_dbg(dev, "%s: Bad context rctxid=%016llx\n",
1329 __func__, rctxid);
1330 rc = -EINVAL;
1331 goto out;
1332 }
1333
1334 list_for_each_entry(lun_access, &ctxi->luns, list)
1335 if (lun_access->lli == lli) {
1336 dev_dbg(dev, "%s: Already attached\n",
1337 __func__);
1338 rc = -EINVAL;
1339 goto out;
1340 }
1341 }
1342
1343 rc = scsi_device_get(sdev);
1344 if (unlikely(rc)) {
1345 dev_err(dev, "%s: Unable to get sdev reference\n", __func__);
1346 goto out;
1347 }
1348
1349 lun_access = kzalloc(sizeof(*lun_access), GFP_KERNEL);
1350 if (unlikely(!lun_access)) {
1351 dev_err(dev, "%s: Unable to allocate lun_access\n", __func__);
1352 rc = -ENOMEM;
1353 goto err;
1354 }
1355
1356 lun_access->lli = lli;
1357 lun_access->sdev = sdev;
1358
1359
1360 if (ctxi) {
1361 dev_dbg(dev, "%s: Reusing context for LUN rctxid=%016llx\n",
1362 __func__, rctxid);
1363 kref_get(&ctxi->kref);
1364 list_add(&lun_access->list, &ctxi->luns);
1365 goto out_attach;
1366 }
1367
1368 ctxi = create_context(cfg);
1369 if (unlikely(!ctxi)) {
1370 dev_err(dev, "%s: Failed to create context ctxid=%d\n",
1371 __func__, ctxid);
1372 goto err;
1373 }
1374
1375 ctx = cxl_dev_context_init(cfg->dev);
1376 if (IS_ERR_OR_NULL(ctx)) {
1377 dev_err(dev, "%s: Could not initialize context %p\n",
1378 __func__, ctx);
1379 rc = -ENODEV;
1380 goto err;
1381 }
1382
1383 work = &ctxi->work;
1384 work->num_interrupts = attach->num_interrupts;
1385 work->flags = CXL_START_WORK_NUM_IRQS;
1386
1387 rc = cxl_start_work(ctx, work);
1388 if (unlikely(rc)) {
1389 dev_dbg(dev, "%s: Could not start context rc=%d\n",
1390 __func__, rc);
1391 goto err;
1392 }
1393
1394 ctxid = cxl_process_element(ctx);
1395 if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
1396 dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
1397 rc = -EPERM;
1398 goto err;
1399 }
1400
1401 file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd);
1402 if (unlikely(fd < 0)) {
1403 rc = -ENODEV;
1404 dev_err(dev, "%s: Could not get file descriptor\n", __func__);
1405 goto err;
1406 }
1407
1408
1409 perms = SISL_RHT_PERM(attach->hdr.flags + 1);
1410
1411
1412 init_context(ctxi, cfg, ctx, ctxid, file, perms);
1413
1414 rc = afu_attach(cfg, ctxi);
1415 if (unlikely(rc)) {
1416 dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
1417 goto err;
1418 }
1419
1420
1421
1422
1423
1424
1425
1426 list_add(&lun_access->list, &ctxi->luns);
1427 mutex_lock(&cfg->ctx_tbl_list_mutex);
1428 mutex_lock(&ctxi->mutex);
1429 cfg->ctx_tbl[ctxid] = ctxi;
1430 mutex_unlock(&cfg->ctx_tbl_list_mutex);
1431 fd_install(fd, file);
1432
1433out_attach:
1434 if (fd != -1)
1435 flags |= DK_CXLFLASH_APP_CLOSE_ADAP_FD;
1436 if (afu_is_sq_cmd_mode(afu))
1437 flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
1438
1439 attach->hdr.return_flags = flags;
1440 attach->context_id = ctxi->ctxid;
1441 attach->block_size = gli->blk_len;
1442 attach->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
1443 attach->last_lba = gli->max_lba;
1444 attach->max_xfer = sdev->host->max_sectors * MAX_SECTOR_UNIT;
1445 attach->max_xfer /= gli->blk_len;
1446
1447out:
1448 attach->adap_fd = fd;
1449
1450 if (ctxi)
1451 put_context(ctxi);
1452
1453 dev_dbg(dev, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
1454 __func__, ctxid, fd, attach->block_size, rc, attach->last_lba);
1455 return rc;
1456
1457err:
1458
1459 if (!IS_ERR_OR_NULL(ctx)) {
1460 cxl_stop_context(ctx);
1461 cxl_release_context(ctx);
1462 ctx = NULL;
1463 }
1464
1465
1466
1467
1468
1469
1470
1471
1472 if (fd > 0) {
1473 file->f_op = &null_fops;
1474 fput(file);
1475 put_unused_fd(fd);
1476 fd = -1;
1477 file = NULL;
1478 }
1479
1480
1481 if (ctxi) {
1482 destroy_context(cfg, ctxi);
1483 ctxi = NULL;
1484 }
1485
1486 kfree(lun_access);
1487 scsi_device_put(sdev);
1488 goto out;
1489}
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501static int recover_context(struct cxlflash_cfg *cfg,
1502 struct ctx_info *ctxi,
1503 int *adap_fd)
1504{
1505 struct device *dev = &cfg->dev->dev;
1506 int rc = 0;
1507 int fd = -1;
1508 int ctxid = -1;
1509 struct file *file;
1510 struct cxl_context *ctx;
1511 struct afu *afu = cfg->afu;
1512
1513 ctx = cxl_dev_context_init(cfg->dev);
1514 if (IS_ERR_OR_NULL(ctx)) {
1515 dev_err(dev, "%s: Could not initialize context %p\n",
1516 __func__, ctx);
1517 rc = -ENODEV;
1518 goto out;
1519 }
1520
1521 rc = cxl_start_work(ctx, &ctxi->work);
1522 if (unlikely(rc)) {
1523 dev_dbg(dev, "%s: Could not start context rc=%d\n",
1524 __func__, rc);
1525 goto err1;
1526 }
1527
1528 ctxid = cxl_process_element(ctx);
1529 if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
1530 dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid);
1531 rc = -EPERM;
1532 goto err2;
1533 }
1534
1535 file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd);
1536 if (unlikely(fd < 0)) {
1537 rc = -ENODEV;
1538 dev_err(dev, "%s: Could not get file descriptor\n", __func__);
1539 goto err2;
1540 }
1541
1542
1543 ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
1544
1545 rc = afu_attach(cfg, ctxi);
1546 if (rc) {
1547 dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
1548 goto err3;
1549 }
1550
1551
1552
1553
1554
1555 ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
1556 ctxi->ctx = ctx;
1557 ctxi->file = file;
1558
1559
1560
1561
1562
1563
1564
1565 mutex_unlock(&ctxi->mutex);
1566 mutex_lock(&cfg->ctx_tbl_list_mutex);
1567 mutex_lock(&ctxi->mutex);
1568 list_del_init(&ctxi->list);
1569 cfg->ctx_tbl[ctxid] = ctxi;
1570 mutex_unlock(&cfg->ctx_tbl_list_mutex);
1571 fd_install(fd, file);
1572 *adap_fd = fd;
1573out:
1574 dev_dbg(dev, "%s: returning ctxid=%d fd=%d rc=%d\n",
1575 __func__, ctxid, fd, rc);
1576 return rc;
1577
1578err3:
1579 fput(file);
1580 put_unused_fd(fd);
1581err2:
1582 cxl_stop_context(ctx);
1583err1:
1584 cxl_release_context(ctx);
1585 goto out;
1586}
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618static int cxlflash_afu_recover(struct scsi_device *sdev,
1619 struct dk_cxlflash_recover_afu *recover)
1620{
1621 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1622 struct device *dev = &cfg->dev->dev;
1623 struct llun_info *lli = sdev->hostdata;
1624 struct afu *afu = cfg->afu;
1625 struct ctx_info *ctxi = NULL;
1626 struct mutex *mutex = &cfg->ctx_recovery_mutex;
1627 u64 flags;
1628 u64 ctxid = DECODE_CTXID(recover->context_id),
1629 rctxid = recover->context_id;
1630 long reg;
1631 int lretry = 20;
1632 int new_adap_fd = -1;
1633 int rc = 0;
1634
1635 atomic_inc(&cfg->recovery_threads);
1636 up_read(&cfg->ioctl_rwsem);
1637 rc = mutex_lock_interruptible(mutex);
1638 down_read(&cfg->ioctl_rwsem);
1639 if (rc)
1640 goto out;
1641 rc = check_state(cfg);
1642 if (rc) {
1643 dev_err(dev, "%s: Failed state rc=%d\n", __func__, rc);
1644 rc = -ENODEV;
1645 goto out;
1646 }
1647
1648 dev_dbg(dev, "%s: reason=%016llx rctxid=%016llx\n",
1649 __func__, recover->reason, rctxid);
1650
1651retry:
1652
1653 ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
1654 if (unlikely(!ctxi)) {
1655 dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
1656 rc = -EINVAL;
1657 goto out;
1658 }
1659
1660 if (ctxi->err_recovery_active) {
1661retry_recover:
1662 rc = recover_context(cfg, ctxi, &new_adap_fd);
1663 if (unlikely(rc)) {
1664 dev_err(dev, "%s: Recovery failed ctxid=%llu rc=%d\n",
1665 __func__, ctxid, rc);
1666 if ((rc == -ENODEV) &&
1667 ((atomic_read(&cfg->recovery_threads) > 1) ||
1668 (lretry--))) {
1669 dev_dbg(dev, "%s: Going to try again\n",
1670 __func__);
1671 mutex_unlock(mutex);
1672 msleep(100);
1673 rc = mutex_lock_interruptible(mutex);
1674 if (rc)
1675 goto out;
1676 goto retry_recover;
1677 }
1678
1679 goto out;
1680 }
1681
1682 ctxi->err_recovery_active = false;
1683
1684 flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD |
1685 DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET;
1686 if (afu_is_sq_cmd_mode(afu))
1687 flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE;
1688
1689 recover->hdr.return_flags = flags;
1690 recover->context_id = ctxi->ctxid;
1691 recover->adap_fd = new_adap_fd;
1692 recover->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
1693 goto out;
1694 }
1695
1696
1697 reg = readq_be(&afu->ctrl_map->mbox_r);
1698 if (reg == -1) {
1699 dev_dbg(dev, "%s: MMIO fail, wait for recovery.\n", __func__);
1700
1701
1702
1703
1704
1705
1706 put_context(ctxi);
1707 ctxi = NULL;
1708 ssleep(1);
1709 rc = check_state(cfg);
1710 if (unlikely(rc))
1711 goto out;
1712 goto retry;
1713 }
1714
1715 dev_dbg(dev, "%s: MMIO working, no recovery required\n", __func__);
1716out:
1717 if (likely(ctxi))
1718 put_context(ctxi);
1719 mutex_unlock(mutex);
1720 atomic_dec_if_positive(&cfg->recovery_threads);
1721 return rc;
1722}
1723
1724
1725
1726
1727
1728
1729
1730
1731static int process_sense(struct scsi_device *sdev,
1732 struct dk_cxlflash_verify *verify)
1733{
1734 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1735 struct device *dev = &cfg->dev->dev;
1736 struct llun_info *lli = sdev->hostdata;
1737 struct glun_info *gli = lli->parent;
1738 u64 prev_lba = gli->max_lba;
1739 struct scsi_sense_hdr sshdr = { 0 };
1740 int rc = 0;
1741
1742 rc = scsi_normalize_sense((const u8 *)&verify->sense_data,
1743 DK_CXLFLASH_VERIFY_SENSE_LEN, &sshdr);
1744 if (!rc) {
1745 dev_err(dev, "%s: Failed to normalize sense data\n", __func__);
1746 rc = -EINVAL;
1747 goto out;
1748 }
1749
1750 switch (sshdr.sense_key) {
1751 case NO_SENSE:
1752 case RECOVERED_ERROR:
1753
1754 case NOT_READY:
1755 break;
1756 case UNIT_ATTENTION:
1757 switch (sshdr.asc) {
1758 case 0x29:
1759
1760 case 0x2A:
1761 rc = read_cap16(sdev, lli);
1762 if (rc) {
1763 rc = -ENODEV;
1764 break;
1765 }
1766 if (prev_lba != gli->max_lba)
1767 dev_dbg(dev, "%s: Capacity changed old=%lld "
1768 "new=%lld\n", __func__, prev_lba,
1769 gli->max_lba);
1770 break;
1771 case 0x3F:
1772 scsi_scan_host(cfg->host);
1773 break;
1774 default:
1775 rc = -EIO;
1776 break;
1777 }
1778 break;
1779 default:
1780 rc = -EIO;
1781 break;
1782 }
1783out:
1784 dev_dbg(dev, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__,
1785 sshdr.sense_key, sshdr.asc, sshdr.ascq, rc);
1786 return rc;
1787}
1788
1789
1790
1791
1792
1793
1794
1795
1796static int cxlflash_disk_verify(struct scsi_device *sdev,
1797 struct dk_cxlflash_verify *verify)
1798{
1799 int rc = 0;
1800 struct ctx_info *ctxi = NULL;
1801 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1802 struct device *dev = &cfg->dev->dev;
1803 struct llun_info *lli = sdev->hostdata;
1804 struct glun_info *gli = lli->parent;
1805 struct sisl_rht_entry *rhte = NULL;
1806 res_hndl_t rhndl = verify->rsrc_handle;
1807 u64 ctxid = DECODE_CTXID(verify->context_id),
1808 rctxid = verify->context_id;
1809 u64 last_lba = 0;
1810
1811 dev_dbg(dev, "%s: ctxid=%llu rhndl=%016llx, hint=%016llx, "
1812 "flags=%016llx\n", __func__, ctxid, verify->rsrc_handle,
1813 verify->hint, verify->hdr.flags);
1814
1815 ctxi = get_context(cfg, rctxid, lli, 0);
1816 if (unlikely(!ctxi)) {
1817 dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
1818 rc = -EINVAL;
1819 goto out;
1820 }
1821
1822 rhte = get_rhte(ctxi, rhndl, lli);
1823 if (unlikely(!rhte)) {
1824 dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n",
1825 __func__, rhndl);
1826 rc = -EINVAL;
1827 goto out;
1828 }
1829
1830
1831
1832
1833
1834 if (verify->hint & DK_CXLFLASH_VERIFY_HINT_SENSE) {
1835
1836
1837
1838 ctxi->unavail = true;
1839 mutex_unlock(&ctxi->mutex);
1840 rc = process_sense(sdev, verify);
1841 if (unlikely(rc)) {
1842 dev_err(dev, "%s: Failed to validate sense data (%d)\n",
1843 __func__, rc);
1844 mutex_lock(&ctxi->mutex);
1845 ctxi->unavail = false;
1846 goto out;
1847 }
1848 mutex_lock(&ctxi->mutex);
1849 ctxi->unavail = false;
1850 }
1851
1852 switch (gli->mode) {
1853 case MODE_PHYSICAL:
1854 last_lba = gli->max_lba;
1855 break;
1856 case MODE_VIRTUAL:
1857
1858 last_lba = ((u64)rhte->lxt_cnt * MC_CHUNK_SIZE * gli->blk_len);
1859 last_lba /= CXLFLASH_BLOCK_SIZE;
1860 last_lba--;
1861 break;
1862 default:
1863 WARN(1, "Unsupported LUN mode!");
1864 }
1865
1866 verify->last_lba = last_lba;
1867
1868out:
1869 if (likely(ctxi))
1870 put_context(ctxi);
1871 dev_dbg(dev, "%s: returning rc=%d llba=%llx\n",
1872 __func__, rc, verify->last_lba);
1873 return rc;
1874}
1875
1876
1877
1878
1879
1880
1881
1882static char *decode_ioctl(int cmd)
1883{
1884 switch (cmd) {
1885 case DK_CXLFLASH_ATTACH:
1886 return __stringify_1(DK_CXLFLASH_ATTACH);
1887 case DK_CXLFLASH_USER_DIRECT:
1888 return __stringify_1(DK_CXLFLASH_USER_DIRECT);
1889 case DK_CXLFLASH_USER_VIRTUAL:
1890 return __stringify_1(DK_CXLFLASH_USER_VIRTUAL);
1891 case DK_CXLFLASH_VLUN_RESIZE:
1892 return __stringify_1(DK_CXLFLASH_VLUN_RESIZE);
1893 case DK_CXLFLASH_RELEASE:
1894 return __stringify_1(DK_CXLFLASH_RELEASE);
1895 case DK_CXLFLASH_DETACH:
1896 return __stringify_1(DK_CXLFLASH_DETACH);
1897 case DK_CXLFLASH_VERIFY:
1898 return __stringify_1(DK_CXLFLASH_VERIFY);
1899 case DK_CXLFLASH_VLUN_CLONE:
1900 return __stringify_1(DK_CXLFLASH_VLUN_CLONE);
1901 case DK_CXLFLASH_RECOVER_AFU:
1902 return __stringify_1(DK_CXLFLASH_RECOVER_AFU);
1903 case DK_CXLFLASH_MANAGE_LUN:
1904 return __stringify_1(DK_CXLFLASH_MANAGE_LUN);
1905 }
1906
1907 return "UNKNOWN";
1908}
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921static int cxlflash_disk_direct_open(struct scsi_device *sdev, void *arg)
1922{
1923 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
1924 struct device *dev = &cfg->dev->dev;
1925 struct afu *afu = cfg->afu;
1926 struct llun_info *lli = sdev->hostdata;
1927 struct glun_info *gli = lli->parent;
1928
1929 struct dk_cxlflash_udirect *pphys = (struct dk_cxlflash_udirect *)arg;
1930
1931 u64 ctxid = DECODE_CTXID(pphys->context_id),
1932 rctxid = pphys->context_id;
1933 u64 lun_size = 0;
1934 u64 last_lba = 0;
1935 u64 rsrc_handle = -1;
1936 u32 port = CHAN2PORT(sdev->channel);
1937
1938 int rc = 0;
1939
1940 struct ctx_info *ctxi = NULL;
1941 struct sisl_rht_entry *rhte = NULL;
1942
1943 dev_dbg(dev, "%s: ctxid=%llu ls=%llu\n", __func__, ctxid, lun_size);
1944
1945 rc = cxlflash_lun_attach(gli, MODE_PHYSICAL, false);
1946 if (unlikely(rc)) {
1947 dev_dbg(dev, "%s: Failed attach to LUN (PHYSICAL)\n", __func__);
1948 goto out;
1949 }
1950
1951 ctxi = get_context(cfg, rctxid, lli, 0);
1952 if (unlikely(!ctxi)) {
1953 dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
1954 rc = -EINVAL;
1955 goto err1;
1956 }
1957
1958 rhte = rhte_checkout(ctxi, lli);
1959 if (unlikely(!rhte)) {
1960 dev_dbg(dev, "%s: Too many opens ctxid=%lld\n",
1961 __func__, ctxid);
1962 rc = -EMFILE;
1963 goto err1;
1964 }
1965
1966 rsrc_handle = (rhte - ctxi->rht_start);
1967
1968 rht_format1(rhte, lli->lun_id[sdev->channel], ctxi->rht_perms, port);
1969 cxlflash_afu_sync(afu, ctxid, rsrc_handle, AFU_LW_SYNC);
1970
1971 last_lba = gli->max_lba;
1972 pphys->hdr.return_flags = 0;
1973 pphys->last_lba = last_lba;
1974 pphys->rsrc_handle = rsrc_handle;
1975
1976out:
1977 if (likely(ctxi))
1978 put_context(ctxi);
1979 dev_dbg(dev, "%s: returning handle=%llu rc=%d llba=%llu\n",
1980 __func__, rsrc_handle, rc, last_lba);
1981 return rc;
1982
1983err1:
1984 cxlflash_lun_detach(gli);
1985 goto out;
1986}
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999static int ioctl_common(struct scsi_device *sdev, int cmd)
2000{
2001 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
2002 struct device *dev = &cfg->dev->dev;
2003 struct llun_info *lli = sdev->hostdata;
2004 int rc = 0;
2005
2006 if (unlikely(!lli)) {
2007 dev_dbg(dev, "%s: Unknown LUN\n", __func__);
2008 rc = -EINVAL;
2009 goto out;
2010 }
2011
2012 rc = check_state(cfg);
2013 if (unlikely(rc) && (cfg->state == STATE_FAILTERM)) {
2014 switch (cmd) {
2015 case DK_CXLFLASH_VLUN_RESIZE:
2016 case DK_CXLFLASH_RELEASE:
2017 case DK_CXLFLASH_DETACH:
2018 dev_dbg(dev, "%s: Command override rc=%d\n",
2019 __func__, rc);
2020 rc = 0;
2021 break;
2022 }
2023 }
2024out:
2025 return rc;
2026}
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044int cxlflash_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
2045{
2046 typedef int (*sioctl) (struct scsi_device *, void *);
2047
2048 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
2049 struct device *dev = &cfg->dev->dev;
2050 struct afu *afu = cfg->afu;
2051 struct dk_cxlflash_hdr *hdr;
2052 char buf[sizeof(union cxlflash_ioctls)];
2053 size_t size = 0;
2054 bool known_ioctl = false;
2055 int idx;
2056 int rc = 0;
2057 struct Scsi_Host *shost = sdev->host;
2058 sioctl do_ioctl = NULL;
2059
2060 static const struct {
2061 size_t size;
2062 sioctl ioctl;
2063 } ioctl_tbl[] = {
2064 {sizeof(struct dk_cxlflash_attach), (sioctl)cxlflash_disk_attach},
2065 {sizeof(struct dk_cxlflash_udirect), cxlflash_disk_direct_open},
2066 {sizeof(struct dk_cxlflash_release), (sioctl)cxlflash_disk_release},
2067 {sizeof(struct dk_cxlflash_detach), (sioctl)cxlflash_disk_detach},
2068 {sizeof(struct dk_cxlflash_verify), (sioctl)cxlflash_disk_verify},
2069 {sizeof(struct dk_cxlflash_recover_afu), (sioctl)cxlflash_afu_recover},
2070 {sizeof(struct dk_cxlflash_manage_lun), (sioctl)cxlflash_manage_lun},
2071 {sizeof(struct dk_cxlflash_uvirtual), cxlflash_disk_virtual_open},
2072 {sizeof(struct dk_cxlflash_resize), (sioctl)cxlflash_vlun_resize},
2073 {sizeof(struct dk_cxlflash_clone), (sioctl)cxlflash_disk_clone},
2074 };
2075
2076
2077 down_read(&cfg->ioctl_rwsem);
2078
2079
2080 if (afu->internal_lun)
2081 switch (cmd) {
2082 case DK_CXLFLASH_RELEASE:
2083 case DK_CXLFLASH_USER_VIRTUAL:
2084 case DK_CXLFLASH_VLUN_RESIZE:
2085 case DK_CXLFLASH_VLUN_CLONE:
2086 dev_dbg(dev, "%s: %s not supported for lun_mode=%d\n",
2087 __func__, decode_ioctl(cmd), afu->internal_lun);
2088 rc = -EINVAL;
2089 goto cxlflash_ioctl_exit;
2090 }
2091
2092 switch (cmd) {
2093 case DK_CXLFLASH_ATTACH:
2094 case DK_CXLFLASH_USER_DIRECT:
2095 case DK_CXLFLASH_RELEASE:
2096 case DK_CXLFLASH_DETACH:
2097 case DK_CXLFLASH_VERIFY:
2098 case DK_CXLFLASH_RECOVER_AFU:
2099 case DK_CXLFLASH_USER_VIRTUAL:
2100 case DK_CXLFLASH_VLUN_RESIZE:
2101 case DK_CXLFLASH_VLUN_CLONE:
2102 dev_dbg(dev, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n",
2103 __func__, decode_ioctl(cmd), cmd, shost->host_no,
2104 sdev->channel, sdev->id, sdev->lun);
2105 rc = ioctl_common(sdev, cmd);
2106 if (unlikely(rc))
2107 goto cxlflash_ioctl_exit;
2108
2109
2110
2111 case DK_CXLFLASH_MANAGE_LUN:
2112 known_ioctl = true;
2113 idx = _IOC_NR(cmd) - _IOC_NR(DK_CXLFLASH_ATTACH);
2114 size = ioctl_tbl[idx].size;
2115 do_ioctl = ioctl_tbl[idx].ioctl;
2116
2117 if (likely(do_ioctl))
2118 break;
2119
2120
2121 default:
2122 rc = -EINVAL;
2123 goto cxlflash_ioctl_exit;
2124 }
2125
2126 if (unlikely(copy_from_user(&buf, arg, size))) {
2127 dev_err(dev, "%s: copy_from_user() fail "
2128 "size=%lu cmd=%d (%s) arg=%p\n",
2129 __func__, size, cmd, decode_ioctl(cmd), arg);
2130 rc = -EFAULT;
2131 goto cxlflash_ioctl_exit;
2132 }
2133
2134 hdr = (struct dk_cxlflash_hdr *)&buf;
2135 if (hdr->version != DK_CXLFLASH_VERSION_0) {
2136 dev_dbg(dev, "%s: Version %u not supported for %s\n",
2137 __func__, hdr->version, decode_ioctl(cmd));
2138 rc = -EINVAL;
2139 goto cxlflash_ioctl_exit;
2140 }
2141
2142 if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->rsvd[2] || hdr->return_flags) {
2143 dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__);
2144 rc = -EINVAL;
2145 goto cxlflash_ioctl_exit;
2146 }
2147
2148 rc = do_ioctl(sdev, (void *)&buf);
2149 if (likely(!rc))
2150 if (unlikely(copy_to_user(arg, &buf, size))) {
2151 dev_err(dev, "%s: copy_to_user() fail "
2152 "size=%lu cmd=%d (%s) arg=%p\n",
2153 __func__, size, cmd, decode_ioctl(cmd), arg);
2154 rc = -EFAULT;
2155 }
2156
2157
2158
2159cxlflash_ioctl_exit:
2160 up_read(&cfg->ioctl_rwsem);
2161 if (unlikely(rc && known_ioctl))
2162 dev_err(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2163 "returned rc %d\n", __func__,
2164 decode_ioctl(cmd), cmd, shost->host_no,
2165 sdev->channel, sdev->id, sdev->lun, rc);
2166 else
2167 dev_dbg(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2168 "returned rc %d\n", __func__, decode_ioctl(cmd),
2169 cmd, shost->host_no, sdev->channel, sdev->id,
2170 sdev->lun, rc);
2171 return rc;
2172}
2173