ibmvscsi.c 70 KB

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