scsi.c 23 KB

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