i2o_block.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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 __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_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 __iomem *msg;
  157. u32 m;
  158. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  159. if (m == I2O_QUEUE_EMPTY)
  160. return -ETIMEDOUT;
  161. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  162. writel(I2O_CMD_BLOCK_MMOUNT << 24 | HOST_TID << 12 | dev->lct_data.tid,
  163. &msg->u.head[1]);
  164. writel(-1, &msg->body[0]);
  165. writel(0, &msg->body[1]);
  166. osm_debug("Mounting...\n");
  167. return i2o_msg_post_wait(dev->iop, m, 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 __iomem *msg;
  182. u32 m;
  183. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  184. if (m == I2O_QUEUE_EMPTY)
  185. return -ETIMEDOUT;
  186. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  187. writel(I2O_CMD_BLOCK_MLOCK << 24 | HOST_TID << 12 | dev->lct_data.tid,
  188. &msg->u.head[1]);
  189. writel(-1, &msg->body[0]);
  190. osm_debug("Locking...\n");
  191. return i2o_msg_post_wait(dev->iop, m, 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 __iomem *msg;
  206. u32 m;
  207. m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
  208. if (m == I2O_QUEUE_EMPTY)
  209. return -ETIMEDOUT;
  210. writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  211. writel(I2O_CMD_BLOCK_MUNLOCK << 24 | HOST_TID << 12 | dev->lct_data.tid,
  212. &msg->u.head[1]);
  213. writel(media_id, &msg->body[0]);
  214. osm_debug("Unlocking...\n");
  215. return i2o_msg_post_wait(dev->iop, m, 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 __iomem *msg;
  231. u32 m;
  232. int rc;
  233. m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
  234. if (m == I2O_QUEUE_EMPTY)
  235. return -ETIMEDOUT;
  236. writel(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
  237. writel(I2O_CMD_BLOCK_POWER << 24 | HOST_TID << 12 | i2o_dev->lct_data.
  238. tid, &msg->u.head[1]);
  239. writel(op << 24, &msg->body[0]);
  240. osm_debug("Power...\n");
  241. rc = i2o_msg_post_wait(c, m, 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 __iomem ** 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 __iomem *msg;
  635. u32 __iomem *mptr;
  636. struct i2o_block_request *ireq = req->special;
  637. u32 m;
  638. u32 tcntxt;
  639. u32 sgl_offset = SGL_OFFSET_8;
  640. u32 ctl_flags = 0x00000000;
  641. int rc;
  642. u32 cmd;
  643. if (unlikely(!dev->i2o_dev)) {
  644. osm_err("transfer to removed drive\n");
  645. rc = -ENODEV;
  646. goto exit;
  647. }
  648. c = dev->i2o_dev->iop;
  649. m = i2o_msg_get(c, &msg);
  650. if (m == I2O_QUEUE_EMPTY) {
  651. rc = -EBUSY;
  652. goto exit;
  653. }
  654. tcntxt = i2o_cntxt_list_add(c, req);
  655. if (!tcntxt) {
  656. rc = -ENOMEM;
  657. goto nop_msg;
  658. }
  659. writel(i2o_block_driver.context, &msg->u.s.icntxt);
  660. writel(tcntxt, &msg->u.s.tcntxt);
  661. mptr = &msg->body[0];
  662. if (rq_data_dir(req) == READ) {
  663. cmd = I2O_CMD_BLOCK_READ << 24;
  664. switch (dev->rcache) {
  665. case CACHE_PREFETCH:
  666. ctl_flags = 0x201F0008;
  667. break;
  668. case CACHE_SMARTFETCH:
  669. if (req->nr_sectors > 16)
  670. ctl_flags = 0x201F0008;
  671. else
  672. ctl_flags = 0x001F0000;
  673. break;
  674. default:
  675. break;
  676. }
  677. } else {
  678. cmd = I2O_CMD_BLOCK_WRITE << 24;
  679. switch (dev->wcache) {
  680. case CACHE_WRITETHROUGH:
  681. ctl_flags = 0x001F0008;
  682. break;
  683. case CACHE_WRITEBACK:
  684. ctl_flags = 0x001F0010;
  685. break;
  686. case CACHE_SMARTBACK:
  687. if (req->nr_sectors > 16)
  688. ctl_flags = 0x001F0004;
  689. else
  690. ctl_flags = 0x001F0010;
  691. break;
  692. case CACHE_SMARTTHROUGH:
  693. if (req->nr_sectors > 16)
  694. ctl_flags = 0x001F0004;
  695. else
  696. ctl_flags = 0x001F0010;
  697. default:
  698. break;
  699. }
  700. }
  701. #ifdef CONFIG_I2O_EXT_ADAPTEC
  702. if (c->adaptec) {
  703. u8 cmd[10];
  704. u32 scsi_flags;
  705. u16 hwsec = queue_hardsect_size(req->q) >> KERNEL_SECTOR_SHIFT;
  706. memset(cmd, 0, 10);
  707. sgl_offset = SGL_OFFSET_12;
  708. writel(I2O_CMD_PRIVATE << 24 | HOST_TID << 12 | tid,
  709. &msg->u.head[1]);
  710. writel(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC, mptr++);
  711. writel(tid, mptr++);
  712. /*
  713. * ENABLE_DISCONNECT
  714. * SIMPLE_TAG
  715. * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
  716. */
  717. if (rq_data_dir(req) == READ) {
  718. cmd[0] = 0x28;
  719. scsi_flags = 0x60a0000a;
  720. } else {
  721. cmd[0] = 0x2A;
  722. scsi_flags = 0xa0a0000a;
  723. }
  724. writel(scsi_flags, mptr++);
  725. *((u32 *) & cmd[2]) = cpu_to_be32(req->sector * hwsec);
  726. *((u16 *) & cmd[7]) = cpu_to_be16(req->nr_sectors * hwsec);
  727. memcpy_toio(mptr, cmd, 10);
  728. mptr += 4;
  729. writel(req->nr_sectors << KERNEL_SECTOR_SHIFT, mptr++);
  730. } else
  731. #endif
  732. {
  733. writel(cmd | HOST_TID << 12 | tid, &msg->u.head[1]);
  734. writel(ctl_flags, mptr++);
  735. writel(req->nr_sectors << KERNEL_SECTOR_SHIFT, mptr++);
  736. writel((u32) (req->sector << KERNEL_SECTOR_SHIFT), mptr++);
  737. writel(req->sector >> (32 - KERNEL_SECTOR_SHIFT), mptr++);
  738. }
  739. if (!i2o_block_sglist_alloc(c, ireq, &mptr)) {
  740. rc = -ENOMEM;
  741. goto context_remove;
  742. }
  743. writel(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) |
  744. sgl_offset, &msg->u.head[0]);
  745. list_add_tail(&ireq->queue, &dev->open_queue);
  746. dev->open_queue_depth++;
  747. i2o_msg_post(c, m);
  748. return 0;
  749. context_remove:
  750. i2o_cntxt_list_remove(c, req);
  751. nop_msg:
  752. i2o_msg_nop(c, m);
  753. exit:
  754. return rc;
  755. };
  756. /**
  757. * i2o_block_request_fn - request queue handling function
  758. * q: request queue from which the request could be fetched
  759. *
  760. * Takes the next request from the queue, transfers it and if no error
  761. * occurs dequeue it from the queue. On arrival of the reply the message
  762. * will be processed further. If an error occurs requeue the request.
  763. */
  764. static void i2o_block_request_fn(struct request_queue *q)
  765. {
  766. struct request *req;
  767. while (!blk_queue_plugged(q)) {
  768. req = elv_next_request(q);
  769. if (!req)
  770. break;
  771. if (blk_fs_request(req)) {
  772. struct i2o_block_delayed_request *dreq;
  773. struct i2o_block_request *ireq = req->special;
  774. unsigned int queue_depth;
  775. queue_depth = ireq->i2o_blk_dev->open_queue_depth;
  776. if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS) {
  777. if (!i2o_block_transfer(req)) {
  778. blkdev_dequeue_request(req);
  779. continue;
  780. } else
  781. osm_info("transfer error\n");
  782. }
  783. if (queue_depth)
  784. break;
  785. /* stop the queue and retry later */
  786. dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
  787. if (!dreq)
  788. continue;
  789. dreq->queue = q;
  790. INIT_WORK(&dreq->work, i2o_block_delayed_request_fn,
  791. dreq);
  792. if (!queue_delayed_work(i2o_block_driver.event_queue,
  793. &dreq->work,
  794. I2O_BLOCK_RETRY_TIME))
  795. kfree(dreq);
  796. else {
  797. blk_stop_queue(q);
  798. break;
  799. }
  800. } else
  801. end_request(req, 0);
  802. }
  803. };
  804. /* I2O Block device operations definition */
  805. static struct block_device_operations i2o_block_fops = {
  806. .owner = THIS_MODULE,
  807. .open = i2o_block_open,
  808. .release = i2o_block_release,
  809. .ioctl = i2o_block_ioctl,
  810. .media_changed = i2o_block_media_changed
  811. };
  812. /**
  813. * i2o_block_device_alloc - Allocate memory for a I2O Block device
  814. *
  815. * Allocate memory for the i2o_block_device struct, gendisk and request
  816. * queue and initialize them as far as no additional information is needed.
  817. *
  818. * Returns a pointer to the allocated I2O Block device on succes or a
  819. * negative error code on failure.
  820. */
  821. static struct i2o_block_device *i2o_block_device_alloc(void)
  822. {
  823. struct i2o_block_device *dev;
  824. struct gendisk *gd;
  825. struct request_queue *queue;
  826. int rc;
  827. dev = kmalloc(sizeof(*dev), GFP_KERNEL);
  828. if (!dev) {
  829. osm_err("Insufficient memory to allocate I2O Block disk.\n");
  830. rc = -ENOMEM;
  831. goto exit;
  832. }
  833. memset(dev, 0, sizeof(*dev));
  834. INIT_LIST_HEAD(&dev->open_queue);
  835. spin_lock_init(&dev->lock);
  836. dev->rcache = CACHE_PREFETCH;
  837. dev->wcache = CACHE_WRITEBACK;
  838. /* allocate a gendisk with 16 partitions */
  839. gd = alloc_disk(16);
  840. if (!gd) {
  841. osm_err("Insufficient memory to allocate gendisk.\n");
  842. rc = -ENOMEM;
  843. goto cleanup_dev;
  844. }
  845. /* initialize the request queue */
  846. queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
  847. if (!queue) {
  848. osm_err("Insufficient memory to allocate request queue.\n");
  849. rc = -ENOMEM;
  850. goto cleanup_queue;
  851. }
  852. blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
  853. blk_queue_issue_flush_fn(queue, i2o_block_issue_flush);
  854. gd->major = I2O_MAJOR;
  855. gd->queue = queue;
  856. gd->fops = &i2o_block_fops;
  857. gd->private_data = dev;
  858. dev->gd = gd;
  859. return dev;
  860. cleanup_queue:
  861. put_disk(gd);
  862. cleanup_dev:
  863. kfree(dev);
  864. exit:
  865. return ERR_PTR(rc);
  866. };
  867. /**
  868. * i2o_block_probe - verify if dev is a I2O Block device and install it
  869. * @dev: device to verify if it is a I2O Block device
  870. *
  871. * We only verify if the user_tid of the device is 0xfff and then install
  872. * the device. Otherwise it is used by some other device (e. g. RAID).
  873. *
  874. * Returns 0 on success or negative error code on failure.
  875. */
  876. static int i2o_block_probe(struct device *dev)
  877. {
  878. struct i2o_device *i2o_dev = to_i2o_device(dev);
  879. struct i2o_controller *c = i2o_dev->iop;
  880. struct i2o_block_device *i2o_blk_dev;
  881. struct gendisk *gd;
  882. struct request_queue *queue;
  883. static int unit = 0;
  884. int rc;
  885. u64 size;
  886. u32 blocksize;
  887. u32 flags, status;
  888. u16 body_size = 4;
  889. unsigned short max_sectors;
  890. #ifdef CONFIG_I2O_EXT_ADAPTEC
  891. if (c->adaptec)
  892. body_size = 8;
  893. #endif
  894. if (c->limit_sectors)
  895. max_sectors = I2O_MAX_SECTORS_LIMITED;
  896. else
  897. max_sectors = I2O_MAX_SECTORS;
  898. /* skip devices which are used by IOP */
  899. if (i2o_dev->lct_data.user_tid != 0xfff) {
  900. osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
  901. return -ENODEV;
  902. }
  903. if (i2o_device_claim(i2o_dev)) {
  904. osm_warn("Unable to claim device. Installation aborted\n");
  905. rc = -EFAULT;
  906. goto exit;
  907. }
  908. i2o_blk_dev = i2o_block_device_alloc();
  909. if (IS_ERR(i2o_blk_dev)) {
  910. osm_err("could not alloc a new I2O block device");
  911. rc = PTR_ERR(i2o_blk_dev);
  912. goto claim_release;
  913. }
  914. i2o_blk_dev->i2o_dev = i2o_dev;
  915. dev_set_drvdata(dev, i2o_blk_dev);
  916. /* setup gendisk */
  917. gd = i2o_blk_dev->gd;
  918. gd->first_minor = unit << 4;
  919. sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
  920. sprintf(gd->devfs_name, "i2o/hd%c", 'a' + unit);
  921. gd->driverfs_dev = &i2o_dev->device;
  922. /* setup request queue */
  923. queue = gd->queue;
  924. queue->queuedata = i2o_blk_dev;
  925. blk_queue_max_phys_segments(queue, I2O_MAX_PHYS_SEGMENTS);
  926. blk_queue_max_sectors(queue, max_sectors);
  927. blk_queue_max_hw_segments(queue, i2o_sg_tablesize(c, body_size));
  928. osm_debug("max sectors = %d\n", queue->max_phys_segments);
  929. osm_debug("phys segments = %d\n", queue->max_sectors);
  930. osm_debug("max hw segments = %d\n", queue->max_hw_segments);
  931. /*
  932. * Ask for the current media data. If that isn't supported
  933. * then we ask for the device capacity data
  934. */
  935. if (i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4) ||
  936. i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4)) {
  937. blk_queue_hardsect_size(queue, blocksize);
  938. } else
  939. osm_warn("unable to get blocksize of %s\n", gd->disk_name);
  940. if (i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8) ||
  941. i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8)) {
  942. set_capacity(gd, size >> KERNEL_SECTOR_SHIFT);
  943. } else
  944. osm_warn("could not get size of %s\n", gd->disk_name);
  945. if (!i2o_parm_field_get(i2o_dev, 0x0000, 2, &i2o_blk_dev->power, 2))
  946. i2o_blk_dev->power = 0;
  947. i2o_parm_field_get(i2o_dev, 0x0000, 5, &flags, 4);
  948. i2o_parm_field_get(i2o_dev, 0x0000, 6, &status, 4);
  949. i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
  950. add_disk(gd);
  951. unit++;
  952. osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
  953. i2o_blk_dev->gd->disk_name);
  954. return 0;
  955. claim_release:
  956. i2o_device_claim_release(i2o_dev);
  957. exit:
  958. return rc;
  959. };
  960. /* Block OSM driver struct */
  961. static struct i2o_driver i2o_block_driver = {
  962. .name = OSM_NAME,
  963. .event = i2o_block_event,
  964. .reply = i2o_block_reply,
  965. .classes = i2o_block_class_id,
  966. .driver = {
  967. .probe = i2o_block_probe,
  968. .remove = i2o_block_remove,
  969. },
  970. };
  971. /**
  972. * i2o_block_init - Block OSM initialization function
  973. *
  974. * Allocate the slab and mempool for request structs, registers i2o_block
  975. * block device and finally register the Block OSM in the I2O core.
  976. *
  977. * Returns 0 on success or negative error code on failure.
  978. */
  979. static int __init i2o_block_init(void)
  980. {
  981. int rc;
  982. int size;
  983. printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
  984. /* Allocate request mempool and slab */
  985. size = sizeof(struct i2o_block_request);
  986. i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
  987. SLAB_HWCACHE_ALIGN, NULL,
  988. NULL);
  989. if (!i2o_blk_req_pool.slab) {
  990. osm_err("can't init request slab\n");
  991. rc = -ENOMEM;
  992. goto exit;
  993. }
  994. i2o_blk_req_pool.pool = mempool_create(I2O_BLOCK_REQ_MEMPOOL_SIZE,
  995. mempool_alloc_slab,
  996. mempool_free_slab,
  997. i2o_blk_req_pool.slab);
  998. if (!i2o_blk_req_pool.pool) {
  999. osm_err("can't init request mempool\n");
  1000. rc = -ENOMEM;
  1001. goto free_slab;
  1002. }
  1003. /* Register the block device interfaces */
  1004. rc = register_blkdev(I2O_MAJOR, "i2o_block");
  1005. if (rc) {
  1006. osm_err("unable to register block device\n");
  1007. goto free_mempool;
  1008. }
  1009. #ifdef MODULE
  1010. osm_info("registered device at major %d\n", I2O_MAJOR);
  1011. #endif
  1012. /* Register Block OSM into I2O core */
  1013. rc = i2o_driver_register(&i2o_block_driver);
  1014. if (rc) {
  1015. osm_err("Could not register Block driver\n");
  1016. goto unregister_blkdev;
  1017. }
  1018. return 0;
  1019. unregister_blkdev:
  1020. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1021. free_mempool:
  1022. mempool_destroy(i2o_blk_req_pool.pool);
  1023. free_slab:
  1024. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1025. exit:
  1026. return rc;
  1027. };
  1028. /**
  1029. * i2o_block_exit - Block OSM exit function
  1030. *
  1031. * Unregisters Block OSM from I2O core, unregisters i2o_block block device
  1032. * and frees the mempool and slab.
  1033. */
  1034. static void __exit i2o_block_exit(void)
  1035. {
  1036. /* Unregister I2O Block OSM from I2O core */
  1037. i2o_driver_unregister(&i2o_block_driver);
  1038. /* Unregister block device */
  1039. unregister_blkdev(I2O_MAJOR, "i2o_block");
  1040. /* Free request mempool and slab */
  1041. mempool_destroy(i2o_blk_req_pool.pool);
  1042. kmem_cache_destroy(i2o_blk_req_pool.slab);
  1043. };
  1044. MODULE_AUTHOR("Red Hat");
  1045. MODULE_LICENSE("GPL");
  1046. MODULE_DESCRIPTION(OSM_DESCRIPTION);
  1047. MODULE_VERSION(OSM_VERSION);
  1048. module_init(i2o_block_init);
  1049. module_exit(i2o_block_exit);