@@ -19,6 +19,8 @@ IS_X86_WITH_BLUEFIELD=false
1919IS_ARM_WITH_BLUEFIELD=false
2020IS_X86_WITH_MLX=false
2121CONFIG_ONLY=false
22+ OPT_VFIO_AUTO_BIND=false
23+ VFIO_BIND_ONLY=false
2224
2325function log() {
2426 echo " $( date +" %Y-%m-%d_%H-%M-%S-%3N" ) $1 "
@@ -139,6 +141,46 @@ process_multiport_eswitch_mode() {
139141 udevadm settle
140142}
141143
144+ function vfio_bind() {
145+ local pf0=" ${devs[0]} "
146+
147+ lsmod | grep -q ' ^vfio_pci' || {
148+ log " vfio-pci module not loaded, loading it"
149+ modprobe vfio-pci
150+ }
151+
152+ numvfs=$( cat /sys/bus/pci/devices/$pf0 /sriov_numvfs)
153+ vf_offset=$( cat /sys/bus/pci/devices/$pf0 /sriov_offset)
154+
155+ vf_in_block_count=0
156+ vf_block_count=0
157+ vf_block_hex_index=0
158+
159+ modified_pci=${pf0::- 3}
160+
161+ # pci address of vfs are organized in a block of 8
162+ # e.g., 3b:00.0 ... 3b:00.7, 3b:01.0 ... 3b:01.7, etc.
163+ for i in $( seq $vf_offset $(( vf_offset + numvfs - 1 )) ) ; do
164+ vf_in_block_count=$(( $i % 8 ))
165+ if [ $vf_in_block_count -eq 0 ]; then
166+ vf_block_count=$[$vf_block_count + 1]
167+ vf_block_hex_index=$( printf ' %x' $vf_block_count )
168+ fi
169+ digits=${# vf_block_hex_index}
170+ if [ $digits -eq 2 ]; then
171+ modified_pci=${pf0::- 4}
172+ fi
173+
174+ vf_pci_addr=$modified_pci$vf_block_hex_index " ." $vf_in_block_count
175+
176+ echo ' vfio-pci' > /sys/bus/pci/devices/$vf_pci_addr /driver_override
177+ echo $vf_pci_addr > /sys/bus/pci/drivers/vfio-pci/bind
178+ log " VFs bound to vfio-pci driver"
179+ done
180+
181+ sleep 1
182+ }
183+
142184function create_vf() {
143185 local pf0=" ${devs[0]} "
144186 local pf1=" ${devs[1]} "
@@ -161,6 +203,13 @@ function create_vf() {
161203 return
162204 fi
163205
206+ if [[ " $VFIO_BIND_ONLY " == " true" ]]; then
207+ log " Skipping VF creation as requested, binding VFs to vfio-pci driver"
208+ vfio_bind
209+ actualvfs=$( cat /sys/bus/pci/devices/$pf0 /sriov_numvfs)
210+ return
211+ fi
212+
164213 # we disable automatic binding so that VFs don't get created, saves a lot of time
165214 # plus we don't need to unbind them before enabling switchdev mode
166215 log " disabling automatic binding of VFs on pf0 '$pf0 '"
@@ -188,8 +237,16 @@ function create_vf() {
188237 actualvfs=$(( NUMVFS_DESIRED< totalvfs ? NUMVFS_DESIRED : totalvfs))
189238 log " creating $actualvfs virtual functions"
190239 echo $actualvfs > /sys/bus/pci/devices/$pf0 /sriov_numvfs
240+
241+ if [[ " $OPT_VFIO_AUTO_BIND " == " true" ]]; then
242+ log " Binding VFs to vfio-pci driver"
243+ vfio_bind
244+ else
245+ log " Skipping VF binding to vfio-pci driver"
246+ fi
191247}
192248
249+
193250function get_pattern() {
194251 local dev=$1
195252 pattern=$( devlink port | grep pci/$dev / | grep " virtual\|pcivf" | awk ' {print $5}' | sed -rn ' s/(.*[a-z_])[0-9]{1,3}$/\1/p' | uniq)
258315
259316while [[ $# -gt 0 ]]; do
260317 case $1 in
318+ --vfio-auto-bind)
319+ OPT_VFIO_AUTO_BIND=true
320+ ;;
261321 --multiport-eswitch)
262322 OPT_MULTIPORT=true
263323 ;;
@@ -267,6 +327,9 @@ while [[ $# -gt 0 ]]; do
267327 --config-only)
268328 CONFIG_ONLY=true
269329 ;;
330+ --vfio-bind-only)
331+ VFIO_BIND_ONLY=true
332+ ;;
270333 * )
271334 err " Invalid argument $1 "
272335 esac
0 commit comments