ibmvscsi.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  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-robin 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 <linux/slab.h>
  73. #include <linux/of.h>
  74. #include <linux/pm.h>
  75. #include <asm/firmware.h>
  76. #include <asm/vio.h>
  77. #include <scsi/scsi.h>
  78. #include <scsi/scsi_cmnd.h>
  79. #include <scsi/scsi_host.h>
  80. #include <scsi/scsi_device.h>
  81. #include <scsi/scsi_transport_srp.h>
  82. #include "ibmvscsi.h"
  83. /* The values below are somewhat arbitrary default values, but
  84. * OS/400 will use 3 busses (disks, CDs, tapes, I think.)
  85. * Note that there are 3 bits of channel value, 6 bits of id, and
  86. * 5 bits of LUN.
  87. */
  88. static int max_id = 64;
  89. static int max_channel = 3;
  90. static int init_timeout = 300;
  91. static int login_timeout = 60;
  92. static int info_timeout = 30;
  93. static int abort_timeout = 60;
  94. static int reset_timeout = 60;
  95. static int max_requests = IBMVSCSI_MAX_REQUESTS_DEFAULT;
  96. static int max_events = IBMVSCSI_MAX_REQUESTS_DEFAULT + 2;
  97. static int fast_fail = 1;
  98. static int client_reserve = 1;
  99. static struct scsi_transport_template *ibmvscsi_transport_template;
  100. #define IBMVSCSI_VERSION "1.5.8"
  101. static struct ibmvscsi_ops *ibmvscsi_ops;
  102. MODULE_DESCRIPTION("IBM Virtual SCSI");
  103. MODULE_AUTHOR("Dave Boutcher");
  104. MODULE_LICENSE("GPL");
  105. MODULE_VERSION(IBMVSCSI_VERSION);
  106. module_param_named(max_id, max_id, int, S_IRUGO | S_IWUSR);
  107. MODULE_PARM_DESC(max_id, "Largest ID value for each channel");
  108. module_param_named(max_channel, max_channel, int, S_IRUGO | S_IWUSR);
  109. MODULE_PARM_DESC(max_channel, "Largest channel value");
  110. module_param_named(init_timeout, init_timeout, int, S_IRUGO | S_IWUSR);
  111. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds");
  112. module_param_named(max_requests, max_requests, int, S_IRUGO);
  113. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter");
  114. module_param_named(fast_fail, fast_fail, int, S_IRUGO | S_IWUSR);
  115. MODULE_PARM_DESC(fast_fail, "Enable fast fail. [Default=1]");
  116. module_param_named(client_reserve, client_reserve, int, S_IRUGO );
  117. MODULE_PARM_DESC(client_reserve, "Attempt client managed reserve/release");
  118. /* ------------------------------------------------------------
  119. * Routines for the event pool and event structs
  120. */
  121. /**
  122. * initialize_event_pool: - Allocates and initializes the event pool for a host
  123. * @pool: event_pool to be initialized
  124. * @size: Number of events in pool
  125. * @hostdata: ibmvscsi_host_data who owns the event pool
  126. *
  127. * Returns zero on success.
  128. */
  129. static int initialize_event_pool(struct event_pool *pool,
  130. int size, struct ibmvscsi_host_data *hostdata)
  131. {
  132. int i;
  133. pool->size = size;
  134. pool->next = 0;
  135. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  136. if (!pool->events)
  137. return -ENOMEM;
  138. pool->iu_storage =
  139. dma_alloc_coherent(hostdata->dev,
  140. pool->size * sizeof(*pool->iu_storage),
  141. &pool->iu_token, 0);
  142. if (!pool->iu_storage) {
  143. kfree(pool->events);
  144. return -ENOMEM;
  145. }
  146. for (i = 0; i < pool->size; ++i) {
  147. struct srp_event_struct *evt = &pool->events[i];
  148. memset(&evt->crq, 0x00, sizeof(evt->crq));
  149. atomic_set(&evt->free, 1);
  150. evt->crq.valid = 0x80;
  151. evt->crq.IU_length = sizeof(*evt->xfer_iu);
  152. evt->crq.IU_data_ptr = pool->iu_token +
  153. sizeof(*evt->xfer_iu) * i;
  154. evt->xfer_iu = pool->iu_storage + i;
  155. evt->hostdata = hostdata;
  156. evt->ext_list = NULL;
  157. evt->ext_list_token = 0;
  158. }
  159. return 0;
  160. }
  161. /**
  162. * release_event_pool: - Frees memory of an event pool of a host
  163. * @pool: event_pool to be released
  164. * @hostdata: ibmvscsi_host_data who owns the even pool
  165. *
  166. * Returns zero on success.
  167. */
  168. static void release_event_pool(struct event_pool *pool,
  169. struct ibmvscsi_host_data *hostdata)
  170. {
  171. int i, in_use = 0;
  172. for (i = 0; i < pool->size; ++i) {
  173. if (atomic_read(&pool->events[i].free) != 1)
  174. ++in_use;
  175. if (pool->events[i].ext_list) {
  176. dma_free_coherent(hostdata->dev,
  177. SG_ALL * sizeof(struct srp_direct_buf),
  178. pool->events[i].ext_list,
  179. pool->events[i].ext_list_token);
  180. }
  181. }
  182. if (in_use)
  183. dev_warn(hostdata->dev, "releasing event pool with %d "
  184. "events still in use?\n", in_use);
  185. kfree(pool->events);
  186. dma_free_coherent(hostdata->dev,
  187. pool->size * sizeof(*pool->iu_storage),
  188. pool->iu_storage, pool->iu_token);
  189. }
  190. /**
  191. * valid_event_struct: - Determines if event is valid.
  192. * @pool: event_pool that contains the event
  193. * @evt: srp_event_struct to be checked for validity
  194. *
  195. * Returns zero if event is invalid, one otherwise.
  196. */
  197. static int valid_event_struct(struct event_pool *pool,
  198. struct srp_event_struct *evt)
  199. {
  200. int index = evt - pool->events;
  201. if (index < 0 || index >= pool->size) /* outside of bounds */
  202. return 0;
  203. if (evt != pool->events + index) /* unaligned */
  204. return 0;
  205. return 1;
  206. }
  207. /**
  208. * ibmvscsi_free-event_struct: - Changes status of event to "free"
  209. * @pool: event_pool that contains the event
  210. * @evt: srp_event_struct to be modified
  211. *
  212. */
  213. static void free_event_struct(struct event_pool *pool,
  214. struct srp_event_struct *evt)
  215. {
  216. if (!valid_event_struct(pool, evt)) {
  217. dev_err(evt->hostdata->dev, "Freeing invalid event_struct %p "
  218. "(not in pool %p)\n", evt, pool->events);
  219. return;
  220. }
  221. if (atomic_inc_return(&evt->free) != 1) {
  222. dev_err(evt->hostdata->dev, "Freeing event_struct %p "
  223. "which is not in use!\n", evt);
  224. return;
  225. }
  226. }
  227. /**
  228. * get_evt_struct: - Gets the next free event in pool
  229. * @pool: event_pool that contains the events to be searched
  230. *
  231. * Returns the next event in "free" state, and NULL if none are free.
  232. * Note that no synchronization is done here, we assume the host_lock
  233. * will syncrhonze things.
  234. */
  235. static struct srp_event_struct *get_event_struct(struct event_pool *pool)
  236. {
  237. int i;
  238. int poolsize = pool->size;
  239. int offset = pool->next;
  240. for (i = 0; i < poolsize; i++) {
  241. offset = (offset + 1) % poolsize;
  242. if (!atomic_dec_if_positive(&pool->events[offset].free)) {
  243. pool->next = offset;
  244. return &pool->events[offset];
  245. }
  246. }
  247. printk(KERN_ERR "ibmvscsi: found no event struct in pool!\n");
  248. return NULL;
  249. }
  250. /**
  251. * init_event_struct: Initialize fields in an event struct that are always
  252. * required.
  253. * @evt: The event
  254. * @done: Routine to call when the event is responded to
  255. * @format: SRP or MAD format
  256. * @timeout: timeout value set in the CRQ
  257. */
  258. static void init_event_struct(struct srp_event_struct *evt_struct,
  259. void (*done) (struct srp_event_struct *),
  260. u8 format,
  261. int timeout)
  262. {
  263. evt_struct->cmnd = NULL;
  264. evt_struct->cmnd_done = NULL;
  265. evt_struct->sync_srp = NULL;
  266. evt_struct->crq.format = format;
  267. evt_struct->crq.timeout = timeout;
  268. evt_struct->done = done;
  269. }
  270. /* ------------------------------------------------------------
  271. * Routines for receiving SCSI responses from the hosting partition
  272. */
  273. /**
  274. * set_srp_direction: Set the fields in the srp related to data
  275. * direction and number of buffers based on the direction in
  276. * the scsi_cmnd and the number of buffers
  277. */
  278. static void set_srp_direction(struct scsi_cmnd *cmd,
  279. struct srp_cmd *srp_cmd,
  280. int numbuf)
  281. {
  282. u8 fmt;
  283. if (numbuf == 0)
  284. return;
  285. if (numbuf == 1)
  286. fmt = SRP_DATA_DESC_DIRECT;
  287. else {
  288. fmt = SRP_DATA_DESC_INDIRECT;
  289. numbuf = min(numbuf, MAX_INDIRECT_BUFS);
  290. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  291. srp_cmd->data_out_desc_cnt = numbuf;
  292. else
  293. srp_cmd->data_in_desc_cnt = numbuf;
  294. }
  295. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  296. srp_cmd->buf_fmt = fmt << 4;
  297. else
  298. srp_cmd->buf_fmt = fmt;
  299. }
  300. static void unmap_sg_list(int num_entries,
  301. struct device *dev,
  302. struct srp_direct_buf *md)
  303. {
  304. int i;
  305. for (i = 0; i < num_entries; ++i)
  306. dma_unmap_single(dev, md[i].va, md[i].len, DMA_BIDIRECTIONAL);
  307. }
  308. /**
  309. * unmap_cmd_data: - Unmap data pointed in srp_cmd based on the format
  310. * @cmd: srp_cmd whose additional_data member will be unmapped
  311. * @dev: device for which the memory is mapped
  312. *
  313. */
  314. static void unmap_cmd_data(struct srp_cmd *cmd,
  315. struct srp_event_struct *evt_struct,
  316. struct device *dev)
  317. {
  318. u8 out_fmt, in_fmt;
  319. out_fmt = cmd->buf_fmt >> 4;
  320. in_fmt = cmd->buf_fmt & ((1U << 4) - 1);
  321. if (out_fmt == SRP_NO_DATA_DESC && in_fmt == SRP_NO_DATA_DESC)
  322. return;
  323. else if (out_fmt == SRP_DATA_DESC_DIRECT ||
  324. in_fmt == SRP_DATA_DESC_DIRECT) {
  325. struct srp_direct_buf *data =
  326. (struct srp_direct_buf *) cmd->add_data;
  327. dma_unmap_single(dev, data->va, data->len, DMA_BIDIRECTIONAL);
  328. } else {
  329. struct srp_indirect_buf *indirect =
  330. (struct srp_indirect_buf *) cmd->add_data;
  331. int num_mapped = indirect->table_desc.len /
  332. sizeof(struct srp_direct_buf);
  333. if (num_mapped <= MAX_INDIRECT_BUFS) {
  334. unmap_sg_list(num_mapped, dev, &indirect->desc_list[0]);
  335. return;
  336. }
  337. unmap_sg_list(num_mapped, dev, evt_struct->ext_list);
  338. }
  339. }
  340. static int map_sg_list(struct scsi_cmnd *cmd, int nseg,
  341. struct srp_direct_buf *md)
  342. {
  343. int i;
  344. struct scatterlist *sg;
  345. u64 total_length = 0;
  346. scsi_for_each_sg(cmd, sg, nseg, i) {
  347. struct srp_direct_buf *descr = md + i;
  348. descr->va = sg_dma_address(sg);
  349. descr->len = sg_dma_len(sg);
  350. descr->key = 0;
  351. total_length += sg_dma_len(sg);
  352. }
  353. return total_length;
  354. }
  355. /**
  356. * map_sg_data: - Maps dma for a scatterlist and initializes decriptor fields
  357. * @cmd: Scsi_Cmnd with the scatterlist
  358. * @srp_cmd: srp_cmd that contains the memory descriptor
  359. * @dev: device for which to map dma memory
  360. *
  361. * Called by map_data_for_srp_cmd() when building srp cmd from scsi cmd.
  362. * Returns 1 on success.
  363. */
  364. static int map_sg_data(struct scsi_cmnd *cmd,
  365. struct srp_event_struct *evt_struct,
  366. struct srp_cmd *srp_cmd, struct device *dev)
  367. {
  368. int sg_mapped;
  369. u64 total_length = 0;
  370. struct srp_direct_buf *data =
  371. (struct srp_direct_buf *) srp_cmd->add_data;
  372. struct srp_indirect_buf *indirect =
  373. (struct srp_indirect_buf *) data;
  374. sg_mapped = scsi_dma_map(cmd);
  375. if (!sg_mapped)
  376. return 1;
  377. else if (sg_mapped < 0)
  378. return 0;
  379. set_srp_direction(cmd, srp_cmd, sg_mapped);
  380. /* special case; we can use a single direct descriptor */
  381. if (sg_mapped == 1) {
  382. map_sg_list(cmd, sg_mapped, data);
  383. return 1;
  384. }
  385. indirect->table_desc.va = 0;
  386. indirect->table_desc.len = sg_mapped * sizeof(struct srp_direct_buf);
  387. indirect->table_desc.key = 0;
  388. if (sg_mapped <= MAX_INDIRECT_BUFS) {
  389. total_length = map_sg_list(cmd, sg_mapped,
  390. &indirect->desc_list[0]);
  391. indirect->len = total_length;
  392. return 1;
  393. }
  394. /* get indirect table */
  395. if (!evt_struct->ext_list) {
  396. evt_struct->ext_list = (struct srp_direct_buf *)
  397. dma_alloc_coherent(dev,
  398. SG_ALL * sizeof(struct srp_direct_buf),
  399. &evt_struct->ext_list_token, 0);
  400. if (!evt_struct->ext_list) {
  401. if (!firmware_has_feature(FW_FEATURE_CMO))
  402. sdev_printk(KERN_ERR, cmd->device,
  403. "Can't allocate memory "
  404. "for indirect table\n");
  405. scsi_dma_unmap(cmd);
  406. return 0;
  407. }
  408. }
  409. total_length = map_sg_list(cmd, sg_mapped, evt_struct->ext_list);
  410. indirect->len = total_length;
  411. indirect->table_desc.va = evt_struct->ext_list_token;
  412. indirect->table_desc.len = sg_mapped * sizeof(indirect->desc_list[0]);
  413. memcpy(indirect->desc_list, evt_struct->ext_list,
  414. MAX_INDIRECT_BUFS * sizeof(struct srp_direct_buf));
  415. return 1;
  416. }
  417. /**
  418. * map_data_for_srp_cmd: - Calls functions to map data for srp cmds
  419. * @cmd: struct scsi_cmnd with the memory to be mapped
  420. * @srp_cmd: srp_cmd that contains the memory descriptor
  421. * @dev: dma device for which to map dma memory
  422. *
  423. * Called by scsi_cmd_to_srp_cmd() when converting scsi cmds to srp cmds
  424. * Returns 1 on success.
  425. */
  426. static int map_data_for_srp_cmd(struct scsi_cmnd *cmd,
  427. struct srp_event_struct *evt_struct,
  428. struct srp_cmd *srp_cmd, struct device *dev)
  429. {
  430. switch (cmd->sc_data_direction) {
  431. case DMA_FROM_DEVICE:
  432. case DMA_TO_DEVICE:
  433. break;
  434. case DMA_NONE:
  435. return 1;
  436. case DMA_BIDIRECTIONAL:
  437. sdev_printk(KERN_ERR, cmd->device,
  438. "Can't map DMA_BIDIRECTIONAL to read/write\n");
  439. return 0;
  440. default:
  441. sdev_printk(KERN_ERR, cmd->device,
  442. "Unknown data direction 0x%02x; can't map!\n",
  443. cmd->sc_data_direction);
  444. return 0;
  445. }
  446. return map_sg_data(cmd, evt_struct, srp_cmd, dev);
  447. }
  448. /**
  449. * purge_requests: Our virtual adapter just shut down. purge any sent requests
  450. * @hostdata: the adapter
  451. */
  452. static void purge_requests(struct ibmvscsi_host_data *hostdata, int error_code)
  453. {
  454. struct srp_event_struct *tmp_evt, *pos;
  455. unsigned long flags;
  456. spin_lock_irqsave(hostdata->host->host_lock, flags);
  457. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  458. list_del(&tmp_evt->list);
  459. del_timer(&tmp_evt->timer);
  460. if (tmp_evt->cmnd) {
  461. tmp_evt->cmnd->result = (error_code << 16);
  462. unmap_cmd_data(&tmp_evt->iu.srp.cmd,
  463. tmp_evt,
  464. tmp_evt->hostdata->dev);
  465. if (tmp_evt->cmnd_done)
  466. tmp_evt->cmnd_done(tmp_evt->cmnd);
  467. } else if (tmp_evt->done)
  468. tmp_evt->done(tmp_evt);
  469. free_event_struct(&tmp_evt->hostdata->pool, tmp_evt);
  470. }
  471. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  472. }
  473. /**
  474. * ibmvscsi_reset_host - Reset the connection to the server
  475. * @hostdata: struct ibmvscsi_host_data to reset
  476. */
  477. static void ibmvscsi_reset_host(struct ibmvscsi_host_data *hostdata)
  478. {
  479. scsi_block_requests(hostdata->host);
  480. atomic_set(&hostdata->request_limit, 0);
  481. purge_requests(hostdata, DID_ERROR);
  482. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue, hostdata)) ||
  483. (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0)) ||
  484. (vio_enable_interrupts(to_vio_dev(hostdata->dev)))) {
  485. atomic_set(&hostdata->request_limit, -1);
  486. dev_err(hostdata->dev, "error after reset\n");
  487. }
  488. scsi_unblock_requests(hostdata->host);
  489. }
  490. /**
  491. * ibmvscsi_timeout - Internal command timeout handler
  492. * @evt_struct: struct srp_event_struct that timed out
  493. *
  494. * Called when an internally generated command times out
  495. */
  496. static void ibmvscsi_timeout(struct srp_event_struct *evt_struct)
  497. {
  498. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  499. dev_err(hostdata->dev, "Command timed out (%x). Resetting connection\n",
  500. evt_struct->iu.srp.cmd.opcode);
  501. ibmvscsi_reset_host(hostdata);
  502. }
  503. /* ------------------------------------------------------------
  504. * Routines for sending and receiving SRPs
  505. */
  506. /**
  507. * ibmvscsi_send_srp_event: - Transforms event to u64 array and calls send_crq()
  508. * @evt_struct: evt_struct to be sent
  509. * @hostdata: ibmvscsi_host_data of host
  510. * @timeout: timeout in seconds - 0 means do not time command
  511. *
  512. * Returns the value returned from ibmvscsi_send_crq(). (Zero for success)
  513. * Note that this routine assumes that host_lock is held for synchronization
  514. */
  515. static int ibmvscsi_send_srp_event(struct srp_event_struct *evt_struct,
  516. struct ibmvscsi_host_data *hostdata,
  517. unsigned long timeout)
  518. {
  519. u64 *crq_as_u64 = (u64 *) &evt_struct->crq;
  520. int request_status = 0;
  521. int rc;
  522. /* If we have exhausted our request limit, just fail this request,
  523. * unless it is for a reset or abort.
  524. * Note that there are rare cases involving driver generated requests
  525. * (such as task management requests) that the mid layer may think we
  526. * can handle more requests (can_queue) when we actually can't
  527. */
  528. if (evt_struct->crq.format == VIOSRP_SRP_FORMAT) {
  529. request_status =
  530. atomic_dec_if_positive(&hostdata->request_limit);
  531. /* If request limit was -1 when we started, it is now even
  532. * less than that
  533. */
  534. if (request_status < -1)
  535. goto send_error;
  536. /* Otherwise, we may have run out of requests. */
  537. /* If request limit was 0 when we started the adapter is in the
  538. * process of performing a login with the server adapter, or
  539. * we may have run out of requests.
  540. */
  541. else if (request_status == -1 &&
  542. evt_struct->iu.srp.login_req.opcode != SRP_LOGIN_REQ)
  543. goto send_busy;
  544. /* Abort and reset calls should make it through.
  545. * Nothing except abort and reset should use the last two
  546. * slots unless we had two or less to begin with.
  547. */
  548. else if (request_status < 2 &&
  549. evt_struct->iu.srp.cmd.opcode != SRP_TSK_MGMT) {
  550. /* In the case that we have less than two requests
  551. * available, check the server limit as a combination
  552. * of the request limit and the number of requests
  553. * in-flight (the size of the send list). If the
  554. * server limit is greater than 2, return busy so
  555. * that the last two are reserved for reset and abort.
  556. */
  557. int server_limit = request_status;
  558. struct srp_event_struct *tmp_evt;
  559. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  560. server_limit++;
  561. }
  562. if (server_limit > 2)
  563. goto send_busy;
  564. }
  565. }
  566. /* Copy the IU into the transfer area */
  567. *evt_struct->xfer_iu = evt_struct->iu;
  568. evt_struct->xfer_iu->srp.rsp.tag = (u64)evt_struct;
  569. /* Add this to the sent list. We need to do this
  570. * before we actually send
  571. * in case it comes back REALLY fast
  572. */
  573. list_add_tail(&evt_struct->list, &hostdata->sent);
  574. init_timer(&evt_struct->timer);
  575. if (timeout) {
  576. evt_struct->timer.data = (unsigned long) evt_struct;
  577. evt_struct->timer.expires = jiffies + (timeout * HZ);
  578. evt_struct->timer.function = (void (*)(unsigned long))ibmvscsi_timeout;
  579. add_timer(&evt_struct->timer);
  580. }
  581. if ((rc =
  582. ibmvscsi_ops->send_crq(hostdata, crq_as_u64[0], crq_as_u64[1])) != 0) {
  583. list_del(&evt_struct->list);
  584. del_timer(&evt_struct->timer);
  585. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  586. * Firmware will send a CRQ with a transport event (0xFF) to
  587. * tell this client what has happened to the transport. This
  588. * will be handled in ibmvscsi_handle_crq()
  589. */
  590. if (rc == H_CLOSED) {
  591. dev_warn(hostdata->dev, "send warning. "
  592. "Receive queue closed, will retry.\n");
  593. goto send_busy;
  594. }
  595. dev_err(hostdata->dev, "send error %d\n", rc);
  596. atomic_inc(&hostdata->request_limit);
  597. goto send_error;
  598. }
  599. return 0;
  600. send_busy:
  601. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  602. free_event_struct(&hostdata->pool, evt_struct);
  603. if (request_status != -1)
  604. atomic_inc(&hostdata->request_limit);
  605. return SCSI_MLQUEUE_HOST_BUSY;
  606. send_error:
  607. unmap_cmd_data(&evt_struct->iu.srp.cmd, evt_struct, hostdata->dev);
  608. if (evt_struct->cmnd != NULL) {
  609. evt_struct->cmnd->result = DID_ERROR << 16;
  610. evt_struct->cmnd_done(evt_struct->cmnd);
  611. } else if (evt_struct->done)
  612. evt_struct->done(evt_struct);
  613. free_event_struct(&hostdata->pool, evt_struct);
  614. return 0;
  615. }
  616. /**
  617. * handle_cmd_rsp: - Handle responses from commands
  618. * @evt_struct: srp_event_struct to be handled
  619. *
  620. * Used as a callback by when sending scsi cmds.
  621. * Gets called by ibmvscsi_handle_crq()
  622. */
  623. static void handle_cmd_rsp(struct srp_event_struct *evt_struct)
  624. {
  625. struct srp_rsp *rsp = &evt_struct->xfer_iu->srp.rsp;
  626. struct scsi_cmnd *cmnd = evt_struct->cmnd;
  627. if (unlikely(rsp->opcode != SRP_RSP)) {
  628. if (printk_ratelimit())
  629. dev_warn(evt_struct->hostdata->dev,
  630. "bad SRP RSP type %d\n", rsp->opcode);
  631. }
  632. if (cmnd) {
  633. cmnd->result |= rsp->status;
  634. if (((cmnd->result >> 1) & 0x1f) == CHECK_CONDITION)
  635. memcpy(cmnd->sense_buffer,
  636. rsp->data,
  637. rsp->sense_data_len);
  638. unmap_cmd_data(&evt_struct->iu.srp.cmd,
  639. evt_struct,
  640. evt_struct->hostdata->dev);
  641. if (rsp->flags & SRP_RSP_FLAG_DOOVER)
  642. scsi_set_resid(cmnd, rsp->data_out_res_cnt);
  643. else if (rsp->flags & SRP_RSP_FLAG_DIOVER)
  644. scsi_set_resid(cmnd, rsp->data_in_res_cnt);
  645. }
  646. if (evt_struct->cmnd_done)
  647. evt_struct->cmnd_done(cmnd);
  648. }
  649. /**
  650. * lun_from_dev: - Returns the lun of the scsi device
  651. * @dev: struct scsi_device
  652. *
  653. */
  654. static inline u16 lun_from_dev(struct scsi_device *dev)
  655. {
  656. return (0x2 << 14) | (dev->id << 8) | (dev->channel << 5) | dev->lun;
  657. }
  658. /**
  659. * ibmvscsi_queue: - The queuecommand function of the scsi template
  660. * @cmd: struct scsi_cmnd to be executed
  661. * @done: Callback function to be called when cmd is completed
  662. */
  663. static int ibmvscsi_queuecommand(struct scsi_cmnd *cmnd,
  664. void (*done) (struct scsi_cmnd *))
  665. {
  666. struct srp_cmd *srp_cmd;
  667. struct srp_event_struct *evt_struct;
  668. struct srp_indirect_buf *indirect;
  669. struct ibmvscsi_host_data *hostdata = shost_priv(cmnd->device->host);
  670. u16 lun = lun_from_dev(cmnd->device);
  671. u8 out_fmt, in_fmt;
  672. cmnd->result = (DID_OK << 16);
  673. evt_struct = get_event_struct(&hostdata->pool);
  674. if (!evt_struct)
  675. return SCSI_MLQUEUE_HOST_BUSY;
  676. /* Set up the actual SRP IU */
  677. srp_cmd = &evt_struct->iu.srp.cmd;
  678. memset(srp_cmd, 0x00, SRP_MAX_IU_LEN);
  679. srp_cmd->opcode = SRP_CMD;
  680. memcpy(srp_cmd->cdb, cmnd->cmnd, sizeof(srp_cmd->cdb));
  681. srp_cmd->lun = ((u64) lun) << 48;
  682. if (!map_data_for_srp_cmd(cmnd, evt_struct, srp_cmd, hostdata->dev)) {
  683. if (!firmware_has_feature(FW_FEATURE_CMO))
  684. sdev_printk(KERN_ERR, cmnd->device,
  685. "couldn't convert cmd to srp_cmd\n");
  686. free_event_struct(&hostdata->pool, evt_struct);
  687. return SCSI_MLQUEUE_HOST_BUSY;
  688. }
  689. init_event_struct(evt_struct,
  690. handle_cmd_rsp,
  691. VIOSRP_SRP_FORMAT,
  692. cmnd->request->timeout/HZ);
  693. evt_struct->cmnd = cmnd;
  694. evt_struct->cmnd_done = done;
  695. /* Fix up dma address of the buffer itself */
  696. indirect = (struct srp_indirect_buf *) srp_cmd->add_data;
  697. out_fmt = srp_cmd->buf_fmt >> 4;
  698. in_fmt = srp_cmd->buf_fmt & ((1U << 4) - 1);
  699. if ((in_fmt == SRP_DATA_DESC_INDIRECT ||
  700. out_fmt == SRP_DATA_DESC_INDIRECT) &&
  701. indirect->table_desc.va == 0) {
  702. indirect->table_desc.va = evt_struct->crq.IU_data_ptr +
  703. offsetof(struct srp_cmd, add_data) +
  704. offsetof(struct srp_indirect_buf, desc_list);
  705. }
  706. return ibmvscsi_send_srp_event(evt_struct, hostdata, 0);
  707. }
  708. /* ------------------------------------------------------------
  709. * Routines for driver initialization
  710. */
  711. /**
  712. * map_persist_bufs: - Pre-map persistent data for adapter logins
  713. * @hostdata: ibmvscsi_host_data of host
  714. *
  715. * Map the capabilities and adapter info DMA buffers to avoid runtime failures.
  716. * Return 1 on error, 0 on success.
  717. */
  718. static int map_persist_bufs(struct ibmvscsi_host_data *hostdata)
  719. {
  720. hostdata->caps_addr = dma_map_single(hostdata->dev, &hostdata->caps,
  721. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  722. if (dma_mapping_error(hostdata->dev, hostdata->caps_addr)) {
  723. dev_err(hostdata->dev, "Unable to map capabilities buffer!\n");
  724. return 1;
  725. }
  726. hostdata->adapter_info_addr = dma_map_single(hostdata->dev,
  727. &hostdata->madapter_info,
  728. sizeof(hostdata->madapter_info),
  729. DMA_BIDIRECTIONAL);
  730. if (dma_mapping_error(hostdata->dev, hostdata->adapter_info_addr)) {
  731. dev_err(hostdata->dev, "Unable to map adapter info buffer!\n");
  732. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  733. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  734. return 1;
  735. }
  736. return 0;
  737. }
  738. /**
  739. * unmap_persist_bufs: - Unmap persistent data needed for adapter logins
  740. * @hostdata: ibmvscsi_host_data of host
  741. *
  742. * Unmap the capabilities and adapter info DMA buffers
  743. */
  744. static void unmap_persist_bufs(struct ibmvscsi_host_data *hostdata)
  745. {
  746. dma_unmap_single(hostdata->dev, hostdata->caps_addr,
  747. sizeof(hostdata->caps), DMA_BIDIRECTIONAL);
  748. dma_unmap_single(hostdata->dev, hostdata->adapter_info_addr,
  749. sizeof(hostdata->madapter_info), DMA_BIDIRECTIONAL);
  750. }
  751. /**
  752. * login_rsp: - Handle response to SRP login request
  753. * @evt_struct: srp_event_struct with the response
  754. *
  755. * Used as a "done" callback by when sending srp_login. Gets called
  756. * by ibmvscsi_handle_crq()
  757. */
  758. static void login_rsp(struct srp_event_struct *evt_struct)
  759. {
  760. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  761. switch (evt_struct->xfer_iu->srp.login_rsp.opcode) {
  762. case SRP_LOGIN_RSP: /* it worked! */
  763. break;
  764. case SRP_LOGIN_REJ: /* refused! */
  765. dev_info(hostdata->dev, "SRP_LOGIN_REJ reason %u\n",
  766. evt_struct->xfer_iu->srp.login_rej.reason);
  767. /* Login failed. */
  768. atomic_set(&hostdata->request_limit, -1);
  769. return;
  770. default:
  771. dev_err(hostdata->dev, "Invalid login response typecode 0x%02x!\n",
  772. evt_struct->xfer_iu->srp.login_rsp.opcode);
  773. /* Login failed. */
  774. atomic_set(&hostdata->request_limit, -1);
  775. return;
  776. }
  777. dev_info(hostdata->dev, "SRP_LOGIN succeeded\n");
  778. hostdata->client_migrated = 0;
  779. /* Now we know what the real request-limit is.
  780. * This value is set rather than added to request_limit because
  781. * request_limit could have been set to -1 by this client.
  782. */
  783. atomic_set(&hostdata->request_limit,
  784. evt_struct->xfer_iu->srp.login_rsp.req_lim_delta);
  785. /* If we had any pending I/Os, kick them */
  786. scsi_unblock_requests(hostdata->host);
  787. }
  788. /**
  789. * send_srp_login: - Sends the srp login
  790. * @hostdata: ibmvscsi_host_data of host
  791. *
  792. * Returns zero if successful.
  793. */
  794. static int send_srp_login(struct ibmvscsi_host_data *hostdata)
  795. {
  796. int rc;
  797. unsigned long flags;
  798. struct srp_login_req *login;
  799. struct srp_event_struct *evt_struct = get_event_struct(&hostdata->pool);
  800. BUG_ON(!evt_struct);
  801. init_event_struct(evt_struct, login_rsp,
  802. VIOSRP_SRP_FORMAT, login_timeout);
  803. login = &evt_struct->iu.srp.login_req;
  804. memset(login, 0, sizeof(*login));
  805. login->opcode = SRP_LOGIN_REQ;
  806. login->req_it_iu_len = sizeof(union srp_iu);
  807. login->req_buf_fmt = SRP_BUF_FORMAT_DIRECT | SRP_BUF_FORMAT_INDIRECT;
  808. spin_lock_irqsave(hostdata->host->host_lock, flags);
  809. /* Start out with a request limit of 0, since this is negotiated in
  810. * the login request we are just sending and login requests always
  811. * get sent by the driver regardless of request_limit.
  812. */
  813. atomic_set(&hostdata->request_limit, 0);
  814. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, login_timeout * 2);
  815. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  816. dev_info(hostdata->dev, "sent SRP login\n");
  817. return rc;
  818. };
  819. /**
  820. * capabilities_rsp: - Handle response to MAD adapter capabilities request
  821. * @evt_struct: srp_event_struct with the response
  822. *
  823. * Used as a "done" callback by when sending adapter_info.
  824. */
  825. static void capabilities_rsp(struct srp_event_struct *evt_struct)
  826. {
  827. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  828. if (evt_struct->xfer_iu->mad.capabilities.common.status) {
  829. dev_err(hostdata->dev, "error 0x%X getting capabilities info\n",
  830. evt_struct->xfer_iu->mad.capabilities.common.status);
  831. } else {
  832. if (hostdata->caps.migration.common.server_support != SERVER_SUPPORTS_CAP)
  833. dev_info(hostdata->dev, "Partition migration not supported\n");
  834. if (client_reserve) {
  835. if (hostdata->caps.reserve.common.server_support ==
  836. SERVER_SUPPORTS_CAP)
  837. dev_info(hostdata->dev, "Client reserve enabled\n");
  838. else
  839. dev_info(hostdata->dev, "Client reserve not supported\n");
  840. }
  841. }
  842. send_srp_login(hostdata);
  843. }
  844. /**
  845. * send_mad_capabilities: - Sends the mad capabilities request
  846. * and stores the result so it can be retrieved with
  847. * @hostdata: ibmvscsi_host_data of host
  848. */
  849. static void send_mad_capabilities(struct ibmvscsi_host_data *hostdata)
  850. {
  851. struct viosrp_capabilities *req;
  852. struct srp_event_struct *evt_struct;
  853. unsigned long flags;
  854. struct device_node *of_node = hostdata->dev->archdata.of_node;
  855. const char *location;
  856. evt_struct = get_event_struct(&hostdata->pool);
  857. BUG_ON(!evt_struct);
  858. init_event_struct(evt_struct, capabilities_rsp,
  859. VIOSRP_MAD_FORMAT, info_timeout);
  860. req = &evt_struct->iu.mad.capabilities;
  861. memset(req, 0, sizeof(*req));
  862. hostdata->caps.flags = CAP_LIST_SUPPORTED;
  863. if (hostdata->client_migrated)
  864. hostdata->caps.flags |= CLIENT_MIGRATED;
  865. strncpy(hostdata->caps.name, dev_name(&hostdata->host->shost_gendev),
  866. sizeof(hostdata->caps.name));
  867. hostdata->caps.name[sizeof(hostdata->caps.name) - 1] = '\0';
  868. location = of_get_property(of_node, "ibm,loc-code", NULL);
  869. location = location ? location : dev_name(hostdata->dev);
  870. strncpy(hostdata->caps.loc, location, sizeof(hostdata->caps.loc));
  871. hostdata->caps.loc[sizeof(hostdata->caps.loc) - 1] = '\0';
  872. req->common.type = VIOSRP_CAPABILITIES_TYPE;
  873. req->buffer = hostdata->caps_addr;
  874. hostdata->caps.migration.common.cap_type = MIGRATION_CAPABILITIES;
  875. hostdata->caps.migration.common.length = sizeof(hostdata->caps.migration);
  876. hostdata->caps.migration.common.server_support = SERVER_SUPPORTS_CAP;
  877. hostdata->caps.migration.ecl = 1;
  878. if (client_reserve) {
  879. hostdata->caps.reserve.common.cap_type = RESERVATION_CAPABILITIES;
  880. hostdata->caps.reserve.common.length = sizeof(hostdata->caps.reserve);
  881. hostdata->caps.reserve.common.server_support = SERVER_SUPPORTS_CAP;
  882. hostdata->caps.reserve.type = CLIENT_RESERVE_SCSI_2;
  883. req->common.length = sizeof(hostdata->caps);
  884. } else
  885. req->common.length = sizeof(hostdata->caps) - sizeof(hostdata->caps.reserve);
  886. spin_lock_irqsave(hostdata->host->host_lock, flags);
  887. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  888. dev_err(hostdata->dev, "couldn't send CAPABILITIES_REQ!\n");
  889. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  890. };
  891. /**
  892. * fast_fail_rsp: - Handle response to MAD enable fast fail
  893. * @evt_struct: srp_event_struct with the response
  894. *
  895. * Used as a "done" callback by when sending enable fast fail. Gets called
  896. * by ibmvscsi_handle_crq()
  897. */
  898. static void fast_fail_rsp(struct srp_event_struct *evt_struct)
  899. {
  900. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  901. u8 status = evt_struct->xfer_iu->mad.fast_fail.common.status;
  902. if (status == VIOSRP_MAD_NOT_SUPPORTED)
  903. dev_err(hostdata->dev, "fast_fail not supported in server\n");
  904. else if (status == VIOSRP_MAD_FAILED)
  905. dev_err(hostdata->dev, "fast_fail request failed\n");
  906. else if (status != VIOSRP_MAD_SUCCESS)
  907. dev_err(hostdata->dev, "error 0x%X enabling fast_fail\n", status);
  908. send_mad_capabilities(hostdata);
  909. }
  910. /**
  911. * init_host - Start host initialization
  912. * @hostdata: ibmvscsi_host_data of host
  913. *
  914. * Returns zero if successful.
  915. */
  916. static int enable_fast_fail(struct ibmvscsi_host_data *hostdata)
  917. {
  918. int rc;
  919. unsigned long flags;
  920. struct viosrp_fast_fail *fast_fail_mad;
  921. struct srp_event_struct *evt_struct;
  922. if (!fast_fail) {
  923. send_mad_capabilities(hostdata);
  924. return 0;
  925. }
  926. evt_struct = get_event_struct(&hostdata->pool);
  927. BUG_ON(!evt_struct);
  928. init_event_struct(evt_struct, fast_fail_rsp, VIOSRP_MAD_FORMAT, info_timeout);
  929. fast_fail_mad = &evt_struct->iu.mad.fast_fail;
  930. memset(fast_fail_mad, 0, sizeof(*fast_fail_mad));
  931. fast_fail_mad->common.type = VIOSRP_ENABLE_FAST_FAIL;
  932. fast_fail_mad->common.length = sizeof(*fast_fail_mad);
  933. spin_lock_irqsave(hostdata->host->host_lock, flags);
  934. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  935. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  936. return rc;
  937. }
  938. /**
  939. * adapter_info_rsp: - Handle response to MAD adapter info request
  940. * @evt_struct: srp_event_struct with the response
  941. *
  942. * Used as a "done" callback by when sending adapter_info. Gets called
  943. * by ibmvscsi_handle_crq()
  944. */
  945. static void adapter_info_rsp(struct srp_event_struct *evt_struct)
  946. {
  947. struct ibmvscsi_host_data *hostdata = evt_struct->hostdata;
  948. if (evt_struct->xfer_iu->mad.adapter_info.common.status) {
  949. dev_err(hostdata->dev, "error %d getting adapter info\n",
  950. evt_struct->xfer_iu->mad.adapter_info.common.status);
  951. } else {
  952. dev_info(hostdata->dev, "host srp version: %s, "
  953. "host partition %s (%d), OS %d, max io %u\n",
  954. hostdata->madapter_info.srp_version,
  955. hostdata->madapter_info.partition_name,
  956. hostdata->madapter_info.partition_number,
  957. hostdata->madapter_info.os_type,
  958. hostdata->madapter_info.port_max_txu[0]);
  959. if (hostdata->madapter_info.port_max_txu[0])
  960. hostdata->host->max_sectors =
  961. hostdata->madapter_info.port_max_txu[0] >> 9;
  962. if (hostdata->madapter_info.os_type == 3 &&
  963. strcmp(hostdata->madapter_info.srp_version, "1.6a") <= 0) {
  964. dev_err(hostdata->dev, "host (Ver. %s) doesn't support large transfers\n",
  965. hostdata->madapter_info.srp_version);
  966. dev_err(hostdata->dev, "limiting scatterlists to %d\n",
  967. MAX_INDIRECT_BUFS);
  968. hostdata->host->sg_tablesize = MAX_INDIRECT_BUFS;
  969. }
  970. if (hostdata->madapter_info.os_type == 3) {
  971. enable_fast_fail(hostdata);
  972. return;
  973. }
  974. }
  975. send_srp_login(hostdata);
  976. }
  977. /**
  978. * send_mad_adapter_info: - Sends the mad adapter info request
  979. * and stores the result so it can be retrieved with
  980. * sysfs. We COULD consider causing a failure if the
  981. * returned SRP version doesn't match ours.
  982. * @hostdata: ibmvscsi_host_data of host
  983. *
  984. * Returns zero if successful.
  985. */
  986. static void send_mad_adapter_info(struct ibmvscsi_host_data *hostdata)
  987. {
  988. struct viosrp_adapter_info *req;
  989. struct srp_event_struct *evt_struct;
  990. unsigned long flags;
  991. evt_struct = get_event_struct(&hostdata->pool);
  992. BUG_ON(!evt_struct);
  993. init_event_struct(evt_struct,
  994. adapter_info_rsp,
  995. VIOSRP_MAD_FORMAT,
  996. info_timeout);
  997. req = &evt_struct->iu.mad.adapter_info;
  998. memset(req, 0x00, sizeof(*req));
  999. req->common.type = VIOSRP_ADAPTER_INFO_TYPE;
  1000. req->common.length = sizeof(hostdata->madapter_info);
  1001. req->buffer = hostdata->adapter_info_addr;
  1002. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1003. if (ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2))
  1004. dev_err(hostdata->dev, "couldn't send ADAPTER_INFO_REQ!\n");
  1005. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1006. };
  1007. /**
  1008. * init_adapter: Start virtual adapter initialization sequence
  1009. *
  1010. */
  1011. static void init_adapter(struct ibmvscsi_host_data *hostdata)
  1012. {
  1013. send_mad_adapter_info(hostdata);
  1014. }
  1015. /**
  1016. * sync_completion: Signal that a synchronous command has completed
  1017. * Note that after returning from this call, the evt_struct is freed.
  1018. * the caller waiting on this completion shouldn't touch the evt_struct
  1019. * again.
  1020. */
  1021. static void sync_completion(struct srp_event_struct *evt_struct)
  1022. {
  1023. /* copy the response back */
  1024. if (evt_struct->sync_srp)
  1025. *evt_struct->sync_srp = *evt_struct->xfer_iu;
  1026. complete(&evt_struct->comp);
  1027. }
  1028. /**
  1029. * ibmvscsi_abort: Abort a command...from scsi host template
  1030. * send this over to the server and wait synchronously for the response
  1031. */
  1032. static int ibmvscsi_eh_abort_handler(struct scsi_cmnd *cmd)
  1033. {
  1034. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1035. struct srp_tsk_mgmt *tsk_mgmt;
  1036. struct srp_event_struct *evt;
  1037. struct srp_event_struct *tmp_evt, *found_evt;
  1038. union viosrp_iu srp_rsp;
  1039. int rsp_rc;
  1040. unsigned long flags;
  1041. u16 lun = lun_from_dev(cmd->device);
  1042. unsigned long wait_switch = 0;
  1043. /* First, find this command in our sent list so we can figure
  1044. * out the correct tag
  1045. */
  1046. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1047. wait_switch = jiffies + (init_timeout * HZ);
  1048. do {
  1049. found_evt = NULL;
  1050. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1051. if (tmp_evt->cmnd == cmd) {
  1052. found_evt = tmp_evt;
  1053. break;
  1054. }
  1055. }
  1056. if (!found_evt) {
  1057. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1058. return SUCCESS;
  1059. }
  1060. evt = get_event_struct(&hostdata->pool);
  1061. if (evt == NULL) {
  1062. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1063. sdev_printk(KERN_ERR, cmd->device,
  1064. "failed to allocate abort event\n");
  1065. return FAILED;
  1066. }
  1067. init_event_struct(evt,
  1068. sync_completion,
  1069. VIOSRP_SRP_FORMAT,
  1070. abort_timeout);
  1071. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1072. /* Set up an abort SRP command */
  1073. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1074. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1075. tsk_mgmt->lun = ((u64) lun) << 48;
  1076. tsk_mgmt->tsk_mgmt_func = SRP_TSK_ABORT_TASK;
  1077. tsk_mgmt->task_tag = (u64) found_evt;
  1078. evt->sync_srp = &srp_rsp;
  1079. init_completion(&evt->comp);
  1080. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, abort_timeout * 2);
  1081. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1082. break;
  1083. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1084. msleep(10);
  1085. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1086. } while (time_before(jiffies, wait_switch));
  1087. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1088. if (rsp_rc != 0) {
  1089. sdev_printk(KERN_ERR, cmd->device,
  1090. "failed to send abort() event. rc=%d\n", rsp_rc);
  1091. return FAILED;
  1092. }
  1093. sdev_printk(KERN_INFO, cmd->device,
  1094. "aborting command. lun 0x%llx, tag 0x%llx\n",
  1095. (((u64) lun) << 48), (u64) found_evt);
  1096. wait_for_completion(&evt->comp);
  1097. /* make sure we got a good response */
  1098. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1099. if (printk_ratelimit())
  1100. sdev_printk(KERN_WARNING, cmd->device, "abort bad SRP RSP type %d\n",
  1101. srp_rsp.srp.rsp.opcode);
  1102. return FAILED;
  1103. }
  1104. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1105. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1106. else
  1107. rsp_rc = srp_rsp.srp.rsp.status;
  1108. if (rsp_rc) {
  1109. if (printk_ratelimit())
  1110. sdev_printk(KERN_WARNING, cmd->device,
  1111. "abort code %d for task tag 0x%llx\n",
  1112. rsp_rc, tsk_mgmt->task_tag);
  1113. return FAILED;
  1114. }
  1115. /* Because we dropped the spinlock above, it's possible
  1116. * The event is no longer in our list. Make sure it didn't
  1117. * complete while we were aborting
  1118. */
  1119. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1120. found_evt = NULL;
  1121. list_for_each_entry(tmp_evt, &hostdata->sent, list) {
  1122. if (tmp_evt->cmnd == cmd) {
  1123. found_evt = tmp_evt;
  1124. break;
  1125. }
  1126. }
  1127. if (found_evt == NULL) {
  1128. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1129. sdev_printk(KERN_INFO, cmd->device, "aborted task tag 0x%llx completed\n",
  1130. tsk_mgmt->task_tag);
  1131. return SUCCESS;
  1132. }
  1133. sdev_printk(KERN_INFO, cmd->device, "successfully aborted task tag 0x%llx\n",
  1134. tsk_mgmt->task_tag);
  1135. cmd->result = (DID_ABORT << 16);
  1136. list_del(&found_evt->list);
  1137. unmap_cmd_data(&found_evt->iu.srp.cmd, found_evt,
  1138. found_evt->hostdata->dev);
  1139. free_event_struct(&found_evt->hostdata->pool, found_evt);
  1140. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1141. atomic_inc(&hostdata->request_limit);
  1142. return SUCCESS;
  1143. }
  1144. /**
  1145. * ibmvscsi_eh_device_reset_handler: Reset a single LUN...from scsi host
  1146. * template send this over to the server and wait synchronously for the
  1147. * response
  1148. */
  1149. static int ibmvscsi_eh_device_reset_handler(struct scsi_cmnd *cmd)
  1150. {
  1151. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1152. struct srp_tsk_mgmt *tsk_mgmt;
  1153. struct srp_event_struct *evt;
  1154. struct srp_event_struct *tmp_evt, *pos;
  1155. union viosrp_iu srp_rsp;
  1156. int rsp_rc;
  1157. unsigned long flags;
  1158. u16 lun = lun_from_dev(cmd->device);
  1159. unsigned long wait_switch = 0;
  1160. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1161. wait_switch = jiffies + (init_timeout * HZ);
  1162. do {
  1163. evt = get_event_struct(&hostdata->pool);
  1164. if (evt == NULL) {
  1165. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1166. sdev_printk(KERN_ERR, cmd->device,
  1167. "failed to allocate reset event\n");
  1168. return FAILED;
  1169. }
  1170. init_event_struct(evt,
  1171. sync_completion,
  1172. VIOSRP_SRP_FORMAT,
  1173. reset_timeout);
  1174. tsk_mgmt = &evt->iu.srp.tsk_mgmt;
  1175. /* Set up a lun reset SRP command */
  1176. memset(tsk_mgmt, 0x00, sizeof(*tsk_mgmt));
  1177. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1178. tsk_mgmt->lun = ((u64) lun) << 48;
  1179. tsk_mgmt->tsk_mgmt_func = SRP_TSK_LUN_RESET;
  1180. evt->sync_srp = &srp_rsp;
  1181. init_completion(&evt->comp);
  1182. rsp_rc = ibmvscsi_send_srp_event(evt, hostdata, reset_timeout * 2);
  1183. if (rsp_rc != SCSI_MLQUEUE_HOST_BUSY)
  1184. break;
  1185. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1186. msleep(10);
  1187. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1188. } while (time_before(jiffies, wait_switch));
  1189. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1190. if (rsp_rc != 0) {
  1191. sdev_printk(KERN_ERR, cmd->device,
  1192. "failed to send reset event. rc=%d\n", rsp_rc);
  1193. return FAILED;
  1194. }
  1195. sdev_printk(KERN_INFO, cmd->device, "resetting device. lun 0x%llx\n",
  1196. (((u64) lun) << 48));
  1197. wait_for_completion(&evt->comp);
  1198. /* make sure we got a good response */
  1199. if (unlikely(srp_rsp.srp.rsp.opcode != SRP_RSP)) {
  1200. if (printk_ratelimit())
  1201. sdev_printk(KERN_WARNING, cmd->device, "reset bad SRP RSP type %d\n",
  1202. srp_rsp.srp.rsp.opcode);
  1203. return FAILED;
  1204. }
  1205. if (srp_rsp.srp.rsp.flags & SRP_RSP_FLAG_RSPVALID)
  1206. rsp_rc = *((int *)srp_rsp.srp.rsp.data);
  1207. else
  1208. rsp_rc = srp_rsp.srp.rsp.status;
  1209. if (rsp_rc) {
  1210. if (printk_ratelimit())
  1211. sdev_printk(KERN_WARNING, cmd->device,
  1212. "reset code %d for task tag 0x%llx\n",
  1213. rsp_rc, tsk_mgmt->task_tag);
  1214. return FAILED;
  1215. }
  1216. /* We need to find all commands for this LUN that have not yet been
  1217. * responded to, and fail them with DID_RESET
  1218. */
  1219. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1220. list_for_each_entry_safe(tmp_evt, pos, &hostdata->sent, list) {
  1221. if ((tmp_evt->cmnd) && (tmp_evt->cmnd->device == cmd->device)) {
  1222. if (tmp_evt->cmnd)
  1223. tmp_evt->cmnd->result = (DID_RESET << 16);
  1224. list_del(&tmp_evt->list);
  1225. unmap_cmd_data(&tmp_evt->iu.srp.cmd, tmp_evt,
  1226. tmp_evt->hostdata->dev);
  1227. free_event_struct(&tmp_evt->hostdata->pool,
  1228. tmp_evt);
  1229. atomic_inc(&hostdata->request_limit);
  1230. if (tmp_evt->cmnd_done)
  1231. tmp_evt->cmnd_done(tmp_evt->cmnd);
  1232. else if (tmp_evt->done)
  1233. tmp_evt->done(tmp_evt);
  1234. }
  1235. }
  1236. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1237. return SUCCESS;
  1238. }
  1239. /**
  1240. * ibmvscsi_eh_host_reset_handler - Reset the connection to the server
  1241. * @cmd: struct scsi_cmnd having problems
  1242. */
  1243. static int ibmvscsi_eh_host_reset_handler(struct scsi_cmnd *cmd)
  1244. {
  1245. unsigned long wait_switch = 0;
  1246. struct ibmvscsi_host_data *hostdata = shost_priv(cmd->device->host);
  1247. dev_err(hostdata->dev, "Resetting connection due to error recovery\n");
  1248. ibmvscsi_reset_host(hostdata);
  1249. for (wait_switch = jiffies + (init_timeout * HZ);
  1250. time_before(jiffies, wait_switch) &&
  1251. atomic_read(&hostdata->request_limit) < 2;) {
  1252. msleep(10);
  1253. }
  1254. if (atomic_read(&hostdata->request_limit) <= 0)
  1255. return FAILED;
  1256. return SUCCESS;
  1257. }
  1258. /**
  1259. * ibmvscsi_handle_crq: - Handles and frees received events in the CRQ
  1260. * @crq: Command/Response queue
  1261. * @hostdata: ibmvscsi_host_data of host
  1262. *
  1263. */
  1264. void ibmvscsi_handle_crq(struct viosrp_crq *crq,
  1265. struct ibmvscsi_host_data *hostdata)
  1266. {
  1267. long rc;
  1268. unsigned long flags;
  1269. struct srp_event_struct *evt_struct =
  1270. (struct srp_event_struct *)crq->IU_data_ptr;
  1271. switch (crq->valid) {
  1272. case 0xC0: /* initialization */
  1273. switch (crq->format) {
  1274. case 0x01: /* Initialization message */
  1275. dev_info(hostdata->dev, "partner initialized\n");
  1276. /* Send back a response */
  1277. if ((rc = ibmvscsi_ops->send_crq(hostdata,
  1278. 0xC002000000000000LL, 0)) == 0) {
  1279. /* Now login */
  1280. init_adapter(hostdata);
  1281. } else {
  1282. dev_err(hostdata->dev, "Unable to send init rsp. rc=%ld\n", rc);
  1283. }
  1284. break;
  1285. case 0x02: /* Initialization response */
  1286. dev_info(hostdata->dev, "partner initialization complete\n");
  1287. /* Now login */
  1288. init_adapter(hostdata);
  1289. break;
  1290. default:
  1291. dev_err(hostdata->dev, "unknown crq message type: %d\n", crq->format);
  1292. }
  1293. return;
  1294. case 0xFF: /* Hypervisor telling us the connection is closed */
  1295. scsi_block_requests(hostdata->host);
  1296. atomic_set(&hostdata->request_limit, 0);
  1297. if (crq->format == 0x06) {
  1298. /* We need to re-setup the interpartition connection */
  1299. dev_info(hostdata->dev, "Re-enabling adapter!\n");
  1300. hostdata->client_migrated = 1;
  1301. purge_requests(hostdata, DID_REQUEUE);
  1302. if ((ibmvscsi_ops->reenable_crq_queue(&hostdata->queue,
  1303. hostdata)) ||
  1304. (ibmvscsi_ops->send_crq(hostdata,
  1305. 0xC001000000000000LL, 0))) {
  1306. atomic_set(&hostdata->request_limit,
  1307. -1);
  1308. dev_err(hostdata->dev, "error after enable\n");
  1309. }
  1310. } else {
  1311. dev_err(hostdata->dev, "Virtual adapter failed rc %d!\n",
  1312. crq->format);
  1313. purge_requests(hostdata, DID_ERROR);
  1314. if ((ibmvscsi_ops->reset_crq_queue(&hostdata->queue,
  1315. hostdata)) ||
  1316. (ibmvscsi_ops->send_crq(hostdata,
  1317. 0xC001000000000000LL, 0))) {
  1318. atomic_set(&hostdata->request_limit,
  1319. -1);
  1320. dev_err(hostdata->dev, "error after reset\n");
  1321. }
  1322. }
  1323. scsi_unblock_requests(hostdata->host);
  1324. return;
  1325. case 0x80: /* real payload */
  1326. break;
  1327. default:
  1328. dev_err(hostdata->dev, "got an invalid message type 0x%02x\n",
  1329. crq->valid);
  1330. return;
  1331. }
  1332. /* The only kind of payload CRQs we should get are responses to
  1333. * things we send. Make sure this response is to something we
  1334. * actually sent
  1335. */
  1336. if (!valid_event_struct(&hostdata->pool, evt_struct)) {
  1337. dev_err(hostdata->dev, "returned correlation_token 0x%p is invalid!\n",
  1338. (void *)crq->IU_data_ptr);
  1339. return;
  1340. }
  1341. if (atomic_read(&evt_struct->free)) {
  1342. dev_err(hostdata->dev, "received duplicate correlation_token 0x%p!\n",
  1343. (void *)crq->IU_data_ptr);
  1344. return;
  1345. }
  1346. if (crq->format == VIOSRP_SRP_FORMAT)
  1347. atomic_add(evt_struct->xfer_iu->srp.rsp.req_lim_delta,
  1348. &hostdata->request_limit);
  1349. del_timer(&evt_struct->timer);
  1350. if ((crq->status != VIOSRP_OK && crq->status != VIOSRP_OK2) && evt_struct->cmnd)
  1351. evt_struct->cmnd->result = DID_ERROR << 16;
  1352. if (evt_struct->done)
  1353. evt_struct->done(evt_struct);
  1354. else
  1355. dev_err(hostdata->dev, "returned done() is NULL; not running it!\n");
  1356. /*
  1357. * Lock the host_lock before messing with these structures, since we
  1358. * are running in a task context
  1359. */
  1360. spin_lock_irqsave(evt_struct->hostdata->host->host_lock, flags);
  1361. list_del(&evt_struct->list);
  1362. free_event_struct(&evt_struct->hostdata->pool, evt_struct);
  1363. spin_unlock_irqrestore(evt_struct->hostdata->host->host_lock, flags);
  1364. }
  1365. /**
  1366. * ibmvscsi_get_host_config: Send the command to the server to get host
  1367. * configuration data. The data is opaque to us.
  1368. */
  1369. static int ibmvscsi_do_host_config(struct ibmvscsi_host_data *hostdata,
  1370. unsigned char *buffer, int length)
  1371. {
  1372. struct viosrp_host_config *host_config;
  1373. struct srp_event_struct *evt_struct;
  1374. unsigned long flags;
  1375. dma_addr_t addr;
  1376. int rc;
  1377. evt_struct = get_event_struct(&hostdata->pool);
  1378. if (!evt_struct) {
  1379. dev_err(hostdata->dev, "couldn't allocate event for HOST_CONFIG!\n");
  1380. return -1;
  1381. }
  1382. init_event_struct(evt_struct,
  1383. sync_completion,
  1384. VIOSRP_MAD_FORMAT,
  1385. info_timeout);
  1386. host_config = &evt_struct->iu.mad.host_config;
  1387. /* Set up a lun reset SRP command */
  1388. memset(host_config, 0x00, sizeof(*host_config));
  1389. host_config->common.type = VIOSRP_HOST_CONFIG_TYPE;
  1390. host_config->common.length = length;
  1391. host_config->buffer = addr = dma_map_single(hostdata->dev, buffer,
  1392. length,
  1393. DMA_BIDIRECTIONAL);
  1394. if (dma_mapping_error(hostdata->dev, host_config->buffer)) {
  1395. if (!firmware_has_feature(FW_FEATURE_CMO))
  1396. dev_err(hostdata->dev,
  1397. "dma_mapping error getting host config\n");
  1398. free_event_struct(&hostdata->pool, evt_struct);
  1399. return -1;
  1400. }
  1401. init_completion(&evt_struct->comp);
  1402. spin_lock_irqsave(hostdata->host->host_lock, flags);
  1403. rc = ibmvscsi_send_srp_event(evt_struct, hostdata, info_timeout * 2);
  1404. spin_unlock_irqrestore(hostdata->host->host_lock, flags);
  1405. if (rc == 0)
  1406. wait_for_completion(&evt_struct->comp);
  1407. dma_unmap_single(hostdata->dev, addr, length, DMA_BIDIRECTIONAL);
  1408. return rc;
  1409. }
  1410. /**
  1411. * ibmvscsi_slave_configure: Set the "allow_restart" flag for each disk.
  1412. * @sdev: struct scsi_device device to configure
  1413. *
  1414. * Enable allow_restart for a device if it is a disk. Adjust the
  1415. * queue_depth here also as is required by the documentation for
  1416. * struct scsi_host_template.
  1417. */
  1418. static int ibmvscsi_slave_configure(struct scsi_device *sdev)
  1419. {
  1420. struct Scsi_Host *shost = sdev->host;
  1421. unsigned long lock_flags = 0;
  1422. spin_lock_irqsave(shost->host_lock, lock_flags);
  1423. if (sdev->type == TYPE_DISK) {
  1424. sdev->allow_restart = 1;
  1425. blk_queue_rq_timeout(sdev->request_queue, 120 * HZ);
  1426. }
  1427. scsi_adjust_queue_depth(sdev, 0, shost->cmd_per_lun);
  1428. spin_unlock_irqrestore(shost->host_lock, lock_flags);
  1429. return 0;
  1430. }
  1431. /**
  1432. * ibmvscsi_change_queue_depth - Change the device's queue depth
  1433. * @sdev: scsi device struct
  1434. * @qdepth: depth to set
  1435. * @reason: calling context
  1436. *
  1437. * Return value:
  1438. * actual depth set
  1439. **/
  1440. static int ibmvscsi_change_queue_depth(struct scsi_device *sdev, int qdepth,
  1441. int reason)
  1442. {
  1443. if (reason != SCSI_QDEPTH_DEFAULT)
  1444. return -EOPNOTSUPP;
  1445. if (qdepth > IBMVSCSI_MAX_CMDS_PER_LUN)
  1446. qdepth = IBMVSCSI_MAX_CMDS_PER_LUN;
  1447. scsi_adjust_queue_depth(sdev, 0, qdepth);
  1448. return sdev->queue_depth;
  1449. }
  1450. /* ------------------------------------------------------------
  1451. * sysfs attributes
  1452. */
  1453. static ssize_t show_host_vhost_loc(struct device *dev,
  1454. struct device_attribute *attr, char *buf)
  1455. {
  1456. struct Scsi_Host *shost = class_to_shost(dev);
  1457. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1458. int len;
  1459. len = snprintf(buf, sizeof(hostdata->caps.loc), "%s\n",
  1460. hostdata->caps.loc);
  1461. return len;
  1462. }
  1463. static struct device_attribute ibmvscsi_host_vhost_loc = {
  1464. .attr = {
  1465. .name = "vhost_loc",
  1466. .mode = S_IRUGO,
  1467. },
  1468. .show = show_host_vhost_loc,
  1469. };
  1470. static ssize_t show_host_vhost_name(struct device *dev,
  1471. struct device_attribute *attr, char *buf)
  1472. {
  1473. struct Scsi_Host *shost = class_to_shost(dev);
  1474. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1475. int len;
  1476. len = snprintf(buf, sizeof(hostdata->caps.name), "%s\n",
  1477. hostdata->caps.name);
  1478. return len;
  1479. }
  1480. static struct device_attribute ibmvscsi_host_vhost_name = {
  1481. .attr = {
  1482. .name = "vhost_name",
  1483. .mode = S_IRUGO,
  1484. },
  1485. .show = show_host_vhost_name,
  1486. };
  1487. static ssize_t show_host_srp_version(struct device *dev,
  1488. struct device_attribute *attr, char *buf)
  1489. {
  1490. struct Scsi_Host *shost = class_to_shost(dev);
  1491. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1492. int len;
  1493. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1494. hostdata->madapter_info.srp_version);
  1495. return len;
  1496. }
  1497. static struct device_attribute ibmvscsi_host_srp_version = {
  1498. .attr = {
  1499. .name = "srp_version",
  1500. .mode = S_IRUGO,
  1501. },
  1502. .show = show_host_srp_version,
  1503. };
  1504. static ssize_t show_host_partition_name(struct device *dev,
  1505. struct device_attribute *attr,
  1506. char *buf)
  1507. {
  1508. struct Scsi_Host *shost = class_to_shost(dev);
  1509. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1510. int len;
  1511. len = snprintf(buf, PAGE_SIZE, "%s\n",
  1512. hostdata->madapter_info.partition_name);
  1513. return len;
  1514. }
  1515. static struct device_attribute ibmvscsi_host_partition_name = {
  1516. .attr = {
  1517. .name = "partition_name",
  1518. .mode = S_IRUGO,
  1519. },
  1520. .show = show_host_partition_name,
  1521. };
  1522. static ssize_t show_host_partition_number(struct device *dev,
  1523. struct device_attribute *attr,
  1524. char *buf)
  1525. {
  1526. struct Scsi_Host *shost = class_to_shost(dev);
  1527. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1528. int len;
  1529. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1530. hostdata->madapter_info.partition_number);
  1531. return len;
  1532. }
  1533. static struct device_attribute ibmvscsi_host_partition_number = {
  1534. .attr = {
  1535. .name = "partition_number",
  1536. .mode = S_IRUGO,
  1537. },
  1538. .show = show_host_partition_number,
  1539. };
  1540. static ssize_t show_host_mad_version(struct device *dev,
  1541. struct device_attribute *attr, char *buf)
  1542. {
  1543. struct Scsi_Host *shost = class_to_shost(dev);
  1544. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1545. int len;
  1546. len = snprintf(buf, PAGE_SIZE, "%d\n",
  1547. hostdata->madapter_info.mad_version);
  1548. return len;
  1549. }
  1550. static struct device_attribute ibmvscsi_host_mad_version = {
  1551. .attr = {
  1552. .name = "mad_version",
  1553. .mode = S_IRUGO,
  1554. },
  1555. .show = show_host_mad_version,
  1556. };
  1557. static ssize_t show_host_os_type(struct device *dev,
  1558. struct device_attribute *attr, char *buf)
  1559. {
  1560. struct Scsi_Host *shost = class_to_shost(dev);
  1561. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1562. int len;
  1563. len = snprintf(buf, PAGE_SIZE, "%d\n", hostdata->madapter_info.os_type);
  1564. return len;
  1565. }
  1566. static struct device_attribute ibmvscsi_host_os_type = {
  1567. .attr = {
  1568. .name = "os_type",
  1569. .mode = S_IRUGO,
  1570. },
  1571. .show = show_host_os_type,
  1572. };
  1573. static ssize_t show_host_config(struct device *dev,
  1574. struct device_attribute *attr, char *buf)
  1575. {
  1576. struct Scsi_Host *shost = class_to_shost(dev);
  1577. struct ibmvscsi_host_data *hostdata = shost_priv(shost);
  1578. /* returns null-terminated host config data */
  1579. if (ibmvscsi_do_host_config(hostdata, buf, PAGE_SIZE) == 0)
  1580. return strlen(buf);
  1581. else
  1582. return 0;
  1583. }
  1584. static struct device_attribute ibmvscsi_host_config = {
  1585. .attr = {
  1586. .name = "config",
  1587. .mode = S_IRUGO,
  1588. },
  1589. .show = show_host_config,
  1590. };
  1591. static struct device_attribute *ibmvscsi_attrs[] = {
  1592. &ibmvscsi_host_vhost_loc,
  1593. &ibmvscsi_host_vhost_name,
  1594. &ibmvscsi_host_srp_version,
  1595. &ibmvscsi_host_partition_name,
  1596. &ibmvscsi_host_partition_number,
  1597. &ibmvscsi_host_mad_version,
  1598. &ibmvscsi_host_os_type,
  1599. &ibmvscsi_host_config,
  1600. NULL
  1601. };
  1602. /* ------------------------------------------------------------
  1603. * SCSI driver registration
  1604. */
  1605. static struct scsi_host_template driver_template = {
  1606. .module = THIS_MODULE,
  1607. .name = "IBM POWER Virtual SCSI Adapter " IBMVSCSI_VERSION,
  1608. .proc_name = "ibmvscsi",
  1609. .queuecommand = ibmvscsi_queuecommand,
  1610. .eh_abort_handler = ibmvscsi_eh_abort_handler,
  1611. .eh_device_reset_handler = ibmvscsi_eh_device_reset_handler,
  1612. .eh_host_reset_handler = ibmvscsi_eh_host_reset_handler,
  1613. .slave_configure = ibmvscsi_slave_configure,
  1614. .change_queue_depth = ibmvscsi_change_queue_depth,
  1615. .cmd_per_lun = IBMVSCSI_CMDS_PER_LUN_DEFAULT,
  1616. .can_queue = IBMVSCSI_MAX_REQUESTS_DEFAULT,
  1617. .this_id = -1,
  1618. .sg_tablesize = SG_ALL,
  1619. .use_clustering = ENABLE_CLUSTERING,
  1620. .shost_attrs = ibmvscsi_attrs,
  1621. };
  1622. /**
  1623. * ibmvscsi_get_desired_dma - Calculate IO memory desired by the driver
  1624. *
  1625. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1626. *
  1627. * Return value:
  1628. * Number of bytes of IO data the driver will need to perform well.
  1629. */
  1630. static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  1631. {
  1632. /* iu_storage data allocated in initialize_event_pool */
  1633. unsigned long desired_io = max_events * sizeof(union viosrp_iu);
  1634. /* add io space for sg data */
  1635. desired_io += (IBMVSCSI_MAX_SECTORS_DEFAULT * 512 *
  1636. IBMVSCSI_CMDS_PER_LUN_DEFAULT);
  1637. return desired_io;
  1638. }
  1639. /**
  1640. * Called by bus code for each adapter
  1641. */
  1642. static int ibmvscsi_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1643. {
  1644. struct ibmvscsi_host_data *hostdata;
  1645. struct Scsi_Host *host;
  1646. struct device *dev = &vdev->dev;
  1647. struct srp_rport_identifiers ids;
  1648. struct srp_rport *rport;
  1649. unsigned long wait_switch = 0;
  1650. int rc;
  1651. dev_set_drvdata(&vdev->dev, NULL);
  1652. host = scsi_host_alloc(&driver_template, sizeof(*hostdata));
  1653. if (!host) {
  1654. dev_err(&vdev->dev, "couldn't allocate host data\n");
  1655. goto scsi_host_alloc_failed;
  1656. }
  1657. host->transportt = ibmvscsi_transport_template;
  1658. hostdata = shost_priv(host);
  1659. memset(hostdata, 0x00, sizeof(*hostdata));
  1660. INIT_LIST_HEAD(&hostdata->sent);
  1661. hostdata->host = host;
  1662. hostdata->dev = dev;
  1663. atomic_set(&hostdata->request_limit, -1);
  1664. hostdata->host->max_sectors = IBMVSCSI_MAX_SECTORS_DEFAULT;
  1665. if (map_persist_bufs(hostdata)) {
  1666. dev_err(&vdev->dev, "couldn't map persistent buffers\n");
  1667. goto persist_bufs_failed;
  1668. }
  1669. rc = ibmvscsi_ops->init_crq_queue(&hostdata->queue, hostdata, max_events);
  1670. if (rc != 0 && rc != H_RESOURCE) {
  1671. dev_err(&vdev->dev, "couldn't initialize crq. rc=%d\n", rc);
  1672. goto init_crq_failed;
  1673. }
  1674. if (initialize_event_pool(&hostdata->pool, max_events, hostdata) != 0) {
  1675. dev_err(&vdev->dev, "couldn't initialize event pool\n");
  1676. goto init_pool_failed;
  1677. }
  1678. host->max_lun = 8;
  1679. host->max_id = max_id;
  1680. host->max_channel = max_channel;
  1681. host->max_cmd_len = 16;
  1682. if (scsi_add_host(hostdata->host, hostdata->dev))
  1683. goto add_host_failed;
  1684. /* we don't have a proper target_port_id so let's use the fake one */
  1685. memcpy(ids.port_id, hostdata->madapter_info.partition_name,
  1686. sizeof(ids.port_id));
  1687. ids.roles = SRP_RPORT_ROLE_TARGET;
  1688. rport = srp_rport_add(host, &ids);
  1689. if (IS_ERR(rport))
  1690. goto add_srp_port_failed;
  1691. /* Try to send an initialization message. Note that this is allowed
  1692. * to fail if the other end is not acive. In that case we don't
  1693. * want to scan
  1694. */
  1695. if (ibmvscsi_ops->send_crq(hostdata, 0xC001000000000000LL, 0) == 0
  1696. || rc == H_RESOURCE) {
  1697. /*
  1698. * Wait around max init_timeout secs for the adapter to finish
  1699. * initializing. When we are done initializing, we will have a
  1700. * valid request_limit. We don't want Linux scanning before
  1701. * we are ready.
  1702. */
  1703. for (wait_switch = jiffies + (init_timeout * HZ);
  1704. time_before(jiffies, wait_switch) &&
  1705. atomic_read(&hostdata->request_limit) < 2;) {
  1706. msleep(10);
  1707. }
  1708. /* if we now have a valid request_limit, initiate a scan */
  1709. if (atomic_read(&hostdata->request_limit) > 0)
  1710. scsi_scan_host(host);
  1711. }
  1712. dev_set_drvdata(&vdev->dev, hostdata);
  1713. return 0;
  1714. add_srp_port_failed:
  1715. scsi_remove_host(hostdata->host);
  1716. add_host_failed:
  1717. release_event_pool(&hostdata->pool, hostdata);
  1718. init_pool_failed:
  1719. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata, max_events);
  1720. init_crq_failed:
  1721. unmap_persist_bufs(hostdata);
  1722. persist_bufs_failed:
  1723. scsi_host_put(host);
  1724. scsi_host_alloc_failed:
  1725. return -1;
  1726. }
  1727. static int ibmvscsi_remove(struct vio_dev *vdev)
  1728. {
  1729. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(&vdev->dev);
  1730. unmap_persist_bufs(hostdata);
  1731. release_event_pool(&hostdata->pool, hostdata);
  1732. ibmvscsi_ops->release_crq_queue(&hostdata->queue, hostdata,
  1733. max_events);
  1734. srp_remove_host(hostdata->host);
  1735. scsi_remove_host(hostdata->host);
  1736. scsi_host_put(hostdata->host);
  1737. return 0;
  1738. }
  1739. /**
  1740. * ibmvscsi_resume: Resume from suspend
  1741. * @dev: device struct
  1742. *
  1743. * We may have lost an interrupt across suspend/resume, so kick the
  1744. * interrupt handler
  1745. */
  1746. static int ibmvscsi_resume(struct device *dev)
  1747. {
  1748. struct ibmvscsi_host_data *hostdata = dev_get_drvdata(dev);
  1749. return ibmvscsi_ops->resume(hostdata);
  1750. }
  1751. /**
  1752. * ibmvscsi_device_table: Used by vio.c to match devices in the device tree we
  1753. * support.
  1754. */
  1755. static struct vio_device_id ibmvscsi_device_table[] __devinitdata = {
  1756. {"vscsi", "IBM,v-scsi"},
  1757. { "", "" }
  1758. };
  1759. MODULE_DEVICE_TABLE(vio, ibmvscsi_device_table);
  1760. static struct dev_pm_ops ibmvscsi_pm_ops = {
  1761. .resume = ibmvscsi_resume
  1762. };
  1763. static struct vio_driver ibmvscsi_driver = {
  1764. .id_table = ibmvscsi_device_table,
  1765. .probe = ibmvscsi_probe,
  1766. .remove = ibmvscsi_remove,
  1767. .get_desired_dma = ibmvscsi_get_desired_dma,
  1768. .driver = {
  1769. .name = "ibmvscsi",
  1770. .owner = THIS_MODULE,
  1771. .pm = &ibmvscsi_pm_ops,
  1772. }
  1773. };
  1774. static struct srp_function_template ibmvscsi_transport_functions = {
  1775. };
  1776. int __init ibmvscsi_module_init(void)
  1777. {
  1778. int ret;
  1779. /* Ensure we have two requests to do error recovery */
  1780. driver_template.can_queue = max_requests;
  1781. max_events = max_requests + 2;
  1782. if (firmware_has_feature(FW_FEATURE_ISERIES))
  1783. ibmvscsi_ops = &iseriesvscsi_ops;
  1784. else if (firmware_has_feature(FW_FEATURE_VIO))
  1785. ibmvscsi_ops = &rpavscsi_ops;
  1786. else
  1787. return -ENODEV;
  1788. ibmvscsi_transport_template =
  1789. srp_attach_transport(&ibmvscsi_transport_functions);
  1790. if (!ibmvscsi_transport_template)
  1791. return -ENOMEM;
  1792. ret = vio_register_driver(&ibmvscsi_driver);
  1793. if (ret)
  1794. srp_release_transport(ibmvscsi_transport_template);
  1795. return ret;
  1796. }
  1797. void __exit ibmvscsi_module_exit(void)
  1798. {
  1799. vio_unregister_driver(&ibmvscsi_driver);
  1800. srp_release_transport(ibmvscsi_transport_template);
  1801. }
  1802. module_init(ibmvscsi_module_init);
  1803. module_exit(ibmvscsi_module_exit);