ibmvscsi.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /* ------------------------------------------------------------
  2. * ibmvscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2004
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.com)
  6. * Dave Boutcher (sleddog@us.ibm.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. * USA
  22. *
  23. * ------------------------------------------------------------
  24. * Emulation of a SCSI host adapter for Virtual I/O devices
  25. *
  26. * This driver supports the SCSI adapter implemented by the IBM
  27. * Power5 firmware. That SCSI adapter is not a physical adapter,
  28. * but allows Linux SCSI peripheral drivers to directly
  29. * access devices in another logical partition on the physical system.
  30. *
  31. * The virtual adapter(s) are present in the open firmware device
  32. * tree just like real adapters.
  33. *
  34. * One of the capabilities provided on these systems is the ability
  35. * to DMA between partitions. The architecture states that for VSCSI,
  36. * the server side is allowed to DMA to and from the client. The client
  37. * is never trusted to DMA to or from the server directly.
  38. *
  39. * Messages are sent between partitions on a "Command/Response Queue"
  40. * (CRQ), which is just a buffer of 16 byte entries in the receiver's
  41. * Senders cannot access the buffer directly, but send messages by
  42. * making a hypervisor call and passing in the 16 bytes. The hypervisor
  43. * puts the message in the next 16 byte space in round-robbin fashion,
  44. * turns on the high order bit of the message (the valid bit), and
  45. * generates an interrupt to the receiver (if interrupts are turned on.)
  46. * The receiver just turns off the valid bit when they have copied out
  47. * the message.
  48. *
  49. * The VSCSI client builds a SCSI Remote Protocol (SRP) Information Unit
  50. * (IU) (as defined in the T10 standard available at www.t10.org), gets
  51. * a DMA address for the message, and sends it to the server as the
  52. * payload of a CRQ message. The server DMAs the SRP IU and processes it,
  53. * including doing any additional data transfers. When it is done, it
  54. * DMAs the SRP response back to the same address as the request came from,
  55. * and sends a CRQ message back to inform the client that the request has
  56. * completed.
  57. *
  58. * Note that some of the underlying infrastructure is different between
  59. * machines conforming to the "RS/6000 Platform Architecture" (RPA) and
  60. * the older iSeries hypervisor models. To support both, some low level
  61. * routines have been broken out into rpa_vscsi.c and iseries_vscsi.c.
  62. * The Makefile should pick one, not two, not zero, of these.
  63. *
  64. * TODO: This is currently pretty tied to the IBM i/pSeries hypervisor
  65. * interfaces. It would be really nice to abstract this above an RDMA
  66. * layer.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/dma-mapping.h>
  71. #include <linux/delay.h>
  72. #include <asm/vio.h>
  73. #include <scsi/scsi.h>
  74. #include <scsi/scsi_cmnd.h>
  75. #include <scsi/scsi_host.h>
  76. #include <scsi/scsi_device.h>
  77. #include "ibmvscsi.h"
  78. /* The values below are somewhat arbitrary default values, but
  79. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  80. * Note that there are 3 bits of channel value, 6 bits of id, and
  81. * 5 bits of LUN.
  82. */
  83. static int max_id = 64;
  84. static int max_channel = 3;
  85. static int init_timeout = 5;
  86. static int max_requests = 50;
  87. #define IBMVSCSI_VERSION "1.5.8"
  88. MODULE_DESCRIPTION("IBM Virtual SCSI");
  89. MODULE_AUTHOR("Dave Boutcher");
  90. MODULE_LICENSE("GPL");
  91. MODULE_VERSION(IBMVSCSI_VERSION);
  92. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  94. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  95. MODULE_PARM_DESC(max_channel, "Largest channel value");
  96. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  98. module_param_named(max_requests, max_requests, int, S_IRUGO | S_IWUSR);
  99. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  100. /* ------------------------------------------------------------
  101. * Routines for the event pool and event structs
  102. */
  103. /**
  104. * initialize_event_pool: - Allocates and initializes the event pool for a host
  105. * @pool: event_pool to be initialized
  106. * @size: Number of events in pool
  107. * @hostdata: ibmvscsi_host_data who owns the event pool
  108. *
  109. * Returns zero on success.
  110. */
  111. static int initialize_event_pool(struct event_pool *pool,
  112. int size, struct ibmvscsi_host_data *hostdata)
  113. {
  114. int i;
  115. pool->size = size;
  116. pool->next = 0;
  117. pool->events = kmalloc(pool->size * sizeof(*pool->events), GFP_KERNEL);
  118. if (!pool->events)
  119. return -ENOMEM;
  120. memset(pool->events, 0x00, pool->size * sizeof(*pool->events));
  121. pool->iu_storage =
  122. dma_alloc_coherent(hostdata->dev,
  123. pool->size * sizeof(*pool->iu_storage),
  124. &pool->iu_token, 0);
  125. if (!pool->iu_storage) {
  126. kfree(pool->events);
  127. return -ENOMEM;
  128. }
  129. for (i = 0; i < pool->size; ++i) {
  130. struct srp_event_struct *evt = &pool->events[i];
  131. memset(&evt->crq, 0x00, sizeof(evt->crq));
  132. atomic_set(&evt->free, 1);
  133. evt->crq.valid = 0x80;
  134. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  135. evt->crq.IU_data_ptr = pool->iu_token +
  136. sizeof(*evt->xfer_iu) * i;
  137. evt->xfer_iu = pool->iu_storage + i;
  138. evt->hostdata = hostdata;
  139. evt->ext_list = NULL;
  140. evt->ext_list_token = 0;
  141. }
  142. return 0;
  143. }
  144. /**
  145. * release_event_pool: - Frees memory of an event pool of a host
  146. * @pool: event_pool to be released
  147. * @hostdata: ibmvscsi_host_data who owns the even pool
  148. *
  149. * Returns zero on success.
  150. */
  151. static void release_event_pool(struct event_pool *pool,
  152. struct ibmvscsi_host_data *hostdata)
  153. {
  154. int i, in_use = 0;
  155. for (i = 0; i < pool->size; ++i) {
  156. if (atomic_read(&pool->events[i].free) != 1)
  157. ++in_use;
  158. if (pool->events[i].ext_list) {
  159. dma_free_coherent(hostdata->dev,
  160. SG_ALL * sizeof(struct srp_direct_buf),
  161. pool->events[i].ext_list,
  162. pool->events[i].ext_list_token);
  163. }
  164. }
  165. if (in_use)
  166. printk(KERN_WARNING
  167. "ibmvscsi: releasing event pool with %d "
  168. "events still in use?\n", in_use);
  169. kfree(pool->events);
  170. dma_free_coherent(hostdata->dev,
  171. pool->size * sizeof(*pool->iu_storage),
  172. pool->iu_storage, pool->iu_token);
  173. }
  174. /**
  175. * valid_event_struct: - Determines if event is valid.
  176. * @pool: event_pool that contains the event
  177. * @evt: srp_event_struct to be checked for validity
  178. *
  179. * Returns zero if event is invalid, one otherwise.
  180. */
  181. static int valid_event_struct(struct event_pool *pool,
  182. struct srp_event_struct *evt)
  183. {
  184. int index = evt - pool->events;
  185. if (index < 0 || index >= pool->size) /* outside of bounds */
  186. return 0;
  187. if (evt != pool->events + index) /* unaligned */
  188. return 0;
  189. return 1;
  190. }
  191. /**
  192. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  193. * @pool: event_pool that contains the event
  194. * @evt: srp_event_struct to be modified
  195. *
  196. */
  197. static void free_event_struct(struct event_pool *pool,
  198. struct srp_event_struct *evt)
  199. {
  200. if (!valid_event_struct(pool, evt)) {
  201. printk(KERN_ERR
  202. "ibmvscsi: Freeing invalid event_struct %p "
  203. "(not in pool %p)\n", evt, pool->events);
  204. return;
  205. }
  206. if (atomic_inc_return(&evt->free) != 1) {
  207. printk(KERN_ERR
  208. "ibmvscsi: Freeing event_struct %p "
  209. "which is not in use!\n", evt);
  210. return;
  211. }
  212. }
  213. /**
  214. * get_evt_struct: - Gets the next free event in pool
  215. * @pool: event_pool that contains the events to be searched
  216. *
  217. * Returns the next event in "free" state, and NULL if none are free.
  218. * Note that no synchronization is done here, we assume the host_lock
  219. * will syncrhonze things.
  220. */
  221. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  222. {
  223. int i;
  224. int poolsize = pool->size;
  225. int offset = pool->next;
  226. for (i = 0; i < poolsize; i++) {
  227. offset = (offset + 1) % poolsize;
  228. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  229. pool->next = offset;
  230. return &pool->events[offset];
  231. }
  232. }
  233. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  234. return NULL;
  235. }
  236. /**
  237. * init_event_struct: Initialize fields in an event struct that are always
  238. * required.
  239. * @evt: The event
  240. * @done: Routine to call when the event is responded to
  241. * @format: SRP or MAD format
  242. * @timeout: timeout value set in the CRQ
  243. */
  244. static void init_event_struct(struct srp_event_struct *evt_struct,
  245. void (*done) (struct srp_event_struct *),
  246. u8 format,
  247. int timeout)
  248. {
  249. evt_struct->cmnd = NULL;
  250. evt_struct->cmnd_done = NULL;
  251. evt_struct->sync_srp = NULL;
  252. evt_struct->crq.format = format;
  253. evt_struct->crq.timeout = timeout;
  254. evt_struct->done = done;
  255. }
  256. /* ------------------------------------------------------------
  257. * Routines for receiving SCSI responses from the hosting partition
  258. */
  259. /**
  260. * set_srp_direction: Set the fields in the srp related to data
  261. * direction and number of buffers based on the direction in
  262. * the scsi_cmnd and the number of buffers
  263. */
  264. static void set_srp_direction(struct scsi_cmnd *cmd,
  265. struct srp_cmd *srp_cmd,
  266. int numbuf)
  267. {
  268. u8 fmt;
  269. if (numbuf == 0)
  270. return;
  271. if (numbuf == 1)
  272. fmt = SRP_DATA_DESC_DIRECT;
  273. else {
  274. fmt = SRP_DATA_DESC_INDIRECT;
  275. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  276. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  277. srp_cmd->data_out_desc_cnt = numbuf;
  278. else
  279. srp_cmd->data_in_desc_cnt = numbuf;
  280. }
  281. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  282. srp_cmd->buf_fmt = fmt << 4;
  283. else
  284. srp_cmd->buf_fmt = fmt;
  285. }
  286. static void unmap_sg_list(int num_entries,
  287. struct device *dev,
  288. struct srp_direct_buf *md)
  289. {
  290. int i;
  291. for (i = 0; i < num_entries; ++i)
  292. dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
  293. }
  294. /**
  295. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  296. * @cmd: srp_cmd whose additional_data member will be unmapped
  297. * @dev: device for which the memory is mapped
  298. *
  299. */
  300. static void unmap_cmd_data(struct srp_cmd *cmd,
  301. struct srp_event_struct *evt_struct,
  302. struct device *dev)
  303. {
  304. u8 out_fmt, in_fmt;
  305. out_fmt = cmd->buf_fmt >> 4;
  306. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  307. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  308. return;
  309. else if (out_fmt == SRP_DATA_DESC_DIRECT ||
  310. in_fmt == SRP_DATA_DESC_DIRECT) {
  311. struct srp_direct_buf *data =
  312. (struct srp_direct_buf *) cmd->add_data;
  313. dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
  314. } else {
  315. struct srp_indirect_buf *indirect =
  316. (struct srp_indirect_buf *) cmd->add_data;
  317. int num_mapped = indirect->table_desc.len /
  318. sizeof(struct srp_direct_buf);
  319. if (num_mapped <= MAX_INDIRECT_BUFS) {
  320. unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
  321. return;
  322. }
  323. unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
  324. }
  325. }
  326. static int map_sg_list(int num_entries,
  327. struct scatterlist *sg,
  328. struct srp_direct_buf *md)
  329. {
  330. int i;
  331. u64 total_length = 0;
  332. for (i = 0; i < num_entries; ++i) {
  333. struct srp_direct_buf *descr = md + i;
  334. struct scatterlist *sg_entry = &sg[i];
  335. descr->va = sg_dma_address(sg_entry);
  336. descr->len = sg_dma_len(sg_entry);
  337. descr->key = 0;
  338. total_length += sg_dma_len(sg_entry);
  339. }
  340. return total_length;
  341. }
  342. /**
  343. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  344. * @cmd: Scsi_Cmnd with the scatterlist
  345. * @srp_cmd: srp_cmd that contains the memory descriptor
  346. * @dev: device for which to map dma memory
  347. *
  348. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  349. * Returns 1 on success.
  350. */
  351. static int map_sg_data(struct scsi_cmnd *cmd,
  352. struct srp_event_struct *evt_struct,
  353. struct srp_cmd *srp_cmd, struct device *dev)
  354. {
  355. int sg_mapped;
  356. u64 total_length = 0;
  357. struct scatterlist *sg = cmd->request_buffer;
  358. struct srp_direct_buf *data =
  359. (struct srp_direct_buf *) srp_cmd->add_data;
  360. struct srp_indirect_buf *indirect =
  361. (struct srp_indirect_buf *) data;
  362. sg_mapped = dma_map_sg(dev, sg, cmd->use_sg, DMA_BIDIRECTIONAL);
  363. if (sg_mapped == 0)
  364. return 0;
  365. set_srp_direction(cmd, srp_cmd, sg_mapped);
  366. /* special case; we can use a single direct descriptor */
  367. if (sg_mapped == 1) {
  368. data->va = sg_dma_address(&sg[0]);
  369. data->len = sg_dma_len(&sg[0]);
  370. data->key = 0;
  371. return 1;
  372. }
  373. if (sg_mapped > SG_ALL) {
  374. printk(KERN_ERR
  375. "ibmvscsi: More than %d mapped sg entries, got %d\n",
  376. SG_ALL, sg_mapped);
  377. return 0;
  378. }
  379. indirect->table_desc.va = 0;
  380. indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
  381. indirect->table_desc.key = 0;
  382. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  383. total_length = map_sg_list(sg_mapped, sg,
  384. &indirect->desc_list[0]);
  385. indirect->len = total_length;
  386. return 1;
  387. }
  388. /* get indirect table */
  389. if (!evt_struct->ext_list) {
  390. evt_struct->ext_list = (struct srp_direct_buf *)
  391. dma_alloc_coherent(dev,
  392. SG_ALL * sizeof(struct srp_direct_buf),
  393. &evt_struct->ext_list_token, 0);
  394. if (!evt_struct->ext_list) {
  395. printk(KERN_ERR
  396. "ibmvscsi: Can't allocate memory for indirect table\n");
  397. return 0;
  398. }
  399. }
  400. total_length = map_sg_list(sg_mapped, sg, evt_struct->ext_list);
  401. indirect->len = total_length;
  402. indirect->table_desc.va = evt_struct->ext_list_token;
  403. indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
  404. memcpy(indirect->desc_list, evt_struct->ext_list,
  405. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  406. return 1;
  407. }
  408. /**
  409. * map_single_data: - Maps memory and initializes memory decriptor fields
  410. * @cmd: struct scsi_cmnd with the memory to be mapped
  411. * @srp_cmd: srp_cmd that contains the memory descriptor
  412. * @dev: device for which to map dma memory
  413. *
  414. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  415. * Returns 1 on success.
  416. */
  417. static int map_single_data(struct scsi_cmnd *cmd,
  418. struct srp_cmd *srp_cmd, struct device *dev)
  419. {
  420. struct srp_direct_buf *data =
  421. (struct srp_direct_buf *) srp_cmd->add_data;
  422. data->va =
  423. dma_map_single(dev, cmd->request_buffer,
  424. cmd->request_bufflen,
  425. DMA_BIDIRECTIONAL);
  426. if (dma_mapping_error(data->va)) {
  427. printk(KERN_ERR
  428. "ibmvscsi: Unable to map request_buffer for command!\n");
  429. return 0;
  430. }
  431. data->len = cmd->request_bufflen;
  432. data->key = 0;
  433. set_srp_direction(cmd, srp_cmd, 1);
  434. return 1;
  435. }
  436. /**
  437. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  438. * @cmd: struct scsi_cmnd with the memory to be mapped
  439. * @srp_cmd: srp_cmd that contains the memory descriptor
  440. * @dev: dma device for which to map dma memory
  441. *
  442. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  443. * Returns 1 on success.
  444. */
  445. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  446. struct srp_event_struct *evt_struct,
  447. struct srp_cmd *srp_cmd, struct device *dev)
  448. {
  449. switch (cmd->sc_data_direction) {
  450. case DMA_FROM_DEVICE:
  451. case DMA_TO_DEVICE:
  452. break;
  453. case DMA_NONE:
  454. return 1;
  455. case DMA_BIDIRECTIONAL:
  456. printk(KERN_ERR
  457. "ibmvscsi: Can't map DMA_BIDIRECTIONAL to read/write\n");
  458. return 0;
  459. default:
  460. printk(KERN_ERR
  461. "ibmvscsi: Unknown data direction 0x%02x; can't map!\n",
  462. cmd->sc_data_direction);
  463. return 0;
  464. }
  465. if (!cmd->request_buffer)
  466. return 1;
  467. if (cmd->use_sg)
  468. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  469. return map_single_data(cmd, srp_cmd, dev);
  470. }
  471. /* ------------------------------------------------------------
  472. * Routines for sending and receiving SRPs
  473. */
  474. /**
  475. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  476. * @evt_struct: evt_struct to be sent
  477. * @hostdata: ibmvscsi_host_data of host
  478. *
  479. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  480. * Note that this routine assumes that host_lock is held for synchronization
  481. */
  482. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  483. struct ibmvscsi_host_data *hostdata)
  484. {
  485. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  486. int rc;
  487. /* If we have exhausted our request limit, just fail this request.
  488. * Note that there are rare cases involving driver generated requests
  489. * (such as task management requests) that the mid layer may think we
  490. * can handle more requests (can_queue) when we actually can't
  491. */
  492. if ((evt_struct->crq.format == VIOSRP_SRP_FORMAT) &&
  493. (atomic_dec_if_positive(&hostdata->request_limit) < 0))
  494. goto send_error;
  495. /* Copy the IU into the transfer area */
  496. *evt_struct->xfer_iu = evt_struct->iu;
  497. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  498. /* Add this to the sent list. We need to do this
  499. * before we actually send
  500. * in case it comes back REALLY fast
  501. */
  502. list_add_tail(&evt_struct->list, &hostdata->sent);
  503. if ((rc =
  504. ibmvscsi_send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  505. list_del(&evt_struct->list);
  506. printk(KERN_ERR "ibmvscsi: send error %d\n",
  507. rc);
  508. goto send_error;
  509. }
  510. return 0;
  511. send_error:
  512. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  513. free_event_struct(&hostdata->pool, evt_struct);
  514. return SCSI_MLQUEUE_HOST_BUSY;
  515. }
  516. /**
  517. * handle_cmd_rsp: - Handle responses from commands
  518. * @evt_struct: srp_event_struct to be handled
  519. *
  520. * Used as a callback by when sending scsi cmds.
  521. * Gets called by ibmvscsi_handle_crq()
  522. */
  523. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  524. {
  525. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  526. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  527. if (unlikely(rsp->opcode != SRP_RSP)) {
  528. if (printk_ratelimit())
  529. printk(KERN_WARNING
  530. "ibmvscsi: bad SRP RSP type %d\n",
  531. rsp->opcode);
  532. }
  533. if (cmnd) {
  534. cmnd->result = rsp->status;
  535. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  536. memcpy(cmnd->sense_buffer,
  537. rsp->data,
  538. rsp->sense_data_len);
  539. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  540. evt_struct,
  541. evt_struct->hostdata->dev);
  542. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  543. cmnd->resid = rsp->data_out_res_cnt;
  544. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  545. cmnd->resid = rsp->data_in_res_cnt;
  546. }
  547. if (evt_struct->cmnd_done)
  548. evt_struct->cmnd_done(cmnd);
  549. }
  550. /**
  551. * lun_from_dev: - Returns the lun of the scsi device
  552. * @dev: struct scsi_device
  553. *
  554. */
  555. static inline u16 lun_from_dev(struct scsi_device *dev)
  556. {
  557. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  558. }
  559. /**
  560. * ibmvscsi_queue: - The queuecommand function of the scsi template
  561. * @cmd: struct scsi_cmnd to be executed
  562. * @done: Callback function to be called when cmd is completed
  563. */
  564. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  565. void (*done) (struct scsi_cmnd *))
  566. {
  567. struct srp_cmd *srp_cmd;
  568. struct srp_event_struct *evt_struct;
  569. struct srp_indirect_buf *indirect;
  570. struct ibmvscsi_host_data *hostdata =
  571. (struct ibmvscsi_host_data *)&cmnd->device->host->hostdata;
  572. u16 lun = lun_from_dev(cmnd->device);
  573. u8 out_fmt, in_fmt;
  574. evt_struct = get_event_struct(&hostdata->pool);
  575. if (!evt_struct)
  576. return SCSI_MLQUEUE_HOST_BUSY;
  577. /* Set up the actual SRP IU */
  578. srp_cmd = &evt_struct->iu.srp.cmd;
  579. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  580. srp_cmd->opcode = SRP_CMD;
  581. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(cmnd->cmnd));
  582. srp_cmd->lun = ((u64) lun) << 48;
  583. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  584. printk(KERN_ERR "ibmvscsi: couldn't convert cmd to srp_cmd\n");
  585. free_event_struct(&hostdata->pool, evt_struct);
  586. return SCSI_MLQUEUE_HOST_BUSY;
  587. }
  588. init_event_struct(evt_struct,
  589. handle_cmd_rsp,
  590. VIOSRP_SRP_FORMAT,
  591. cmnd->timeout_per_command/HZ);
  592. evt_struct->cmnd = cmnd;
  593. evt_struct->cmnd_done = done;
  594. /* Fix up dma address of the buffer itself */
  595. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  596. out_fmt = srp_cmd->buf_fmt >> 4;
  597. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  598. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  599. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  600. indirect->table_desc.va == 0) {
  601. indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
  602. offsetof(struct srp_cmd, add_data) +
  603. offsetof(struct srp_indirect_buf, desc_list);
  604. }
  605. return ibmvscsi_send_srp_event(evt_struct, hostdata);
  606. }
  607. /* ------------------------------------------------------------
  608. * Routines for driver initialization
  609. */
  610. /**
  611. * adapter_info_rsp: - Handle response to MAD adapter info request
  612. * @evt_struct: srp_event_struct with the response
  613. *
  614. * Used as a "done" callback by when sending adapter_info. Gets called
  615. * by ibmvscsi_handle_crq()
  616. */
  617. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  618. {
  619. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  620. dma_unmap_single(hostdata->dev,
  621. evt_struct->iu.mad.adapter_info.buffer,
  622. evt_struct->iu.mad.adapter_info.common.length,
  623. DMA_BIDIRECTIONAL);
  624. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  625. printk("ibmvscsi: error %d getting adapter info\n",
  626. evt_struct->xfer_iu->mad.adapter_info.common.status);
  627. } else {
  628. printk("ibmvscsi: host srp version: %s, "
  629. "host partition %s (%d), OS %d, max io %u\n",
  630. hostdata->madapter_info.srp_version,
  631. hostdata->madapter_info.partition_name,
  632. hostdata->madapter_info.partition_number,
  633. hostdata->madapter_info.os_type,
  634. hostdata->madapter_info.port_max_txu[0]);
  635. if (hostdata->madapter_info.port_max_txu[0])
  636. hostdata->host->max_sectors =
  637. hostdata->madapter_info.port_max_txu[0] >> 9;
  638. if (hostdata->madapter_info.os_type == 3 &&
  639. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  640. printk("ibmvscsi: host (Ver. %s) doesn't support large"
  641. "transfers\n",
  642. hostdata->madapter_info.srp_version);
  643. printk("ibmvscsi: limiting scatterlists to %d\n",
  644. MAX_INDIRECT_BUFS);
  645. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  646. }
  647. }
  648. }
  649. /**
  650. * send_mad_adapter_info: - Sends the mad adapter info request
  651. * and stores the result so it can be retrieved with
  652. * sysfs. We COULD consider causing a failure if the
  653. * returned SRP version doesn't match ours.
  654. * @hostdata: ibmvscsi_host_data of host
  655. *
  656. * Returns zero if successful.
  657. */
  658. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  659. {
  660. struct viosrp_adapter_info *req;
  661. struct srp_event_struct *evt_struct;
  662. evt_struct = get_event_struct(&hostdata->pool);
  663. if (!evt_struct) {
  664. printk(KERN_ERR "ibmvscsi: couldn't allocate an event "
  665. "for ADAPTER_INFO_REQ!\n");
  666. return;
  667. }
  668. init_event_struct(evt_struct,
  669. adapter_info_rsp,
  670. VIOSRP_MAD_FORMAT,
  671. init_timeout * HZ);
  672. req = &evt_struct->iu.mad.adapter_info;
  673. memset(req, 0x00, sizeof(*req));
  674. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  675. req->common.length = sizeof(hostdata->madapter_info);
  676. req->buffer = dma_map_single(hostdata->dev,
  677. &hostdata->madapter_info,
  678. sizeof(hostdata->madapter_info),
  679. DMA_BIDIRECTIONAL);
  680. if (dma_mapping_error(req->buffer)) {
  681. printk(KERN_ERR
  682. "ibmvscsi: Unable to map request_buffer "
  683. "for adapter_info!\n");
  684. free_event_struct(&hostdata->pool, evt_struct);
  685. return;
  686. }
  687. if (ibmvscsi_send_srp_event(evt_struct, hostdata))
  688. printk(KERN_ERR "ibmvscsi: couldn't send ADAPTER_INFO_REQ!\n");
  689. };
  690. /**
  691. * login_rsp: - Handle response to SRP login request
  692. * @evt_struct: srp_event_struct with the response
  693. *
  694. * Used as a "done" callback by when sending srp_login. Gets called
  695. * by ibmvscsi_handle_crq()
  696. */
  697. static void login_rsp(struct srp_event_struct *evt_struct)
  698. {
  699. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  700. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  701. case SRP_LOGIN_RSP: /* it worked! */
  702. break;
  703. case SRP_LOGIN_REJ: /* refused! */
  704. printk(KERN_INFO "ibmvscsi: SRP_LOGIN_REJ reason %u\n",
  705. evt_struct->xfer_iu->srp.login_rej.reason);
  706. /* Login failed. */
  707. atomic_set(&hostdata->request_limit, -1);
  708. return;
  709. default:
  710. printk(KERN_ERR
  711. "ibmvscsi: Invalid login response typecode 0x%02x!\n",
  712. evt_struct->xfer_iu->srp.login_rsp.opcode);
  713. /* Login failed. */
  714. atomic_set(&hostdata->request_limit, -1);
  715. return;
  716. }
  717. printk(KERN_INFO "ibmvscsi: SRP_LOGIN succeeded\n");
  718. if (evt_struct->xfer_iu->srp.login_rsp.req_lim_delta >
  719. (max_requests - 2))
  720. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta =
  721. max_requests - 2;
  722. /* Now we know what the real request-limit is */
  723. atomic_set(&hostdata->request_limit,
  724. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
  725. hostdata->host->can_queue =
  726. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta - 2;
  727. if (hostdata->host->can_queue < 1) {
  728. printk(KERN_ERR "ibmvscsi: Invalid request_limit_delta\n");
  729. return;
  730. }
  731. /* If we had any pending I/Os, kick them */
  732. scsi_unblock_requests(hostdata->host);
  733. send_mad_adapter_info(hostdata);
  734. return;
  735. }
  736. /**
  737. * send_srp_login: - Sends the srp login
  738. * @hostdata: ibmvscsi_host_data of host
  739. *
  740. * Returns zero if successful.
  741. */
  742. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  743. {
  744. int rc;
  745. unsigned long flags;
  746. struct srp_login_req *login;
  747. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  748. if (!evt_struct) {
  749. printk(KERN_ERR
  750. "ibmvscsi: couldn't allocate an event for login req!\n");
  751. return FAILED;
  752. }
  753. init_event_struct(evt_struct,
  754. login_rsp,
  755. VIOSRP_SRP_FORMAT,
  756. init_timeout * HZ);
  757. login = &evt_struct->iu.srp.login_req;
  758. memset(login, 0x00, sizeof(struct srp_login_req));
  759. login->opcode = SRP_LOGIN_REQ;
  760. login->req_it_iu_len = sizeof(union srp_iu);
  761. login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
  762. spin_lock_irqsave(hostdata->host->host_lock, flags);
  763. /* Start out with a request limit of 1, since this is negotiated in
  764. * the login request we are just sending
  765. */
  766. atomic_set(&hostdata->request_limit, 1);
  767. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  768. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  769. printk("ibmvscsic: sent SRP login\n");
  770. return rc;
  771. };
  772. /**
  773. * sync_completion: Signal that a synchronous command has completed
  774. * Note that after returning from this call, the evt_struct is freed.
  775. * the caller waiting on this completion shouldn't touch the evt_struct
  776. * again.
  777. */
  778. static void sync_completion(struct srp_event_struct *evt_struct)
  779. {
  780. /* copy the response back */
  781. if (evt_struct->sync_srp)
  782. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  783. complete(&evt_struct->comp);
  784. }
  785. /**
  786. * ibmvscsi_abort: Abort a command...from scsi host template
  787. * send this over to the server and wait synchronously for the response
  788. */
  789. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  790. {
  791. struct ibmvscsi_host_data *hostdata =
  792. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  793. struct srp_tsk_mgmt *tsk_mgmt;
  794. struct srp_event_struct *evt;
  795. struct srp_event_struct *tmp_evt, *found_evt;
  796. union viosrp_iu srp_rsp;
  797. int rsp_rc;
  798. unsigned long flags;
  799. u16 lun = lun_from_dev(cmd->device);
  800. /* First, find this command in our sent list so we can figure
  801. * out the correct tag
  802. */
  803. spin_lock_irqsave(hostdata->host->host_lock, flags);
  804. found_evt = NULL;
  805. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  806. if (tmp_evt->cmnd == cmd) {
  807. found_evt = tmp_evt;
  808. break;
  809. }
  810. }
  811. if (!found_evt) {
  812. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  813. return FAILED;
  814. }
  815. evt = get_event_struct(&hostdata->pool);
  816. if (evt == NULL) {
  817. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  818. printk(KERN_ERR "ibmvscsi: failed to allocate abort event\n");
  819. return FAILED;
  820. }
  821. init_event_struct(evt,
  822. sync_completion,
  823. VIOSRP_SRP_FORMAT,
  824. init_timeout * HZ);
  825. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  826. /* Set up an abort SRP command */
  827. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  828. tsk_mgmt->opcode = SRP_TSK_MGMT;
  829. tsk_mgmt->lun = ((u64) lun) << 48;
  830. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  831. tsk_mgmt->task_tag = (u64) found_evt;
  832. printk(KERN_INFO "ibmvscsi: aborting command. lun 0x%lx, tag 0x%lx\n",
  833. tsk_mgmt->lun, tsk_mgmt->task_tag);
  834. evt->sync_srp = &srp_rsp;
  835. init_completion(&evt->comp);
  836. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata);
  837. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  838. if (rsp_rc != 0) {
  839. printk(KERN_ERR "ibmvscsi: failed to send abort() event\n");
  840. return FAILED;
  841. }
  842. wait_for_completion(&evt->comp);
  843. /* make sure we got a good response */
  844. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  845. if (printk_ratelimit())
  846. printk(KERN_WARNING
  847. "ibmvscsi: abort bad SRP RSP type %d\n",
  848. srp_rsp.srp.rsp.opcode);
  849. return FAILED;
  850. }
  851. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  852. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  853. else
  854. rsp_rc = srp_rsp.srp.rsp.status;
  855. if (rsp_rc) {
  856. if (printk_ratelimit())
  857. printk(KERN_WARNING
  858. "ibmvscsi: abort code %d for task tag 0x%lx\n",
  859. rsp_rc,
  860. tsk_mgmt->task_tag);
  861. return FAILED;
  862. }
  863. /* Because we dropped the spinlock above, it's possible
  864. * The event is no longer in our list. Make sure it didn't
  865. * complete while we were aborting
  866. */
  867. spin_lock_irqsave(hostdata->host->host_lock, flags);
  868. found_evt = NULL;
  869. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  870. if (tmp_evt->cmnd == cmd) {
  871. found_evt = tmp_evt;
  872. break;
  873. }
  874. }
  875. if (found_evt == NULL) {
  876. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  877. printk(KERN_INFO
  878. "ibmvscsi: aborted task tag 0x%lx completed\n",
  879. tsk_mgmt->task_tag);
  880. return SUCCESS;
  881. }
  882. printk(KERN_INFO
  883. "ibmvscsi: successfully aborted task tag 0x%lx\n",
  884. tsk_mgmt->task_tag);
  885. cmd->result = (DID_ABORT << 16);
  886. list_del(&found_evt->list);
  887. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  888. found_evt->hostdata->dev);
  889. free_event_struct(&found_evt->hostdata->pool, found_evt);
  890. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  891. atomic_inc(&hostdata->request_limit);
  892. return SUCCESS;
  893. }
  894. /**
  895. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  896. * template send this over to the server and wait synchronously for the
  897. * response
  898. */
  899. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  900. {
  901. struct ibmvscsi_host_data *hostdata =
  902. (struct ibmvscsi_host_data *)cmd->device->host->hostdata;
  903. struct srp_tsk_mgmt *tsk_mgmt;
  904. struct srp_event_struct *evt;
  905. struct srp_event_struct *tmp_evt, *pos;
  906. union viosrp_iu srp_rsp;
  907. int rsp_rc;
  908. unsigned long flags;
  909. u16 lun = lun_from_dev(cmd->device);
  910. spin_lock_irqsave(hostdata->host->host_lock, flags);
  911. evt = get_event_struct(&hostdata->pool);
  912. if (evt == NULL) {
  913. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  914. printk(KERN_ERR "ibmvscsi: failed to allocate reset event\n");
  915. return FAILED;
  916. }
  917. init_event_struct(evt,
  918. sync_completion,
  919. VIOSRP_SRP_FORMAT,
  920. init_timeout * HZ);
  921. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  922. /* Set up a lun reset SRP command */
  923. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  924. tsk_mgmt->opcode = SRP_TSK_MGMT;
  925. tsk_mgmt->lun = ((u64) lun) << 48;
  926. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  927. printk(KERN_INFO "ibmvscsi: resetting device. lun 0x%lx\n",
  928. tsk_mgmt->lun);
  929. evt->sync_srp = &srp_rsp;
  930. init_completion(&evt->comp);
  931. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata);
  932. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  933. if (rsp_rc != 0) {
  934. printk(KERN_ERR "ibmvscsi: failed to send reset event\n");
  935. return FAILED;
  936. }
  937. wait_for_completion(&evt->comp);
  938. /* make sure we got a good response */
  939. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  940. if (printk_ratelimit())
  941. printk(KERN_WARNING
  942. "ibmvscsi: reset bad SRP RSP type %d\n",
  943. srp_rsp.srp.rsp.opcode);
  944. return FAILED;
  945. }
  946. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  947. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  948. else
  949. rsp_rc = srp_rsp.srp.rsp.status;
  950. if (rsp_rc) {
  951. if (printk_ratelimit())
  952. printk(KERN_WARNING
  953. "ibmvscsi: reset code %d for task tag 0x%lx\n",
  954. rsp_rc, tsk_mgmt->task_tag);
  955. return FAILED;
  956. }
  957. /* We need to find all commands for this LUN that have not yet been
  958. * responded to, and fail them with DID_RESET
  959. */
  960. spin_lock_irqsave(hostdata->host->host_lock, flags);
  961. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  962. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  963. if (tmp_evt->cmnd)
  964. tmp_evt->cmnd->result = (DID_RESET << 16);
  965. list_del(&tmp_evt->list);
  966. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  967. tmp_evt->hostdata->dev);
  968. free_event_struct(&tmp_evt->hostdata->pool,
  969. tmp_evt);
  970. atomic_inc(&hostdata->request_limit);
  971. if (tmp_evt->cmnd_done)
  972. tmp_evt->cmnd_done(tmp_evt->cmnd);
  973. else if (tmp_evt->done)
  974. tmp_evt->done(tmp_evt);
  975. }
  976. }
  977. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  978. return SUCCESS;
  979. }
  980. /**
  981. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  982. * @hostdata: the adapter
  983. */
  984. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  985. {
  986. struct srp_event_struct *tmp_evt, *pos;
  987. unsigned long flags;
  988. spin_lock_irqsave(hostdata->host->host_lock, flags);
  989. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  990. list_del(&tmp_evt->list);
  991. if (tmp_evt->cmnd) {
  992. tmp_evt->cmnd->result = (error_code << 16);
  993. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  994. tmp_evt,
  995. tmp_evt->hostdata->dev);
  996. if (tmp_evt->cmnd_done)
  997. tmp_evt->cmnd_done(tmp_evt->cmnd);
  998. } else {
  999. if (tmp_evt->done) {
  1000. tmp_evt->done(tmp_evt);
  1001. }
  1002. }
  1003. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  1004. }
  1005. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1006. }
  1007. /**
  1008. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1009. * @crq: Command/Response queue
  1010. * @hostdata: ibmvscsi_host_data of host
  1011. *
  1012. */
  1013. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1014. struct ibmvscsi_host_data *hostdata)
  1015. {
  1016. unsigned long flags;
  1017. struct srp_event_struct *evt_struct =
  1018. (struct srp_event_struct *)crq->IU_data_ptr;
  1019. switch (crq->valid) {
  1020. case 0xC0: /* initialization */
  1021. switch (crq->format) {
  1022. case 0x01: /* Initialization message */
  1023. printk(KERN_INFO "ibmvscsi: partner initialized\n");
  1024. /* Send back a response */
  1025. if (ibmvscsi_send_crq(hostdata,
  1026. 0xC002000000000000LL, 0) == 0) {
  1027. /* Now login */
  1028. send_srp_login(hostdata);
  1029. } else {
  1030. printk(KERN_ERR
  1031. "ibmvscsi: Unable to send init rsp\n");
  1032. }
  1033. break;
  1034. case 0x02: /* Initialization response */
  1035. printk(KERN_INFO
  1036. "ibmvscsi: partner initialization complete\n");
  1037. /* Now login */
  1038. send_srp_login(hostdata);
  1039. break;
  1040. default:
  1041. printk(KERN_ERR "ibmvscsi: unknown crq message type\n");
  1042. }
  1043. return;
  1044. case 0xFF: /* Hypervisor telling us the connection is closed */
  1045. scsi_block_requests(hostdata->host);
  1046. if (crq->format == 0x06) {
  1047. /* We need to re-setup the interpartition connection */
  1048. printk(KERN_INFO
  1049. "ibmvscsi: Re-enabling adapter!\n");
  1050. atomic_set(&hostdata->request_limit, -1);
  1051. purge_requests(hostdata, DID_REQUEUE);
  1052. if (ibmvscsi_reenable_crq_queue(&hostdata->queue,
  1053. hostdata) == 0)
  1054. if (ibmvscsi_send_crq(hostdata,
  1055. 0xC001000000000000LL, 0))
  1056. printk(KERN_ERR
  1057. "ibmvscsi: transmit error after"
  1058. " enable\n");
  1059. } else {
  1060. printk(KERN_INFO
  1061. "ibmvscsi: Virtual adapter failed rc %d!\n",
  1062. crq->format);
  1063. atomic_set(&hostdata->request_limit, -1);
  1064. purge_requests(hostdata, DID_ERROR);
  1065. ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
  1066. }
  1067. scsi_unblock_requests(hostdata->host);
  1068. return;
  1069. case 0x80: /* real payload */
  1070. break;
  1071. default:
  1072. printk(KERN_ERR
  1073. "ibmvscsi: got an invalid message type 0x%02x\n",
  1074. crq->valid);
  1075. return;
  1076. }
  1077. /* The only kind of payload CRQs we should get are responses to
  1078. * things we send. Make sure this response is to something we
  1079. * actually sent
  1080. */
  1081. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1082. printk(KERN_ERR
  1083. "ibmvscsi: returned correlation_token 0x%p is invalid!\n",
  1084. (void *)crq->IU_data_ptr);
  1085. return;
  1086. }
  1087. if (atomic_read(&evt_struct->free)) {
  1088. printk(KERN_ERR
  1089. "ibmvscsi: received duplicate correlation_token 0x%p!\n",
  1090. (void *)crq->IU_data_ptr);
  1091. return;
  1092. }
  1093. if (crq->format == VIOSRP_SRP_FORMAT)
  1094. atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
  1095. &hostdata->request_limit);
  1096. if (evt_struct->done)
  1097. evt_struct->done(evt_struct);
  1098. else
  1099. printk(KERN_ERR
  1100. "ibmvscsi: returned done() is NULL; not running it!\n");
  1101. /*
  1102. * Lock the host_lock before messing with these structures, since we
  1103. * are running in a task context
  1104. */
  1105. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1106. list_del(&evt_struct->list);
  1107. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1108. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1109. }
  1110. /**
  1111. * ibmvscsi_get_host_config: Send the command to the server to get host
  1112. * configuration data. The data is opaque to us.
  1113. */
  1114. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1115. unsigned char *buffer, int length)
  1116. {
  1117. struct viosrp_host_config *host_config;
  1118. struct srp_event_struct *evt_struct;
  1119. int rc;
  1120. evt_struct = get_event_struct(&hostdata->pool);
  1121. if (!evt_struct) {
  1122. printk(KERN_ERR
  1123. "ibmvscsi: could't allocate event for HOST_CONFIG!\n");
  1124. return -1;
  1125. }
  1126. init_event_struct(evt_struct,
  1127. sync_completion,
  1128. VIOSRP_MAD_FORMAT,
  1129. init_timeout * HZ);
  1130. host_config = &evt_struct->iu.mad.host_config;
  1131. /* Set up a lun reset SRP command */
  1132. memset(host_config, 0x00, sizeof(*host_config));
  1133. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1134. host_config->common.length = length;
  1135. host_config->buffer = dma_map_single(hostdata->dev, buffer, length,
  1136. DMA_BIDIRECTIONAL);
  1137. if (dma_mapping_error(host_config->buffer)) {
  1138. printk(KERN_ERR
  1139. "ibmvscsi: dma_mapping error " "getting host config\n");
  1140. free_event_struct(&hostdata->pool, evt_struct);
  1141. return -1;
  1142. }
  1143. init_completion(&evt_struct->comp);
  1144. rc = ibmvscsi_send_srp_event(evt_struct, hostdata);
  1145. if (rc == 0) {
  1146. wait_for_completion(&evt_struct->comp);
  1147. dma_unmap_single(hostdata->dev, host_config->buffer,
  1148. length, DMA_BIDIRECTIONAL);
  1149. }
  1150. return rc;
  1151. }
  1152. /* ------------------------------------------------------------
  1153. * sysfs attributes
  1154. */
  1155. static ssize_t show_host_srp_version(struct class_device *class_dev, char *buf)
  1156. {
  1157. struct Scsi_Host *shost = class_to_shost(class_dev);
  1158. struct ibmvscsi_host_data *hostdata =
  1159. (struct ibmvscsi_host_data *)shost->hostdata;
  1160. int len;
  1161. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1162. hostdata->madapter_info.srp_version);
  1163. return len;
  1164. }
  1165. static struct class_device_attribute ibmvscsi_host_srp_version = {
  1166. .attr = {
  1167. .name = "srp_version",
  1168. .mode = S_IRUGO,
  1169. },
  1170. .show = show_host_srp_version,
  1171. };
  1172. static ssize_t show_host_partition_name(struct class_device *class_dev,
  1173. char *buf)
  1174. {
  1175. struct Scsi_Host *shost = class_to_shost(class_dev);
  1176. struct ibmvscsi_host_data *hostdata =
  1177. (struct ibmvscsi_host_data *)shost->hostdata;
  1178. int len;
  1179. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1180. hostdata->madapter_info.partition_name);
  1181. return len;
  1182. }
  1183. static struct class_device_attribute ibmvscsi_host_partition_name = {
  1184. .attr = {
  1185. .name = "partition_name",
  1186. .mode = S_IRUGO,
  1187. },
  1188. .show = show_host_partition_name,
  1189. };
  1190. static ssize_t show_host_partition_number(struct class_device *class_dev,
  1191. char *buf)
  1192. {
  1193. struct Scsi_Host *shost = class_to_shost(class_dev);
  1194. struct ibmvscsi_host_data *hostdata =
  1195. (struct ibmvscsi_host_data *)shost->hostdata;
  1196. int len;
  1197. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1198. hostdata->madapter_info.partition_number);
  1199. return len;
  1200. }
  1201. static struct class_device_attribute ibmvscsi_host_partition_number = {
  1202. .attr = {
  1203. .name = "partition_number",
  1204. .mode = S_IRUGO,
  1205. },
  1206. .show = show_host_partition_number,
  1207. };
  1208. static ssize_t show_host_mad_version(struct class_device *class_dev, char *buf)
  1209. {
  1210. struct Scsi_Host *shost = class_to_shost(class_dev);
  1211. struct ibmvscsi_host_data *hostdata =
  1212. (struct ibmvscsi_host_data *)shost->hostdata;
  1213. int len;
  1214. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1215. hostdata->madapter_info.mad_version);
  1216. return len;
  1217. }
  1218. static struct class_device_attribute ibmvscsi_host_mad_version = {
  1219. .attr = {
  1220. .name = "mad_version",
  1221. .mode = S_IRUGO,
  1222. },
  1223. .show = show_host_mad_version,
  1224. };
  1225. static ssize_t show_host_os_type(struct class_device *class_dev, char *buf)
  1226. {
  1227. struct Scsi_Host *shost = class_to_shost(class_dev);
  1228. struct ibmvscsi_host_data *hostdata =
  1229. (struct ibmvscsi_host_data *)shost->hostdata;
  1230. int len;
  1231. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1232. return len;
  1233. }
  1234. static struct class_device_attribute ibmvscsi_host_os_type = {
  1235. .attr = {
  1236. .name = "os_type",
  1237. .mode = S_IRUGO,
  1238. },
  1239. .show = show_host_os_type,
  1240. };
  1241. static ssize_t show_host_config(struct class_device *class_dev, char *buf)
  1242. {
  1243. struct Scsi_Host *shost = class_to_shost(class_dev);
  1244. struct ibmvscsi_host_data *hostdata =
  1245. (struct ibmvscsi_host_data *)shost->hostdata;
  1246. /* returns null-terminated host config data */
  1247. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1248. return strlen(buf);
  1249. else
  1250. return 0;
  1251. }
  1252. static struct class_device_attribute ibmvscsi_host_config = {
  1253. .attr = {
  1254. .name = "config",
  1255. .mode = S_IRUGO,
  1256. },
  1257. .show = show_host_config,
  1258. };
  1259. static struct class_device_attribute *ibmvscsi_attrs[] = {
  1260. &ibmvscsi_host_srp_version,
  1261. &ibmvscsi_host_partition_name,
  1262. &ibmvscsi_host_partition_number,
  1263. &ibmvscsi_host_mad_version,
  1264. &ibmvscsi_host_os_type,
  1265. &ibmvscsi_host_config,
  1266. NULL
  1267. };
  1268. /* ------------------------------------------------------------
  1269. * SCSI driver registration
  1270. */
  1271. static struct scsi_host_template driver_template = {
  1272. .module = THIS_MODULE,
  1273. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1274. .proc_name = "ibmvscsi",
  1275. .queuecommand = ibmvscsi_queuecommand,
  1276. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1277. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1278. .cmd_per_lun = 16,
  1279. .can_queue = 1, /* Updated after SRP_LOGIN */
  1280. .this_id = -1,
  1281. .sg_tablesize = SG_ALL,
  1282. .use_clustering = ENABLE_CLUSTERING,
  1283. .shost_attrs = ibmvscsi_attrs,
  1284. };
  1285. /**
  1286. * Called by bus code for each adapter
  1287. */
  1288. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1289. {
  1290. struct ibmvscsi_host_data *hostdata;
  1291. struct Scsi_Host *host;
  1292. struct device *dev = &vdev->dev;
  1293. unsigned long wait_switch = 0;
  1294. vdev->dev.driver_data = NULL;
  1295. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1296. if (!host) {
  1297. printk(KERN_ERR "ibmvscsi: couldn't allocate host data\n");
  1298. goto scsi_host_alloc_failed;
  1299. }
  1300. hostdata = (struct ibmvscsi_host_data *)host->hostdata;
  1301. memset(hostdata, 0x00, sizeof(*hostdata));
  1302. INIT_LIST_HEAD(&hostdata->sent);
  1303. hostdata->host = host;
  1304. hostdata->dev = dev;
  1305. atomic_set(&hostdata->request_limit, -1);
  1306. hostdata->host->max_sectors = 32 * 8; /* default max I/O 32 pages */
  1307. if (ibmvscsi_init_crq_queue(&hostdata->queue, hostdata,
  1308. max_requests) != 0) {
  1309. printk(KERN_ERR "ibmvscsi: couldn't initialize crq\n");
  1310. goto init_crq_failed;
  1311. }
  1312. if (initialize_event_pool(&hostdata->pool, max_requests, hostdata) != 0) {
  1313. printk(KERN_ERR "ibmvscsi: couldn't initialize event pool\n");
  1314. goto init_pool_failed;
  1315. }
  1316. host->max_lun = 8;
  1317. host->max_id = max_id;
  1318. host->max_channel = max_channel;
  1319. if (scsi_add_host(hostdata->host, hostdata->dev))
  1320. goto add_host_failed;
  1321. /* Try to send an initialization message. Note that this is allowed
  1322. * to fail if the other end is not acive. In that case we don't
  1323. * want to scan
  1324. */
  1325. if (ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0) == 0) {
  1326. /*
  1327. * Wait around max init_timeout secs for the adapter to finish
  1328. * initializing. When we are done initializing, we will have a
  1329. * valid request_limit. We don't want Linux scanning before
  1330. * we are ready.
  1331. */
  1332. for (wait_switch = jiffies + (init_timeout * HZ);
  1333. time_before(jiffies, wait_switch) &&
  1334. atomic_read(&hostdata->request_limit) < 2;) {
  1335. msleep(10);
  1336. }
  1337. /* if we now have a valid request_limit, initiate a scan */
  1338. if (atomic_read(&hostdata->request_limit) > 0)
  1339. scsi_scan_host(host);
  1340. }
  1341. vdev->dev.driver_data = hostdata;
  1342. return 0;
  1343. add_host_failed:
  1344. release_event_pool(&hostdata->pool, hostdata);
  1345. init_pool_failed:
  1346. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata, max_requests);
  1347. init_crq_failed:
  1348. scsi_host_put(host);
  1349. scsi_host_alloc_failed:
  1350. return -1;
  1351. }
  1352. static int ibmvscsi_remove(struct vio_dev *vdev)
  1353. {
  1354. struct ibmvscsi_host_data *hostdata = vdev->dev.driver_data;
  1355. release_event_pool(&hostdata->pool, hostdata);
  1356. ibmvscsi_release_crq_queue(&hostdata->queue, hostdata,
  1357. max_requests);
  1358. scsi_remove_host(hostdata->host);
  1359. scsi_host_put(hostdata->host);
  1360. return 0;
  1361. }
  1362. /**
  1363. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1364. * support.
  1365. */
  1366. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1367. {"vscsi", "IBM,v-scsi"},
  1368. { "", "" }
  1369. };
  1370. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1371. static struct vio_driver ibmvscsi_driver = {
  1372. .id_table = ibmvscsi_device_table,
  1373. .probe = ibmvscsi_probe,
  1374. .remove = ibmvscsi_remove,
  1375. .driver = {
  1376. .name = "ibmvscsi",
  1377. .owner = THIS_MODULE,
  1378. }
  1379. };
  1380. int __init ibmvscsi_module_init(void)
  1381. {
  1382. return vio_register_driver(&ibmvscsi_driver);
  1383. }
  1384. void __exit ibmvscsi_module_exit(void)
  1385. {
  1386. vio_unregister_driver(&ibmvscsi_driver);
  1387. }
  1388. module_init(ibmvscsi_module_init);
  1389. module_exit(ibmvscsi_module_exit);