TARG(4)

πŸ’– TARG(4) β€” SCSI target emulator driver πŸ’–

okay bestie, sit down, we're learning about targ from the Kernel Interfaces Manual, and honestly? it's giving main character energy. ✨


🌸 NAME 🌸

targ β€” the SCSI target emulator driver. yes that's it, that's the whole name, slay. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

so if you wanna compile this driver into the kernel (no cap, it's a kernel-config situation), you drop this line into your kernel configuration file:

device targ

that's the whole thing. iconic. πŸŽ€


✨ DESCRIPTION ✨

okay so here's the tea: the targ driver gives usermode programs an interface to emulate SCSI target devices. and if you want a whole example of it doing its thing, there's a sample program that emulates a disk drive β€” lowkey similar to da(4) β€” living right here: /usr/share/examples/scsi_target. go look, it's right there. 🫢

the targ driver hands you the control device /dev/targ. so the flow is:

  1. you open the device πŸ”“
  2. the file descriptor has to be bound to a specific bus/target/LUN and enabled to process CCBs β€” that's the TARGIOCENABLE ioctl doing the work
  3. then your process uses write(2) to send CCBs over to the SIM
  4. and poll(2) or kqueue(2) to check if responses are ready πŸ‘€
  5. pointers to completed CCBs come back to you via read(2)
  6. and any data transfers the user CCBs asked for? done via zero-copy IO. periodt. πŸ”₯

πŸ’… IOCTLS πŸ’…

these ioctl(2) calls are all defined in the header file <cam/scsi/scsi_targetio.h>. let's go one by one, bestie:

  • TARGIOCENABLE β€” (struct ioc_enable_lun) β€” this one enables target mode on the LUN you specify. and you specify it with this structure right here:
struct ioc_enable_lun {
        path_id_t       path_id;
        target_id_t     target_id;
        lun_id_t        lun_id;
        int             grp6_len;
        int             grp7_len;
};
🚨 important: the selected path (bus), target, and LUN must not already be in use, or you get EADDRINUSE back. no sharing, babe.

and if grp6_len or grp7_len are non-zero, then reception of vendor-specific commands is enabled. cute little bonus. ✨

  • TARGIOCDISABLE β€” disables target mode and aborts all pending CCBs. the CCBs may optionally be read as they complete, if you want. after that you can call TARGIOCENABLE again to activate a different LUN. multiple disable calls have no effect (it's not gonna get more disabled, bestie). and btw, the close(2) system call automatically disables target mode if it's enabled β€” so you don't even have to ask. πŸ’œ
  • TARGIOCDEBUG β€” (int) β€” enables CAM_PERIPH debugging if the argument is non-zero, otherwise it disables it. that's it. flip the switch. 🎚️

πŸ“ FILES πŸ“

  • <cam/scsi/scsi_targetio.h> β€” describes the usermode interface.
  • /sys/cam/scsi/scsi_target.c β€” is the driver source file.
  • /dev/targ β€” is the control device.

🌈 SEE ALSO 🌈


πŸ‘‘ AUTHORS πŸ‘‘

the targ driver first appeared in FreeBSD 3.0 and was written by Justin T. Gibbs. then it got rewritten for FreeBSD 5.0 by Nate Lawson <nate@root.org>. glow-up era fr fr. πŸ’–


πŸ’€ BUGS πŸ’€

okay so the fine print, and we keep it real here:

  • Currently, only the ahc(4) and isp(4) drivers fully support target mode. so like… keep that in mind. 😭
  • The ahc(4) driver does not support tagged queuing in target mode. not a vibe, but it is the truth.

πŸŽ€ and that's the whole page, bestie β€” this manual came to you from Debian, dated December 13, 2011. literally vintage, and still eating. bye mwah πŸ’…πŸ¦‹βœ¨
πŸ“¦ where this page comes from