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