i2o_block.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Block OSM
  3. *
  4. * Copyright (C) 1999-2002 Red Hat Software
  5. *
  6. * Written by Alan Cox, Building Number Three Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * For the purpose of avoiding doubt the preferred form of the work
  19. * for making modifications shall be a standards compliant form such
  20. * gzipped tar and not one requiring a proprietary or patent encumbered
  21. * tool to unpack.
  22. *
  23. * Fixes/additions:
  24. * Steve Ralston:
  25. * Multiple device handling error fixes,
  26. * Added a queue depth.
  27. * Alan Cox:
  28. * FC920 has an rmw bug. Dont or in the end marker.
  29. * Removed queue walk, fixed for 64bitness.
  30. * Rewrote much of the code over time
  31. * Added indirect block lists
  32. * Handle 64K limits on many controllers
  33. * Don't use indirects on the Promise (breaks)
  34. * Heavily chop down the queue depths
  35. * Deepak Saxena:
  36. * Independent queues per IOP
  37. * Support for dynamic device creation/deletion
  38. * Code cleanup
  39. * Support for larger I/Os through merge* functions
  40. * (taken from DAC960 driver)
  41. * Boji T Kannanthanam:
  42. * Set the I2O Block devices to be detected in increasing
  43. * order of TIDs during boot.
  44. * Search and set the I2O block device that we boot off
  45. * from as the first device to be claimed (as /dev/i2o/hda)
  46. * Properly attach/detach I2O gendisk structure from the
  47. * system gendisk list. The I2O block devices now appear in
  48. * /proc/partitions.
  49. * Markus Lidel <Markus.Lidel@shadowconnect.com>:
  50. * Minor bugfixes for 2.6.
  51. */
  52. #include <linux/module.h>
  53. #include <linux/i2o.h>
  54. #include <linux/mempool.h>
  55. #include <linux/genhd.h>
  56. #include <linux/blkdev.h>
  57. #include <linux/hdreg.h>
  58. #include "i2o_block.h"
  59. #define OSM_NAME "block-osm"
  60. #define OSM_VERSION "1.287"
  61. #define OSM_DESCRIPTION "I2O Block Device OSM"
  62. static struct i2o_driver i2o_block_driver;
  63. /* global Block OSM request mempool */
  64. static struct i2o_block_mempool i2o_blk_req_pool;
  65. /* Block OSM class handling definition */
  66. static struct i2o_class_id i2o_block_class_id[] = {
  67. {I2O_CLASS_RANDOM_BLOCK_STORAGE},
  68. {I2O_CLASS_END}
  69. };
  70. /**
  71. * i2o_block_device_free - free the memory of the I2O Block device
  72. * @dev: I2O Block device, which should be cleaned up
  73. *
  74. * Frees the request queue, gendisk and the i2o_block_device structure.
  75. */
  76. static void i2o_block_device_free(struct i2o_block_device *dev)
  77. {
  78. blk_cleanup_queue(dev->gd->queue);
  79. put_disk(dev->gd);
  80. kfree(dev);
  81. };
  82. /**
  83. * i2o_block_remove - remove the I2O Block device from the system again
  84. * @dev: I2O Block device which should be removed
  85. *
  86. * Remove gendisk from system and free all allocated memory.
  87. *
  88. * Always returns 0.
  89. */
  90. static int i2o_block_remove(struct device *dev)
  91. {
  92. struct i2o_device *i2o_dev = to_i2o_device(dev);
  93. struct i2o_block_device *i2o_blk_dev = dev_get_drvdata(dev);
  94. osm_info("device removed (TID: %03x): %s\n", i2o_dev->lct_data.tid,
  95. i2o_blk_dev->gd->disk_name);
  96. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0);
  97. del_gendisk(i2o_blk_dev->gd);
  98. dev_set_drvdata(dev, NULL);
  99. i2o_device_claim_release(i2o_dev);
  100. i2o_block_device_free(i2o_blk_dev);
  101. return 0;
  102. };
  103. /**
  104. * i2o_block_device flush - Flush all dirty data of I2O device dev
  105. * @dev: I2O device which should be flushed
  106. *
  107. * Flushes all dirty data on device dev.
  108. *
  109. * Returns 0 on success or negative error code on failure.
  110. */
  111. static int i2o_block_device_flush(struct i2o_device *dev)
  112. {
  113. struct i2o_message *msg;
  114. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  115. if (IS_ERR(msg))
  116. return PTR_ERR(msg);
  117. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  118. msg->u.head[1] =
  119. cpu_to_le32(I2O_CMD_BLOCK_CFLUSH << 24 | HOST_TID << 12 | dev->
  120. lct_data.tid);
  121. msg->body[0] = cpu_to_le32(60 << 16);
  122. osm_debug("Flushing...\n");
  123. return i2o_msg_post_wait(dev->iop, msg, 60);
  124. };
  125. /**
  126. * i2o_block_issue_flush - device-flush interface for block-layer
  127. * @queue: the request queue of the device which should be flushed
  128. * @disk: gendisk
  129. * @error_sector: error offset
  130. *
  131. * Helper function to provide flush functionality to block-layer.
  132. *
  133. * Returns 0 on success or negative error code on failure.
  134. */
  135. static int i2o_block_issue_flush(request_queue_t * queue, struct gendisk *disk,
  136. sector_t * error_sector)
  137. {
  138. struct i2o_block_device *i2o_blk_dev = queue->queuedata;
  139. int rc = -ENODEV;
  140. if (likely(i2o_blk_dev))
  141. rc = i2o_block_device_flush(i2o_blk_dev->i2o_dev);
  142. return rc;
  143. }
  144. /**
  145. * i2o_block_device_mount - Mount (load) the media of device dev
  146. * @dev: I2O device which should receive the mount request
  147. * @media_id: Media Identifier
  148. *
  149. * Load a media into drive. Identifier should be set to -1, because the
  150. * spec does not support any other value.
  151. *
  152. * Returns 0 on success or negative error code on failure.
  153. */
  154. static int i2o_block_device_mount(struct i2o_device *dev, u32 media_id)
  155. {
  156. struct i2o_message *msg;
  157. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  158. if (IS_ERR(msg))
  159. return PTR_ERR(msg);
  160. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  161. msg->u.head[1] =
  162. cpu_to_le32(I2O_CMD_BLOCK_MMOUNT << 24 | HOST_TID << 12 | dev->
  163. lct_data.tid);
  164. msg->body[0] = cpu_to_le32(-1);
  165. msg->body[1] = cpu_to_le32(0x00000000);
  166. osm_debug("Mounting...\n");
  167. return i2o_msg_post_wait(dev->iop, msg, 2);
  168. };
  169. /**
  170. * i2o_block_device_lock - Locks the media of device dev
  171. * @dev: I2O device which should receive the lock request
  172. * @media_id: Media Identifier
  173. *
  174. * Lock media of device dev to prevent removal. The media identifier
  175. * should be set to -1, because the spec does not support any other value.
  176. *
  177. * Returns 0 on success or negative error code on failure.
  178. */
  179. static int i2o_block_device_lock(struct i2o_device *dev, u32 media_id)
  180. {
  181. struct i2o_message *msg;
  182. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  183. if (IS_ERR(msg) == I2O_QUEUE_EMPTY)
  184. return PTR_ERR(msg);
  185. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  186. msg->u.head[1] =
  187. cpu_to_le32(I2O_CMD_BLOCK_MLOCK << 24 | HOST_TID << 12 | dev->
  188. lct_data.tid);
  189. msg->body[0] = cpu_to_le32(-1);
  190. osm_debug("Locking...\n");
  191. return i2o_msg_post_wait(dev->iop, msg, 2);
  192. };
  193. /**
  194. * i2o_block_device_unlock - Unlocks the media of device dev
  195. * @dev: I2O device which should receive the unlocked request
  196. * @media_id: Media Identifier
  197. *
  198. * Unlocks the media in device dev. The media identifier should be set to
  199. * -1, because the spec does not support any other value.
  200. *
  201. * Returns 0 on success or negative error code on failure.
  202. */
  203. static int i2o_block_device_unlock(struct i2o_device *dev, u32 media_id)
  204. {
  205. struct i2o_message *msg;
  206. msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
  207. if (IS_ERR(msg))
  208. return PTR_ERR(msg);
  209. msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
  210. msg->u.head[1] =
  211. cpu_to_le32(I2O_CMD_BLOCK_MUNLOCK << 24 | HOST_TID << 12 | dev->
  212. lct_data.tid);
  213. msg->body[0] = cpu_to_le32(media_id);
  214. osm_debug("Unlocking...\n");
  215. return i2o_msg_post_wait(dev->iop, msg, 2);
  216. };
  217. /**
  218. * i2o_block_device_power - Power management for device dev
  219. * @dev: I2O device which should receive the power management request
  220. * @operation: Operation which should be send
  221. *
  222. * Send a power management request to the device dev.
  223. *
  224. * Returns 0 on success or negative error code on failure.
  225. */
  226. static int i2o_block_device_power(struct i2o_block_device *dev, u8 op)
  227. {
  228. struct i2o_device *i2o_dev = dev->i2o_dev;
  229. struct i2o_controller *c = i2o_dev->iop;
  230. struct i2o_message *msg;
  231. int rc;
  232. msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
  233. if (IS_ERR(msg))
  234. return PTR_ERR(msg);
  235. msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
  236. msg->u.head[1] =
  237. cpu_to_le32(I2O_CMD_BLOCK_POWER << 24 | HOST_TID << 12 | i2o_dev->
  238. lct_data.tid);
  239. msg->body[0] = cpu_to_le32(op << 24);
  240. osm_debug("Power...\n");
  241. rc = i2o_msg_post_wait(c, msg, 60);
  242. if (!rc)
  243. dev->power = op;
  244. return rc;
  245. };
  246. /**
  247. * i2o_block_request_alloc - Allocate an I2O block request struct
  248. *
  249. * Allocates an I2O block request struct and initialize the list.
  250. *
  251. * Returns a i2o_block_request pointer on success or negative error code
  252. * on failure.
  253. */
  254. static inline struct i2o_block_request *i2o_block_request_alloc(void)
  255. {
  256. struct i2o_block_request *ireq;
  257. ireq = mempool_alloc(i2o_blk_req_pool.pool, GFP_ATOMIC);
  258. if (!ireq)
  259. return ERR_PTR(-ENOMEM);
  260. INIT_LIST_HEAD(&ireq->queue);
  261. return ireq;
  262. };
  263. /**
  264. * i2o_block_request_free - Frees a I2O block request
  265. * @ireq: I2O block request which should be freed
  266. *
  267. * Fres the allocated memory (give it back to the request mempool).
  268. */
  269. static inline void i2o_block_request_free(struct i2o_block_request *ireq)
  270. {
  271. mempool_free(ireq, i2o_blk_req_pool.pool);
  272. };
  273. /**
  274. * i2o_block_sglist_alloc - Allocate the SG list and map it
  275. * @c: I2O controller to which the request belongs
  276. * @ireq: I2O block request
  277. *
  278. * Builds the SG list and map it to be accessable by the controller.
  279. *
  280. * Returns 0 on failure or 1 on success.
  281. */
  282. static inline int i2o_block_sglist_alloc(struct i2o_controller *c,
  283. struct i2o_block_request *ireq,
  284. u32 ** mptr)
  285. {
  286. int nents;
  287. enum dma_data_direction direction;
  288. ireq->dev = &c->pdev->dev;
  289. nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);
  290. if (rq_data_dir(ireq->req) == READ)
  291. direction = PCI_DMA_FROMDEVICE;
  292. else
  293. direction = PCI_DMA_TODEVICE;
  294. ireq->sg_nents = nents;
  295. return i2o_dma_map_sg(c, ireq->sg_table, nents, direction, mptr);
  296. };
  297. /**
  298. * i2o_block_sglist_free - Frees the SG list
  299. * @ireq: I2O block request from which the SG should be freed
  300. *
  301. * Frees the SG list from the I2O block request.
  302. */
  303. static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
  304. {
  305. enum dma_data_direction direction;
  306. if (rq_data_dir(ireq->req) == READ)
  307. direction = PCI_DMA_FROMDEVICE;
  308. else
  309. direction = PCI_DMA_TODEVICE;
  310. dma_unmap_sg(ireq->dev, ireq->sg_table, ireq->sg_nents, direction);
  311. };
  312. /**
  313. * i2o_block_prep_req_fn - Allocates I2O block device specific struct
  314. * @q: request queue for the request
  315. * @req: the request to prepare
  316. *
  317. * Allocate the necessary i2o_block_request struct and connect it to
  318. * the request. This is needed that we not loose the SG list later on.
  319. *
  320. * Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
  321. */
  322. static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
  323. {
  324. struct i2o_block_device *i2o_blk_dev = q->queuedata;
  325. struct i2o_block_request *ireq;
  326. if (unlikely(!i2o_blk_dev)) {
  327. osm_err("block device already removed\n");
  328. return BLKPREP_KILL;
  329. }
  330. /* request is already processed by us, so return */
  331. if (req->flags & REQ_SPECIAL) {
  332. osm_debug("REQ_SPECIAL already set!\n");
  333. req->flags |= REQ_DONTPREP;
  334. return BLKPREP_OK;
  335. }
  336. /* connect the i2o_block_request to the request */
  337. if (!req->special) {
  338. ireq = i2o_block_request_alloc();
  339. if (unlikely(IS_ERR(ireq))) {
  340. osm_debug("unable to allocate i2o_block_request!\n");
  341. return BLKPREP_DEFER;
  342. }
  343. ireq->i2o_blk_dev = i2o_blk_dev;
  344. req->special = ireq;
  345. ireq->req = req;
  346. } else
  347. ireq = req->special;
  348. /* do not come back here */
  349. req->flags |= REQ_DONTPREP | REQ_SPECIAL;
  350. return BLKPREP_OK;
  351. };
  352. /**
  353. * i2o_block_delayed_request_fn - delayed request queue function
  354. * delayed_request: the delayed request with the queue to start
  355. *
  356. * If the request queue is stopped for a disk, and there is no open
  357. * request, a new event is created, which calls this function to start
  358. * the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
  359. * be started again.
  360. */
  361. static void i2o_block_delayed_request_fn(void *delayed_request)
  362. {
  363. struct i2o_block_delayed_request *dreq = delayed_request;
  364. struct request_queue *q = dreq->queue;
  365. unsigned long flags;
  366. spin_lock_irqsave(q->queue_lock, flags);
  367. blk_start_queue(q);
  368. spin_unlock_irqrestore(q->queue_lock, flags);
  369. kfree(dreq);
  370. };
  371. /**
  372. * i2o_block_end_request - Post-processing of completed commands
  373. * @req: request which should be completed
  374. * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
  375. * @nr_bytes: number of bytes to complete
  376. *
  377. * Mark the request as complete. The lock must not be held when entering.
  378. *
  379. */
  380. static void i2o_block_end_request(struct request *req, int uptodate,
  381. int nr_bytes)
  382. {
  383. struct i2o_block_request *ireq = req->special;
  384. struct i2o_block_device *dev = ireq->i2o_blk_dev;
  385. request_queue_t *q = req->q;
  386. unsigned long flags;
  387. if (end_that_request_chunk(req, uptodate, nr_bytes)) {
  388. int leftover = (req->hard_nr_sectors << KERNEL_SECTOR_SHIFT);
  389. if (blk_pc_request(req))
  390. leftover = req->data_len;
  391. if (end_io_error(uptodate))
  392. end_that_request_chunk(req, 0, leftover);
  393. }
  394. add_disk_randomness(req->rq_disk);
  395. spin_lock_irqsave(q->queue_lock, flags);
  396. end_that_request_last(req);
  397. if (likely(dev)) {
  398. dev->open_queue_depth--;
  399. list_del(&ireq->queue);
  400. }
  401. blk_start_queue(q);
  402. spin_unlock_irqrestore(q->queue_lock, flags);
  403. i2o_block_sglist_free(ireq);
  404. i2o_block_request_free(ireq);
  405. };
  406. /**
  407. * i2o_block_reply - Block OSM reply handler.
  408. * @c: I2O controller from which the message arrives
  409. * @m: message id of reply
  410. * qmsg: the actuall I2O message reply
  411. *
  412. * This function gets all the message replies.
  413. *
  414. */
  415. static int i2o_block_reply(struct i2o_controller *c, u32 m,
  416. struct i2o_message *msg)
  417. {
  418. struct request *req;
  419. int uptodate = 1;
  420. req = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
  421. if (unlikely(!req)) {
  422. osm_err("NULL reply received!\n");
  423. return -1;
  424. }
  425. /*
  426. * Lets see what is cooking. We stuffed the
  427. * request in the context.
  428. */
  429. if ((le32_to_cpu(msg->body[0]) >> 24) != 0) {
  430. u32 status = le32_to_cpu(msg->body[0]);
  431. /*
  432. * Device not ready means two things. One is that the
  433. * the thing went offline (but not a removal media)
  434. *
  435. * The second is that you have a SuperTrak 100 and the
  436. * firmware got constipated. Unlike standard i2o card
  437. * setups the supertrak returns an error rather than
  438. * blocking for the timeout in these cases.
  439. *
  440. * Don't stick a supertrak100 into cache aggressive modes
  441. */
  442. osm_err("TID %03x error status: 0x%02x, detailed status: "
  443. "0x%04x\n", (le32_to_cpu(msg->u.head[1]) >> 12 & 0xfff),
  444. status >> 24, status & 0xffff);
  445. req->errors++;
  446. uptodate = 0;
  447. }
  448. i2o_block_end_request(req, uptodate, le32_to_cpu(msg->body[1]));
  449. return 1;
  450. };
  451. static void i2o_block_event(struct i2o_event *evt)
  452. {
  453. osm_debug("event received\n");
  454. kfree(evt);
  455. };
  456. /*
  457. * SCSI-CAM for ioctl geometry mapping
  458. * Duplicated with SCSI - this should be moved into somewhere common
  459. * perhaps genhd ?
  460. *
  461. * LBA -> CHS mapping table taken from:
  462. *
  463. * "Incorporating the I2O Architecture into BIOS for Intel Architecture
  464. * Platforms"
  465. *
  466. * This is an I2O document that is only available to I2O members,
  467. * not developers.
  468. *
  469. * From my understanding, this is how all the I2O cards do this
  470. *
  471. * Disk Size | Sectors | Heads | Cylinders
  472. * ---------------+---------+-------+-------------------
  473. * 1 < X <= 528M | 63 | 16 | X/(63 * 16 * 512)
  474. * 528M < X <= 1G | 63 | 32 | X/(63 * 32 * 512)
  475. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  476. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  477. *
  478. */
  479. #define BLOCK_SIZE_528M 1081344
  480. #define BLOCK_SIZE_1G 2097152
  481. #define BLOCK_SIZE_21G 4403200
  482. #define BLOCK_SIZE_42G 8806400
  483. #define BLOCK_SIZE_84G 17612800
  484. static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
  485. unsigned char *hds, unsigned char *secs)
  486. {
  487. unsigned long heads, sectors, cylinders;
  488. sectors = 63L; /* Maximize sectors per track */
  489. if (capacity <= BLOCK_SIZE_528M)
  490. heads = 16;
  491. else if (capacity <= BLOCK_SIZE_1G)
  492. heads = 32;
  493. else if (capacity <= BLOCK_SIZE_21G)
  494. heads = 64;
  495. else if (capacity <= BLOCK_SIZE_42G)
  496. heads = 128;
  497. else
  498. heads = 255;
  499. cylinders = (unsigned long)capacity / (heads * sectors);
  500. *cyls = (unsigned short)cylinders; /* Stuff return values */
  501. *secs = (unsigned char)sectors;
  502. *hds = (unsigned char)heads;
  503. }
  504. /**
  505. * i2o_block_open - Open the block device
  506. *
  507. * Power up the device, mount and lock the media. This function is called,
  508. * if the block device is opened for access.
  509. *
  510. * Returns 0 on success or negative error code on failure.
  511. */
  512. static int i2o_block_open(struct inode *inode, struct file *file)
  513. {
  514. struct i2o_block_device *dev = inode->i_bdev->bd_disk->private_data;
  515. if (!dev->i2o_dev)
  516. return -ENODEV;
  517. if (dev->power > 0x1f)
  518. i2o_block_device_power(dev, 0x02);
  519. i2o_block_device_mount(dev->i2o_dev, -1);
  520. i2o_block_device_lock(dev->i2o_dev, -1);
  521. osm_debug("Ready.\n");
  522. return 0;
  523. };
  524. /**
  525. * i2o_block_release - Release the I2O block device
  526. *
  527. * Unlock and unmount the media, and power down the device. Gets called if
  528. * the block device is closed.
  529. *
  530. * Returns 0 on success or negative error code on failure.
  531. */
  532. static int i2o_block_release(struct inode *inode, struct file *file)
  533. {
  534. struct gendisk *disk = inode->i_bdev->bd_disk;
  535. struct i2o_block_device *dev = disk->private_data;
  536. u8 operation;
  537. /*
  538. * This is to deail with the case of an application
  539. * opening a device and then the device dissapears while
  540. * it's in use, and then the application tries to release
  541. * it. ex: Unmounting a deleted RAID volume at reboot.
  542. * If we send messages, it will just cause FAILs since
  543. * the TID no longer exists.
  544. */
  545. if (!dev->i2o_dev)
  546. return 0;
  547. i2o_block_device_flush(dev->i2o_dev);
  548. i2o_block_device_unlock(dev->i2o_dev, -1);
  549. if (dev->flags & (1 << 3 | 1 << 4)) /* Removable */
  550. operation = 0x21;
  551. else
  552. operation = 0x24;
  553. i2o_block_device_power(dev, operation);
  554. return 0;
  555. }
  556. /**
  557. * i2o_block_ioctl - Issue device specific ioctl calls.
  558. * @cmd: ioctl command
  559. * @arg: arg
  560. *
  561. * Handles ioctl request for the block device.
  562. *
  563. * Return 0 on success or negative error on failure.
  564. */
  565. static int i2o_block_ioctl(struct inode *inode, struct file *file,
  566. unsigned int cmd, unsigned long arg)
  567. {
  568. struct gendisk *disk = inode->i_bdev->bd_disk;
  569. struct i2o_block_device *dev = disk->private_data;
  570. void __user *argp = (void __user *)arg;
  571. /* Anyone capable of this syscall can do *real bad* things */
  572. if (!capable(CAP_SYS_ADMIN))
  573. return -EPERM;
  574. switch (cmd) {
  575. case HDIO_GETGEO:
  576. {
  577. struct hd_geometry g;
  578. i2o_block_biosparam(get_capacity(disk),
  579. &g.cylinders, &g.heads, &g.sectors);
  580. g.start = get_start_sect(inode->i_bdev);
  581. return copy_to_user(argp, &g, sizeof(g)) ? -EFAULT : 0;
  582. }
  583. case BLKI2OGRSTRAT:
  584. return put_user(dev->rcache, (int __user *)arg);
  585. case BLKI2OGWSTRAT:
  586. return put_user(dev->wcache, (int __user *)arg);
  587. case BLKI2OSRSTRAT:
  588. if (arg < 0 || arg > CACHE_SMARTFETCH)
  589. return -EINVAL;
  590. dev->rcache = arg;
  591. break;
  592. case BLKI2OSWSTRAT:
  593. if (arg != 0
  594. && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
  595. return -EINVAL;
  596. dev->wcache = arg;
  597. break;
  598. }
  599. return -ENOTTY;
  600. };
  601. /**
  602. * i2o_block_media_changed - Have we seen a media change?
  603. * @disk: gendisk which should be verified
  604. *
  605. * Verifies if the media has changed.
  606. *
  607. * Returns 1 if the media was changed or 0 otherwise.
  608. */
  609. static int i2o_block_media_changed(struct gendisk *disk)
  610. {
  611. struct i2o_block_device *p = disk->private_data;
  612. if (p->media_change_flag) {
  613. p->media_change_flag = 0;
  614. return 1;
  615. }
  616. return 0;
  617. }
  618. /**
  619. * i2o_block_transfer - Transfer a request to/from the I2O controller
  620. * @req: the request which should be transfered
  621. *
  622. * This function converts the request into a I2O message. The necessary
  623. * DMA buffers are allocated and after everything is setup post the message
  624. * to the I2O controller. No cleanup is done by this function. It is done
  625. * on the interrupt side when the reply arrives.
  626. *
  627. * Return 0 on success or negative error code on failure.
  628. */
  629. static int i2o_block_transfer(struct request *req)
  630. {
  631. struct i2o_block_device *dev = req->rq_disk->private_data;
  632. struct i2o_controller *c;
  633. int tid = dev->i2o_dev->lct_data.tid;
  634. struct i2o_message *msg;
  635. u32 *mptr;
  636. struct i2o_block_request *ireq = req->special;
  637. u32 tcntxt;
  638. u32 sgl_offset = SGL_OFFSET_8;
  639. u32 ctl_flags = 0x00000000;
  640. int rc;
  641. u32 cmd;
  642. if (unlikely(!dev->i2o_dev)) {
  643. osm_err("transfer to removed drive\n");
  644. rc = -ENODEV;
  645. goto exit;
  646. }
  647. c = dev->i2o_dev->iop;
  648. msg = i2o_msg_get(c);
  649. if (IS_ERR(msg)) {
  650. rc = PTR_ERR(msg);
  651. goto exit;
  652. }
  653. tcntxt = i2o_cntxt_list_add(c, req);
  654. if (!tcntxt) {
  655. rc = -ENOMEM;
  656. goto nop_msg;
  657. }
  658. msg->u.s.icntxt = cpu_to_le32(i2o_block_driver.context);
  659. msg->u.s.tcntxt = cpu_to_le32(tcntxt);
  660. mptr = &msg->body[0];
  661. if (rq_data_dir(req) == READ) {
  662. cmd = I2O_CMD_BLOCK_READ << 24;
  663. switch (dev->rcache) {
  664. case CACHE_PREFETCH:
  665. ctl_flags = 0x201F0008;
  666. break;
  667. case CACHE_SMARTFETCH:
  668. if (req->nr_sectors > 16)
  669. ctl_flags = 0x201F0008;
  670. else
  671. ctl_flags = 0x001F0000;
  672. break;
  673. default:
  674. break;
  675. }
  676. } else {
  677. cmd = I2O_CMD_BLOCK_WRITE << 24;
  678. switch (dev->wcache) {
  679. case CACHE_WRITETHROUGH:
  680. ctl_flags = 0x001F0008;
  681. break;
  682. case CACHE_WRITEBACK:
  683. ctl_flags = 0x001F0010;
  684. break;
  685. case CACHE_SMARTBACK:
  686. if (req->nr_sectors > 16)
  687. ctl_flags = 0x001F0004;
  688. else
  689. ctl_flags = 0x001F0010;
  690. break;
  691. case CACHE_SMARTTHROUGH:
  692. if (req->nr_sectors > 16)
  693. ctl_flags = 0x001F0004;
  694. else
  695. ctl_flags = 0x001F0010;
  696. default:
  697. break;
  698. }
  699. }
  700. #ifdef CONFIG_I2O_EXT_ADAPTEC
  701. if (c->adaptec) {
  702. u8 cmd[10];
  703. u32 scsi_flags;
  704. u16 hwsec = queue_hardsect_size(req->q) >> KERNEL_SECTOR_SHIFT;
  705. memset(cmd, 0, 10);
  706. sgl_offset = SGL_OFFSET_12;
  707. msg->u.head[1] =
  708. cpu_to_le32(I2O_CMD_PRIVATE << 24 | HOST_TID << 12 | tid);
  709. *mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
  710. *mptr++ = cpu_to_le32(tid);
  711. /*
  712. * ENABLE_DISCONNECT
  713. * SIMPLE_TAG
  714. * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
  715. */
  716. if (rq_data_dir(req) == READ) {
  717. cmd[0] = 0x28;
  718. scsi_flags = 0x60a0000a;
  719. } else {
  720. cmd[0] = 0x2A;
  721. scsi_flags = 0xa0a0000a;
  722. }
  723. *mptr++ = cpu_to_le32(scsi_flags);
  724. *((u32 *) & cmd[2]) = cpu_to_be32(req->sector * hwsec);
  725. *((u16 *) & cmd[7]) = cpu_to_be16(req->nr_sectors * hwsec);
  726. memcpy(mptr, cmd, 10);
  727. mptr += 4;
  728. *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
  729. } else
  730. #endif
  731. {
  732. msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
  733. *mptr++ = cpu_to_le32(ctl_flags);
  734. *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
  735. *mptr++ =
  736. cpu_to_le32((u32) (req->sector << KERNEL_SECTOR_SHIFT));
  737. *mptr++ =
  738. cpu_to_le32(req->sector >> (32 - KERNEL_SECTOR_SHIFT));
  739. }
  740. if (!i2o_block_sglist_alloc(c, ireq, &mptr)) {
  741. rc = -ENOMEM;
  742. goto context_remove;
  743. }
  744. msg->u.head[0] =
  745. cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
  746. list_add_tail(&ireq->queue, &dev->open_queue);
  747. dev->open_queue_depth++;
  748. i2o_msg_post(c, msg);
  749. return 0;
  750. context_remove:
  751. i2o_cntxt_list_remove(c, req);
  752. nop_msg:
  753. i2o_msg_nop(c, msg);
  754. exit:
  755. return rc;
  756. };
  757. /**
  758. * i2o_block_request_fn - request queue handling function
  759. * q: request queue from which the request could be fetched
  760. *
  761. * Takes the next request from the queue, transfers it and if no error
  762. * occurs dequeue it from the queue. On arrival of the reply the message
  763. * will be processed further. If an error occurs requeue the request.
  764. */
  765. static void i2o_block_request_fn(struct request_queue *q)
  766. {
  767. struct request *req;
  768. while (!blk_queue_plugged(q)) {
  769. req = elv_next_request(q);
  770. if (!req)
  771. break;
  772. if (blk_fs_request(req)) {
  773. struct i2o_block_delayed_request *dreq;
  774. struct i2o_block_request *ireq = req->special;
  775. unsigned int queue_depth;
  776. queue_depth = ireq->i2o_blk_dev->open_queue_depth;
  777. if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS) {
  778. if (!i2o_block_transfer(req)) {
  779. blkdev_dequeue_request(req);
  780. continue;
  781. } else
  782. osm_info("transfer error\n");
  783. }
  784. if (queue_depth)
  785. break;
  786. /* stop the queue and retry later */
  787. dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
  788. if (!dreq)
  789. continue;
  790. dreq->queue = q;
  791. INIT_WORK(&dreq->work, i2o_block_delayed_request_fn,
  792. dreq);
  793. if (!queue_delayed_work(i2o_block_driver.event_queue,
  794. &dreq->work,
  795. I2O_BLOCK_RETRY_TIME))
  796. kfree(dreq);
  797. else {
  798. blk_stop_queue(q);
  799. break;
  800. }
  801. } else
  802. end_request(req, 0);
  803. }
  804. };
  805. /* I2O Block device operations definition */
  806. static struct block_device_operations i2o_block_fops = {
  807. .owner = THIS_MODULE,
  808. .open = i2o_block_open,
  809. .release = i2o_block_release,
  810. .ioctl = i2o_block_ioctl,
  811. .media_changed = i2o_block_media_changed
  812. };
  813. /**
  814. * i2o_block_device_alloc - Allocate memory for a I2O Block device
  815. *
  816. * Allocate memory for the i2o_block_device struct, gendisk and request
  817. * queue and initialize them as far as no additional information is needed.
  818. *
  819. * Returns a pointer to the allocated I2O Block device on succes or a
  820. * negative error code on failure.
  821. */
  822. static struct i2o_block_device *i2o_block_device_alloc(void)
  823. {
  824. struct i2o_block_device *dev;
  825. struct gendisk *gd;
  826. struct request_queue *queue;
  827. int rc;
  828. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  829. if (!dev) {
  830. osm_err("Insufficient memory to allocate I2O Block disk.\n");
  831. rc = -ENOMEM;
  832. goto exit;
  833. }
  834. memset(dev, 0, sizeof(*dev));
  835. INIT_LIST_HEAD(&dev->open_queue);
  836. spin_lock_init(&dev->lock);
  837. dev->rcache = CACHE_PREFETCH;
  838. dev->wcache = CACHE_WRITEBACK;
  839. /* allocate a gendisk with 16 partitions */
  840. gd = alloc_disk(16);
  841. if (!gd) {
  842. osm_err("Insufficient memory to allocate gendisk.\n");
  843. rc = -ENOMEM;
  844. goto cleanup_dev;
  845. }
  846. /* initialize the request queue */
  847. queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
  848. if (!queue) {
  849. osm_err("Insufficient memory to allocate request queue.\n");
  850. rc = -ENOMEM;
  851. goto cleanup_queue;
  852. }
  853. blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
  854. blk_queue_issue_flush_fn(queue, i2o_block_issue_flush);
  855. gd->major = I2O_MAJOR;
  856. gd->queue = queue;
  857. gd->fops = &i2o_block_fops;
  858. gd->private_data = dev;
  859. dev->gd = gd;
  860. return dev;
  861. cleanup_queue:
  862. put_disk(gd);
  863. cleanup_dev:
  864. kfree(dev);
  865. exit:
  866. return ERR_PTR(rc);
  867. };
  868. /**
  869. * i2o_block_probe - verify if dev is a I2O Block device and install it
  870. * @dev: device to verify if it is a I2O Block device
  871. *
  872. * We only verify if the user_tid of the device is 0xfff and then install
  873. * the device. Otherwise it is used by some other device (e. g. RAID).
  874. *
  875. * Returns 0 on success or negative error code on failure.
  876. */
  877. static int i2o_block_probe(struct device *dev)
  878. {
  879. struct i2o_device *i2o_dev = to_i2o_device(dev);
  880. struct i2o_controller *c = i2o_dev->iop;
  881. struct i2o_block_device *i2o_blk_dev;
  882. struct gendisk *gd;
  883. struct request_queue *queue;
  884. static int unit = 0;
  885. int rc;
  886. u64 size;
  887. u32 blocksize;
  888. u32 flags, status;
  889. u16 body_size = 4;
  890. unsigned short max_sectors;
  891. #ifdef CONFIG_I2O_EXT_ADAPTEC
  892. if (c->adaptec)
  893. body_size = 8;
  894. #endif
  895. if (c->limit_sectors)
  896. max_sectors = I2O_MAX_SECTORS_LIMITED;
  897. else
  898. max_sectors = I2O_MAX_SECTORS;
  899. /* skip devices which are used by IOP */
  900. if (i2o_dev->lct_data.user_tid != 0xfff) {
  901. osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
  902. return -ENODEV;
  903. }
  904. if (i2o_device_claim(i2o_dev)) {
  905. osm_warn("Unable to claim device. Installation aborted\n");
  906. rc = -EFAULT;
  907. goto exit;
  908. }
  909. i2o_blk_dev = i2o_block_device_alloc();
  910. if (IS_ERR(i2o_blk_dev)) {
  911. osm_err("could not alloc a new I2O block device");
  912. rc = PTR_ERR(i2o_blk_dev);
  913. goto claim_release;
  914. }
  915. i2o_blk_dev->i2o_dev = i2o_dev;
  916. dev_set_drvdata(dev, i2o_blk_dev);
  917. /* setup gendisk */
  918. gd = i2o_blk_dev->gd;
  919. gd->first_minor = unit << 4;
  920. sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
  921. sprintf(gd->devfs_name, "i2o/hd%c", 'a' + unit);
  922. gd->driverfs_dev = &i2o_dev->device;
  923. /* setup request queue */
  924. queue = gd->queue;
  925. queue->queuedata = i2o_blk_dev;
  926. blk_queue_max_phys_segments(queue, I2O_MAX_PHYS_SEGMENTS);
  927. blk_queue_max_sectors(queue, max_sectors);
  928. blk_queue_max_hw_segments(queue, i2o_sg_tablesize(c, body_size));
  929. osm_debug("max sectors = %d\n", queue->max_phys_segments);
  930. osm_debug("phys segments = %d\n", queue->max_sectors);
  931. osm_debug("max hw segments = %d\n", queue->max_hw_segments);
  932. /*
  933. * Ask for the current media data. If that isn't supported
  934. * then we ask for the device capacity data
  935. */
  936. if (i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4) ||
  937. i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4)) {
  938. blk_queue_hardsect_size(queue, blocksize);
  939. } else
  940. osm_warn("unable to get blocksize of %s\n", gd->disk_name);
  941. if (i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8) ||
  942. i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8)) {
  943. set_capacity(gd, size >> KERNEL_SECTOR_SHIFT);
  944. } else
  945. osm_warn("could not get size of %s\n", gd->disk_name);
  946. if (!i2o_parm_field_get(i2o_dev, 0x0000, 2, &i2o_blk_dev->power, 2))
  947. i2o_blk_dev->power = 0;
  948. i2o_parm_field_get(i2o_dev, 0x0000, 5, &flags, 4);
  949. i2o_parm_field_get(i2o_dev, 0x0000, 6, &status, 4);
  950. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
  951. add_disk(gd);
  952. unit++;
  953. osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
  954. i2o_blk_dev->gd->disk_name);
  955. return 0;
  956. claim_release:
  957. i2o_device_claim_release(i2o_dev);
  958. exit:
  959. return rc;
  960. };
  961. /* Block OSM driver struct */
  962. static struct i2o_driver i2o_block_driver = {
  963. .name = OSM_NAME,
  964. .event = i2o_block_event,
  965. .reply = i2o_block_reply,
  966. .classes = i2o_block_class_id,
  967. .driver = {
  968. .probe = i2o_block_probe,
  969. .remove = i2o_block_remove,
  970. },
  971. };
  972. /**
  973. * i2o_block_init - Block OSM initialization function
  974. *
  975. * Allocate the slab and mempool for request structs, registers i2o_block
  976. * block device and finally register the Block OSM in the I2O core.
  977. *
  978. * Returns 0 on success or negative error code on failure.
  979. */
  980. static int __init i2o_block_init(void)
  981. {
  982. int rc;
  983. int size;
  984. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  985. /* Allocate request mempool and slab */
  986. size = sizeof(struct i2o_block_request);
  987. i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
  988. SLAB_HWCACHE_ALIGN, NULL,
  989. NULL);
  990. if (!i2o_blk_req_pool.slab) {
  991. osm_err("can't init request slab\n");
  992. rc = -ENOMEM;
  993. goto exit;
  994. }
  995. i2o_blk_req_pool.pool = mempool_create(I2O_BLOCK_REQ_MEMPOOL_SIZE,
  996. mempool_alloc_slab,
  997. mempool_free_slab,
  998. i2o_blk_req_pool.slab);
  999. if (!i2o_blk_req_pool.pool) {
  1000. osm_err("can't init request mempool\n");
  1001. rc = -ENOMEM;
  1002. goto free_slab;
  1003. }
  1004. /* Register the block device interfaces */
  1005. rc = register_blkdev(I2O_MAJOR, "i2o_block");
  1006. if (rc) {
  1007. osm_err("unable to register block device\n");
  1008. goto free_mempool;
  1009. }
  1010. #ifdef MODULE
  1011. osm_info("registered device at major %d\n", I2O_MAJOR);
  1012. #endif
  1013. /* Register Block OSM into I2O core */
  1014. rc = i2o_driver_register(&i2o_block_driver);
  1015. if (rc) {
  1016. osm_err("Could not register Block driver\n");
  1017. goto unregister_blkdev;
  1018. }
  1019. return 0;
  1020. unregister_blkdev:
  1021. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1022. free_mempool:
  1023. mempool_destroy(i2o_blk_req_pool.pool);
  1024. free_slab:
  1025. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1026. exit:
  1027. return rc;
  1028. };
  1029. /**
  1030. * i2o_block_exit - Block OSM exit function
  1031. *
  1032. * Unregisters Block OSM from I2O core, unregisters i2o_block block device
  1033. * and frees the mempool and slab.
  1034. */
  1035. static void __exit i2o_block_exit(void)
  1036. {
  1037. /* Unregister I2O Block OSM from I2O core */
  1038. i2o_driver_unregister(&i2o_block_driver);
  1039. /* Unregister block device */
  1040. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1041. /* Free request mempool and slab */
  1042. mempool_destroy(i2o_blk_req_pool.pool);
  1043. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1044. };
  1045. MODULE_AUTHOR("Red Hat");
  1046. MODULE_LICENSE("GPL");
  1047. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1048. MODULE_VERSION(OSM_VERSION);
  1049. module_init(i2o_block_init);
  1050. module_exit(i2o_block_exit);