i2o_block.c 32 KB

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