Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / disk / scsi.c
blobb7b6834e78fe7c833e1d7b39d6d7b9dc37926d03
1 /* scsi.c - scsi support. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2008 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/disk.h>
21 #include <grub/dl.h>
22 #include <grub/kernel.h>
23 #include <grub/misc.h>
24 #include <grub/mm.h>
25 #include <grub/types.h>
26 #include <grub/machine/kernel.h>
27 #include <grub/scsi.h>
28 #include <grub/scsicmd.h>
31 static grub_scsi_dev_t grub_scsi_dev_list;
33 void
34 grub_scsi_dev_register (grub_scsi_dev_t dev)
36 dev->next = grub_scsi_dev_list;
37 grub_scsi_dev_list = dev;
40 void
41 grub_scsi_dev_unregister (grub_scsi_dev_t dev)
43 grub_scsi_dev_t *p, q;
45 for (p = &grub_scsi_dev_list, q = *p; q; p = &(q->next), q = q->next)
46 if (q == dev)
48 *p = q->next;
49 break;
54 /* Determine the the device is removable and the type of the device
55 SCSI. */
56 static grub_err_t
57 grub_scsi_inquiry (grub_scsi_t scsi)
59 struct grub_scsi_inquiry iq;
60 struct grub_scsi_inquiry_data iqd;
61 grub_err_t err;
63 iq.opcode = grub_scsi_cmd_inquiry;
64 iq.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
65 iq.reserved = 0;
66 iq.alloc_length = 0x24; /* XXX: Hardcoded for now */
67 iq.reserved2 = 0;
69 err = scsi->dev->read (scsi, sizeof (iq), (char *) &iq,
70 sizeof (iqd), (char *) &iqd);
71 if (err)
72 return err;
74 scsi->devtype = iqd.devtype & GRUB_SCSI_DEVTYPE_MASK;
75 scsi->removable = iqd.rmb >> GRUB_SCSI_REMOVABLE_BIT;
77 return GRUB_ERR_NONE;
80 /* Read the capacity and block size of SCSI. */
81 static grub_err_t
82 grub_scsi_read_capacity (grub_scsi_t scsi)
84 struct grub_scsi_read_capacity rc;
85 struct grub_scsi_read_capacity_data rcd;
86 grub_err_t err;
88 rc.opcode = grub_scsi_cmd_read_capacity;
89 rc.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
90 grub_memset (rc.reserved, 0, sizeof (rc.reserved));
92 err = scsi->dev->read (scsi, sizeof (rc), (char *) &rc,
93 sizeof (rcd), (char *) &rcd);
94 if (err)
95 return err;
97 scsi->size = grub_be_to_cpu32 (rcd.size);
98 scsi->blocksize = grub_be_to_cpu32 (rcd.blocksize);
100 return GRUB_ERR_NONE;
103 /* Send a SCSI request for DISK: read SIZE sectors starting with
104 sector SECTOR to BUF. */
105 static grub_err_t
106 grub_scsi_read10 (grub_disk_t disk, grub_disk_addr_t sector,
107 grub_size_t size, char *buf)
109 grub_scsi_t scsi;
110 struct grub_scsi_read10 rd;
112 scsi = disk->data;
114 rd.opcode = grub_scsi_cmd_read10;
115 rd.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
116 rd.lba = grub_cpu_to_be32 (sector);
117 rd.reserved = 0;
118 rd.size = grub_cpu_to_be16 (size);
119 rd.reserved2 = 0;
120 rd.pad = 0;
122 return scsi->dev->read (scsi, sizeof (rd), (char *) &rd, size * 512, buf);
125 /* Send a SCSI request for DISK: read SIZE sectors starting with
126 sector SECTOR to BUF. */
127 static grub_err_t
128 grub_scsi_read12 (grub_disk_t disk, grub_disk_addr_t sector,
129 grub_size_t size, char *buf)
131 grub_scsi_t scsi;
132 struct grub_scsi_read12 rd;
134 scsi = disk->data;
136 rd.opcode = grub_scsi_cmd_read12;
137 rd.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
138 rd.lba = grub_cpu_to_be32 (sector);
139 rd.size = grub_cpu_to_be32 (size);
140 rd.reserved = 0;
141 rd.control = 0;
143 return scsi->dev->read (scsi, sizeof (rd), (char *) &rd, size * 512, buf);
146 #if 0
147 /* Send a SCSI request for DISK: write the data stored in BUF to SIZE
148 sectors starting with SECTOR. */
149 static grub_err_t
150 grub_scsi_write10 (grub_disk_t disk, grub_disk_addr_t sector,
151 grub_size_t size, char *buf)
153 grub_scsi_t scsi;
154 struct grub_scsi_write10 wr;
156 scsi = disk->data;
158 wr.opcode = grub_scsi_cmd_write10;
159 wr.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
160 wr.lba = grub_cpu_to_be32 (sector);
161 wr.reserved = 0;
162 wr.size = grub_cpu_to_be16 (size);
163 wr.reserved2 = 0;
164 wr.pad = 0;
166 return scsi->dev->write (scsi, sizeof (wr), (char *) &wr, size * 512, buf);
169 /* Send a SCSI request for DISK: write the data stored in BUF to SIZE
170 sectors starting with SECTOR. */
171 static grub_err_t
172 grub_scsi_write12 (grub_disk_t disk, grub_disk_addr_t sector,
173 grub_size_t size, char *buf)
175 grub_scsi_t scsi;
176 struct grub_scsi_write10 wr;
178 scsi = disk->data;
180 wr.opcode = grub_scsi_cmd_write12;
181 wr.lun = scsi->lun << GRUB_SCSI_LUN_SHIFT;
182 wr.lba = grub_cpu_to_be32 (sector);
183 wr.size = grub_cpu_to_be32 (size);
184 wr.reserved = 0;
185 wr.pad = 0;
187 return scsi->dev->write (scsi, sizeof (wr), (char *) &wr, size * 512, buf);
189 #endif
192 static int
193 grub_scsi_iterate (int (*hook) (const char *name))
195 grub_scsi_dev_t p;
197 auto int scsi_iterate (const char *name, int luns);
199 int scsi_iterate (const char *name, int luns)
201 char sname[40];
202 int i;
204 /* In case of a single LUN, just return `usbX'. */
205 if (luns == 1)
206 return hook (name);
208 /* In case of multiple LUNs, every LUN will get a prefix to
209 distinguish it. */
210 for (i = 0; i < luns; i++)
212 grub_sprintf (sname, "%s%c", name, 'a' + i);
213 if (hook (sname))
214 return 1;
216 return 0;
219 for (p = grub_scsi_dev_list; p; p = p->next)
220 if (p->iterate && (p->iterate) (scsi_iterate))
221 return 1;
223 return 0;
226 static grub_err_t
227 grub_scsi_open (const char *name, grub_disk_t disk)
229 grub_scsi_dev_t p;
230 grub_scsi_t scsi;
231 grub_err_t err;
232 int len;
233 int lun;
235 scsi = grub_malloc (sizeof (*scsi));
236 if (! scsi)
237 return grub_errno;
239 len = grub_strlen (name);
240 lun = name[len - 1] - 'a';
242 /* Try to detect a LUN ('a'-'z'), otherwise just use the first
243 LUN. */
244 if (lun < 0 || lun > 26)
245 lun = 0;
247 for (p = grub_scsi_dev_list; p; p = p->next)
249 if (! p->open (name, scsi))
251 disk->id = (unsigned long) "scsi"; /* XXX */
252 disk->data = scsi;
253 scsi->dev = p;
254 scsi->lun = lun;
255 scsi->name = grub_strdup (name);
256 if (! scsi->name)
258 return grub_errno;
261 grub_dprintf ("scsi", "dev opened\n");
263 err = grub_scsi_inquiry (scsi);
264 if (err)
266 grub_dprintf ("scsi", "inquiry failed\n");
267 return grub_errno;
270 grub_dprintf ("scsi", "inquiry: devtype=0x%02x removable=%d\n",
271 scsi->devtype, scsi->removable);
273 /* Try to be conservative about the device types
274 supported. */
275 if (scsi->devtype != grub_scsi_devtype_direct
276 && scsi->devtype != grub_scsi_devtype_cdrom)
278 return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
279 "unknown SCSI device");
282 if (scsi->devtype == grub_scsi_devtype_cdrom)
283 disk->has_partitions = 0;
284 else
285 disk->has_partitions = 1;
287 err = grub_scsi_read_capacity (scsi);
288 if (err)
290 grub_dprintf ("scsi", "READ CAPACITY failed\n");
291 return grub_errno;
294 /* SCSI blocks can be something else than 512, although GRUB
295 wants 512 byte blocks. */
296 disk->total_sectors = ((scsi->size * scsi->blocksize)
297 << GRUB_DISK_SECTOR_BITS);
299 grub_dprintf ("scsi", "capacity=%llu, blksize=%d\n",
300 disk->total_sectors, scsi->blocksize);
302 return GRUB_ERR_NONE;
306 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not a SCSI disk");
309 static void
310 grub_scsi_close (grub_disk_t disk)
312 grub_scsi_t scsi;
314 scsi = disk->data;
315 return scsi->dev->close (scsi);
318 static grub_err_t
319 grub_scsi_read (grub_disk_t disk, grub_disk_addr_t sector,
320 grub_size_t size, char *buf)
322 grub_scsi_t scsi;
324 scsi = disk->data;
326 /* SCSI sectors are variable in size. GRUB uses 512 byte
327 sectors. */
328 sector = grub_divmod64 (sector, scsi->blocksize >> GRUB_DISK_SECTOR_BITS,
329 NULL);
331 /* Depending on the type, select a read function. */
332 switch (scsi->devtype)
334 case grub_scsi_devtype_direct:
335 return grub_scsi_read10 (disk, sector, size, buf);
337 case grub_scsi_devtype_cdrom:
338 return grub_scsi_read12 (disk, sector, size, buf);
341 /* XXX: Never reached. */
342 return GRUB_ERR_NONE;
345 static grub_err_t
346 grub_scsi_write (grub_disk_t disk __attribute((unused)),
347 grub_disk_addr_t sector __attribute((unused)),
348 grub_size_t size __attribute((unused)),
349 const char *buf __attribute((unused)))
351 #if 0
352 /* XXX: Not tested yet! */
354 /* XXX: This should depend on the device type? */
355 return grub_scsi_write10 (disk, sector, size, buf);
356 #endif
357 return GRUB_ERR_NOT_IMPLEMENTED_YET;
361 static struct grub_disk_dev grub_scsi_dev =
363 .name = "scsi",
364 .id = GRUB_DISK_DEVICE_SCSI_ID,
365 .iterate = grub_scsi_iterate,
366 .open = grub_scsi_open,
367 .close = grub_scsi_close,
368 .read = grub_scsi_read,
369 .write = grub_scsi_write,
370 .next = 0
373 GRUB_MOD_INIT(scsi)
375 grub_disk_dev_register (&grub_scsi_dev);
378 GRUB_MOD_FINI(scsi)
380 grub_disk_dev_unregister (&grub_scsi_dev);