@@ -62,14 +62,16 @@ static void zpci_bus_release(struct kref *kref)
6262{
6363 struct zpci_bus * zbus = container_of (kref , struct zpci_bus , kref );
6464
65- pci_lock_rescan_remove ();
66- pci_stop_root_bus (zbus -> bus );
65+ if (zbus -> bus ) {
66+ pci_lock_rescan_remove ();
67+ pci_stop_root_bus (zbus -> bus );
6768
68- zpci_free_domain (zbus -> domain_nr );
69- pci_free_resource_list (& zbus -> resources );
69+ zpci_free_domain (zbus -> domain_nr );
70+ pci_free_resource_list (& zbus -> resources );
7071
71- pci_remove_root_bus (zbus -> bus );
72- pci_unlock_rescan_remove ();
72+ pci_remove_root_bus (zbus -> bus );
73+ pci_unlock_rescan_remove ();
74+ }
7375
7476 spin_lock (& zbus_list_lock );
7577 list_del (& zbus -> bus_next );
@@ -82,6 +84,23 @@ static void zpci_bus_put(struct zpci_bus *zbus)
8284 kref_put (& zbus -> kref , zpci_bus_release );
8385}
8486
87+ static struct zpci_bus * zpci_bus_get (int pchid )
88+ {
89+ struct zpci_bus * zbus ;
90+
91+ spin_lock (& zbus_list_lock );
92+ list_for_each_entry (zbus , & zbus_list , bus_next ) {
93+ if (pchid == zbus -> pchid ) {
94+ kref_get (& zbus -> kref );
95+ goto out_unlock ;
96+ }
97+ }
98+ zbus = NULL ;
99+ out_unlock :
100+ spin_unlock (& zbus_list_lock );
101+ return zbus ;
102+ }
103+
85104static struct zpci_bus * zpci_bus_alloc (int pchid )
86105{
87106 struct zpci_bus * zbus ;
@@ -107,37 +126,128 @@ static struct zpci_bus *zpci_bus_alloc(int pchid)
107126 return zbus ;
108127}
109128
110- int zpci_bus_device_register (struct zpci_dev * zdev , struct pci_ops * ops )
129+ static int zpci_bus_add_device (struct zpci_bus * zbus , struct zpci_dev * zdev )
111130{
112- struct zpci_bus * zbus ;
131+ struct pci_bus * bus ;
132+ struct resource_entry * window , * n ;
133+ struct resource * res ;
134+ struct pci_dev * pdev ;
113135 int rc ;
114136
137+ bus = zbus -> bus ;
138+ if (!bus )
139+ return - EINVAL ;
140+
141+ pdev = pci_get_slot (bus , zdev -> devfn );
142+ if (pdev ) {
143+ /* Device is already known. */
144+ pci_dev_put (pdev );
145+ return 0 ;
146+ }
147+
148+ rc = zpci_init_slot (zdev );
149+ if (rc )
150+ return rc ;
151+ zdev -> has_hp_slot = 1 ;
152+
153+ resource_list_for_each_entry_safe (window , n , & zbus -> resources ) {
154+ res = window -> res ;
155+ pci_bus_add_resource (bus , res , 0 );
156+ }
157+
158+ pdev = pci_scan_single_device (bus , zdev -> devfn );
159+ if (pdev ) {
160+ pdev -> multifunction = 1 ;
161+ pci_bus_add_device (pdev );
162+ }
163+
164+ return 0 ;
165+ }
166+
167+ static void zpci_bus_add_devices (struct zpci_bus * zbus )
168+ {
169+ int i ;
170+
171+ for (i = 1 ; i < ZPCI_FUNCTIONS_PER_BUS ; i ++ )
172+ if (zbus -> function [i ])
173+ zpci_bus_add_device (zbus , zbus -> function [i ]);
174+
175+ pci_lock_rescan_remove ();
176+ pci_bus_add_devices (zbus -> bus );
177+ pci_unlock_rescan_remove ();
178+ }
179+
180+ int zpci_bus_device_register (struct zpci_dev * zdev , struct pci_ops * ops )
181+ {
182+ struct zpci_bus * zbus = NULL ;
183+ int rc = - EBADF ;
184+
115185 if (zpci_nb_devices == ZPCI_NR_DEVICES ) {
116186 pr_warn ("Adding PCI function %08x failed because the configured limit of %d is reached\n" ,
117187 zdev -> fid , ZPCI_NR_DEVICES );
118188 return - ENOSPC ;
119189 }
120190 zpci_nb_devices ++ ;
121191
122- if (zdev -> devfn != ZPCI_DEVFN )
192+ if (zdev -> devfn >= ZPCI_FUNCTIONS_PER_BUS )
123193 return - EINVAL ;
124194
125- zbus = zpci_bus_alloc (zdev -> pchid );
126- if (!zbus )
127- return - ENOMEM ;
195+ if (!s390_pci_no_rid && zdev -> rid_available )
196+ zbus = zpci_bus_get (zdev -> pchid );
197+
198+ if (!zbus ) {
199+ zbus = zpci_bus_alloc (zdev -> pchid );
200+ if (!zbus )
201+ return - ENOMEM ;
202+ }
128203
129204 zdev -> zbus = zbus ;
130- zbus -> function [ZPCI_DEVFN ] = zdev ;
205+ if (zbus -> function [zdev -> devfn ]) {
206+ pr_err ("devfn %04x is already assigned\n" , zdev -> devfn );
207+ goto error ; /* rc already set */
208+ }
209+ zbus -> function [zdev -> devfn ] = zdev ;
131210
132211 zpci_setup_bus_resources (zdev , & zbus -> resources );
133- zbus -> max_bus_speed = zdev -> max_bus_speed ;
134212
135- rc = zpci_bus_scan (zbus , (u16 )zdev -> uid , ops );
136- if (!rc )
137- return 0 ;
213+ if (zbus -> bus ) {
214+ if (!zbus -> multifunction ) {
215+ WARN_ONCE (1 , "zbus is not multifunction\n" );
216+ goto error_bus ;
217+ }
218+ if (!zdev -> rid_available ) {
219+ WARN_ONCE (1 , "rid_available not set for multifunction\n" );
220+ goto error_bus ;
221+ }
222+ rc = zpci_bus_add_device (zbus , zdev );
223+ if (rc )
224+ goto error_bus ;
225+ } else if (zdev -> devfn == 0 ) {
226+ if (zbus -> multifunction && !zdev -> rid_available ) {
227+ WARN_ONCE (1 , "rid_available not set on function 0 for multifunction\n" );
228+ goto error_bus ;
229+ }
230+ rc = zpci_bus_scan (zbus , (u16 )zdev -> uid , ops );
231+ if (rc )
232+ goto error_bus ;
233+ zpci_bus_add_devices (zbus );
234+ rc = zpci_init_slot (zdev );
235+ if (rc )
236+ goto error_bus ;
237+ zdev -> has_hp_slot = 1 ;
238+ zbus -> multifunction = zdev -> rid_available ;
239+ zbus -> max_bus_speed = zdev -> max_bus_speed ;
240+ } else {
241+ zbus -> multifunction = 1 ;
242+ }
138243
244+ return 0 ;
245+
246+ error_bus :
247+ zpci_nb_devices -- ;
248+ zbus -> function [zdev -> devfn ] = NULL ;
249+ error :
139250 pr_err ("Adding PCI function %08x failed\n" , zdev -> fid );
140- zdev -> zbus = NULL ;
141251 zpci_bus_put (zbus );
142252 return rc ;
143253}
@@ -147,6 +257,6 @@ void zpci_bus_device_unregister(struct zpci_dev *zdev)
147257 struct zpci_bus * zbus = zdev -> zbus ;
148258
149259 zpci_nb_devices -- ;
150- zbus -> function [ZPCI_DEVFN ] = NULL ;
260+ zbus -> function [zdev -> devfn ] = NULL ;
151261 zpci_bus_put (zbus );
152262}
0 commit comments