scsi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * scsi.c Copyright (C) 1992 Drew Eckhardt
  4. * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
  5. * Copyright (C) 2002, 2003 Christoph Hellwig
  6. *
  7. * generic mid-level SCSI driver
  8. * Initial versions: Drew Eckhardt
  9. * Subsequent revisions: Eric Youngdale
  10. *
  11. * <drew@colorado.edu>
  12. *
  13. * Bug correction thanks go to :
  14. * Rik Faith <faith@cs.unc.edu>
  15. * Tommy Thorn <tthorn>
  16. * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
  17. *
  18. * Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
  19. * add scatter-gather, multiple outstanding request, and other
  20. * enhancements.
  21. *
  22. * Native multichannel, wide scsi, /proc/scsi and hot plugging
  23. * support added by Michael Neuffer <mike@i-connect.net>
  24. *
  25. * Added request_module("scsi_hostadapter") for kerneld:
  26. * (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf)
  27. * Bjorn Ekwall <bj0rn@blox.se>
  28. * (changed to kmod)
  29. *
  30. * Major improvements to the timeout, abort, and reset processing,
  31. * as well as performance modifications for large queue depths by
  32. * Leonard N. Zubkoff <lnz@dandelion.com>
  33. *
  34. * Converted cli() code to spinlocks, Ingo Molnar
  35. *
  36. * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
  37. *
  38. * out_of_space hacks, D. Gilbert (dpg) 990608
  39. */
  40. #include <linux/module.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/kernel.h>
  43. #include <linux/timer.h>
  44. #include <linux/string.h>
  45. #include <linux/slab.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/delay.h>
  48. #include <linux/init.h>
  49. #include <linux/completion.h>
  50. #include <linux/unistd.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/kmod.h>
  53. #include <linux/interrupt.h>
  54. #include <linux/notifier.h>
  55. #include <linux/cpu.h>
  56. #include <linux/mutex.h>
  57. #include <linux/async.h>
  58. #include <asm/unaligned.h>
  59. #include <scsi/scsi.h>
  60. #include <scsi/scsi_cmnd.h>
  61. #include <scsi/scsi_dbg.h>
  62. #include <scsi/scsi_device.h>
  63. #include <scsi/scsi_driver.h>
  64. #include <scsi/scsi_eh.h>
  65. #include <scsi/scsi_host.h>
  66. #include <scsi/scsi_tcq.h>
  67. #include "scsi_priv.h"
  68. #include "scsi_logging.h"
  69. #define CREATE_TRACE_POINTS
  70. #include <trace/events/scsi.h>
  71. /*
  72. * Definitions and constants.
  73. */
  74. /*
  75. * Note - the initial logging level can be set here to log events at boot time.
  76. * After the system is up, you may enable logging via the /proc interface.
  77. */
  78. unsigned int scsi_logging_level;
  79. #if defined(CONFIG_SCSI_LOGGING)
  80. EXPORT_SYMBOL(scsi_logging_level);
  81. #endif
  82. /*
  83. * Domain for asynchronous system resume operations. It is marked 'exclusive'
  84. * to avoid being included in the async_synchronize_full() that is invoked by
  85. * dpm_resume().
  86. */
  87. ASYNC_DOMAIN_EXCLUSIVE(scsi_sd_pm_domain);
  88. EXPORT_SYMBOL(scsi_sd_pm_domain);
  89. #ifdef CONFIG_SCSI_LOGGING
  90. void scsi_log_send(struct scsi_cmnd *cmd)
  91. {
  92. unsigned int level;
  93. /*
  94. * If ML QUEUE log level is greater than or equal to:
  95. *
  96. * 1: nothing (match completion)
  97. *
  98. * 2: log opcode + command of all commands + cmd address
  99. *
  100. * 3: same as 2
  101. *
  102. * 4: same as 3
  103. */
  104. if (unlikely(scsi_logging_level)) {
  105. level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
  106. SCSI_LOG_MLQUEUE_BITS);
  107. if (level > 1) {
  108. scmd_printk(KERN_INFO, cmd,
  109. "Send: scmd 0x%p\n", cmd);
  110. scsi_print_command(cmd);
  111. }
  112. }
  113. }
  114. void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
  115. {
  116. unsigned int level;
  117. /*
  118. * If ML COMPLETE log level is greater than or equal to:
  119. *
  120. * 1: log disposition, result, opcode + command, and conditionally
  121. * sense data for failures or non SUCCESS dispositions.
  122. *
  123. * 2: same as 1 but for all command completions.
  124. *
  125. * 3: same as 2
  126. *
  127. * 4: same as 3 plus dump extra junk
  128. */
  129. if (unlikely(scsi_logging_level)) {
  130. level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
  131. SCSI_LOG_MLCOMPLETE_BITS);
  132. if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
  133. (level > 1)) {
  134. scsi_print_result(cmd, "Done", disposition);
  135. scsi_print_command(cmd);
  136. if (status_byte(cmd->result) == CHECK_CONDITION)
  137. scsi_print_sense(cmd);
  138. if (level > 3)
  139. scmd_printk(KERN_INFO, cmd,
  140. "scsi host busy %d failed %d\n",
  141. scsi_host_busy(cmd->device->host),
  142. cmd->device->host->host_failed);
  143. }
  144. }
  145. }
  146. #endif
  147. /**
  148. * scsi_finish_command - cleanup and pass command back to upper layer
  149. * @cmd: the command
  150. *
  151. * Description: Pass command off to upper layer for finishing of I/O
  152. * request, waking processes that are waiting on results,
  153. * etc.
  154. */
  155. void scsi_finish_command(struct scsi_cmnd *cmd)
  156. {
  157. struct scsi_device *sdev = cmd->device;
  158. struct scsi_target *starget = scsi_target(sdev);
  159. struct Scsi_Host *shost = sdev->host;
  160. struct scsi_driver *drv;
  161. unsigned int good_bytes;
  162. scsi_device_unbusy(sdev, cmd);
  163. /*
  164. * Clear the flags that say that the device/target/host is no longer
  165. * capable of accepting new commands.
  166. */
  167. if (atomic_read(&shost->host_blocked))
  168. atomic_set(&shost->host_blocked, 0);
  169. if (atomic_read(&starget->target_blocked))
  170. atomic_set(&starget->target_blocked, 0);
  171. if (atomic_read(&sdev->device_blocked))
  172. atomic_set(&sdev->device_blocked, 0);
  173. /*
  174. * If we have valid sense information, then some kind of recovery
  175. * must have taken place. Make a note of this.
  176. */
  177. if (SCSI_SENSE_VALID(cmd))
  178. cmd->result |= (DRIVER_SENSE << 24);
  179. SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
  180. "Notifying upper driver of completion "
  181. "(result %x)\n", cmd->result));
  182. good_bytes = scsi_bufflen(cmd);
  183. if (!blk_rq_is_passthrough(cmd->request)) {
  184. int old_good_bytes = good_bytes;
  185. drv = scsi_cmd_to_driver(cmd);
  186. if (drv->done)
  187. good_bytes = drv->done(cmd);
  188. /*
  189. * USB may not give sense identifying bad sector and
  190. * simply return a residue instead, so subtract off the
  191. * residue if drv->done() error processing indicates no
  192. * change to the completion length.
  193. */
  194. if (good_bytes == old_good_bytes)
  195. good_bytes -= scsi_get_resid(cmd);
  196. }
  197. scsi_io_completion(cmd, good_bytes);
  198. }
  199. /**
  200. * scsi_change_queue_depth - change a device's queue depth
  201. * @sdev: SCSI Device in question
  202. * @depth: number of commands allowed to be queued to the driver
  203. *
  204. * Sets the device queue depth and returns the new value.
  205. */
  206. int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
  207. {
  208. if (depth > 0) {
  209. sdev->queue_depth = depth;
  210. wmb();
  211. }
  212. if (sdev->request_queue)
  213. blk_set_queue_depth(sdev->request_queue, depth);
  214. return sdev->queue_depth;
  215. }
  216. EXPORT_SYMBOL(scsi_change_queue_depth);
  217. /**
  218. * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
  219. * @sdev: SCSI Device in question
  220. * @depth: Current number of outstanding SCSI commands on this device,
  221. * not counting the one returned as QUEUE_FULL.
  222. *
  223. * Description: This function will track successive QUEUE_FULL events on a
  224. * specific SCSI device to determine if and when there is a
  225. * need to adjust the queue depth on the device.
  226. *
  227. * Returns: 0 - No change needed, >0 - Adjust queue depth to this new depth,
  228. * -1 - Drop back to untagged operation using host->cmd_per_lun
  229. * as the untagged command depth
  230. *
  231. * Lock Status: None held on entry
  232. *
  233. * Notes: Low level drivers may call this at any time and we will do
  234. * "The Right Thing." We are interrupt context safe.
  235. */
  236. int scsi_track_queue_full(struct scsi_device *sdev, int depth)
  237. {
  238. /*
  239. * Don't let QUEUE_FULLs on the same
  240. * jiffies count, they could all be from
  241. * same event.
  242. */
  243. if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
  244. return 0;
  245. sdev->last_queue_full_time = jiffies;
  246. if (sdev->last_queue_full_depth != depth) {
  247. sdev->last_queue_full_count = 1;
  248. sdev->last_queue_full_depth = depth;
  249. } else {
  250. sdev->last_queue_full_count++;
  251. }
  252. if (sdev->last_queue_full_count <= 10)
  253. return 0;
  254. return scsi_change_queue_depth(sdev, depth);
  255. }
  256. EXPORT_SYMBOL(scsi_track_queue_full);
  257. /**
  258. * scsi_vpd_inquiry - Request a device provide us with a VPD page
  259. * @sdev: The device to ask
  260. * @buffer: Where to put the result
  261. * @page: Which Vital Product Data to return
  262. * @len: The length of the buffer
  263. *
  264. * This is an internal helper function. You probably want to use
  265. * scsi_get_vpd_page instead.
  266. *
  267. * Returns size of the vpd page on success or a negative error number.
  268. */
  269. static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
  270. u8 page, unsigned len)
  271. {
  272. int result;
  273. unsigned char cmd[16];
  274. if (len < 4)
  275. return -EINVAL;
  276. cmd[0] = INQUIRY;
  277. cmd[1] = 1; /* EVPD */
  278. cmd[2] = page;
  279. cmd[3] = len >> 8;
  280. cmd[4] = len & 0xff;
  281. cmd[5] = 0; /* Control byte */
  282. /*
  283. * I'm not convinced we need to try quite this hard to get VPD, but
  284. * all the existing users tried this hard.
  285. */
  286. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer,
  287. len, NULL, 30 * HZ, 3, NULL);
  288. if (result)
  289. return -EIO;
  290. /* Sanity check that we got the page back that we asked for */
  291. if (buffer[1] != page)
  292. return -EIO;
  293. return get_unaligned_be16(&buffer[2]) + 4;
  294. }
  295. /**
  296. * scsi_get_vpd_page - Get Vital Product Data from a SCSI device
  297. * @sdev: The device to ask
  298. * @page: Which Vital Product Data to return
  299. * @buf: where to store the VPD
  300. * @buf_len: number of bytes in the VPD buffer area
  301. *
  302. * SCSI devices may optionally supply Vital Product Data. Each 'page'
  303. * of VPD is defined in the appropriate SCSI document (eg SPC, SBC).
  304. * If the device supports this VPD page, this routine returns a pointer
  305. * to a buffer containing the data from that page. The caller is
  306. * responsible for calling kfree() on this pointer when it is no longer
  307. * needed. If we cannot retrieve the VPD page this routine returns %NULL.
  308. */
  309. int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
  310. int buf_len)
  311. {
  312. int i, result;
  313. if (sdev->skip_vpd_pages)
  314. goto fail;
  315. /* Ask for all the pages supported by this device */
  316. result = scsi_vpd_inquiry(sdev, buf, 0, buf_len);
  317. if (result < 4)
  318. goto fail;
  319. /* If the user actually wanted this page, we can skip the rest */
  320. if (page == 0)
  321. return 0;
  322. for (i = 4; i < min(result, buf_len); i++)
  323. if (buf[i] == page)
  324. goto found;
  325. if (i < result && i >= buf_len)
  326. /* ran off the end of the buffer, give us benefit of doubt */
  327. goto found;
  328. /* The device claims it doesn't support the requested page */
  329. goto fail;
  330. found:
  331. result = scsi_vpd_inquiry(sdev, buf, page, buf_len);
  332. if (result < 0)
  333. goto fail;
  334. return 0;
  335. fail:
  336. return -EINVAL;
  337. }
  338. EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
  339. /**
  340. * scsi_get_vpd_buf - Get Vital Product Data from a SCSI device
  341. * @sdev: The device to ask
  342. * @page: Which Vital Product Data to return
  343. *
  344. * Returns %NULL upon failure.
  345. */
  346. static struct scsi_vpd *scsi_get_vpd_buf(struct scsi_device *sdev, u8 page)
  347. {
  348. struct scsi_vpd *vpd_buf;
  349. int vpd_len = SCSI_VPD_PG_LEN, result;
  350. retry_pg:
  351. vpd_buf = kmalloc(sizeof(*vpd_buf) + vpd_len, GFP_KERNEL);
  352. if (!vpd_buf)
  353. return NULL;
  354. result = scsi_vpd_inquiry(sdev, vpd_buf->data, page, vpd_len);
  355. if (result < 0) {
  356. kfree(vpd_buf);
  357. return NULL;
  358. }
  359. if (result > vpd_len) {
  360. vpd_len = result;
  361. kfree(vpd_buf);
  362. goto retry_pg;
  363. }
  364. vpd_buf->len = result;
  365. return vpd_buf;
  366. }
  367. static void scsi_update_vpd_page(struct scsi_device *sdev, u8 page,
  368. struct scsi_vpd __rcu **sdev_vpd_buf)
  369. {
  370. struct scsi_vpd *vpd_buf;
  371. vpd_buf = scsi_get_vpd_buf(sdev, page);
  372. if (!vpd_buf)
  373. return;
  374. mutex_lock(&sdev->inquiry_mutex);
  375. vpd_buf = rcu_replace_pointer(*sdev_vpd_buf, vpd_buf,
  376. lockdep_is_held(&sdev->inquiry_mutex));
  377. mutex_unlock(&sdev->inquiry_mutex);
  378. if (vpd_buf)
  379. kfree_rcu(vpd_buf, rcu);
  380. }
  381. /**
  382. * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
  383. * @sdev: The device to ask
  384. *
  385. * Attach the 'Device Identification' VPD page (0x83) and the
  386. * 'Unit Serial Number' VPD page (0x80) to a SCSI device
  387. * structure. This information can be used to identify the device
  388. * uniquely.
  389. */
  390. void scsi_attach_vpd(struct scsi_device *sdev)
  391. {
  392. int i;
  393. struct scsi_vpd *vpd_buf;
  394. if (!scsi_device_supports_vpd(sdev))
  395. return;
  396. /* Ask for all the pages supported by this device */
  397. vpd_buf = scsi_get_vpd_buf(sdev, 0);
  398. if (!vpd_buf)
  399. return;
  400. for (i = 4; i < vpd_buf->len; i++) {
  401. if (vpd_buf->data[i] == 0x0)
  402. scsi_update_vpd_page(sdev, 0x0, &sdev->vpd_pg0);
  403. if (vpd_buf->data[i] == 0x80)
  404. scsi_update_vpd_page(sdev, 0x80, &sdev->vpd_pg80);
  405. if (vpd_buf->data[i] == 0x83)
  406. scsi_update_vpd_page(sdev, 0x83, &sdev->vpd_pg83);
  407. if (vpd_buf->data[i] == 0x89)
  408. scsi_update_vpd_page(sdev, 0x89, &sdev->vpd_pg89);
  409. }
  410. kfree(vpd_buf);
  411. }
  412. /**
  413. * scsi_report_opcode - Find out if a given command opcode is supported
  414. * @sdev: scsi device to query
  415. * @buffer: scratch buffer (must be at least 20 bytes long)
  416. * @len: length of buffer
  417. * @opcode: opcode for command to look up
  418. *
  419. * Uses the REPORT SUPPORTED OPERATION CODES to look up the given
  420. * opcode. Returns -EINVAL if RSOC fails, 0 if the command opcode is
  421. * unsupported and 1 if the device claims to support the command.
  422. */
  423. int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
  424. unsigned int len, unsigned char opcode)
  425. {
  426. unsigned char cmd[16];
  427. struct scsi_sense_hdr sshdr;
  428. int result;
  429. if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
  430. return -EINVAL;
  431. memset(cmd, 0, 16);
  432. cmd[0] = MAINTENANCE_IN;
  433. cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
  434. cmd[2] = 1; /* One command format */
  435. cmd[3] = opcode;
  436. put_unaligned_be32(len, &cmd[6]);
  437. memset(buffer, 0, len);
  438. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  439. &sshdr, 30 * HZ, 3, NULL);
  440. if (result && scsi_sense_valid(&sshdr) &&
  441. sshdr.sense_key == ILLEGAL_REQUEST &&
  442. (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
  443. return -EINVAL;
  444. if ((buffer[1] & 3) == 3) /* Command supported */
  445. return 1;
  446. return 0;
  447. }
  448. EXPORT_SYMBOL(scsi_report_opcode);
  449. /**
  450. * scsi_device_get - get an additional reference to a scsi_device
  451. * @sdev: device to get a reference to
  452. *
  453. * Description: Gets a reference to the scsi_device and increments the use count
  454. * of the underlying LLDD module. You must hold host_lock of the
  455. * parent Scsi_Host or already have a reference when calling this.
  456. *
  457. * This will fail if a device is deleted or cancelled, or when the LLD module
  458. * is in the process of being unloaded.
  459. */
  460. int scsi_device_get(struct scsi_device *sdev)
  461. {
  462. if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
  463. goto fail;
  464. if (!get_device(&sdev->sdev_gendev))
  465. goto fail;
  466. if (!try_module_get(sdev->host->hostt->module))
  467. goto fail_put_device;
  468. return 0;
  469. fail_put_device:
  470. put_device(&sdev->sdev_gendev);
  471. fail:
  472. return -ENXIO;
  473. }
  474. EXPORT_SYMBOL(scsi_device_get);
  475. /**
  476. * scsi_device_put - release a reference to a scsi_device
  477. * @sdev: device to release a reference on.
  478. *
  479. * Description: Release a reference to the scsi_device and decrements the use
  480. * count of the underlying LLDD module. The device is freed once the last
  481. * user vanishes.
  482. */
  483. void scsi_device_put(struct scsi_device *sdev)
  484. {
  485. struct module *mod = sdev->host->hostt->module;
  486. put_device(&sdev->sdev_gendev);
  487. module_put(mod);
  488. }
  489. EXPORT_SYMBOL(scsi_device_put);
  490. /* helper for shost_for_each_device, see that for documentation */
  491. struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
  492. struct scsi_device *prev)
  493. {
  494. struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
  495. struct scsi_device *next = NULL;
  496. unsigned long flags;
  497. spin_lock_irqsave(shost->host_lock, flags);
  498. while (list->next != &shost->__devices) {
  499. next = list_entry(list->next, struct scsi_device, siblings);
  500. /* skip devices that we can't get a reference to */
  501. if (!scsi_device_get(next))
  502. break;
  503. next = NULL;
  504. list = list->next;
  505. }
  506. spin_unlock_irqrestore(shost->host_lock, flags);
  507. if (prev)
  508. scsi_device_put(prev);
  509. return next;
  510. }
  511. EXPORT_SYMBOL(__scsi_iterate_devices);
  512. /**
  513. * starget_for_each_device - helper to walk all devices of a target
  514. * @starget: target whose devices we want to iterate over.
  515. * @data: Opaque passed to each function call.
  516. * @fn: Function to call on each device
  517. *
  518. * This traverses over each device of @starget. The devices have
  519. * a reference that must be released by scsi_host_put when breaking
  520. * out of the loop.
  521. */
  522. void starget_for_each_device(struct scsi_target *starget, void *data,
  523. void (*fn)(struct scsi_device *, void *))
  524. {
  525. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  526. struct scsi_device *sdev;
  527. shost_for_each_device(sdev, shost) {
  528. if ((sdev->channel == starget->channel) &&
  529. (sdev->id == starget->id))
  530. fn(sdev, data);
  531. }
  532. }
  533. EXPORT_SYMBOL(starget_for_each_device);
  534. /**
  535. * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
  536. * @starget: target whose devices we want to iterate over.
  537. * @data: parameter for callback @fn()
  538. * @fn: callback function that is invoked for each device
  539. *
  540. * This traverses over each device of @starget. It does _not_
  541. * take a reference on the scsi_device, so the whole loop must be
  542. * protected by shost->host_lock.
  543. *
  544. * Note: The only reason why drivers would want to use this is because
  545. * they need to access the device list in irq context. Otherwise you
  546. * really want to use starget_for_each_device instead.
  547. **/
  548. void __starget_for_each_device(struct scsi_target *starget, void *data,
  549. void (*fn)(struct scsi_device *, void *))
  550. {
  551. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  552. struct scsi_device *sdev;
  553. __shost_for_each_device(sdev, shost) {
  554. if ((sdev->channel == starget->channel) &&
  555. (sdev->id == starget->id))
  556. fn(sdev, data);
  557. }
  558. }
  559. EXPORT_SYMBOL(__starget_for_each_device);
  560. /**
  561. * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
  562. * @starget: SCSI target pointer
  563. * @lun: SCSI Logical Unit Number
  564. *
  565. * Description: Looks up the scsi_device with the specified @lun for a given
  566. * @starget. The returned scsi_device does not have an additional
  567. * reference. You must hold the host's host_lock over this call and
  568. * any access to the returned scsi_device. A scsi_device in state
  569. * SDEV_DEL is skipped.
  570. *
  571. * Note: The only reason why drivers should use this is because
  572. * they need to access the device list in irq context. Otherwise you
  573. * really want to use scsi_device_lookup_by_target instead.
  574. **/
  575. struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
  576. u64 lun)
  577. {
  578. struct scsi_device *sdev;
  579. list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
  580. if (sdev->sdev_state == SDEV_DEL)
  581. continue;
  582. if (sdev->lun ==lun)
  583. return sdev;
  584. }
  585. return NULL;
  586. }
  587. EXPORT_SYMBOL(__scsi_device_lookup_by_target);
  588. /**
  589. * scsi_device_lookup_by_target - find a device given the target
  590. * @starget: SCSI target pointer
  591. * @lun: SCSI Logical Unit Number
  592. *
  593. * Description: Looks up the scsi_device with the specified @lun for a given
  594. * @starget. The returned scsi_device has an additional reference that
  595. * needs to be released with scsi_device_put once you're done with it.
  596. **/
  597. struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
  598. u64 lun)
  599. {
  600. struct scsi_device *sdev;
  601. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  602. unsigned long flags;
  603. spin_lock_irqsave(shost->host_lock, flags);
  604. sdev = __scsi_device_lookup_by_target(starget, lun);
  605. if (sdev && scsi_device_get(sdev))
  606. sdev = NULL;
  607. spin_unlock_irqrestore(shost->host_lock, flags);
  608. return sdev;
  609. }
  610. EXPORT_SYMBOL(scsi_device_lookup_by_target);
  611. /**
  612. * __scsi_device_lookup - find a device given the host (UNLOCKED)
  613. * @shost: SCSI host pointer
  614. * @channel: SCSI channel (zero if only one channel)
  615. * @id: SCSI target number (physical unit number)
  616. * @lun: SCSI Logical Unit Number
  617. *
  618. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  619. * for a given host. The returned scsi_device does not have an additional
  620. * reference. You must hold the host's host_lock over this call and any access
  621. * to the returned scsi_device.
  622. *
  623. * Note: The only reason why drivers would want to use this is because
  624. * they need to access the device list in irq context. Otherwise you
  625. * really want to use scsi_device_lookup instead.
  626. **/
  627. struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
  628. uint channel, uint id, u64 lun)
  629. {
  630. struct scsi_device *sdev;
  631. list_for_each_entry(sdev, &shost->__devices, siblings) {
  632. if (sdev->sdev_state == SDEV_DEL)
  633. continue;
  634. if (sdev->channel == channel && sdev->id == id &&
  635. sdev->lun ==lun)
  636. return sdev;
  637. }
  638. return NULL;
  639. }
  640. EXPORT_SYMBOL(__scsi_device_lookup);
  641. /**
  642. * scsi_device_lookup - find a device given the host
  643. * @shost: SCSI host pointer
  644. * @channel: SCSI channel (zero if only one channel)
  645. * @id: SCSI target number (physical unit number)
  646. * @lun: SCSI Logical Unit Number
  647. *
  648. * Description: Looks up the scsi_device with the specified @channel, @id, @lun
  649. * for a given host. The returned scsi_device has an additional reference that
  650. * needs to be released with scsi_device_put once you're done with it.
  651. **/
  652. struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
  653. uint channel, uint id, u64 lun)
  654. {
  655. struct scsi_device *sdev;
  656. unsigned long flags;
  657. spin_lock_irqsave(shost->host_lock, flags);
  658. sdev = __scsi_device_lookup(shost, channel, id, lun);
  659. if (sdev && scsi_device_get(sdev))
  660. sdev = NULL;
  661. spin_unlock_irqrestore(shost->host_lock, flags);
  662. return sdev;
  663. }
  664. EXPORT_SYMBOL(scsi_device_lookup);
  665. MODULE_DESCRIPTION("SCSI core");
  666. MODULE_LICENSE("GPL");
  667. module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
  668. MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
  669. static int __init init_scsi(void)
  670. {
  671. int error;
  672. error = scsi_init_procfs();
  673. if (error)
  674. goto cleanup_queue;
  675. error = scsi_init_devinfo();
  676. if (error)
  677. goto cleanup_procfs;
  678. error = scsi_init_hosts();
  679. if (error)
  680. goto cleanup_devlist;
  681. error = scsi_init_sysctl();
  682. if (error)
  683. goto cleanup_hosts;
  684. error = scsi_sysfs_register();
  685. if (error)
  686. goto cleanup_sysctl;
  687. scsi_netlink_init();
  688. printk(KERN_NOTICE "SCSI subsystem initialized\n");
  689. return 0;
  690. cleanup_sysctl:
  691. scsi_exit_sysctl();
  692. cleanup_hosts:
  693. scsi_exit_hosts();
  694. cleanup_devlist:
  695. scsi_exit_devinfo();
  696. cleanup_procfs:
  697. scsi_exit_procfs();
  698. cleanup_queue:
  699. scsi_exit_queue();
  700. printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
  701. -error);
  702. return error;
  703. }
  704. static void __exit exit_scsi(void)
  705. {
  706. scsi_netlink_exit();
  707. scsi_sysfs_unregister();
  708. scsi_exit_sysctl();
  709. scsi_exit_hosts();
  710. scsi_exit_devinfo();
  711. scsi_exit_procfs();
  712. scsi_exit_queue();
  713. }
  714. subsys_initcall(init_scsi);
  715. module_exit(exit_scsi);