1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/blkdev.h>
4 #include <linux/init.h>
5 #include <linux/kernel.h>
6 #include <linux/module.h>
7 #include <linux/moduleparam.h>
8 #include <linux/proc_fs.h>
9 #include <linux/seq_file.h>
10 #include <linux/slab.h>
11 
12 #include <scsi/scsi_device.h>
13 #include <scsi/scsi_devinfo.h>
14 
15 #include "scsi_priv.h"
16 
17 
18 /*
19  * scsi_dev_info_list: structure to hold black/white listed devices.
20  */
21 struct scsi_dev_info_list {
22 	struct list_head dev_info_list;
23 	char vendor[8];
24 	char model[16];
25 	blist_flags_t flags;
26 	unsigned compatible; /* for use with scsi_static_device_list entries */
27 };
28 
29 struct scsi_dev_info_list_table {
30 	struct list_head node;	/* our node for being on the master list */
31 	struct list_head scsi_dev_info_list; /* head of dev info list */
32 	const char *name;	/* name of list for /proc (NULL for global) */
33 	int key;		/* unique numeric identifier */
34 };
35 
36 
37 static blist_flags_t scsi_default_dev_flags;
38 static LIST_HEAD(scsi_dev_info_list);
39 static char scsi_dev_flags[256];
40 
41 /*
42  * scsi_static_device_list: deprecated list of devices that require
43  * settings that differ from the default, includes black-listed (broken)
44  * devices. The entries here are added to the tail of scsi_dev_info_list
45  * via scsi_dev_info_list_init.
46  *
47  * Do not add to this list, use the command line or proc interface to add
48  * to the scsi_dev_info_list. This table will eventually go away.
49  */
50 static struct {
51 	char *vendor;
52 	char *model;
53 	char *revision;	/* revision known to be bad, unused */
54 	blist_flags_t flags;
55 } scsi_static_device_list[] __initdata = {
56 	/*
57 	 * The following devices are known not to tolerate a lun != 0 scan
58 	 * for one reason or another. Some will respond to all luns,
59 	 * others will lock up.
60 	 */
61 	{"Aashima", "IMAGERY 2400SP", "1.03", BLIST_NOLUN},	/* locks up */
62 	{"CHINON", "CD-ROM CDS-431", "H42", BLIST_NOLUN},	/* locks up */
63 	{"CHINON", "CD-ROM CDS-535", "Q14", BLIST_NOLUN},	/* locks up */
64 	{"DENON", "DRD-25X", "V", BLIST_NOLUN},			/* locks up */
65 	{"HITACHI", "DK312C", "CM81", BLIST_NOLUN},	/* responds to all lun */
66 	{"HITACHI", "DK314C", "CR21", BLIST_NOLUN},	/* responds to all lun */
67 	{"IBM", "2104-DU3", NULL, BLIST_NOLUN},		/* locks up */
68 	{"IBM", "2104-TU3", NULL, BLIST_NOLUN},		/* locks up */
69 	{"IMS", "CDD521/10", "2.06", BLIST_NOLUN},	/* locks up */
70 	{"MAXTOR", "XT-3280", "PR02", BLIST_NOLUN},	/* locks up */
71 	{"MAXTOR", "XT-4380S", "B3C", BLIST_NOLUN},	/* locks up */
72 	{"MAXTOR", "MXT-1240S", "I1.2", BLIST_NOLUN},	/* locks up */
73 	{"MAXTOR", "XT-4170S", "B5A", BLIST_NOLUN},	/* locks up */
74 	{"MAXTOR", "XT-8760S", "B7B", BLIST_NOLUN},	/* locks up */
75 	{"MEDIAVIS", "RENO CD-ROMX2A", "2.03", BLIST_NOLUN},	/* responds to all lun */
76 	{"MICROTEK", "ScanMakerIII", "2.30", BLIST_NOLUN},	/* responds to all lun */
77 	{"NEC", "CD-ROM DRIVE:841", "1.0", BLIST_NOLUN},/* locks up */
78 	{"PHILIPS", "PCA80SC", "V4-2", BLIST_NOLUN},	/* responds to all lun */
79 	{"RODIME", "RO3000S", "2.33", BLIST_NOLUN},	/* locks up */
80 	{"SUN", "SENA", NULL, BLIST_NOLUN},		/* responds to all luns */
81 	/*
82 	 * The following causes a failed REQUEST SENSE on lun 1 for
83 	 * aha152x controller, which causes SCSI code to reset bus.
84 	 */
85 	{"SANYO", "CRD-250S", "1.20", BLIST_NOLUN},
86 	/*
87 	 * The following causes a failed REQUEST SENSE on lun 1 for
88 	 * aha152x controller, which causes SCSI code to reset bus.
89 	 */
90 	{"SEAGATE", "ST157N", "\004|j", BLIST_NOLUN},
91 	{"SEAGATE", "ST296", "921", BLIST_NOLUN},	/* responds to all lun */
92 	{"SEAGATE", "ST1581", "6538", BLIST_NOLUN},	/* responds to all lun */
93 	{"SONY", "CD-ROM CDU-541", "4.3d", BLIST_NOLUN},
94 	{"SONY", "CD-ROM CDU-55S", "1.0i", BLIST_NOLUN},
95 	{"SONY", "CD-ROM CDU-561", "1.7x", BLIST_NOLUN},
96 	{"SONY", "CD-ROM CDU-8012", NULL, BLIST_NOLUN},
97 	{"SONY", "SDT-5000", "3.17", BLIST_SELECT_NO_ATN},
98 	{"TANDBERG", "TDC 3600", "U07", BLIST_NOLUN},	/* locks up */
99 	{"TEAC", "CD-R55S", "1.0H", BLIST_NOLUN},	/* locks up */
100 	/*
101 	 * The following causes a failed REQUEST SENSE on lun 1 for
102 	 * seagate controller, which causes SCSI code to reset bus.
103 	 */
104 	{"TEAC", "CD-ROM", "1.06", BLIST_NOLUN},
105 	{"TEAC", "MT-2ST/45S2-27", "RV M", BLIST_NOLUN},	/* responds to all lun */
106 	/*
107 	 * The following causes a failed REQUEST SENSE on lun 1 for
108 	 * seagate controller, which causes SCSI code to reset bus.
109 	 */
110 	{"HP", "C1750A", "3226", BLIST_NOLUN},		/* scanjet iic */
111 	{"HP", "C1790A", NULL, BLIST_NOLUN},		/* scanjet iip */
112 	{"HP", "C2500A", NULL, BLIST_NOLUN},		/* scanjet iicx */
113 	{"MEDIAVIS", "CDR-H93MV", "1.31", BLIST_NOLUN},	/* locks up */
114 	{"MICROTEK", "ScanMaker II", "5.61", BLIST_NOLUN},	/* responds to all lun */
115 	{"MITSUMI", "CD-R CR-2201CS", "6119", BLIST_NOLUN},	/* locks up */
116 	{"NEC", "D3856", "0009", BLIST_NOLUN},
117 	{"QUANTUM", "LPS525S", "3110", BLIST_NOLUN},	/* locks up */
118 	{"QUANTUM", "PD1225S", "3110", BLIST_NOLUN},	/* locks up */
119 	{"QUANTUM", "FIREBALL ST4.3S", "0F0C", BLIST_NOLUN},	/* locks up */
120 	{"RELISYS", "Scorpio", NULL, BLIST_NOLUN},	/* responds to all lun */
121 	{"SANKYO", "CP525", "6.64", BLIST_NOLUN},	/* causes failed REQ SENSE, extra reset */
122 	{"TEXEL", "CD-ROM", "1.06", BLIST_NOLUN | BLIST_BORKEN},
123 	{"transtec", "T5008", "0001", BLIST_NOREPORTLUN },
124 	{"YAMAHA", "CDR100", "1.00", BLIST_NOLUN},	/* locks up */
125 	{"YAMAHA", "CDR102", "1.00", BLIST_NOLUN},	/* locks up */
126 	{"YAMAHA", "CRW8424S", "1.0", BLIST_NOLUN},	/* locks up */
127 	{"YAMAHA", "CRW6416S", "1.0c", BLIST_NOLUN},	/* locks up */
128 	{"", "Scanner", "1.80", BLIST_NOLUN},	/* responds to all lun */
129 
130 	/*
131 	 * Other types of devices that have special flags.
132 	 * Note that all USB devices should have the BLIST_INQUIRY_36 flag.
133 	 */
134 	{"3PARdata", "VV", NULL, BLIST_REPORTLUN2},
135 	{"ADAPTEC", "AACRAID", NULL, BLIST_FORCELUN},
136 	{"ADAPTEC", "Adaptec 5400S", NULL, BLIST_FORCELUN},
137 	{"AIX", "VDASD", NULL, BLIST_TRY_VPD_PAGES},
138 	{"AFT PRO", "-IX CF", "0.0>", BLIST_FORCELUN},
139 	{"BELKIN", "USB 2 HS-CF", "1.95",  BLIST_FORCELUN | BLIST_INQUIRY_36},
140 	{"BROWNIE", "1200U3P", NULL, BLIST_NOREPORTLUN},
141 	{"BROWNIE", "1600U3P", NULL, BLIST_NOREPORTLUN},
142 	{"CANON", "IPUBJD", NULL, BLIST_SPARSELUN},
143 	{"CBOX3", "USB Storage-SMC", "300A", BLIST_FORCELUN | BLIST_INQUIRY_36},
144 	{"CMD", "CRA-7280", NULL, BLIST_SPARSELUN},	/* CMD RAID Controller */
145 	{"CNSI", "G7324", NULL, BLIST_SPARSELUN},	/* Chaparral G7324 RAID */
146 	{"CNSi", "G8324", NULL, BLIST_SPARSELUN},	/* Chaparral G8324 RAID */
147 	{"COMPAQ", "ARRAY CONTROLLER", NULL, BLIST_SPARSELUN | BLIST_LARGELUN |
148 		BLIST_MAX_512 | BLIST_REPORTLUN2},	/* Compaq RA4x00 */
149 	{"COMPAQ", "LOGICAL VOLUME", NULL, BLIST_FORCELUN | BLIST_MAX_512}, /* Compaq RA4x00 */
150 	{"COMPAQ", "CR3500", NULL, BLIST_FORCELUN},
151 	{"COMPAQ", "MSA1000", NULL, BLIST_SPARSELUN | BLIST_NOSTARTONADD},
152 	{"COMPAQ", "MSA1000 VOLUME", NULL, BLIST_SPARSELUN | BLIST_NOSTARTONADD},
153 	{"COMPAQ", "HSV110", NULL, BLIST_REPORTLUN2 | BLIST_NOSTARTONADD},
154 	{"DDN", "SAN DataDirector", "*", BLIST_SPARSELUN},
155 	{"DEC", "HSG80", NULL, BLIST_REPORTLUN2 | BLIST_NOSTARTONADD},
156 	{"DELL", "PV660F", NULL, BLIST_SPARSELUN},
157 	{"DELL", "PV660F   PSEUDO", NULL, BLIST_SPARSELUN},
158 	{"DELL", "PSEUDO DEVICE .", NULL, BLIST_SPARSELUN},	/* Dell PV 530F */
159 	{"DELL", "PV530F", NULL, BLIST_SPARSELUN},
160 	{"DELL", "PERCRAID", NULL, BLIST_FORCELUN},
161 	{"DGC", "RAID", NULL, BLIST_SPARSELUN},	/* EMC CLARiiON, storage on LUN 0 */
162 	{"DGC", "DISK", NULL, BLIST_SPARSELUN},	/* EMC CLARiiON, no storage on LUN 0 */
163 	{"EMC",  "Invista", "*", BLIST_SPARSELUN | BLIST_LARGELUN},
164 	{"EMC", "SYMMETRIX", NULL, BLIST_SPARSELUN | BLIST_LARGELUN |
165 	 BLIST_REPORTLUN2 | BLIST_RETRY_ITF},
166 	{"EMULEX", "MD21/S2     ESDI", NULL, BLIST_SINGLELUN},
167 	{"easyRAID", "16P", NULL, BLIST_NOREPORTLUN},
168 	{"easyRAID", "X6P", NULL, BLIST_NOREPORTLUN},
169 	{"easyRAID", "F8", NULL, BLIST_NOREPORTLUN},
170 	{"FSC", "CentricStor", "*", BLIST_SPARSELUN | BLIST_LARGELUN},
171 	{"FUJITSU", "ETERNUS_DXM", "*", BLIST_RETRY_ASC_C1},
172 	{"Generic", "USB SD Reader", "1.00", BLIST_FORCELUN | BLIST_INQUIRY_36},
173 	{"Generic", "USB Storage-SMC", NULL, BLIST_FORCELUN | BLIST_INQUIRY_36}, /* FW: 0180 and 0207 */
174 	{"HITACHI", "DF400", "*", BLIST_REPORTLUN2},
175 	{"HITACHI", "DF500", "*", BLIST_REPORTLUN2},
176 	{"HITACHI", "DISK-SUBSYSTEM", "*", BLIST_REPORTLUN2},
177 	{"HITACHI", "HUS1530", "*", BLIST_NO_DIF},
178 	{"HITACHI", "OPEN-", "*", BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES},
179 	{"HP", "A6189A", NULL, BLIST_SPARSELUN | BLIST_LARGELUN},	/* HP VA7400 */
180 	{"HP", "OPEN-", "*", BLIST_REPORTLUN2 | BLIST_TRY_VPD_PAGES}, /* HP XP Arrays */
181 	{"HP", "NetRAID-4M", NULL, BLIST_FORCELUN},
182 	{"HP", "HSV100", NULL, BLIST_REPORTLUN2 | BLIST_NOSTARTONADD},
183 	{"HP", "C1557A", NULL, BLIST_FORCELUN},
184 	{"HP", "C3323-300", "4269", BLIST_NOTQ},
185 	{"HP", "C5713A", NULL, BLIST_NOREPORTLUN},
186 	{"HP", "DISK-SUBSYSTEM", "*", BLIST_REPORTLUN2},
187 	{"IBM", "AuSaV1S2", NULL, BLIST_FORCELUN},
188 	{"IBM", "ProFibre 4000R", "*", BLIST_SPARSELUN | BLIST_LARGELUN},
189 	{"IBM", "2105", NULL, BLIST_RETRY_HWERROR},
190 	{"iomega", "jaz 1GB", "J.86", BLIST_NOTQ | BLIST_NOLUN},
191 	{"IOMEGA", "ZIP", NULL, BLIST_NOTQ | BLIST_NOLUN},
192 	{"IOMEGA", "Io20S         *F", NULL, BLIST_KEY},
193 	{"INSITE", "Floptical   F*8I", NULL, BLIST_KEY},
194 	{"INSITE", "I325VM", NULL, BLIST_KEY},
195 	{"Intel", "Multi-Flex", NULL, BLIST_NO_RSOC},
196 	{"iRiver", "iFP Mass Driver", NULL, BLIST_NOT_LOCKABLE | BLIST_INQUIRY_36},
197 	{"LASOUND", "CDX7405", "3.10", BLIST_MAX5LUN | BLIST_SINGLELUN},
198 	{"Marvell", "Console", NULL, BLIST_SKIP_VPD_PAGES},
199 	{"Marvell", "91xx Config", "1.01", BLIST_SKIP_VPD_PAGES},
200 	{"MATSHITA", "PD-1", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
201 	{"MATSHITA", "DMC-LC5", NULL, BLIST_NOT_LOCKABLE | BLIST_INQUIRY_36},
202 	{"MATSHITA", "DMC-LC40", NULL, BLIST_NOT_LOCKABLE | BLIST_INQUIRY_36},
203 	{"Medion", "Flash XL  MMC/SD", "2.6D", BLIST_FORCELUN},
204 	{"MegaRAID", "LD", NULL, BLIST_FORCELUN},
205 	{"MICROP", "4110", NULL, BLIST_NOTQ},
206 	{"MSFT", "Virtual HD", NULL, BLIST_MAX_1024 | BLIST_NO_RSOC},
207 	{"MYLEX", "DACARMRB", "*", BLIST_REPORTLUN2},
208 	{"nCipher", "Fastness Crypto", NULL, BLIST_FORCELUN},
209 	{"NAKAMICH", "MJ-4.8S", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
210 	{"NAKAMICH", "MJ-5.16S", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
211 	{"NEC", "PD-1 ODX654P", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
212 	{"NEC", "iStorage", NULL, BLIST_REPORTLUN2},
213 	{"NRC", "MBR-7", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
214 	{"NRC", "MBR-7.4", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
215 	{"PIONEER", "CD-ROM DRM-600", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
216 	{"PIONEER", "CD-ROM DRM-602X", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
217 	{"PIONEER", "CD-ROM DRM-604X", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
218 	{"PIONEER", "CD-ROM DRM-624X", NULL, BLIST_FORCELUN | BLIST_SINGLELUN},
219 	{"Promise", "VTrak E610f", NULL, BLIST_SPARSELUN | BLIST_NO_RSOC},
220 	{"Promise", "", NULL, BLIST_SPARSELUN},
221 	{"QEMU", "QEMU CD-ROM", NULL, BLIST_SKIP_VPD_PAGES},
222 	{"QNAP", "iSCSI Storage", NULL, BLIST_MAX_1024},
223 	{"SYNOLOGY", "iSCSI Storage", NULL, BLIST_MAX_1024},
224 	{"QUANTUM", "XP34301", "1071", BLIST_NOTQ},
225 	{"REGAL", "CDC-4X", NULL, BLIST_MAX5LUN | BLIST_SINGLELUN},
226 	{"SanDisk", "ImageMate CF-SD1", NULL, BLIST_FORCELUN},
227 	{"SEAGATE", "ST34555N", "0930", BLIST_NOTQ},	/* Chokes on tagged INQUIRY */
228 	{"SEAGATE", "ST3390N", "9546", BLIST_NOTQ},
229 	{"SEAGATE", "ST900MM0006", NULL, BLIST_SKIP_VPD_PAGES},
230 	{"SGI", "RAID3", "*", BLIST_SPARSELUN},
231 	{"SGI", "RAID5", "*", BLIST_SPARSELUN},
232 	{"SGI", "TP9100", "*", BLIST_REPORTLUN2},
233 	{"SGI", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
234 	{"IBM", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
235 	{"SUN", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
236 	{"DELL", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
237 	{"STK", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
238 	{"NETAPP", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
239 	{"LSI", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
240 	{"ENGENIO", "Universal Xport", "*", BLIST_NO_ULD_ATTACH},
241 	{"SMSC", "USB 2 HS-CF", NULL, BLIST_SPARSELUN | BLIST_INQUIRY_36},
242 	{"SONY", "CD-ROM CDU-8001", NULL, BLIST_BORKEN},
243 	{"SONY", "TSL", NULL, BLIST_FORCELUN},		/* DDS3 & DDS4 autoloaders */
244 	{"ST650211", "CF", NULL, BLIST_RETRY_HWERROR},
245 	{"SUN", "T300", "*", BLIST_SPARSELUN},
246 	{"SUN", "T4", "*", BLIST_SPARSELUN},
247 	{"Tornado-", "F4", "*", BLIST_NOREPORTLUN},
248 	{"TOSHIBA", "CDROM", NULL, BLIST_ISROM},
249 	{"TOSHIBA", "CD-ROM", NULL, BLIST_ISROM},
250 	{"Traxdata", "CDR4120", NULL, BLIST_NOLUN},	/* locks up */
251 	{"USB2.0", "SMARTMEDIA/XD", NULL, BLIST_FORCELUN | BLIST_INQUIRY_36},
252 	{"WangDAT", "Model 2600", "01.7", BLIST_SELECT_NO_ATN},
253 	{"WangDAT", "Model 3200", "02.2", BLIST_SELECT_NO_ATN},
254 	{"WangDAT", "Model 1300", "02.4", BLIST_SELECT_NO_ATN},
255 	{"WDC WD25", "00JB-00FUA0", NULL, BLIST_NOREPORTLUN},
256 	{"XYRATEX", "RS", "*", BLIST_SPARSELUN | BLIST_LARGELUN},
257 	{"Zzyzx", "RocketStor 500S", NULL, BLIST_SPARSELUN},
258 	{"Zzyzx", "RocketStor 2000", NULL, BLIST_SPARSELUN},
259 	{ NULL, NULL, NULL, 0 },
260 };
261 
scsi_devinfo_lookup_by_key(int key)262 static struct scsi_dev_info_list_table *scsi_devinfo_lookup_by_key(int key)
263 {
264 	struct scsi_dev_info_list_table *devinfo_table;
265 	int found = 0;
266 
267 	list_for_each_entry(devinfo_table, &scsi_dev_info_list, node)
268 		if (devinfo_table->key == key) {
269 			found = 1;
270 			break;
271 		}
272 	if (!found)
273 		return ERR_PTR(-EINVAL);
274 
275 	return devinfo_table;
276 }
277 
278 /*
279  * scsi_strcpy_devinfo: called from scsi_dev_info_list_add to copy into
280  * devinfo vendor and model strings.
281  */
scsi_strcpy_devinfo(char * name,char * to,size_t to_length,char * from,int compatible)282 static void scsi_strcpy_devinfo(char *name, char *to, size_t to_length,
283 				char *from, int compatible)
284 {
285 	size_t from_length;
286 
287 	from_length = strlen(from);
288 	/* This zero-pads the destination */
289 	strncpy(to, from, to_length);
290 	if (from_length < to_length && !compatible) {
291 		/*
292 		 * space pad the string if it is short.
293 		 */
294 		memset(&to[from_length], ' ', to_length - from_length);
295 	}
296 	if (from_length > to_length)
297 		 printk(KERN_WARNING "%s: %s string '%s' is too long\n",
298 			__func__, name, from);
299 }
300 
301 /**
302  * scsi_dev_info_list_add - add one dev_info list entry.
303  * @compatible: if true, null terminate short strings.  Otherwise space pad.
304  * @vendor:	vendor string
305  * @model:	model (product) string
306  * @strflags:	integer string
307  * @flags:	if strflags NULL, use this flag value
308  *
309  * Description:
310  *	Create and add one dev_info entry for @vendor, @model, @strflags or
311  *	@flag. If @compatible, add to the tail of the list, do not space
312  *	pad, and set devinfo->compatible. The scsi_static_device_list entries
313  *	are added with @compatible 1 and @clfags NULL.
314  *
315  * Returns: 0 OK, -error on failure.
316  **/
scsi_dev_info_list_add(int compatible,char * vendor,char * model,char * strflags,blist_flags_t flags)317 static int scsi_dev_info_list_add(int compatible, char *vendor, char *model,
318 			    char *strflags, blist_flags_t flags)
319 {
320 	return scsi_dev_info_list_add_keyed(compatible, vendor, model,
321 					    strflags, flags,
322 					    SCSI_DEVINFO_GLOBAL);
323 }
324 
325 /**
326  * scsi_dev_info_list_add_keyed - add one dev_info list entry.
327  * @compatible: if true, null terminate short strings.  Otherwise space pad.
328  * @vendor:	vendor string
329  * @model:	model (product) string
330  * @strflags:	integer string
331  * @flags:	if strflags NULL, use this flag value
332  * @key:	specify list to use
333  *
334  * Description:
335  *	Create and add one dev_info entry for @vendor, @model,
336  *	@strflags or @flag in list specified by @key. If @compatible,
337  *	add to the tail of the list, do not space pad, and set
338  *	devinfo->compatible. The scsi_static_device_list entries are
339  *	added with @compatible 1 and @clfags NULL.
340  *
341  * Returns: 0 OK, -error on failure.
342  **/
scsi_dev_info_list_add_keyed(int compatible,char * vendor,char * model,char * strflags,blist_flags_t flags,enum scsi_devinfo_key key)343 int scsi_dev_info_list_add_keyed(int compatible, char *vendor, char *model,
344 				 char *strflags, blist_flags_t flags,
345 				 enum scsi_devinfo_key key)
346 {
347 	struct scsi_dev_info_list *devinfo;
348 	struct scsi_dev_info_list_table *devinfo_table =
349 		scsi_devinfo_lookup_by_key(key);
350 
351 	if (IS_ERR(devinfo_table))
352 		return PTR_ERR(devinfo_table);
353 
354 	devinfo = kmalloc(sizeof(*devinfo), GFP_KERNEL);
355 	if (!devinfo) {
356 		printk(KERN_ERR "%s: no memory\n", __func__);
357 		return -ENOMEM;
358 	}
359 
360 	scsi_strcpy_devinfo("vendor", devinfo->vendor, sizeof(devinfo->vendor),
361 			    vendor, compatible);
362 	scsi_strcpy_devinfo("model", devinfo->model, sizeof(devinfo->model),
363 			    model, compatible);
364 
365 	if (strflags) {
366 		unsigned long long val;
367 		int ret = kstrtoull(strflags, 0, &val);
368 
369 		if (ret != 0) {
370 			kfree(devinfo);
371 			return ret;
372 		}
373 		flags = (__force blist_flags_t)val;
374 	}
375 	if (flags & __BLIST_UNUSED_MASK) {
376 		pr_err("scsi_devinfo (%s:%s): unsupported flags 0x%llx",
377 		       vendor, model, flags & __BLIST_UNUSED_MASK);
378 		kfree(devinfo);
379 		return -EINVAL;
380 	}
381 	devinfo->flags = flags;
382 	devinfo->compatible = compatible;
383 
384 	if (compatible)
385 		list_add_tail(&devinfo->dev_info_list,
386 			      &devinfo_table->scsi_dev_info_list);
387 	else
388 		list_add(&devinfo->dev_info_list,
389 			 &devinfo_table->scsi_dev_info_list);
390 
391 	return 0;
392 }
393 EXPORT_SYMBOL(scsi_dev_info_list_add_keyed);
394 
395 /**
396  * scsi_dev_info_list_find - find a matching dev_info list entry.
397  * @vendor:	full vendor string
398  * @model:	full model (product) string
399  * @key:	specify list to use
400  *
401  * Description:
402  *	Finds the first dev_info entry matching @vendor, @model
403  *	in list specified by @key.
404  *
405  * Returns: pointer to matching entry, or ERR_PTR on failure.
406  **/
scsi_dev_info_list_find(const char * vendor,const char * model,enum scsi_devinfo_key key)407 static struct scsi_dev_info_list *scsi_dev_info_list_find(const char *vendor,
408 		const char *model, enum scsi_devinfo_key key)
409 {
410 	struct scsi_dev_info_list *devinfo;
411 	struct scsi_dev_info_list_table *devinfo_table =
412 		scsi_devinfo_lookup_by_key(key);
413 	size_t vmax, mmax, mlen;
414 	const char *vskip, *mskip;
415 
416 	if (IS_ERR(devinfo_table))
417 		return (struct scsi_dev_info_list *) devinfo_table;
418 
419 	/* Prepare for "compatible" matches */
420 
421 	/*
422 	 * XXX why skip leading spaces? If an odd INQUIRY
423 	 * value, that should have been part of the
424 	 * scsi_static_device_list[] entry, such as "  FOO"
425 	 * rather than "FOO". Since this code is already
426 	 * here, and we don't know what device it is
427 	 * trying to work with, leave it as-is.
428 	 */
429 	vmax = sizeof(devinfo->vendor);
430 	vskip = vendor;
431 	while (vmax > 0 && *vskip == ' ') {
432 		vmax--;
433 		vskip++;
434 	}
435 	/* Also skip trailing spaces */
436 	while (vmax > 0 && vskip[vmax - 1] == ' ')
437 		--vmax;
438 
439 	mmax = sizeof(devinfo->model);
440 	mskip = model;
441 	while (mmax > 0 && *mskip == ' ') {
442 		mmax--;
443 		mskip++;
444 	}
445 	while (mmax > 0 && mskip[mmax - 1] == ' ')
446 		--mmax;
447 
448 	list_for_each_entry(devinfo, &devinfo_table->scsi_dev_info_list,
449 			    dev_info_list) {
450 		if (devinfo->compatible) {
451 			/*
452 			 * vendor strings must be an exact match
453 			 */
454 			if (vmax != strnlen(devinfo->vendor,
455 					    sizeof(devinfo->vendor)) ||
456 			    memcmp(devinfo->vendor, vskip, vmax))
457 				continue;
458 
459 			/*
460 			 * @model specifies the full string, and
461 			 * must be larger or equal to devinfo->model
462 			 */
463 			mlen = strnlen(devinfo->model, sizeof(devinfo->model));
464 			if (mmax < mlen || memcmp(devinfo->model, mskip, mlen))
465 				continue;
466 			return devinfo;
467 		} else {
468 			if (!memcmp(devinfo->vendor, vendor,
469 				    sizeof(devinfo->vendor)) &&
470 			    !memcmp(devinfo->model, model,
471 				    sizeof(devinfo->model)))
472 				return devinfo;
473 		}
474 	}
475 
476 	return ERR_PTR(-ENOENT);
477 }
478 
479 /**
480  * scsi_dev_info_list_del_keyed - remove one dev_info list entry.
481  * @vendor:	vendor string
482  * @model:	model (product) string
483  * @key:	specify list to use
484  *
485  * Description:
486  *	Remove and destroy one dev_info entry for @vendor, @model
487  *	in list specified by @key.
488  *
489  * Returns: 0 OK, -error on failure.
490  **/
scsi_dev_info_list_del_keyed(char * vendor,char * model,enum scsi_devinfo_key key)491 int scsi_dev_info_list_del_keyed(char *vendor, char *model,
492 				 enum scsi_devinfo_key key)
493 {
494 	struct scsi_dev_info_list *found;
495 
496 	found = scsi_dev_info_list_find(vendor, model, key);
497 	if (IS_ERR(found))
498 		return PTR_ERR(found);
499 
500 	list_del(&found->dev_info_list);
501 	kfree(found);
502 	return 0;
503 }
504 EXPORT_SYMBOL(scsi_dev_info_list_del_keyed);
505 
506 /**
507  * scsi_dev_info_list_add_str - parse dev_list and add to the scsi_dev_info_list.
508  * @dev_list:	string of device flags to add
509  *
510  * Description:
511  *	Parse dev_list, and add entries to the scsi_dev_info_list.
512  *	dev_list is of the form "vendor:product:flag,vendor:product:flag".
513  *	dev_list is modified via strsep. Can be called for command line
514  *	addition, for proc or mabye a sysfs interface.
515  *
516  * Returns: 0 if OK, -error on failure.
517  **/
scsi_dev_info_list_add_str(char * dev_list)518 static int scsi_dev_info_list_add_str(char *dev_list)
519 {
520 	char *vendor, *model, *strflags, *next;
521 	char *next_check;
522 	int res = 0;
523 
524 	next = dev_list;
525 	if (next && next[0] == '"') {
526 		/*
527 		 * Ignore both the leading and trailing quote.
528 		 */
529 		next++;
530 		next_check = ",\"";
531 	} else {
532 		next_check = ",";
533 	}
534 
535 	/*
536 	 * For the leading and trailing '"' case, the for loop comes
537 	 * through the last time with vendor[0] == '\0'.
538 	 */
539 	for (vendor = strsep(&next, ":"); vendor && (vendor[0] != '\0')
540 	     && (res == 0); vendor = strsep(&next, ":")) {
541 		strflags = NULL;
542 		model = strsep(&next, ":");
543 		if (model)
544 			strflags = strsep(&next, next_check);
545 		if (!model || !strflags) {
546 			printk(KERN_ERR "%s: bad dev info string '%s' '%s'"
547 			       " '%s'\n", __func__, vendor, model,
548 			       strflags);
549 			res = -EINVAL;
550 		} else
551 			res = scsi_dev_info_list_add(0 /* compatible */, vendor,
552 						     model, strflags, 0);
553 	}
554 	return res;
555 }
556 
557 /**
558  * get_device_flags - get device specific flags from the dynamic device list.
559  * @sdev:       &scsi_device to get flags for
560  * @vendor:	vendor name
561  * @model:	model name
562  *
563  * Description:
564  *     Search the global scsi_dev_info_list (specified by list zero)
565  *     for an entry matching @vendor and @model, if found, return the
566  *     matching flags value, else return the host or global default
567  *     settings.  Called during scan time.
568  **/
scsi_get_device_flags(struct scsi_device * sdev,const unsigned char * vendor,const unsigned char * model)569 blist_flags_t scsi_get_device_flags(struct scsi_device *sdev,
570 				    const unsigned char *vendor,
571 				    const unsigned char *model)
572 {
573 	return scsi_get_device_flags_keyed(sdev, vendor, model,
574 					   SCSI_DEVINFO_GLOBAL);
575 }
576 
577 
578 /**
579  * scsi_get_device_flags_keyed - get device specific flags from the dynamic device list
580  * @sdev:       &scsi_device to get flags for
581  * @vendor:	vendor name
582  * @model:	model name
583  * @key:	list to look up
584  *
585  * Description:
586  *     Search the scsi_dev_info_list specified by @key for an entry
587  *     matching @vendor and @model, if found, return the matching
588  *     flags value, else return the host or global default settings.
589  *     Called during scan time.
590  **/
scsi_get_device_flags_keyed(struct scsi_device * sdev,const unsigned char * vendor,const unsigned char * model,enum scsi_devinfo_key key)591 blist_flags_t scsi_get_device_flags_keyed(struct scsi_device *sdev,
592 				const unsigned char *vendor,
593 				const unsigned char *model,
594 				enum scsi_devinfo_key key)
595 {
596 	struct scsi_dev_info_list *devinfo;
597 
598 	devinfo = scsi_dev_info_list_find(vendor, model, key);
599 	if (!IS_ERR(devinfo))
600 		return devinfo->flags;
601 
602 	/* key or device not found: return nothing */
603 	if (key != SCSI_DEVINFO_GLOBAL)
604 		return 0;
605 
606 	/* except for the global list, where we have an exception */
607 	if (sdev->sdev_bflags)
608 		return sdev->sdev_bflags;
609 
610 	return scsi_default_dev_flags;
611 }
612 EXPORT_SYMBOL(scsi_get_device_flags_keyed);
613 
614 #ifdef CONFIG_SCSI_PROC_FS
615 struct double_list {
616 	struct list_head *top;
617 	struct list_head *bottom;
618 };
619 
devinfo_seq_show(struct seq_file * m,void * v)620 static int devinfo_seq_show(struct seq_file *m, void *v)
621 {
622 	struct double_list *dl = v;
623 	struct scsi_dev_info_list_table *devinfo_table =
624 		list_entry(dl->top, struct scsi_dev_info_list_table, node);
625 	struct scsi_dev_info_list *devinfo =
626 		list_entry(dl->bottom, struct scsi_dev_info_list,
627 			   dev_info_list);
628 
629 	if (devinfo_table->scsi_dev_info_list.next == dl->bottom &&
630 	    devinfo_table->name)
631 		seq_printf(m, "[%s]:\n", devinfo_table->name);
632 
633 	seq_printf(m, "'%.8s' '%.16s' 0x%llx\n",
634 		   devinfo->vendor, devinfo->model, devinfo->flags);
635 	return 0;
636 }
637 
devinfo_seq_start(struct seq_file * m,loff_t * ppos)638 static void *devinfo_seq_start(struct seq_file *m, loff_t *ppos)
639 {
640 	struct double_list *dl = kmalloc(sizeof(*dl), GFP_KERNEL);
641 	loff_t pos = *ppos;
642 
643 	if (!dl)
644 		return NULL;
645 
646 	list_for_each(dl->top, &scsi_dev_info_list) {
647 		struct scsi_dev_info_list_table *devinfo_table =
648 			list_entry(dl->top, struct scsi_dev_info_list_table,
649 				   node);
650 		list_for_each(dl->bottom, &devinfo_table->scsi_dev_info_list)
651 			if (pos-- == 0)
652 				return dl;
653 	}
654 
655 	kfree(dl);
656 	return NULL;
657 }
658 
devinfo_seq_next(struct seq_file * m,void * v,loff_t * ppos)659 static void *devinfo_seq_next(struct seq_file *m, void *v, loff_t *ppos)
660 {
661 	struct double_list *dl = v;
662 	struct scsi_dev_info_list_table *devinfo_table =
663 		list_entry(dl->top, struct scsi_dev_info_list_table, node);
664 
665 	++*ppos;
666 	dl->bottom = dl->bottom->next;
667 	while (&devinfo_table->scsi_dev_info_list == dl->bottom) {
668 		dl->top = dl->top->next;
669 		if (dl->top == &scsi_dev_info_list) {
670 			kfree(dl);
671 			return NULL;
672 		}
673 		devinfo_table = list_entry(dl->top,
674 					   struct scsi_dev_info_list_table,
675 					   node);
676 		dl->bottom = devinfo_table->scsi_dev_info_list.next;
677 	}
678 
679 	return dl;
680 }
681 
devinfo_seq_stop(struct seq_file * m,void * v)682 static void devinfo_seq_stop(struct seq_file *m, void *v)
683 {
684 	kfree(v);
685 }
686 
687 static const struct seq_operations scsi_devinfo_seq_ops = {
688 	.start	= devinfo_seq_start,
689 	.next	= devinfo_seq_next,
690 	.stop	= devinfo_seq_stop,
691 	.show	= devinfo_seq_show,
692 };
693 
proc_scsi_devinfo_open(struct inode * inode,struct file * file)694 static int proc_scsi_devinfo_open(struct inode *inode, struct file *file)
695 {
696 	return seq_open(file, &scsi_devinfo_seq_ops);
697 }
698 
699 /*
700  * proc_scsi_dev_info_write - allow additions to scsi_dev_info_list via /proc.
701  *
702  * Description: Adds a black/white list entry for vendor and model with an
703  * integer value of flag to the scsi device info list.
704  * To use, echo "vendor:model:flag" > /proc/scsi/device_info
705  */
proc_scsi_devinfo_write(struct file * file,const char __user * buf,size_t length,loff_t * ppos)706 static ssize_t proc_scsi_devinfo_write(struct file *file,
707 				       const char __user *buf,
708 				       size_t length, loff_t *ppos)
709 {
710 	char *buffer;
711 	ssize_t err = length;
712 
713 	if (!buf || length>PAGE_SIZE)
714 		return -EINVAL;
715 	if (!(buffer = (char *) __get_free_page(GFP_KERNEL)))
716 		return -ENOMEM;
717 	if (copy_from_user(buffer, buf, length)) {
718 		err =-EFAULT;
719 		goto out;
720 	}
721 
722 	if (length < PAGE_SIZE)
723 		buffer[length] = '\0';
724 	else if (buffer[PAGE_SIZE-1]) {
725 		err = -EINVAL;
726 		goto out;
727 	}
728 
729 	scsi_dev_info_list_add_str(buffer);
730 
731 out:
732 	free_page((unsigned long)buffer);
733 	return err;
734 }
735 
736 static const struct file_operations scsi_devinfo_proc_fops = {
737 	.owner		= THIS_MODULE,
738 	.open		= proc_scsi_devinfo_open,
739 	.read		= seq_read,
740 	.write		= proc_scsi_devinfo_write,
741 	.llseek		= seq_lseek,
742 	.release	= seq_release,
743 };
744 #endif /* CONFIG_SCSI_PROC_FS */
745 
746 module_param_string(dev_flags, scsi_dev_flags, sizeof(scsi_dev_flags), 0);
747 MODULE_PARM_DESC(dev_flags,
748 	 "Given scsi_dev_flags=vendor:model:flags[,v:m:f] add black/white"
749 	 " list entries for vendor and model with an integer value of flags"
750 	 " to the scsi device info list");
751 
752 module_param_named(default_dev_flags, scsi_default_dev_flags, ullong, 0644);
753 MODULE_PARM_DESC(default_dev_flags,
754 		 "scsi default device flag uint64_t value");
755 
756 /**
757  * scsi_exit_devinfo - remove /proc/scsi/device_info & the scsi_dev_info_list
758  **/
scsi_exit_devinfo(void)759 void scsi_exit_devinfo(void)
760 {
761 #ifdef CONFIG_SCSI_PROC_FS
762 	remove_proc_entry("scsi/device_info", NULL);
763 #endif
764 
765 	scsi_dev_info_remove_list(SCSI_DEVINFO_GLOBAL);
766 }
767 
768 /**
769  * scsi_dev_info_add_list - add a new devinfo list
770  * @key:	key of the list to add
771  * @name:	Name of the list to add (for /proc/scsi/device_info)
772  *
773  * Adds the requested list, returns zero on success, -EEXIST if the
774  * key is already registered to a list, or other error on failure.
775  */
scsi_dev_info_add_list(enum scsi_devinfo_key key,const char * name)776 int scsi_dev_info_add_list(enum scsi_devinfo_key key, const char *name)
777 {
778 	struct scsi_dev_info_list_table *devinfo_table =
779 		scsi_devinfo_lookup_by_key(key);
780 
781 	if (!IS_ERR(devinfo_table))
782 		/* list already exists */
783 		return -EEXIST;
784 
785 	devinfo_table = kmalloc(sizeof(*devinfo_table), GFP_KERNEL);
786 
787 	if (!devinfo_table)
788 		return -ENOMEM;
789 
790 	INIT_LIST_HEAD(&devinfo_table->node);
791 	INIT_LIST_HEAD(&devinfo_table->scsi_dev_info_list);
792 	devinfo_table->name = name;
793 	devinfo_table->key = key;
794 	list_add_tail(&devinfo_table->node, &scsi_dev_info_list);
795 
796 	return 0;
797 }
798 EXPORT_SYMBOL(scsi_dev_info_add_list);
799 
800 /**
801  * scsi_dev_info_remove_list - destroy an added devinfo list
802  * @key: key of the list to destroy
803  *
804  * Iterates over the entire list first, freeing all the values, then
805  * frees the list itself.  Returns 0 on success or -EINVAL if the key
806  * can't be found.
807  */
scsi_dev_info_remove_list(enum scsi_devinfo_key key)808 int scsi_dev_info_remove_list(enum scsi_devinfo_key key)
809 {
810 	struct list_head *lh, *lh_next;
811 	struct scsi_dev_info_list_table *devinfo_table =
812 		scsi_devinfo_lookup_by_key(key);
813 
814 	if (IS_ERR(devinfo_table))
815 		/* no such list */
816 		return -EINVAL;
817 
818 	/* remove from the master list */
819 	list_del(&devinfo_table->node);
820 
821 	list_for_each_safe(lh, lh_next, &devinfo_table->scsi_dev_info_list) {
822 		struct scsi_dev_info_list *devinfo;
823 
824 		devinfo = list_entry(lh, struct scsi_dev_info_list,
825 				     dev_info_list);
826 		kfree(devinfo);
827 	}
828 	kfree(devinfo_table);
829 
830 	return 0;
831 }
832 EXPORT_SYMBOL(scsi_dev_info_remove_list);
833 
834 /**
835  * scsi_init_devinfo - set up the dynamic device list.
836  *
837  * Description:
838  *	Add command line entries from scsi_dev_flags, then add
839  *	scsi_static_device_list entries to the scsi device info list.
840  */
scsi_init_devinfo(void)841 int __init scsi_init_devinfo(void)
842 {
843 #ifdef CONFIG_SCSI_PROC_FS
844 	struct proc_dir_entry *p;
845 #endif
846 	int error, i;
847 
848 	error = scsi_dev_info_add_list(SCSI_DEVINFO_GLOBAL, NULL);
849 	if (error)
850 		return error;
851 
852 	error = scsi_dev_info_list_add_str(scsi_dev_flags);
853 	if (error)
854 		goto out;
855 
856 	for (i = 0; scsi_static_device_list[i].vendor; i++) {
857 		error = scsi_dev_info_list_add(1 /* compatibile */,
858 				scsi_static_device_list[i].vendor,
859 				scsi_static_device_list[i].model,
860 				NULL,
861 				scsi_static_device_list[i].flags);
862 		if (error)
863 			goto out;
864 	}
865 
866 #ifdef CONFIG_SCSI_PROC_FS
867 	p = proc_create("scsi/device_info", 0, NULL, &scsi_devinfo_proc_fops);
868 	if (!p) {
869 		error = -ENOMEM;
870 		goto out;
871 	}
872 #endif /* CONFIG_SCSI_PROC_FS */
873 
874  out:
875 	if (error)
876 		scsi_exit_devinfo();
877 	return error;
878 }
879