Bring in ISCSI initiator support.
[dragonfly.git] / sys / dev / disk / iscsi / initiator / isc_cam.c
1 /*-
2  * Copyright (c) 2005-2008 Daniel Braniss <danny@cs.huji.ac.il>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/iscsi/initiator/isc_cam.c,v 1.3 2008/11/25 07:17:11 scottl Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/callout.h>
32 #include <sys/lock.h>
33 #include <sys/conf.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/mbuf.h>
37 #include <sys/uio.h>
38 #include <sys/sysctl.h>
39 #include <sys/eventhandler.h>
40 #include <sys/mutex.h>
41 #include <sys/mutex2.h>
42
43 #include <bus/cam/cam.h>
44 #include <bus/cam/cam_ccb.h>
45 #include <bus/cam/cam_sim.h>
46 #include <bus/cam/cam_xpt_sim.h>
47 #include <bus/cam/cam_periph.h>
48
49 #include <dev/disk/iscsi/initiator/iscsi.h>
50 #include <dev/disk/iscsi/initiator/iscsivar.h>
51
52 // XXX: untested/incomplete
53 void
54 ic_freeze(isc_session_t *sp)
55 {
56      debug_called(8);
57 #if 0
58      sdebug(2, "freezing path=%p", sp->cam_path == NULL? 0: sp->cam_path);
59      if((sp->cam_path != NULL) && !(sp->flags & ISC_FROZEN)) {
60           xpt_freeze_devq(sp->cam_path, 1);
61      }
62 #endif
63      sp->flags |= ISC_FROZEN;
64 }
65
66 // XXX: untested/incomplete
67 void
68 ic_release(isc_session_t *sp)
69 {
70      debug_called(8);
71 #if 0
72      sdebug(2, "release path=%p", sp->cam_path == NULL? 0: sp->cam_path);
73      if((sp->cam_path != NULL) && (sp->flags & ISC_FROZEN)) {
74           xpt_release_devq(sp->cam_path, 1, TRUE);
75      }
76 #endif
77      sp->flags &= ~ISC_FROZEN;
78 }
79
80 void
81 ic_lost_target(isc_session_t *sp, int target)
82 {
83      struct isc_softc   *isp = sp->isc;
84
85      debug_called(8);
86      sdebug(2, "target=%d", target);
87      if(sp->cam_path != NULL) {
88           lockmgr(&isp->cam_lock, LK_EXCLUSIVE);
89           xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
90           xpt_free_path(sp->cam_path);
91           lockmgr(&isp->cam_lock, LK_RELEASE);
92           sp->cam_path = 0; // XXX
93      }
94 }
95
96 static void
97 _scan_callback(struct cam_periph *periph, union ccb *ccb)
98 {
99      isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
100
101      debug_called(8);
102
103      kfree(ccb, M_TEMP);
104
105      if(sp->flags & ISC_FFPWAIT) {
106           sp->flags &= ~ISC_FFPWAIT;
107           wakeup(sp);
108      }
109 }
110
111 static void
112 _scan_target(isc_session_t *sp, int target)
113 {
114      union ccb          *ccb;
115
116      debug_called(8);
117      sdebug(2, "target=%d", target);
118
119      if((ccb = kmalloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
120           xdebug("scan failed (can't allocate CCB)");
121           return;
122      }
123      CAM_LOCK(sp->isc);
124      xpt_setup_ccb(&ccb->ccb_h, sp->cam_path, 5/*priority (low)*/);
125      ccb->ccb_h.func_code       = XPT_SCAN_BUS;
126      ccb->ccb_h.cbfcnp          = _scan_callback;
127      ccb->crcn.flags            = CAM_FLAG_NONE;
128      ccb->ccb_h.spriv_ptr0      = sp;
129
130      xpt_action(ccb);
131      CAM_UNLOCK(sp->isc);
132 }
133
134 int
135 ic_fullfeature(struct cdev *dev)
136 {
137      struct isc_softc   *isp = dev->si_drv1;
138      isc_session_t      *sp = (isc_session_t *)dev->si_drv2;
139
140      debug_called(8);
141      sdebug(3, "dev=%d sc=%p", minor(dev), isp);
142
143      sp->flags &= ~ISC_FFPHASE;
144      sp->flags |= ISC_FFPWAIT;
145
146      CAM_LOCK(isp);
147      if(xpt_create_path(&sp->cam_path, xpt_periph, cam_sim_path(sp->isc->cam_sim),
148                         sp->sid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
149           xdebug("can't create cam path");
150           CAM_UNLOCK(isp);
151           return ENODEV; // XXX
152      }
153      CAM_UNLOCK(isp);
154
155      _scan_target(sp, sp->sid);
156
157      while(sp->flags & ISC_FFPWAIT)
158           tsleep(sp, 0, "ffp", 5*hz); // timeout should be configurable
159      if(sp->target_nluns > 0) {
160           sp->flags |= ISC_FFPHASE;
161           return 0;
162      }
163
164      return ENODEV;
165 }
166
167 static void
168 _inq(struct cam_sim *sim, union ccb *ccb, int maxluns)
169 {
170      struct ccb_pathinq *cpi = &ccb->cpi;
171
172      debug_called(4);
173
174      cpi->version_num = 1; /* XXX??? */
175      cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
176      cpi->target_sprt = 0;
177      cpi->hba_misc = 0;
178      cpi->hba_eng_cnt = 0;
179      cpi->max_target = ISCSI_MAX_TARGETS - 1;
180      cpi->initiator_id = ISCSI_MAX_TARGETS;
181      cpi->max_lun = maxluns;
182      cpi->bus_id = cam_sim_bus(sim);
183      cpi->base_transfer_speed = 3300;
184      strncpy(cpi->sim_vid, "DragonFly", SIM_IDLEN);
185      strncpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
186      strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
187      cpi->unit_number = cam_sim_unit(sim);
188      cpi->ccb_h.status = CAM_REQ_CMP;
189 }
190
191 static __inline int
192 _scsi_encap(struct cam_sim *sim, union ccb *ccb)
193 {
194      int                ret;
195      struct isc_softc   *isp = (struct isc_softc *)cam_sim_softc(sim);
196
197      lockmgr(&isp->cam_lock, LK_EXCLUSIVE);
198      ret = scsi_encap(sim, ccb);
199      lockmgr(&isp->cam_lock, LK_RELEASE);
200      return ret;
201 }
202
203 static void
204 ic_action(struct cam_sim *sim, union ccb *ccb)
205 {
206      struct ccb_hdr     *ccb_h = &ccb->ccb_h;
207      struct isc_softc   *isp = (struct isc_softc *)cam_sim_softc(sim);
208      isc_session_t      *sp;
209
210      debug_called(8);
211
212      if((ccb_h->target_id != CAM_TARGET_WILDCARD) && (ccb_h->target_id < MAX_SESSIONS))
213           sp = isp->sessions[ccb_h->target_id];
214      else
215           sp = NULL;
216
217      ccb_h->spriv_ptr0 = sp;
218
219      debug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%d retry_count=%d timeout=%d",
220            ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
221            ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
222            ccb->ccb_h.retry_count, ccb_h->timeout);
223      /*
224       | first quick check
225       */
226      switch(ccb_h->func_code) {
227      default:
228           // XXX: maybe check something else?
229           break;
230
231      case XPT_SCSI_IO:
232      case XPT_RESET_DEV:
233      case XPT_GET_TRAN_SETTINGS:
234      case XPT_SET_TRAN_SETTINGS:
235      case XPT_CALC_GEOMETRY:
236           if(sp == NULL) {
237                ccb->ccb_h.status = CAM_DEV_NOT_THERE;
238                xpt_done(ccb);
239                return;
240           }
241           break;
242
243      case XPT_PATH_INQ:
244      case XPT_NOOP:
245           if(sp == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
246                ccb->ccb_h.status = CAM_DEV_NOT_THERE;
247                xpt_done(ccb);
248                debug(4, "status = CAM_DEV_NOT_THERE");
249                return;
250           }
251      }
252
253      switch(ccb_h->func_code) {
254
255      case XPT_PATH_INQ:
256           _inq(sim, ccb, (sp? sp->opt.maxluns: ISCSI_MAX_LUNS) - 1);
257           break;
258
259      case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
260      {
261           struct ccb_pathinq *cpi = &ccb->cpi;
262
263           debug(3, "XPT_RESET_BUS");
264           cpi->ccb_h.status = CAM_REQ_CMP;
265           break;
266      }
267
268      case XPT_SCSI_IO:
269      {
270           struct ccb_scsiio* csio = &ccb->csio;
271
272           debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
273           if(sp == NULL) {
274                ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
275                debug(4, "xpt_done.status=%d", ccb_h->status);
276                break;
277           }
278           if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
279                debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
280                ccb_h->status = CAM_LUN_INVALID;
281                break;
282           }
283           if(_scsi_encap(sim, ccb) != 0)
284                return;
285           break;
286      }
287
288      case XPT_CALC_GEOMETRY:
289      {
290           struct        ccb_calc_geometry *ccg;
291
292           ccg = &ccb->ccg;
293           debug(6, "XPT_CALC_GEOMETRY vsize=%jd bsize=%d", ccg->volume_size, ccg->block_size);
294           if(ccg->block_size == 0 ||
295              (ccg->volume_size < ccg->block_size)) {
296                // print error message  ...
297                /* XXX: what error is appropiate? */
298                break;
299           } else
300                cam_calc_geometry(ccg, /*extended*/1);
301           break;
302      }
303
304      case XPT_GET_TRAN_SETTINGS:
305      default:
306           ccb_h->status = CAM_REQ_INVALID;
307           break;
308      }
309      xpt_done(ccb);
310      return;
311 }
312
313 static void
314 ic_poll(struct cam_sim *sim)
315 {
316      debug_called(8);
317
318 }
319
320 int
321 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
322 {
323      int        i;
324
325      debug_called(8);
326
327      if(sp && sp->isc->cam_sim) {
328           cp->path_id = cam_sim_path(sp->isc->cam_sim);
329           cp->target_id = sp->sid;
330           cp->target_nluns = sp->target_nluns; // XXX: -1?
331           for(i = 0; i < cp->target_nluns; i++)
332                cp->target_lun[i] = sp->target_lun[i];
333           return 0;
334      }
335      return ENXIO;
336 }
337
338 void
339 ic_destroy(struct isc_softc *isp)
340 {
341      debug_called(8);
342
343      CAM_LOCK(isp); // can't harm :-)
344
345      xpt_async(AC_LOST_DEVICE, isp->cam_path, NULL);
346      xpt_free_path(isp->cam_path);
347      xpt_bus_deregister(cam_sim_path(isp->cam_sim));
348      cam_sim_free(isp->cam_sim);
349
350      CAM_UNLOCK(isp);
351 }
352
353 int
354 ic_init(struct isc_softc *isp)
355 {
356      struct cam_sim     *sim;
357      struct cam_devq    *devq;
358      struct cam_path    *path;
359
360      if((devq = cam_simq_alloc(256)) == NULL)
361           return ENOMEM;
362
363      lockinit(&isp->cam_lock, "isc-cam", 0, LK_CANRECURSE);
364      sim = cam_sim_alloc(ic_action, ic_poll,
365                          "iscsi", isp, 0, /* unit */
366                          &sim_mplock,
367                          1,             /* max_dev_transactions */
368                          100,           /* max_tagged_dev_transactions */
369                          devq);
370      if(sim == NULL) {
371           cam_simq_release(devq);
372           lockuninit(&isp->cam_lock);
373           return ENXIO;
374      }
375      CAM_LOCK(isp);
376      if(xpt_bus_register(sim,
377                          0/*bus_number*/) != CAM_SUCCESS)
378           goto bad;
379
380      if(xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
381                         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
382           xpt_bus_deregister(cam_sim_path(sim));
383           goto bad;
384      }
385
386      CAM_UNLOCK(isp);
387
388      isp->cam_sim = sim;
389      isp->cam_path = path;
390
391      debug(2, "cam subsystem initialized"); // XXX: add dev ...
392      debug(4, "sim=%p path=%p", sim, path);
393      return 0;
394
395  bad:
396      cam_sim_free(sim);
397      CAM_UNLOCK(isp);
398      return ENXIO;
399 }