ibmvscsi.c 45 KB

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