i2o_block.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 "$Rev$"
  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 __iomem *msg;
  114. u32 m;
  115. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  116. if (m == I2O_QUEUE_EMPTY)
  117. return -ETIMEDOUT;
  118. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  119. writel(I2O_CMD_BLOCK_CFLUSH << 24 | HOST_TID << 12 | dev->lct_data.tid,
  120. &msg->u.head[1]);
  121. writel(60 << 16, &msg->body[0]);
  122. osm_debug("Flushing...\n");
  123. return i2o_msg_post_wait(dev->iop, m, 60);
  124. };
  125. /**
  126. * i2o_block_device_mount - Mount (load) the media of device dev
  127. * @dev: I2O device which should receive the mount request
  128. * @media_id: Media Identifier
  129. *
  130. * Load a media into drive. Identifier should be set to -1, because the
  131. * spec does not support any other value.
  132. *
  133. * Returns 0 on success or negative error code on failure.
  134. */
  135. static int i2o_block_device_mount(struct i2o_device *dev, u32 media_id)
  136. {
  137. struct i2o_message __iomem *msg;
  138. u32 m;
  139. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  140. if (m == I2O_QUEUE_EMPTY)
  141. return -ETIMEDOUT;
  142. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  143. writel(I2O_CMD_BLOCK_MMOUNT << 24 | HOST_TID << 12 | dev->lct_data.tid,
  144. &msg->u.head[1]);
  145. writel(-1, &msg->body[0]);
  146. writel(0, &msg->body[1]);
  147. osm_debug("Mounting...\n");
  148. return i2o_msg_post_wait(dev->iop, m, 2);
  149. };
  150. /**
  151. * i2o_block_device_lock - Locks the media of device dev
  152. * @dev: I2O device which should receive the lock request
  153. * @media_id: Media Identifier
  154. *
  155. * Lock media of device dev to prevent removal. The media identifier
  156. * should be set to -1, because the spec does not support any other value.
  157. *
  158. * Returns 0 on success or negative error code on failure.
  159. */
  160. static int i2o_block_device_lock(struct i2o_device *dev, u32 media_id)
  161. {
  162. struct i2o_message __iomem *msg;
  163. u32 m;
  164. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  165. if (m == I2O_QUEUE_EMPTY)
  166. return -ETIMEDOUT;
  167. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  168. writel(I2O_CMD_BLOCK_MLOCK << 24 | HOST_TID << 12 | dev->lct_data.tid,
  169. &msg->u.head[1]);
  170. writel(-1, &msg->body[0]);
  171. osm_debug("Locking...\n");
  172. return i2o_msg_post_wait(dev->iop, m, 2);
  173. };
  174. /**
  175. * i2o_block_device_unlock - Unlocks the media of device dev
  176. * @dev: I2O device which should receive the unlocked request
  177. * @media_id: Media Identifier
  178. *
  179. * Unlocks the media in device dev. The media identifier should be set to
  180. * -1, because the spec does not support any other value.
  181. *
  182. * Returns 0 on success or negative error code on failure.
  183. */
  184. static int i2o_block_device_unlock(struct i2o_device *dev, u32 media_id)
  185. {
  186. struct i2o_message __iomem *msg;
  187. u32 m;
  188. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  189. if (m == I2O_QUEUE_EMPTY)
  190. return -ETIMEDOUT;
  191. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  192. writel(I2O_CMD_BLOCK_MUNLOCK << 24 | HOST_TID << 12 | dev->lct_data.tid,
  193. &msg->u.head[1]);
  194. writel(media_id, &msg->body[0]);
  195. osm_debug("Unlocking...\n");
  196. return i2o_msg_post_wait(dev->iop, m, 2);
  197. };
  198. /**
  199. * i2o_block_device_power - Power management for device dev
  200. * @dev: I2O device which should receive the power management request
  201. * @operation: Operation which should be send
  202. *
  203. * Send a power management request to the device dev.
  204. *
  205. * Returns 0 on success or negative error code on failure.
  206. */
  207. static int i2o_block_device_power(struct i2o_block_device *dev, u8 op)
  208. {
  209. struct i2o_device *i2o_dev = dev->i2o_dev;
  210. struct i2o_controller *c = i2o_dev->iop;
  211. struct i2o_message __iomem *msg;
  212. u32 m;
  213. int rc;
  214. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  215. if (m == I2O_QUEUE_EMPTY)
  216. return -ETIMEDOUT;
  217. writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  218. writel(I2O_CMD_BLOCK_POWER << 24 | HOST_TID << 12 | i2o_dev->lct_data.
  219. tid, &msg->u.head[1]);
  220. writel(op << 24, &msg->body[0]);
  221. osm_debug("Power...\n");
  222. rc = i2o_msg_post_wait(c, m, 60);
  223. if (!rc)
  224. dev->power = op;
  225. return rc;
  226. };
  227. /**
  228. * i2o_block_request_alloc - Allocate an I2O block request struct
  229. *
  230. * Allocates an I2O block request struct and initialize the list.
  231. *
  232. * Returns a i2o_block_request pointer on success or negative error code
  233. * on failure.
  234. */
  235. static inline struct i2o_block_request *i2o_block_request_alloc(void)
  236. {
  237. struct i2o_block_request *ireq;
  238. ireq = mempool_alloc(i2o_blk_req_pool.pool, GFP_ATOMIC);
  239. if (!ireq)
  240. return ERR_PTR(-ENOMEM);
  241. INIT_LIST_HEAD(&ireq->queue);
  242. return ireq;
  243. };
  244. /**
  245. * i2o_block_request_free - Frees a I2O block request
  246. * @ireq: I2O block request which should be freed
  247. *
  248. * Fres the allocated memory (give it back to the request mempool).
  249. */
  250. static inline void i2o_block_request_free(struct i2o_block_request *ireq)
  251. {
  252. mempool_free(ireq, i2o_blk_req_pool.pool);
  253. };
  254. /**
  255. * i2o_block_sglist_alloc - Allocate the SG list and map it
  256. * @ireq: I2O block request
  257. *
  258. * Builds the SG list and map it into to be accessable by the controller.
  259. *
  260. * Returns the number of elements in the SG list or 0 on failure.
  261. */
  262. static inline int i2o_block_sglist_alloc(struct i2o_block_request *ireq)
  263. {
  264. struct device *dev = &ireq->i2o_blk_dev->i2o_dev->iop->pdev->dev;
  265. int nents;
  266. nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);
  267. if (rq_data_dir(ireq->req) == READ)
  268. ireq->sg_dma_direction = PCI_DMA_FROMDEVICE;
  269. else
  270. ireq->sg_dma_direction = PCI_DMA_TODEVICE;
  271. ireq->sg_nents = dma_map_sg(dev, ireq->sg_table, nents,
  272. ireq->sg_dma_direction);
  273. return ireq->sg_nents;
  274. };
  275. /**
  276. * i2o_block_sglist_free - Frees the SG list
  277. * @ireq: I2O block request from which the SG should be freed
  278. *
  279. * Frees the SG list from the I2O block request.
  280. */
  281. static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
  282. {
  283. struct device *dev = &ireq->i2o_blk_dev->i2o_dev->iop->pdev->dev;
  284. dma_unmap_sg(dev, ireq->sg_table, ireq->sg_nents,
  285. ireq->sg_dma_direction);
  286. };
  287. /**
  288. * i2o_block_prep_req_fn - Allocates I2O block device specific struct
  289. * @q: request queue for the request
  290. * @req: the request to prepare
  291. *
  292. * Allocate the necessary i2o_block_request struct and connect it to
  293. * the request. This is needed that we not loose the SG list later on.
  294. *
  295. * Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
  296. */
  297. static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
  298. {
  299. struct i2o_block_device *i2o_blk_dev = q->queuedata;
  300. struct i2o_block_request *ireq;
  301. /* request is already processed by us, so return */
  302. if (req->flags & REQ_SPECIAL) {
  303. osm_debug("REQ_SPECIAL already set!\n");
  304. req->flags |= REQ_DONTPREP;
  305. return BLKPREP_OK;
  306. }
  307. /* connect the i2o_block_request to the request */
  308. if (!req->special) {
  309. ireq = i2o_block_request_alloc();
  310. if (unlikely(IS_ERR(ireq))) {
  311. osm_debug("unable to allocate i2o_block_request!\n");
  312. return BLKPREP_DEFER;
  313. }
  314. ireq->i2o_blk_dev = i2o_blk_dev;
  315. req->special = ireq;
  316. ireq->req = req;
  317. } else
  318. ireq = req->special;
  319. /* do not come back here */
  320. req->flags |= REQ_DONTPREP | REQ_SPECIAL;
  321. return BLKPREP_OK;
  322. };
  323. /**
  324. * i2o_block_delayed_request_fn - delayed request queue function
  325. * delayed_request: the delayed request with the queue to start
  326. *
  327. * If the request queue is stopped for a disk, and there is no open
  328. * request, a new event is created, which calls this function to start
  329. * the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
  330. * be started again.
  331. */
  332. static void i2o_block_delayed_request_fn(void *delayed_request)
  333. {
  334. struct i2o_block_delayed_request *dreq = delayed_request;
  335. struct request_queue *q = dreq->queue;
  336. unsigned long flags;
  337. spin_lock_irqsave(q->queue_lock, flags);
  338. blk_start_queue(q);
  339. spin_unlock_irqrestore(q->queue_lock, flags);
  340. kfree(dreq);
  341. };
  342. /**
  343. * i2o_block_end_request - Post-processing of completed commands
  344. * @req: request which should be completed
  345. * @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
  346. * @nr_bytes: number of bytes to complete
  347. *
  348. * Mark the request as complete. The lock must not be held when entering.
  349. *
  350. */
  351. static void i2o_block_end_request(struct request *req, int uptodate,
  352. int nr_bytes)
  353. {
  354. struct i2o_block_request *ireq = req->special;
  355. struct i2o_block_device *dev = ireq->i2o_blk_dev;
  356. request_queue_t *q = dev->gd->queue;
  357. unsigned long flags;
  358. if (end_that_request_chunk(req, uptodate, nr_bytes)) {
  359. int leftover = (req->hard_nr_sectors << 9);
  360. if (blk_pc_request(req))
  361. leftover = req->data_len;
  362. if (end_io_error(uptodate))
  363. end_that_request_chunk(req, 0, leftover);
  364. }
  365. add_disk_randomness(req->rq_disk);
  366. spin_lock_irqsave(q->queue_lock, flags);
  367. end_that_request_last(req);
  368. dev->open_queue_depth--;
  369. list_del(&ireq->queue);
  370. blk_start_queue(q);
  371. spin_unlock_irqrestore(q->queue_lock, flags);
  372. i2o_block_sglist_free(ireq);
  373. i2o_block_request_free(ireq);
  374. };
  375. /**
  376. * i2o_block_reply - Block OSM reply handler.
  377. * @c: I2O controller from which the message arrives
  378. * @m: message id of reply
  379. * qmsg: the actuall I2O message reply
  380. *
  381. * This function gets all the message replies.
  382. *
  383. */
  384. static int i2o_block_reply(struct i2o_controller *c, u32 m,
  385. struct i2o_message *msg)
  386. {
  387. struct request *req;
  388. int uptodate = 1;
  389. req = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
  390. if (unlikely(!req)) {
  391. osm_err("NULL reply received!\n");
  392. return -1;
  393. }
  394. /*
  395. * Lets see what is cooking. We stuffed the
  396. * request in the context.
  397. */
  398. if ((le32_to_cpu(msg->body[0]) >> 24) != 0) {
  399. u32 status = le32_to_cpu(msg->body[0]);
  400. /*
  401. * Device not ready means two things. One is that the
  402. * the thing went offline (but not a removal media)
  403. *
  404. * The second is that you have a SuperTrak 100 and the
  405. * firmware got constipated. Unlike standard i2o card
  406. * setups the supertrak returns an error rather than
  407. * blocking for the timeout in these cases.
  408. *
  409. * Don't stick a supertrak100 into cache aggressive modes
  410. */
  411. osm_err("%03x error status: %02x, detailed status: %04x\n",
  412. (le32_to_cpu(msg->u.head[1]) >> 12 & 0xfff),
  413. status >> 24, status & 0xffff);
  414. req->errors++;
  415. uptodate = 0;
  416. }
  417. i2o_block_end_request(req, uptodate, le32_to_cpu(msg->body[1]));
  418. return 1;
  419. };
  420. static void i2o_block_event(struct i2o_event *evt)
  421. {
  422. osm_info("event received\n");
  423. kfree(evt);
  424. };
  425. /*
  426. * SCSI-CAM for ioctl geometry mapping
  427. * Duplicated with SCSI - this should be moved into somewhere common
  428. * perhaps genhd ?
  429. *
  430. * LBA -> CHS mapping table taken from:
  431. *
  432. * "Incorporating the I2O Architecture into BIOS for Intel Architecture
  433. * Platforms"
  434. *
  435. * This is an I2O document that is only available to I2O members,
  436. * not developers.
  437. *
  438. * From my understanding, this is how all the I2O cards do this
  439. *
  440. * Disk Size | Sectors | Heads | Cylinders
  441. * ---------------+---------+-------+-------------------
  442. * 1 < X <= 528M | 63 | 16 | X/(63 * 16 * 512)
  443. * 528M < X <= 1G | 63 | 32 | X/(63 * 32 * 512)
  444. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  445. * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
  446. *
  447. */
  448. #define BLOCK_SIZE_528M 1081344
  449. #define BLOCK_SIZE_1G 2097152
  450. #define BLOCK_SIZE_21G 4403200
  451. #define BLOCK_SIZE_42G 8806400
  452. #define BLOCK_SIZE_84G 17612800
  453. static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
  454. unsigned char *hds, unsigned char *secs)
  455. {
  456. unsigned long heads, sectors, cylinders;
  457. sectors = 63L; /* Maximize sectors per track */
  458. if (capacity <= BLOCK_SIZE_528M)
  459. heads = 16;
  460. else if (capacity <= BLOCK_SIZE_1G)
  461. heads = 32;
  462. else if (capacity <= BLOCK_SIZE_21G)
  463. heads = 64;
  464. else if (capacity <= BLOCK_SIZE_42G)
  465. heads = 128;
  466. else
  467. heads = 255;
  468. cylinders = (unsigned long)capacity / (heads * sectors);
  469. *cyls = (unsigned short)cylinders; /* Stuff return values */
  470. *secs = (unsigned char)sectors;
  471. *hds = (unsigned char)heads;
  472. }
  473. /**
  474. * i2o_block_open - Open the block device
  475. *
  476. * Power up the device, mount and lock the media. This function is called,
  477. * if the block device is opened for access.
  478. *
  479. * Returns 0 on success or negative error code on failure.
  480. */
  481. static int i2o_block_open(struct inode *inode, struct file *file)
  482. {
  483. struct i2o_block_device *dev = inode->i_bdev->bd_disk->private_data;
  484. if (!dev->i2o_dev)
  485. return -ENODEV;
  486. if (dev->power > 0x1f)
  487. i2o_block_device_power(dev, 0x02);
  488. i2o_block_device_mount(dev->i2o_dev, -1);
  489. i2o_block_device_lock(dev->i2o_dev, -1);
  490. osm_debug("Ready.\n");
  491. return 0;
  492. };
  493. /**
  494. * i2o_block_release - Release the I2O block device
  495. *
  496. * Unlock and unmount the media, and power down the device. Gets called if
  497. * the block device is closed.
  498. *
  499. * Returns 0 on success or negative error code on failure.
  500. */
  501. static int i2o_block_release(struct inode *inode, struct file *file)
  502. {
  503. struct gendisk *disk = inode->i_bdev->bd_disk;
  504. struct i2o_block_device *dev = disk->private_data;
  505. u8 operation;
  506. /*
  507. * This is to deail with the case of an application
  508. * opening a device and then the device dissapears while
  509. * it's in use, and then the application tries to release
  510. * it. ex: Unmounting a deleted RAID volume at reboot.
  511. * If we send messages, it will just cause FAILs since
  512. * the TID no longer exists.
  513. */
  514. if (!dev->i2o_dev)
  515. return 0;
  516. i2o_block_device_flush(dev->i2o_dev);
  517. i2o_block_device_unlock(dev->i2o_dev, -1);
  518. if (dev->flags & (1 << 3 | 1 << 4)) /* Removable */
  519. operation = 0x21;
  520. else
  521. operation = 0x24;
  522. i2o_block_device_power(dev, operation);
  523. return 0;
  524. }
  525. /**
  526. * i2o_block_ioctl - Issue device specific ioctl calls.
  527. * @cmd: ioctl command
  528. * @arg: arg
  529. *
  530. * Handles ioctl request for the block device.
  531. *
  532. * Return 0 on success or negative error on failure.
  533. */
  534. static int i2o_block_ioctl(struct inode *inode, struct file *file,
  535. unsigned int cmd, unsigned long arg)
  536. {
  537. struct gendisk *disk = inode->i_bdev->bd_disk;
  538. struct i2o_block_device *dev = disk->private_data;
  539. void __user *argp = (void __user *)arg;
  540. /* Anyone capable of this syscall can do *real bad* things */
  541. if (!capable(CAP_SYS_ADMIN))
  542. return -EPERM;
  543. switch (cmd) {
  544. case HDIO_GETGEO:
  545. {
  546. struct hd_geometry g;
  547. i2o_block_biosparam(get_capacity(disk),
  548. &g.cylinders, &g.heads, &g.sectors);
  549. g.start = get_start_sect(inode->i_bdev);
  550. return copy_to_user(argp, &g, sizeof(g)) ? -EFAULT : 0;
  551. }
  552. case BLKI2OGRSTRAT:
  553. return put_user(dev->rcache, (int __user *)arg);
  554. case BLKI2OGWSTRAT:
  555. return put_user(dev->wcache, (int __user *)arg);
  556. case BLKI2OSRSTRAT:
  557. if (arg < 0 || arg > CACHE_SMARTFETCH)
  558. return -EINVAL;
  559. dev->rcache = arg;
  560. break;
  561. case BLKI2OSWSTRAT:
  562. if (arg != 0
  563. && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
  564. return -EINVAL;
  565. dev->wcache = arg;
  566. break;
  567. }
  568. return -ENOTTY;
  569. };
  570. /**
  571. * i2o_block_media_changed - Have we seen a media change?
  572. * @disk: gendisk which should be verified
  573. *
  574. * Verifies if the media has changed.
  575. *
  576. * Returns 1 if the media was changed or 0 otherwise.
  577. */
  578. static int i2o_block_media_changed(struct gendisk *disk)
  579. {
  580. struct i2o_block_device *p = disk->private_data;
  581. if (p->media_change_flag) {
  582. p->media_change_flag = 0;
  583. return 1;
  584. }
  585. return 0;
  586. }
  587. /**
  588. * i2o_block_transfer - Transfer a request to/from the I2O controller
  589. * @req: the request which should be transfered
  590. *
  591. * This function converts the request into a I2O message. The necessary
  592. * DMA buffers are allocated and after everything is setup post the message
  593. * to the I2O controller. No cleanup is done by this function. It is done
  594. * on the interrupt side when the reply arrives.
  595. *
  596. * Return 0 on success or negative error code on failure.
  597. */
  598. static int i2o_block_transfer(struct request *req)
  599. {
  600. struct i2o_block_device *dev = req->rq_disk->private_data;
  601. struct i2o_controller *c = dev->i2o_dev->iop;
  602. int tid = dev->i2o_dev->lct_data.tid;
  603. struct i2o_message __iomem *msg;
  604. void __iomem *mptr;
  605. struct i2o_block_request *ireq = req->special;
  606. struct scatterlist *sg;
  607. int sgnum;
  608. int i;
  609. u32 m;
  610. u32 tcntxt;
  611. u32 sg_flags;
  612. int rc;
  613. m = i2o_msg_get(c, &msg);
  614. if (m == I2O_QUEUE_EMPTY) {
  615. rc = -EBUSY;
  616. goto exit;
  617. }
  618. tcntxt = i2o_cntxt_list_add(c, req);
  619. if (!tcntxt) {
  620. rc = -ENOMEM;
  621. goto nop_msg;
  622. }
  623. if ((sgnum = i2o_block_sglist_alloc(ireq)) <= 0) {
  624. rc = -ENOMEM;
  625. goto context_remove;
  626. }
  627. /* Build the message based on the request. */
  628. writel(i2o_block_driver.context, &msg->u.s.icntxt);
  629. writel(tcntxt, &msg->u.s.tcntxt);
  630. writel(req->nr_sectors << 9, &msg->body[1]);
  631. writel((((u64) req->sector) << 9) & 0xffffffff, &msg->body[2]);
  632. writel(req->sector >> 23, &msg->body[3]);
  633. mptr = &msg->body[4];
  634. sg = ireq->sg_table;
  635. if (rq_data_dir(req) == READ) {
  636. writel(I2O_CMD_BLOCK_READ << 24 | HOST_TID << 12 | tid,
  637. &msg->u.head[1]);
  638. sg_flags = 0x10000000;
  639. switch (dev->rcache) {
  640. case CACHE_NULL:
  641. writel(0, &msg->body[0]);
  642. break;
  643. case CACHE_PREFETCH:
  644. writel(0x201F0008, &msg->body[0]);
  645. break;
  646. case CACHE_SMARTFETCH:
  647. if (req->nr_sectors > 16)
  648. writel(0x201F0008, &msg->body[0]);
  649. else
  650. writel(0x001F0000, &msg->body[0]);
  651. break;
  652. }
  653. } else {
  654. writel(I2O_CMD_BLOCK_WRITE << 24 | HOST_TID << 12 | tid,
  655. &msg->u.head[1]);
  656. sg_flags = 0x14000000;
  657. switch (dev->wcache) {
  658. case CACHE_NULL:
  659. writel(0, &msg->body[0]);
  660. break;
  661. case CACHE_WRITETHROUGH:
  662. writel(0x001F0008, &msg->body[0]);
  663. break;
  664. case CACHE_WRITEBACK:
  665. writel(0x001F0010, &msg->body[0]);
  666. break;
  667. case CACHE_SMARTBACK:
  668. if (req->nr_sectors > 16)
  669. writel(0x001F0004, &msg->body[0]);
  670. else
  671. writel(0x001F0010, &msg->body[0]);
  672. break;
  673. case CACHE_SMARTTHROUGH:
  674. if (req->nr_sectors > 16)
  675. writel(0x001F0004, &msg->body[0]);
  676. else
  677. writel(0x001F0010, &msg->body[0]);
  678. }
  679. }
  680. for (i = sgnum; i > 0; i--) {
  681. if (i == 1)
  682. sg_flags |= 0x80000000;
  683. writel(sg_flags | sg_dma_len(sg), mptr);
  684. writel(sg_dma_address(sg), mptr + 4);
  685. mptr += 8;
  686. sg++;
  687. }
  688. writel(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | SGL_OFFSET_8,
  689. &msg->u.head[0]);
  690. list_add_tail(&ireq->queue, &dev->open_queue);
  691. dev->open_queue_depth++;
  692. i2o_msg_post(c, m);
  693. return 0;
  694. context_remove:
  695. i2o_cntxt_list_remove(c, req);
  696. nop_msg:
  697. i2o_msg_nop(c, m);
  698. exit:
  699. return rc;
  700. };
  701. /**
  702. * i2o_block_request_fn - request queue handling function
  703. * q: request queue from which the request could be fetched
  704. *
  705. * Takes the next request from the queue, transfers it and if no error
  706. * occurs dequeue it from the queue. On arrival of the reply the message
  707. * will be processed further. If an error occurs requeue the request.
  708. */
  709. static void i2o_block_request_fn(struct request_queue *q)
  710. {
  711. struct request *req;
  712. while (!blk_queue_plugged(q)) {
  713. req = elv_next_request(q);
  714. if (!req)
  715. break;
  716. if (blk_fs_request(req)) {
  717. struct i2o_block_delayed_request *dreq;
  718. struct i2o_block_request *ireq = req->special;
  719. unsigned int queue_depth;
  720. queue_depth = ireq->i2o_blk_dev->open_queue_depth;
  721. if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS)
  722. if (!i2o_block_transfer(req)) {
  723. blkdev_dequeue_request(req);
  724. continue;
  725. }
  726. if (queue_depth)
  727. break;
  728. /* stop the queue and retry later */
  729. dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
  730. if (!dreq)
  731. continue;
  732. dreq->queue = q;
  733. INIT_WORK(&dreq->work, i2o_block_delayed_request_fn,
  734. dreq);
  735. osm_info("transfer error\n");
  736. if (!queue_delayed_work(i2o_block_driver.event_queue,
  737. &dreq->work,
  738. I2O_BLOCK_RETRY_TIME))
  739. kfree(dreq);
  740. else {
  741. blk_stop_queue(q);
  742. break;
  743. }
  744. } else
  745. end_request(req, 0);
  746. }
  747. };
  748. /* I2O Block device operations definition */
  749. static struct block_device_operations i2o_block_fops = {
  750. .owner = THIS_MODULE,
  751. .open = i2o_block_open,
  752. .release = i2o_block_release,
  753. .ioctl = i2o_block_ioctl,
  754. .media_changed = i2o_block_media_changed
  755. };
  756. /**
  757. * i2o_block_device_alloc - Allocate memory for a I2O Block device
  758. *
  759. * Allocate memory for the i2o_block_device struct, gendisk and request
  760. * queue and initialize them as far as no additional information is needed.
  761. *
  762. * Returns a pointer to the allocated I2O Block device on succes or a
  763. * negative error code on failure.
  764. */
  765. static struct i2o_block_device *i2o_block_device_alloc(void)
  766. {
  767. struct i2o_block_device *dev;
  768. struct gendisk *gd;
  769. struct request_queue *queue;
  770. int rc;
  771. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  772. if (!dev) {
  773. osm_err("Insufficient memory to allocate I2O Block disk.\n");
  774. rc = -ENOMEM;
  775. goto exit;
  776. }
  777. memset(dev, 0, sizeof(*dev));
  778. INIT_LIST_HEAD(&dev->open_queue);
  779. spin_lock_init(&dev->lock);
  780. dev->rcache = CACHE_PREFETCH;
  781. dev->wcache = CACHE_WRITEBACK;
  782. /* allocate a gendisk with 16 partitions */
  783. gd = alloc_disk(16);
  784. if (!gd) {
  785. osm_err("Insufficient memory to allocate gendisk.\n");
  786. rc = -ENOMEM;
  787. goto cleanup_dev;
  788. }
  789. /* initialize the request queue */
  790. queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
  791. if (!queue) {
  792. osm_err("Insufficient memory to allocate request queue.\n");
  793. rc = -ENOMEM;
  794. goto cleanup_queue;
  795. }
  796. blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
  797. gd->major = I2O_MAJOR;
  798. gd->queue = queue;
  799. gd->fops = &i2o_block_fops;
  800. gd->private_data = dev;
  801. dev->gd = gd;
  802. return dev;
  803. cleanup_queue:
  804. put_disk(gd);
  805. cleanup_dev:
  806. kfree(dev);
  807. exit:
  808. return ERR_PTR(rc);
  809. };
  810. /**
  811. * i2o_block_probe - verify if dev is a I2O Block device and install it
  812. * @dev: device to verify if it is a I2O Block device
  813. *
  814. * We only verify if the user_tid of the device is 0xfff and then install
  815. * the device. Otherwise it is used by some other device (e. g. RAID).
  816. *
  817. * Returns 0 on success or negative error code on failure.
  818. */
  819. static int i2o_block_probe(struct device *dev)
  820. {
  821. struct i2o_device *i2o_dev = to_i2o_device(dev);
  822. struct i2o_block_device *i2o_blk_dev;
  823. struct i2o_controller *c = i2o_dev->iop;
  824. struct gendisk *gd;
  825. struct request_queue *queue;
  826. static int unit = 0;
  827. int rc;
  828. u64 size;
  829. u32 blocksize;
  830. u32 flags, status;
  831. int segments;
  832. /* skip devices which are used by IOP */
  833. if (i2o_dev->lct_data.user_tid != 0xfff) {
  834. osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
  835. return -ENODEV;
  836. }
  837. if (i2o_device_claim(i2o_dev)) {
  838. osm_warn("Unable to claim device. Installation aborted\n");
  839. rc = -EFAULT;
  840. goto exit;
  841. }
  842. i2o_blk_dev = i2o_block_device_alloc();
  843. if (IS_ERR(i2o_blk_dev)) {
  844. osm_err("could not alloc a new I2O block device");
  845. rc = PTR_ERR(i2o_blk_dev);
  846. goto claim_release;
  847. }
  848. i2o_blk_dev->i2o_dev = i2o_dev;
  849. dev_set_drvdata(dev, i2o_blk_dev);
  850. /* setup gendisk */
  851. gd = i2o_blk_dev->gd;
  852. gd->first_minor = unit << 4;
  853. sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
  854. sprintf(gd->devfs_name, "i2o/hd%c", 'a' + unit);
  855. gd->driverfs_dev = &i2o_dev->device;
  856. /* setup request queue */
  857. queue = gd->queue;
  858. queue->queuedata = i2o_blk_dev;
  859. blk_queue_max_phys_segments(queue, I2O_MAX_SEGMENTS);
  860. blk_queue_max_sectors(queue, I2O_MAX_SECTORS);
  861. if (c->short_req)
  862. segments = 8;
  863. else {
  864. i2o_status_block *sb;
  865. sb = c->status_block.virt;
  866. segments = (sb->inbound_frame_size -
  867. sizeof(struct i2o_message) / 4 - 4) / 2;
  868. }
  869. blk_queue_max_hw_segments(queue, segments);
  870. osm_debug("max sectors = %d\n", I2O_MAX_SECTORS);
  871. osm_debug("phys segments = %d\n", I2O_MAX_SEGMENTS);
  872. osm_debug("hw segments = %d\n", segments);
  873. /*
  874. * Ask for the current media data. If that isn't supported
  875. * then we ask for the device capacity data
  876. */
  877. if (!i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8))
  878. if (!i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8)) {
  879. osm_warn("could not get size of %s\n", gd->disk_name);
  880. size = 0;
  881. }
  882. if (!i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4))
  883. if (!i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4)) {
  884. osm_warn("unable to get blocksize of %s\n",
  885. gd->disk_name);
  886. blocksize = 0;
  887. }
  888. if (!i2o_parm_field_get(i2o_dev, 0x0000, 2, &i2o_blk_dev->power, 2))
  889. i2o_blk_dev->power = 0;
  890. i2o_parm_field_get(i2o_dev, 0x0000, 5, &flags, 4);
  891. i2o_parm_field_get(i2o_dev, 0x0000, 6, &status, 4);
  892. set_capacity(gd, size >> 9);
  893. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
  894. add_disk(gd);
  895. unit++;
  896. osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
  897. i2o_blk_dev->gd->disk_name);
  898. return 0;
  899. claim_release:
  900. i2o_device_claim_release(i2o_dev);
  901. exit:
  902. return rc;
  903. };
  904. /* Block OSM driver struct */
  905. static struct i2o_driver i2o_block_driver = {
  906. .name = OSM_NAME,
  907. .event = i2o_block_event,
  908. .reply = i2o_block_reply,
  909. .classes = i2o_block_class_id,
  910. .driver = {
  911. .probe = i2o_block_probe,
  912. .remove = i2o_block_remove,
  913. },
  914. };
  915. /**
  916. * i2o_block_init - Block OSM initialization function
  917. *
  918. * Allocate the slab and mempool for request structs, registers i2o_block
  919. * block device and finally register the Block OSM in the I2O core.
  920. *
  921. * Returns 0 on success or negative error code on failure.
  922. */
  923. static int __init i2o_block_init(void)
  924. {
  925. int rc;
  926. int size;
  927. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  928. /* Allocate request mempool and slab */
  929. size = sizeof(struct i2o_block_request);
  930. i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
  931. SLAB_HWCACHE_ALIGN, NULL,
  932. NULL);
  933. if (!i2o_blk_req_pool.slab) {
  934. osm_err("can't init request slab\n");
  935. rc = -ENOMEM;
  936. goto exit;
  937. }
  938. i2o_blk_req_pool.pool = mempool_create(I2O_REQ_MEMPOOL_SIZE,
  939. mempool_alloc_slab,
  940. mempool_free_slab,
  941. i2o_blk_req_pool.slab);
  942. if (!i2o_blk_req_pool.pool) {
  943. osm_err("can't init request mempool\n");
  944. rc = -ENOMEM;
  945. goto free_slab;
  946. }
  947. /* Register the block device interfaces */
  948. rc = register_blkdev(I2O_MAJOR, "i2o_block");
  949. if (rc) {
  950. osm_err("unable to register block device\n");
  951. goto free_mempool;
  952. }
  953. #ifdef MODULE
  954. osm_info("registered device at major %d\n", I2O_MAJOR);
  955. #endif
  956. /* Register Block OSM into I2O core */
  957. rc = i2o_driver_register(&i2o_block_driver);
  958. if (rc) {
  959. osm_err("Could not register Block driver\n");
  960. goto unregister_blkdev;
  961. }
  962. return 0;
  963. unregister_blkdev:
  964. unregister_blkdev(I2O_MAJOR, "i2o_block");
  965. free_mempool:
  966. mempool_destroy(i2o_blk_req_pool.pool);
  967. free_slab:
  968. kmem_cache_destroy(i2o_blk_req_pool.slab);
  969. exit:
  970. return rc;
  971. };
  972. /**
  973. * i2o_block_exit - Block OSM exit function
  974. *
  975. * Unregisters Block OSM from I2O core, unregisters i2o_block block device
  976. * and frees the mempool and slab.
  977. */
  978. static void __exit i2o_block_exit(void)
  979. {
  980. /* Unregister I2O Block OSM from I2O core */
  981. i2o_driver_unregister(&i2o_block_driver);
  982. /* Unregister block device */
  983. unregister_blkdev(I2O_MAJOR, "i2o_block");
  984. /* Free request mempool and slab */
  985. mempool_destroy(i2o_blk_req_pool.pool);
  986. kmem_cache_destroy(i2o_blk_req_pool.slab);
  987. };
  988. MODULE_AUTHOR("Red Hat");
  989. MODULE_LICENSE("GPL");
  990. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  991. MODULE_VERSION(OSM_VERSION);
  992. module_init(i2o_block_init);
  993. module_exit(i2o_block_exit);