]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/iscsi/initiator/isc_cam.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / iscsi / initiator / isc_cam.c
1 /*-
2  * Copyright (c) 2005-2007 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  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_iscsi_initiator.h"
32
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/callout.h>
36 #if __FreeBSD_version >= 700000
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #endif
40 #include <sys/conf.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/uio.h>
45 #include <sys/sysctl.h>
46
47 #include <cam/cam.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_sim.h>
50 #include <cam/cam_xpt_sim.h>
51 #include <cam/cam_periph.h>
52
53 #include <dev/iscsi/initiator/iscsi.h>
54 #include <dev/iscsi/initiator/iscsivar.h>
55
56 // XXX: untested/incomplete
57 void
58 ic_freeze(isc_session_t *sp)
59 {
60      debug_called(8);
61 #if 0
62      sdebug(2, "freezing path=%p", sp->cam_path == NULL? 0: sp->cam_path);
63      if((sp->cam_path != NULL) && !(sp->flags & ISC_FROZEN)) {
64           xpt_freeze_devq(sp->cam_path, 1);
65      }
66 #endif
67      sp->flags |= ISC_FROZEN;
68 }
69
70 // XXX: untested/incomplete
71 void
72 ic_release(isc_session_t *sp)
73 {
74      debug_called(8);
75 #if 0
76      sdebug(2, "release path=%p", sp->cam_path == NULL? 0: sp->cam_path);
77      if((sp->cam_path != NULL) && (sp->flags & ISC_FROZEN)) {
78           xpt_release_devq(sp->cam_path, 1, TRUE);
79      }
80 #endif
81      sp->flags &= ~ISC_FROZEN;
82 }
83
84 void
85 ic_lost_target(isc_session_t *sp, int target)
86 {
87      struct isc_softc   *isp = sp->isc;
88
89      debug_called(8);
90      sdebug(2, "target=%d", target);
91      if(sp->cam_path != NULL) {
92           mtx_lock(&isp->cam_mtx);
93           xpt_async(AC_LOST_DEVICE, sp->cam_path, NULL);
94           xpt_free_path(sp->cam_path);
95           mtx_unlock(&isp->cam_mtx);
96           sp->cam_path = 0; // XXX
97      }
98 }
99
100 static void
101 _scan_callback(struct cam_periph *periph, union ccb *ccb)
102 {
103      isc_session_t *sp = (isc_session_t *)ccb->ccb_h.spriv_ptr0;
104
105      debug_called(8);
106
107      free(ccb, M_TEMP);
108
109      if(sp->flags & ISC_FFPWAIT) {
110           sp->flags &= ~ISC_FFPWAIT;
111           wakeup(sp);
112      }
113 }
114
115 static void
116 _scan_target(isc_session_t *sp, int target)
117 {
118      union ccb          *ccb;
119
120      debug_called(8);
121      sdebug(2, "target=%d", target);
122
123      if((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
124           xdebug("scan failed (can't allocate CCB)");
125           return;
126      }
127      CAM_LOCK(sp->isc);
128      xpt_setup_ccb(&ccb->ccb_h, sp->cam_path, 5/*priority (low)*/);
129      ccb->ccb_h.func_code       = XPT_SCAN_BUS;
130      ccb->ccb_h.cbfcnp          = _scan_callback;
131      ccb->crcn.flags            = CAM_FLAG_NONE;
132      ccb->ccb_h.spriv_ptr0      = sp;
133
134      xpt_action(ccb);
135      CAM_UNLOCK(sp->isc);
136 }
137
138 int
139 ic_fullfeature(struct cdev *dev)
140 {
141      struct isc_softc   *isp = dev->si_drv1;
142      isc_session_t      *sp = (isc_session_t *)dev->si_drv2;
143
144      debug_called(8);
145      sdebug(3, "dev=%d sc=%p", minor(dev), isp);
146
147      sp->flags &= ~ISC_FFPHASE;
148      sp->flags |= ISC_FFPWAIT;
149
150      CAM_LOCK(isp);
151      if(xpt_create_path(&sp->cam_path, xpt_periph, cam_sim_path(sp->isc->cam_sim),
152                         sp->sid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
153           xdebug("can't create cam path");
154           CAM_UNLOCK(isp);
155           return ENODEV; // XXX
156      }
157      CAM_UNLOCK(isp);
158
159      _scan_target(sp, sp->sid);
160
161      while(sp->flags & ISC_FFPWAIT)
162           tsleep(sp, PRIBIO, "ffp", 5*hz); // the timeout time should
163                                             // be configurable
164      if(sp->target_nluns > 0) {
165           sp->flags |= ISC_FFPHASE;
166           return 0;
167      }
168
169      return ENODEV;
170 }
171
172 static void
173 _inq(struct cam_sim *sim, union ccb *ccb, int maxluns)
174 {
175      struct ccb_pathinq *cpi = &ccb->cpi;
176
177      debug_called(4);
178
179      cpi->version_num = 1; /* XXX??? */
180      cpi->hba_inquiry = PI_SDTR_ABLE | PI_TAG_ABLE | PI_WIDE_32;
181      cpi->target_sprt = 0;
182      cpi->hba_misc = 0;
183      cpi->hba_eng_cnt = 0;
184      cpi->max_target = ISCSI_MAX_TARGETS - 1;
185      cpi->initiator_id = ISCSI_MAX_TARGETS;
186      cpi->max_lun = maxluns;
187      cpi->bus_id = cam_sim_bus(sim);
188      cpi->base_transfer_speed = 3300;
189      strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
190      strncpy(cpi->hba_vid, "iSCSI", HBA_IDLEN);
191      strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
192      cpi->unit_number = cam_sim_unit(sim);
193      cpi->ccb_h.status = CAM_REQ_CMP;
194 }
195
196 static void
197 ic_action(struct cam_sim *sim, union ccb *ccb)
198 {
199      struct ccb_hdr     *ccb_h = &ccb->ccb_h;
200      struct isc_softc   *isp = (struct isc_softc *)cam_sim_softc(sim);
201      isc_session_t      *sp;
202
203      debug_called(8);
204
205      if((ccb_h->target_id != CAM_TARGET_WILDCARD) && (ccb_h->target_id < MAX_SESSIONS))
206           sp = isp->sessions[ccb_h->target_id];
207      else
208           sp = NULL;
209
210      ccb_h->spriv_ptr0 = sp;
211
212      debug(4, "func_code=0x%x flags=0x%x status=0x%x target=%d lun=%d retry_count=%d timeout=%d",
213            ccb_h->func_code, ccb->ccb_h.flags, ccb->ccb_h.status,
214            ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 
215            ccb->ccb_h.retry_count, ccb_h->timeout);
216      /*
217       | first quick check
218       */
219      switch(ccb_h->func_code) {
220      default:
221           // XXX: maybe check something else?
222           break;
223
224      case XPT_SCSI_IO:
225      case XPT_RESET_DEV:
226      case XPT_GET_TRAN_SETTINGS:
227      case XPT_SET_TRAN_SETTINGS:
228      case XPT_CALC_GEOMETRY:
229           if(sp == NULL) {
230                ccb->ccb_h.status = CAM_DEV_NOT_THERE;
231 #if __FreeBSD_version < 700000
232                XPT_DONE(isp, ccb);
233 #else
234                xpt_done(ccb);
235 #endif
236                return;
237           }
238           break;
239
240      case XPT_PATH_INQ:
241      case XPT_NOOP:
242           if(sp == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
243                ccb->ccb_h.status = CAM_DEV_NOT_THERE;
244 #if __FreeBSD_version < 700000
245                XPT_DONE(isp, ccb);
246 #else
247                xpt_done(ccb);
248 #endif
249                debug(4, "status = CAM_DEV_NOT_THERE");
250                return;
251           }
252      }
253
254      switch(ccb_h->func_code) {
255
256      case XPT_PATH_INQ:
257           _inq(sim, ccb, (sp? sp->opt.maxluns: ISCSI_MAX_LUNS) - 1);
258           break;
259
260      case XPT_RESET_BUS: // (can just be a stub that does nothing and completes)
261      {
262           struct ccb_pathinq *cpi = &ccb->cpi;
263
264           debug(3, "XPT_RESET_BUS");
265           cpi->ccb_h.status = CAM_REQ_CMP;
266           break;
267      }
268
269      case XPT_SCSI_IO: 
270      {
271           struct ccb_scsiio* csio = &ccb->csio;
272
273           debug(4, "XPT_SCSI_IO cmd=0x%x", csio->cdb_io.cdb_bytes[0]);
274           if(sp == NULL) {
275                ccb_h->status = CAM_REQ_INVALID; //CAM_NO_NEXUS;
276                debug(4, "xpt_done.status=%d", ccb_h->status);
277                break;
278           }
279           if(ccb_h->target_lun == CAM_LUN_WILDCARD) {
280                debug(3, "target=%d: bad lun (-1)", ccb_h->target_id);
281                ccb_h->status = CAM_LUN_INVALID;
282                break;
283           }
284 #if __FreeBSD_version < 700000
285           if(scsi_encap(sim, ccb) != 0)
286                return;
287 #else
288           mtx_unlock(&isp->cam_mtx);
289           if(scsi_encap(sim, ccb) != 0) {
290                mtx_lock(&isp->cam_mtx);
291                return;
292           }
293           mtx_lock(&isp->cam_mtx);
294 #endif
295           break;
296      }
297  
298      case XPT_CALC_GEOMETRY:
299      {
300           struct        ccb_calc_geometry *ccg;
301
302           ccg = &ccb->ccg;
303           debug(6, "XPT_CALC_GEOMETRY vsize=%jd bsize=%d", ccg->volume_size, ccg->block_size);
304           if(ccg->block_size == 0 ||
305              (ccg->volume_size < ccg->block_size)) {
306                // print error message  ...
307                /* XXX: what error is appropiate? */
308                break;
309           } else
310                cam_calc_geometry(ccg, /*extended*/1);
311           break;
312      }
313
314      case XPT_GET_TRAN_SETTINGS:
315      default:
316           ccb_h->status = CAM_REQ_INVALID;
317           break;
318      }
319 #if __FreeBSD_version < 700000
320      XPT_DONE(isp, ccb);
321 #else
322      xpt_done(ccb);
323 #endif
324      return;
325 }
326
327 static void
328 ic_poll(struct cam_sim *sim)
329 {
330      debug_called(8);
331
332 }
333
334 int
335 ic_getCamVals(isc_session_t *sp, iscsi_cam_t *cp)
336 {
337      int        i;
338
339      debug_called(8);
340
341      if(sp && sp->isc->cam_sim) {
342           cp->path_id = cam_sim_path(sp->isc->cam_sim);
343           cp->target_id = sp->sid;
344           cp->target_nluns = sp->target_nluns; // XXX: -1?
345           for(i = 0; i < cp->target_nluns; i++)
346                cp->target_lun[i] = sp->target_lun[i];
347           return 0;
348      }
349      return ENXIO;
350 }
351
352 void
353 ic_destroy(struct isc_softc *isp)
354 {
355      debug_called(8);
356
357      CAM_LOCK(isp); // can't harm :-)
358
359      xpt_async(AC_LOST_DEVICE, isp->cam_path, NULL);
360      xpt_free_path(isp->cam_path);
361      
362      xpt_bus_deregister(cam_sim_path(isp->cam_sim));
363      cam_sim_free(isp->cam_sim, TRUE /*free_devq*/);
364
365      CAM_UNLOCK(isp);
366 }
367
368 int
369 ic_init(struct isc_softc *isp)
370 {
371      struct cam_sim     *sim;
372      struct cam_devq    *devq;
373      struct cam_path    *path;
374
375      if((devq = cam_simq_alloc(256)) == NULL)
376           return ENOMEM;
377
378 #if __FreeBSD_version >= 700000
379      mtx_init(&isp->cam_mtx, "isc-cam", NULL, MTX_DEF);
380 #else
381      isp->cam_mtx = Giant;
382 #endif
383      sim = cam_sim_alloc(ic_action, ic_poll,
384                          "iscsi", isp, 0/*unit*/,
385 #if __FreeBSD_version >= 700000
386                          &isp->cam_mtx,
387 #endif
388                          1/*max_dev_transactions*/,
389                          100/*max_tagged_dev_transactions*/,
390                          devq);
391      if(sim == NULL) {
392           cam_simq_free(devq);
393 #if __FreeBSD_version >= 700000
394           mtx_destroy(&isp->cam_mtx);
395 #endif
396           return ENXIO;
397      }
398      CAM_LOCK(isp);
399      if(xpt_bus_register(sim, NULL, 0/*bus_number*/) != CAM_SUCCESS)
400           goto bad;
401
402      if(xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
403                         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
404           xpt_bus_deregister(cam_sim_path(sim));
405           goto bad;
406      }
407
408      CAM_UNLOCK(isp);
409
410      isp->cam_sim = sim;
411      isp->cam_path = path;
412
413      debug(2, "cam subsystem initialized"); // XXX: add dev ...
414      debug(4, "sim=%p path=%p", sim, path);
415      return 0;
416
417  bad:
418      cam_sim_free(sim, /*free_devq*/TRUE);
419      CAM_UNLOCK(isp);
420 #if __FreeBSD_version >= 700000
421      mtx_destroy(&isp->cam_mtx);
422 #endif
423      return ENXIO;
424 }